@happy-ts/fetch-t 1.0.10 → 1.0.12
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 +21 -2
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +21 -2
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +14 -0
- package/docs/functions/fetchT.md +205 -24
- package/docs/interfaces/FetchInit.md +5 -18
- package/docs/interfaces/FetchTask.md +3 -3
- package/docs/type-aliases/FetchResponse.md +1 -1
- package/package.json +8 -8
- package/src/fetch/defines.ts +5 -0
- package/src/fetch/fetch.ts +35 -3
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
|
@@ -7,9 +7,19 @@ function fetchT(url, init) {
|
|
|
7
7
|
if (typeof url !== "string") {
|
|
8
8
|
invariant(url instanceof URL, () => `Url must be a string or URL object but received ${url}.`);
|
|
9
9
|
}
|
|
10
|
-
const {
|
|
10
|
+
const {
|
|
11
|
+
// default not abort able
|
|
12
|
+
abortable = false,
|
|
13
|
+
responseType,
|
|
14
|
+
timeout,
|
|
15
|
+
...rest
|
|
16
|
+
} = init ?? {};
|
|
17
|
+
const shouldWaitTimeout = timeout != null;
|
|
18
|
+
if (shouldWaitTimeout) {
|
|
19
|
+
invariant(typeof timeout === "number" && timeout > 0, () => `Timeout must be a number greater than 0 but received ${timeout}.`);
|
|
20
|
+
}
|
|
11
21
|
let controller;
|
|
12
|
-
if (abortable) {
|
|
22
|
+
if (abortable || shouldWaitTimeout) {
|
|
13
23
|
controller = new AbortController();
|
|
14
24
|
rest.signal = controller.signal;
|
|
15
25
|
}
|
|
@@ -42,6 +52,15 @@ function fetchT(url, init) {
|
|
|
42
52
|
}).catch((err) => {
|
|
43
53
|
return happyRusty.Err(err);
|
|
44
54
|
});
|
|
55
|
+
if (shouldWaitTimeout) {
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
if (!controller.signal.aborted) {
|
|
58
|
+
const error = new Error();
|
|
59
|
+
error.name = "TimeoutError";
|
|
60
|
+
controller.abort(error);
|
|
61
|
+
}
|
|
62
|
+
}, timeout);
|
|
63
|
+
}
|
|
45
64
|
if (abortable) {
|
|
46
65
|
return {
|
|
47
66
|
abort(reason) {
|
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/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` 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 = 'TimeoutError';\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":";;;;;AAgKgB,SAAA,MAAA,CAAU,KAAmB,IAAmD,EAAA;AAE5F,EAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AACzB,IAAA,SAAA,CAAU,GAAe,YAAA,GAAA,EAAK,MAAM,CAAA,gDAAA,EAAoD,GAAI,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,GACnG;AAEA,EAAM,MAAA;AAAA;AAAA,IAEF,SAAY,GAAA,KAAA;AAAA,IACZ,YAAA;AAAA,IACA,OAAA;AAAA,IACA,GAAG,IAAA;AAAA,GACP,GAAI,QAAQ,EAAC,CAAA;AAEb,EAAA,MAAM,oBAAoB,OAAW,IAAA,IAAA,CAAA;AAErC,EAAA,IAAI,iBAAmB,EAAA;AACnB,IAAU,SAAA,CAAA,OAAO,YAAY,QAAY,IAAA,OAAA,GAAU,GAAG,MAAM,CAAA,qDAAA,EAAyD,OAA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;AAAA,GACH,CAAA,CAAE,KAAM,CAAA,CAAC,GAAQ,KAAA;AACd,IAAA,OAAOD,eAAI,GAAG,CAAA,CAAA;AAAA,GACjB,CAAA,CAAA;AAED,EAAA,IAAI,iBAAmB,EAAA;AACnB,IAAA,UAAA,CAAW,MAAM;AACb,MAAI,IAAA,CAAC,UAAW,CAAA,MAAA,CAAO,OAAS,EAAA;AAC5B,QAAM,MAAA,KAAA,GAAQ,IAAI,KAAM,EAAA,CAAA;AACxB,QAAA,KAAA,CAAM,IAAO,GAAA,cAAA,CAAA;AACb,QAAA,UAAA,CAAW,MAAM,KAAK,CAAA,CAAA;AAAA,OAC1B;AAAA,OACD,OAAO,CAAA,CAAA;AAAA,GACd;AAEA,EAAA,IAAI,SAAW,EAAA;AACX,IAAO,OAAA;AAAA,MACH,MAAM,MAAoB,EAAA;AACtB,QAAA,UAAA,CAAW,MAAM,MAAM,CAAA,CAAA;AAAA,OAC3B;AAAA,MACA,IAAI,OAAmB,GAAA;AACnB,QAAA,OAAO,WAAW,MAAO,CAAA,OAAA,CAAA;AAAA,OAC7B;AAAA,MACA,QAAA;AAAA,KACJ,CAAA;AAAA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;"}
|
package/dist/main.mjs
CHANGED
|
@@ -5,9 +5,19 @@ function fetchT(url, init) {
|
|
|
5
5
|
if (typeof url !== "string") {
|
|
6
6
|
invariant(url instanceof URL, () => `Url must be a string or URL object but received ${url}.`);
|
|
7
7
|
}
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
// default not abort able
|
|
10
|
+
abortable = false,
|
|
11
|
+
responseType,
|
|
12
|
+
timeout,
|
|
13
|
+
...rest
|
|
14
|
+
} = init ?? {};
|
|
15
|
+
const shouldWaitTimeout = timeout != null;
|
|
16
|
+
if (shouldWaitTimeout) {
|
|
17
|
+
invariant(typeof timeout === "number" && timeout > 0, () => `Timeout must be a number greater than 0 but received ${timeout}.`);
|
|
18
|
+
}
|
|
9
19
|
let controller;
|
|
10
|
-
if (abortable) {
|
|
20
|
+
if (abortable || shouldWaitTimeout) {
|
|
11
21
|
controller = new AbortController();
|
|
12
22
|
rest.signal = controller.signal;
|
|
13
23
|
}
|
|
@@ -40,6 +50,15 @@ function fetchT(url, init) {
|
|
|
40
50
|
}).catch((err) => {
|
|
41
51
|
return Err(err);
|
|
42
52
|
});
|
|
53
|
+
if (shouldWaitTimeout) {
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
if (!controller.signal.aborted) {
|
|
56
|
+
const error = new Error();
|
|
57
|
+
error.name = "TimeoutError";
|
|
58
|
+
controller.abort(error);
|
|
59
|
+
}
|
|
60
|
+
}, timeout);
|
|
61
|
+
}
|
|
43
62
|
if (abortable) {
|
|
44
63
|
return {
|
|
45
64
|
abort(reason) {
|
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,E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AAA,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/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` 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 = 'TimeoutError';\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 abor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,OAAO,WAAW,MAAO,CAAA,OAAA,CAAA;AAAA,OAC7B;AAAA,MACA,QAAA;AAAA,KACJ,CAAA;AAAA,GACG,MAAA;AACH,IAAO,OAAA,QAAA,CAAA;AAAA,GACX;AACJ;;;;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -39,6 +39,10 @@ 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
|
}
|
|
43
47
|
|
|
44
48
|
/**
|
|
@@ -148,6 +152,16 @@ declare function fetchT(url: string | URL, init: FetchInit & {
|
|
|
148
152
|
declare function fetchT(url: string | URL, init: FetchInit & {
|
|
149
153
|
abortable: false;
|
|
150
154
|
}): FetchResponse<Response>;
|
|
155
|
+
/**
|
|
156
|
+
* Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
|
|
157
|
+
*
|
|
158
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
159
|
+
* @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
|
|
160
|
+
* @returns A `FetchResponse` representing the operation with a generic `Response`.
|
|
161
|
+
*/
|
|
162
|
+
declare function fetchT(url: string | URL, init: FetchInit & {
|
|
163
|
+
timeout: number;
|
|
164
|
+
}): FetchResponse<Response>;
|
|
151
165
|
/**
|
|
152
166
|
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
153
167
|
*
|
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
|
-
[
|
|
58
|
+
[fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
95
|
+
[fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
132
|
+
[fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
175
|
+
[fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
212
|
+
[fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
249
|
+
[fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
286
|
+
[fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
329
|
+
[fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
366
|
+
[fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
403
|
+
[fetch.ts:119](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
483
|
+
[fetch.ts:142](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
520
|
+
[fetch.ts:151](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/src/fetch/fetch.ts#L151)
|
|
@@ -14,21 +14,8 @@ Extends the standard `RequestInit` interface from the Fetch API to include addit
|
|
|
14
14
|
|
|
15
15
|
## Properties
|
|
16
16
|
|
|
17
|
-
| Property | Type | Description |
|
|
18
|
-
| ------ | ------ | ------ | ------ |
|
|
19
|
-
| `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. |
|
|
20
|
-
| `
|
|
21
|
-
| `
|
|
22
|
-
| `credentials?` | `RequestCredentials` | A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. | `RequestInit.credentials` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1697 |
|
|
23
|
-
| `headers?` | `HeadersInit` | A Headers object, an object literal, or an array of two-item arrays to set request's headers. | `RequestInit.headers` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1699 |
|
|
24
|
-
| `integrity?` | `string` | A cryptographic hash of the resource to be fetched by request. Sets request's integrity. | `RequestInit.integrity` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1701 |
|
|
25
|
-
| `keepalive?` | `boolean` | A boolean to set request's keepalive. | `RequestInit.keepalive` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1703 |
|
|
26
|
-
| `method?` | `string` | A string to set request's method. | `RequestInit.method` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1705 |
|
|
27
|
-
| `mode?` | `RequestMode` | A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. | `RequestInit.mode` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1707 |
|
|
28
|
-
| `priority?` | `RequestPriority` | - | `RequestInit.priority` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1708 |
|
|
29
|
-
| `redirect?` | `RequestRedirect` | A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. | `RequestInit.redirect` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1710 |
|
|
30
|
-
| `referrer?` | `string` | A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. | `RequestInit.referrer` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1712 |
|
|
31
|
-
| `referrerPolicy?` | `ReferrerPolicy` | A referrer policy to set request's referrerPolicy. | `RequestInit.referrerPolicy` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1714 |
|
|
32
|
-
| `responseType?` | `"text"` \| `"arraybuffer"` \| `"blob"` \| `"json"` | Specifies the expected response type of the fetch request. | - | [src/fetch/defines.ts:47](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/defines.ts#L47) |
|
|
33
|
-
| `signal?` | `null` \| `AbortSignal` | An AbortSignal to set request's signal. | `RequestInit.signal` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1716 |
|
|
34
|
-
| `window?` | `null` | Can only be null. Used to disassociate request from any Window. | `RequestInit.window` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1718 |
|
|
17
|
+
| Property | Type | Description | Defined in |
|
|
18
|
+
| ------ | ------ | ------ | ------ |
|
|
19
|
+
| `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | [defines.ts:42](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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. | [
|
|
22
|
-
| `response` | [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\> | The response of the fetch task, represented as an `AsyncResult`. | [
|
|
21
|
+
| `aborted` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
46
|
+
[defines.ts:22](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/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
|
-
[
|
|
23
|
+
[defines.ts:9](https://github.com/JiangJie/fetch-t/blob/ab9a610ee9b332a4dd133c29ce0932dbdbc0353d/src/fetch/defines.ts#L9)
|
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.12",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"source": "src/mod.ts",
|
|
9
9
|
"main": "dist/main.cjs",
|
|
@@ -43,18 +43,18 @@
|
|
|
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
|
-
"happy-rusty": "^1.1.
|
|
57
|
+
"happy-rusty": "^1.1.2",
|
|
58
58
|
"tiny-invariant": "^1.3.3"
|
|
59
59
|
},
|
|
60
60
|
"packageManager": "pnpm@9.5.0"
|
package/src/fetch/defines.ts
CHANGED
|
@@ -45,4 +45,9 @@ 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
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Specifies the maximum time in milliseconds to wait for the fetch request to complete.
|
|
51
|
+
*/
|
|
52
|
+
timeout?: number;
|
|
48
53
|
}
|
package/src/fetch/fetch.ts
CHANGED
|
@@ -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 = 'TimeoutError';
|
|
225
|
+
controller.abort(error);
|
|
226
|
+
}
|
|
227
|
+
}, timeout);
|
|
228
|
+
}
|
|
229
|
+
|
|
198
230
|
if (abortable) {
|
|
199
231
|
return {
|
|
200
232
|
abort(reason?: any): void {
|