forked from sindresorhus/ky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
458 lines (355 loc) · 13 KB
/
index.d.ts
1
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
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/// <reference lib="dom"/>
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
type LiteralUnion<LiteralType extends BaseType, BaseType extends Primitive> =
| LiteralType
| (BaseType & {_?: never});
export type Input = Request | URL | string;
export type BeforeRequestHook = (
request: Request,
options: NormalizedOptions,
) => Request | Response | void | Promise<Request | Response | void>;
export type BeforeRetryHook = (
request: Request,
options: NormalizedOptions,
error: Error,
retryCount: number,
) => void | Promise<void>;
export type AfterResponseHook = (
request: Request,
options: NormalizedOptions,
response: Response,
) => Response | void | Promise<Response | void>;
export interface DownloadProgress {
percent: number;
transferredBytes: number;
/**
Note: If it's not possible to retrieve the body size, it will be `0`.
*/
totalBytes: number;
}
export interface Hooks {
/**
This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives normalized input and options as arguments. You could, for example, modify `options.headers` here.
A [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) can be returned from this hook to completely avoid making a HTTP request. This can be used to mock a request, check an internal cache, etc. An **important** consideration when returning a `Response` from this hook is that all the following hooks will be skipped, so **ensure you only return a `Response` from the last hook**.
@default []
*/
beforeRequest?: BeforeRequestHook[];
/**
This hook enables you to modify the request right before retry. Ky will make no further changes to the request after this. The hook function receives the normalized input and options, an error instance and the retry count as arguments. You could, for example, modify `options.headers` here.
@example
```
import ky from 'ky';
(async () => {
await ky('https://example.com', {
hooks: {
beforeRetry: [
async (input, options, errors, retryCount) => {
const token = await ky('https://example.com/refresh-token');
options.headers.set('Authorization', `token ${token}`);
}
]
}
});
})();
```
@default []
*/
beforeRetry?: BeforeRetryHook[];
/**
This hook enables you to read and optionally modify the response. The hook function receives normalized input, options, and a clone of the response as arguments. The return value of the hook function will be used by Ky as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
@default []
@example
```
import ky from 'ky';
(async () => {
await ky('https://example.com', {
hooks: {
afterResponse: [
(_input, _options, response) => {
// You could do something with the response, for example, logging.
log(response);
// Or return a `Response` instance to overwrite the response.
return new Response('A different response', {status: 200});
},
// Or retry with a fresh token on a 403 error
async (input, options, response) => {
if (response.status === 403) {
// Get a fresh token
const token = await ky('https://example.com/token').text();
// Retry with the token
options.headers.set('Authorization', `token ${token}`);
return ky(input, options);
}
}
]
}
});
})();
```
*/
afterResponse?: AfterResponseHook[];
}
export interface RetryOptions {
/**
The number of times to retry failed requests.
@default 2
*/
limit?: number;
/**
The HTTP methods allowed to retry.
@default ['get', 'put', 'head', 'delete', 'options', 'trace']
*/
methods?: string[];
/**
The HTTP status codes allowed to retry.
@default [408, 413, 429, 500, 502, 503, 504]
*/
statusCodes?: number[];
/**
The HTTP status codes allowed to retry with a `Retry-After` header.
@default [413, 429, 503]
*/
afterStatusCodes?: number[];
/**
If the `Retry-After` header is greater than `maxRetryAfter`, the request will be canceled.
@default Infinity
*/
maxRetryAfter?: number;
}
/**
Options are the same as `window.fetch`, with some exceptions.
*/
export interface Options extends RequestInit {
/**
HTTP method used to make the request.
Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD` and `DELETE`) are uppercased in order to avoid server errors due to case sensitivity.
*/
method?: LiteralUnion<'get' | 'post' | 'put' | 'delete' | 'patch' | 'head', string>;
/**
Shortcut for sending JSON. Use this instead of the `body` option.
Accepts a plain object which will be `JSON.stringify()`'d and the correct header will be set for you.
*/
json?: unknown;
/**
Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL.
Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).
*/
searchParams?: string | {[key: string]: string | number | boolean} | Array<Array<string | number | boolean>> | URLSearchParams;
/**
A prefix to prepend to the `input` URL when making the request. It can be any valid URL, either relative or absolute. A trailing slash `/` is optional and will be added automatically, if needed, when it is joined with `input`. Only takes effect when `input` is a string. The `input` argument cannot start with a slash `/` when using this option.
Useful when used with [`ky.extend()`](#kyextenddefaultoptions) to create niche-specific Ky-instances.
Notes:
- After `prefixUrl` and `input` are joined, the result is resolved against the [base URL](https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI) of the page (if any).
- Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion about how the `input` URL is handled, given that `input` will not follow the normal URL resolution rules when `prefixUrl` is being used, which changes the meaning of a leading slash.
@example
```
import ky from 'ky';
// On https://example.com
(async () => {
await ky('unicorn', {prefixUrl: '/api'});
//=> 'https://example.com/api/unicorn'
await ky('unicorn', {prefixUrl: 'https://cats.com'});
//=> 'https://cats.com/unicorn'
})();
```
*/
prefixUrl?: URL | string;
/**
An object representing `limit`, `methods`, `statusCodes` and `maxRetryAfter` fields for maximum retry count, allowed methods, allowed status codes and maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time.
If `retry` is a number, it will be used as `limit` and other defaults will remain in place.
If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`. If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
Delays between retries is calculated with the function `0.3 * (2 ** (retry - 1)) * 1000`, where `retry` is the attempt number (starts from 1).
@example
```
import ky from 'ky';
(async () => {
const parsed = await ky('https://example.com', {
retry: {
limit: 10,
methods: ['get'],
statusCodes: [413]
}
}).json();
})();
```
*/
retry?: RetryOptions | number;
/**
Timeout in milliseconds for getting a response. Can not be greater than 2147483647.
If set to `false`, there will be no timeout.
@default 10000
*/
timeout?: number | false;
/**
Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially.
*/
hooks?: Hooks;
/**
Throw a `HTTPError` for error responses (non-2xx status codes).
Setting this to `false` may be useful if you are checking for resource availability and are expecting error responses.
@default true
*/
throwHttpErrors?: boolean;
/**
Download progress event handler.
@param chunk - Note: It's empty for the first call.
@example
```
import ky from 'ky';
(async () => {
await ky('https://example.com', {
onDownloadProgress: (progress, chunk) => {
// Example output:
// `0% - 0 of 1271 bytes`
// `100% - 1271 of 1271 bytes`
console.log(`${progress.percent * 100}% - ${progress.transferredBytes} of ${progress.totalBytes} bytes`);
}
});
})();
```
*/
onDownloadProgress?: (progress: DownloadProgress, chunk: Uint8Array) => void;
}
/**
Normalized options passed to the `fetch` call and the `beforeRequest` hooks.
*/
export interface NormalizedOptions extends RequestInit {
// Extended from `RequestInit`, but ensured to be set (not optional).
method: RequestInit['method'];
credentials: RequestInit['credentials'];
// Extended from custom `KyOptions`, but ensured to be set (not optional).
retry: Options['retry'];
prefixUrl: Options['prefixUrl'];
onDownloadProgress: Options['onDownloadProgress'];
// New type.
headers: Headers;
}
/**
Returns a `Response` object with `Body` methods added for convenience. So you can, for example, call `ky.get(input).json()` directly without having to await the `Response` first. When called like that, an appropriate `Accept` header will be set depending on the body method used. Unlike the `Body` methods of `window.Fetch`; these will throw an `HTTPError` if the response status is not in the range of `200...299`. Also, `.json()` will return an empty string if the response status is `204` instead of throwing a parse error due to an empty body.
*/
export interface ResponsePromise extends Promise<Response> {
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
// TODO: Use `json<T extends JSONValue>(): Promise<T>;` when it's fixed in TS.
// See https://github.com/microsoft/TypeScript/issues/15300 and https://github.com/sindresorhus/ky/pull/80
/**
Get the response body as JSON.
@example
```
import ky from 'ky';
const parsed = await ky(…).json();
```
@example
```
import ky from 'ky';
interface Result {
value: number;
}
const result = await ky(…).json<Result>();
```
*/
json<T>(): Promise<T>;
text(): Promise<string>;
}
/**
The error has a response property with the `Response` object.
*/
export class HTTPError extends Error {
constructor(response: Response);
response: Response;
}
/**
The error thrown when the request times out.
*/
export class TimeoutError extends Error {
constructor();
}
declare const ky: {
/**
Fetch the given `url`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` method added.
@example
```
import ky from 'ky';
(async () => {
const parsed = await ky('https://example.com', {json: {foo: true}}).json();
console.log(parsed);
//=> `{data: '🦄'}`
})();
```
*/
(url: Input, options?: Options): ResponsePromise;
/**
Fetch the given `url` using the option `{method: 'get'}`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` methods added.
*/
get(url: Input, options?: Options): ResponsePromise;
/**
Fetch the given `url` using the option `{method: 'post'}`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` methods added.
*/
post(url: Input, options?: Options): ResponsePromise;
/**
Fetch the given `url` using the option `{method: 'put'}`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` methods added.
*/
put(url: Input, options?: Options): ResponsePromise;
/**
Fetch the given `url` using the option `{method: 'delete'}`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` methods added.
*/
delete(url: Input, options?: Options): ResponsePromise;
/**
Fetch the given `url` using the option `{method: 'patch'}`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` methods added.
*/
patch(url: Input, options?: Options): ResponsePromise;
/**
Fetch the given `url` using the option `{method: 'head'}`.
@param url - `Request` object, `URL` object, or URL string.
@returns A promise with `Body` methods added.
*/
head(url: Input, options?: Options): ResponsePromise;
/**
Create a new Ky instance with complete new defaults.
@returns A new Ky instance.
*/
create(defaultOptions: Options): typeof ky;
/**
Create a new Ky instance with some defaults overridden with your own.
In contrast to `ky.create()`, `ky.extend()` inherits defaults from its parent.
@returns A new Ky instance.
*/
extend(defaultOptions: Options): typeof ky;
/**
A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry.
This will also short circuit the remaining `beforeRetry` hooks.
@example
```
import ky from 'ky';
(async () => {
await ky('https://example.com', {
hooks: {
beforeRetry: [
async (request, options, errors, retryCount) => {
const shouldStopRetry = await ky('https://example.com/api');
if (shouldStopRetry) {
return ky.stop;
}
}
]
}
});
})();
```
*/
readonly stop: unique symbol;
};
export default ky;