mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-27 00:56:07 +00:00
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:
parent
8a9def00de
commit
1ec51699d1
|
@ -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), {
|
||||
headers,
|
||||
baseURL,
|
||||
params: {
|
||||
...params,
|
||||
},
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue