aboutsummaryrefslogtreecommitdiff
path: root/apps/web/server/proxy.ts
blob: 4fc152da9dd99e1d5a7c548000ea142766d410b5 (plain) (blame)
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
import { AppLoadContext } from "@remix-run/cloudflare";

export const proxy = async (
	path: string,
	requestOptions: RequestInit,
	originalRequest: Request,
	context: AppLoadContext,
) => {
	try {
		// update the request headers to add the cookie
		const requestHeaders = new Headers(requestOptions.headers);
		requestHeaders.set("Cookie", originalRequest.headers.get("Cookie") || "");

		const backendUrl =
			context.cloudflare.env.BACKEND_URL ?? "https://supermemory-backend.dhravya.workers.dev";

		const response = await fetch(`${backendUrl}${path}`, {
			...requestOptions,
			headers: requestHeaders,
		});

		if (!response.ok) {
			console.error("Proxy request failed", await response.text());
			return new Response(JSON.stringify({ error: "Proxy request failed" }), {
				status: response.status,
				headers: {
					"Content-Type": "application/json",
				},
			});
		}

		return response;
	} catch (error) {
		console.error("Proxy request failed:", error);
		return new Response(JSON.stringify({ error: "Proxy request failed" }), {
			status: 500,
			headers: {
				"Content-Type": "application/json",
			},
		});
	}
};