@happy-ts/fetch-t 1.0.16 → 1.0.17
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.map +1 -1
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +1 -8
- package/docs/functions/fetchT.md +12 -50
- package/docs/interfaces/FetchInit.md +3 -3
- package/docs/interfaces/FetchTask.md +3 -3
- package/docs/type-aliases/FetchResponse.md +1 -1
- package/docs/variables/ABORT_ERROR.md +1 -1
- package/docs/variables/TIMEOUT_ERROR.md +1 -1
- package/package.json +10 -9
- package/src/fetch/fetch.ts +1 -9
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 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,MAA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escript-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` 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;;ACmEb,SAAA,MAAA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;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.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 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,MAAM,oBAAoB,OAAW,IAAA,IAAA,CAAA;AACrC,EAAI,IAAA,WAAA,CAAA;AAEJ,EAAA,IAAI,iBAAmB,EAAA;AACnB,IAAU,SAAA,CAAA,OAAO,YAAY,QAAY,IAAA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* 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` 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;;ACmEb,SAAA,MAAA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J;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/types.d.ts
CHANGED
|
@@ -150,13 +150,6 @@ declare function fetchT<T>(url: string | URL, init: FetchInit & {
|
|
|
150
150
|
declare function fetchT(url: string | URL, init: FetchInit & {
|
|
151
151
|
abortable: true;
|
|
152
152
|
}): FetchTask<Response>;
|
|
153
|
-
/**
|
|
154
|
-
* Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
|
|
155
|
-
*
|
|
156
|
-
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
157
|
-
* @returns A `FetchResponse` representing the operation with a generic `Response`.
|
|
158
|
-
*/
|
|
159
|
-
declare function fetchT(url: string | URL): FetchResponse<Response>;
|
|
160
153
|
/**
|
|
161
154
|
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
162
155
|
*
|
|
@@ -165,7 +158,7 @@ declare function fetchT(url: string | URL): FetchResponse<Response>;
|
|
|
165
158
|
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
166
159
|
* @returns A `FetchResponse` representing the operation with a `Response` object.
|
|
167
160
|
*/
|
|
168
|
-
declare function fetchT(url: string | URL, init
|
|
161
|
+
declare function fetchT(url: string | URL, init?: FetchInit): FetchResponse<Response>;
|
|
169
162
|
|
|
170
163
|
export { ABORT_ERROR, type FetchInit, type FetchResponse, type FetchTask, TIMEOUT_ERROR, fetchT };
|
|
171
164
|
//# sourceMappingURL=types.d.ts.map
|
package/docs/functions/fetchT.md
CHANGED
|
@@ -57,7 +57,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
57
57
|
|
|
58
58
|
### Defined in
|
|
59
59
|
|
|
60
|
-
[fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/
|
|
60
|
+
[fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L14)
|
|
61
61
|
|
|
62
62
|
## fetchT(url, init)
|
|
63
63
|
|
|
@@ -96,7 +96,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
96
96
|
|
|
97
97
|
### Defined in
|
|
98
98
|
|
|
99
|
-
[fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/
|
|
99
|
+
[fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L26)
|
|
100
100
|
|
|
101
101
|
## fetchT(url, init)
|
|
102
102
|
|
|
@@ -135,7 +135,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
135
135
|
|
|
136
136
|
### Defined in
|
|
137
137
|
|
|
138
|
-
[fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/
|
|
138
|
+
[fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L38)
|
|
139
139
|
|
|
140
140
|
## fetchT(url, init)
|
|
141
141
|
|
|
@@ -180,7 +180,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
180
180
|
|
|
181
181
|
### Defined in
|
|
182
182
|
|
|
183
|
-
[fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/
|
|
183
|
+
[fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L51)
|
|
184
184
|
|
|
185
185
|
## fetchT(url, init)
|
|
186
186
|
|
|
@@ -219,7 +219,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
219
219
|
|
|
220
220
|
### Defined in
|
|
221
221
|
|
|
222
|
-
[fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/
|
|
222
|
+
[fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L63)
|
|
223
223
|
|
|
224
224
|
## fetchT(url, init)
|
|
225
225
|
|
|
@@ -258,7 +258,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
258
258
|
|
|
259
259
|
### Defined in
|
|
260
260
|
|
|
261
|
-
[fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/
|
|
261
|
+
[fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L74)
|
|
262
262
|
|
|
263
263
|
## fetchT(url, init)
|
|
264
264
|
|
|
@@ -297,7 +297,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
297
297
|
|
|
298
298
|
### Defined in
|
|
299
299
|
|
|
300
|
-
[fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/
|
|
300
|
+
[fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L85)
|
|
301
301
|
|
|
302
302
|
## fetchT(url, init)
|
|
303
303
|
|
|
@@ -342,7 +342,7 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
342
342
|
|
|
343
343
|
### Defined in
|
|
344
344
|
|
|
345
|
-
[fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/
|
|
345
|
+
[fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L97)
|
|
346
346
|
|
|
347
347
|
## fetchT(url, init)
|
|
348
348
|
|
|
@@ -381,50 +381,12 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
381
381
|
|
|
382
382
|
### Defined in
|
|
383
383
|
|
|
384
|
-
[fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/
|
|
385
|
-
|
|
386
|
-
## fetchT(url)
|
|
387
|
-
|
|
388
|
-
```ts
|
|
389
|
-
function fetchT(url): FetchResponse<Response>
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
|
|
393
|
-
|
|
394
|
-
### Parameters
|
|
395
|
-
|
|
396
|
-
| Parameter | Type | Description |
|
|
397
|
-
| ------ | ------ | ------ |
|
|
398
|
-
| `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
|
|
399
|
-
|
|
400
|
-
### Returns
|
|
401
|
-
|
|
402
|
-
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
|
|
403
|
-
|
|
404
|
-
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
405
|
-
|
|
406
|
-
A `FetchResponse` representing the operation with a generic `Response`.
|
|
407
|
-
|
|
408
|
-
### Type Param
|
|
409
|
-
|
|
410
|
-
The expected type of the response data when not using a specific `responseType`.
|
|
411
|
-
|
|
412
|
-
### Param
|
|
413
|
-
|
|
414
|
-
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
415
|
-
|
|
416
|
-
### Param
|
|
417
|
-
|
|
418
|
-
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
419
|
-
|
|
420
|
-
### Defined in
|
|
421
|
-
|
|
422
|
-
[fetch.ts:118](https://github.com/JiangJie/fetch-t/blob/83aeaf257a9785c71aa40d9d0ccd18ebf56e3b59/src/fetch/fetch.ts#L118)
|
|
384
|
+
[fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L108)
|
|
423
385
|
|
|
424
386
|
## fetchT(url, init)
|
|
425
387
|
|
|
426
388
|
```ts
|
|
427
|
-
function fetchT(url, init): FetchResponse<Response>
|
|
389
|
+
function fetchT(url, init?): FetchResponse<Response>
|
|
428
390
|
```
|
|
429
391
|
|
|
430
392
|
Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
@@ -434,7 +396,7 @@ Fetches a resource from the network and returns a `FetchResponse` or `FetchTask`
|
|
|
434
396
|
| Parameter | Type | Description |
|
|
435
397
|
| ------ | ------ | ------ |
|
|
436
398
|
| `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
|
|
437
|
-
| `init
|
|
399
|
+
| `init`? | [`FetchInit`](../interfaces/FetchInit.md) | Additional options for the fetch operation, including custom `FetchInit` properties. |
|
|
438
400
|
|
|
439
401
|
### Returns
|
|
440
402
|
|
|
@@ -458,4 +420,4 @@ Additional options for the fetch operation, including custom `FetchInit` propert
|
|
|
458
420
|
|
|
459
421
|
### Defined in
|
|
460
422
|
|
|
461
|
-
[fetch.ts:
|
|
423
|
+
[fetch.ts:120](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/src/fetch/fetch.ts#L120)
|
|
@@ -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/
|
|
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/
|
|
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/
|
|
19
|
+
| `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | [defines.ts:42](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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 | Modifier | Type | Description | Defined in |
|
|
20
20
|
| ------ | ------ | ------ | ------ | ------ |
|
|
21
|
-
| `aborted` | `readonly` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/
|
|
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/
|
|
21
|
+
| `aborted` | `readonly` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/
|
|
46
|
+
[defines.ts:22](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/
|
|
23
|
+
[defines.ts:9](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/
|
|
17
|
+
[defines.ts:58](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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/
|
|
17
|
+
[defines.ts:63](https://github.com/JiangJie/fetch-t/blob/d90c4d8dc6d5e11996ad3e2f1da5b1c12e4b5058/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.
|
|
6
|
+
"version": "1.0.17",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"source": "src/mod.ts",
|
|
9
9
|
"main": "dist/main.cjs",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"sideEffects": false,
|
|
22
22
|
"scripts": {
|
|
23
23
|
"check": "pnpm exec tsc --noEmit",
|
|
24
|
-
"lint": "pnpm exec eslint
|
|
24
|
+
"lint": "pnpm exec eslint .",
|
|
25
25
|
"prebuild": "pnpm dlx rimraf dist && pnpm run check && pnpm run lint",
|
|
26
26
|
"build": "pnpm exec rollup --config rollup.config.mjs",
|
|
27
27
|
"pretest": "pnpm dlx rimraf coverage",
|
|
@@ -43,19 +43,20 @@
|
|
|
43
43
|
"responseType"
|
|
44
44
|
],
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@
|
|
47
|
-
"@
|
|
48
|
-
"eslint": "^8.
|
|
49
|
-
"rollup": "^4.19.
|
|
46
|
+
"@eslint/js": "^9.8.0",
|
|
47
|
+
"@types/eslint__js": "^8.42.3",
|
|
48
|
+
"eslint": "^9.8.0",
|
|
49
|
+
"rollup": "^4.19.2",
|
|
50
50
|
"rollup-plugin-dts": "^6.1.1",
|
|
51
51
|
"rollup-plugin-esbuild": "^6.1.1",
|
|
52
52
|
"typedoc": "^0.26.5",
|
|
53
53
|
"typedoc-plugin-markdown": "^4.2.3",
|
|
54
|
-
"typescript": "^5.5.4"
|
|
54
|
+
"typescript": "^5.5.4",
|
|
55
|
+
"typescript-eslint": "^8.0.0"
|
|
55
56
|
},
|
|
56
57
|
"dependencies": {
|
|
57
|
-
"happy-rusty": "^1.
|
|
58
|
+
"happy-rusty": "^1.2.0",
|
|
58
59
|
"tiny-invariant": "^1.3.3"
|
|
59
60
|
},
|
|
60
|
-
"packageManager": "pnpm@9.
|
|
61
|
+
"packageManager": "pnpm@9.6.0"
|
|
61
62
|
}
|
package/src/fetch/fetch.ts
CHANGED
|
@@ -109,14 +109,6 @@ export function fetchT(url: string | URL, init: FetchInit & {
|
|
|
109
109
|
abortable: true;
|
|
110
110
|
}): FetchTask<Response>;
|
|
111
111
|
|
|
112
|
-
/**
|
|
113
|
-
* Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
|
|
114
|
-
*
|
|
115
|
-
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
116
|
-
* @returns A `FetchResponse` representing the operation with a generic `Response`.
|
|
117
|
-
*/
|
|
118
|
-
export function fetchT(url: string | URL): FetchResponse<Response>;
|
|
119
|
-
|
|
120
112
|
/**
|
|
121
113
|
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
122
114
|
*
|
|
@@ -125,7 +117,7 @@ export function fetchT(url: string | URL): FetchResponse<Response>;
|
|
|
125
117
|
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
126
118
|
* @returns A `FetchResponse` representing the operation with a `Response` object.
|
|
127
119
|
*/
|
|
128
|
-
export function fetchT(url: string | URL, init
|
|
120
|
+
export function fetchT(url: string | URL, init?: FetchInit): FetchResponse<Response>;
|
|
129
121
|
|
|
130
122
|
/**
|
|
131
123
|
* Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.
|