2023-07-22 16:18:28 +02:00
|
|
|
const axios = require('axios');
|
2023-06-18 22:01:13 +02:00
|
|
|
|
|
|
|
exports.handler = function(event, context, callback) {
|
2023-07-28 22:46:20 +02:00
|
|
|
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}`;
|
2023-07-22 16:18:28 +02:00
|
|
|
|
|
|
|
axios.get(endpoint)
|
2023-06-18 22:01:13 +02:00
|
|
|
.then(
|
2023-07-22 16:18:28 +02:00
|
|
|
(response) => {
|
2023-06-18 22:01:13 +02:00
|
|
|
callback(null, {
|
|
|
|
statusCode: 200,
|
2023-07-22 16:18:28 +02:00
|
|
|
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'}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|