added useFallback to decide which TMDB url to use

Signed-off-by: Megh Rathod <me@meghrathod.dev>
This commit is contained in:
Megh Rathod 2024-04-14 16:19:51 +05:30
parent 0e3f82df30
commit 995c855ac2
No known key found for this signature in database

View File

@ -143,8 +143,9 @@ export function decodeTMDBId(
};
}
const baseURL = "https://api.themoviedb.org/3";
const otherUrl = "https://api.tmdb.org/3";
let baseURL = "https://api.themoviedb.org/3";
let useFallback = false;
const apiKey = conf().TMDB_READ_API_KEY;
@ -155,33 +156,26 @@ const headers = {
async function get<T>(url: string, params?: object): Promise<T> {
if (!apiKey) throw new Error("TMDB API key not set");
let res: T;
try {
res = await mwFetch<T>(encodeURI(url), {
headers,
baseURL,
baseURL: !useFallback ? baseURL : otherUrl,
params: {
...params,
},
signal: AbortSignal.timeout(baseURL !== otherUrl ? 5000 : 30000),
signal: AbortSignal.timeout(!useFallback ? 5000 : 30000),
});
} catch (err) {
if (baseURL !== otherUrl) {
baseURL = otherUrl;
res = await mwFetch<T>(encodeURI(url), {
headers,
baseURL,
params: {
...params,
},
});
} else {
res = Promise.reject();
}
useFallback = true;
res = await mwFetch<T>(encodeURI(url), {
headers,
baseURL: otherUrl,
params: {
...params,
},
});
}
return res;
}