-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.ts
47 lines (42 loc) · 1.14 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { handler } from 'kysely-zod-sqlite/driver/d1-driver';
export interface Env {
D1_DB: D1Database;
DB_API_KEY: string;
}
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Allow-Credentials': 'true',
};
export default {
async fetch(
request: Request,
env: Env,
_: ExecutionContext
): Promise<Response> {
if (request.method === 'OPTIONS') {
return new Response('OK', { headers: corsHeaders, status: 200 });
}
const apiKey = request.headers.get('api-key');
if (apiKey != env.DB_API_KEY) {
return new Response('Error', { status: 401 });
}
const body: any = await request.json();
// TODO validation body
try {
const result = await handler(env.D1_DB, body);
return new Response(JSON.stringify(result), {
headers: corsHeaders,
});
} catch (error: any) {
console.error('error', error);
// send error to browser for debug
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: corsHeaders,
});
}
},
};