2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-28 09:38:41 +00:00

Do not throw error if fetch API is not available, until actual request is sent

This commit is contained in:
Vjacheslav Trushkin 2021-04-24 14:21:17 +03:00
parent 93856ba889
commit ac37225e5e

View File

@ -25,7 +25,12 @@ const pathCache: Record<string, string> = Object.create(null);
* Use this to set 'cross-fetch' in node.js environment if you are retrieving icons on server side.
* Not needed when using stuff like Next.js or SvelteKit because components use API only on client side.
*/
let fetchModule = fetch;
let fetchModule: typeof fetch | null = null;
try {
fetchModule = fetch;
} catch (err) {
//
}
export function setFetch(fetch: typeof fetchModule): void {
fetchModule = fetch;
@ -140,6 +145,12 @@ export const getAPIModule: GetIconifyAPIModule = (
.replace('{prefix}', prefix)
.replace('{icons}', iconsList);
if (!fetchModule) {
// Fail: return 424 Failed Dependency (its not meant to be used like that, but it is the best match)
status.done(void 0, 424);
return;
}
// console.log('API query:', host + path);
fetchModule(host + path)
.then((response) => {