// import { FreshContext } from '$fresh/server.ts' import { TablesForModels } from '@lyrics/db.ts' import { handleRpcRequest, ProcedureServerError } from 'saurpc' import { rpcs } from '@lyrics/rpc.ts' export type Method = 'GET' | 'PUT' | 'DELETE' export function isMethod(value: string): value is Method { return (value in TablesForModels) } function toErrorResponse(e: ProcedureServerError) { const { type, message } = e const headers = { 'content-type': 'application/json' } switch (type) { case 'rpc_not_found': console.info('RPC Not Found:', e) return new Response( JSON.stringify({ type, message }), { status: 404, headers }, ) case 'invalid_request': console.info('Invalid RPC:', e) return new Response( JSON.stringify({ type, message }), { status: 400, headers }, ) case 'exception_caught': console.error('RPC Exception:', e) return new Response( JSON.stringify({ type }), { status: 500, headers }, ) } } export const handler = async ( req: Request, // _ctx: FreshContext, ): Promise => { try { return await handleRpcRequest(req, rpcs) } catch (e) { if (e instanceof ProcedureServerError) { return toErrorResponse(e) } else { console.error('Error handling RPC request:', e) return new Response('unknown error occurred', { status: 500 }) } } }