c20ca36a41
5 minute cache, forcing revalidate when stale. May up this during FWA or other cons. I'm expecting as time goes on fursona endpoints stabilise and are updated less often, letting me increase this further.
36 lines
905 B
TypeScript
36 lines
905 B
TypeScript
export const config = {
|
|
runtime: 'edge'
|
|
};
|
|
|
|
async function fetcher(url: string) {
|
|
console.log(`https://${url}/.well-known/fursona`);
|
|
const res = await fetch(`https://${url}/.well-known/fursona`);
|
|
// If 404, return null
|
|
if (res.status === 404) {
|
|
return null;
|
|
}
|
|
// Try to parse json, if not return null
|
|
try {
|
|
const json = await res.json();
|
|
return json;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default async (request: Request) => {
|
|
// Proxy the request to the `req.url` value and return it as a JSON string.
|
|
const params = new URL(request.url).searchParams;
|
|
const domain = params.get('domain') as string;
|
|
const data = await fetcher(domain);
|
|
if (data === null) {
|
|
return new Response('Not Found', { status: 404 });
|
|
}
|
|
return new Response(JSON.stringify(data), {
|
|
headers: {
|
|
'content-type': 'application/json'
|
|
'cache-control': 'public, max-age=300, must-revalidate',
|
|
}
|
|
});
|
|
};
|