@happy-ts/fetch-t 1.0.14 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.cjs CHANGED
@@ -18,6 +18,7 @@ function fetchT(url, init) {
18
18
  ...rest
19
19
  } = init ?? {};
20
20
  const shouldWaitTimeout = timeout != null;
21
+ let cancelTimer;
21
22
  if (shouldWaitTimeout) {
22
23
  invariant(typeof timeout === "number" && timeout > 0, () => `Timeout must be a number greater than 0 but received ${timeout}.`);
23
24
  }
@@ -27,6 +28,7 @@ function fetchT(url, init) {
27
28
  rest.signal = controller.signal;
28
29
  }
29
30
  const response = fetch(url, rest).then(async (res) => {
31
+ cancelTimer?.();
30
32
  if (!res.ok) {
31
33
  await res.body?.cancel();
32
34
  return happyRusty.Err(new Error(`fetch status: ${res.status}`));
@@ -53,26 +55,36 @@ function fetchT(url, init) {
53
55
  }
54
56
  }
55
57
  }).catch((err) => {
58
+ cancelTimer?.();
56
59
  return happyRusty.Err(err);
57
60
  });
58
61
  if (shouldWaitTimeout) {
59
- setTimeout(() => {
62
+ const timer = setTimeout(() => {
60
63
  if (!controller.signal.aborted) {
61
64
  const error = new Error();
62
65
  error.name = TIMEOUT_ERROR;
63
66
  controller.abort(error);
64
67
  }
65
68
  }, timeout);
69
+ cancelTimer = () => {
70
+ if (timer) {
71
+ clearTimeout(timer);
72
+ }
73
+ cancelTimer = null;
74
+ };
66
75
  }
67
76
  if (abortable) {
68
77
  return {
69
78
  abort(reason) {
79
+ cancelTimer?.();
70
80
  controller.abort(reason);
71
81
  },
72
82
  get aborted() {
73
83
  return controller.signal.aborted;
74
84
  },
75
- response
85
+ get response() {
86
+ return response;
87
+ }
76
88
  };
77
89
  } else {
78
90
  return response;
package/dist/main.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.cjs","sources":["../src/fetch/defines.ts","../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { AsyncResult } from 'happy-rusty';\n\n/**\n * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport type FetchResponse<T> = AsyncResult<T, any>;\n\n/**\n * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport interface FetchTask<T> {\n /**\n * Aborts the fetch task, optionally with a reason for the abortion.\n *\n * @param reason - An optional parameter to indicate why the task was aborted.\n */\n abort(reason?: any): void;\n\n /**\n * Indicates whether the fetch task has been aborted.\n */\n aborted: boolean;\n\n /**\n * The response of the fetch task, represented as an `AsyncResult`.\n */\n response: FetchResponse<T>;\n}\n\n/**\n * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.\n */\nexport interface FetchInit extends RequestInit {\n /**\n * Indicates whether the fetch request should be abortable.\n */\n abortable?: boolean;\n\n /**\n * Specifies the expected response type of the fetch request.\n */\n responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';\n\n /**\n * Specifies the maximum time in milliseconds to wait for the fetch request to complete.\n */\n timeout?: number;\n}\n\n/**\n * Name of abort error;\n */\nexport const ABORT_ERROR = 'AbortError';\n\n/**\n * Name of timeout error;\n */\nexport const TIMEOUT_ERROR = 'TimeoutError';","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport { TIMEOUT_ERROR, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the response data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'text'.\n * @returns A `FetchResponse` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.\n * @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'arraybuffer';\n}): FetchResponse<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'blob'.\n * @returns A `FetchResponse` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'blob';\n}): FetchResponse<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'json'.\n * @returns A `FetchResponse` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n responseType: 'json';\n}): FetchResponse<T>;\n\n/**\n * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.\n * @returns A `FetchTask` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init: FetchInit): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.\n */\nexport function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n // most cases\n if (typeof url !== 'string') {\n invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);\n }\n\n const {\n // default not abort able\n abortable = false,\n responseType,\n timeout,\n ...rest\n } = init ?? {};\n\n const shouldWaitTimeout = timeout != null;\n\n if (shouldWaitTimeout) {\n invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);\n }\n\n let controller: AbortController;\n\n if (abortable || shouldWaitTimeout) {\n controller = new AbortController();\n rest.signal = controller.signal;\n }\n\n const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {\n if (!res.ok) {\n await res.body?.cancel();\n return Err(new Error(`fetch status: ${ res.status }`));\n }\n\n switch (responseType) {\n case 'arraybuffer': {\n return Ok(await res.arrayBuffer() as T);\n }\n case 'blob': {\n return Ok(await res.blob() as T);\n }\n case 'json': {\n try {\n return Ok(await res.json() as T);\n } catch {\n return Err(new Error('Response is invalid json while responseType is json'));\n }\n }\n case 'text': {\n return Ok(await res.text() as T);\n }\n default: {\n // default return the Response object\n return Ok(res as T);\n }\n }\n }).catch((err) => {\n return Err(err);\n });\n\n if (shouldWaitTimeout) {\n setTimeout(() => {\n if (!controller.signal.aborted) {\n const error = new Error();\n error.name = TIMEOUT_ERROR;\n controller.abort(error);\n }\n }, timeout);\n }\n\n if (abortable) {\n return {\n abort(reason?: any): void {\n controller.abort(reason);\n },\n get aborted(): boolean {\n return controller.signal.aborted;\n },\n response,\n };\n } else {\n return response;\n }\n}"],"names":["Err","Ok"],"mappings":";;;;;AAyDO,MAAM,WAAc,GAAA,aAAA;AAKpB,MAAM,aAAgB,GAAA;;AC2Eb,SAAA,MAAA,CAAU,KAAmB,IAAmD,EA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IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;;;"}
1
+ {"version":3,"file":"main.cjs","sources":["../src/fetch/defines.ts","../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { AsyncResult } from 'happy-rusty';\n\n/**\n * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport type FetchResponse<T> = AsyncResult<T, any>;\n\n/**\n * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport interface FetchTask<T> {\n /**\n * Aborts the fetch task, optionally with a reason for the abortion.\n *\n * @param reason - An optional parameter to indicate why the task was aborted.\n */\n abort(reason?: any): void;\n\n /**\n * Indicates whether the fetch task has been aborted.\n */\n readonly aborted: boolean;\n\n /**\n * The response of the fetch task, represented as an `AsyncResult`.\n */\n readonly response: FetchResponse<T>;\n}\n\n/**\n * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.\n */\nexport interface FetchInit extends RequestInit {\n /**\n * Indicates whether the fetch request should be abortable.\n */\n abortable?: boolean;\n\n /**\n * Specifies the expected response type of the fetch request.\n */\n responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';\n\n /**\n * Specifies the maximum time in milliseconds to wait for the fetch request to complete.\n */\n timeout?: number;\n}\n\n/**\n * Name of abort error;\n */\nexport const ABORT_ERROR = 'AbortError' as const;\n\n/**\n * Name of timeout error;\n */\nexport const TIMEOUT_ERROR = 'TimeoutError' as const;","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport { TIMEOUT_ERROR, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the response data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'text'.\n * @returns A `FetchResponse` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.\n * @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'arraybuffer';\n}): FetchResponse<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'blob'.\n * @returns A `FetchResponse` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'blob';\n}): FetchResponse<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'json'.\n * @returns A `FetchResponse` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n responseType: 'json';\n}): FetchResponse<T>;\n\n/**\n * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.\n * @returns A `FetchTask` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init: FetchInit): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.\n */\nexport function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n // most cases\n if (typeof url !== 'string') {\n invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);\n }\n\n const {\n // default not abort able\n abortable = false,\n responseType,\n timeout,\n ...rest\n } = init ?? {};\n\n const shouldWaitTimeout = timeout != null;\n let cancelTimer: (() => void) | null;\n\n if (shouldWaitTimeout) {\n invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);\n }\n\n let controller: AbortController;\n\n if (abortable || shouldWaitTimeout) {\n controller = new AbortController();\n rest.signal = controller.signal;\n }\n\n const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {\n cancelTimer?.();\n\n if (!res.ok) {\n await res.body?.cancel();\n return Err(new Error(`fetch status: ${ res.status }`));\n }\n\n switch (responseType) {\n case 'arraybuffer': {\n return Ok(await res.arrayBuffer() as T);\n }\n case 'blob': {\n return Ok(await res.blob() as T);\n }\n case 'json': {\n try {\n return Ok(await res.json() as T);\n } catch {\n return Err(new Error('Response is invalid json while responseType is json'));\n }\n }\n case 'text': {\n return Ok(await res.text() as T);\n }\n default: {\n // default return the Response object\n return Ok(res as T);\n }\n }\n }).catch((err) => {\n cancelTimer?.();\n\n return Err(err);\n });\n\n if (shouldWaitTimeout) {\n const timer = setTimeout(() => {\n if (!controller.signal.aborted) {\n const error = new Error();\n error.name = TIMEOUT_ERROR;\n controller.abort(error);\n }\n }, timeout);\n\n cancelTimer = (): void => {\n if (timer) {\n clearTimeout(timer);\n }\n\n cancelTimer = null;\n };\n }\n\n if (abortable) {\n return {\n abort(reason?: any): void {\n cancelTimer?.();\n controller.abort(reason);\n },\n get aborted(): boolean {\n return controller.signal.aborted;\n },\n get response(): FetchResponse<T> {\n return response;\n },\n };\n } else {\n return response;\n }\n}"],"names":["Err","Ok"],"mappings":";;;;;AAyDO,MAAM,WAAc,GAAA,aAAA;AAKpB,MAAM,aAAgB,GAAA;;AC2Eb,SAAA,MAAA,CAAU,KAAmB,IAAmD,EAAA;AAE5F,EAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AACzB,IAAA,SAAA,CAAU,GAAe,YAAA,GAAA,EAAK,MAAM,CAAA,gDAAA,EAAoD,GAAI,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,GACnG;AAEA,EAAM,MAAA;AAAA;AAAA,IAEF,SAAY,GAAA,KAAA;AAAA,IACZ,YAAA;AAAA,IACA,OAAA;AAAA,IACA,GAAG,IAAA;AAAA,GACP,GAAI,QAAQ,EAAC,CAAA;AAEb,EAAA,MAAM,oBAAoB,OAAW,IAAA,IAAA,CAAA;AACrC,EAAI,IAAA,WAAA,CAAA;AAEJ,EAAA,IAAI,iBAAmB,EAAA;AACnB,IAAU,SAAA,CAAA,OAAO,YAAY,QAAY,IAAA,OAAA,GAAU,GAAG,MAAM,CAAA,qDAAA,EAAyD,OAAQ,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,GACpI;AAEA,EAAI,IAAA,UAAA,CAAA;AAEJ,EAAA,IAAI,aAAa,iBAAmB,EAAA;AAChC,IAAA,UAAA,GAAa,IAAI,eAAgB,EAAA,CAAA;AACjC,IAAA,IAAA,CAAK,SAAS,UAAW,CAAA,MAAA,CAAA;AAAA,GAC7B;AAEA,EAAA,MAAM,WAA6B,KAAM,CAAA,GAAA,EAAK,IAAI,CAAE,CAAA,IAAA,CAAK,OAAO,GAA0B,KAAA;AACtF,IAAc,WAAA,IAAA,CAAA;AAEd,IAAI,IA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AACxB,QAAA,KAAA,CAAM,IAAO,GAAA,aAAA,CAAA;AACb,QAAA,UAAA,CAAW,MAAM,KAAK,CAAA,CAAA;AAAA,OAC1B;AAAA,OACD,OAAO,CAAA,CAAA;AAEV,IAAA,WAAA,GAAc,MAAY;AACtB,MAAA,IAAI,KAAO,EAAA;AACP,QAAA,YAAA,CAAa,KAAK,CAAA,CAAA;AAAA,OACtB;AAEA,MAAc,WAAA,GAAA,IAAA,CAAA;AAAA,KAClB,CAAA;AAAA,GACJ;AAEA,EAAA,IAAI,SAAW,EAAA;AACX,IAAO,OAAA;AAAA,MACH,MAAM,MAAoB,EAAA;AACtB,QAAc,WAAA,IAAA,CAAA;AACd,QAAA,UAAA,CAAW,MAAM,MAAM,CAAA,CAAA;AAAA,OAC3B;AAAA,MACA,IAAI,OAAmB,GAAA;AACnB,QAAA,OAAO,WAAW,MAAO,CAAA,OAAA,CAAA;AAAA,OAC7B;AAAA,MACA,IAAI,QAA6B,GAAA;AAC7B,QAAO,OAAA,QAAA,CAAA;AAAA,OACX;AAAA,KACJ,CAAA;AAAA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;;;"}
package/dist/main.mjs CHANGED
@@ -16,6 +16,7 @@ function fetchT(url, init) {
16
16
  ...rest
17
17
  } = init ?? {};
18
18
  const shouldWaitTimeout = timeout != null;
19
+ let cancelTimer;
19
20
  if (shouldWaitTimeout) {
20
21
  invariant(typeof timeout === "number" && timeout > 0, () => `Timeout must be a number greater than 0 but received ${timeout}.`);
21
22
  }
@@ -25,6 +26,7 @@ function fetchT(url, init) {
25
26
  rest.signal = controller.signal;
26
27
  }
27
28
  const response = fetch(url, rest).then(async (res) => {
29
+ cancelTimer?.();
28
30
  if (!res.ok) {
29
31
  await res.body?.cancel();
30
32
  return Err(new Error(`fetch status: ${res.status}`));
@@ -51,26 +53,36 @@ function fetchT(url, init) {
51
53
  }
52
54
  }
53
55
  }).catch((err) => {
56
+ cancelTimer?.();
54
57
  return Err(err);
55
58
  });
56
59
  if (shouldWaitTimeout) {
57
- setTimeout(() => {
60
+ const timer = setTimeout(() => {
58
61
  if (!controller.signal.aborted) {
59
62
  const error = new Error();
60
63
  error.name = TIMEOUT_ERROR;
61
64
  controller.abort(error);
62
65
  }
63
66
  }, timeout);
67
+ cancelTimer = () => {
68
+ if (timer) {
69
+ clearTimeout(timer);
70
+ }
71
+ cancelTimer = null;
72
+ };
64
73
  }
65
74
  if (abortable) {
66
75
  return {
67
76
  abort(reason) {
77
+ cancelTimer?.();
68
78
  controller.abort(reason);
69
79
  },
70
80
  get aborted() {
71
81
  return controller.signal.aborted;
72
82
  },
73
- response
83
+ get response() {
84
+ return response;
85
+ }
74
86
  };
75
87
  } else {
76
88
  return response;
package/dist/main.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.mjs","sources":["../src/fetch/defines.ts","../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { AsyncResult } from 'happy-rusty';\n\n/**\n * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport type FetchResponse<T> = AsyncResult<T, any>;\n\n/**\n * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport interface FetchTask<T> {\n /**\n * Aborts the fetch task, optionally with a reason for the abortion.\n *\n * @param reason - An optional parameter to indicate why the task was aborted.\n */\n abort(reason?: any): void;\n\n /**\n * Indicates whether the fetch task has been aborted.\n */\n aborted: boolean;\n\n /**\n * The response of the fetch task, represented as an `AsyncResult`.\n */\n response: FetchResponse<T>;\n}\n\n/**\n * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.\n */\nexport interface FetchInit extends RequestInit {\n /**\n * Indicates whether the fetch request should be abortable.\n */\n abortable?: boolean;\n\n /**\n * Specifies the expected response type of the fetch request.\n */\n responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';\n\n /**\n * Specifies the maximum time in milliseconds to wait for the fetch request to complete.\n */\n timeout?: number;\n}\n\n/**\n * Name of abort error;\n */\nexport const ABORT_ERROR = 'AbortError';\n\n/**\n * Name of timeout error;\n */\nexport const TIMEOUT_ERROR = 'TimeoutError';","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport { TIMEOUT_ERROR, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the response data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'text'.\n * @returns A `FetchResponse` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.\n * @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'arraybuffer';\n}): FetchResponse<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'blob'.\n * @returns A `FetchResponse` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'blob';\n}): FetchResponse<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'json'.\n * @returns A `FetchResponse` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n responseType: 'json';\n}): FetchResponse<T>;\n\n/**\n * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.\n * @returns A `FetchTask` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init: FetchInit): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.\n */\nexport function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n // most cases\n if (typeof url !== 'string') {\n invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);\n }\n\n const {\n // default not abort able\n abortable = false,\n responseType,\n timeout,\n ...rest\n } = init ?? {};\n\n const shouldWaitTimeout = timeout != null;\n\n if (shouldWaitTimeout) {\n invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);\n }\n\n let controller: AbortController;\n\n if (abortable || shouldWaitTimeout) {\n controller = new AbortController();\n rest.signal = controller.signal;\n }\n\n const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {\n if (!res.ok) {\n await res.body?.cancel();\n return Err(new Error(`fetch status: ${ res.status }`));\n }\n\n switch (responseType) {\n case 'arraybuffer': {\n return Ok(await res.arrayBuffer() as T);\n }\n case 'blob': {\n return Ok(await res.blob() as T);\n }\n case 'json': {\n try {\n return Ok(await res.json() as T);\n } catch {\n return Err(new Error('Response is invalid json while responseType is json'));\n }\n }\n case 'text': {\n return Ok(await res.text() as T);\n }\n default: {\n // default return the Response object\n return Ok(res as T);\n }\n }\n }).catch((err) => {\n return Err(err);\n });\n\n if (shouldWaitTimeout) {\n setTimeout(() => {\n if (!controller.signal.aborted) {\n const error = new Error();\n error.name = TIMEOUT_ERROR;\n controller.abort(error);\n }\n }, timeout);\n }\n\n if (abortable) {\n return {\n abort(reason?: any): void {\n controller.abort(reason);\n },\n get aborted(): boolean {\n return controller.signal.aborted;\n },\n response,\n };\n } else {\n return response;\n }\n}"],"names":[],"mappings":";;;AAyDO,MAAM,WAAc,GAAA,aAAA;AAKpB,MAAM,aAAgB,GAAA;;AC2Eb,SAAA,MAAA,CAAU,KAAmB,IAAmD,EAAA;AAE5F,EAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AACzB,IAAA,SAAA,CAAU,GAAe,YAAA,GAAA,EAAK,MAAM,CAAA,gDAAA,EAAoD,GAAI,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,GACnG;AAEA,EAAM,MAAA;AAAA;AAAA,IAEF,SAAY,GAAA,KAAA;AAAA,IACZ,YAAA;AAAA,IACA,OAAA;AAAA,IACA,GAAG,IAAA;AAAA,GACP,GAAI,QAAQ,EAAC,CAAA;AAEb,EAAA,MAAM,oBAAoB,OAAW,IAAA,IAAA,CAAA;AAErC,EAAA,IAAI,iBAAmB,EAAA;AACnB,IAAU,SAAA,CAAA,OAAO,YAAY,QAAY,IAAA,OAAA,GAAU,GAAG,MAAM,CAAA,qDAAA,EAAyD,OAAQ,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,GACpI;AAEA,EAAI,IAAA,UAAA,CAAA;AAEJ,EAAA,IAAI,aAAa,iBAAmB,EAAA;AAChC,IAAA,UAAA,GAA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,EAAA;AACnB,IAAA,UAAA,CAAW,MAAM;AACb,MAAI,IAAA,CAAC,UAAW,CAAA,MAAA,CAAO,OAAS,EAAA;AAC5B,QAAM,MAAA,KAAA,GAAQ,IAAI,KAAM,EAAA,CAAA;AACxB,QAAA,KAAA,CAAM,IAAO,GAAA,aAAA,CAAA;AACb,QAAA,UAAA,CAAW,MAAM,KAAK,CAAA,CAAA;AAAA,OAC1B;AAAA,OACD,OAAO,CAAA,CAAA;AAAA,GACd;AAEA,EAAA,IAAI,SAAW,EAAA;AACX,IAAO,OAAA;AAAA,MACH,MAAM,MAAoB,EAAA;AACtB,QAAA,UAAA,CAAW,MAAM,MAAM,CAAA,CAAA;AAAA,OAC3B;AAAA,MACA,IAAI,OAAmB,GAAA;AACnB,QAAA,OAAO,WAAW,MAAO,CAAA,OAAA,CAAA;AAAA,OAC7B;AAAA,MACA,QAAA;AAAA,KACJ,CAAA;AAAA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;"}
1
+ {"version":3,"file":"main.mjs","sources":["../src/fetch/defines.ts","../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { AsyncResult } from 'happy-rusty';\n\n/**\n * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport type FetchResponse<T> = AsyncResult<T, any>;\n\n/**\n * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport interface FetchTask<T> {\n /**\n * Aborts the fetch task, optionally with a reason for the abortion.\n *\n * @param reason - An optional parameter to indicate why the task was aborted.\n */\n abort(reason?: any): void;\n\n /**\n * Indicates whether the fetch task has been aborted.\n */\n readonly aborted: boolean;\n\n /**\n * The response of the fetch task, represented as an `AsyncResult`.\n */\n readonly response: FetchResponse<T>;\n}\n\n/**\n * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.\n */\nexport interface FetchInit extends RequestInit {\n /**\n * Indicates whether the fetch request should be abortable.\n */\n abortable?: boolean;\n\n /**\n * Specifies the expected response type of the fetch request.\n */\n responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';\n\n /**\n * Specifies the maximum time in milliseconds to wait for the fetch request to complete.\n */\n timeout?: number;\n}\n\n/**\n * Name of abort error;\n */\nexport const ABORT_ERROR = 'AbortError' as const;\n\n/**\n * Name of timeout error;\n */\nexport const TIMEOUT_ERROR = 'TimeoutError' as const;","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport { TIMEOUT_ERROR, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the response data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'text'.\n * @returns A `FetchResponse` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.\n * @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'arraybuffer';\n}): FetchResponse<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'blob'.\n * @returns A `FetchResponse` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'blob';\n}): FetchResponse<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'json'.\n * @returns A `FetchResponse` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n responseType: 'json';\n}): FetchResponse<T>;\n\n/**\n * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.\n * @returns A `FetchTask` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init: FetchInit): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.\n */\nexport function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n // most cases\n if (typeof url !== 'string') {\n invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);\n }\n\n const {\n // default not abort able\n abortable = false,\n responseType,\n timeout,\n ...rest\n } = init ?? {};\n\n const shouldWaitTimeout = timeout != null;\n let cancelTimer: (() => void) | null;\n\n if (shouldWaitTimeout) {\n invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);\n }\n\n let controller: AbortController;\n\n if (abortable || shouldWaitTimeout) {\n controller = new AbortController();\n rest.signal = controller.signal;\n }\n\n const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {\n cancelTimer?.();\n\n if (!res.ok) {\n await res.body?.cancel();\n return Err(new Error(`fetch status: ${ res.status }`));\n }\n\n switch (responseType) {\n case 'arraybuffer': {\n return Ok(await res.arrayBuffer() as T);\n }\n case 'blob': {\n return Ok(await res.blob() as T);\n }\n case 'json': {\n try {\n return Ok(await res.json() as T);\n } catch {\n return Err(new Error('Response is invalid json while responseType is json'));\n }\n }\n case 'text': {\n return Ok(await res.text() as T);\n }\n default: {\n // default return the Response object\n return Ok(res as T);\n }\n }\n }).catch((err) => {\n cancelTimer?.();\n\n return Err(err);\n });\n\n if (shouldWaitTimeout) {\n const timer = setTimeout(() => {\n if (!controller.signal.aborted) {\n const error = new Error();\n error.name = TIMEOUT_ERROR;\n controller.abort(error);\n }\n }, timeout);\n\n cancelTimer = (): void => {\n if (timer) {\n clearTimeout(timer);\n }\n\n cancelTimer = null;\n };\n }\n\n if (abortable) {\n return {\n abort(reason?: any): void {\n cancelTimer?.();\n controller.abort(reason);\n },\n get aborted(): boolean {\n return controller.signal.aborted;\n },\n get response(): FetchResponse<T> {\n return response;\n },\n };\n } else {\n return response;\n }\n}"],"names":[],"mappings":";;;AAyDO,MAAM,WAAc,GAAA,aAAA;AAKpB,MAAM,aAAgB,GAAA;;AC2Eb,SAAA,MAAA,CAAU,KAAmB,IAAmD,EAAA;AAE5F,EAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AACzB,IAAA,SAAA,CAAU,GAAe,YAAA,GAAA,EAAK,MAAM,CAAA,gDAAA,EAAoD,GAAI,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,GACnG;AAEA,EAAM,MAAA;AAAA;AAAA,IAEF,SAAY,GAAA,KAAA;AAAA,IACZ,YAAA;AAAA,IACA,OAAA;AAAA,IACA,GAAG,IAAA;AAAA,GACP,GAAI,QAAQ,EAAC,CAAA;AAEb,EAAA,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AA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;"}
package/dist/types.d.ts CHANGED
@@ -21,11 +21,11 @@ interface FetchTask<T> {
21
21
  /**
22
22
  * Indicates whether the fetch task has been aborted.
23
23
  */
24
- aborted: boolean;
24
+ readonly aborted: boolean;
25
25
  /**
26
26
  * The response of the fetch task, represented as an `AsyncResult`.
27
27
  */
28
- response: FetchResponse<T>;
28
+ readonly response: FetchResponse<T>;
29
29
  }
30
30
  /**
31
31
  * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
@@ -47,11 +47,11 @@ interface FetchInit extends RequestInit {
47
47
  /**
48
48
  * Name of abort error;
49
49
  */
50
- declare const ABORT_ERROR = "AbortError";
50
+ declare const ABORT_ERROR: "AbortError";
51
51
  /**
52
52
  * Name of timeout error;
53
53
  */
54
- declare const TIMEOUT_ERROR = "TimeoutError";
54
+ declare const TIMEOUT_ERROR: "TimeoutError";
55
55
 
56
56
  /**
57
57
  * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
@@ -41,6 +41,8 @@ Fetches a resource from the network as a text string and returns a `FetchTask` r
41
41
 
42
42
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
43
43
 
44
+ A `FetchTask` representing the operation with a `string` response.
45
+
44
46
  ### Type Param
45
47
 
46
48
  The expected type of the response data when not using a specific `responseType`.
@@ -55,7 +57,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
55
57
 
56
58
  ### Defined in
57
59
 
58
- [fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L14)
60
+ [fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L14)
59
61
 
60
62
  ## fetchT(url, init)
61
63
 
@@ -78,6 +80,8 @@ Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask`
78
80
 
79
81
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
80
82
 
83
+ A `FetchTask` representing the operation with an `ArrayBuffer` response.
84
+
81
85
  ### Type Param
82
86
 
83
87
  The expected type of the response data when not using a specific `responseType`.
@@ -92,7 +96,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
92
96
 
93
97
  ### Defined in
94
98
 
95
- [fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L26)
99
+ [fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L26)
96
100
 
97
101
  ## fetchT(url, init)
98
102
 
@@ -115,6 +119,8 @@ Fetches a resource from the network as a Blob and returns a `FetchTask` represen
115
119
 
116
120
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
117
121
 
122
+ A `FetchTask` representing the operation with a `Blob` response.
123
+
118
124
  ### Type Param
119
125
 
120
126
  The expected type of the response data when not using a specific `responseType`.
@@ -129,7 +135,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
129
135
 
130
136
  ### Defined in
131
137
 
132
- [fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L38)
138
+ [fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L38)
133
139
 
134
140
  ## fetchT(url, init)
135
141
 
@@ -158,6 +164,8 @@ Fetches a resource from the network and parses it as JSON, returning a `FetchTas
158
164
 
159
165
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
160
166
 
167
+ A `FetchTask` representing the operation with a response parsed as JSON.
168
+
161
169
  ### Type Param
162
170
 
163
171
  The expected type of the response data when not using a specific `responseType`.
@@ -172,7 +180,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
172
180
 
173
181
  ### Defined in
174
182
 
175
- [fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L51)
183
+ [fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L51)
176
184
 
177
185
  ## fetchT(url, init)
178
186
 
@@ -195,6 +203,8 @@ Fetches a resource from the network as a text string and returns a `FetchRespons
195
203
 
196
204
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
197
205
 
206
+ A `FetchResponse` representing the operation with a `string` response.
207
+
198
208
  ### Type Param
199
209
 
200
210
  The expected type of the response data when not using a specific `responseType`.
@@ -209,7 +219,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
209
219
 
210
220
  ### Defined in
211
221
 
212
- [fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L63)
222
+ [fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L63)
213
223
 
214
224
  ## fetchT(url, init)
215
225
 
@@ -232,6 +242,8 @@ Fetches a resource from the network as an ArrayBuffer and returns a `FetchRespon
232
242
 
233
243
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
234
244
 
245
+ A `FetchResponse` representing the operation with an `ArrayBuffer` response.
246
+
235
247
  ### Type Param
236
248
 
237
249
  The expected type of the response data when not using a specific `responseType`.
@@ -246,7 +258,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
246
258
 
247
259
  ### Defined in
248
260
 
249
- [fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L74)
261
+ [fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L74)
250
262
 
251
263
  ## fetchT(url, init)
252
264
 
@@ -269,6 +281,8 @@ Fetches a resource from the network as a Blob and returns a `FetchResponse` repr
269
281
 
270
282
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
271
283
 
284
+ A `FetchResponse` representing the operation with a `Blob` response.
285
+
272
286
  ### Type Param
273
287
 
274
288
  The expected type of the response data when not using a specific `responseType`.
@@ -283,7 +297,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
283
297
 
284
298
  ### Defined in
285
299
 
286
- [fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L85)
300
+ [fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L85)
287
301
 
288
302
  ## fetchT(url, init)
289
303
 
@@ -312,6 +326,8 @@ Fetches a resource from the network and parses it as JSON, returning a `FetchRes
312
326
 
313
327
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
314
328
 
329
+ A `FetchResponse` representing the operation with a response parsed as JSON.
330
+
315
331
  ### Type Param
316
332
 
317
333
  The expected type of the response data when not using a specific `responseType`.
@@ -326,7 +342,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
326
342
 
327
343
  ### Defined in
328
344
 
329
- [fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L97)
345
+ [fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L97)
330
346
 
331
347
  ## fetchT(url, init)
332
348
 
@@ -349,6 +365,8 @@ Fetches a resource from the network and returns a `FetchTask` representing the o
349
365
 
350
366
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
351
367
 
368
+ A `FetchTask` representing the operation with a generic `Response`.
369
+
352
370
  ### Type Param
353
371
 
354
372
  The expected type of the response data when not using a specific `responseType`.
@@ -363,7 +381,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
363
381
 
364
382
  ### Defined in
365
383
 
366
- [fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L108)
384
+ [fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L108)
367
385
 
368
386
  ## fetchT(url)
369
387
 
@@ -385,6 +403,8 @@ Fetches a resource from the network and returns a `FetchResponse` representing t
385
403
 
386
404
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
387
405
 
406
+ A `FetchResponse` representing the operation with a generic `Response`.
407
+
388
408
  ### Type Param
389
409
 
390
410
  The expected type of the response data when not using a specific `responseType`.
@@ -399,7 +419,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
399
419
 
400
420
  ### Defined in
401
421
 
402
- [fetch.ts:118](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L118)
422
+ [fetch.ts:118](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L118)
403
423
 
404
424
  ## fetchT(url, init)
405
425
 
@@ -422,6 +442,8 @@ Fetches a resource from the network and returns a `FetchResponse` or `FetchTask`
422
442
 
423
443
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
424
444
 
445
+ A `FetchResponse` representing the operation with a `Response` object.
446
+
425
447
  ### Type Param
426
448
 
427
449
  The expected type of the response data when not using a specific `responseType`.
@@ -436,4 +458,4 @@ Additional options for the fetch operation, including custom `FetchInit` propert
436
458
 
437
459
  ### Defined in
438
460
 
439
- [fetch.ts:128](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/fetch.ts#L128)
461
+ [fetch.ts:128](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L128)
@@ -16,6 +16,6 @@ Extends the standard `RequestInit` interface from the Fetch API to include addit
16
16
 
17
17
  | Property | Type | Description | Defined in |
18
18
  | ------ | ------ | ------ | ------ |
19
- | `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | [defines.ts:42](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L42) |
20
- | `responseType?` | `"text"` \| `"arraybuffer"` \| `"blob"` \| `"json"` | Specifies the expected response type of the fetch request. | [defines.ts:47](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L47) |
21
- | `timeout?` | `number` | Specifies the maximum time in milliseconds to wait for the fetch request to complete. | [defines.ts:52](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L52) |
19
+ | `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | [defines.ts:42](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L42) |
20
+ | `responseType?` | `"text"` \| `"arraybuffer"` \| `"blob"` \| `"json"` | Specifies the expected response type of the fetch request. | [defines.ts:47](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L47) |
21
+ | `timeout?` | `number` | Specifies the maximum time in milliseconds to wait for the fetch request to complete. | [defines.ts:52](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L52) |
@@ -16,10 +16,10 @@ Defines the structure and behavior of a fetch task, including the ability to abo
16
16
 
17
17
  ## Properties
18
18
 
19
- | Property | Type | Description | Defined in |
20
- | ------ | ------ | ------ | ------ |
21
- | `aborted` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L27) |
22
- | `response` | [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\> | The response of the fetch task, represented as an `AsyncResult`. | [defines.ts:32](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L32) |
19
+ | Property | Modifier | Type | Description | Defined in |
20
+ | ------ | ------ | ------ | ------ | ------ |
21
+ | `aborted` | `readonly` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L27) |
22
+ | `response` | `readonly` | [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\> | The response of the fetch task, represented as an `AsyncResult`. | [defines.ts:32](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L32) |
23
23
 
24
24
  ## Methods
25
25
 
@@ -43,4 +43,4 @@ Aborts the fetch task, optionally with a reason for the abortion.
43
43
 
44
44
  #### Defined in
45
45
 
46
- [defines.ts:22](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L22)
46
+ [defines.ts:22](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L22)
@@ -20,4 +20,4 @@ Represents the response of a fetch operation, encapsulating the result data or a
20
20
 
21
21
  ## Defined in
22
22
 
23
- [defines.ts:9](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L9)
23
+ [defines.ts:9](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L9)
@@ -7,11 +7,11 @@
7
7
  # Variable: ABORT\_ERROR
8
8
 
9
9
  ```ts
10
- const ABORT_ERROR: "AbortError" = 'AbortError';
10
+ const ABORT_ERROR: "AbortError";
11
11
  ```
12
12
 
13
13
  Name of abort error;
14
14
 
15
15
  ## Defined in
16
16
 
17
- [defines.ts:58](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L58)
17
+ [defines.ts:58](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L58)
@@ -7,11 +7,11 @@
7
7
  # Variable: TIMEOUT\_ERROR
8
8
 
9
9
  ```ts
10
- const TIMEOUT_ERROR: "TimeoutError" = 'TimeoutError';
10
+ const TIMEOUT_ERROR: "TimeoutError";
11
11
  ```
12
12
 
13
13
  Name of timeout error;
14
14
 
15
15
  ## Defined in
16
16
 
17
- [defines.ts:63](https://github.com/JiangJie/fetch-t/blob/e31c84b39da301aedb3faeb020c606fa1a2c6bcb/src/fetch/defines.ts#L63)
17
+ [defines.ts:63](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/defines.ts#L63)
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Abortable fetch wrapper with the ability to specify the return type.",
4
4
  "author": "jiang115jie@gmail.com",
5
5
  "license": "GPL-3.0",
6
- "version": "1.0.14",
6
+ "version": "1.0.16",
7
7
  "type": "module",
8
8
  "source": "src/mod.ts",
9
9
  "main": "dist/main.cjs",
@@ -46,11 +46,11 @@
46
46
  "@typescript-eslint/eslint-plugin": "^7.17.0",
47
47
  "@typescript-eslint/parser": "^7.17.0",
48
48
  "eslint": "^8.57.0",
49
- "rollup": "^4.19.0",
49
+ "rollup": "^4.19.1",
50
50
  "rollup-plugin-dts": "^6.1.1",
51
51
  "rollup-plugin-esbuild": "^6.1.1",
52
52
  "typedoc": "^0.26.5",
53
- "typedoc-plugin-markdown": "^4.2.2",
53
+ "typedoc-plugin-markdown": "^4.2.3",
54
54
  "typescript": "^5.5.4"
55
55
  },
56
56
  "dependencies": {
@@ -24,12 +24,12 @@ export interface FetchTask<T> {
24
24
  /**
25
25
  * Indicates whether the fetch task has been aborted.
26
26
  */
27
- aborted: boolean;
27
+ readonly aborted: boolean;
28
28
 
29
29
  /**
30
30
  * The response of the fetch task, represented as an `AsyncResult`.
31
31
  */
32
- response: FetchResponse<T>;
32
+ readonly response: FetchResponse<T>;
33
33
  }
34
34
 
35
35
  /**
@@ -55,9 +55,9 @@ export interface FetchInit extends RequestInit {
55
55
  /**
56
56
  * Name of abort error;
57
57
  */
58
- export const ABORT_ERROR = 'AbortError';
58
+ export const ABORT_ERROR = 'AbortError' as const;
59
59
 
60
60
  /**
61
61
  * Name of timeout error;
62
62
  */
63
- export const TIMEOUT_ERROR = 'TimeoutError';
63
+ export const TIMEOUT_ERROR = 'TimeoutError' as const;
@@ -150,6 +150,7 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
150
150
  } = init ?? {};
151
151
 
152
152
  const shouldWaitTimeout = timeout != null;
153
+ let cancelTimer: (() => void) | null;
153
154
 
154
155
  if (shouldWaitTimeout) {
155
156
  invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);
@@ -163,6 +164,8 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
163
164
  }
164
165
 
165
166
  const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {
167
+ cancelTimer?.();
168
+
166
169
  if (!res.ok) {
167
170
  await res.body?.cancel();
168
171
  return Err(new Error(`fetch status: ${ res.status }`));
@@ -191,28 +194,41 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
191
194
  }
192
195
  }
193
196
  }).catch((err) => {
197
+ cancelTimer?.();
198
+
194
199
  return Err(err);
195
200
  });
196
201
 
197
202
  if (shouldWaitTimeout) {
198
- setTimeout(() => {
203
+ const timer = setTimeout(() => {
199
204
  if (!controller.signal.aborted) {
200
205
  const error = new Error();
201
206
  error.name = TIMEOUT_ERROR;
202
207
  controller.abort(error);
203
208
  }
204
209
  }, timeout);
210
+
211
+ cancelTimer = (): void => {
212
+ if (timer) {
213
+ clearTimeout(timer);
214
+ }
215
+
216
+ cancelTimer = null;
217
+ };
205
218
  }
206
219
 
207
220
  if (abortable) {
208
221
  return {
209
222
  abort(reason?: any): void {
223
+ cancelTimer?.();
210
224
  controller.abort(reason);
211
225
  },
212
226
  get aborted(): boolean {
213
227
  return controller.signal.aborted;
214
228
  },
215
- response,
229
+ get response(): FetchResponse<T> {
230
+ return response;
231
+ },
216
232
  };
217
233
  } else {
218
234
  return response;