mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-20 14:37:43 +01:00
added useFallback to decide which TMDB url to use
Signed-off-by: Megh Rathod <me@meghrathod.dev>
This commit is contained in:
parent
0e3f82df30
commit
995c855ac2
1 changed files with 12 additions and 18 deletions
|
@ -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;
|
||||
useFallback = true;
|
||||
res = await mwFetch<T>(encodeURI(url), {
|
||||
headers,
|
||||
baseURL,
|
||||
baseURL: otherUrl,
|
||||
params: {
|
||||
...params,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
res = Promise.reject();
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue