2024-05-06 22:51:32 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
import middleware from './_common/middleware.js';
|
2023-06-18 22:01:13 +02:00
|
|
|
|
2024-05-06 22:51:32 +02:00
|
|
|
const qualityHandler = async (url, event, context) => {
|
2023-08-09 23:33:36 +02:00
|
|
|
const apiKey = process.env.GOOGLE_CLOUD_API_KEY;
|
2023-06-18 22:01:13 +02:00
|
|
|
|
2023-07-23 01:05:04 +02:00
|
|
|
if (!apiKey) {
|
2024-03-14 08:59:03 +01:00
|
|
|
throw new Error(
|
|
|
|
'Missing Google API. You need to set the `GOOGLE_CLOUD_API_KEY` environment variable'
|
|
|
|
);
|
2023-07-23 01:05:04 +02:00
|
|
|
}
|
|
|
|
|
2024-03-14 08:59:03 +01: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-08-09 23:33:36 +02:00
|
|
|
|
2024-03-14 08:59:03 +01:00
|
|
|
return (await axios.get(endpoint)).data;
|
2023-06-18 22:01:13 +02:00
|
|
|
};
|
2023-08-09 23:33:36 +02:00
|
|
|
|
2024-05-06 22:51:32 +02:00
|
|
|
export const handler = middleware(qualityHandler);
|
|
|
|
export default handler;
|