forked from sindresorhus/ky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
163 lines (140 loc) · 4.39 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
export type BeforeRequestHook = (options: Object) => void;
export type AfterResponseHook = (response: Response) => Response | void;
export interface Hooks {
/**
* Before the request is sent.
*
* 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 the normalized options as the first argument. You could, for example, modify `options.headers` here.
*
* @default []
*/
beforeRequest: BeforeRequestHook[];
/**
* After the response is received.
*
* This hook enables you to read and optionally modify the response. 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 []
*/
afterResponse: AfterResponseHook[];
}
/**
* Options are the same as fetch, with some exceptions.
*/
export interface Options extends RequestInit {
/**
* Shortcut for sending JSON. Use this instead of the `body` option.
*/
json?: object;
/**
* Search parameters to include in the request URL.
* Setting this will override all existing search parameters in the input URL.
*/
searchParams?: string | {[key: string]: string | number} | URLSearchParams;
/**
* Prepends the input with the specified prefix.
* The prefix can be any valid URL, either relative or absolute.
*/
prefixUrl?: URL | string;
/**
* Numer of times to retry failed requests.
*
* @default 2
*/
retry?: number;
/**
* Timeout in milliseconds for getting a response.
*
* @default 10000
*/
timeout?: number;
/**
* 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).
*
* @default true
*/
throwHttpErrors?: boolean;
}
/**
* Returns a `Response` object with `Body` methods added for convenience.
*/
export interface ResponsePromise extends Promise<Response> {
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
json(): Promise<unknown>;
text(): Promise<string>;
}
/**
* The error has a response property with the `Response` object.
*/
export class HTTPError extends Error {
response: Response;
}
/**
* The error thrown when the request times out.
*/
export class TimeoutError extends Error {}
export interface Ky {
/**
* Fetches the `input` URL.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `input` URL with the option `{method: 'get'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
get(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `input` URL with the option `{method: 'post'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
post(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `input` URL with the option `{method: 'put'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
put(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `input` URL with the option `{method: 'patch'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
patch(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `input` URL with the option `{method: 'head'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
head(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Fetches the `input` URL with the option `{method: 'delete'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
delete(input: Request | URL | string, options?: Options): ResponsePromise;
/**
* Create a new Ky instance with some defaults overridden with your own.
*
* @returns New Ky instance
*/
extend(defaultOptions: Options): Ky;
}
declare const ky: Ky;
export default ky;