@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/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 -207
- package/package.json +37 -22
- package/dist/types.d.ts.map +0 -1
- package/docs/README.md +0 -39
- package/docs/classes/FetchError.md +0 -49
- package/docs/functions/fetchT.md +0 -403
- package/docs/interfaces/FetchInit.md +0 -25
- package/docs/interfaces/FetchProgress.md +0 -18
- package/docs/interfaces/FetchTask.md +0 -46
- package/docs/type-aliases/FetchResponse.md +0 -22
- package/docs/type-aliases/FetchResponseType.md +0 -15
- package/docs/variables/ABORT_ERROR.md +0 -15
- package/docs/variables/TIMEOUT_ERROR.md +0 -15
- 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
package/dist/types.d.ts
CHANGED
|
@@ -1,207 +1,432 @@
|
|
|
1
|
-
import { AsyncResult
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
*
|
|
74
|
-
* @param
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
abortable
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
*
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* @
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
1
|
+
import { AsyncResult } from 'happy-rusty';
|
|
2
|
+
import { IOResult } from 'happy-rusty';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error name for aborted fetch requests.
|
|
6
|
+
*
|
|
7
|
+
* This matches the standard `AbortError` name used by the Fetch API when a request
|
|
8
|
+
* is cancelled via `AbortController.abort()`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { fetchT, ABORT_ERROR } from '@happy-ts/fetch-t';
|
|
13
|
+
*
|
|
14
|
+
* const task = fetchT('https://api.example.com/data', { abortable: true });
|
|
15
|
+
* task.abort();
|
|
16
|
+
*
|
|
17
|
+
* const result = await task.response;
|
|
18
|
+
* result.inspectErr((err) => {
|
|
19
|
+
* if (err.name === ABORT_ERROR) {
|
|
20
|
+
* console.log('Request was aborted');
|
|
21
|
+
* }
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const ABORT_ERROR: "AbortError";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Custom error class for HTTP error responses (non-2xx status codes).
|
|
29
|
+
*
|
|
30
|
+
* Thrown when `Response.ok` is `false`. Contains the HTTP status code
|
|
31
|
+
* for programmatic error handling.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { fetchT, FetchError } from '@happy-ts/fetch-t';
|
|
36
|
+
*
|
|
37
|
+
* const result = await fetchT('https://api.example.com/not-found', {
|
|
38
|
+
* responseType: 'json',
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* result.inspectErr((err) => {
|
|
42
|
+
* if (err instanceof FetchError) {
|
|
43
|
+
* console.log('HTTP Status:', err.status); // e.g., 404
|
|
44
|
+
* console.log('Status Text:', err.message); // e.g., "Not Found"
|
|
45
|
+
*
|
|
46
|
+
* // Handle specific status codes
|
|
47
|
+
* switch (err.status) {
|
|
48
|
+
* case 401:
|
|
49
|
+
* console.log('Unauthorized - please login');
|
|
50
|
+
* break;
|
|
51
|
+
* case 404:
|
|
52
|
+
* console.log('Resource not found');
|
|
53
|
+
* break;
|
|
54
|
+
* case 500:
|
|
55
|
+
* console.log('Server error');
|
|
56
|
+
* break;
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare class FetchError extends Error {
|
|
63
|
+
/**
|
|
64
|
+
* The error name, always `'FetchError'`.
|
|
65
|
+
*/
|
|
66
|
+
name: string;
|
|
67
|
+
/**
|
|
68
|
+
* The HTTP status code of the response (e.g., 404, 500).
|
|
69
|
+
*/
|
|
70
|
+
status: number;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new FetchError instance.
|
|
73
|
+
*
|
|
74
|
+
* @param message - The status text from the HTTP response (e.g., "Not Found").
|
|
75
|
+
* @param status - The HTTP status code (e.g., 404).
|
|
76
|
+
*/
|
|
77
|
+
constructor(message: string, status: number);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Extended fetch options that add additional capabilities to the standard `RequestInit`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* import { fetchT, type FetchInit } from '@happy-ts/fetch-t';
|
|
86
|
+
*
|
|
87
|
+
* const options: FetchInit = {
|
|
88
|
+
* // Standard RequestInit options
|
|
89
|
+
* method: 'POST',
|
|
90
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
91
|
+
* body: JSON.stringify({ key: 'value' }),
|
|
92
|
+
*
|
|
93
|
+
* // Extended options
|
|
94
|
+
* abortable: true, // Return FetchTask for manual abort control
|
|
95
|
+
* responseType: 'json', // Auto-parse response as JSON
|
|
96
|
+
* timeout: 10000, // Abort after 10 seconds
|
|
97
|
+
* onProgress: (result) => { // Track download progress
|
|
98
|
+
* result.inspect(({ completedByteLength, totalByteLength }) => {
|
|
99
|
+
* console.log(`${completedByteLength}/${totalByteLength}`);
|
|
100
|
+
* });
|
|
101
|
+
* },
|
|
102
|
+
* onChunk: (chunk) => { // Receive raw data chunks
|
|
103
|
+
* console.log('Received chunk:', chunk.byteLength, 'bytes');
|
|
104
|
+
* },
|
|
105
|
+
* };
|
|
106
|
+
*
|
|
107
|
+
* const task = fetchT('https://api.example.com/upload', options);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare interface FetchInit extends RequestInit {
|
|
111
|
+
/**
|
|
112
|
+
* When `true`, returns a `FetchTask` instead of `FetchResponse`.
|
|
113
|
+
*
|
|
114
|
+
* The `FetchTask` provides `abort()` method and `aborted` status.
|
|
115
|
+
*
|
|
116
|
+
* @default false
|
|
117
|
+
*/
|
|
118
|
+
abortable?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Specifies how the response body should be parsed.
|
|
121
|
+
*
|
|
122
|
+
* - `'text'` - Returns `string`
|
|
123
|
+
* - `'json'` - Returns parsed JSON (type `T`)
|
|
124
|
+
* - `'arraybuffer'` - Returns `ArrayBuffer`
|
|
125
|
+
* - `'blob'` - Returns `Blob`
|
|
126
|
+
* - `undefined` - Returns raw `Response` object
|
|
127
|
+
*/
|
|
128
|
+
responseType?: FetchResponseType;
|
|
129
|
+
/**
|
|
130
|
+
* Maximum time in milliseconds to wait for the request to complete.
|
|
131
|
+
*
|
|
132
|
+
* If exceeded, the request is automatically aborted with a `TimeoutError`.
|
|
133
|
+
* Must be a positive number.
|
|
134
|
+
*/
|
|
135
|
+
timeout?: number;
|
|
136
|
+
/**
|
|
137
|
+
* Callback invoked during download to report progress.
|
|
138
|
+
*
|
|
139
|
+
* Receives an `IOResult<FetchProgress>`:
|
|
140
|
+
* - `Ok(FetchProgress)` - Progress update with byte counts
|
|
141
|
+
* - `Err(Error)` - If `Content-Length` header is missing (called once)
|
|
142
|
+
*
|
|
143
|
+
* @param progressResult - The progress result, either success with progress data or error.
|
|
144
|
+
*/
|
|
145
|
+
onProgress?: (progressResult: IOResult<FetchProgress>) => void;
|
|
146
|
+
/**
|
|
147
|
+
* Callback invoked when a chunk of data is received.
|
|
148
|
+
*
|
|
149
|
+
* Useful for streaming or processing data as it arrives.
|
|
150
|
+
* Each chunk is a `Uint8Array` containing the raw bytes.
|
|
151
|
+
*
|
|
152
|
+
* @param chunk - The raw data chunk received from the response stream.
|
|
153
|
+
*/
|
|
154
|
+
onChunk?: (chunk: Uint8Array) => void;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Represents the download progress of a fetch operation.
|
|
159
|
+
*
|
|
160
|
+
* Passed to the `onProgress` callback when tracking download progress.
|
|
161
|
+
* Note: Progress tracking requires the server to send a `Content-Length` header.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* import { fetchT, type FetchProgress } from '@happy-ts/fetch-t';
|
|
166
|
+
*
|
|
167
|
+
* await fetchT('https://example.com/file.zip', {
|
|
168
|
+
* responseType: 'blob',
|
|
169
|
+
* onProgress: (result) => {
|
|
170
|
+
* result.inspect((progress: FetchProgress) => {
|
|
171
|
+
* const percent = (progress.completedByteLength / progress.totalByteLength) * 100;
|
|
172
|
+
* console.log(`Downloaded: ${percent.toFixed(1)}%`);
|
|
173
|
+
* });
|
|
174
|
+
* },
|
|
175
|
+
* });
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
export declare interface FetchProgress {
|
|
179
|
+
/**
|
|
180
|
+
* The total number of bytes to be received (from Content-Length header).
|
|
181
|
+
*/
|
|
182
|
+
totalByteLength: number;
|
|
183
|
+
/**
|
|
184
|
+
* The number of bytes received so far.
|
|
185
|
+
*/
|
|
186
|
+
completedByteLength: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Represents the response of a fetch operation as an async Result type.
|
|
191
|
+
*
|
|
192
|
+
* This is an alias for `AsyncResult<T, E>` from the `happy-rusty` library,
|
|
193
|
+
* providing Rust-like error handling without throwing exceptions.
|
|
194
|
+
*
|
|
195
|
+
* @typeParam T - The type of the data expected in a successful response.
|
|
196
|
+
* @typeParam E - The type of the error (defaults to `any`). Typically `Error` or `FetchError`.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* import { fetchT, type FetchResponse } from '@happy-ts/fetch-t';
|
|
201
|
+
*
|
|
202
|
+
* // FetchResponse is a Promise that resolves to Result<T, E>
|
|
203
|
+
* const response: FetchResponse<string> = fetchT('https://api.example.com', {
|
|
204
|
+
* responseType: 'text',
|
|
205
|
+
* });
|
|
206
|
+
*
|
|
207
|
+
* const result = await response;
|
|
208
|
+
* result
|
|
209
|
+
* .inspect((text) => console.log('Success:', text))
|
|
210
|
+
* .inspectErr((err) => console.error('Error:', err));
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export declare type FetchResponse<T, E = any> = AsyncResult<T, E>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Specifies the expected response type for automatic parsing.
|
|
217
|
+
*
|
|
218
|
+
* - `'text'` - Parse response as string via `Response.text()`
|
|
219
|
+
* - `'json'` - Parse response as JSON via `Response.json()`
|
|
220
|
+
* - `'arraybuffer'` - Parse response as ArrayBuffer via `Response.arrayBuffer()`
|
|
221
|
+
* - `'blob'` - Parse response as Blob via `Response.blob()`
|
|
222
|
+
*
|
|
223
|
+
* If not specified, the raw `Response` object is returned.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* import { fetchT, type FetchResponseType } from '@happy-ts/fetch-t';
|
|
228
|
+
*
|
|
229
|
+
* const responseType: FetchResponseType = 'json';
|
|
230
|
+
*
|
|
231
|
+
* const result = await fetchT('https://api.example.com/data', { responseType });
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
export declare type FetchResponseType = 'text' | 'arraybuffer' | 'blob' | 'json';
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Fetches a resource from the network as a text string and returns an abortable `FetchTask`.
|
|
238
|
+
*
|
|
239
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
240
|
+
* @param init - Additional options for the fetch operation, must include `abortable: true` and `responseType: 'text'`.
|
|
241
|
+
* @returns A `FetchTask` representing the abortable operation with a `string` response.
|
|
242
|
+
*/
|
|
243
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
244
|
+
abortable: true;
|
|
245
|
+
responseType: 'text';
|
|
246
|
+
}): FetchTask<string>;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Fetches a resource from the network as an ArrayBuffer and returns an abortable `FetchTask`.
|
|
250
|
+
*
|
|
251
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
252
|
+
* @param init - Additional options for the fetch operation, must include `abortable: true` and `responseType: 'arraybuffer'`.
|
|
253
|
+
* @returns A `FetchTask` representing the abortable operation with an `ArrayBuffer` response.
|
|
254
|
+
*/
|
|
255
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
256
|
+
abortable: true;
|
|
257
|
+
responseType: 'arraybuffer';
|
|
258
|
+
}): FetchTask<ArrayBuffer>;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Fetches a resource from the network as a Blob and returns an abortable `FetchTask`.
|
|
262
|
+
*
|
|
263
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
264
|
+
* @param init - Additional options for the fetch operation, must include `abortable: true` and `responseType: 'blob'`.
|
|
265
|
+
* @returns A `FetchTask` representing the abortable operation with a `Blob` response.
|
|
266
|
+
*/
|
|
267
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
268
|
+
abortable: true;
|
|
269
|
+
responseType: 'blob';
|
|
270
|
+
}): FetchTask<Blob>;
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Fetches a resource from the network and parses it as JSON, returning an abortable `FetchTask`.
|
|
274
|
+
*
|
|
275
|
+
* @typeParam T - The expected type of the parsed JSON data.
|
|
276
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
277
|
+
* @param init - Additional options for the fetch operation, must include `abortable: true` and `responseType: 'json'`.
|
|
278
|
+
* @returns A `FetchTask` representing the abortable operation with a response parsed as type `T`.
|
|
279
|
+
*/
|
|
280
|
+
export declare function fetchT<T>(url: string | URL, init: FetchInit & {
|
|
281
|
+
abortable: true;
|
|
282
|
+
responseType: 'json';
|
|
283
|
+
}): FetchTask<T>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Fetches a resource from the network as a text string.
|
|
287
|
+
*
|
|
288
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
289
|
+
* @param init - Additional options for the fetch operation, must include `responseType: 'text'`.
|
|
290
|
+
* @returns A `FetchResponse` representing the operation with a `string` response.
|
|
291
|
+
*/
|
|
292
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
293
|
+
responseType: 'text';
|
|
294
|
+
}): FetchResponse<string, Error>;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Fetches a resource from the network as an ArrayBuffer.
|
|
298
|
+
*
|
|
299
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
300
|
+
* @param init - Additional options for the fetch operation, must include `responseType: 'arraybuffer'`.
|
|
301
|
+
* @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.
|
|
302
|
+
*/
|
|
303
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
304
|
+
responseType: 'arraybuffer';
|
|
305
|
+
}): FetchResponse<ArrayBuffer, Error>;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Fetches a resource from the network as a Blob.
|
|
309
|
+
*
|
|
310
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
311
|
+
* @param init - Additional options for the fetch operation, must include `responseType: 'blob'`.
|
|
312
|
+
* @returns A `FetchResponse` representing the operation with a `Blob` response.
|
|
313
|
+
*/
|
|
314
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
315
|
+
responseType: 'blob';
|
|
316
|
+
}): FetchResponse<Blob, Error>;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Fetches a resource from the network and parses it as JSON.
|
|
320
|
+
*
|
|
321
|
+
* @typeParam T - The expected type of the parsed JSON data.
|
|
322
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
323
|
+
* @param init - Additional options for the fetch operation, must include `responseType: 'json'`.
|
|
324
|
+
* @returns A `FetchResponse` representing the operation with a response parsed as type `T`.
|
|
325
|
+
*/
|
|
326
|
+
export declare function fetchT<T>(url: string | URL, init: FetchInit & {
|
|
327
|
+
responseType: 'json';
|
|
328
|
+
}): FetchResponse<T, Error>;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Fetches a resource from the network and returns an abortable `FetchTask` with a generic `Response`.
|
|
332
|
+
*
|
|
333
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
334
|
+
* @param init - Additional options for the fetch operation, must include `abortable: true`.
|
|
335
|
+
* @returns A `FetchTask` representing the abortable operation with a `Response` object.
|
|
336
|
+
*/
|
|
337
|
+
export declare function fetchT(url: string | URL, init: FetchInit & {
|
|
338
|
+
abortable: true;
|
|
339
|
+
}): FetchTask<Response>;
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Fetches a resource from the network and returns a `FetchResponse` with a generic `Response` object.
|
|
343
|
+
*
|
|
344
|
+
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
|
|
345
|
+
* @param init - Optional additional options for the fetch operation.
|
|
346
|
+
* @returns A `FetchResponse` representing the operation with a `Response` object.
|
|
347
|
+
*/
|
|
348
|
+
export declare function fetchT(url: string | URL, init?: FetchInit): FetchResponse<Response>;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Represents an abortable fetch operation with control methods.
|
|
352
|
+
*
|
|
353
|
+
* Returned when `abortable: true` is set in the fetch options. Provides
|
|
354
|
+
* the ability to cancel the request and check its abort status.
|
|
355
|
+
*
|
|
356
|
+
* @typeParam T - The type of the data expected in the response.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```typescript
|
|
360
|
+
* import { fetchT, type FetchTask } from '@happy-ts/fetch-t';
|
|
361
|
+
*
|
|
362
|
+
* interface User {
|
|
363
|
+
* id: number;
|
|
364
|
+
* name: string;
|
|
365
|
+
* }
|
|
366
|
+
*
|
|
367
|
+
* const task: FetchTask<User> = fetchT<User>('https://api.example.com/user/1', {
|
|
368
|
+
* abortable: true,
|
|
369
|
+
* responseType: 'json',
|
|
370
|
+
* });
|
|
371
|
+
*
|
|
372
|
+
* // Check if aborted
|
|
373
|
+
* console.log('Is aborted:', task.aborted); // false
|
|
374
|
+
*
|
|
375
|
+
* // Abort with optional reason
|
|
376
|
+
* task.abort('User navigated away');
|
|
377
|
+
*
|
|
378
|
+
* // Access the response (will be an error after abort)
|
|
379
|
+
* const result = await task.response;
|
|
380
|
+
* result.inspectErr((err) => console.log('Aborted:', err.message));
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
export declare interface FetchTask<T> {
|
|
384
|
+
/**
|
|
385
|
+
* Aborts the fetch task, optionally with a reason.
|
|
386
|
+
*
|
|
387
|
+
* Once aborted, the `response` promise will resolve to an `Err` containing
|
|
388
|
+
* an `AbortError`. The abort reason can be any value and will be passed
|
|
389
|
+
* to the underlying `AbortController.abort()`.
|
|
390
|
+
*
|
|
391
|
+
* @param reason - An optional value indicating why the task was aborted.
|
|
392
|
+
* This can be an Error, string, or any other value.
|
|
393
|
+
*/
|
|
394
|
+
abort(reason?: any): void;
|
|
395
|
+
/**
|
|
396
|
+
* Indicates whether the fetch task has been aborted.
|
|
397
|
+
*
|
|
398
|
+
* Returns `true` if `abort()` was called or if the request timed out.
|
|
399
|
+
*/
|
|
400
|
+
readonly aborted: boolean;
|
|
401
|
+
/**
|
|
402
|
+
* The response promise of the fetch task.
|
|
403
|
+
*
|
|
404
|
+
* Resolves to `Ok<T>` on success, or `Err<Error>` on failure (including abort).
|
|
405
|
+
*/
|
|
406
|
+
readonly response: FetchResponse<T>;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Error name for timed out fetch requests.
|
|
411
|
+
*
|
|
412
|
+
* This is set on the `Error.name` property when a request exceeds the specified
|
|
413
|
+
* `timeout` duration and is automatically aborted.
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* ```typescript
|
|
417
|
+
* import { fetchT, TIMEOUT_ERROR } from '@happy-ts/fetch-t';
|
|
418
|
+
*
|
|
419
|
+
* const result = await fetchT('https://api.example.com/slow-endpoint', {
|
|
420
|
+
* timeout: 5000, // 5 seconds
|
|
421
|
+
* });
|
|
422
|
+
*
|
|
423
|
+
* result.inspectErr((err) => {
|
|
424
|
+
* if (err.name === TIMEOUT_ERROR) {
|
|
425
|
+
* console.log('Request timed out after 5 seconds');
|
|
426
|
+
* }
|
|
427
|
+
* });
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
430
|
+
export declare const TIMEOUT_ERROR: "TimeoutError";
|
|
431
|
+
|
|
432
|
+
export { }
|