-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.d.ts
executable file
·350 lines (285 loc) · 10.9 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
/// <reference types="node" />
import { EventEmitter } from 'events';
import * as Http from 'http';
import * as Https from 'https';
import * as Stream from 'stream';
import * as Url from 'url';
import { Boom } from '@hapi/boom';
/**
* An HTTP request client.
*/
declare class Client {
/**
* An object containing the node agents used for pooling connections for `http` and `https`.
*/
agents: Client.Agents;
/**
* An event emitter used to deliver events when the `events` option is set.
*/
events?: Client.Events;
/**
* Creates a new client.
*
* @param options - the client default options.
*/
constructor(options?: Client.Options);
/**
* Creates a new client using the current client options as defaults and the provided options as override.
*
* @param options - the client override options.
*
* @returns a new client.
*/
defaults(options: Client.Options): Client;
/**
* Request an HTTP resource.
*
* @param method - a string specifying the HTTP request method. Defaults to 'GET'.
* @param url - the URI of the requested resource.
* @param options - default options override.
*
* @returns a promise resolving into an HTTP response object with a 'req' property holding a reference to the HTTP request object.
*/
request(method: string, url: string, options?: Client.request.Options): Promise<Http.IncomingMessage> & { req: Http.ClientRequest };
/**
* Reads a readable stream and returns the parsed payload.
*
* @param res - the readable stream.
* @param options - default options override.
*
* @returns the parsed payload based on the provided options.
*/
read<T = Buffer>(res: Stream.Readable | Http.IncomingMessage, options?: Client.read.Options): Promise<T>;
/**
* Converts a buffer, string, or an array of them into a readable stream.
*
* @param payload - a string, buffer, or an array of them.
* @param encoding - the payload encoding.
*
* @returns a readable stream.
*/
toReadableStream(payload: Client.toReadableStream.Payload, encoding?: string): Stream.Readable;
/**
* Parses the HTTP Cache-Control header.
*
* @param field - the header content.
*
* @returns an object with the header parameters or null if invalid.
*/
parseCacheControl(field: string): Client.parseCacheControl.Parameters | null;
/**
* Performs an HTTP GET request.
*
* @param uri - the resource URI.
* @param options - default options override.
*
* @returns the received payload Buffer or parsed payload based on the options.
*/
get<T>(uri: string, options?: Client.request.Options & Client.read.Options): Promise<Client.request.Response<T>>;
/**
* Performs an HTTP POST request.
*
* @param uri - the resource URI.
* @param options - default options override.
*
* @returns the received payload Buffer or parsed payload based on the options.
*/
post<T>(uri: string, options?: Client.request.Options & Client.read.Options): Promise<Client.request.Response<T>>;
/**
* Performs an HTTP PATCH request.
*
* @param uri - the resource URI.
* @param options - default options override.
*
* @returns the received payload Buffer or parsed payload based on the options.
*/
patch<T>(uri: string, options?: Client.request.Options & Client.read.Options): Promise<Client.request.Response<T>>;
/**
* Performs an HTTP PUT request.
*
* @param uri - the resource URI.
* @param options - default options override.
*
* @returns the received payload Buffer or parsed payload based on the options.
*/
put<T>(uri: string, options?: Client.request.Options & Client.read.Options): Promise<Client.request.Response<T>>;
/**
* Performs an HTTP DELETE request.
*
* @param uri - the resource URI.
* @param options - default options override.
*
* @returns the received payload Buffer or parsed payload based on the options.
*/
delete<T>(uri: string, options?: Client.request.Options & Client.read.Options): Promise<Client.request.Response<T>>;
}
declare namespace Client {
interface Options extends request.Options, read.Options {
/**
* An object containing the node agents used for pooling connections for `http` and `https`.
*/
readonly agents?: Agents;
/**
* Enables events.
*
* @default false
*/
readonly events?: boolean;
}
interface Agents {
/**
* The agent used for HTTP requests.
*/
readonly http: Http.Agent;
/**
* The agent used for HTTPS requests.
*/
readonly https: Https.Agent;
/**
* The agent used for HTTPS requests which ignores unauthorized requests.
*/
readonly httpsAllowUnauthorized: Https.Agent;
}
class Events extends EventEmitter {
on(event: 'preRequest', litener: Events.preRequest): this;
once(event: 'preRequest', litener: Events.preRequest): this;
addListener(event: 'preRequest', litener: Events.preRequest): this;
on(event: 'request', listener: Events.request): this;
once(event: 'request', listener: Events.request): this;
addListener(event: 'request', listener: Events.request): this;
on(event: 'response', listener: Events.response): this;
once(event: 'response', listener: Events.response): this;
addListener(event: 'response', listener: Events.response): this;
}
namespace Events {
type preRequest = (uri: string, options: Client.Options) => void;
type request = (req: Http.ClientRequest) => void;
type response = (err: Boom | undefined, details: { req: Http.ClientRequest, res: Http.IncomingMessage | undefined, start: number, url: Url.URL }) => void;
}
namespace request {
interface Options {
/**
* Node HTTP or HTTPS Agent object (false disables agent pooling).
*/
readonly agent?: Http.Agent | Https.Agent | false;
/**
* Fully qualified URL string used as the base URL.
*/
readonly baseUrl?: string;
/**
* A function to call before a redirect is triggered.
*
* @param redirectMethod - a string specifying the redirect method.
* @param statusCode - HTTP status code of the response that triggered the redirect.
* @param location - The redirect location string.
* @param resHeaders - An object with the headers received as part of the redirection response.
* @param redirectOptions - Options that will be applied to the redirect request. Changes to this object are applied to the redirection request.
* @param next - the callback function called to perform the redirection.
*/
readonly beforeRedirect?: (redirectMethod: string, statusCode: number, location: string, resHeaders: Record<string, string>, redirectOptions: Client.request.Options, next: () => void) => void;
/**
* TLS list of TLS ciphers to override node's default.
*/
readonly ciphers?: string;
/**
* An object containing the request headers.
*/
readonly headers?: Record<string, string>;
/**
* Determines how to handle gzipped payloads.
*
* @default false
*/
readonly gunzip?: boolean | 'force';
/**
* The request body as a string, Buffer, readable stream, or an object that can be serialized using `JSON.stringify()`.
*/
readonly payload?: Payload;
/**
* Enables redirects on 303 responses (using GET).
*
* @default false
*/
readonly redirect303?: boolean;
/**
* Overrides the HTTP method used when following 301 and 302 redirections. Defaults to the original method.
*/
readonly redirectMethod?: string;
/**
* The maximum number of redirects to follow.
*
* @default false
*/
readonly redirects?: number | false;
/**
* A function to call when a redirect was triggered.
*
* @param statusCode - HTTP status code of the response that triggered the redirect.
* @param location - the redirected location string.
* @param req - the new ClientRequest object which replaces the one initially returned.
*/
readonly redirected?: (statusCode: number, location: string, req: Http.ClientRequest) => void;
/**
* TLS flag indicating whether the client should reject a response from a server with invalid certificates.
*/
readonly rejectUnauthorized?: boolean;
/**
* TLS flag indicating the SSL method to use, e.g. `SSLv3_method` to force SSL version 3.
*/
readonly secureProtocol?: string;
/**
* A UNIX socket path string for direct server connection.
*/
readonly socketPath?: string;
/**
* Number of milliseconds to wait without receiving a response before aborting the request.
*
* @default 0
*/
readonly timeout?: number;
}
type Payload = string | Buffer | Stream.Readable | object;
interface Response<T = Buffer> {
res: Http.IncomingMessage;
payload: T;
}
}
namespace read {
interface Options {
/**
* Determines how to handle gzipped payloads.
*
* @default false
*/
readonly gunzip?: boolean | 'force';
/**
* Determines how to parse the payload as JSON.
*/
readonly json?: boolean | 'strict' | 'force';
/**
* The maximum allowed response payload size.
*
* @default 0
*/
readonly maxBytes?: number;
/**
* The number of milliseconds to wait while reading data before aborting handling of the response.
*
* @default 0
*/
readonly timeout?: number;
}
}
namespace toReadableStream {
type Item = string | Buffer;
type Payload = Item | Item[];
}
namespace parseCacheControl {
interface Parameters {
'max-age'?: number;
[key: string]: string | number | undefined;
}
}
}
declare const client: Client;
export = client;