mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-30 16:17: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";
|
import { getLoadbalancedProxyUrl } from "@/utils/providers";
|
||||||
|
|
||||||
type P<T> = Parameters<typeof ofetch<T, any>>;
|
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);
|
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 ?? "";
|
let combinedUrl = ops?.baseURL ?? "";
|
||||||
if (
|
if (
|
||||||
combinedUrl.length > 0 &&
|
combinedUrl.length > 0 &&
|
||||||
|
@ -45,45 +50,25 @@ export function proxiedFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
||||||
parsedUrl.searchParams.set(k, v);
|
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,
|
...ops,
|
||||||
baseURL: undefined,
|
baseURL: undefined,
|
||||||
params: {
|
params: {
|
||||||
destination: parsedUrl.toString(),
|
destination: parsedUrl.toString(),
|
||||||
},
|
},
|
||||||
query: {},
|
query: {},
|
||||||
|
headers,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function rawProxiedFetch<T>(
|
export function proxiedFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
||||||
url: string,
|
return singularProxiedFetch<T>(getLoadbalancedProxyUrl(), url, ops);
|
||||||
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(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,15 +82,19 @@ export function makeProviderUrl(base: string) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function connectServerSideEvents<T>(
|
export async function getApiToken(): Promise<string | null> {
|
||||||
url: string,
|
|
||||||
endEvents: string[]
|
|
||||||
) {
|
|
||||||
// fetch token to use
|
|
||||||
let apiToken = getTokenIfValid();
|
let apiToken = getTokenIfValid();
|
||||||
if (!apiToken && isTurnstileInitialized()) {
|
if (!apiToken && isTurnstileInitialized()) {
|
||||||
apiToken = `turnstile|${await getTurnstileToken()}`;
|
apiToken = `turnstile|${await getTurnstileToken()}`;
|
||||||
}
|
}
|
||||||
|
return apiToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function connectServerSideEvents<T>(
|
||||||
|
url: string,
|
||||||
|
endEvents: string[]
|
||||||
|
) {
|
||||||
|
const apiToken = await getApiToken();
|
||||||
|
|
||||||
// insert token, if its set
|
// insert token, if its set
|
||||||
const parsedUrl = new URL(url);
|
const parsedUrl = new URL(url);
|
||||||
|
|
|
@ -2,7 +2,7 @@ import classNames from "classnames";
|
||||||
import { useMemo, useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { useAsyncFn } from "react-use";
|
import { useAsyncFn } from "react-use";
|
||||||
|
|
||||||
import { mwFetch } from "@/backend/helpers/fetch";
|
import { singularProxiedFetch } from "@/backend/helpers/fetch";
|
||||||
import { Button } from "@/components/buttons/Button";
|
import { Button } from "@/components/buttons/Button";
|
||||||
import { Icon, Icons } from "@/components/Icon";
|
import { Icon, Icons } from "@/components/Icon";
|
||||||
import { Box } from "@/components/layout/Box";
|
import { Box } from "@/components/layout/Box";
|
||||||
|
@ -69,11 +69,11 @@ export function WorkerTestPart() {
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
await mwFetch(worker.url, {
|
await singularProxiedFetch(
|
||||||
query: {
|
worker.url,
|
||||||
destination: "https://postman-echo.com/get",
|
"https://postman-echo.com/get",
|
||||||
},
|
{}
|
||||||
});
|
);
|
||||||
updateWorker(worker.id, {
|
updateWorker(worker.id, {
|
||||||
id: worker.id,
|
id: worker.id,
|
||||||
status: "success",
|
status: "success",
|
||||||
|
@ -94,7 +94,7 @@ export function WorkerTestPart() {
|
||||||
<p className="mb-8 mt-2">{workerList.length} worker(s) registered</p>
|
<p className="mb-8 mt-2">{workerList.length} worker(s) registered</p>
|
||||||
<Box>
|
<Box>
|
||||||
{workerList.map((v, i) => {
|
{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}`;
|
const name = `Worker ${i + 1}`;
|
||||||
if (!s) return <WorkerItem name={name} key={v.id} />;
|
if (!s) return <WorkerItem name={name} key={v.id} />;
|
||||||
if (s.status === "error")
|
if (s.status === "error")
|
||||||
|
|
Loading…
Reference in a new issue