mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-13 08:15:06 +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
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user