@happy-ts/fetch-t 1.0.13 → 1.0.15

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,20 +55,28 @@ 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() {
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 * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: false;\n}): FetchResponse<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 * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n timeout: number;\n}): 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` or `FetchTask` depending on the `abortable` option in `init`.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;\n\n/**\n * Fetches a resource from the network 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 - Standard `RequestInit` options for the fetch operation.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init?: RequestInit): 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;;ACkGb,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,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,C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AAA,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.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 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 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,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,qDAA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G,GAAQ,CAAA,CAAA;AAAA,OACtB;AAAA,KACJ;AAAA,GACH,CAAA,CAAE,KAAM,CAAA,CAAC,GAAQ,KAAA;AACd,IAAc,WAAA,IAAA,CAAA;AAEd,IAAA,OAAOD,eAAI,GAAG,CAAA,CAAA;AAAA,GACjB,CAAA,CAAA;AAED,EAAA,IAAI,iBAAmB,EAAA;AACnB,IAAM,MAAA,KAAA,GAAQ,WAAW,MAAM;AAC3B,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;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,QAAA;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,20 +53,28 @@ 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() {
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 * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: false;\n}): FetchResponse<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 * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.\n * @returns A `FetchResponse` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n timeout: number;\n}): 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` or `FetchTask` depending on the `abortable` option in `init`.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;\n\n/**\n * Fetches a resource from the network 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 - Standard `RequestInit` options for the fetch operation.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init?: RequestInit): 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;\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nB,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 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 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 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;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,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C5B,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;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,QAAA;AAAA,KACJ,CAAA;AAAA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;"}
package/dist/types.d.ts CHANGED
@@ -154,39 +154,18 @@ declare function fetchT(url: string | URL, init: FetchInit & {
154
154
  * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
155
155
  *
156
156
  * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
157
- * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
158
157
  * @returns A `FetchResponse` representing the operation with a generic `Response`.
159
158
  */
160
- declare function fetchT(url: string | URL, init: FetchInit & {
161
- abortable: false;
162
- }): FetchResponse<Response>;
163
- /**
164
- * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
165
- *
166
- * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
167
- * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
168
- * @returns A `FetchResponse` representing the operation with a generic `Response`.
169
- */
170
- declare function fetchT(url: string | URL, init: FetchInit & {
171
- timeout: number;
172
- }): FetchResponse<Response>;
159
+ declare function fetchT(url: string | URL): FetchResponse<Response>;
173
160
  /**
174
161
  * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
175
162
  *
176
163
  * @typeParam T - The expected type of the response data when not using a specific `responseType`.
177
164
  * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
178
165
  * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
179
- * @returns A `FetchResponse` or `FetchTask` depending on the `abortable` option in `init`.
180
- */
181
- declare function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;
182
- /**
183
- * Fetches a resource from the network and returns a `FetchResponse` representing the operation.
184
- *
185
- * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
186
- * @param init - Standard `RequestInit` options for the fetch operation.
187
166
  * @returns A `FetchResponse` representing the operation with a `Response` object.
188
167
  */
189
- declare function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;
168
+ declare function fetchT(url: string | URL, init: FetchInit): FetchResponse<Response>;
190
169
 
191
170
  export { ABORT_ERROR, type FetchInit, type FetchResponse, type FetchTask, TIMEOUT_ERROR, fetchT };
192
171
  //# sourceMappingURL=types.d.ts.map
