Browse Source

fix: add alternate tmdb endpoint to fix errors when main url is blocked

Signed-off-by: Megh Rathod <me@meghrathod.dev>
pull/1108/head
Megh Rathod 1 year ago
parent
commit
1ec51699d1
No known key found for this signature in database
  1. 44
      src/backend/metadata/tmdb.ts

44
src/backend/metadata/tmdb.ts

@ -143,7 +143,8 @@ export function decodeTMDBId( @@ -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 = { @@ -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…
Cancel
Save