diff --git a/src/backend/helpers/fetch.ts b/src/backend/helpers/fetch.ts index fca9ab89..646d8ab9 100644 --- a/src/backend/helpers/fetch.ts +++ b/src/backend/helpers/fetch.ts @@ -1,5 +1,6 @@ -import { FetchOptions, FetchResponse, ofetch } from "ofetch"; +import { ofetch } from "ofetch"; +import { getApiToken } from "@/backend/helpers/providerApi"; import { getLoadbalancedProxyUrl } from "@/utils/providers"; type P = Parameters>; @@ -21,7 +22,11 @@ export function mwFetch(url: string, ops: P[1] = {}): R { return baseFetch(url, ops); } -export function proxiedFetch(url: string, ops: P[1] = {}): R { +export async function singularProxiedFetch( + proxyUrl: string, + url: string, + ops: P[1] = {} +): R { let combinedUrl = ops?.baseURL ?? ""; if ( combinedUrl.length > 0 && @@ -45,45 +50,25 @@ export function proxiedFetch(url: string, ops: P[1] = {}): R { parsedUrl.searchParams.set(k, v); }); - return baseFetch(getLoadbalancedProxyUrl(), { + let headers = ops.headers ?? {}; + const apiToken = await getApiToken(); + if (apiToken) + headers = { + ...headers, + "X-Token": apiToken, + }; + + return baseFetch(proxyUrl, { ...ops, baseURL: undefined, params: { destination: parsedUrl.toString(), }, query: {}, + headers, }); } -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(getLoadbalancedProxyUrl(), { - ...ops, - baseURL: undefined, - params: { - destination: parsedUrl.toString(), - }, - }); +export function proxiedFetch(url: string, ops: P[1] = {}): R { + return singularProxiedFetch(getLoadbalancedProxyUrl(), url, ops); } diff --git a/src/backend/helpers/providerApi.ts b/src/backend/helpers/providerApi.ts index fa608e6a..89d3eb13 100644 --- a/src/backend/helpers/providerApi.ts +++ b/src/backend/helpers/providerApi.ts @@ -82,15 +82,19 @@ export function makeProviderUrl(base: string) { }; } -export async function connectServerSideEvents( - url: string, - endEvents: string[] -) { - // fetch token to use +export async function getApiToken(): Promise { let apiToken = getTokenIfValid(); if (!apiToken && isTurnstileInitialized()) { apiToken = `turnstile|${await getTurnstileToken()}`; } + return apiToken; +} + +export async function connectServerSideEvents( + url: string, + endEvents: string[] +) { + const apiToken = await getApiToken(); // insert token, if its set const parsedUrl = new URL(url); diff --git a/src/pages/parts/admin/WorkerTestPart.tsx b/src/pages/parts/admin/WorkerTestPart.tsx index e1314e0a..88230f11 100644 --- a/src/pages/parts/admin/WorkerTestPart.tsx +++ b/src/pages/parts/admin/WorkerTestPart.tsx @@ -2,7 +2,7 @@ import classNames from "classnames"; import { useMemo, useState } from "react"; import { useAsyncFn } from "react-use"; -import { mwFetch } from "@/backend/helpers/fetch"; +import { singularProxiedFetch } from "@/backend/helpers/fetch"; import { Button } from "@/components/buttons/Button"; import { Icon, Icons } from "@/components/Icon"; import { Box } from "@/components/layout/Box"; @@ -69,11 +69,11 @@ export function WorkerTestPart() { }); continue; } - await mwFetch(worker.url, { - query: { - destination: "https://postman-echo.com/get", - }, - }); + await singularProxiedFetch( + worker.url, + "https://postman-echo.com/get", + {} + ); updateWorker(worker.id, { id: worker.id, status: "success", @@ -94,7 +94,7 @@ export function WorkerTestPart() {

{workerList.length} worker(s) registered

{workerList.map((v, i) => { - const s = workerState.find((segment) => segment.id); + const s = workerState.find((segment) => segment.id === v.id); const name = `Worker ${i + 1}`; if (!s) return ; if (s.status === "error")