@happy-ts/fetch-t 1.0.8 → 1.0.10

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 CHANGED
@@ -4,6 +4,7 @@
4
4
  [![JSR Version](https://jsr.io/badges/@happy-ts/fetch-t)](https://jsr.io/@happy-ts/fetch-t)
5
5
  [![JSR Score](https://jsr.io/badges/@happy-ts/fetch-t/score)](https://jsr.io/@happy-ts/fetch-t/score)
6
6
  [![Build Status](https://github.com/jiangjie/fetch-t/actions/workflows/test.yml/badge.svg)](https://github.com/jiangjie/fetch-t/actions/workflows/test.yml)
7
+ [![codecov](https://codecov.io/gh/JiangJie/fetch-t/graph/badge.svg)](https://codecov.io/gh/JiangJie/fetch-t)
7
8
 
8
9
  ---
9
10
 
@@ -70,10 +71,12 @@ somethingHappenAsync(() => {
70
71
 
71
72
  const res = await fetchTask.response;
72
73
  if (res.isErr()) {
73
- console.assert(res.err() === 'cancel');
74
+ console.assert(res.unwrapErr() === 'cancel');
74
75
  } else {
75
76
  console.log(res.unwrap());
76
77
  }
77
78
  ```
78
79
 
79
- 更多示例可参见测试用例 <a href="tests/fetch.test.ts">fetch.test.ts</a>。
80
+ 更多示例可参见测试用例 <a href="tests/fetch.test.ts">fetch.test.ts</a>。
81
+
82
+ ## [文档](docs/README.md)
package/README.md CHANGED
@@ -5,10 +5,11 @@
5
5
  [![JSR Version](https://jsr.io/badges/@happy-ts/fetch-t)](https://jsr.io/@happy-ts/fetch-t)
6
6
  [![JSR Score](https://jsr.io/badges/@happy-ts/fetch-t/score)](https://jsr.io/@happy-ts/fetch-t/score)
7
7
  [![Build Status](https://github.com/jiangjie/fetch-t/actions/workflows/test.yml/badge.svg)](https://github.com/jiangjie/fetch-t/actions/workflows/test.yml)
8
+ [![codecov](https://codecov.io/gh/JiangJie/fetch-t/graph/badge.svg)](https://codecov.io/gh/JiangJie/fetch-t)
8
9
 
9
10
  ---
10
11
 
11
- <a href="README.cn.md">[中文]</a>
12
+ ## [中文](README.cn.md)
12
13
 
13
14
  ---
14
15
 
@@ -81,10 +82,12 @@ somethingHappenAsync(() => {
81
82
 
82
83
  const res = await fetchTask.response;
83
84
  if (res.isErr()) {
84
- console.assert(res.err() === 'cancel');
85
+ console.assert(res.unwrapErr() === 'cancel');
85
86
  } else {
86
87
  console.log(res.unwrap());
87
88
  }
88
89
  ```
89
90
 
90
91
  For more examples, please refer to test case <a href="tests/fetch.test.ts">fetch.test.ts</a>.
92
+
93
+ ## [Docs](docs/README.md)
package/dist/main.cjs CHANGED
@@ -1,19 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  var happyRusty = require('happy-rusty');
4
-
5
- function invariant(expr, createMsg) {
6
- if (!expr) {
7
- throw new TypeError(createMsg());
8
- }
9
- }
10
- function assertURL(url) {
11
- invariant(url instanceof URL, () => `Url must be an URL object. Received ${JSON.stringify(url)}`);
12
- }
4
+ var invariant = require('tiny-invariant');
13
5
 
14
6
  function fetchT(url, init) {
15
7
  if (typeof url !== "string") {
16
- assertURL(url);
8
+ invariant(url instanceof URL, () => `Url must be a string or URL object but received ${url}.`);
17
9
  }
18
10
  const { abortable = false, responseType, ...rest } = init ?? {};
19
11
  let controller;
@@ -23,6 +15,7 @@ function fetchT(url, init) {
23
15
  }
24
16
  const response = fetch(url, rest).then(async (res) => {
25
17
  if (!res.ok) {
18
+ await res.body?.cancel();
26
19
  return happyRusty.Err(new Error(`fetch status: ${res.status}`));
27
20
  }
28
21
  switch (responseType) {
package/dist/main.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.cjs","sources":["../src/fetch/assertions.ts","../src/fetch/fetch.ts"],"sourcesContent":["/**\n * assert function\n * @param expr\n * @param createMsg return a string message to throw\n */\nfunction invariant(expr: unknown, createMsg: () => string): void {\n if (!expr) {\n throw new TypeError(createMsg());\n }\n}\n\n/**\n * assert url is an URL object\n *\n * @param url\n */\nexport function assertURL(url: URL): void {\n invariant(url instanceof URL, () => `Url must be an URL object. Received ${ JSON.stringify(url) }`);\n}","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport { assertURL } from './assertions.ts';\nimport type { FetchInit, FetchResponse, FetchTask } from './defines.ts';\n\n/**\n * Return `FetchTask<string>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n/**\n * Return `FetchTask<ArrayBuffer>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n/**\n * Return `FetchTask<Blob>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n/**\n * Return `FetchTask<T>`.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n/**\n * Return `FetchResponse<string>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string>;\n/**\n * Return `FetchResponse<ArrayBuffer>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'arraybuffer';\n}): FetchResponse<ArrayBuffer>;\n/**\n * Return `FetchResponse<Blob>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'blob';\n}): FetchResponse<Blob>;\n/**\n * Return `FetchResponse<T>`.\n * @param url\n * @param init\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n responseType: 'json';\n}): FetchResponse<T>;\n/**\n * Return `FetchTask<Response>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n/**\n * Return `FetchResponse<Response>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: false;\n}): FetchResponse<Response>;\n/**\n * Return `FetchTask<T>` or `FetchResponse<T>`.\n * @param url\n * @param init\n */\nexport function fetchT<T = any>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;\n/**\n * Return `FetchResponse<Response>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;\n/**\n * Request the url and return the corresponding type based on the responseType.\n * @param url url to fetch\n * @param init fetch init\n * @returns {FetchTask<T> | FetchResponse<T>} an abort able fetch task or just response\n */\nexport function fetchT<T = any>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n if (typeof url !== 'string') {\n assertURL(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 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":";;;;AAKA,SAAS,SAAA,CAAU,MAAe,SAA+B,EAAA;AAC7D,EAAA,IAAI,CAAC,IAAM,EAAA;AACP,IAAM,MAAA,IAAI,SAAU,CAAA,SAAA,EAAW,CAAA,CAAA;AAAA,GACnC;AACJ,CAAA;AAOO,SAAS,UAAU,GAAgB,EAAA;AACtC,EAAU,SAAA,CAAA,GAAA,YAAe,KAAK,MAAM,CAAA,oCAAA,EAAwC,KAAK,SAAU,CAAA,GAAG,CAAE,CAAE,CAAA,CAAA,CAAA;AACtG;;ACuFgB,SAAA,MAAA,CAAgB,KAAmB,IAAmD,EAAA;AAClG,EAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AACzB,IAAA,SAAA,CAAU,GAAG,CAAA,CAAA;AAAA,GACjB;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,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` 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;;;;"}
package/dist/main.mjs CHANGED
@@ -1,17 +1,9 @@
1
1
  import { Err, Ok } from 'happy-rusty';
2
-
3
- function invariant(expr, createMsg) {
4
- if (!expr) {
5
- throw new TypeError(createMsg());
6
- }
7
- }
8
- function assertURL(url) {
9
- invariant(url instanceof URL, () => `Url must be an URL object. Received ${JSON.stringify(url)}`);
10
- }
2
+ import invariant from 'tiny-invariant';
11
3
 
12
4
  function fetchT(url, init) {
13
5
  if (typeof url !== "string") {
14
- assertURL(url);
6
+ invariant(url instanceof URL, () => `Url must be a string or URL object but received ${url}.`);
15
7
  }
16
8
  const { abortable = false, responseType, ...rest } = init ?? {};
17
9
  let controller;
@@ -21,6 +13,7 @@ function fetchT(url, init) {
21
13
  }
22
14
  const response = fetch(url, rest).then(async (res) => {
23
15
  if (!res.ok) {
16
+ await res.body?.cancel();
24
17
  return Err(new Error(`fetch status: ${res.status}`));
25
18
  }
26
19
  switch (responseType) {
package/dist/main.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.mjs","sources":["../src/fetch/assertions.ts","../src/fetch/fetch.ts"],"sourcesContent":["/**\n * assert function\n * @param expr\n * @param createMsg return a string message to throw\n */\nfunction invariant(expr: unknown, createMsg: () => string): void {\n if (!expr) {\n throw new TypeError(createMsg());\n }\n}\n\n/**\n * assert url is an URL object\n *\n * @param url\n */\nexport function assertURL(url: URL): void {\n invariant(url instanceof URL, () => `Url must be an URL object. Received ${ JSON.stringify(url) }`);\n}","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Err, Ok } from 'happy-rusty';\nimport { assertURL } from './assertions.ts';\nimport type { FetchInit, FetchResponse, FetchTask } from './defines.ts';\n\n/**\n * Return `FetchTask<string>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n/**\n * Return `FetchTask<ArrayBuffer>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n/**\n * Return `FetchTask<Blob>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n/**\n * Return `FetchTask<T>`.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n/**\n * Return `FetchResponse<string>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string>;\n/**\n * Return `FetchResponse<ArrayBuffer>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'arraybuffer';\n}): FetchResponse<ArrayBuffer>;\n/**\n * Return `FetchResponse<Blob>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'blob';\n}): FetchResponse<Blob>;\n/**\n * Return `FetchResponse<T>`.\n * @param url\n * @param init\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n responseType: 'json';\n}): FetchResponse<T>;\n/**\n * Return `FetchTask<Response>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n/**\n * Return `FetchResponse<Response>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: false;\n}): FetchResponse<Response>;\n/**\n * Return `FetchTask<T>` or `FetchResponse<T>`.\n * @param url\n * @param init\n */\nexport function fetchT<T = any>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;\n/**\n * Return `FetchResponse<Response>`.\n * @param url\n * @param init\n */\nexport function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;\n/**\n * Request the url and return the corresponding type based on the responseType.\n * @param url url to fetch\n * @param init fetch init\n * @returns {FetchTask<T> | FetchResponse<T>} an abort able fetch task or just response\n */\nexport function fetchT<T = any>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n if (typeof url !== 'string') {\n assertURL(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 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":";;AAKA,SAAS,SAAA,CAAU,MAAe,SAA+B,EAAA;AAC7D,EAAA,IAAI,CAAC,IAAM,EAAA;AACP,IAAM,MAAA,IAAI,SAAU,CAAA,SAAA,EAAW,CAAA,CAAA;AAAA,GACnC;AACJ,CAAA;AAOO,SAAS,UAAU,GAAgB,EAAA;AACtC,EAAU,SAAA,CAAA,GAAA,YAAe,KAAK,MAAM,CAAA,oCAAA,EAAwC,KAAK,SAAU,CAAA,GAAG,CAAE,CAAE,CAAA,CAAA,CAAA;AACtG;;ACuFgB,SAAA,MAAA,CAAgB,KAAmB,IAAmD,EAAA;AAClG,EAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AACzB,IAAA,SAAA,CAAU,GAAG,CAAA,CAAA;AAAA,GACjB;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,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/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;;;;"}
package/dist/types.d.ts CHANGED
@@ -1,133 +1,168 @@
1
1
  import { AsyncResult } from 'happy-rusty';
2
2
 
3
3
  /**
4
- * Response generic type.
4
+ * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.
5
+ *
6
+ * @typeParam T - The type of the data expected in the response.
5
7
  */
6
8
  type FetchResponse<T> = AsyncResult<T, any>;
7
9
  /**
8
- * Return type of fetchT when abortable.
10
+ * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.
11
+ *
12
+ * @typeParam T - The type of the data expected in the response.
9
13
  */
10
14
  interface FetchTask<T> {
11
15
  /**
12
- * Aborts the request.
13
- * @param reason The reason for aborting the request.
16
+ * Aborts the fetch task, optionally with a reason for the abortion.
17
+ *
18
+ * @param reason - An optional parameter to indicate why the task was aborted.
14
19
  */
15
20
  abort(reason?: any): void;
16
21
  /**
17
- * Returns true if inner AbortSignal's AbortController has signaled to abort, and false otherwise.
22
+ * Indicates whether the fetch task has been aborted.
18
23
  */
19
24
  aborted: boolean;
20
25
  /**
21
- * The response of the request.
26
+ * The response of the fetch task, represented as an `AsyncResult`.
22
27
  */
23
28
  response: FetchResponse<T>;
24
29
  }
25
30
  /**
26
- * Parameters for fetchT.
31
+ * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
27
32
  */
28
33
  interface FetchInit extends RequestInit {
29
34
  /**
30
- * true well return abort method.
35
+ * Indicates whether the fetch request should be abortable.
31
36
  */
32
37
  abortable?: boolean;
33
38
  /**
34
- * The response type of the request.
39
+ * Specifies the expected response type of the fetch request.
35
40
  */
36
41
  responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
37
42
  }
38
43
 
39
44
  /**
40
- * Return `FetchTask<string>`.
41
- * @param url
42
- * @param init
45
+ * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
46
+ *
47
+ * @typeParam T - The expected type of the response data.
48
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
49
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
50
+ * @returns A `FetchTask` representing the operation with a `string` response.
43
51
  */
44
52
  declare function fetchT(url: string | URL, init: FetchInit & {
45
53
  abortable: true;
46
54
  responseType: 'text';
47
55
  }): FetchTask<string>;
48
56
  /**
49
- * Return `FetchTask<ArrayBuffer>`.
50
- * @param url
51
- * @param init
57
+ * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.
58
+ *
59
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
60
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
61
+ * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.
52
62
  */
53
63
  declare function fetchT(url: string | URL, init: FetchInit & {
54
64
  abortable: true;
55
65
  responseType: 'arraybuffer';
56
66
  }): FetchTask<ArrayBuffer>;
57
67
  /**
58
- * Return `FetchTask<Blob>`.
59
- * @param url
60
- * @param init
68
+ * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.
69
+ *
70
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
71
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
72
+ * @returns A `FetchTask` representing the operation with a `Blob` response.
61
73
  */
62
74
  declare function fetchT(url: string | URL, init: FetchInit & {
63
75
  abortable: true;
64
76
  responseType: 'blob';
65
77
  }): FetchTask<Blob>;
66
78
  /**
67
- * Return `FetchTask<T>`.
79
+ * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.
80
+ *
81
+ * @typeParam T - The expected type of the parsed JSON data.
82
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
83
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
84
+ * @returns A `FetchTask` representing the operation with a response parsed as JSON.
68
85
  */
69
86
  declare function fetchT<T>(url: string | URL, init: FetchInit & {
70
87
  abortable: true;
71
88
  responseType: 'json';
72
89
  }): FetchTask<T>;
73
90
  /**
74
- * Return `FetchResponse<string>`.
75
- * @param url
76
- * @param init
91
+ * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.
92
+ *
93
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
94
+ * @param init - Additional options for the fetch operation, specifying the response type as 'text'.
95
+ * @returns A `FetchResponse` representing the operation with a `string` response.
77
96
  */
78
97
  declare function fetchT(url: string | URL, init: FetchInit & {
79
98
  responseType: 'text';
80
99
  }): FetchResponse<string>;
81
100
  /**
82
- * Return `FetchResponse<ArrayBuffer>`.
83
- * @param url
84
- * @param init
101
+ * Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.
102
+ *
103
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
104
+ * @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.
105
+ * @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.
85
106
  */
86
107
  declare function fetchT(url: string | URL, init: FetchInit & {
87
108
  responseType: 'arraybuffer';
88
109
  }): FetchResponse<ArrayBuffer>;
89
110
  /**
90
- * Return `FetchResponse<Blob>`.
91
- * @param url
92
- * @param init
111
+ * Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.
112
+ *
113
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
114
+ * @param init - Additional options for the fetch operation, specifying the response type as 'blob'.
115
+ * @returns A `FetchResponse` representing the operation with a `Blob` response.
93
116
  */
94
117
  declare function fetchT(url: string | URL, init: FetchInit & {
95
118
  responseType: 'blob';
96
119
  }): FetchResponse<Blob>;
97
120
  /**
98
- * Return `FetchResponse<T>`.
99
- * @param url
100
- * @param init
121
+ * Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.
122
+ *
123
+ * @typeParam T - The expected type of the parsed JSON data.
124
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
125
+ * @param init - Additional options for the fetch operation, specifying the response type as 'json'.
126
+ * @returns A `FetchResponse` representing the operation with a response parsed as JSON.
101
127
  */
102
128
  declare function fetchT<T>(url: string | URL, init: FetchInit & {
103
129
  responseType: 'json';
104
130
  }): FetchResponse<T>;
105
131
  /**
106
- * Return `FetchTask<Response>`.
107
- * @param url
108
- * @param init
132
+ * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.
133
+ *
134
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
135
+ * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.
136
+ * @returns A `FetchTask` representing the operation with a generic `Response`.
109
137
  */
110
138
  declare function fetchT(url: string | URL, init: FetchInit & {
111
139
  abortable: true;
112
140
  }): FetchTask<Response>;
113
141
  /**
114
- * Return `FetchResponse<Response>`.
115
- * @param url
116
- * @param init
142
+ * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
143
+ *
144
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
145
+ * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
146
+ * @returns A `FetchResponse` representing the operation with a generic `Response`.
117
147
  */
118
148
  declare function fetchT(url: string | URL, init: FetchInit & {
119
149
  abortable: false;
120
150
  }): FetchResponse<Response>;
121
151
  /**
122
- * Return `FetchTask<T>` or `FetchResponse<T>`.
123
- * @param url
124
- * @param init
152
+ * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
153
+ *
154
+ * @typeParam T - The expected type of the response data when not using a specific `responseType`.
155
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
156
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
157
+ * @returns A `FetchResponse` or `FetchTask` depending on the `abortable` option in `init`.
125
158
  */
126
- declare function fetchT<T = any>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;
159
+ declare function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;
127
160
  /**
128
- * Return `FetchResponse<Response>`.
129
- * @param url
130
- * @param init
161
+ * Fetches a resource from the network and returns a `FetchResponse` representing the operation.
162
+ *
163
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
164
+ * @param init - Standard `RequestInit` options for the fetch operation.
165
+ * @returns A `FetchResponse` representing the operation with a `Response` object.
131
166
  */
132
167
  declare function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;
133
168
 
package/docs/README.md ADDED
@@ -0,0 +1,24 @@
1
+ **@happy-ts/fetch-t** • **Docs**
2
+
3
+ ***
4
+
5
+ # @happy-ts/fetch-t
6
+
7
+ ## Interfaces
8
+
9
+ | Interface | Description |
10
+ | ------ | ------ |
11
+ | [FetchInit](interfaces/FetchInit.md) | Extends the standard `RequestInit` interface from the Fetch API to include additional custom options. |
12
+ | [FetchTask](interfaces/FetchTask.md) | Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status. |
13
+
14
+ ## Type Aliases
15
+
16
+ | Type alias | Description |
17
+ | ------ | ------ |
18
+ | [FetchResponse](type-aliases/FetchResponse.md) | Represents the response of a fetch operation, encapsulating the result data or any error that occurred. |
19
+
20
+ ## Functions
21
+
22
+ | Function | Description |
23
+ | ------ | ------ |
24
+ | [fetchT](functions/fetchT.md) | Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation. |
@@ -0,0 +1,339 @@
1
+ [**@happy-ts/fetch-t**](../README.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../README.md) / fetchT
6
+
7
+ # Function: fetchT()
8
+
9
+ Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.
10
+
11
+ ## Type Param
12
+
13
+ The expected type of the response data when not using a specific `responseType`.
14
+
15
+ ## Param
16
+
17
+ The resource to fetch. Can be a URL object or a string representing a URL.
18
+
19
+ ## Param
20
+
21
+ Additional options for the fetch operation, including custom `FetchInit` properties.
22
+
23
+ ## fetchT(url, init)
24
+
25
+ ```ts
26
+ function fetchT(url, init): FetchTask<string>
27
+ ```
28
+
29
+ Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
30
+
31
+ ### Parameters
32
+
33
+ | Parameter | Type | Description |
34
+ | ------ | ------ | ------ |
35
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
36
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"text"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
37
+
38
+ ### Returns
39
+
40
+ [`FetchTask`](../interfaces/FetchTask.md)\<`string`\>
41
+
42
+ A `FetchTask` representing the operation with a `string` response.
43
+
44
+ ### Defined in
45
+
46
+ [src/fetch/fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L14)
47
+
48
+ ## fetchT(url, init)
49
+
50
+ ```ts
51
+ function fetchT(url, init): FetchTask<ArrayBuffer>
52
+ ```
53
+
54
+ Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.
55
+
56
+ ### Parameters
57
+
58
+ | Parameter | Type | Description |
59
+ | ------ | ------ | ------ |
60
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
61
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"arraybuffer"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
62
+
63
+ ### Returns
64
+
65
+ [`FetchTask`](../interfaces/FetchTask.md)\<`ArrayBuffer`\>
66
+
67
+ A `FetchTask` representing the operation with an `ArrayBuffer` response.
68
+
69
+ ### Defined in
70
+
71
+ [src/fetch/fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L26)
72
+
73
+ ## fetchT(url, init)
74
+
75
+ ```ts
76
+ function fetchT(url, init): FetchTask<Blob>
77
+ ```
78
+
79
+ Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.
80
+
81
+ ### Parameters
82
+
83
+ | Parameter | Type | Description |
84
+ | ------ | ------ | ------ |
85
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
86
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"blob"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
87
+
88
+ ### Returns
89
+
90
+ [`FetchTask`](../interfaces/FetchTask.md)\<`Blob`\>
91
+
92
+ A `FetchTask` representing the operation with a `Blob` response.
93
+
94
+ ### Defined in
95
+
96
+ [src/fetch/fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L38)
97
+
98
+ ## fetchT(url, init)
99
+
100
+ ```ts
101
+ function fetchT<T>(url, init): FetchTask<T>
102
+ ```
103
+
104
+ Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.
105
+
106
+ ### Type Parameters
107
+
108
+ | Type Parameter | Description |
109
+ | ------ | ------ |
110
+ | `T` | The expected type of the parsed JSON data. |
111
+
112
+ ### Parameters
113
+
114
+ | Parameter | Type | Description |
115
+ | ------ | ------ | ------ |
116
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
117
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"json"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
118
+
119
+ ### Returns
120
+
121
+ [`FetchTask`](../interfaces/FetchTask.md)\<`T`\>
122
+
123
+ A `FetchTask` representing the operation with a response parsed as JSON.
124
+
125
+ ### Defined in
126
+
127
+ [src/fetch/fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L51)
128
+
129
+ ## fetchT(url, init)
130
+
131
+ ```ts
132
+ function fetchT(url, init): FetchResponse<string>
133
+ ```
134
+
135
+ Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.
136
+
137
+ ### Parameters
138
+
139
+ | Parameter | Type | Description |
140
+ | ------ | ------ | ------ |
141
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
142
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"text"`; \} | Additional options for the fetch operation, specifying the response type as 'text'. |
143
+
144
+ ### Returns
145
+
146
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`string`\>
147
+
148
+ A `FetchResponse` representing the operation with a `string` response.
149
+
150
+ ### Defined in
151
+
152
+ [src/fetch/fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L63)
153
+
154
+ ## fetchT(url, init)
155
+
156
+ ```ts
157
+ function fetchT(url, init): FetchResponse<ArrayBuffer>
158
+ ```
159
+
160
+ Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.
161
+
162
+ ### Parameters
163
+
164
+ | Parameter | Type | Description |
165
+ | ------ | ------ | ------ |
166
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
167
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"arraybuffer"`; \} | Additional options for the fetch operation, specifying the response type as 'arraybuffer'. |
168
+
169
+ ### Returns
170
+
171
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`ArrayBuffer`\>
172
+
173
+ A `FetchResponse` representing the operation with an `ArrayBuffer` response.
174
+
175
+ ### Defined in
176
+
177
+ [src/fetch/fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L74)
178
+
179
+ ## fetchT(url, init)
180
+
181
+ ```ts
182
+ function fetchT(url, init): FetchResponse<Blob>
183
+ ```
184
+
185
+ Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.
186
+
187
+ ### Parameters
188
+
189
+ | Parameter | Type | Description |
190
+ | ------ | ------ | ------ |
191
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
192
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"blob"`; \} | Additional options for the fetch operation, specifying the response type as 'blob'. |
193
+
194
+ ### Returns
195
+
196
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Blob`\>
197
+
198
+ A `FetchResponse` representing the operation with a `Blob` response.
199
+
200
+ ### Defined in
201
+
202
+ [src/fetch/fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L85)
203
+
204
+ ## fetchT(url, init)
205
+
206
+ ```ts
207
+ function fetchT<T>(url, init): FetchResponse<T>
208
+ ```
209
+
210
+ Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.
211
+
212
+ ### Type Parameters
213
+
214
+ | Type Parameter | Description |
215
+ | ------ | ------ |
216
+ | `T` | The expected type of the parsed JSON data. |
217
+
218
+ ### Parameters
219
+
220
+ | Parameter | Type | Description |
221
+ | ------ | ------ | ------ |
222
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
223
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"json"`; \} | Additional options for the fetch operation, specifying the response type as 'json'. |
224
+
225
+ ### Returns
226
+
227
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
228
+
229
+ A `FetchResponse` representing the operation with a response parsed as JSON.
230
+
231
+ ### Defined in
232
+
233
+ [src/fetch/fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L97)
234
+
235
+ ## fetchT(url, init)
236
+
237
+ ```ts
238
+ function fetchT(url, init): FetchTask<Response>
239
+ ```
240
+
241
+ Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.
242
+
243
+ ### Parameters
244
+
245
+ | Parameter | Type | Description |
246
+ | ------ | ------ | ------ |
247
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
248
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; \} | Additional options for the fetch operation, indicating that the operation should be abortable. |
249
+
250
+ ### Returns
251
+
252
+ [`FetchTask`](../interfaces/FetchTask.md)\<`Response`\>
253
+
254
+ A `FetchTask` representing the operation with a generic `Response`.
255
+
256
+ ### Defined in
257
+
258
+ [src/fetch/fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L108)
259
+
260
+ ## fetchT(url, init)
261
+
262
+ ```ts
263
+ function fetchT(url, init): FetchResponse<Response>
264
+ ```
265
+
266
+ Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
267
+
268
+ ### Parameters
269
+
270
+ | Parameter | Type | Description |
271
+ | ------ | ------ | ------ |
272
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
273
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `false`; \} | Additional options for the fetch operation, indicating that the operation should not be abortable. |
274
+
275
+ ### Returns
276
+
277
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
278
+
279
+ A `FetchResponse` representing the operation with a generic `Response`.
280
+
281
+ ### Defined in
282
+
283
+ [src/fetch/fetch.ts:119](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L119)
284
+
285
+ ## fetchT(url, init)
286
+
287
+ ```ts
288
+ function fetchT<T>(url, init): FetchTask<T> | FetchResponse<T>
289
+ ```
290
+
291
+ Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
292
+
293
+ ### Type Parameters
294
+
295
+ | Type Parameter | Description |
296
+ | ------ | ------ |
297
+ | `T` | The expected type of the response data when not using a specific `responseType`. |
298
+
299
+ ### Parameters
300
+
301
+ | Parameter | Type | Description |
302
+ | ------ | ------ | ------ |
303
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
304
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) | Additional options for the fetch operation, including custom `FetchInit` properties. |
305
+
306
+ ### Returns
307
+
308
+ [`FetchTask`](../interfaces/FetchTask.md)\<`T`\> \| [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
309
+
310
+ A `FetchResponse` or `FetchTask` depending on the `abortable` option in `init`.
311
+
312
+ ### Defined in
313
+
314
+ [src/fetch/fetch.ts:131](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L131)
315
+
316
+ ## fetchT(url, init)
317
+
318
+ ```ts
319
+ function fetchT(url, init?): FetchResponse<Response>
320
+ ```
321
+
322
+ Fetches a resource from the network and returns a `FetchResponse` representing the operation.
323
+
324
+ ### Parameters
325
+
326
+ | Parameter | Type | Description |
327
+ | ------ | ------ | ------ |
328
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
329
+ | `init`? | `RequestInit` | Standard `RequestInit` options for the fetch operation. |
330
+
331
+ ### Returns
332
+
333
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
334
+
335
+ A `FetchResponse` representing the operation with a `Response` object.
336
+
337
+ ### Defined in
338
+
339
+ [src/fetch/fetch.ts:140](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/fetch.ts#L140)
@@ -0,0 +1,34 @@
1
+ [**@happy-ts/fetch-t**](../README.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../README.md) / FetchInit
6
+
7
+ # Interface: FetchInit
8
+
9
+ Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
10
+
11
+ ## Extends
12
+
13
+ - `RequestInit`
14
+
15
+ ## Properties
16
+
17
+ | Property | Type | Description | Inherited from | Defined in |
18
+ | ------ | ------ | ------ | ------ | ------ |
19
+ | `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | - | [src/fetch/defines.ts:42](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/defines.ts#L42) |
20
+ | `body?` | `null` \| `BodyInit` | A BodyInit object or null to set request's body. | `RequestInit.body` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1693 |
21
+ | `cache?` | `RequestCache` | A string indicating how the request will interact with the browser's cache to set request's cache. | `RequestInit.cache` | node\_modules/.deno/typescript@5.5.3/node\_modules/typescript/lib/lib.dom.d.ts:1695 |
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 |
@@ -0,0 +1,46 @@
1
+ [**@happy-ts/fetch-t**](../README.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../README.md) / FetchTask
6
+
7
+ # Interface: FetchTask\<T\>
8
+
9
+ Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.
10
+
11
+ ## Type Parameters
12
+
13
+ | Type Parameter | Description |
14
+ | ------ | ------ |
15
+ | `T` | The type of the data expected in the response. |
16
+
17
+ ## Properties
18
+
19
+ | Property | Type | Description | Defined in |
20
+ | ------ | ------ | ------ | ------ |
21
+ | `aborted` | `boolean` | Indicates whether the fetch task has been aborted. | [src/fetch/defines.ts:27](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/defines.ts#L27) |
22
+ | `response` | [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\> | The response of the fetch task, represented as an `AsyncResult`. | [src/fetch/defines.ts:32](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/defines.ts#L32) |
23
+
24
+ ## Methods
25
+
26
+ ### abort()
27
+
28
+ ```ts
29
+ abort(reason?): void
30
+ ```
31
+
32
+ Aborts the fetch task, optionally with a reason for the abortion.
33
+
34
+ #### Parameters
35
+
36
+ | Parameter | Type | Description |
37
+ | ------ | ------ | ------ |
38
+ | `reason`? | `any` | An optional parameter to indicate why the task was aborted. |
39
+
40
+ #### Returns
41
+
42
+ `void`
43
+
44
+ #### Defined in
45
+
46
+ [src/fetch/defines.ts:22](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/src/fetch/defines.ts#L22)
@@ -0,0 +1,23 @@
1
+ [**@happy-ts/fetch-t**](../README.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../README.md) / FetchResponse
6
+
7
+ # Type Alias: FetchResponse\<T\>
8
+
9
+ ```ts
10
+ type FetchResponse<T>: AsyncResult<T, any>;
11
+ ```
12
+
13
+ Represents the response of a fetch operation, encapsulating the result data or any error that occurred.
14
+
15
+ ## Type Parameters
16
+
17
+ | Type Parameter | Description |
18
+ | ------ | ------ |
19
+ | `T` | The type of the data expected in the response. |
20
+
21
+ ## Defined in
22
+
23
+ [src/fetch/defines.ts:9](https://github.com/JiangJie/fetch-t/blob/9e5c4ce034f7bf6add07f55044bccbb16a68960c/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.8",
6
+ "version": "1.0.10",
7
7
  "type": "module",
8
8
  "source": "src/mod.ts",
9
9
  "main": "dist/main.cjs",
@@ -14,6 +14,7 @@
14
14
  "README.md",
15
15
  "README.cn.md",
16
16
  "package.json",
17
+ "docs",
17
18
  "src",
18
19
  "dist"
19
20
  ],
@@ -21,9 +22,14 @@
21
22
  "scripts": {
22
23
  "check": "pnpm exec tsc --noEmit",
23
24
  "lint": "pnpm exec eslint src/",
24
- "prebuild": "pnpm exec rimraf dist && pnpm run check && pnpm run lint",
25
+ "prebuild": "pnpm dlx rimraf dist && pnpm run check && pnpm run lint",
25
26
  "build": "pnpm exec rollup --config rollup.config.mjs",
26
- "test": "bun test --coverage",
27
+ "pretest": "pnpm dlx rimraf coverage",
28
+ "test": "deno test -A --coverage && deno coverage coverage && deno coverage coverage --lcov --output=coverage/cov_profile.lcov",
29
+ "pretest:html": "pnpm run pretest",
30
+ "test:html": "deno test -A --coverage && deno coverage coverage && deno coverage coverage --html",
31
+ "predocs": "pnpm dlx rimraf docs",
32
+ "docs": "pnpm exec typedoc",
27
33
  "prepublishOnly": "pnpm run build"
28
34
  },
29
35
  "repository": {
@@ -37,18 +43,19 @@
37
43
  "responseType"
38
44
  ],
39
45
  "devDependencies": {
40
- "@jest/globals": "^29.7.0",
41
- "@typescript-eslint/eslint-plugin": "^7.9.0",
42
- "@typescript-eslint/parser": "^7.9.0",
46
+ "@typescript-eslint/eslint-plugin": "^7.16.0",
47
+ "@typescript-eslint/parser": "^7.16.0",
43
48
  "eslint": "^8.57.0",
44
- "rimraf": "^5.0.7",
45
- "rollup": "^4.17.2",
46
- "rollup-plugin-dts": "^6.1.0",
49
+ "rollup": "^4.18.1",
50
+ "rollup-plugin-dts": "^6.1.1",
47
51
  "rollup-plugin-esbuild": "^6.1.1",
48
- "typescript": "^5.4.5"
52
+ "typedoc": "^0.26.4",
53
+ "typedoc-plugin-markdown": "^4.2.1",
54
+ "typescript": "^5.5.3"
49
55
  },
50
56
  "dependencies": {
51
- "happy-rusty": "^1.0.9"
57
+ "happy-rusty": "^1.1.1",
58
+ "tiny-invariant": "^1.3.3"
52
59
  },
53
- "packageManager": "pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195"
60
+ "packageManager": "pnpm@9.5.0"
54
61
  }
@@ -2,39 +2,47 @@
2
2
  import type { AsyncResult } from 'happy-rusty';
3
3
 
4
4
  /**
5
- * Response generic type.
5
+ * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.
6
+ *
7
+ * @typeParam T - The type of the data expected in the response.
6
8
  */
7
9
  export type FetchResponse<T> = AsyncResult<T, any>;
8
10
 
9
11
  /**
10
- * Return type of fetchT when abortable.
12
+ * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.
13
+ *
14
+ * @typeParam T - The type of the data expected in the response.
11
15
  */
12
16
  export interface FetchTask<T> {
13
17
  /**
14
- * Aborts the request.
15
- * @param reason The reason for aborting the request.
18
+ * Aborts the fetch task, optionally with a reason for the abortion.
19
+ *
20
+ * @param reason - An optional parameter to indicate why the task was aborted.
16
21
  */
17
22
  abort(reason?: any): void;
23
+
18
24
  /**
19
- * Returns true if inner AbortSignal's AbortController has signaled to abort, and false otherwise.
25
+ * Indicates whether the fetch task has been aborted.
20
26
  */
21
27
  aborted: boolean;
28
+
22
29
  /**
23
- * The response of the request.
30
+ * The response of the fetch task, represented as an `AsyncResult`.
24
31
  */
25
32
  response: FetchResponse<T>;
26
33
  }
27
34
 
28
35
  /**
29
- * Parameters for fetchT.
36
+ * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
30
37
  */
31
38
  export interface FetchInit extends RequestInit {
32
39
  /**
33
- * true well return abort method.
40
+ * Indicates whether the fetch request should be abortable.
34
41
  */
35
42
  abortable?: boolean;
43
+
36
44
  /**
37
- * The response type of the request.
45
+ * Specifies the expected response type of the fetch request.
38
46
  */
39
47
  responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
40
48
  }
@@ -1,111 +1,156 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { Err, Ok } from 'happy-rusty';
3
- import { assertURL } from './assertions.ts';
3
+ import invariant from 'tiny-invariant';
4
4
  import type { FetchInit, FetchResponse, FetchTask } from './defines.ts';
5
5
 
6
6
  /**
7
- * Return `FetchTask<string>`.
8
- * @param url
9
- * @param init
7
+ * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
8
+ *
9
+ * @typeParam T - The expected type of the response data.
10
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
11
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
12
+ * @returns A `FetchTask` representing the operation with a `string` response.
10
13
  */
11
14
  export function fetchT(url: string | URL, init: FetchInit & {
12
15
  abortable: true;
13
16
  responseType: 'text';
14
17
  }): FetchTask<string>;
18
+
15
19
  /**
16
- * Return `FetchTask<ArrayBuffer>`.
17
- * @param url
18
- * @param init
20
+ * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.
21
+ *
22
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
23
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
24
+ * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.
19
25
  */
20
26
  export function fetchT(url: string | URL, init: FetchInit & {
21
27
  abortable: true;
22
28
  responseType: 'arraybuffer';
23
29
  }): FetchTask<ArrayBuffer>;
30
+
24
31
  /**
25
- * Return `FetchTask<Blob>`.
26
- * @param url
27
- * @param init
32
+ * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.
33
+ *
34
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
35
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
36
+ * @returns A `FetchTask` representing the operation with a `Blob` response.
28
37
  */
29
38
  export function fetchT(url: string | URL, init: FetchInit & {
30
39
  abortable: true;
31
40
  responseType: 'blob';
32
41
  }): FetchTask<Blob>;
42
+
33
43
  /**
34
- * Return `FetchTask<T>`.
44
+ * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.
45
+ *
46
+ * @typeParam T - The expected type of the parsed JSON data.
47
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
48
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
49
+ * @returns A `FetchTask` representing the operation with a response parsed as JSON.
35
50
  */
36
51
  export function fetchT<T>(url: string | URL, init: FetchInit & {
37
52
  abortable: true;
38
53
  responseType: 'json';
39
54
  }): FetchTask<T>;
55
+
40
56
  /**
41
- * Return `FetchResponse<string>`.
42
- * @param url
43
- * @param init
57
+ * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.
58
+ *
59
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
60
+ * @param init - Additional options for the fetch operation, specifying the response type as 'text'.
61
+ * @returns A `FetchResponse` representing the operation with a `string` response.
44
62
  */
45
63
  export function fetchT(url: string | URL, init: FetchInit & {
46
64
  responseType: 'text';
47
65
  }): FetchResponse<string>;
66
+
48
67
  /**
49
- * Return `FetchResponse<ArrayBuffer>`.
50
- * @param url
51
- * @param init
68
+ * Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.
69
+ *
70
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
71
+ * @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.
72
+ * @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.
52
73
  */
53
74
  export function fetchT(url: string | URL, init: FetchInit & {
54
75
  responseType: 'arraybuffer';
55
76
  }): FetchResponse<ArrayBuffer>;
77
+
56
78
  /**
57
- * Return `FetchResponse<Blob>`.
58
- * @param url
59
- * @param init
79
+ * Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.
80
+ *
81
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
82
+ * @param init - Additional options for the fetch operation, specifying the response type as 'blob'.
83
+ * @returns A `FetchResponse` representing the operation with a `Blob` response.
60
84
  */
61
85
  export function fetchT(url: string | URL, init: FetchInit & {
62
86
  responseType: 'blob';
63
87
  }): FetchResponse<Blob>;
88
+
64
89
  /**
65
- * Return `FetchResponse<T>`.
66
- * @param url
67
- * @param init
90
+ * Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.
91
+ *
92
+ * @typeParam T - The expected type of the parsed JSON data.
93
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
94
+ * @param init - Additional options for the fetch operation, specifying the response type as 'json'.
95
+ * @returns A `FetchResponse` representing the operation with a response parsed as JSON.
68
96
  */
69
97
  export function fetchT<T>(url: string | URL, init: FetchInit & {
70
98
  responseType: 'json';
71
99
  }): FetchResponse<T>;
100
+
72
101
  /**
73
- * Return `FetchTask<Response>`.
74
- * @param url
75
- * @param init
102
+ * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.
103
+ *
104
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
105
+ * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.
106
+ * @returns A `FetchTask` representing the operation with a generic `Response`.
76
107
  */
77
108
  export function fetchT(url: string | URL, init: FetchInit & {
78
109
  abortable: true;
79
110
  }): FetchTask<Response>;
111
+
80
112
  /**
81
- * Return `FetchResponse<Response>`.
82
- * @param url
83
- * @param init
113
+ * Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
114
+ *
115
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
116
+ * @param init - Additional options for the fetch operation, indicating that the operation should not be abortable.
117
+ * @returns A `FetchResponse` representing the operation with a generic `Response`.
84
118
  */
85
119
  export function fetchT(url: string | URL, init: FetchInit & {
86
120
  abortable: false;
87
121
  }): FetchResponse<Response>;
122
+
88
123
  /**
89
- * Return `FetchTask<T>` or `FetchResponse<T>`.
90
- * @param url
91
- * @param init
124
+ * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
125
+ *
126
+ * @typeParam T - The expected type of the response data when not using a specific `responseType`.
127
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
128
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
129
+ * @returns A `FetchResponse` or `FetchTask` depending on the `abortable` option in `init`.
92
130
  */
93
- export function fetchT<T = any>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;
131
+ export function fetchT<T>(url: string | URL, init: FetchInit): FetchTask<T> | FetchResponse<T>;
132
+
94
133
  /**
95
- * Return `FetchResponse<Response>`.
96
- * @param url
97
- * @param init
134
+ * Fetches a resource from the network and returns a `FetchResponse` representing the operation.
135
+ *
136
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
137
+ * @param init - Standard `RequestInit` options for the fetch operation.
138
+ * @returns A `FetchResponse` representing the operation with a `Response` object.
98
139
  */
99
140
  export function fetchT(url: string | URL, init?: RequestInit): FetchResponse<Response>;
141
+
100
142
  /**
101
- * Request the url and return the corresponding type based on the responseType.
102
- * @param url url to fetch
103
- * @param init fetch init
104
- * @returns {FetchTask<T> | FetchResponse<T>} an abort able fetch task or just response
143
+ * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.
144
+ *
145
+ * @typeParam T - The expected type of the response data when not using a specific `responseType`.
146
+ * @param url - The resource to fetch. Can be a URL object or a string representing a URL.
147
+ * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
148
+ * @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
105
149
  */
106
- export function fetchT<T = any>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {
150
+ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {
151
+ // most cases
107
152
  if (typeof url !== 'string') {
108
- assertURL(url);
153
+ invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);
109
154
  }
110
155
 
111
156
  // default not abort able
@@ -120,6 +165,7 @@ export function fetchT<T = any>(url: string | URL, init?: FetchInit): FetchTask<
120
165
 
121
166
  const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {
122
167
  if (!res.ok) {
168
+ await res.body?.cancel();
123
169
  return Err(new Error(`fetch status: ${ res.status }`));
124
170
  }
125
171
 
@@ -1,19 +0,0 @@
1
- /**
2
- * assert function
3
- * @param expr
4
- * @param createMsg return a string message to throw
5
- */
6
- function invariant(expr: unknown, createMsg: () => string): void {
7
- if (!expr) {
8
- throw new TypeError(createMsg());
9
- }
10
- }
11
-
12
- /**
13
- * assert url is an URL object
14
- *
15
- * @param url
16
- */
17
- export function assertURL(url: URL): void {
18
- invariant(url instanceof URL, () => `Url must be an URL object. Received ${ JSON.stringify(url) }`);
19
- }