mirror of
https://github.com/Lissy93/web-check.git
synced 2024-11-26 10:13:27 +01:00
39 lines
988 B
JavaScript
39 lines
988 B
JavaScript
const normalizeUrl = (url) => {
|
|
return url.startsWith('http') ? url : `https://${url}`;
|
|
};
|
|
|
|
const commonMiddleware = (handler) => {
|
|
return async (event, context, callback) => {
|
|
const queryParams = event.queryStringParameters || event.query || {};
|
|
const rawUrl = queryParams.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;
|