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";
|
const otherUrl = "https://api.tmdb.org/3";
|
||||||
let baseURL = "https://api.themoviedb.org/3";
|
let useFallback = false;
|
||||||
|
|
||||||
const apiKey = conf().TMDB_READ_API_KEY;
|
const apiKey = conf().TMDB_READ_API_KEY;
|
||||||
|
|
||||||
|
@ -155,33 +156,26 @@ 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");
|
||||||
|
|
||||||
let res: T;
|
let res: T;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
res = await mwFetch<T>(encodeURI(url), {
|
res = await mwFetch<T>(encodeURI(url), {
|
||||||
headers,
|
headers,
|
||||||
baseURL,
|
baseURL: !useFallback ? baseURL : otherUrl,
|
||||||
params: {
|
params: {
|
||||||
...params,
|
...params,
|
||||||
},
|
},
|
||||||
signal: AbortSignal.timeout(baseURL !== otherUrl ? 5000 : 30000),
|
signal: AbortSignal.timeout(!useFallback ? 5000 : 30000),
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (baseURL !== otherUrl) {
|
useFallback = true;
|
||||||
baseURL = otherUrl;
|
res = await mwFetch<T>(encodeURI(url), {
|
||||||
res = await mwFetch<T>(encodeURI(url), {
|
headers,
|
||||||
headers,
|
baseURL: otherUrl,
|
||||||
baseURL,
|
params: {
|
||||||
params: {
|
...params,
|
||||||
...params,
|
},
|
||||||
},
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res = Promise.reject();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue