@happy-ts/fetch-t 1.3.2 → 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/CHANGELOG.md +120 -0
- package/LICENSE +21 -674
- package/README.cn.md +138 -46
- package/README.md +136 -49
- package/dist/main.cjs +19 -16
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +15 -14
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +432 -206
- package/package.json +39 -26
- package/dist/types.d.ts.map +0 -1
- package/docs/README.md +0 -39
- package/docs/classes/FetchError.md +0 -47
- package/docs/functions/fetchT.md +0 -423
- package/docs/interfaces/FetchInit.md +0 -23
- package/docs/interfaces/FetchProgress.md +0 -16
- package/docs/interfaces/FetchTask.md +0 -46
- package/docs/type-aliases/FetchResponse.md +0 -24
- package/docs/type-aliases/FetchResponseType.md +0 -17
- package/docs/variables/ABORT_ERROR.md +0 -17
- package/docs/variables/TIMEOUT_ERROR.md +0 -17
- package/src/fetch/constants.ts +0 -9
- package/src/fetch/defines.ts +0 -104
- package/src/fetch/fetch.ts +0 -292
- package/src/mod.ts +0 -3
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@happy-ts/fetch-t](../README.md) / FetchTask
|
|
6
|
-
|
|
7
|
-
# Interface: FetchTask\<T\>
|
|
8
|
-
|
|
9
|
-
Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.
|
|
10
|
-
|
|
11
|
-
## Type Parameters
|
|
12
|
-
|
|
13
|
-
| Type Parameter | Description |
|
|
14
|
-
| ------ | ------ |
|
|
15
|
-
| `T` | The type of the data expected in the response. |
|
|
16
|
-
|
|
17
|
-
## Properties
|
|
18
|
-
|
|
19
|
-
| Property | Modifier | Type | Description | Defined in |
|
|
20
|
-
| ------ | ------ | ------ | ------ | ------ |
|
|
21
|
-
| `aborted` | `readonly` | `boolean` | Indicates whether the fetch task has been aborted. | [defines.ts:27](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/defines.ts#L27) |
|
|
22
|
-
| `response` | `readonly` | [`FetchResponse`](../type-aliases/FetchResponse.md)\<`T`, `any`\> | The response of the fetch task, represented as an `AsyncResult`. | [defines.ts:32](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/defines.ts#L32) |
|
|
23
|
-
|
|
24
|
-
## Methods
|
|
25
|
-
|
|
26
|
-
### abort()
|
|
27
|
-
|
|
28
|
-
```ts
|
|
29
|
-
abort(reason?): void
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
Aborts the fetch task, optionally with a reason for the abortion.
|
|
33
|
-
|
|
34
|
-
#### Parameters
|
|
35
|
-
|
|
36
|
-
| Parameter | Type | Description |
|
|
37
|
-
| ------ | ------ | ------ |
|
|
38
|
-
| `reason`? | `any` | An optional parameter to indicate why the task was aborted. |
|
|
39
|
-
|
|
40
|
-
#### Returns
|
|
41
|
-
|
|
42
|
-
`void`
|
|
43
|
-
|
|
44
|
-
#### Defined in
|
|
45
|
-
|
|
46
|
-
[defines.ts:22](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/defines.ts#L22)
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@happy-ts/fetch-t](../README.md) / FetchResponse
|
|
6
|
-
|
|
7
|
-
# Type Alias: FetchResponse\<T, E\>
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
type FetchResponse<T, E>: AsyncResult<T, E>;
|
|
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 | Default type | Description |
|
|
18
|
-
| ------ | ------ | ------ |
|
|
19
|
-
| `T` | - | The type of the data expected in the response. |
|
|
20
|
-
| `E` | `any` | - |
|
|
21
|
-
|
|
22
|
-
## Defined in
|
|
23
|
-
|
|
24
|
-
[defines.ts:9](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/defines.ts#L9)
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@happy-ts/fetch-t](../README.md) / FetchResponseType
|
|
6
|
-
|
|
7
|
-
# Type Alias: FetchResponseType
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
type FetchResponseType: "text" | "arraybuffer" | "blob" | "json";
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Specifies the expected response type of the fetch request.
|
|
14
|
-
|
|
15
|
-
## Defined in
|
|
16
|
-
|
|
17
|
-
[defines.ts:38](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/defines.ts#L38)
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@happy-ts/fetch-t](../README.md) / ABORT\_ERROR
|
|
6
|
-
|
|
7
|
-
# Variable: ABORT\_ERROR
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
const ABORT_ERROR: "AbortError";
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Name of abort error;
|
|
14
|
-
|
|
15
|
-
## Defined in
|
|
16
|
-
|
|
17
|
-
[constants.ts:4](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/constants.ts#L4)
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
[**@happy-ts/fetch-t**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@happy-ts/fetch-t](../README.md) / TIMEOUT\_ERROR
|
|
6
|
-
|
|
7
|
-
# Variable: TIMEOUT\_ERROR
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
const TIMEOUT_ERROR: "TimeoutError";
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Name of timeout error;
|
|
14
|
-
|
|
15
|
-
## Defined in
|
|
16
|
-
|
|
17
|
-
[constants.ts:9](https://github.com/JiangJie/fetch-t/blob/61346c95bab5342bcbd9e97bca747ef24af2eac6/src/fetch/constants.ts#L9)
|
package/src/fetch/constants.ts
DELETED
package/src/fetch/defines.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import type { AsyncResult, IOResult } from 'happy-rusty';
|
|
3
|
-
|
|
4
|
-
/**
|
|
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.
|
|
8
|
-
*/
|
|
9
|
-
export type FetchResponse<T, E = any> = AsyncResult<T, E>;
|
|
10
|
-
|
|
11
|
-
/**
|
|
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.
|
|
15
|
-
*/
|
|
16
|
-
export interface FetchTask<T> {
|
|
17
|
-
/**
|
|
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.
|
|
21
|
-
*/
|
|
22
|
-
abort(reason?: any): void;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Indicates whether the fetch task has been aborted.
|
|
26
|
-
*/
|
|
27
|
-
readonly aborted: boolean;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The response of the fetch task, represented as an `AsyncResult`.
|
|
31
|
-
*/
|
|
32
|
-
readonly response: FetchResponse<T>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Specifies the expected response type of the fetch request.
|
|
37
|
-
*/
|
|
38
|
-
export type FetchResponseType = 'text' | 'arraybuffer' | 'blob' | 'json';
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Represents the progress of a fetch operation.
|
|
42
|
-
*/
|
|
43
|
-
export interface FetchProgress {
|
|
44
|
-
/**
|
|
45
|
-
* The total number of bytes to be received.
|
|
46
|
-
*/
|
|
47
|
-
totalByteLength: number;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* The number of bytes received so far.
|
|
51
|
-
*/
|
|
52
|
-
completedByteLength: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
|
|
57
|
-
*/
|
|
58
|
-
export interface FetchInit extends RequestInit {
|
|
59
|
-
/**
|
|
60
|
-
* Indicates whether the fetch request should be abortable.
|
|
61
|
-
*/
|
|
62
|
-
abortable?: boolean;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Specifies the expected response type of the fetch request.
|
|
66
|
-
*/
|
|
67
|
-
responseType?: FetchResponseType;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Specifies the maximum time in milliseconds to wait for the fetch request to complete.
|
|
71
|
-
*/
|
|
72
|
-
timeout?: number;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Specifies a function to be called when the fetch request makes progress.
|
|
76
|
-
* @param progressResult - The progress of the fetch request.
|
|
77
|
-
*/
|
|
78
|
-
onProgress?: (progressResult: IOResult<FetchProgress>) => void;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Specifies a function to be called when the fetch request receives a chunk of data.
|
|
82
|
-
* @param chunk - The chunk of data received.
|
|
83
|
-
*/
|
|
84
|
-
onChunk?: (chunk: Uint8Array) => void;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Represents an error that occurred during a fetch operation when the response is not ok.
|
|
89
|
-
*/
|
|
90
|
-
export class FetchError extends Error {
|
|
91
|
-
/**
|
|
92
|
-
* The name of the error.
|
|
93
|
-
*/
|
|
94
|
-
name = 'FetchError';
|
|
95
|
-
/**
|
|
96
|
-
* The status code of the response.
|
|
97
|
-
*/
|
|
98
|
-
status = 0;
|
|
99
|
-
|
|
100
|
-
constructor(message: string, status: number) {
|
|
101
|
-
super(message);
|
|
102
|
-
this.status = status;
|
|
103
|
-
}
|
|
104
|
-
}
|
package/src/fetch/fetch.ts
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
import { Err, Ok } from 'happy-rusty';
|
|
2
|
-
import invariant from 'tiny-invariant';
|
|
3
|
-
import { TIMEOUT_ERROR } from './constants.ts';
|
|
4
|
-
import { FetchError, type FetchInit, type FetchResponse, type FetchTask } from './defines.ts';
|
|
5
|
-
|
|
6
|
-
/**
|
|
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.
|
|
13
|
-
*/
|
|
14
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
15
|
-
abortable: true;
|
|
16
|
-
responseType: 'text';
|
|
17
|
-
}): FetchTask<string>;
|
|
18
|
-
|
|
19
|
-
/**
|
|
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.
|
|
25
|
-
*/
|
|
26
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
27
|
-
abortable: true;
|
|
28
|
-
responseType: 'arraybuffer';
|
|
29
|
-
}): FetchTask<ArrayBuffer>;
|
|
30
|
-
|
|
31
|
-
/**
|
|
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.
|
|
37
|
-
*/
|
|
38
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
39
|
-
abortable: true;
|
|
40
|
-
responseType: 'blob';
|
|
41
|
-
}): FetchTask<Blob>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
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.
|
|
50
|
-
*/
|
|
51
|
-
export function fetchT<T>(url: string | URL, init: FetchInit & {
|
|
52
|
-
abortable: true;
|
|
53
|
-
responseType: 'json';
|
|
54
|
-
}): FetchTask<T>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
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.
|
|
62
|
-
*/
|
|
63
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
64
|
-
responseType: 'text';
|
|
65
|
-
}): FetchResponse<string, Error>;
|
|
66
|
-
|
|
67
|
-
/**
|
|
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.
|
|
73
|
-
*/
|
|
74
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
75
|
-
responseType: 'arraybuffer';
|
|
76
|
-
}): FetchResponse<ArrayBuffer, Error>;
|
|
77
|
-
|
|
78
|
-
/**
|
|
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.
|
|
84
|
-
*/
|
|
85
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
86
|
-
responseType: 'blob';
|
|
87
|
-
}): FetchResponse<Blob, Error>;
|
|
88
|
-
|
|
89
|
-
/**
|
|
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.
|
|
96
|
-
*/
|
|
97
|
-
export function fetchT<T>(url: string | URL, init: FetchInit & {
|
|
98
|
-
responseType: 'json';
|
|
99
|
-
}): FetchResponse<T, Error>;
|
|
100
|
-
|
|
101
|
-
/**
|
|
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`.
|
|
107
|
-
*/
|
|
108
|
-
export function fetchT(url: string | URL, init: FetchInit & {
|
|
109
|
-
abortable: true;
|
|
110
|
-
}): FetchTask<Response>;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
|
|
114
|
-
*
|
|
115
|
-
* @typeParam T - The expected type of the response data when not using a specific `responseType`.
|
|
116
|
-
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
117
|
-
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
118
|
-
* @returns A `FetchResponse` representing the operation with a `Response` object.
|
|
119
|
-
*/
|
|
120
|
-
export function fetchT(url: string | URL, init?: FetchInit): FetchResponse<Response>;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Fetches a resource from the network and returns either a `FetchTask` or `FetchResponse` based on the provided options.
|
|
124
|
-
*
|
|
125
|
-
* @typeParam T - The expected type of the response data when not using a specific `responseType`.
|
|
126
|
-
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
127
|
-
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
|
|
128
|
-
* @returns A `FetchTask` or `FetchResponse` depending on the provided options in `init`.
|
|
129
|
-
*/
|
|
130
|
-
export function fetchT<T>(url: string | URL, init?: FetchInit): FetchTask<T> | FetchResponse<T> {
|
|
131
|
-
// most cases
|
|
132
|
-
if (typeof url !== 'string') {
|
|
133
|
-
invariant(url instanceof URL, () => `Url must be a string or URL object but received ${ url }.`);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const {
|
|
137
|
-
// default not abort able
|
|
138
|
-
abortable = false,
|
|
139
|
-
responseType,
|
|
140
|
-
timeout,
|
|
141
|
-
onProgress,
|
|
142
|
-
onChunk,
|
|
143
|
-
...rest
|
|
144
|
-
} = init ?? {};
|
|
145
|
-
|
|
146
|
-
const shouldWaitTimeout = timeout != null;
|
|
147
|
-
let cancelTimer: (() => void) | null;
|
|
148
|
-
|
|
149
|
-
if (shouldWaitTimeout) {
|
|
150
|
-
invariant(typeof timeout === 'number' && timeout > 0, () => `Timeout must be a number greater than 0 but received ${ timeout }.`);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let controller: AbortController;
|
|
154
|
-
|
|
155
|
-
if (abortable || shouldWaitTimeout) {
|
|
156
|
-
controller = new AbortController();
|
|
157
|
-
rest.signal = controller.signal;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const response: FetchResponse<T> = fetch(url, rest).then(async (res): FetchResponse<T> => {
|
|
161
|
-
cancelTimer?.();
|
|
162
|
-
|
|
163
|
-
if (!res.ok) {
|
|
164
|
-
await res.body?.cancel();
|
|
165
|
-
return Err(new FetchError(res.statusText, res.status));
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (res.body) {
|
|
169
|
-
// should notify progress or data chunk?
|
|
170
|
-
const shouldNotifyProgress = typeof onProgress === 'function';
|
|
171
|
-
const shouldNotifyChunk = typeof onChunk === 'function';
|
|
172
|
-
|
|
173
|
-
if ((shouldNotifyProgress || shouldNotifyChunk)) {
|
|
174
|
-
// tee the original stream to two streams, one for notify progress, another for response
|
|
175
|
-
const [stream1, stream2] = res.body.tee();
|
|
176
|
-
|
|
177
|
-
const reader = stream1.getReader();
|
|
178
|
-
// may has no content-length
|
|
179
|
-
let totalByteLength: number | null = null;
|
|
180
|
-
let completedByteLength = 0;
|
|
181
|
-
|
|
182
|
-
if (shouldNotifyProgress) {
|
|
183
|
-
// try to get content-length
|
|
184
|
-
// compatible with http/2
|
|
185
|
-
const contentLength = res.headers.get('content-length') ?? res.headers.get('Content-Length');
|
|
186
|
-
if (contentLength == null) {
|
|
187
|
-
// response headers has no content-length
|
|
188
|
-
onProgress(Err(new Error('No content-length in response headers.')));
|
|
189
|
-
} else {
|
|
190
|
-
totalByteLength = parseInt(contentLength, 10);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
reader.read().then(function notify({ done, value }) {
|
|
195
|
-
if (done) {
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// notify chunk
|
|
200
|
-
if (shouldNotifyChunk) {
|
|
201
|
-
onChunk(value);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// notify progress
|
|
205
|
-
if (shouldNotifyProgress && totalByteLength != null) {
|
|
206
|
-
completedByteLength += value.byteLength;
|
|
207
|
-
onProgress(Ok({
|
|
208
|
-
totalByteLength,
|
|
209
|
-
completedByteLength,
|
|
210
|
-
}));
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
// continue to read
|
|
215
|
-
reader.read().then(notify);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
// replace the original response with the new one
|
|
219
|
-
res = new Response(stream2, {
|
|
220
|
-
headers: res.headers,
|
|
221
|
-
status: res.status,
|
|
222
|
-
statusText: res.statusText,
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
switch (responseType) {
|
|
228
|
-
case 'arraybuffer': {
|
|
229
|
-
return Ok(await res.arrayBuffer() as T);
|
|
230
|
-
}
|
|
231
|
-
case 'blob': {
|
|
232
|
-
return Ok(await res.blob() as T);
|
|
233
|
-
}
|
|
234
|
-
case 'json': {
|
|
235
|
-
try {
|
|
236
|
-
return Ok(await res.json() as T);
|
|
237
|
-
} catch {
|
|
238
|
-
return Err(new Error('Response is invalid json while responseType is json'));
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
case 'text': {
|
|
242
|
-
return Ok(await res.text() as T);
|
|
243
|
-
}
|
|
244
|
-
default: {
|
|
245
|
-
// default return the Response object
|
|
246
|
-
return Ok(res as T);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}).catch((err) => {
|
|
250
|
-
cancelTimer?.();
|
|
251
|
-
|
|
252
|
-
return Err(err);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
if (shouldWaitTimeout) {
|
|
256
|
-
const timer = setTimeout(() => {
|
|
257
|
-
if (!controller.signal.aborted) {
|
|
258
|
-
const error = new Error();
|
|
259
|
-
error.name = TIMEOUT_ERROR;
|
|
260
|
-
controller.abort(error);
|
|
261
|
-
}
|
|
262
|
-
}, timeout);
|
|
263
|
-
|
|
264
|
-
cancelTimer = (): void => {
|
|
265
|
-
if (timer) {
|
|
266
|
-
clearTimeout(timer);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
cancelTimer = null;
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (abortable) {
|
|
274
|
-
return {
|
|
275
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
276
|
-
abort(reason?: any): void {
|
|
277
|
-
cancelTimer?.();
|
|
278
|
-
controller.abort(reason);
|
|
279
|
-
},
|
|
280
|
-
|
|
281
|
-
get aborted(): boolean {
|
|
282
|
-
return controller.signal.aborted;
|
|
283
|
-
},
|
|
284
|
-
|
|
285
|
-
get response(): FetchResponse<T> {
|
|
286
|
-
return response;
|
|
287
|
-
},
|
|
288
|
-
};
|
|
289
|
-
} else {
|
|
290
|
-
return response;
|
|
291
|
-
}
|
|
292
|
-
}
|
package/src/mod.ts
DELETED