|
1 | | -export async function onRequest({ request, params, env }: { |
| 1 | +export async function onRequest({ |
| 2 | + request, |
| 3 | + params, |
| 4 | + env, |
| 5 | +}: { |
2 | 6 | request: Request; |
3 | 7 | params: { path: string[] }; |
4 | 8 | env: { |
5 | 9 | API_URL?: string; |
6 | 10 | CARDANO_NETWORK?: string; |
7 | 11 | }; |
8 | 12 | }) { |
9 | | - if (request.method === 'OPTIONS') { |
| 13 | + if (request.method === "OPTIONS") { |
10 | 14 | return new Response(null, { |
11 | 15 | headers: { |
12 | | - 'Access-Control-Allow-Origin': '*', |
13 | | - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
14 | | - 'Access-Control-Allow-Headers': 'Content-Type', |
| 16 | + "Access-Control-Allow-Origin": "*", |
| 17 | + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", |
| 18 | + "Access-Control-Allow-Headers": "Content-Type", |
15 | 19 | }, |
16 | 20 | }); |
17 | 21 | } |
18 | 22 |
|
19 | 23 | const url = new URL(request.url); |
20 | | - const apiBaseUrl = env.API_URL || 'https://preprod-api.b7s.services'; |
21 | | - const cardanoNetwork = env.CARDANO_NETWORK || 'preprod'; |
22 | | - const apiUrl = `${apiBaseUrl}/api/${params.path.join('/')}${url.search}`; |
23 | | - |
24 | | - console.log('Proxying request to:', apiUrl); |
25 | | - console.log('Request method:', request.method); |
26 | | - console.log('Cardano Network:', cardanoNetwork); |
27 | | - |
| 24 | + const apiBaseUrl = env.API_URL || "https://preprod-api.b7s.services"; |
| 25 | + const cardanoNetwork = env.CARDANO_NETWORK || "preprod"; |
| 26 | + const apiUrl = `${apiBaseUrl}/api/${params.path.join("/")}${url.search}`; |
| 27 | + |
| 28 | + console.log("Proxying request to:", apiUrl); |
| 29 | + console.log("Request method:", request.method); |
| 30 | + console.log("Cardano Network:", cardanoNetwork); |
| 31 | + |
28 | 32 | const response = await fetch(apiUrl, { |
29 | 33 | method: request.method, |
30 | 34 | headers: request.headers, |
31 | 35 | body: request.body, |
32 | | - redirect: 'manual' |
| 36 | + redirect: "manual", |
33 | 37 | }); |
34 | 38 |
|
35 | | - console.log('Upstream response status:', response.status); |
36 | | - console.log('Upstream response headers:', Object.fromEntries(response.headers.entries())); |
| 39 | + console.log("Upstream response status:", response.status); |
| 40 | + console.log( |
| 41 | + "Upstream response headers:", |
| 42 | + Object.fromEntries(response.headers.entries()), |
| 43 | + ); |
37 | 44 |
|
38 | | - if (params.path.join('/') === 'client/profile' && response.status === 302) { |
39 | | - const location = response.headers.get('location'); |
| 45 | + if (params.path.join("/") === "client/profile" && response.status === 302) { |
| 46 | + const location = response.headers.get("location"); |
40 | 47 | if (location) { |
41 | 48 | return new Response(location, { |
42 | 49 | status: 200, |
43 | 50 | headers: { |
44 | | - 'Content-Type': 'text/plain', |
45 | | - 'Access-Control-Allow-Origin': '*', |
| 51 | + "Content-Type": "text/plain", |
| 52 | + "Access-Control-Allow-Origin": "*", |
46 | 53 | }, |
47 | 54 | }); |
48 | 55 | } |
49 | 56 | } |
50 | 57 |
|
51 | 58 | const responseHeaders = new Headers(); |
52 | | - |
| 59 | + |
53 | 60 | response.headers.forEach((value, key) => { |
54 | 61 | responseHeaders.set(key, value); |
55 | 62 | }); |
56 | | - |
57 | | - responseHeaders.set('Access-Control-Allow-Origin', '*'); |
58 | | - responseHeaders.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); |
59 | | - responseHeaders.set('Access-Control-Allow-Headers', 'Content-Type'); |
| 63 | + |
| 64 | + responseHeaders.set("Access-Control-Allow-Origin", "*"); |
| 65 | + responseHeaders.set( |
| 66 | + "Access-Control-Allow-Methods", |
| 67 | + "GET, POST, PUT, DELETE, OPTIONS", |
| 68 | + ); |
| 69 | + responseHeaders.set("Access-Control-Allow-Headers", "Content-Type"); |
60 | 70 |
|
61 | 71 | const responseBody = await response.text(); |
62 | | - console.log('Response body length:', responseBody.length); |
63 | | - console.log('Response body preview:', responseBody.substring(0, 200)); |
| 72 | + console.log("Response body length:", responseBody.length); |
| 73 | + console.log("Response body preview:", responseBody.substring(0, 200)); |
64 | 74 |
|
65 | 75 | return new Response(responseBody, { |
66 | 76 | status: response.status, |
|
0 commit comments