@happy-ts/fetch-t 1.0.7 → 1.0.9

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/index.md)
package/README.md CHANGED
@@ -1,13 +1,15 @@
1
1
  # fetchT
2
2
 
3
- [![NPM version](http://img.shields.io/npm/v/@happy-ts/fetch-t.svg)](https://npmjs.org/package/@happy-ts/fetch-t)
3
+ [![NPM version](https://img.shields.io/npm/v/@happy-ts/fetch-t.svg)](https://npmjs.org/package/@happy-ts/fetch-t)
4
+ [![NPM downloads](https://badgen.net/npm/dm/@happy-ts/fetch-t)](https://npmjs.org/package/@happy-ts/fetch-t)
4
5
  [![JSR Version](https://jsr.io/badges/@happy-ts/fetch-t)](https://jsr.io/@happy-ts/fetch-t)
5
6
  [![JSR Score](https://jsr.io/badges/@happy-ts/fetch-t/score)](https://jsr.io/@happy-ts/fetch-t/score)
6
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)
7
9
 
8
10
  ---
9
11
 
10
- <a href="README.cn.md">[中文]</a>
12
+ ## [中文](README.cn.md)
11
13
 
12
14
  ---
13
15
 
@@ -20,31 +22,37 @@ The return data of fetchT is of a specific type, which can be either `string`, `
20
22
  ## Installation
21
23
 
22
24
  via pnpm
25
+
23
26
  ```
24
27
  pnpm add @happy-ts/fetch-t
25
28
  ```
26
29
 
27
30
  or via yarn
31
+
28
32
  ```
29
33
  yarn add @happy-ts/fetch-t
30
34
  ```
31
35
 
32
36
  or just from npm
37
+
33
38
  ```
34
39
  npm install --save @happy-ts/fetch-t
35
40
  ```
36
41
 
37
42
  via JSR
43
+
38
44
  ```
39
45
  jsr add @happy-ts/fetch-t
40
46
  ```
41
47
 
42
48
  for deno
49
+
43
50
  ```
44
51
  deno add @happy-ts/fetch-t
45
52
  ```
46
53
 
47
54
  for bun
55
+
48
56
  ```
49
57
  bunx jsr add @happy-ts/fetch-t
50
58
  ```
@@ -53,8 +61,8 @@ bunx jsr add @happy-ts/fetch-t
53
61
 
54
62
  fetchT is a simple encapsulation of the [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API, with two main modifications:
55
63
 
56
- * It adds the `abortable` parameter. If `abortable: true` is passed, fetchT will return a `FetchTask` object that allows you to abort the request by calling `FetchTask.abort()`.
57
- * It supports generic return values by adding the responseType parameter. The optional values for `responseType` include `'text' | 'arraybuffer' | 'blob' | 'json'`. The return value corresponds to the parameter and can be either `string | ArrayBuffer | Blob | T`, where T is the generic type. All return values are of the [Result](https://github.com/JiangJie/happy-rusty) type, which facilitates error handling.
64
+ - It adds the `abortable` parameter. If `abortable: true` is passed, fetchT will return a `FetchTask` object that allows you to abort the request by calling `FetchTask.abort()`.
65
+ - It supports generic return values by adding the responseType parameter. The optional values for `responseType` include `'text' | 'arraybuffer' | 'blob' | 'json'`. The return value corresponds to the parameter and can be either `string | ArrayBuffer | Blob | T`, where T is the generic type. All return values are of the [Result](https://github.com/JiangJie/happy-rusty) type, which facilitates error handling.
58
66
 
59
67
  If you don't have these requirements, it is recommended to use the vanilla [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
60
68
 
@@ -74,10 +82,12 @@ somethingHappenAsync(() => {
74
82
 
75
83
  const res = await fetchTask.response;
76
84
  if (res.isErr()) {
77
- console.assert(res.err() === 'cancel');
85
+ console.assert(res.unwrapErr() === 'cancel');
78
86
  } else {
79
87
  console.log(res.unwrap());
80
88
  }
81
89
  ```
82
90
 
83
- For more examples, please refer to test case <a href="tests/fetch.test.ts">fetch.test.ts</a>.
91
+ For more examples, please refer to test case <a href="tests/fetch.test.ts">fetch.test.ts</a>.
92
+
93
+ ## [Docs](docs/index.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(typeof url === "string", () => `Url must be a string. Received ${JSON.stringify(url)}`);
12
- }
4
+ var invariant = require('tiny-invariant');
13
5
 
14
6
  function fetchT(url, init) {
15
- if (typeof url === "string") {
16
- assertUrl(url);
7
+ if (typeof url !== "string") {
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 a string\n *\n * @param url\n */\nexport function assertUrl(url: string): void {\n invariant(typeof url === 'string', () => `Url must be a string. 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,GAAmB,EAAA;AACzC,EAAU,SAAA,CAAA,OAAO,QAAQ,QAAU,EAAA,MAAM,kCAAmC,IAAK,CAAA,SAAA,CAAU,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(typeof url === "string", () => `Url must be a string. Received ${JSON.stringify(url)}`);
10
- }
2
+ import invariant from 'tiny-invariant';
11
3
 
12
4
  function fetchT(url, init) {
13
- if (typeof url === "string") {
14
- assertUrl(url);
5
+ if (typeof url !== "string") {
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 a string\n *\n * @param url\n */\nexport function assertUrl(url: string): void {\n invariant(typeof url === 'string', () => `Url must be a string. 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,GAAmB,EAAA;AACzC,EAAU,SAAA,CAAA,OAAO,QAAQ,QAAU,EAAA,MAAM,kCAAmC,IAAK,CAAA,SAAA,CAAU,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
 
@@ -0,0 +1,325 @@
1
+ [**@happy-ts/fetch-t**](../index.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../index.md) / fetchT
6
+
7
+ # Function: fetchT()
8
+
9
+ ## fetchT(url, init)
10
+
11
+ ```ts
12
+ function fetchT(url, init): FetchTask<string>
13
+ ```
14
+
15
+ Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
16
+
17
+ ### Parameters
18
+
19
+ | Parameter | Type | Description |
20
+ | :------ | :------ | :------ |
21
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
22
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"text"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
23
+
24
+ ### Returns
25
+
26
+ [`FetchTask`](../interfaces/FetchTask.md)\<`string`\>
27
+
28
+ A `FetchTask` representing the operation with a `string` response.
29
+
30
+ ### Source
31
+
32
+ [src/fetch/fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L14)
33
+
34
+ ## fetchT(url, init)
35
+
36
+ ```ts
37
+ function fetchT(url, init): FetchTask<ArrayBuffer>
38
+ ```
39
+
40
+ Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.
41
+
42
+ ### Parameters
43
+
44
+ | Parameter | Type | Description |
45
+ | :------ | :------ | :------ |
46
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
47
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"arraybuffer"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
48
+
49
+ ### Returns
50
+
51
+ [`FetchTask`](../interfaces/FetchTask.md)\<`ArrayBuffer`\>
52
+
53
+ A `FetchTask` representing the operation with an `ArrayBuffer` response.
54
+
55
+ ### Source
56
+
57
+ [src/fetch/fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L26)
58
+
59
+ ## fetchT(url, init)
60
+
61
+ ```ts
62
+ function fetchT(url, init): FetchTask<Blob>
63
+ ```
64
+
65
+ Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.
66
+
67
+ ### Parameters
68
+
69
+ | Parameter | Type | Description |
70
+ | :------ | :------ | :------ |
71
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
72
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"blob"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
73
+
74
+ ### Returns
75
+
76
+ [`FetchTask`](../interfaces/FetchTask.md)\<`Blob`\>
77
+
78
+ A `FetchTask` representing the operation with a `Blob` response.
79
+
80
+ ### Source
81
+
82
+ [src/fetch/fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L38)
83
+
84
+ ## fetchT(url, init)
85
+
86
+ ```ts
87
+ function fetchT<T>(url, init): FetchTask<T>
88
+ ```
89
+
90
+ Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.
91
+
92
+ ### Type parameters
93
+
94
+ | Type parameter | Description |
95
+ | :------ | :------ |
96
+ | `T` | The expected type of the parsed JSON data. |
97
+
98
+ ### Parameters
99
+
100
+ | Parameter | Type | Description |
101
+ | :------ | :------ | :------ |
102
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
103
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"json"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
104
+
105
+ ### Returns
106
+
107
+ [`FetchTask`](../interfaces/FetchTask.md)\<`T`\>
108
+
109
+ A `FetchTask` representing the operation with a response parsed as JSON.
110
+
111
+ ### Source
112
+
113
+ [src/fetch/fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L51)
114
+
115
+ ## fetchT(url, init)
116
+
117
+ ```ts
118
+ function fetchT(url, init): FetchResponse<string>
119
+ ```
120
+
121
+ Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.
122
+
123
+ ### Parameters
124
+
125
+ | Parameter | Type | Description |
126
+ | :------ | :------ | :------ |
127
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
128
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"text"`; \} | Additional options for the fetch operation, specifying the response type as 'text'. |
129
+
130
+ ### Returns
131
+
132
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`string`\>
133
+
134
+ A `FetchResponse` representing the operation with a `string` response.
135
+
136
+ ### Source
137
+
138
+ [src/fetch/fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L63)
139
+
140
+ ## fetchT(url, init)
141
+
142
+ ```ts
143
+ function fetchT(url, init): FetchResponse<ArrayBuffer>
144
+ ```
145
+
146
+ Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.
147
+
148
+ ### Parameters
149
+
150
+ | Parameter | Type | Description |
151
+ | :------ | :------ | :------ |
152
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
153
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"arraybuffer"`; \} | Additional options for the fetch operation, specifying the response type as 'arraybuffer'. |
154
+
155
+ ### Returns
156
+
157
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`ArrayBuffer`\>
158
+
159
+ A `FetchResponse` representing the operation with an `ArrayBuffer` response.
160
+
161
+ ### Source
162
+
163
+ [src/fetch/fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L74)
164
+
165
+ ## fetchT(url, init)
166
+
167
+ ```ts
168
+ function fetchT(url, init): FetchResponse<Blob>
169
+ ```
170
+
171
+ Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.
172
+
173
+ ### Parameters
174
+
175
+ | Parameter | Type | Description |
176
+ | :------ | :------ | :------ |
177
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
178
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"blob"`; \} | Additional options for the fetch operation, specifying the response type as 'blob'. |
179
+
180
+ ### Returns
181
+
182
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Blob`\>
183
+
184
+ A `FetchResponse` representing the operation with a `Blob` response.
185
+
186
+ ### Source
187
+
188
+ [src/fetch/fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L85)
189
+
190
+ ## fetchT(url, init)
191
+
192
+ ```ts
193
+ function fetchT<T>(url, init): FetchResponse<T>
194
+ ```
195
+
196
+ Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.
197
+
198
+ ### Type parameters
199
+
200
+ | Type parameter | Description |
201
+ | :------ | :------ |
202
+ | `T` | The expected type of the parsed JSON data. |
203
+
204
+ ### Parameters
205
+
206
+ | Parameter | Type | Description |
207
+ | :------ | :------ | :------ |
208
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
209
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"json"`; \} | Additional options for the fetch operation, specifying the response type as 'json'. |
210
+
211
+ ### Returns
212
+
213
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
214
+
215
+ A `FetchResponse` representing the operation with a response parsed as JSON.
216
+
217
+ ### Source
218
+
219
+ [src/fetch/fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L97)
220
+
221
+ ## fetchT(url, init)
222
+
223
+ ```ts
224
+ function fetchT(url, init): FetchTask<Response>
225
+ ```
226
+
227
+ Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.
228
+
229
+ ### Parameters
230
+
231
+ | Parameter | Type | Description |
232
+ | :------ | :------ | :------ |
233
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
234
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; \} | Additional options for the fetch operation, indicating that the operation should be abortable. |
235
+
236
+ ### Returns
237
+
238
+ [`FetchTask`](../interfaces/FetchTask.md)\<`Response`\>
239
+
240
+ A `FetchTask` representing the operation with a generic `Response`.
241
+
242
+ ### Source
243
+
244
+ [src/fetch/fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L108)
245
+
246
+ ## fetchT(url, init)
247
+
248
+ ```ts
249
+ function fetchT(url, init): FetchResponse<Response>
250
+ ```
251
+
252
+ Fetches a resource from the network and returns a `FetchResponse` representing the operation with a generic `Response`.
253
+
254
+ ### Parameters
255
+
256
+ | Parameter | Type | Description |
257
+ | :------ | :------ | :------ |
258
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
259
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `false`; \} | Additional options for the fetch operation, indicating that the operation should not be abortable. |
260
+
261
+ ### Returns
262
+
263
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
264
+
265
+ A `FetchResponse` representing the operation with a generic `Response`.
266
+
267
+ ### Source
268
+
269
+ [src/fetch/fetch.ts:119](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L119)
270
+
271
+ ## fetchT(url, init)
272
+
273
+ ```ts
274
+ function fetchT<T>(url, init): FetchTask<T> | FetchResponse<T>
275
+ ```
276
+
277
+ Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
278
+
279
+ ### Type parameters
280
+
281
+ | Type parameter | Description |
282
+ | :------ | :------ |
283
+ | `T` | The expected type of the response data when not using a specific `responseType`. |
284
+
285
+ ### Parameters
286
+
287
+ | Parameter | Type | Description |
288
+ | :------ | :------ | :------ |
289
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
290
+ | `init` | [`FetchInit`](../interfaces/FetchInit.md) | Additional options for the fetch operation, including custom `FetchInit` properties. |
291
+
292
+ ### Returns
293
+
294
+ [`FetchTask`](../interfaces/FetchTask.md)\<`T`\> \| [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`\>
295
+
296
+ A `FetchResponse` or `FetchTask` depending on the `abortable` option in `init`.
297
+
298
+ ### Source
299
+
300
+ [src/fetch/fetch.ts:131](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L131)
301
+
302
+ ## fetchT(url, init)
303
+
304
+ ```ts
305
+ function fetchT(url, init?): FetchResponse<Response>
306
+ ```
307
+
308
+ Fetches a resource from the network and returns a `FetchResponse` representing the operation.
309
+
310
+ ### Parameters
311
+
312
+ | Parameter | Type | Description |
313
+ | :------ | :------ | :------ |
314
+ | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
315
+ | `init`? | `RequestInit` | Standard `RequestInit` options for the fetch operation. |
316
+
317
+ ### Returns
318
+
319
+ [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
320
+
321
+ A `FetchResponse` representing the operation with a `Response` object.
322
+
323
+ ### Source
324
+
325
+ [src/fetch/fetch.ts:140](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/fetch.ts#L140)
package/docs/index.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,34 @@
1
+ [**@happy-ts/fetch-t**](../index.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../index.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 |
18
+ | :------ | :------ | :------ | :------ |
19
+ | `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | - |
20
+ | `body?` | `null` \| `BodyInit` | A BodyInit object or null to set request's body. | `RequestInit.body` |
21
+ | `cache?` | `RequestCache` | A string indicating how the request will interact with the browser's cache to set request's cache. | `RequestInit.cache` |
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` |
23
+ | `headers?` | `HeadersInit` | A Headers object, an object literal, or an array of two-item arrays to set request's headers. | `RequestInit.headers` |
24
+ | `integrity?` | `string` | A cryptographic hash of the resource to be fetched by request. Sets request's integrity. | `RequestInit.integrity` |
25
+ | `keepalive?` | `boolean` | A boolean to set request's keepalive. | `RequestInit.keepalive` |
26
+ | `method?` | `string` | A string to set request's method. | `RequestInit.method` |
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` |
28
+ | `priority?` | `RequestPriority` | - | `RequestInit.priority` |
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` |
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` |
31
+ | `referrerPolicy?` | `ReferrerPolicy` | A referrer policy to set request's referrerPolicy. | `RequestInit.referrerPolicy` |
32
+ | `responseType?` | `"text"` \| `"arraybuffer"` \| `"blob"` \| `"json"` | Specifies the expected response type of the fetch request. | - |
33
+ | `signal?` | `null` \| `AbortSignal` | An AbortSignal to set request's signal. | `RequestInit.signal` |
34
+ | `window?` | `null` | Can only be null. Used to disassociate request from any Window. | `RequestInit.window` |
@@ -0,0 +1,46 @@
1
+ [**@happy-ts/fetch-t**](../index.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../index.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 |
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`. |
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
+ #### Source
45
+
46
+ [src/fetch/defines.ts:22](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/defines.ts#L22)
@@ -0,0 +1,23 @@
1
+ [**@happy-ts/fetch-t**](../index.md) • **Docs**
2
+
3
+ ***
4
+
5
+ [@happy-ts/fetch-t](../index.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
+ ## Source
22
+
23
+ [src/fetch/defines.ts:9](https://github.com/JiangJie/fetch-t/blob/5ca54f90db4fac871c3148a812ceaa65b2b2c097/src/fetch/defines.ts#L9)
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@happy-ts/fetch-t",
3
- "description": "Cancelable fetch wrapper with the ability to specify the return type.",
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.7",
6
+ "version": "1.0.9",
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": {
@@ -33,21 +39,23 @@
33
39
  "keywords": [
34
40
  "fetch",
35
41
  "abort",
42
+ "cancel",
36
43
  "responseType"
37
44
  ],
38
45
  "devDependencies": {
39
- "@jest/globals": "^29.7.0",
40
- "@typescript-eslint/eslint-plugin": "^7.9.0",
41
- "@typescript-eslint/parser": "^7.9.0",
46
+ "@typescript-eslint/eslint-plugin": "^7.13.0",
47
+ "@typescript-eslint/parser": "^7.13.0",
42
48
  "eslint": "^8.57.0",
43
- "rimraf": "^5.0.7",
44
- "rollup": "^4.17.2",
45
- "rollup-plugin-dts": "^6.1.0",
49
+ "rollup": "^4.18.0",
50
+ "rollup-plugin-dts": "^6.1.1",
46
51
  "rollup-plugin-esbuild": "^6.1.1",
52
+ "typedoc": "^0.25.13",
53
+ "typedoc-plugin-markdown": "^4.0.3",
47
54
  "typescript": "^5.4.5"
48
55
  },
49
56
  "dependencies": {
50
- "happy-rusty": "^1.0.9"
57
+ "happy-rusty": "^1.1.0",
58
+ "tiny-invariant": "^1.3.3"
51
59
  },
52
- "packageManager": "pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195"
60
+ "packageManager": "pnpm@9.3.0"
53
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> {
107
- if (typeof url === 'string') {
108
- assertUrl(url);
150
+ export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {
151
+ // most cases
152
+ if (typeof url !== 'string') {
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 a string
14
- *
15
- * @param url
16
- */
17
- export function assertUrl(url: string): void {
18
- invariant(typeof url === 'string', () => `Url must be a string. Received ${ JSON.stringify(url) }`);
19
- }