@happy-ts/fetch-t 1.0.11 → 1.0.13
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/README.cn.md +3 -0
- package/README.md +3 -0
- package/dist/main.cjs +26 -2
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +25 -3
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +23 -1
- package/docs/README.md +7 -0
- package/docs/functions/fetchT.md +205 -24
- package/docs/interfaces/FetchInit.md +3 -2
- package/docs/interfaces/FetchTask.md +3 -3
- package/docs/type-aliases/FetchResponse.md +1 -1
- package/docs/variables/ABORT_ERROR.md +17 -0
- package/docs/variables/TIMEOUT_ERROR.md +17 -0
- package/package.json +7 -7
- package/src/fetch/defines.ts +16 -1
- package/src/fetch/fetch.ts +36 -4
- package/src/mod.ts +1 -1
package/README.cn.md
CHANGED
|
@@ -14,6 +14,8 @@ fetchT 的返回值包含一个可以 abort 的方法。
|
|
|
14
14
|
|
|
15
15
|
fetchT 的返回数据是一个明确的类型,可以是 `string` `ArrayBuffer` `Blob` `<T>(泛型)`。
|
|
16
16
|
|
|
17
|
+
支持超时自动取消请求。
|
|
18
|
+
|
|
17
19
|
## 安装
|
|
18
20
|
|
|
19
21
|
pnpm
|
|
@@ -63,6 +65,7 @@ import { fetchT } from '@happy-ts/fetch-t';
|
|
|
63
65
|
const fetchTask = fetchT('https://example.com', {
|
|
64
66
|
abortable: true,
|
|
65
67
|
responseType: 'json',
|
|
68
|
+
timeout: 3000,
|
|
66
69
|
});
|
|
67
70
|
|
|
68
71
|
somethingHappenAsync(() => {
|
package/README.md
CHANGED
|
@@ -19,6 +19,8 @@ The return value of fetchT includes an `abort` method.
|
|
|
19
19
|
|
|
20
20
|
The return data of fetchT is of a specific type, which can be either `string`, `ArrayBuffer`, `Blob`, or `<T>(generic)`.
|
|
21
21
|
|
|
22
|
+
Support `timeout`.
|
|
23
|
+
|
|
22
24
|
## Installation
|
|
23
25
|
|
|
24
26
|
via pnpm
|
|
@@ -74,6 +76,7 @@ import { fetchT } from '@happy-ts/fetch-t';
|
|
|
74
76
|
const fetchTask = fetchT('https://example.com', {
|
|
75
77
|
abortable: true,
|
|
76
78
|
responseType: 'json',
|
|
79
|
+
timeout: 3000,
|
|
77
80
|
});
|
|
78
81
|
|
|
79
82
|
somethingHappenAsync(() => {
|
package/dist/main.cjs
CHANGED
|
@@ -3,13 +3,26 @@
|
|
|
3
3
|
var happyRusty = require('happy-rusty');
|
|
4
4
|
var invariant = require('tiny-invariant');
|
|
5
5
|
|
|
6
|
+
const ABORT_ERROR = "AbortError";
|
|
7
|
+
const TIMEOUT_ERROR = "TimeoutError";
|
|
8
|
+
|
|
6
9
|
function fetchT(url, init) {
|
|
7
10
|
if (typeof url !== "string") {
|
|
8
11
|
invariant(url instanceof URL, () => `Url must be a string or URL object but received ${url}.`);
|
|
9
12
|
}
|
|
10
|
-
const {
|
|
13
|
+
const {
|
|
14
|
+
// default not abort able
|
|
15
|
+
abortable = false,
|
|
16
|
+
responseType,
|
|
17
|
+
timeout,
|
|
18
|
+
...rest
|
|
19
|
+
} = init ?? {};
|
|
20
|
+
const shouldWaitTimeout = timeout != null;
|
|
21
|
+
if (shouldWaitTimeout) {
|
|
22
|
+
invariant(typeof timeout === "number" && timeout > 0, () => `Timeout must be a number greater than 0 but received ${timeout}.`);
|
|
23
|
+
}
|
|
11
24
|
let controller;
|
|
12
|
-
if (abortable) {
|
|
25
|
+
if (abortable || shouldWaitTimeout) {
|
|
13
26
|
controller = new AbortController();
|
|
14
27
|
rest.signal = controller.signal;
|
|
15
28
|
}
|
|
@@ -42,6 +55,15 @@ function fetchT(url, init) {
|
|
|
42
55
|
}).catch((err) => {
|
|
43
56
|
return happyRusty.Err(err);
|
|
44
57
|
});
|
|
58
|
+
if (shouldWaitTimeout) {
|
|
59
|
+
setTimeout(() => {
|
|
60
|
+
if (!controller.signal.aborted) {
|
|
61
|
+
const error = new Error();
|
|
62
|
+
error.name = TIMEOUT_ERROR;
|
|
63
|
+
controller.abort(error);
|
|
64
|
+
}
|
|
65
|
+
}, timeout);
|
|
66
|
+
}
|
|
45
67
|
if (abortable) {
|
|
46
68
|
return {
|
|
47
69
|
abort(reason) {
|
|
@@ -57,5 +79,7 @@ function fetchT(url, init) {
|
|
|
57
79
|
}
|
|
58
80
|
}
|
|
59
81
|
|
|
82
|
+
exports.ABORT_ERROR = ABORT_ERROR;
|
|
83
|
+
exports.TIMEOUT_ERROR = TIMEOUT_ERROR;
|
|
60
84
|
exports.fetchT = fetchT;
|
|
61
85
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.cjs","sources":["../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport type { FetchInit, FetchResponse, 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` 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 // default not abort able\n const { abortable = false, responseType, ...rest } = init ?? {};\n\n let controller: AbortController;\n\n if (abortable) {\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 (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":";;;;;AAqJgB,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;AAGA,EAAM,MAAA,EAAE,YAAY,KAAO,EAAA,YAAA,EAAc,GAAG,IAAK,EAAA,GAAI,QAAQ,EAAC,CAAA;AAE9D,EAAI,IAAA,UAAA,CAAA;AAEJ,EAAA,IAAI,SAAW,EAAA;AACX,IAAA,UAAA,GAAa,IAAI,eAAgB,EAAA,CAAA;AACjC,IAAA,IAAA,CAAK,SAAS,UAAW,CAAA,MAAA,CAAA;AAAA,GAC7B;AAEA,EAAA,MAAM,WAA6B,KAAM,CAAA,GAAA,EAAK,IAAI,CAAE,CAAA,IAAA,CAAK,OAAO,GAA0B,KAAA;AACtF,IAAI,IAAA,CAAC,IAAI,EAAI,EAAA;AACT,MAAM,MAAA,GAAA,CAAI,MAAM,MAAO,EAAA,CAAA;AACvB,MAAA,OAAOA,eAAI,IAAI,KAAA,CAAM,iBAAkB,GAAI,CAAA,MAAO,EAAE,CAAC,CAAA,CAAA;AAAA,KACzD;AAEA,IAAA,QAAQ,YAAc;AAAA,MAClB,KAAK,aAAe,EAAA;AAChB,QAAA,OAAOC,aAAG,CAAA,MAAM,GAAI,CAAA,WAAA,EAAkB,CAAA,CAAA;AAAA,OAC1C;AAAA,MACA,KAAK,MAAQ,EAAA;AACT,QAAA,OAAOA,aAAG,CAAA,MAAM,GAAI,CAAA,IAAA,EAAW,CAAA,CAAA;AAAA,OACnC;AAAA,MACA,KAAK,MAAQ,EAAA;AACT,QAAI,IAAA;AACA,UAAA,OAAOA,aAAG,CAAA,MAAM,GAAI,CAAA,IAAA,EAAW,CAAA,CAAA;AAAA,SAC3B,CAAA,MAAA;AACJ,UAAA,OAAOD,cAAI,CAAA,IAAI,KAAM,CAAA,qDAAqD,CAAC,CAAA,CAAA;AAAA,SAC/E;AAAA,OACJ;AAAA,MACA,KAAK,MAAQ,EAAA;AACT,QAAA,OAAOC,aAAG,CAAA,MAAM,GAAI,CAAA,IAAA,EAAW,CAAA,CAAA;AAAA,OACnC;AAAA,MACA,SAAS;AAEL,QAAA,OAAOA,cAAG,GAAQ,CAAA,CAAA;AAAA,OACtB;AAAA,KACJ;AAAA,GACH,CAAA,CAAE,KAAM,CAAA,CAAC,GAAQ,KAAA;AACd,IAAA,OAAOD,eAAI,GAAG,CAAA,CAAA;AAAA,GACjB,CAAA,CAAA;AAED,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 * @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":"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"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
import { Err, Ok } from 'happy-rusty';
|
|
2
2
|
import invariant from 'tiny-invariant';
|
|
3
3
|
|
|
4
|
+
const ABORT_ERROR = "AbortError";
|
|
5
|
+
const TIMEOUT_ERROR = "TimeoutError";
|
|
6
|
+
|
|
4
7
|
function fetchT(url, init) {
|
|
5
8
|
if (typeof url !== "string") {
|
|
6
9
|
invariant(url instanceof URL, () => `Url must be a string or URL object but received ${url}.`);
|
|
7
10
|
}
|
|
8
|
-
const {
|
|
11
|
+
const {
|
|
12
|
+
// default not abort able
|
|
13
|
+
abortable = false,
|
|
14
|
+
responseType,
|
|
15
|
+
timeout,
|
|
16
|
+
...rest
|
|
17
|
+
} = init ?? {};
|
|
18
|
+
const shouldWaitTimeout = timeout != null;
|
|
19
|
+
if (shouldWaitTimeout) {
|
|
20
|
+
invariant(typeof timeout === "number" && timeout > 0, () => `Timeout must be a number greater than 0 but received ${timeout}.`);
|
|
21
|
+
}
|
|
9
22
|
let controller;
|
|
10
|
-
if (abortable) {
|
|
23
|
+
if (abortable || shouldWaitTimeout) {
|
|
11
24
|
controller = new AbortController();
|
|
12
25
|
rest.signal = controller.signal;
|
|
13
26
|
}
|
|
@@ -40,6 +53,15 @@ function fetchT(url, init) {
|
|
|
40
53
|
}).catch((err) => {
|
|
41
54
|
return Err(err);
|
|
42
55
|
});
|
|
56
|
+
if (shouldWaitTimeout) {
|
|
57
|
+
setTimeout(() => {
|
|
58
|
+
if (!controller.signal.aborted) {
|
|
59
|
+
const error = new Error();
|
|
60
|
+
error.name = TIMEOUT_ERROR;
|
|
61
|
+
controller.abort(error);
|
|
62
|
+
}
|
|
63
|
+
}, timeout);
|
|
64
|
+
}
|
|
43
65
|
if (abortable) {
|
|
44
66
|
return {
|
|
45
67
|
abort(reason) {
|
|
@@ -55,5 +77,5 @@ function fetchT(url, init) {
|
|
|
55
77
|
}
|
|
56
78
|
}
|
|
57
79
|
|
|
58
|
-
export { fetchT };
|
|
80
|
+
export { ABORT_ERROR, TIMEOUT_ERROR, fetchT };
|
|
59
81
|
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.mjs","sources":["../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport type { FetchInit, FetchResponse, 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` 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 // default not abort able\n const { abortable = false, responseType, ...rest } = init ?? {};\n\n let controller: AbortController;\n\n if (abortable) {\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 (abortable) {\n return {\n abort(reason?: any): void {\n controller.abort(reason);\n },\n get aborted(): boolean {\n return controller.signal.aborted;\n },\n response,\n };\n } else {\n return response;\n }\n}"],"names":[],"mappings":";;;AAqJgB,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;AAGA,EAAM,MAAA,EAAE,YAAY,KAAO,EAAA,YAAA,EAAc,GAAG,IAAK,EAAA,GAAI,QAAQ,EAAC,CAAA;AAE9D,EAAI,IAAA,UAAA,CAAA;AAEJ,EAAA,IAAI,SAAW,EAAA;AACX,IAAA,UAAA,GAAa,IAAI,eAAgB,EAAA,CAAA;AACjC,IAAA,IAAA,CAAK,SAAS,UAAW,CAAA,MAAA,CAAA;AAAA,GAC7B;AAEA,EAAA,MAAM,WAA6B,KAAM,CAAA,GAAA,EAAK,IAAI,CAAE,CAAA,IAAA,CAAK,OAAO,GAA0B,KAAA;AACtF,IAAI,IAAA,CAAC,IAAI,EAAI,EAAA;AACT,MAAM,MAAA,GAAA,CAAI,MAAM,MAAO,EAAA,CAAA;AACvB,MAAA,OAAO,IAAI,IAAI,KAAA,CAAM,iBAAkB,GAAI,CAAA,MAAO,EAAE,CAAC,CAAA,CAAA;AAAA,KACzD;AAEA,IAAA,QAAQ,YAAc;AAAA,MAClB,KAAK,aAAe,EAAA;AAChB,QAAA,OAAO,EAAG,CAAA,MAAM,GAAI,CAAA,WAAA,EAAkB,CAAA,CAAA;AAAA,OAC1C;AAAA,MACA,KAAK,MAAQ,EAAA;AACT,QAAA,OAAO,EAAG,CAAA,MAAM,GAAI,CAAA,IAAA,EAAW,CAAA,CAAA;AAAA,OACnC;AAAA,MACA,KAAK,MAAQ,EAAA;AACT,QAAI,IAAA;AACA,UAAA,OAAO,EAAG,CAAA,MAAM,GAAI,CAAA,IAAA,EAAW,CAAA,CAAA;AAAA,SAC3B,CAAA,MAAA;AACJ,UAAA,OAAO,GAAI,CAAA,IAAI,KAAM,CAAA,qDAAqD,CAAC,CAAA,CAAA;AAAA,SAC/E;AAAA,OACJ;AAAA,MACA,KAAK,MAAQ,EAAA;AACT,QAAA,OAAO,EAAG,CAAA,MAAM,GAAI,CAAA,IAAA,EAAW,CAAA,CAAA;AAAA,OACnC;AAAA,MACA,SAAS;AAEL,QAAA,OAAO,GAAG,GAAQ,CAAA,CAAA;AAAA,OACtB;AAAA,KACJ;AAAA,GACH,CAAA,CAAE,KAAM,CAAA,CAAC,GAAQ,KAAA;AACd,IAAA,OAAO,IAAI,GAAG,CAAA,CAAA;AAAA,GACjB,CAAA,CAAA;AAED,EAAA,IAAI,SAAW,EAAA;AACX,IAAO,OAAA;AAAA,MACH,MAAM,MAAoB,EAAA;AACtB,QAAA,UAAA,CAAW,MAAM,MAAM,CAAA,CAAA;AAAA,OAC3B;AAAA,MACA,IAAI,OAAmB,GAAA;AACnB,QAAA,OAAO,WAAW,MAAO,CAAA,OAAA,CAAA;AAAA,OAC7B;AAAA,MACA,QAAA;AAAA,KACJ,CAAA;AAAA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;"}
|
|
1
|
+
{"version":3,"file":"main.mjs","sources":["../src/fetch/defines.ts","../src/fetch/fetch.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { AsyncResult } from 'happy-rusty';\n\n/**\n * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport type FetchResponse<T> = AsyncResult<T, any>;\n\n/**\n * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport interface FetchTask<T> {\n /**\n * Aborts the fetch task, optionally with a reason for the abortion.\n *\n * @param reason - An optional parameter to indicate why the task was aborted.\n */\n abort(reason?: any): void;\n\n /**\n * Indicates whether the fetch task has been aborted.\n */\n 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":[],"mappings":"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"}
|
package/dist/types.d.ts
CHANGED
|
@@ -39,7 +39,19 @@ interface FetchInit extends RequestInit {
|
|
|
39
39
|
* Specifies the expected response type of the fetch request.
|
|
40
40
|
*/
|
|
41
41
|
responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
|
|
42
|
+
/**
|
|
43
|
+
* Specifies the maximum time in milliseconds to wait for the fetch request to complete.
|
|
44
|
+
*/
|
|
45
|
+
timeout?: number;
|
|
42
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Name of abort error;
|
|
49
|
+
*/
|
|
50
|
+
declare const ABORT_ERROR = "AbortError";
|
|
51
|
+
/**
|
|
52
|
+
* Name of timeout error;
|
|
53
|
+
*/
|
|
54
|
+
declare const TIMEOUT_ERROR = "TimeoutError";
|
|
43
55
|
|
|
44
56
|
/**
|
|
45
57
|
* Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
|
|
@@ -148,6 +160,16 @@ declare function fetchT(url: string | URL, init: FetchInit & {
|
|
|
148
160
|
declare function fetchT(url: string | URL, init: FetchInit & {
|
|
149
161
|
abortable: false;
|
|
150
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>;
|
|
151
173
|
/**
|
|
152
174
|
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
153
175
|
*
|
|
@@ -166,5 +188,5 @@ declare function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | F
|
|
|
166
188
|
*/
|
|
167
189
|
declare function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;
|
|
168
190
|
|
|
169
|
-
export { type FetchInit, type FetchResponse, type FetchTask, fetchT };
|
|
191
|
+
export { ABORT_ERROR, type FetchInit, type FetchResponse, type FetchTask, TIMEOUT_ERROR, fetchT };
|
|
170
192
|
//# sourceMappingURL=types.d.ts.map
|
package/docs/README.md
CHANGED
|
@@ -17,6 +17,13 @@
|
|
|
17
17
|
| ------ | ------ |
|
|
18
18
|
| [FetchResponse](type-aliases/FetchResponse.md) | Represents the response of a fetch operation, encapsulating the result data or any error that occurred. |
|
|
19
19
|
|
|
20
|
+
## Variables
|
|
21
|
+
|
|
22
|
+
| Variable | Description |
|
|
23
|
+
| ------ | ------ |
|
|
24
|
+
| [ABORT\_ERROR](variables/ABORT_ERROR.md) | Name of abort error; |
|
|
25
|
+
| [TIMEOUT\_ERROR](variables/TIMEOUT_ERROR.md) | Name of timeout error; |
|
|
26
|
+
|
|
20
27
|
## Functions
|
|
21
28
|
|
|
22
29
|
| Function | Description |
|
package/docs/functions/fetchT.md
CHANGED
|
@@ -39,11 +39,23 @@ Fetches a resource from the network as a text string and returns a `FetchTask` r
|
|
|
39
39
|
|
|
40
40
|
[`FetchTask`](../interfaces/FetchTask.md)\<`string`\>
|
|
41
41
|
|
|
42
|
-
A `FetchTask`
|
|
42
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
43
|
+
|
|
44
|
+
### Type Param
|
|
45
|
+
|
|
46
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
47
|
+
|
|
48
|
+
### Param
|
|
49
|
+
|
|
50
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
51
|
+
|
|
52
|
+
### Param
|
|
53
|
+
|
|
54
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
43
55
|
|
|
44
56
|
### Defined in
|
|
45
57
|
|
|
46
|
-
[fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/
|
|
58
|
+
[fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L14)
|
|
47
59
|
|
|
48
60
|
## fetchT(url, init)
|
|
49
61
|
|
|
@@ -64,11 +76,23 @@ Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask`
|
|
|
64
76
|
|
|
65
77
|
[`FetchTask`](../interfaces/FetchTask.md)\<`ArrayBuffer`\>
|
|
66
78
|
|
|
67
|
-
A `FetchTask`
|
|
79
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
80
|
+
|
|
81
|
+
### Type Param
|
|
82
|
+
|
|
83
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
84
|
+
|
|
85
|
+
### Param
|
|
86
|
+
|
|
87
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
88
|
+
|
|
89
|
+
### Param
|
|
90
|
+
|
|
91
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
68
92
|
|
|
69
93
|
### Defined in
|
|
70
94
|
|
|
71
|
-
[fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/
|
|
95
|
+
[fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L26)
|
|
72
96
|
|
|
73
97
|
## fetchT(url, init)
|
|
74
98
|
|
|
@@ -89,11 +113,23 @@ Fetches a resource from the network as a Blob and returns a `FetchTask` represen
|
|
|
89
113
|
|
|
90
114
|
[`FetchTask`](../interfaces/FetchTask.md)\<`Blob`\>
|
|
91
115
|
|
|
92
|
-
A `FetchTask`
|
|
116
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
117
|
+
|
|
118
|
+
### Type Param
|
|
119
|
+
|
|
120
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
121
|
+
|
|
122
|
+
### Param
|
|
123
|
+
|
|
124
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
125
|
+
|
|
126
|
+
### Param
|
|
127
|
+
|
|
128
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
93
129
|
|
|
94
130
|
### Defined in
|
|
95
131
|
|
|
96
|
-
[fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/
|
|
132
|
+
[fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L38)
|
|
97
133
|
|
|
98
134
|
## fetchT(url, init)
|
|
99
135
|
|
|
@@ -120,11 +156,23 @@ Fetches a resource from the network and parses it as JSON, returning a `FetchTas
|
|
|
120
156
|
|
|
121
157
|
[`FetchTask`](../interfaces/FetchTask.md)\<`T`\>
|
|
122
158
|
|
|
123
|
-
A `FetchTask`
|
|
159
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
160
|
+
|
|
161
|
+
### Type Param
|
|
162
|
+
|
|
163
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
164
|
+
|
|
165
|
+
### Param
|
|
166
|
+
|
|
167
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
168
|
+
|
|
169
|
+
### Param
|
|
170
|
+
|
|
171
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
124
172
|
|
|
125
173
|
### Defined in
|
|
126
174
|
|
|
127
|
-
[fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/
|
|
175
|
+
[fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L51)
|
|
128
176
|
|
|
129
177
|
## fetchT(url, init)
|
|
130
178
|
|
|
@@ -145,11 +193,23 @@ Fetches a resource from the network as a text string and returns a `FetchRespons
|
|
|
145
193
|
|
|
146
194
|
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`string`\>
|
|
147
195
|
|
|
148
|
-
A `FetchResponse`
|
|
196
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
197
|
+
|
|
198
|
+
### Type Param
|
|
199
|
+
|
|
200
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
201
|
+
|
|
202
|
+
### Param
|
|
203
|
+
|
|
204
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
205
|
+
|
|
206
|
+
### Param
|
|
207
|
+
|
|
208
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
149
209
|
|
|
150
210
|
### Defined in
|
|
151
211
|
|
|
152
|
-
[fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/
|
|
212
|
+
[fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L63)
|
|
153
213
|
|
|
154
214
|
## fetchT(url, init)
|
|
155
215
|
|
|
@@ -170,11 +230,23 @@ Fetches a resource from the network as an ArrayBuffer and returns a `FetchRespon
|
|
|
170
230
|
|
|
171
231
|
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`ArrayBuffer`\>
|
|
172
232
|
|
|
173
|
-
A `FetchResponse`
|
|
233
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
234
|
+
|
|
235
|
+
### Type Param
|
|
236
|
+
|
|
237
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
238
|
+
|
|
239
|
+
### Param
|
|
240
|
+
|
|
241
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
242
|
+
|
|
243
|
+
### Param
|
|
244
|
+
|
|
245
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
174
246
|
|
|
175
247
|
### Defined in
|
|
176
248
|
|
|
177
|
-
[fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/
|
|
249
|
+
[fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L74)
|
|
178
250
|
|
|
179
251
|
## fetchT(url, init)
|
|
180
252
|
|
|
@@ -195,11 +267,23 @@ Fetches a resource from the network as a Blob and returns a `FetchResponse` repr
|
|
|
195
267
|
|
|
196
268
|
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`Blob`\>
|
|
197
269
|
|
|
198
|
-
A `FetchResponse`
|
|
270
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
271
|
+
|
|
272
|
+
### Type Param
|
|
273
|
+
|
|
274
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
275
|
+
|
|
276
|
+
### Param
|
|
277
|
+
|
|
278
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
279
|
+
|
|
280
|
+
### Param
|
|
281
|
+
|
|
282
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
199
283
|
|
|
200
284
|
### Defined in
|
|
201
285
|
|
|
202
|
-
[fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/
|
|
286
|
+
[fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L85)
|
|
203
287
|
|
|
204
288
|
## fetchT(url, init)
|
|
205
289
|
|
|
@@ -226,11 +310,23 @@ Fetches a resource from the network and parses it as JSON, returning a `FetchRes
|
|
|
226
310
|
|
|
227
311
|
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
|
|
228
312
|
|
|
229
|
-
A `
|
|
313
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
314
|
+
|
|
315
|
+
### Type Param
|
|
316
|
+
|
|
317
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
318
|
+
|
|
319
|
+
### Param
|
|
320
|
+
|
|
321
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
322
|
+
|
|
323
|
+
### Param
|
|
324
|
+
|
|
325
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
230
326
|
|
|
231
327
|
### Defined in
|
|
232
328
|
|
|
233
|
-
[fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/
|
|
329
|
+
[fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L97)
|
|
234
330
|
|
|
235
331
|
## fetchT(url, init)
|
|
236
332
|
|
|
@@ -251,11 +347,23 @@ Fetches a resource from the network and returns a `FetchTask` representing the o
|
|
|
251
347
|
|
|
252
348
|
[`FetchTask`](../interfaces/FetchTask.md)\<`Response`\>
|
|
253
349
|
|
|
254
|
-
A `FetchTask`
|
|
350
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
351
|
+
|
|
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.
|
|
255
363
|
|
|
256
364
|
### Defined in
|
|
257
365
|
|
|
258
|
-
[fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/
|
|
366
|
+
[fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L108)
|
|
259
367
|
|
|
260
368
|
## fetchT(url, init)
|
|
261
369
|
|
|
@@ -276,11 +384,60 @@ Fetches a resource from the network and returns a `FetchResponse` representing t
|
|
|
276
384
|
|
|
277
385
|
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
|
|
278
386
|
|
|
279
|
-
A `FetchResponse`
|
|
387
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
388
|
+
|
|
389
|
+
### Type Param
|
|
390
|
+
|
|
391
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
392
|
+
|
|
393
|
+
### Param
|
|
394
|
+
|
|
395
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
396
|
+
|
|
397
|
+
### Param
|
|
398
|
+
|
|
399
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
280
400
|
|
|
281
401
|
### Defined in
|
|
282
402
|
|
|
283
|
-
[fetch.ts:119](https://github.com/JiangJie/fetch-t/blob/
|
|
403
|
+
[fetch.ts:119](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L119)
|
|
404
|
+
|
|
405
|
+
## fetchT(url, init)
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
function fetchT(url, init): FetchResponse<Response>
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
|
|
412
|
+
|
|
413
|
+
### Parameters
|
|
414
|
+
|
|
415
|
+
| Parameter | Type | Description |
|
|
416
|
+
| ------ | ------ | ------ |
|
|
417
|
+
| `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
|
+
|
|
420
|
+
### Returns
|
|
421
|
+
|
|
422
|
+
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
|
|
423
|
+
|
|
424
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
425
|
+
|
|
426
|
+
### Type Param
|
|
427
|
+
|
|
428
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
429
|
+
|
|
430
|
+
### Param
|
|
431
|
+
|
|
432
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
433
|
+
|
|
434
|
+
### Param
|
|
435
|
+
|
|
436
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
437
|
+
|
|
438
|
+
### Defined in
|
|
439
|
+
|
|
440
|
+
[fetch.ts:130](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L130)
|
|
284
441
|
|
|
285
442
|
## fetchT(url, init)
|
|
286
443
|
|
|
@@ -307,11 +464,23 @@ Fetches a resource from the network and returns a `FetchResponse` or `FetchTask`
|
|
|
307
464
|
|
|
308
465
|
[`FetchTask`](../interfaces/FetchTask.md)\<`T`\> \| [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
|
|
309
466
|
|
|
310
|
-
A `
|
|
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.
|
|
311
480
|
|
|
312
481
|
### Defined in
|
|
313
482
|
|
|
314
|
-
[fetch.ts:
|
|
483
|
+
[fetch.ts:142](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L142)
|
|
315
484
|
|
|
316
485
|
## fetchT(url, init)
|
|
317
486
|
|
|
@@ -332,8 +501,20 @@ Fetches a resource from the network and returns a `FetchResponse` representing t
|
|
|
332
501
|
|
|
333
502
|
[`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
|
|
334
503
|
|
|
335
|
-
A `FetchResponse`
|
|
504
|
+
A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
505
|
+
|
|
506
|
+
### Type Param
|
|
507
|
+
|
|
508
|
+
The expected type of the response data when not using a specific `responseType`.
|
|
509
|
+
|
|
510
|
+
### Param
|
|
511
|
+
|
|
512
|
+
The resource to fetch. Can be a URL object or a string representing a URL.
|
|
513
|
+
|
|
514
|
+
### Param
|
|
515
|
+
|
|
516
|
+
Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
336
517
|
|
|
337
518
|
### Defined in
|
|
338
519
|
|
|
339
|
-
[fetch.ts:
|
|
520
|
+
[fetch.ts:151](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/fetch.ts#L151)
|
|
@@ -16,5 +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/
|
|
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) |
|
|
@@ -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/
|
|
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/
|
|
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) |
|
|
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/c5e0121b76a0dea415931120c31c2d070cc2469d/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/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L9)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[@happy-ts/fetch-t](../README.md) / ABORT\_ERROR
|
|
6
|
+
|
|
7
|
+
# Variable: ABORT\_ERROR
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const ABORT_ERROR: "AbortError" = 'AbortError';
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Name of abort error;
|
|
14
|
+
|
|
15
|
+
## Defined in
|
|
16
|
+
|
|
17
|
+
[defines.ts:58](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/src/fetch/defines.ts#L58)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[@happy-ts/fetch-t](../README.md) / TIMEOUT\_ERROR
|
|
6
|
+
|
|
7
|
+
# Variable: TIMEOUT\_ERROR
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const TIMEOUT_ERROR: "TimeoutError" = 'TimeoutError';
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Name of timeout error;
|
|
14
|
+
|
|
15
|
+
## Defined in
|
|
16
|
+
|
|
17
|
+
[defines.ts:63](https://github.com/JiangJie/fetch-t/blob/c5e0121b76a0dea415931120c31c2d070cc2469d/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.13",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"source": "src/mod.ts",
|
|
9
9
|
"main": "dist/main.cjs",
|
|
@@ -43,15 +43,15 @@
|
|
|
43
43
|
"responseType"
|
|
44
44
|
],
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
47
|
-
"@typescript-eslint/parser": "^7.
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^7.17.0",
|
|
47
|
+
"@typescript-eslint/parser": "^7.17.0",
|
|
48
48
|
"eslint": "^8.57.0",
|
|
49
|
-
"rollup": "^4.
|
|
49
|
+
"rollup": "^4.19.0",
|
|
50
50
|
"rollup-plugin-dts": "^6.1.1",
|
|
51
51
|
"rollup-plugin-esbuild": "^6.1.1",
|
|
52
|
-
"typedoc": "^0.26.
|
|
53
|
-
"typedoc-plugin-markdown": "^4.2.
|
|
54
|
-
"typescript": "^5.5.
|
|
52
|
+
"typedoc": "^0.26.5",
|
|
53
|
+
"typedoc-plugin-markdown": "^4.2.2",
|
|
54
|
+
"typescript": "^5.5.4"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"happy-rusty": "^1.1.2",
|
package/src/fetch/defines.ts
CHANGED
|
@@ -45,4 +45,19 @@ export interface FetchInit extends RequestInit {
|
|
|
45
45
|
* Specifies the expected response type of the fetch request.
|
|
46
46
|
*/
|
|
47
47
|
responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
|
|
48
|
-
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Specifies the maximum time in milliseconds to wait for the fetch request to complete.
|
|
51
|
+
*/
|
|
52
|
+
timeout?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Name of abort error;
|
|
57
|
+
*/
|
|
58
|
+
export const ABORT_ERROR = 'AbortError';
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Name of timeout error;
|
|
62
|
+
*/
|
|
63
|
+
export const TIMEOUT_ERROR = 'TimeoutError';
|
package/src/fetch/fetch.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { Err, Ok } from 'happy-rusty';
|
|
3
3
|
import invariant from 'tiny-invariant';
|
|
4
|
-
import type
|
|
4
|
+
import { TIMEOUT_ERROR, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
|
|
@@ -120,6 +120,17 @@ export function fetchT(url: string | URL, init: FetchInit & {
|
|
|
120
120
|
abortable: false;
|
|
121
121
|
}): FetchResponse<Response>;
|
|
122
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>;
|
|
133
|
+
|
|
123
134
|
/**
|
|
124
135
|
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
125
136
|
*
|
|
@@ -153,12 +164,23 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
|
|
|
153
164
|
invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);
|
|
154
165
|
}
|
|
155
166
|
|
|
156
|
-
|
|
157
|
-
|
|
167
|
+
const {
|
|
168
|
+
// default not abort able
|
|
169
|
+
abortable = false,
|
|
170
|
+
responseType,
|
|
171
|
+
timeout,
|
|
172
|
+
...rest
|
|
173
|
+
} = init ?? {};
|
|
174
|
+
|
|
175
|
+
const shouldWaitTimeout = timeout != null;
|
|
176
|
+
|
|
177
|
+
if (shouldWaitTimeout) {
|
|
178
|
+
invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);
|
|
179
|
+
}
|
|
158
180
|
|
|
159
181
|
let controller: AbortController;
|
|
160
182
|
|
|
161
|
-
if (abortable) {
|
|
183
|
+
if (abortable || shouldWaitTimeout) {
|
|
162
184
|
controller = new AbortController();
|
|
163
185
|
rest.signal = controller.signal;
|
|
164
186
|
}
|
|
@@ -195,6 +217,16 @@ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | F
|
|
|
195
217
|
return Err(err);
|
|
196
218
|
});
|
|
197
219
|
|
|
220
|
+
if (shouldWaitTimeout) {
|
|
221
|
+
setTimeout(() => {
|
|
222
|
+
if (!controller.signal.aborted) {
|
|
223
|
+
const error = new Error();
|
|
224
|
+
error.name = TIMEOUT_ERROR;
|
|
225
|
+
controller.abort(error);
|
|
226
|
+
}
|
|
227
|
+
}, timeout);
|
|
228
|
+
}
|
|
229
|
+
|
|
198
230
|
if (abortable) {
|
|
199
231
|
return {
|
|
200
232
|
abort(reason?: any): void {
|
package/src/mod.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './fetch/defines.ts';
|
|
2
2
|
export { fetchT } from './fetch/fetch.ts';
|