web-check/api/lighthouse-report.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
2023-06-18 22:01:13 +02:00
exports.handler = function(event, context, callback) {
const url = (event.queryStringParameters || event.query).url;
2023-06-18 22:01:13 +02:00
if (!url) {
callback(null, {
statusCode: 400,
body: JSON.stringify({ error: 'URL param is required'}),
});
}
const apiKey = process.env.GOOGLE_CLOUD_API_KEY;
2023-07-23 01:05:04 +02:00
if (!apiKey) {
callback(null, {
statusCode: 500,
body: JSON.stringify({ error: 'API key (GOOGLE_CLOUD_API_KEY) not set'}),
});
}
2023-06-18 22:01:13 +02:00
const endpoint = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=${encodeURIComponent(url)}&category=PERFORMANCE&category=ACCESSIBILITY&category=BEST_PRACTICES&category=SEO&category=PWA&strategy=mobile&key=${apiKey}`;
axios.get(endpoint)
2023-06-18 22:01:13 +02:00
.then(
(response) => {
2023-06-18 22:01:13 +02:00
callback(null, {
statusCode: 200,
body: JSON.stringify(response.data),
2023-06-18 22:01:13 +02:00
});
}
).catch(
() => {
callback(null, {
statusCode: 500,
body: JSON.stringify({ error: 'Error running Lighthouse'}),
});
}
);
};