Updates all API endpoints to use common middleware

This commit is contained in:
Alicia Sykes
2023-08-09 22:33:36 +01:00
parent 85af5f9327
commit d2a56eb526
21 changed files with 264 additions and 550 deletions

View File

@ -1,10 +1,8 @@
const axios = require('axios');
const xml2js = require('xml2js');
const middleware = require('./_common/middleware');
exports.handler = async (event) => {
const url = (event.queryStringParameters || event.query).url;
// const baseUrl = event.queryStringParameters.url.replace(/^(?:https?:\/\/)?/i, "");
// const url = baseUrl.startsWith('http') ? baseUrl : `http://${baseUrl}`;
const fetchSitemapHandler = async (url) => {
let sitemapUrl;
try {
@ -19,24 +17,17 @@ exports.handler = async (event) => {
}
if (!sitemapUrl) {
return {
statusCode: 404,
body: JSON.stringify({ error: 'Sitemap not found in robots.txt' }),
};
throw new Error('Sitemap not found in robots.txt');
}
// Fetch sitemap
const sitemapRes = await axios.get(sitemapUrl);
const sitemap = await xml2js.parseStringPromise(sitemapRes.data);
return {
statusCode: 200,
body: JSON.stringify(sitemap),
};
return sitemap;
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
throw new Error(error.message);
}
};
exports.handler = middleware(fetchSitemapHandler);