movie-web/src/utils/providers.ts
2023-12-29 18:17:37 +01:00

67 lines
1.8 KiB
TypeScript

import {
Fetcher,
ProviderControls,
makeProviders,
makeSimpleProxyFetcher,
makeStandardFetcher,
targets,
} from "@movie-web/providers";
import { getApiToken, setApiToken } from "@/backend/helpers/providerApi";
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
function makeLoadbalancedList(getter: () => string[]) {
let listIndex = -1;
return () => {
const fetchers = getter();
if (listIndex === -1 || listIndex >= fetchers.length) {
listIndex = Math.floor(Math.random() * fetchers.length);
}
const proxyUrl = fetchers[listIndex];
listIndex = (listIndex + 1) % fetchers.length;
return proxyUrl;
};
}
export const getLoadbalancedProxyUrl = makeLoadbalancedList(getProxyUrls);
export const getLoadbalancedProviderApiUrl =
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() {
const fetcher: Fetcher = async (a, b) => {
const currentFetcher = makeSimpleProxyFetcher(
getLoadbalancedProxyUrl(),
fetchButWithApiTokens,
);
return currentFetcher(a, b);
};
return fetcher;
}
export const providers = makeProviders({
fetcher: makeStandardFetcher(fetch),
proxiedFetcher: makeLoadBalancedSimpleProxyFetcher(),
target: targets.BROWSER,
}) as any as ProviderControls;