dd/src/routes/sql-api/query/+server.ts

38 lines
997 B
TypeScript

import type { RequestHandler } from './$types';
export const POST: RequestHandler = async ({ platform, request }) => {
try {
const env = platform?.env;
if (!env?.COUNTER) {
return new Response(JSON.stringify({ error: 'COUNTER binding not found' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
const body = await request.json();
const id = env.COUNTER.idFromName('global-counter');
const stub = env.COUNTER.get(id);
const response = await stub.fetch('https://fake-host/sql/query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
const data = await response.json();
return new Response(JSON.stringify(data, null, 2), {
status: response.status,
headers: { 'Content-Type': 'application/json' }
});
} catch (error: any) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
};