mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-21 14:47:41 +01:00
Fix proxiedFetch not using new turnstile integration
This commit is contained in:
parent
1acc81205a
commit
d998dceb1e
3 changed files with 35 additions and 46 deletions
|
@ -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<T> = Parameters<typeof ofetch<T, any>>;
|
||||
|
@ -21,7 +22,11 @@ export function mwFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
|||
return baseFetch<T>(url, ops);
|
||||
}
|
||||
|
||||
export function proxiedFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
||||
export async function singularProxiedFetch<T>(
|
||||
proxyUrl: string,
|
||||
url: string,
|
||||
ops: P<T>[1] = {}
|
||||
): R<T> {
|
||||
let combinedUrl = ops?.baseURL ?? "";
|
||||
if (
|
||||
combinedUrl.length > 0 &&
|
||||
|
@ -45,45 +50,25 @@ export function proxiedFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
|||
parsedUrl.searchParams.set(k, v);
|
||||
});
|
||||
|
||||
return baseFetch<T>(getLoadbalancedProxyUrl(), {
|
||||
let headers = ops.headers ?? {};
|
||||
const apiToken = await getApiToken();
|
||||
if (apiToken)
|
||||
headers = {
|
||||
...headers,
|
||||
"X-Token": apiToken,
|
||||
};
|
||||
|
||||
return baseFetch<T>(proxyUrl, {
|
||||
...ops,
|
||||
baseURL: undefined,
|
||||
params: {
|
||||
destination: parsedUrl.toString(),
|
||||
},
|
||||
query: {},
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
export function rawProxiedFetch<T>(
|
||||
url: string,
|
||||
ops: FetchOptions = {}
|
||||
): Promise<FetchResponse<T>> {
|
||||
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<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
||||
return singularProxiedFetch<T>(getLoadbalancedProxyUrl(), url, ops);
|
||||
}
|
||||
|
|
|
@ -82,15 +82,19 @@ export function makeProviderUrl(base: string) {
|
|||
};
|
||||
}
|
||||
|
||||
export async function connectServerSideEvents<T>(
|
||||
url: string,
|
||||
endEvents: string[]
|
||||
) {
|
||||
// fetch token to use
|
||||
export async function getApiToken(): Promise<string | null> {
|
||||
let apiToken = getTokenIfValid();
|
||||
if (!apiToken && isTurnstileInitialized()) {
|
||||
apiToken = `turnstile|${await getTurnstileToken()}`;
|
||||
}
|
||||
return apiToken;
|
||||
}
|
||||
|
||||
export async function connectServerSideEvents<T>(
|
||||
url: string,
|
||||
endEvents: string[]
|
||||
) {
|
||||
const apiToken = await getApiToken();
|
||||
|
||||
// insert token, if its set
|
||||
const parsedUrl = new URL(url);
|
||||
|
|
|
@ -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() {
|
|||
<p className="mb-8 mt-2">{workerList.length} worker(s) registered</p>
|
||||
<Box>
|
||||
{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 <WorkerItem name={name} key={v.id} />;
|
||||
if (s.status === "error")
|
||||
|
|
Loading…
Reference in a new issue