@@ -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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L14)
60
+ [fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L26)
99
+ [fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L38)
138
+ [fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L51)
183
+ [fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L63)
222
+ [fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L74)
261
+ [fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L85)
300
+ [fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L97)
345
+ [fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/src/fetch/fetch.ts#L97)
330
346
 
331
347
  ## fetchT(url, init)
332
348
 
@@ -349,42 +365,7 @@ 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
 
352
- ### Type Param
353
-
354
- The expected type of the response data when not using a specific `responseType`.
355
-
356
- ### Param
357
-
358
- The resource to fetch. Can be a URL object or a string representing a URL.
359
-
360
- ### Param
361
-
362
- Additional options for the fetch operation, including custom `FetchInit` properties.
363
-
364
- ### Defined in
365
-
366
- [fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L108)
367
-
368
- ## fetchT(url, init)
369
-
370
- ```ts
371
- function fetchT(url, init): FetchResponse<Response>
372
- ```
373
-
374
- Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
375
-
376
- ### Parameters
377
-
378
- | Parameter | Type | Description |
379
- | ------ | ------ | ------ |
380
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
381
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `false`; \} | Additional options for the fetch operation, indicating that the operation should not be abortable. |
382
-
383
- ### Returns
384
-
385
- [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
386
-
387
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
368
+ A `FetchTask` representing the operation with a generic `Response`.
388
369
 
389
370
  ### Type Param
390
371
 
@@ -400,12 +381,12 @@ Additional options for the fetch operation, including custom `FetchInit` propert
400
381
 
401
382
  ### Defined in
402
383
 
403
- [fetch.ts:119](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L119)
384
+ [fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/src/fetch/fetch.ts#L108)
404
385
 
405
- ## fetchT(url, init)
386
+ ## fetchT(url)
406
387
 
407
388
  ```ts
408
- function fetchT(url, init): FetchResponse<Response>
389
+ function fetchT(url): FetchResponse<Response>
409
390
  ```
410
391
 
411
392
  Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
@@ -415,7 +396,6 @@ Fetches a resource from the network and returns a `FetchResponse` representing t
415
396
  | Parameter | Type | Description |
416
397
  | ------ | ------ | ------ |
417
398
  | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
418
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `timeout`: `number`; \} | Additional options for the fetch operation, indicating that the operation should not be abortable. |
419
399
 
420
400
  ### Returns
421
401
 
@@ -423,6 +403,8 @@ Fetches a resource from the network and returns a `FetchResponse` representing t
423
403
 
424
404
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
425
405
 
406
+ A `FetchResponse` representing the operation with a generic `Response`.
407
+
426
408
  ### Type Param
427
409
 
428
410
  The expected type of the response data when not using a specific `responseType`.
@@ -437,22 +419,16 @@ Additional options for the fetch operation, including custom `FetchInit` propert
437
419
 
438
420
  ### Defined in
439
421
 
440
- [fetch.ts:130](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L130)
422
+ [fetch.ts:118](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/src/fetch/fetch.ts#L118)
441
423
 
442
424
  ## fetchT(url, init)
443
425
 
444
426
  ```ts
445
- function fetchT<T>(url, init): FetchTask<T> | FetchResponse<T>
427
+ function fetchT(url, init): FetchResponse<Response>
446
428
  ```
447
429
 
448
430
  Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
449
431
 
450
- ### Type Parameters
451
-
452
- | Type Parameter | Description |
453
- | ------ | ------ |
454
- | `T` | The expected type of the response data when not using a specific `responseType`. |
455
-
456
432
  ### Parameters
457
433
 
458
434
  | Parameter | Type | Description |
@@ -462,47 +438,12 @@ Fetches a resource from the network and returns a `FetchResponse` or `FetchTask`
462
438
 
463
439
  ### Returns
464
440
 
465
- [`FetchTask`](../interfaces/FetchTask.md)\<`T`\> \| [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
466
-
467
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
468
-
469
- ### Type Param
470
-
471
- The expected type of the response data when not using a specific `responseType`.
472
-
473
- ### Param
474
-
475
- The resource to fetch. Can be a URL object or a string representing a URL.
476
-
477
- ### Param
478
-
479
- Additional options for the fetch operation, including custom `FetchInit` properties.
480
-
481
- ### Defined in
482
-
483
- [fetch.ts:142](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L142)
484
-
485
- ## fetchT(url, init)
486
-
487
- ```ts
488
- function fetchT(url, init?): FetchResponse<Response>
489
- ```
490
-
491
- Fetches a resource from the network and returns a `FetchResponse` representing the operation.
492
-
493
- ### Parameters
494
-
495
- | Parameter | Type | Description |
496
- | ------ | ------ | ------ |
497
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
498
- | `init`? | `RequestInit` | Standard `RequestInit` options for the fetch operation. |
499
-
500
- ### Returns
501
-
502
441
  [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
503
442
 
504
443
  A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
505
444
 
445
+ A `FetchResponse` representing the operation with a `Response` object.
446
+
506
447
  ### Type Param
507
448
 
508
449
  The expected type of the response data when not using a specific `responseType`.
@@ -517,4 +458,4 @@ Additional options for the fetch operation, including custom `FetchInit` propert
517
458
 
518
459
  ### Defined in
519
460
 
520
- [fetch.ts:151](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L151)
461
+ [fetch.ts:128](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/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/c5e0121b76a0dea415931120c31c2d070cc2469d/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/c5e0121b76a0dea415931120c31c2d070cc2469d/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/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/src/fetch/defines.ts#L52) |
@@ -18,8 +18,8 @@ Defines the structure and behavior of a fetch task, including the ability to abo
18
18
 
19
19
  | Property | Type | Description | Defined in |
20
20
  | ------ | ------ | ------ | ------ |
21
- | `aborted` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L32) |
21
+ | `aborted` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L22)
46
+ [defines.ts:22](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L9)
23
+ [defines.ts:9](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/src/fetch/defines.ts#L9)
@@ -14,4 +14,4 @@ Name of abort error;
14
14
 
15
15
  ## Defined in
16
16
 
17
- [defines.ts:58](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L58)
17
+ [defines.ts:58](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/src/fetch/defines.ts#L58)
@@ -14,4 +14,4 @@ Name of timeout error;
14
14
 
15
15
  ## Defined in
16
16
 
17
- [defines.ts:63](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L63)
17
+ [defines.ts:63](https://github.com/JiangJie/fetch-t/blob/9e5d8bc709fe9cc1630d97e0ca16ef403ee959bb/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.13",
6
+ "version": "1.0.15",
7
7
  "type": "module",
8
8
  "source": "src/mod.ts",
9
9
  "main": "dist/main.cjs",
@@ -50,7 +50,7 @@
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": {
@@ -113,23 +113,9 @@ export function fetchT(url: string | URL, init: FetchInit & {
113
113
  * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
114
114
  *
115
115
  * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
116
- * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
117
116
  * @returns A `FetchResponse` representing the operation with a generic `Response`.
118
117
  */
119
- export function fetchT(url: string | URL, init: FetchInit & {
120
- abortable: false;
121
- }): FetchResponse<Response>;
122
-
123
- /**
124
- * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
125
- *
126
- * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
127
- * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
128
- * @returns A `FetchResponse` representing the operation with a generic `Response`.
129
- */
130
- export function fetchT(url: string | URL, init: FetchInit & {
131
- timeout: number;
132
- }): FetchResponse<Response>;
118
+ export function fetchT(url: string | URL): FetchResponse<Response>;
133
119
 
134
120
  /**
135
121
  * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
@@ -137,18 +123,9 @@ export function fetchT(url: string | URL, init: FetchInit & {
137
123
  * @typeParam T - The expected type of the response data when not using a specific `responseType`.
138
124
  * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
139
125
  * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
140
- * @returns A `FetchResponse` or `FetchTask` depending on the `abortable` option in `init`.
141
- */
142
- export function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;
143
-
144
- /**
145
- * Fetches a resource from the network and returns a `FetchResponse` representing the operation.
146
- *
147
- * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
148
- * @param init - Standard `RequestInit` options for the fetch operation.
149
126
  * @returns A `FetchResponse` representing the operation with a `Response` object.
150
127
  */
151
- export function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;
128
+ export function fetchT(url: string | URL, init: FetchInit): FetchResponse<Response>;
152
129
 
153
130
  /**
154
131
  * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.
@@ -173,6 +150,7 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
173
150
  } = init ?? {};
174
151
 
175
152
  const shouldWaitTimeout = timeout != null;
153
+ let cancelTimer: (() => void) | null;
176
154
 
177
155
  if (shouldWaitTimeout) {
178
156
  invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);
@@ -186,6 +164,8 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
186
164
  }
187
165
 
188
166
  const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {
167
+ cancelTimer?.();
168
+
189
169
  if (!res.ok) {
190
170
  await res.body?.cancel();
191
171
  return Err(new Error(`fetch status: ${ res.status }`));
@@ -214,22 +194,33 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
214
194
  }
215
195
  }
216
196
  }).catch((err) => {
197
+ cancelTimer?.();
198
+
217
199
  return Err(err);
218
200
  });
219
201
 
220
202
  if (shouldWaitTimeout) {
221
- setTimeout(() => {
203
+ const timer = setTimeout(() => {
222
204
  if (!controller.signal.aborted) {
223
205
  const error = new Error();
224
206
  error.name = TIMEOUT_ERROR;
225
207
  controller.abort(error);
226
208
  }
227
209
  }, timeout);
210
+
211
+ cancelTimer = (): void => {
212
+ if (timer) {
213
+ clearTimeout(timer);
214
+ }
215
+
216
+ cancelTimer = null;
217
+ };
228
218
  }
229
219
 
230
220
  if (abortable) {
231
221
  return {
232
222
  abort(reason?: any): void {
223
+ cancelTimer?.();
233
224
  controller.abort(reason);
234
225
  },
235
226
  get aborted(): boolean {