-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
114 lines (101 loc) · 4.01 KB
/
server.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// import calendar from "@googleapis/calendar"
import { handler as updateOrderStatusHandler } from "./src/functions/updateOrderStatus"
import { handler as createOrderHandler } from "./src/functions/createOrder"
import { handler as getOrdersHandler } from "./src/functions/getOrders"
import { handler as getOrderDatesHandler } from "./src/functions/getOrderDate"
const ALLOWED_ORIGIN = "*"
const corsHeaders = {
"Access-Control-Allow-Origin": ALLOWED_ORIGIN,
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Max-Age": "86400",
}
export default {
async fetch(req) {
// calendar.calendar({
// version: "v3",
// auth: process.env.GOOGLE_API_KEY,
// }).events.insert({
// requestBody: {
// summary: "Test event",
// description: "This is a test event",
// start: {
// dateTime: "2022-01-01T00:00:00Z",
// timeZone: "Europe/Prague",
// },
// end: {
// dateTime: "2022-01-01T01:00:00Z",
// timeZone: "Europe/Prague",
// }
// },
// })
// // Always add CORS headers to the response
const headers = new Headers(corsHeaders)
headers.set("Content-Type", "application/json")
// Handle preflight requests
if (req.method === "OPTIONS") {
return new Response(null, {
headers,
status: 204,
})
}
const url = new URL(req.url)
try {
// Create order
if (url.pathname === "/api/orders" && req.method === "POST") {
const response = await createOrderHandler({
body: await req.text(),
requestContext: {},
queryStringParameters: {},
} as any)
return new Response(response.body, {
status: response.statusCode,
headers
})
}
// Get orders
if (url.pathname === "/api/orders" && req.method === "GET") {
const response = await getOrdersHandler({
queryStringParameters: Object.fromEntries(url.searchParams),
requestContext: {},
} as any)
return new Response(response.body, {
status: response.statusCode,
headers
})
}
// Get order dates
if (url.pathname === "/api/orders/dates" && req.method === "GET") {
const response = await getOrderDatesHandler()
return new Response(response.body, {
status: response.statusCode,
headers
})
}
// Update order status
if (url.pathname.match(/\/api\/orders\/\d+\/status/) && req.method === "PUT") {
const orderId = url.pathname.split("/")[3]
const response = await updateOrderStatusHandler({
pathParameters: { orderId },
body: await req.text(),
requestContext: {},
} as any)
return new Response(response.body, {
status: response.statusCode,
headers
})
}
return new Response(JSON.stringify({ message: "Not Found" }), {
status: 404,
headers
})
} catch (error) {
console.error("Server error:", error)
return new Response(JSON.stringify({ message: "Internal Server Error" }), {
status: 500,
headers
})
}
}
}