From bcff5a8972173811ab882bee642371dc229a48ad Mon Sep 17 00:00:00 2001 From: JORDAAR <69628820+Jordaar@users.noreply.github.com> Date: Tue, 9 May 2023 12:51:13 +0530 Subject: [PATCH] add rawProxiedFetch --- src/backend/helpers/fetch.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/backend/helpers/fetch.ts b/src/backend/helpers/fetch.ts index 9ed16b7c..16cde080 100644 --- a/src/backend/helpers/fetch.ts +++ b/src/backend/helpers/fetch.ts @@ -1,4 +1,4 @@ -import { ofetch } from "ofetch"; +import { FetchOptions, FetchResponse, ofetch } from "ofetch"; import { conf } from "@/setup/config"; @@ -59,3 +59,36 @@ export function proxiedFetch(url: string, ops: P[1] = {}): R { }, }); } + +export function rawProxiedFetch( + url: string, + ops: FetchOptions = {} +): Promise> { + let combinedUrl = ops?.baseURL ?? ""; + if ( + combinedUrl.length > 0 && + combinedUrl.endsWith("/") && + url.startsWith("/") + ) + combinedUrl += url.slice(1); + else if ( + combinedUrl.length > 0 && + !combinedUrl.endsWith("/") && + !url.startsWith("/") + ) + combinedUrl += `/${url}`; + else combinedUrl += url; + + const parsedUrl = new URL(combinedUrl); + Object.entries(ops?.params ?? {}).forEach(([k, v]) => { + parsedUrl.searchParams.set(k, v); + }); + + return baseFetch.raw(getProxyUrl(), { + ...ops, + baseURL: undefined, + params: { + destination: parsedUrl.toString(), + }, + }); +}