Minor formatting/typing for API

This commit is contained in:
Gabriel Simmer 2023-05-05 22:13:25 +01:00
parent a384b5c477
commit 2df35892e4
Signed by: arch
GPG key ID: C81B106D46C5B875

View file

@ -1,38 +1,35 @@
import type { NextRequest } from 'next/server';
import type { RequestContext } from '@vercel/edge';
export const config = { export const config = {
runtime: 'edge', runtime: 'edge'
}; };
async function fetcher(url) { async function fetcher(url: string) {
console.log(`https://${url}/.well-known/fursona`); console.log(`https://${url}/.well-known/fursona`);
const res = await fetch(`https://${url}/.well-known/fursona`); const res = await fetch(`https://${url}/.well-known/fursona`);
// If 404, return null // If 404, return null
if (res.status === 404) { if (res.status === 404) {
return null; return null;
} }
// Try to parse json, if not return null // Try to parse json, if not return null
try { try {
const json = await res.json(); const json = await res.json();
return json; return json;
} catch (e) { } catch (e) {
return null; return null;
} }
} }
export default async (request: NextRequest) => { export default async (request: Request) => {
// Proxy the request to the `req.url` value and return it as a JSON string. // Proxy the request to the `req.url` value and return it as a JSON string.
const params = new URL(request.url).searchParams; const params = new URL(request.url).searchParams;
const domain = params.get("domain"); const domain = params.get('domain') as string;
const data = await fetcher(domain); const data = await fetcher(domain);
if data === null { if (data === null) {
return new Response('Not Found', { status: 404 }); return new Response('Not Found', { status: 404 });
} }
return new Response(JSON.stringify(data), { return new Response(JSON.stringify(data), {
headers: { headers: {
'content-type': 'application/json', 'content-type': 'application/json'
// 'cache-control': 'public, max-age=600, stale-while-revalidate=1200', // 'cache-control': 'public, max-age=600, stale-while-revalidate=1200',
}, }
}); });
}; };