1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | import { Url, URL, URLSearchParams } from 'url';
|
20 | import * as http from 'http';
|
21 | import * as https from 'https';
|
22 | import * as nodeStream from 'stream';
|
23 | import { CookieJar } from 'tough-cookie';
|
24 |
|
25 | export = got;
|
26 |
|
27 | declare class RequestError extends StdError {
|
28 | name: 'RequestError';
|
29 | }
|
30 |
|
31 | declare class ReadError extends StdError {
|
32 | name: 'ReadError';
|
33 | }
|
34 |
|
35 | declare class ParseError extends StdError {
|
36 | name: 'ParseError';
|
37 | statusCode: number;
|
38 | statusMessage: string;
|
39 | }
|
40 |
|
41 | declare class HTTPError extends StdError {
|
42 | name: 'HTTPError';
|
43 | statusCode: number;
|
44 | statusMessage: string;
|
45 | headers: http.IncomingHttpHeaders;
|
46 | body: Buffer | string | object;
|
47 | }
|
48 |
|
49 | declare class MaxRedirectsError extends StdError {
|
50 | name: 'MaxRedirectsError';
|
51 | statusCode: number;
|
52 | statusMessage: string;
|
53 | redirectUrls: string[];
|
54 | }
|
55 |
|
56 | declare class UnsupportedProtocolError extends StdError {
|
57 | name: 'UnsupportedProtocolError';
|
58 | }
|
59 |
|
60 | declare class CancelError extends StdError {
|
61 | name: 'CancelError';
|
62 | }
|
63 |
|
64 | declare class TimeoutError extends StdError {
|
65 | name: 'TimeoutError';
|
66 | event: keyof got.TimeoutOptions;
|
67 | }
|
68 |
|
69 | declare class StdError extends Error {
|
70 | code?: string | undefined;
|
71 | host?: string | undefined;
|
72 | hostname?: string | undefined;
|
73 | method?: string | undefined;
|
74 | path?: string | undefined;
|
75 | protocol?: string | undefined;
|
76 | url?: string | undefined;
|
77 | response?: any;
|
78 | }
|
79 |
|
80 | declare const got: got.GotInstance;
|
81 |
|
82 | interface InternalRequestOptions extends https.RequestOptions {
|
83 |
|
84 | timeout?: any;
|
85 | agent?: any;
|
86 | }
|
87 |
|
88 | declare namespace got {
|
89 | interface GotFn {
|
90 | (url: GotUrl): GotPromise<string>;
|
91 | (url: GotUrl, options: GotJSONOptions): GotPromise<any>;
|
92 | (url: GotUrl, options: GotFormOptions<string>): GotPromise<string>;
|
93 | (url: GotUrl, options: GotFormOptions<null>): GotPromise<Buffer>;
|
94 | (url: GotUrl, options: GotBodyOptions<string>): GotPromise<string>;
|
95 | (url: GotUrl, options: GotBodyOptions<null>): GotPromise<Buffer>;
|
96 | }
|
97 |
|
98 | interface GotJSONFn {
|
99 | (url: GotUrl): GotPromise<any>;
|
100 | (url: GotUrl, options: Partial<GotJSONOptions>): GotPromise<any>;
|
101 | }
|
102 |
|
103 | interface GotFormFn<T extends string | null> {
|
104 | (url: GotUrl): GotPromise<T extends null ? Buffer : string>;
|
105 | (url: GotUrl, options: Partial<GotFormOptions<T>>): GotPromise<T extends null ? Buffer : string>;
|
106 | }
|
107 |
|
108 | interface GotBodyFn<T extends string | null> {
|
109 | (url: GotUrl): GotPromise<T extends null ? Buffer : string>;
|
110 | (url: GotUrl, options: GotBodyOptions<T>): GotPromise<T extends null ? Buffer : string>;
|
111 | }
|
112 |
|
113 | type GotInstance<T = GotFn> = T &
|
114 | Record<'get' | 'post' | 'put' | 'patch' | 'head' | 'delete', T> &
|
115 | {
|
116 | stream: GotStreamFn & Record<'get' | 'post' | 'put' | 'patch' | 'head' | 'delete', GotStreamFn>;
|
117 | extend: GotExtend;
|
118 | RequestError: typeof RequestError;
|
119 | ReadError: typeof ReadError;
|
120 | ParseError: typeof ParseError;
|
121 | HTTPError: typeof HTTPError;
|
122 | MaxRedirectsError: typeof MaxRedirectsError;
|
123 | UnsupportedProtocolError: typeof UnsupportedProtocolError;
|
124 | CancelError: typeof CancelError;
|
125 | TimeoutError: typeof TimeoutError;
|
126 | };
|
127 |
|
128 | interface GotExtend {
|
129 | (options: GotJSONOptions): GotInstance<GotJSONFn>;
|
130 | (options: GotFormOptions<string>): GotInstance<GotFormFn<string>>;
|
131 | (options: GotFormOptions<null>): GotInstance<GotFormFn<null>>;
|
132 | (options: GotBodyOptions<string>): GotInstance<GotBodyFn<string>>;
|
133 | (options: GotBodyOptions<null>): GotInstance<GotBodyFn<null>>;
|
134 | }
|
135 |
|
136 | type GotStreamFn = (url: GotUrl, options?: GotOptions<string | null>) => GotEmitter & nodeStream.Duplex;
|
137 |
|
138 | type GotUrl = string | https.RequestOptions | Url | URL;
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | interface Hooks<Options, Body extends Buffer | string | object> {
|
149 | init?: Array<InitHook<Options>> | undefined;
|
150 | beforeRequest?: Array<BeforeRequestHook<Options>> | undefined;
|
151 | beforeRedirect?: Array<BeforeRedirectHook<Options>> | undefined;
|
152 | beforeRetry?: Array<BeforeRetryHook<Options>> | undefined;
|
153 | beforeError?: BeforeErrorHook[] | undefined;
|
154 | afterResponse?: Array<AfterResponseHook<Options, Body>> | undefined;
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 | type InitHook<Options> = (options: Options) => void;
|
161 |
|
162 | |
163 |
|
164 |
|
165 | type BeforeRequestHook<Options> = (options: Options) => any;
|
166 |
|
167 | |
168 |
|
169 |
|
170 | type BeforeRedirectHook<Options> = (options: Options) => any;
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 | type BeforeRetryHook<Options> = (options: Options, error: GotError, retryCount: number) => any;
|
178 |
|
179 | type BeforeErrorHook = (error: GotError) => Error | Promise<Error>;
|
180 |
|
181 | |
182 |
|
183 |
|
184 |
|
185 | type AfterResponseHook<Options, Body extends Buffer | string | object> = (
|
186 | response: Response<Body>,
|
187 | retryWithMergedOptions: (updateOptions: Options) => GotPromise<Body>
|
188 | ) => Response<Body> | Promise<Response<Body>>;
|
189 |
|
190 | interface GotBodyOptions<E extends string | null> extends GotOptions<E> {
|
191 | body?: string | Buffer | nodeStream.Readable | undefined;
|
192 | hooks?: Hooks<GotBodyOptions<E>, string | Buffer | nodeStream.Readable> | undefined;
|
193 | }
|
194 |
|
195 | interface GotJSONOptions extends GotOptions<string | null> {
|
196 |
|
197 | body?: object | undefined;
|
198 | form?: boolean | undefined;
|
199 | json: true;
|
200 | hooks?: Hooks<GotJSONOptions, object> | undefined;
|
201 | }
|
202 |
|
203 | interface GotFormOptions<E extends string | null> extends GotOptions<E> {
|
204 | body?: Record<string, any> | undefined;
|
205 | form: true;
|
206 | json?: boolean | undefined;
|
207 | hooks?: Hooks<GotFormOptions<E>, Record<string, any>> | undefined;
|
208 | }
|
209 |
|
210 | type RequestFunction = typeof https.request;
|
211 |
|
212 | interface GotOptions<E extends string | null> extends InternalRequestOptions {
|
213 | baseUrl?: string | undefined;
|
214 | cookieJar?: CookieJar | undefined;
|
215 | encoding?: E | undefined;
|
216 | query?: Record<string, any> | URLSearchParams | string | undefined;
|
217 | timeout?: number | TimeoutOptions | undefined;
|
218 | retry?: number | RetryOptions | undefined;
|
219 | followRedirect?: boolean | undefined;
|
220 | decompress?: boolean | undefined;
|
221 | useElectronNet?: boolean | undefined;
|
222 | throwHttpErrors?: boolean | undefined;
|
223 | agent?: http.Agent | boolean | AgentOptions | undefined;
|
224 | cache?: Cache | undefined;
|
225 | request?: RequestFunction | undefined;
|
226 | }
|
227 |
|
228 | |
229 |
|
230 |
|
231 |
|
232 |
|
233 | interface TimeoutOptions {
|
234 | |
235 |
|
236 |
|
237 |
|
238 | lookup?: number | undefined;
|
239 | |
240 |
|
241 |
|
242 |
|
243 | connect?: number | undefined;
|
244 | |
245 |
|
246 |
|
247 |
|
248 | secureConnect?: number | undefined;
|
249 | |
250 |
|
251 |
|
252 | socket?: number | undefined;
|
253 | |
254 |
|
255 |
|
256 |
|
257 | response?: number | undefined;
|
258 | |
259 |
|
260 |
|
261 |
|
262 | send?: number | undefined;
|
263 | |
264 |
|
265 |
|
266 | request?: number | undefined;
|
267 | }
|
268 |
|
269 | type RetryFunction = (retry: number, error: any) => number;
|
270 |
|
271 | interface RetryOptions {
|
272 | retries?: number | RetryFunction | undefined;
|
273 | methods?: Array<'GET' | 'POST' | 'PUT' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE'> | undefined;
|
274 | statusCodes?: Array<408 | 413 | 429 | 500 | 502 | 503 | 504> | undefined;
|
275 | maxRetryAfter?: number | undefined;
|
276 | |
277 |
|
278 |
|
279 | errorCodes?: string[] | undefined;
|
280 | }
|
281 |
|
282 | interface AgentOptions {
|
283 | http: http.Agent;
|
284 | https: https.Agent;
|
285 | }
|
286 |
|
287 | interface Cache {
|
288 | set(key: string, value: any, ttl?: number): any;
|
289 | get(key: string): any;
|
290 | delete(key: string): any;
|
291 | }
|
292 |
|
293 | interface GotTimingsPhases {
|
294 | wait: number;
|
295 | dns: number;
|
296 | tcp: number;
|
297 | request: number;
|
298 | firstByte: number;
|
299 | download: number;
|
300 | total: number;
|
301 | }
|
302 |
|
303 | interface GotTimings {
|
304 | start: number;
|
305 | socket: number;
|
306 | lookup: number;
|
307 | connect: number;
|
308 | upload: number;
|
309 | response: number;
|
310 | end: number;
|
311 | error: number;
|
312 | phases: GotTimingsPhases;
|
313 | }
|
314 |
|
315 | interface Response<B extends Buffer | string | object> extends http.IncomingMessage {
|
316 | body: B;
|
317 | url: string;
|
318 | requestUrl: string;
|
319 | timings: GotTimings;
|
320 | fromCache: boolean;
|
321 | redirectUrls?: string[] | undefined;
|
322 | retryCount: number;
|
323 |
|
324 |
|
325 | statusCode: number;
|
326 | statusMessage: string;
|
327 | }
|
328 |
|
329 | type GotPromise<B extends Buffer | string | object> = Promise<Response<B>> & { cancel(): void };
|
330 |
|
331 | interface GotEmitter {
|
332 | addListener(event: 'request', listener: (req: http.ClientRequest) => void): this;
|
333 | addListener(event: 'response', listener: (res: http.IncomingMessage) => void): this;
|
334 | addListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions<string | null> & Url) => void): this;
|
335 | addListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
|
336 | addListener(event: 'downloadProgress', listener: (progress: Progress) => void): this;
|
337 | addListener(event: 'uploadProgress', listener: (progress: Progress) => void): this;
|
338 |
|
339 | on(event: 'request', listener: (req: http.ClientRequest) => void): this;
|
340 | on(event: 'response', listener: (res: http.IncomingMessage) => void): this;
|
341 | on(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions<string | null> & Url) => void): this;
|
342 | on(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
|
343 | on(event: 'downloadProgress', listener: (progress: Progress) => void): this;
|
344 | on(event: 'uploadProgress', listener: (progress: Progress) => void): this;
|
345 |
|
346 | once(event: 'request', listener: (req: http.ClientRequest) => void): this;
|
347 | once(event: 'response', listener: (res: http.IncomingMessage) => void): this;
|
348 | once(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions<string | null> & Url) => void): this;
|
349 | once(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
|
350 | once(event: 'downloadProgress', listener: (progress: Progress) => void): this;
|
351 | once(event: 'uploadProgress', listener: (progress: Progress) => void): this;
|
352 |
|
353 | prependListener(event: 'request', listener: (req: http.ClientRequest) => void): this;
|
354 | prependListener(event: 'response', listener: (res: http.IncomingMessage) => void): this;
|
355 | prependListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions<string | null> & Url) => void): this;
|
356 | prependListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
|
357 | prependListener(event: 'downloadProgress', listener: (progress: Progress) => void): this;
|
358 | prependListener(event: 'uploadProgress', listener: (progress: Progress) => void): this;
|
359 |
|
360 | prependOnceListener(event: 'request', listener: (req: http.ClientRequest) => void): this;
|
361 | prependOnceListener(event: 'response', listener: (res: http.IncomingMessage) => void): this;
|
362 | prependOnceListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions<string | null> & Url) => void): this;
|
363 | prependOnceListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
|
364 | prependOnceListener(event: 'downloadProgress', listener: (progress: Progress) => void): this;
|
365 | prependOnceListener(event: 'uploadProgress', listener: (progress: Progress) => void): this;
|
366 |
|
367 | removeListener(event: 'request', listener: (req: http.ClientRequest) => void): this;
|
368 | removeListener(event: 'response', listener: (res: http.IncomingMessage) => void): this;
|
369 | removeListener(event: 'redirect', listener: (res: http.IncomingMessage, nextOptions: GotOptions<string | null> & Url) => void): this;
|
370 | removeListener(event: 'error', listener: (error: GotError, body?: any, res?: http.IncomingMessage) => void): this;
|
371 | removeListener(event: 'downloadProgress', listener: (progress: Progress) => void): this;
|
372 | removeListener(event: 'uploadProgress', listener: (progress: Progress) => void): this;
|
373 | }
|
374 |
|
375 | type GotError = RequestError | ReadError | ParseError | HTTPError | MaxRedirectsError | UnsupportedProtocolError | CancelError | TimeoutError;
|
376 |
|
377 | interface Progress {
|
378 | percent: number;
|
379 | transferred: number;
|
380 | total: number | null;
|
381 | }
|
382 | }
|