UNPKG

15.6 kBTypeScriptView Raw
1// Type definitions for got 9.6
2// Project: https://github.com/sindresorhus/got#readme
3// Definitions by: BendingBender <https://github.com/BendingBender>
4// Linus Unnebäck <https://github.com/LinusU>
5// Konstantin Ikonnikov <https://github.com/ikokostya>
6// Stijn Van Nieuwenhuyse <https://github.com/stijnvn>
7// Matthew Bull <https://github.com/wingsbob>
8// Ryan Wilson-Perkin <https://github.com/ryanwilsonperkin>
9// Paul Hawxby <https://github.com/phawxby>
10// Ivy Witter <https://github.com/ivywit>
11// Huachao Mao <https://github.com/Huachao>
12// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
13// TypeScript Version: 2.8
14
15// Got v10 comes bundled with typings so these should only be maintained for Got v9.
16
17/// <reference types="node"/>
18
19import { Url, URL, URLSearchParams } from 'url';
20import * as http from 'http';
21import * as https from 'https';
22import * as nodeStream from 'stream';
23import { CookieJar } from 'tough-cookie';
24
25export = got;
26
27declare class RequestError extends StdError {
28 name: 'RequestError';
29}
30
31declare class ReadError extends StdError {
32 name: 'ReadError';
33}
34
35declare class ParseError extends StdError {
36 name: 'ParseError';
37 statusCode: number;
38 statusMessage: string;
39}
40
41declare class HTTPError extends StdError {
42 name: 'HTTPError';
43 statusCode: number;
44 statusMessage: string;
45 headers: http.IncomingHttpHeaders;
46 body: Buffer | string | object;
47}
48
49declare class MaxRedirectsError extends StdError {
50 name: 'MaxRedirectsError';
51 statusCode: number;
52 statusMessage: string;
53 redirectUrls: string[];
54}
55
56declare class UnsupportedProtocolError extends StdError {
57 name: 'UnsupportedProtocolError';
58}
59
60declare class CancelError extends StdError {
61 name: 'CancelError';
62}
63
64declare class TimeoutError extends StdError {
65 name: 'TimeoutError';
66 event: keyof got.TimeoutOptions;
67}
68
69declare 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
80declare const got: got.GotInstance;
81
82interface InternalRequestOptions extends https.RequestOptions {
83 // Redeclare options with `any` type for allow specify types incompatible with http.RequestOptions.
84 timeout?: any;
85 agent?: any;
86}
87
88declare 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 * Hooks allow modifications during the request lifecycle. Hook functions may be async and are
142 * run serially.
143 *
144 * @see https://github.com/sindresorhus/got#hooks
145 * @template Options Request options.
146 * @template Body Response body type.
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 * @param options Unnormalized request options.
159 */
160 type InitHook<Options> = (options: Options) => void;
161
162 /**
163 * @param options Normalized request options.
164 */
165 type BeforeRequestHook<Options> = (options: Options) => any;
166
167 /**
168 * @param options Normalized request options.
169 */
170 type BeforeRedirectHook<Options> = (options: Options) => any;
171
172 /**
173 * @param options Normalized request options.
174 * @param error Request error.
175 * @param retryCount Number of retry.
176 */
177 type BeforeRetryHook<Options> = (options: Options, error: GotError, retryCount: number) => any;
178
179 type BeforeErrorHook = (error: GotError) => Error | Promise<Error>;
180
181 /**
182 * @param response Response object.
183 * @param retryWithMergedOptions Retries request with the updated options.
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 // Body must be an object or array. See https://github.com/sindresorhus/got/issues/511
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 * Contains properties to constrain the duration of each phase of the request lifecycle.
230 *
231 * @see https://github.com/sindresorhus/got#timeout
232 */
233 interface TimeoutOptions {
234 /**
235 * Starts when a socket is assigned and ends when the hostname has been resolved. Does not
236 * apply when using a Unix domain socket.
237 */
238 lookup?: number | undefined;
239 /**
240 * Starts when `lookup` completes (or when the socket is assigned if lookup does not apply
241 * to the request) and ends when the socket is connected.
242 */
243 connect?: number | undefined;
244 /**
245 * Starts when `connect` completes and ends when the handshaking process completes (HTTPS
246 * only).
247 */
248 secureConnect?: number | undefined;
249 /**
250 * Starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
251 */
252 socket?: number | undefined;
253 /**
254 * Starts when the request has been written to the socket and ends when the response headers
255 * are received.
256 */
257 response?: number | undefined;
258 /**
259 * Starts when the socket is connected and ends with the request has been written to the
260 * socket.
261 */
262 send?: number | undefined;
263 /**
264 * Starts when the request is initiated and ends when the response's end event fires.
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 * Allowed error codes.
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 // got's Response is always a "response obtained from http.ClientRequest", therefore these two properties are never undefined
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}