mirror of
https://github.com/Lissy93/web-check.git
synced 2024-11-30 04:05:29 +01:00
39 lines
952 B
JavaScript
39 lines
952 B
JavaScript
const normalizeUrl = (url) => {
|
|
return url.startsWith('http') ? url : `https://${url}`;
|
|
};
|
|
|
|
const commonMiddleware = (handler) => {
|
|
return async (event, context, callback) => {
|
|
|
|
const rawUrl = (event.queryStringParameters || event.query).url;
|
|
|
|
if (!rawUrl) {
|
|
callback(null, {
|
|
statusCode: 500,
|
|
body: JSON.stringify({ error: 'No URL specified' }),
|
|
});
|
|
}
|
|
|
|
const url = normalizeUrl(rawUrl);
|
|
|
|
try {
|
|
const response = await handler(url, event, context);
|
|
if (response.body && response.statusCode) {
|
|
callback(null, response);
|
|
} else {
|
|
callback(null, {
|
|
statusCode: 200,
|
|
body: typeof response === 'object' ? JSON.stringify(response) : response,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
callback(null, {
|
|
statusCode: 500,
|
|
body: JSON.stringify({ error: error.message }),
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
module.exports = commonMiddleware;
|