well-known-fursona/api/[domain]/fursona.ts

40 lines
1,001 B
TypeScript
Raw Normal View History

2023-05-05 09:30:36 +01:00
export const config = {
2023-05-05 22:13:25 +01:00
runtime: 'edge'
2023-05-05 09:30:36 +01:00
};
2023-05-05 22:13:25 +01:00
async function fetcher(url: string) {
console.log(`https://${url}/.well-known/fursona`);
let res = await fetch(`https://${url}/.well-known/fursona`);
2023-05-05 22:13:25 +01:00
// If 404, return null
if (res.status === 404) {
res = await fetch(`https://${url}/.well-known/fursona.json`);
if (res.status === 404) {
return null
}
2023-05-05 22:13:25 +01:00
}
// Try to parse json, if not return null
try {
const json = await res.json();
return json;
} catch (e) {
return null;
}
2023-05-05 09:30:36 +01:00
}
2023-05-05 23:51:52 +01:00
2023-05-05 22:13:25 +01:00
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: {
2023-05-05 23:51:52 +01:00
'content-type': 'application/json',
'cache-control': 'public, max-age=300, must-revalidate',
2023-05-05 22:13:25 +01:00
}
});
2023-05-05 15:00:57 +01:00
};