Преглед изворни кода

fix: fetchAdvanced AbortController error

Sv443 пре 2 месеци
родитељ
комит
3f86215
2 измењених фајлова са 17 додато и 10 уклоњено
  1. 5 0
      .changeset/witty-bobcats-remember.md
  2. 12 10
      lib/misc.ts

+ 5 - 0
.changeset/witty-bobcats-remember.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": patch
+---
+
+Fixed fetchAdvanced error "'abort' called on an object that does not implement interface AbortController"

+ 12 - 10
lib/misc.ts

@@ -58,29 +58,31 @@ export type FetchAdvancedOpts = Prettify<
 /** Calls the fetch API with special options like a timeout */
 export async function fetchAdvanced(input: string | RequestInfo | URL, options: FetchAdvancedOpts = {}): Promise<Response> {
   const { timeout = 10000 } = options;
-  const { signal, abort } = new AbortController();
+  const ctl = new AbortController();
 
-  options.signal?.addEventListener("abort", abort);
+  const { signal, ...restOpts } = options;
 
-  let signalOpts: Partial<RequestInit> = {},
+  signal?.addEventListener("abort", () => ctl.abort());
+
+  let sigOpts: Partial<RequestInit> = {},
     id: ReturnType<typeof setTimeout> | undefined = undefined;
 
   if(timeout >= 0) {
-    id = setTimeout(() => abort(), timeout);
-    signalOpts = { signal };
+    id = setTimeout(() => ctl.abort(), timeout);
+    sigOpts = { signal: ctl.signal };
   }
 
   try {
     const res = await fetch(input, {
-      ...options,
-      ...signalOpts,
+      ...restOpts,
+      ...sigOpts,
     });
-    id && clearTimeout(id);
+    typeof id !== "undefined" && clearTimeout(id);
     return res;
   }
   catch(err) {
-    id && clearTimeout(id);
-    throw err;
+    typeof id !== "undefined" && clearTimeout(id);
+    throw new Error("Error while calling fetch", { cause: err });
   }
 }