@happy-ts/fetch-t 1.3.3 → 1.4.0

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/package.json CHANGED
@@ -1,35 +1,42 @@
1
1
  {
2
2
  "name": "@happy-ts/fetch-t",
3
- "description": "Abortable fetch wrapper with the ability to specify the return type.",
3
+ "description": "Type-safe Fetch API wrapper with abortable requests, timeout support, progress tracking, and Rust-like Result error handling.",
4
4
  "author": "jiang115jie@gmail.com",
5
- "license": "GPL-3.0",
6
- "version": "1.3.3",
5
+ "license": "MIT",
6
+ "version": "1.4.0",
7
7
  "type": "module",
8
8
  "source": "src/mod.ts",
9
9
  "main": "dist/main.cjs",
10
10
  "module": "dist/main.mjs",
11
11
  "types": "dist/types.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/types.d.ts",
15
+ "import": "./dist/main.mjs",
16
+ "require": "./dist/main.cjs",
17
+ "default": "./dist/main.mjs"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
12
21
  "files": [
13
22
  "LICENSE",
14
23
  "README.md",
15
24
  "README.cn.md",
16
- "package.json",
17
- "docs",
18
- "src",
25
+ "CHANGELOG.md",
19
26
  "dist"
20
27
  ],
21
28
  "sideEffects": false,
22
29
  "scripts": {
23
30
  "check": "pnpm exec tsc --noEmit",
24
31
  "lint": "pnpm exec eslint .",
25
- "prebuild": "pnpm dlx rimraf dist && pnpm run check && pnpm run lint",
26
- "build": "pnpm exec rollup --config rollup.config.mjs",
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",
32
+ "prebuild": "pnpm run check && pnpm run lint",
33
+ "build": "pnpm exec vite build",
34
+ "test": "pnpm exec vitest run --coverage",
35
+ "test:watch": "pnpm exec vitest",
36
+ "test:ui": "pnpm exec vitest --ui",
31
37
  "predocs": "pnpm dlx rimraf docs",
32
38
  "docs": "pnpm exec typedoc",
39
+ "eg": "node examples/main.ts",
33
40
  "prepublishOnly": "pnpm run build"
34
41
  },
35
42
  "repository": {
@@ -40,21 +47,29 @@
40
47
  "fetch",
41
48
  "abort",
42
49
  "cancel",
43
- "responseType"
50
+ "timeout",
51
+ "progress",
52
+ "streaming",
53
+ "type-safe",
54
+ "typescript",
55
+ "result",
56
+ "async"
44
57
  ],
45
58
  "devDependencies": {
46
- "@eslint/js": "^9.39.1",
47
- "eslint": "^9.39.1",
48
- "rollup": "^4.53.3",
49
- "rollup-plugin-dts": "^6.3.0",
50
- "rollup-plugin-esbuild": "^6.2.1",
51
- "typedoc": "^0.27.9",
52
- "typedoc-plugin-markdown": "^4.4.2",
59
+ "@eslint/js": "^9.39.2",
60
+ "@stylistic/eslint-plugin": "^5.6.1",
61
+ "@vitest/coverage-v8": "^4.0.16",
62
+ "eslint": "^9.39.2",
63
+ "msw": "^2.12.4",
64
+ "typedoc": "^0.28.15",
53
65
  "typescript": "^5.9.3",
54
- "typescript-eslint": "^8.48.0"
66
+ "typescript-eslint": "^8.50.0",
67
+ "vite": "^7.3.0",
68
+ "vite-plugin-dts": "^4.5.4",
69
+ "vitest": "^4.0.16"
55
70
  },
56
71
  "dependencies": {
57
- "happy-rusty": "^1.5.0",
72
+ "happy-rusty": "^1.6.1",
58
73
  "tiny-invariant": "^1.3.3"
59
74
  }
60
75
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sources":["../src/fetch/constants.ts","../src/fetch/defines.ts","../src/fetch/fetch.ts"],"sourcesContent":["/**\n * Name of abort error;\n */\nexport const ABORT_ERROR = 'AbortError' as const;\n\n/**\n * Name of timeout error;\n */\nexport const TIMEOUT_ERROR = 'TimeoutError' as const;","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { AsyncResult, IOResult } from 'happy-rusty';\n\n/**\n * Represents the response of a fetch operation, encapsulating the result data or any error that occurred.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport type FetchResponse<T, E = any> = AsyncResult<T, E>;\n\n/**\n * Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.\n *\n * @typeParam T - The type of the data expected in the response.\n */\nexport interface FetchTask<T> {\n /**\n * Aborts the fetch task, optionally with a reason for the abortion.\n *\n * @param reason - An optional parameter to indicate why the task was aborted.\n */\n abort(reason?: any): void;\n\n /**\n * Indicates whether the fetch task has been aborted.\n */\n readonly aborted: boolean;\n\n /**\n * The response of the fetch task, represented as an `AsyncResult`.\n */\n readonly response: FetchResponse<T>;\n}\n\n/**\n * Specifies the expected response type of the fetch request.\n */\nexport type FetchResponseType = 'text' | 'arraybuffer' | 'blob' | 'json';\n\n/**\n * Represents the progress of a fetch operation.\n */\nexport interface FetchProgress {\n /**\n * The total number of bytes to be received.\n */\n totalByteLength: number;\n\n /**\n * The number of bytes received so far.\n */\n completedByteLength: number;\n}\n\n/**\n * Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.\n */\nexport interface FetchInit extends RequestInit {\n /**\n * Indicates whether the fetch request should be abortable.\n */\n abortable?: boolean;\n\n /**\n * Specifies the expected response type of the fetch request.\n */\n responseType?: FetchResponseType;\n\n /**\n * Specifies the maximum time in milliseconds to wait for the fetch request to complete.\n */\n timeout?: number;\n\n /**\n * Specifies a function to be called when the fetch request makes progress.\n * @param progressResult - The progress of the fetch request.\n */\n onProgress?: (progressResult: IOResult<FetchProgress>) => void;\n\n /**\n * Specifies a function to be called when the fetch request receives a chunk of data.\n * @param chunk - The chunk of data received.\n */\n onChunk?: (chunk: Uint8Array) => void;\n}\n\n/**\n * Represents an error that occurred during a fetch operation when the response is not ok.\n */\nexport class FetchError extends Error {\n /**\n * The name of the error.\n */\n override name = 'FetchError';\n /**\n * The status code of the response.\n */\n status = 0;\n\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n }\n}","import { Err, Ok } from 'happy-rusty';\nimport invariant from 'tiny-invariant';\nimport { TIMEOUT_ERROR } from './constants.ts';\nimport { FetchError, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the response data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'text';\n}): FetchTask<string>;\n\n/**\n * Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'arraybuffer';\n}): FetchTask<ArrayBuffer>;\n\n/**\n * Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a `Blob` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'blob';\n}): FetchTask<Blob>;\n\n/**\n * Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.\n *\n * @typeParam T - The expected type of the parsed JSON data.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` representing the operation with a response parsed as JSON.\n */\nexport function fetchT<T>(url: string | URL, init: FetchInit & {\n abortable: true;\n responseType: 'json';\n}): FetchTask<T>;\n\n/**\n * Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, specifying the response type as 'text'.\n * @returns A `FetchResponse` representing the operation with a `string` response.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n responseType: 'text';\n}): FetchResponse<string, Error>;\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, Error>;\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, Error>;\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, Error>;\n\n/**\n * Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.\n *\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, indicating that the operation should be abortable.\n * @returns A `FetchTask` representing the operation with a generic `Response`.\n */\nexport function fetchT(url: string | URL, init: FetchInit & {\n abortable: true;\n}): FetchTask<Response>;\n\n/**\n * Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchResponse` representing the operation with a `Response` object.\n */\nexport function fetchT(url: string | URL, init?: FetchInit): FetchResponse<Response>;\n\n/**\n * Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.\n *\n * @typeParam T - The expected type of the response data when not using a specific `responseType`.\n * @param url - The resource to fetch. Can be a URL object or a string representing a URL.\n * @param init - Additional options for the fetch operation, including custom `FetchInit` properties.\n * @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.\n */\nexport function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {\n // most cases\n if (typeof url !== 'string') {\n invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);\n }\n\n const {\n // default not abort able\n abortable = false,\n responseType,\n timeout,\n onProgress,\n onChunk,\n ...rest\n } = init ?? {};\n\n const shouldWaitTimeout = timeout != null;\n let cancelTimer: (() => void) | null;\n\n if (shouldWaitTimeout) {\n invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);\n }\n\n let controller: AbortController;\n\n if (abortable || shouldWaitTimeout) {\n controller = new AbortController();\n rest.signal = controller.signal;\n }\n\n const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {\n cancelTimer?.();\n\n if (!res.ok) {\n await res.body?.cancel();\n return Err(new FetchError(res.statusText, res.status));\n }\n\n if (res.body) {\n // should notify progress or data chunk?\n const shouldNotifyProgress = typeof onProgress === 'function';\n const shouldNotifyChunk = typeof onChunk === 'function';\n\n if ((shouldNotifyProgress || shouldNotifyChunk)) {\n // tee the original stream to two streams, one for notify progress, another for response\n const [stream1, stream2] = res.body.tee();\n\n const reader = stream1.getReader();\n // may has no content-length\n let totalByteLength: number | null = null;\n let completedByteLength = 0;\n\n if (shouldNotifyProgress) {\n // try to get content-length\n // compatible with http/2\n const contentLength = res.headers.get('content-length') ?? res.headers.get('Content-Length');\n if (contentLength == null) {\n // response headers has no content-length\n onProgress(Err(new Error('No content-length in response headers.')));\n } else {\n totalByteLength = parseInt(contentLength, 10);\n }\n }\n\n reader.read().then(function notify({ done, value }) {\n if (done) {\n return;\n }\n\n // notify chunk\n if (shouldNotifyChunk) {\n onChunk(value);\n }\n\n // notify progress\n if (shouldNotifyProgress && totalByteLength != null) {\n completedByteLength += value.byteLength;\n onProgress(Ok({\n totalByteLength,\n completedByteLength,\n }));\n }\n\n\n // continue to read\n reader.read().then(notify);\n });\n\n // replace the original response with the new one\n res = new Response(stream2, {\n headers: res.headers,\n status: res.status,\n statusText: res.statusText,\n });\n }\n }\n\n switch (responseType) {\n case 'arraybuffer': {\n return Ok(await res.arrayBuffer() as T);\n }\n case 'blob': {\n return Ok(await res.blob() as T);\n }\n case 'json': {\n try {\n return Ok(await res.json() as T);\n } catch {\n return Err(new Error('Response is invalid json while responseType is json'));\n }\n }\n case 'text': {\n return Ok(await res.text() as T);\n }\n default: {\n // default return the Response object\n return Ok(res as T);\n }\n }\n }).catch((err) => {\n cancelTimer?.();\n\n return Err(err);\n });\n\n if (shouldWaitTimeout) {\n const timer = setTimeout(() => {\n if (!controller.signal.aborted) {\n const error = new Error();\n error.name = TIMEOUT_ERROR;\n controller.abort(error);\n }\n }, timeout);\n\n cancelTimer = (): void => {\n if (timer) {\n clearTimeout(timer);\n }\n\n cancelTimer = null;\n };\n }\n\n if (abortable) {\n return {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n abort(reason?: any): void {\n cancelTimer?.();\n controller.abort(reason);\n },\n\n get aborted(): boolean {\n return controller.signal.aborted;\n },\n\n get response(): FetchResponse<T> {\n return response;\n },\n };\n } else {\n return response;\n }\n}"],"names":[],"mappings":";;AAAA;AACA;AACA;AACO,cAAA,WAAA;AACP;AACA;AACA;AACO,cAAA,aAAA;;ACNP;AACA;AACA;AACA;AACA;AACO,KAAA,aAAA,eAAA,WAAA;AACP;AACA;AACA;AACA;AACA;AACO,UAAA,SAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uBAAA,aAAA;AACA;AACA;AACA;AACA;AACO,KAAA,iBAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,UAAA,SAAA,SAAA,WAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA,mBAAA,iBAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,kCAAA,QAAA,CAAA,aAAA;AACA;AACA;AACA;AACA;AACA,sBAAA,UAAA;AACA;AACA;AACA;AACA;AACO,cAAA,UAAA,SAAA,KAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;ACpFA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA;AACA,IAAA,SAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA;AACA,IAAA,SAAA,CAAA,WAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA;AACA,IAAA,SAAA,CAAA,IAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,kBAAA,GAAA,QAAA,SAAA;AACP;AACA;AACA,IAAA,SAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA,IAAA,aAAA,SAAA,KAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA,IAAA,aAAA,CAAA,WAAA,EAAA,KAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA,IAAA,aAAA,CAAA,IAAA,EAAA,KAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,kBAAA,GAAA,QAAA,SAAA;AACP;AACA,IAAA,aAAA,IAAA,KAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,QAAA,SAAA;AACP;AACA,IAAA,SAAA,CAAA,QAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,eAAA,GAAA,SAAA,SAAA,GAAA,aAAA,CAAA,QAAA;;;;"}
package/docs/README.md DELETED
@@ -1,39 +0,0 @@
1
- **@happy-ts/fetch-t**
2
-
3
- ***
4
-
5
- # @happy-ts/fetch-t
6
-
7
- ## Classes
8
-
9
- | Class | Description |
10
- | ------ | ------ |
11
- | [FetchError](classes/FetchError.md) | Represents an error that occurred during a fetch operation when the response is not ok. |
12
-
13
- ## Interfaces
14
-
15
- | Interface | Description |
16
- | ------ | ------ |
17
- | [FetchInit](interfaces/FetchInit.md) | Extends the standard `RequestInit` interface from the Fetch API to include additional custom options. |
18
- | [FetchProgress](interfaces/FetchProgress.md) | Represents the progress of a fetch operation. |
19
- | [FetchTask](interfaces/FetchTask.md) | Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status. |
20
-
21
- ## Type Aliases
22
-
23
- | Type alias | Description |
24
- | ------ | ------ |
25
- | [FetchResponse](type-aliases/FetchResponse.md) | Represents the response of a fetch operation, encapsulating the result data or any error that occurred. |
26
- | [FetchResponseType](type-aliases/FetchResponseType.md) | Specifies the expected response type of the fetch request. |
27
-
28
- ## Variables
29
-
30
- | Variable | Description |
31
- | ------ | ------ |
32
- | [ABORT\_ERROR](variables/ABORT_ERROR.md) | Name of abort error; |
33
- | [TIMEOUT\_ERROR](variables/TIMEOUT_ERROR.md) | Name of timeout error; |
34
-
35
- ## Functions
36
-
37
- | Function | Description |
38
- | ------ | ------ |
39
- | [fetchT](functions/fetchT.md) | Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options. |
@@ -1,49 +0,0 @@
1
- [**@happy-ts/fetch-t**](../README.md)
2
-
3
- ***
4
-
5
- [@happy-ts/fetch-t](../README.md) / FetchError
6
-
7
- # Class: FetchError
8
-
9
- Defined in: [defines.ts:90](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L90)
10
-
11
- Represents an error that occurred during a fetch operation when the response is not ok.
12
-
13
- ## Extends
14
-
15
- - `Error`
16
-
17
- ## Constructors
18
-
19
- ### new FetchError()
20
-
21
- ```ts
22
- new FetchError(message, status): FetchError
23
- ```
24
-
25
- Defined in: [defines.ts:100](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L100)
26
-
27
- #### Parameters
28
-
29
- | Parameter | Type |
30
- | ------ | ------ |
31
- | `message` | `string` |
32
- | `status` | `number` |
33
-
34
- #### Returns
35
-
36
- [`FetchError`](FetchError.md)
37
-
38
- #### Overrides
39
-
40
- ```ts
41
- Error.constructor
42
- ```
43
-
44
- ## Properties
45
-
46
- | Property | Type | Default value | Description | Overrides | Defined in |
47
- | ------ | ------ | ------ | ------ | ------ | ------ |
48
- | <a id="name"></a> `name` | `string` | `'FetchError'` | The name of the error. | `Error.name` | [defines.ts:94](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L94) |
49
- | <a id="status-1"></a> `status` | `number` | `0` | The status code of the response. | - | [defines.ts:98](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L98) |
@@ -1,403 +0,0 @@
1
- [**@happy-ts/fetch-t**](../README.md)
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
- ## Call Signature
24
-
25
- ```ts
26
- function fetchT(url, init): FetchTask<string>
27
- ```
28
-
29
- Defined in: [fetch.ts:14](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L14)
30
-
31
- Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
32
-
33
- ### Parameters
34
-
35
- | Parameter | Type | Description |
36
- | ------ | ------ | ------ |
37
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
38
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"text"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
39
-
40
- ### Returns
41
-
42
- [`FetchTask`](../interfaces/FetchTask.md)\<`string`\>
43
-
44
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
45
-
46
- A `FetchTask` representing the operation with a `string` response.
47
-
48
- ### Type Param
49
-
50
- The expected type of the response data when not using a specific `responseType`.
51
-
52
- ### Param
53
-
54
- The resource to fetch. Can be a URL object or a string representing a URL.
55
-
56
- ### Param
57
-
58
- Additional options for the fetch operation, including custom `FetchInit` properties.
59
-
60
- ## Call Signature
61
-
62
- ```ts
63
- function fetchT(url, init): FetchTask<ArrayBuffer>
64
- ```
65
-
66
- Defined in: [fetch.ts:26](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L26)
67
-
68
- Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.
69
-
70
- ### Parameters
71
-
72
- | Parameter | Type | Description |
73
- | ------ | ------ | ------ |
74
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
75
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"arraybuffer"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
76
-
77
- ### Returns
78
-
79
- [`FetchTask`](../interfaces/FetchTask.md)\<`ArrayBuffer`\>
80
-
81
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
82
-
83
- A `FetchTask` representing the operation with an `ArrayBuffer` response.
84
-
85
- ### Type Param
86
-
87
- The expected type of the response data when not using a specific `responseType`.
88
-
89
- ### Param
90
-
91
- The resource to fetch. Can be a URL object or a string representing a URL.
92
-
93
- ### Param
94
-
95
- Additional options for the fetch operation, including custom `FetchInit` properties.
96
-
97
- ## Call Signature
98
-
99
- ```ts
100
- function fetchT(url, init): FetchTask<Blob>
101
- ```
102
-
103
- Defined in: [fetch.ts:38](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L38)
104
-
105
- Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.
106
-
107
- ### Parameters
108
-
109
- | Parameter | Type | Description |
110
- | ------ | ------ | ------ |
111
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
112
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"blob"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
113
-
114
- ### Returns
115
-
116
- [`FetchTask`](../interfaces/FetchTask.md)\<`Blob`\>
117
-
118
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
119
-
120
- A `FetchTask` representing the operation with a `Blob` response.
121
-
122
- ### Type Param
123
-
124
- The expected type of the response data when not using a specific `responseType`.
125
-
126
- ### Param
127
-
128
- The resource to fetch. Can be a URL object or a string representing a URL.
129
-
130
- ### Param
131
-
132
- Additional options for the fetch operation, including custom `FetchInit` properties.
133
-
134
- ## Call Signature
135
-
136
- ```ts
137
- function fetchT<T>(url, init): FetchTask<T>
138
- ```
139
-
140
- Defined in: [fetch.ts:51](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L51)
141
-
142
- Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.
143
-
144
- ### Type Parameters
145
-
146
- | Type Parameter | Description |
147
- | ------ | ------ |
148
- | `T` | The expected type of the parsed JSON data. |
149
-
150
- ### Parameters
151
-
152
- | Parameter | Type | Description |
153
- | ------ | ------ | ------ |
154
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
155
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; `responseType`: `"json"`; \} | Additional options for the fetch operation, including custom `FetchInit` properties. |
156
-
157
- ### Returns
158
-
159
- [`FetchTask`](../interfaces/FetchTask.md)\<`T`\>
160
-
161
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
162
-
163
- A `FetchTask` representing the operation with a response parsed as JSON.
164
-
165
- ### Type Param
166
-
167
- The expected type of the response data when not using a specific `responseType`.
168
-
169
- ### Param
170
-
171
- The resource to fetch. Can be a URL object or a string representing a URL.
172
-
173
- ### Param
174
-
175
- Additional options for the fetch operation, including custom `FetchInit` properties.
176
-
177
- ## Call Signature
178
-
179
- ```ts
180
- function fetchT(url, init): FetchResponse<string, Error>
181
- ```
182
-
183
- Defined in: [fetch.ts:63](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L63)
184
-
185
- Fetches a resource from the network as a text string 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`: `"text"`; \} | Additional options for the fetch operation, specifying the response type as 'text'. |
193
-
194
- ### Returns
195
-
196
- [`FetchResponse`](../type-aliases/FetchResponse.md)\<`string`, `Error`\>
197
-
198
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
199
-
200
- A `FetchResponse` representing the operation with a `string` response.
201
-
202
- ### Type Param
203
-
204
- The expected type of the response data when not using a specific `responseType`.
205
-
206
- ### Param
207
-
208
- The resource to fetch. Can be a URL object or a string representing a URL.
209
-
210
- ### Param
211
-
212
- Additional options for the fetch operation, including custom `FetchInit` properties.
213
-
214
- ## Call Signature
215
-
216
- ```ts
217
- function fetchT(url, init): FetchResponse<ArrayBuffer, Error>
218
- ```
219
-
220
- Defined in: [fetch.ts:74](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L74)
221
-
222
- Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.
223
-
224
- ### Parameters
225
-
226
- | Parameter | Type | Description |
227
- | ------ | ------ | ------ |
228
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
229
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"arraybuffer"`; \} | Additional options for the fetch operation, specifying the response type as 'arraybuffer'. |
230
-
231
- ### Returns
232
-
233
- [`FetchResponse`](../type-aliases/FetchResponse.md)\<`ArrayBuffer`, `Error`\>
234
-
235
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
236
-
237
- A `FetchResponse` representing the operation with an `ArrayBuffer` response.
238
-
239
- ### Type Param
240
-
241
- The expected type of the response data when not using a specific `responseType`.
242
-
243
- ### Param
244
-
245
- The resource to fetch. Can be a URL object or a string representing a URL.
246
-
247
- ### Param
248
-
249
- Additional options for the fetch operation, including custom `FetchInit` properties.
250
-
251
- ## Call Signature
252
-
253
- ```ts
254
- function fetchT(url, init): FetchResponse<Blob, Error>
255
- ```
256
-
257
- Defined in: [fetch.ts:85](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L85)
258
-
259
- Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.
260
-
261
- ### Parameters
262
-
263
- | Parameter | Type | Description |
264
- | ------ | ------ | ------ |
265
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
266
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"blob"`; \} | Additional options for the fetch operation, specifying the response type as 'blob'. |
267
-
268
- ### Returns
269
-
270
- [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Blob`, `Error`\>
271
-
272
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
273
-
274
- A `FetchResponse` representing the operation with a `Blob` response.
275
-
276
- ### Type Param
277
-
278
- The expected type of the response data when not using a specific `responseType`.
279
-
280
- ### Param
281
-
282
- The resource to fetch. Can be a URL object or a string representing a URL.
283
-
284
- ### Param
285
-
286
- Additional options for the fetch operation, including custom `FetchInit` properties.
287
-
288
- ## Call Signature
289
-
290
- ```ts
291
- function fetchT<T>(url, init): FetchResponse<T, Error>
292
- ```
293
-
294
- Defined in: [fetch.ts:97](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L97)
295
-
296
- Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.
297
-
298
- ### Type Parameters
299
-
300
- | Type Parameter | Description |
301
- | ------ | ------ |
302
- | `T` | The expected type of the parsed JSON data. |
303
-
304
- ### Parameters
305
-
306
- | Parameter | Type | Description |
307
- | ------ | ------ | ------ |
308
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
309
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `responseType`: `"json"`; \} | Additional options for the fetch operation, specifying the response type as 'json'. |
310
-
311
- ### Returns
312
-
313
- [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`, `Error`\>
314
-
315
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
316
-
317
- A `FetchResponse` representing the operation with a response parsed as JSON.
318
-
319
- ### Type Param
320
-
321
- The expected type of the response data when not using a specific `responseType`.
322
-
323
- ### Param
324
-
325
- The resource to fetch. Can be a URL object or a string representing a URL.
326
-
327
- ### Param
328
-
329
- Additional options for the fetch operation, including custom `FetchInit` properties.
330
-
331
- ## Call Signature
332
-
333
- ```ts
334
- function fetchT(url, init): FetchTask<Response>
335
- ```
336
-
337
- Defined in: [fetch.ts:108](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L108)
338
-
339
- Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.
340
-
341
- ### Parameters
342
-
343
- | Parameter | Type | Description |
344
- | ------ | ------ | ------ |
345
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
346
- | `init` | [`FetchInit`](../interfaces/FetchInit.md) & \{ `abortable`: `true`; \} | Additional options for the fetch operation, indicating that the operation should be abortable. |
347
-
348
- ### Returns
349
-
350
- [`FetchTask`](../interfaces/FetchTask.md)\<`Response`\>
351
-
352
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
353
-
354
- A `FetchTask` representing the operation with a generic `Response`.
355
-
356
- ### Type Param
357
-
358
- The expected type of the response data when not using a specific `responseType`.
359
-
360
- ### Param
361
-
362
- The resource to fetch. Can be a URL object or a string representing a URL.
363
-
364
- ### Param
365
-
366
- Additional options for the fetch operation, including custom `FetchInit` properties.
367
-
368
- ## Call Signature
369
-
370
- ```ts
371
- function fetchT(url, init?): FetchResponse<Response>
372
- ```
373
-
374
- Defined in: [fetch.ts:120](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/fetch.ts#L120)
375
-
376
- Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
377
-
378
- ### Parameters
379
-
380
- | Parameter | Type | Description |
381
- | ------ | ------ | ------ |
382
- | `url` | `string` \| `URL` | The resource to fetch. Can be a URL object or a string representing a URL. |
383
- | `init`? | [`FetchInit`](../interfaces/FetchInit.md) | Additional options for the fetch operation, including custom `FetchInit` properties. |
384
-
385
- ### Returns
386
-
387
- [`FetchResponse`](../type-aliases/FetchResponse.md)\<`Response`\>
388
-
389
- A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
390
-
391
- A `FetchResponse` representing the operation with a `Response` object.
392
-
393
- ### Type Param
394
-
395
- The expected type of the response data when not using a specific `responseType`.
396
-
397
- ### Param
398
-
399
- The resource to fetch. Can be a URL object or a string representing a URL.
400
-
401
- ### Param
402
-
403
- Additional options for the fetch operation, including custom `FetchInit` properties.
@@ -1,25 +0,0 @@
1
- [**@happy-ts/fetch-t**](../README.md)
2
-
3
- ***
4
-
5
- [@happy-ts/fetch-t](../README.md) / FetchInit
6
-
7
- # Interface: FetchInit
8
-
9
- Defined in: [defines.ts:58](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L58)
10
-
11
- Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
12
-
13
- ## Extends
14
-
15
- - `RequestInit`
16
-
17
- ## Properties
18
-
19
- | Property | Type | Description | Defined in |
20
- | ------ | ------ | ------ | ------ |
21
- | <a id="abortable"></a> `abortable?` | `boolean` | Indicates whether the fetch request should be abortable. | [defines.ts:62](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L62) |
22
- | <a id="onchunk"></a> `onChunk?` | (`chunk`: `Uint8Array`) => `void` | Specifies a function to be called when the fetch request receives a chunk of data. | [defines.ts:84](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L84) |
23
- | <a id="onprogress"></a> `onProgress?` | (`progressResult`: `IOResult`\<[`FetchProgress`](FetchProgress.md)\>) => `void` | Specifies a function to be called when the fetch request makes progress. | [defines.ts:78](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L78) |
24
- | <a id="responsetype"></a> `responseType?` | [`FetchResponseType`](../type-aliases/FetchResponseType.md) | Specifies the expected response type of the fetch request. | [defines.ts:67](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L67) |
25
- | <a id="timeout"></a> `timeout?` | `number` | Specifies the maximum time in milliseconds to wait for the fetch request to complete. | [defines.ts:72](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L72) |
@@ -1,18 +0,0 @@
1
- [**@happy-ts/fetch-t**](../README.md)
2
-
3
- ***
4
-
5
- [@happy-ts/fetch-t](../README.md) / FetchProgress
6
-
7
- # Interface: FetchProgress
8
-
9
- Defined in: [defines.ts:43](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L43)
10
-
11
- Represents the progress of a fetch operation.
12
-
13
- ## Properties
14
-
15
- | Property | Type | Description | Defined in |
16
- | ------ | ------ | ------ | ------ |
17
- | <a id="completedbytelength"></a> `completedByteLength` | `number` | The number of bytes received so far. | [defines.ts:52](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L52) |
18
- | <a id="totalbytelength"></a> `totalByteLength` | `number` | The total number of bytes to be received. | [defines.ts:47](https://github.com/JiangJie/fetch-t/blob/bef789cb418392a07597af77b2942bea27482d3e/src/fetch/defines.ts#L47) |