fix: add alternate tmdb endpoint to fix errors when main url is blocked

Signed-off-by: Megh Rathod <me@meghrathod.dev>
This commit is contained in:
Megh Rathod 2024-04-12 11:33:18 +05:30
parent 8a9def00de
commit 1ec51699d1
No known key found for this signature in database

View File

@ -143,7 +143,8 @@ export function decodeTMDBId(
};
}
const baseURL = "https://api.tmdb.org/3";
const otherUrl = "https://api.tmdb.org/3";
let baseURL = "https://api.themoviedb.org/3";
const apiKey = conf().TMDB_READ_API_KEY;
@ -155,13 +156,40 @@ const headers = {
async function get<T>(url: string, params?: object): Promise<T> {
if (!apiKey) throw new Error("TMDB API key not set");
const res = await mwFetch<any>(encodeURI(url), {
const controller = new AbortController();
const { signal } = controller;
const timeoutId =
baseURL === otherUrl
? setTimeout(() => controller.abort(), 15000)
: setTimeout(() => controller.abort(), 3000);
let res: Promise<T>;
try {
res = await mwFetch<any>(encodeURI(url), {
headers,
baseURL,
params: {
...params,
},
signal,
});
clearTimeout(timeoutId);
} catch (err) {
if (baseURL !== otherUrl) {
baseURL = otherUrl;
res = await mwFetch<any>(encodeURI(url), {
headers,
baseURL,
params: {
...params,
},
});
} else {
res = Promise.reject();
}
}
return res;
}