mirror of
https://github.com/movie-web/movie-web.git
synced 2025-01-01 13:55:59 +00:00
Implement turnstile on provider package simple proxy
This commit is contained in:
parent
4db6dcca48
commit
d9fd16613a
|
@ -7,6 +7,7 @@ import {
|
||||||
targets,
|
targets,
|
||||||
} from "@movie-web/providers";
|
} from "@movie-web/providers";
|
||||||
|
|
||||||
|
import { getApiToken, setApiToken } from "@/backend/helpers/providerApi";
|
||||||
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
|
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
|
||||||
|
|
||||||
function makeLoadbalancedList(getter: () => string[]) {
|
function makeLoadbalancedList(getter: () => string[]) {
|
||||||
|
@ -26,11 +27,32 @@ export const getLoadbalancedProxyUrl = makeLoadbalancedList(getProxyUrls);
|
||||||
export const getLoadbalancedProviderApiUrl =
|
export const getLoadbalancedProviderApiUrl =
|
||||||
makeLoadbalancedList(getProviderApiUrls);
|
makeLoadbalancedList(getProviderApiUrls);
|
||||||
|
|
||||||
|
async function fetchButWithApiTokens(
|
||||||
|
input: RequestInfo | URL,
|
||||||
|
init?: RequestInit | undefined
|
||||||
|
): Promise<Response> {
|
||||||
|
const apiToken = await getApiToken();
|
||||||
|
const headers = new Headers(init?.headers);
|
||||||
|
if (apiToken) headers.set("X-Token", apiToken);
|
||||||
|
const response = await fetch(
|
||||||
|
input,
|
||||||
|
init
|
||||||
|
? {
|
||||||
|
...init,
|
||||||
|
headers,
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
const newApiToken = response.headers.get("X-Token");
|
||||||
|
if (newApiToken) setApiToken(newApiToken);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
function makeLoadBalancedSimpleProxyFetcher() {
|
function makeLoadBalancedSimpleProxyFetcher() {
|
||||||
const fetcher: ProviderBuilderOptions["fetcher"] = (a, b) => {
|
const fetcher: ProviderBuilderOptions["fetcher"] = async (a, b) => {
|
||||||
const currentFetcher = makeSimpleProxyFetcher(
|
const currentFetcher = makeSimpleProxyFetcher(
|
||||||
getLoadbalancedProxyUrl(),
|
getLoadbalancedProxyUrl(),
|
||||||
fetch
|
fetchButWithApiTokens
|
||||||
);
|
);
|
||||||
return currentFetcher(a, b);
|
return currentFetcher(a, b);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue