From ecec5df9f6f0b756e4adb96b3cc6934660d7309f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Erik=20Hedenstro=CC=88m?= <erik@hedenstroem.com>
Date: Fri, 1 Mar 2024 14:33:21 +0100
Subject: [PATCH] Added proxying

---
 src/index.ts | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/index.ts b/src/index.ts
index 8087b85..c9599f2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -23,10 +23,33 @@ export interface Env {
 	//
 	// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
 	// MY_QUEUE: Queue;
+	ALLOWED_OPENAI_KEYS: string;
+	HELICONE_KEY: string;
 }
 
 export default {
 	async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
-		return new Response('Hello World!');
+
+		// The URL you want to proxy the requests to
+		const url = new URL(request.url);
+
+		// Modify the request to point to the target URL
+		// For example, if you're proxying to https://example.com
+		url.hostname = "oai.hconeai.com";
+		
+		// Clone the request to modify it
+		const modifiedRequest = new Request(url.toString(), request);
+		
+		// Add your custom header to the request
+		modifiedRequest.headers.set("Host", url.hostname);
+		modifiedRequest.headers.set("Helicone-Auth", "Bearer " + env.HELICONE_KEY);
+		console.log(modifiedRequest);
+		
+		// Make the fetch request with the modified request
+		const response = await fetch(modifiedRequest);
+		
+		// Return the response from the target server
+		return response;
+		//return new Response('Hello World!');
 	},
 };
-- 
GitLab