web/src/pages/api/awesome-privacy.json.ts
33 lines · 1060 bytes
2 symbols in this file
1import type { APIRoute } from 'astro';
2import * as yaml from 'js-yaml';
3
4import type { AwesomePrivacy } from '../../types/Service';
5
6const awesomePrivacyYamlPath =
7 'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml';
8
9export const GET: APIRoute = async () => {
10 const response = await fetch(awesomePrivacyYamlPath).catch(() => null);
11 const data = response?.ok
12 ? (yaml.load(await response.text()) as AwesomePrivacy)
13 : null;
14
15 // Caching a failed fetch would serve a broken dataset for the whole max-age
16 if (!data?.categories) {
17 return new Response(JSON.stringify({ error: 'Failed to fetch dataset' }), {
18 status: 502,
19 headers: {
20 'content-type': 'application/json',
21 'cache-control': 'no-store',
22 },
23 });
24 }
25
26 return new Response(JSON.stringify(data), {
27 headers: {
28 'content-type': 'application/json',
29 // ~250KB fetched from GitHub raw, and it only changes on merge
30 'cache-control': 'public, max-age=300, stale-while-revalidate=86400',
31 },
32 });
33};