From 6ad9d3ad3ee249614eb8810e9ba79aa26afbd85f Mon Sep 17 00:00:00 2001 From: Srdjan S Date: Tue, 10 Dec 2024 06:27:57 +0100 Subject: [PATCH] Override request URL --- index.js | 7 ++++++- wrangler.toml.example | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3e0965d..23316a7 100644 --- a/index.js +++ b/index.js @@ -50,9 +50,14 @@ async function reportMetric(request, response, startTime, endTime, env) { export default { async fetch(request, env, ctx) { + const url = new URL(request.url); + const newUrl = `${env.REQUEST_URL}${url.pathname}${url.search}`; + // override the request with the new URL + request = new Request(newUrl, request); const reqStartTime = Date.now(); - const response = await fetch('https://example.com'); + const response = await fetch(request); const reqEndTime = Date.now(); + ctx.waitUntil(reportMetric(request, response, reqStartTime, reqEndTime, env)); return response; } diff --git a/wrangler.toml.example b/wrangler.toml.example index 3e34452..5ca3df3 100644 --- a/wrangler.toml.example +++ b/wrangler.toml.example @@ -4,6 +4,7 @@ compatibility_date = "2024-12-05" compatibility_flags = ["nodejs_compat"] [vars] +REQUEST_URL = "https://example.com" INFLUX_METRIC = "example" INFLUX_URL = "http://localhost:8086" INFLUX_TOKEN = "example"