import DB from "."; const db = new DB(); const ready = db.init(); type Route = Partial< Record< Bun.Serve.HTTPMethod, Bun.Serve.Handler, Response> > >; export const handler: Route = { async GET(req: Bun.BunRequest) { await ready; if (!db.ready) return Response.json({ error: "DB failed to initialize" }); // const db = new DB(); console.log("Handling HTTP Request on DB", req.url); try { const url = new URL(req.url); const params = url.searchParams; const word = params.get("word"); const lang = params.get("lang"); if (!word) return Response.json({ error: "word param is required" }); if (!lang) return Response.json({ error: "lang param is required" }); const row = await db.fetchExpressionBySpelling(word, lang); if (!row) return Response.json({ error: "No data found" }); else return Response.json({ ok: row }); } catch (e) { return Response.json({ error: `${e}` }); } }, async POST(req: Bun.BunRequest) { await ready; if (!db.ready) return Response.json({ error: "DB failed to initialize" }); // const db = new DB(); console.log("Handling HTTP Request on DB", req.url); try { const reqBody = (await req.json()) as RequestBody; if (!reqBody) return Response.json({ error: "No request body" }); const returnData = "getWordFull" in reqBody ? db.fetchExpressionBySpelling( reqBody.getWordFull.spelling, reqBody.getWordFull.lang, ) : null; const row = await returnData; console.log({ row }); if (!returnData || !row) return Response.json({ error: "No data found" }); else return Response.json({ ok: row }); } catch (e) { return Response.json({ error: `${e}` }); } }, }; type BySpelling = { spelling: string; lang: string }; type ByLanguage = string; // iso6393 type RequestBody = | { getWordFull: BySpelling } | { getWordsenses: BySpelling } | { getWordPhonetics: BySpelling } | { getLanguages: null } | { getExpressions: ByLanguage };