-
Notifications
You must be signed in to change notification settings - Fork 515
/
RequestClient.ts
377 lines (343 loc) · 10.8 KB
/
RequestClient.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
import { HttpMethod } from "../interfaces";
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import * as fs from "fs";
import HttpsProxyAgent from "https-proxy-agent";
import qs from "qs";
import * as https from "https";
import Response from "../http/response";
import Request, {
RequestOptions as LastRequestOptions,
Headers,
} from "../http/request";
const DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded";
const DEFAULT_TIMEOUT = 30000;
const DEFAULT_INITIAL_RETRY_INTERVAL_MILLIS = 100;
const DEFAULT_MAX_RETRY_DELAY = 3000;
const DEFAULT_MAX_RETRIES = 3;
interface BackoffAxiosRequestConfig extends AxiosRequestConfig {
/**
* Current retry attempt performed by Axios
*/
retryCount?: number;
}
interface ExponentialBackoffResponseHandlerOptions {
/**
* Maximum retry delay in milliseconds
*/
maxIntervalMillis: number;
/**
* Maximum number of request retries
*/
maxRetries: number;
}
function getExponentialBackoffResponseHandler(
axios: AxiosInstance,
opts: ExponentialBackoffResponseHandlerOptions
) {
const maxIntervalMillis = opts.maxIntervalMillis;
const maxRetries = opts.maxRetries;
return function (res: AxiosResponse<any, any>) {
const config: BackoffAxiosRequestConfig = res.config;
if (res.status !== 429) {
return res;
}
const retryCount = (config.retryCount || 0) + 1;
if (retryCount <= maxRetries) {
config.retryCount = retryCount;
const baseDelay = Math.min(
maxIntervalMillis,
DEFAULT_INITIAL_RETRY_INTERVAL_MILLIS * Math.pow(2, retryCount)
);
const delay = Math.floor(baseDelay * Math.random()); // Full jitter backoff
return new Promise((resolve: (value: Promise<AxiosResponse>) => void) => {
setTimeout(() => resolve(axios(config)), delay);
});
}
return res;
};
}
class RequestClient {
defaultTimeout: number;
axios: AxiosInstance;
lastResponse?: Response<any>;
lastRequest?: Request<any>;
autoRetry: boolean;
maxRetryDelay: number;
maxRetries: number;
/**
* Make http request
* @param opts - The options passed to https.Agent
* @param opts.timeout - https.Agent timeout option. Used as the socket timeout, AND as the default request timeout.
* @param opts.keepAlive - https.Agent keepAlive option
* @param opts.keepAliveMsecs - https.Agent keepAliveMsecs option
* @param opts.maxSockets - https.Agent maxSockets option
* @param opts.maxTotalSockets - https.Agent maxTotalSockets option
* @param opts.maxFreeSockets - https.Agent maxFreeSockets option
* @param opts.scheduling - https.Agent scheduling option
* @param opts.autoRetry - Enable auto-retry requests with exponential backoff on 429 responses. Defaults to false.
* @param opts.maxRetryDelay - Max retry delay in milliseconds for 429 Too Many Request response retries. Defaults to 3000.
* @param opts.maxRetries - Max number of request retries for 429 Too Many Request responses. Defaults to 3.
*/
constructor(opts?: RequestClient.RequestClientOptions) {
opts = opts || {};
this.defaultTimeout = opts.timeout || DEFAULT_TIMEOUT;
this.autoRetry = opts.autoRetry || false;
this.maxRetryDelay = opts.maxRetryDelay || DEFAULT_MAX_RETRY_DELAY;
this.maxRetries = opts.maxRetries || DEFAULT_MAX_RETRIES;
// construct an https agent
let agentOpts: https.AgentOptions = {
timeout: this.defaultTimeout,
keepAlive: opts.keepAlive,
keepAliveMsecs: opts.keepAliveMsecs,
maxSockets: opts.maxSockets,
maxTotalSockets: opts.maxTotalSockets,
maxFreeSockets: opts.maxFreeSockets,
scheduling: opts.scheduling,
ca: opts.ca,
};
// sets https agent CA bundle if defined in CA bundle filepath env variable
if (process.env.TWILIO_CA_BUNDLE !== undefined) {
if (agentOpts.ca === undefined) {
agentOpts.ca = fs.readFileSync(process.env.TWILIO_CA_BUNDLE);
}
}
let agent;
if (process.env.HTTP_PROXY) {
// Note: if process.env.HTTP_PROXY is set, we're not able to apply the given
// socket timeout. See: https://github.com/TooTallNate/node-https-proxy-agent/pull/96
agent = HttpsProxyAgent(process.env.HTTP_PROXY);
} else {
agent = new https.Agent(agentOpts);
}
// construct an axios instance
this.axios = axios.create();
this.axios.defaults.headers.post["Content-Type"] = DEFAULT_CONTENT_TYPE;
this.axios.defaults.httpsAgent = agent;
if (opts.autoRetry) {
this.axios.interceptors.response.use(
getExponentialBackoffResponseHandler(this.axios, {
maxIntervalMillis: this.maxRetryDelay,
maxRetries: this.maxRetries,
})
);
}
}
/**
* Make http request
* @param opts - The options argument
* @param opts.method - The http method
* @param opts.uri - The request uri
* @param opts.username - The username used for auth
* @param opts.password - The password used for auth
* @param opts.headers - The request headers
* @param opts.params - The request params
* @param opts.data - The request data
* @param opts.timeout - The request timeout in milliseconds (default 30000)
* @param opts.allowRedirects - Should the client follow redirects
* @param opts.forever - Set to true to use the forever-agent
* @param opts.logLevel - Show debug logs
*/
request<TData>(
opts: RequestClient.RequestOptions<TData>
): Promise<Response<TData>> {
if (!opts.method) {
throw new Error("http method is required");
}
if (!opts.uri) {
throw new Error("uri is required");
}
var headers = opts.headers || {};
if (!headers.Connection && !headers.connection && opts.forever) {
headers.Connection = "keep-alive";
} else if (!headers.Connection && !headers.connection) {
headers.Connection = "close";
}
let auth = undefined;
if (opts.username && opts.password) {
auth = Buffer.from(opts.username + ":" + opts.password).toString(
"base64"
);
headers.Authorization = "Basic " + auth;
}
const options: AxiosRequestConfig = {
timeout: opts.timeout || this.defaultTimeout,
maxRedirects: opts.allowRedirects ? 10 : 0,
url: opts.uri,
method: opts.method,
headers: opts.headers,
proxy: false,
validateStatus: (status) => status >= 100 && status < 600,
};
if (opts.data && options.headers) {
if (
options.headers["Content-Type"] === "application/x-www-form-urlencoded"
) {
options.data = qs.stringify(opts.data, { arrayFormat: "repeat" });
} else if (options.headers["Content-Type"] === "application/json") {
options.data = opts.data;
}
}
if (opts.params) {
options.params = opts.params;
options.paramsSerializer = (params) => {
return qs.stringify(params, { arrayFormat: "repeat" });
};
}
const requestOptions: LastRequestOptions<TData> = {
method: opts.method,
url: opts.uri,
auth: auth,
params: options.params,
data: opts.data,
headers: opts.headers,
};
if (opts.logLevel === "debug") {
this.logRequest(requestOptions);
}
const _this = this;
this.lastResponse = undefined;
this.lastRequest = new Request(requestOptions);
return this.axios(options)
.then((response) => {
if (opts.logLevel === "debug") {
console.log(`response.statusCode: ${response.status}`);
console.log(`response.headers: ${JSON.stringify(response.headers)}`);
}
_this.lastResponse = new Response(
response.status,
response.data,
response.headers
);
return {
statusCode: response.status,
body: response.data,
headers: response.headers,
};
})
.catch((error) => {
_this.lastResponse = undefined;
throw error;
});
}
filterLoggingHeaders(headers: Headers) {
return Object.keys(headers).filter((header) => {
return !"authorization".includes(header.toLowerCase());
});
}
private logRequest<TData>(options: LastRequestOptions<TData>) {
console.log("-- BEGIN Twilio API Request --");
console.log(`${options.method} ${options.url}`);
if (options.params) {
console.log("Querystring:");
console.log(options.params);
}
if (options.headers) {
console.log("Headers:");
const filteredHeaderKeys = this.filterLoggingHeaders(
options.headers as Headers
);
filteredHeaderKeys.forEach((header) =>
console.log(`${header}: ${options.headers?.header}`)
);
}
console.log("-- END Twilio API Request --");
}
}
namespace RequestClient {
export interface RequestOptions<TData = any, TParams = object> {
/**
* The HTTP method
*/
method: HttpMethod;
/**
* The request URI
*/
uri: string;
/**
* The username used for auth
*/
username?: string;
/**
* The password used for auth
*/
password?: string;
/**
* The request headers
*/
headers?: Headers;
/**
* The object of params added as query string to the request
*/
params?: TParams;
/**
* The form data that should be submitted
*/
data?: TData;
/**
* The request timeout in milliseconds
*/
timeout?: number;
/**
* Should the client follow redirects
*/
allowRedirects?: boolean;
/**
* Set to true to use the forever-agent
*/
forever?: boolean;
/**
* Set to 'debug' to enable debug logging
*/
logLevel?: string;
}
export interface RequestClientOptions {
/**
* A timeout in milliseconds. This will be used as the HTTPS agent's socket
* timeout, AND as the default request timeout.
*/
timeout?: number;
/**
* https.Agent keepAlive option
*/
keepAlive?: boolean;
/**
* https.Agent keepAliveMSecs option
*/
keepAliveMsecs?: number;
/**
* https.Agent maxSockets option
*/
maxSockets?: number;
/**
* https.Agent maxTotalSockets option
*/
maxTotalSockets?: number;
/**
* https.Agent maxFreeSockets option
*/
maxFreeSockets?: number;
/**
* https.Agent scheduling option
*/
scheduling?: "fifo" | "lifo" | undefined;
/**
* The private CA certificate bundle (if private SSL certificate)
*/
ca?: string | Buffer;
/**
* Enable auto-retry with exponential backoff when receiving 429 Errors from
* the API. Disabled by default.
*/
autoRetry?: boolean;
/**
* Maximum retry delay in milliseconds for 429 Error response retries.
* Defaults to 3000.
*/
maxRetryDelay?: number;
/**
* Maximum number of request retries for 429 Error responses. Defaults to 3.
*/
maxRetries?: number;
}
}
export = RequestClient;