mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-28 08:56:05 +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;
|
const apiKey = conf().TMDB_READ_API_KEY;
|
||||||
|
|
||||||
|
@ -155,13 +156,40 @@ const headers = {
|
||||||
async function get<T>(url: string, params?: object): Promise<T> {
|
async function get<T>(url: string, params?: object): Promise<T> {
|
||||||
if (!apiKey) throw new Error("TMDB API key not set");
|
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,
|
headers,
|
||||||
baseURL,
|
baseURL,
|
||||||
params: {
|
params: {
|
||||||
...params,
|
...params,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
res = Promise.reject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue