box-node-sdk 1.35.0 → 1.37.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -2
- package/README.md +1 -1
- package/lib/api-request-manager.d.ts +38 -0
- package/lib/api-request-manager.js +48 -55
- package/lib/api-request-manager.js.map +1 -0
- package/lib/api-request.d.ts +141 -0
- package/lib/api-request.js +202 -281
- package/lib/api-request.js.map +1 -0
- package/lib/box-client.d.ts +269 -0
- package/lib/box-client.js +551 -713
- package/lib/box-client.js.map +1 -0
- package/lib/box-node-sdk.d.ts +216 -0
- package/lib/box-node-sdk.js +317 -352
- package/lib/box-node-sdk.js.map +1 -0
- package/lib/chunked-uploader.d.ts +129 -0
- package/lib/chunked-uploader.js +287 -358
- package/lib/chunked-uploader.js.map +1 -0
- package/lib/enterprise-event-stream.d.ts +82 -0
- package/lib/enterprise-event-stream.js +189 -203
- package/lib/enterprise-event-stream.js.map +1 -0
- package/lib/event-stream.d.ts +92 -0
- package/lib/event-stream.js +274 -302
- package/lib/event-stream.js.map +1 -0
- package/lib/managers/collaboration-allowlist.d.ts +137 -0
- package/lib/managers/collaboration-allowlist.js +200 -0
- package/lib/managers/collaboration-allowlist.js.map +1 -0
- package/lib/managers/collaboration-whitelist.d.ts +3 -0
- package/lib/managers/collaboration-whitelist.js +8 -222
- package/lib/managers/collaboration-whitelist.js.map +1 -0
- package/lib/managers/collaborations.d.ts +166 -0
- package/lib/managers/collaborations.js +225 -258
- package/lib/managers/collaborations.js.map +1 -0
- package/lib/managers/collections.d.ts +42 -0
- package/lib/managers/collections.js +45 -50
- package/lib/managers/collections.js.map +1 -0
- package/lib/managers/comments.d.ts +103 -0
- package/lib/managers/comments.js +158 -173
- package/lib/managers/comments.js.map +1 -0
- package/lib/managers/device-pins.d.ts +52 -0
- package/lib/managers/device-pins.js +75 -88
- package/lib/managers/device-pins.js.map +1 -0
- package/lib/managers/enterprise.d.ts +162 -0
- package/lib/managers/enterprise.js +168 -199
- package/lib/managers/enterprise.js.map +1 -0
- package/lib/managers/events.d.ts +177 -0
- package/lib/managers/events.js +230 -254
- package/lib/managers/events.js.map +1 -0
- package/lib/managers/files.d.ts +772 -0
- package/lib/managers/files.js +1400 -1602
- package/lib/managers/files.js.map +1 -0
- package/lib/managers/folders.d.ts +347 -0
- package/lib/managers/folders.js +551 -567
- package/lib/managers/folders.js.map +1 -0
- package/lib/managers/groups.d.ts +202 -0
- package/lib/managers/groups.js +238 -287
- package/lib/managers/groups.js.map +1 -0
- package/lib/managers/legal-hold-policies.d.ts +190 -0
- package/lib/managers/legal-hold-policies.js +228 -272
- package/lib/managers/legal-hold-policies.js.map +1 -0
- package/lib/managers/metadata.d.ts +228 -0
- package/lib/managers/metadata.js +265 -328
- package/lib/managers/metadata.js.map +1 -0
- package/lib/managers/recent-items.d.ts +38 -0
- package/lib/managers/recent-items.js +32 -39
- package/lib/managers/recent-items.js.map +1 -0
- package/lib/managers/retention-policies.d.ts +213 -0
- package/lib/managers/retention-policies.js +235 -281
- package/lib/managers/retention-policies.js.map +1 -0
- package/lib/managers/search.d.ts +82 -0
- package/lib/managers/search.js +68 -88
- package/lib/managers/search.js.map +1 -0
- package/lib/managers/shared-items.d.ts +33 -0
- package/lib/managers/shared-items.js +54 -62
- package/lib/managers/shared-items.js.map +1 -0
- package/lib/managers/storage-policies.d.ts +86 -0
- package/lib/managers/storage-policies.js +108 -142
- package/lib/managers/storage-policies.js.map +1 -0
- package/lib/managers/tasks.d.ts +161 -0
- package/lib/managers/tasks.js +219 -260
- package/lib/managers/tasks.js.map +1 -0
- package/lib/managers/terms-of-service.d.ts +161 -0
- package/lib/managers/terms-of-service.js +250 -273
- package/lib/managers/terms-of-service.js.map +1 -0
- package/lib/managers/trash.d.ts +30 -0
- package/lib/managers/trash.js +30 -41
- package/lib/managers/trash.js.map +1 -0
- package/lib/managers/users.d.ts +131 -0
- package/lib/managers/users.js +160 -203
- package/lib/managers/users.js.map +1 -0
- package/lib/managers/web-links.d.ts +127 -0
- package/lib/managers/web-links.js +183 -209
- package/lib/managers/web-links.js.map +1 -0
- package/lib/managers/webhooks.d.ts +166 -0
- package/lib/managers/webhooks.js +312 -305
- package/lib/managers/webhooks.js.map +1 -0
- package/lib/sessions/anonymous-session.d.ts +69 -0
- package/lib/sessions/anonymous-session.js +88 -102
- package/lib/sessions/anonymous-session.js.map +1 -0
- package/lib/sessions/app-auth-session.d.ts +92 -0
- package/lib/sessions/app-auth-session.js +140 -160
- package/lib/sessions/app-auth-session.js.map +1 -0
- package/lib/sessions/basic-session.d.ts +56 -0
- package/lib/sessions/basic-session.js +40 -50
- package/lib/sessions/basic-session.js.map +1 -0
- package/lib/sessions/persistent-session.d.ts +96 -0
- package/lib/sessions/persistent-session.js +191 -211
- package/lib/sessions/persistent-session.js.map +1 -0
- package/lib/token-manager.d.ts +191 -0
- package/lib/token-manager.js +390 -465
- package/lib/token-manager.js.map +1 -0
- package/lib/util/config.d.ts +86 -0
- package/lib/util/config.js +124 -152
- package/lib/util/config.js.map +1 -0
- package/lib/util/errors.d.ts +50 -0
- package/lib/util/errors.js +134 -145
- package/lib/util/errors.js.map +1 -0
- package/lib/util/exponential-backoff.d.ts +11 -0
- package/lib/util/exponential-backoff.js +10 -22
- package/lib/util/exponential-backoff.js.map +1 -0
- package/lib/util/paging-iterator.d.ts +53 -0
- package/lib/util/paging-iterator.js +202 -218
- package/lib/util/paging-iterator.js.map +1 -0
- package/lib/util/url-path.d.ts +16 -0
- package/lib/util/url-path.js +20 -35
- package/lib/util/url-path.js.map +1 -0
- package/package.json +24 -9
package/lib/api-request.js
CHANGED
|
@@ -1,100 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* @fileoverview A Box API Request
|
|
3
4
|
*/
|
|
4
|
-
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
5
8
|
// @NOTE(fschott) 08/05/2014: THIS FILE SHOULD NOT BE ACCESSED DIRECTLY OUTSIDE OF API-REQUEST-MANAGER
|
|
6
9
|
// This module is used by APIRequestManager to make requests. If you'd like to make requests to the
|
|
7
10
|
// Box API, consider using APIRequestManager instead. {@Link APIRequestManager}
|
|
8
|
-
|
|
9
|
-
'use strict';
|
|
10
|
-
|
|
11
11
|
// ------------------------------------------------------------------------------
|
|
12
12
|
// Requirements
|
|
13
13
|
// ------------------------------------------------------------------------------
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// ------------------------------------------------------------------------------
|
|
22
|
-
// Typedefs and Callbacks
|
|
23
|
-
// ------------------------------------------------------------------------------
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The API response object includes information about the request made and its response. The information attached is a subset
|
|
27
|
-
* of the information returned by the request module, which is too large and complex to be safely handled (contains circular
|
|
28
|
-
* references, errors on serialization, etc.)
|
|
29
|
-
* @NOTE(fschott) 08-19-2014: We cannot return the request/response objects directly because they contain loads of extra
|
|
30
|
-
* information, unnecessary bloat, circular dependencies, and cause an infinite loop when stringifying.
|
|
31
|
-
*
|
|
32
|
-
* @typedef {Object} APIRequest~ResponseObject
|
|
33
|
-
* @property {APIRequest~RequestObject} request Information about the request that generated this response
|
|
34
|
-
* @property {int} statusCode The response HTTP status code
|
|
35
|
-
* @property {Object} headers A collection of response headers
|
|
36
|
-
* @property {Object|Buffer|string} [body] The response body. Encoded to JSON by default, but can be a buffer
|
|
37
|
-
* (if encoding fails or if json encoding is disabled) or a string (if string encoding is enabled). Will be undefined
|
|
38
|
-
* if no response body is sent.
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The API request object includes information about the request made. The information attached is a subset of the information
|
|
43
|
-
* of a request module instance, which is too large and complex to be safely handled (contains circular references, errors on
|
|
44
|
-
* serialization, etc.).
|
|
45
|
-
* @NOTE(fschott) 08-19-2014: We cannot return the request/response objects directly because they contain loads of extra
|
|
46
|
-
* information, unnecessary bloat, circular dependencies, and cause an infinite loop when stringifying.
|
|
47
|
-
*
|
|
48
|
-
* @typedef {Object} APIRequest~RequestObject
|
|
49
|
-
* @property {Object} uri Information about the request, including host, path, and the full 'href' url
|
|
50
|
-
* @property {string} method The request method (GET, POST, etc.)
|
|
51
|
-
* @property {Object} headers A collection of headers sent with the request
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The error returned by APIRequest callbacks, which includes any relevent, available information about the request
|
|
56
|
-
* and response. Note that these properties do not exist on stream errors, only errors retuned to the callback.
|
|
57
|
-
*
|
|
58
|
-
* @typedef {Error} APIRequest~Error
|
|
59
|
-
* @property {APIRequest~RequestObject} request Information about the request that generated this error
|
|
60
|
-
* @property {APIRequest~ResponseObject} [response] Information about the response related to this error, if available
|
|
61
|
-
* @property {int} [statusCode] The response HTTP status code
|
|
62
|
-
* @property {boolean} [maxRetriesExceeded] True iff the max number of retries were exceeded. Otherwise, undefined.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Callback invoked when an APIRequest request is complete and finalized. On success,
|
|
67
|
-
* propagates the relevent response information. An err will indicate an unresolvable issue
|
|
68
|
-
* with the request (permanent failure or temp error response from the server, retried too many times).
|
|
69
|
-
*
|
|
70
|
-
* @callback APIRequest~Callback
|
|
71
|
-
* @param {?APIRequest~Error} err If Error object, API request did not get back the data it was supposed to. This
|
|
72
|
-
* could be either because of a temporary error, or a more serious error connecting to the API.
|
|
73
|
-
* @param {APIRequest~ResponseObject} response The response returned by an APIRequestManager request
|
|
74
|
-
*/
|
|
75
|
-
|
|
76
|
-
|
|
14
|
+
var assert_1 = __importDefault(require("assert"));
|
|
15
|
+
var events_1 = require("events");
|
|
16
|
+
var http_status_1 = __importDefault(require("http-status"));
|
|
17
|
+
var request_1 = __importDefault(require("request"));
|
|
18
|
+
var config_1 = __importDefault(require("./util/config"));
|
|
19
|
+
var exponential_backoff_1 = __importDefault(require("./util/exponential-backoff"));
|
|
77
20
|
// ------------------------------------------------------------------------------
|
|
78
21
|
// Private
|
|
79
22
|
// ------------------------------------------------------------------------------
|
|
80
|
-
|
|
81
23
|
// Message to replace removed headers with in the request
|
|
82
24
|
var REMOVED_HEADER_MESSAGE = '[REMOVED BY SDK]';
|
|
83
|
-
|
|
84
25
|
// Range of SERVER ERROR http status codes
|
|
85
|
-
var HTTP_STATUS_CODE_SERVER_ERROR_BLOCK_RANGE = [
|
|
86
|
-
500,
|
|
87
|
-
599
|
|
88
|
-
];
|
|
89
|
-
|
|
26
|
+
var HTTP_STATUS_CODE_SERVER_ERROR_BLOCK_RANGE = [500, 599];
|
|
90
27
|
// Timer used to track elapsed time beginning from executing an async request to emitting the response.
|
|
91
28
|
var asyncRequestTimer;
|
|
92
|
-
|
|
93
29
|
// A map of HTTP status codes and whether or not they can be retried
|
|
94
30
|
var retryableStatusCodes = {};
|
|
95
|
-
retryableStatusCodes[
|
|
96
|
-
retryableStatusCodes[
|
|
97
|
-
|
|
31
|
+
retryableStatusCodes[http_status_1.default.REQUEST_TIMEOUT] = true;
|
|
32
|
+
retryableStatusCodes[http_status_1.default.TOO_MANY_REQUESTS] = true;
|
|
98
33
|
/**
|
|
99
34
|
* Returns true if the response info indicates a temporary/transient error.
|
|
100
35
|
*
|
|
@@ -105,25 +40,21 @@ retryableStatusCodes[httpStatusCodes.TOO_MANY_REQUESTS] = true;
|
|
|
105
40
|
* @private
|
|
106
41
|
*/
|
|
107
42
|
function isTemporaryError(response) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return false;
|
|
43
|
+
var statusCode = response.statusCode;
|
|
44
|
+
// An API error is a temporary/transient if it returns a 5xx HTTP Status, with the exception of the 507 status.
|
|
45
|
+
// The API returns a 507 error when the user has run out of account space, in which case, it should be treated
|
|
46
|
+
// as a permanent, non-retryable error.
|
|
47
|
+
if (statusCode !== http_status_1.default.INSUFFICIENT_STORAGE &&
|
|
48
|
+
statusCode >= HTTP_STATUS_CODE_SERVER_ERROR_BLOCK_RANGE[0] &&
|
|
49
|
+
statusCode <= HTTP_STATUS_CODE_SERVER_ERROR_BLOCK_RANGE[1]) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
// An API error is a temporary/transient error if it returns a HTTP Status that indicates it is a temporary,
|
|
53
|
+
if (retryableStatusCodes[statusCode]) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
125
57
|
}
|
|
126
|
-
|
|
127
58
|
/**
|
|
128
59
|
* Determine whether a given request can be retried, based on its options
|
|
129
60
|
* @param {Object} options The request options
|
|
@@ -131,9 +62,8 @@ function isTemporaryError(response) {
|
|
|
131
62
|
* @private
|
|
132
63
|
*/
|
|
133
64
|
function isRequestRetryable(options) {
|
|
134
|
-
|
|
65
|
+
return !options.formData;
|
|
135
66
|
}
|
|
136
|
-
|
|
137
67
|
/**
|
|
138
68
|
* Clean sensitive headers from the request object. This prevents this data from
|
|
139
69
|
* propagating out to the SDK and getting unintentionally logged via the error or
|
|
@@ -145,20 +75,18 @@ function isRequestRetryable(options) {
|
|
|
145
75
|
* @private
|
|
146
76
|
*/
|
|
147
77
|
function cleanSensitiveHeaders(requestObj) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
78
|
+
if (requestObj.headers) {
|
|
79
|
+
if (requestObj.headers.BoxApi) {
|
|
80
|
+
requestObj.headers.BoxApi = REMOVED_HEADER_MESSAGE;
|
|
81
|
+
}
|
|
82
|
+
if (requestObj.headers.Authorization) {
|
|
83
|
+
requestObj.headers.Authorization = REMOVED_HEADER_MESSAGE;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
156
86
|
}
|
|
157
|
-
|
|
158
87
|
// ------------------------------------------------------------------------------
|
|
159
88
|
// Public
|
|
160
89
|
// ------------------------------------------------------------------------------
|
|
161
|
-
|
|
162
90
|
/**
|
|
163
91
|
* APIRequest helps to prepare and execute requests to the Box API. It supports
|
|
164
92
|
* retries, multipart uploads, and more.
|
|
@@ -168,177 +96,170 @@ function cleanSensitiveHeaders(requestObj) {
|
|
|
168
96
|
* @param {EventEmitter} eventBus Event bus for the SDK instance
|
|
169
97
|
* @constructor
|
|
170
98
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
APIRequest.prototype.execute = function(callback) {
|
|
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
|
-
APIRequest.prototype._handleResponse = function(err
|
|
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
|
-
* @module box-node-sdk/lib/api-request
|
|
342
|
-
* @see {@Link APIRequest}
|
|
343
|
-
*/
|
|
99
|
+
var APIRequest = /** @class */ (function () {
|
|
100
|
+
function APIRequest(config, eventBus) {
|
|
101
|
+
assert_1.default(config instanceof config_1.default, 'Config must be passed to APIRequest constructor');
|
|
102
|
+
assert_1.default(eventBus instanceof events_1.EventEmitter, 'Valid event bus must be passed to APIRequest constructor');
|
|
103
|
+
this.config = config;
|
|
104
|
+
this.eventBus = eventBus;
|
|
105
|
+
this.isRetryable = isRequestRetryable(config.request);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Executes the request with the given options. If a callback is provided, we'll
|
|
109
|
+
* handle the response via callbacks. Otherwise, the response will be streamed to
|
|
110
|
+
* via the stream property. You can access this stream with the getResponseStream()
|
|
111
|
+
* method.
|
|
112
|
+
*
|
|
113
|
+
* @param {APIRequest~Callback} [callback] Callback for handling the response
|
|
114
|
+
* @returns {void}
|
|
115
|
+
*/
|
|
116
|
+
APIRequest.prototype.execute = function (callback) {
|
|
117
|
+
var _this = this;
|
|
118
|
+
this._callback = callback || this._callback;
|
|
119
|
+
// Initiate an async- or stream-based request, based on the presence of the callback.
|
|
120
|
+
if (this._callback) {
|
|
121
|
+
// Start the request timer immediately before executing the async request
|
|
122
|
+
if (!asyncRequestTimer) {
|
|
123
|
+
asyncRequestTimer = process.hrtime();
|
|
124
|
+
}
|
|
125
|
+
this.request = request_1.default(this.config.request, this._handleResponse.bind(this));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.request = request_1.default(this.config.request);
|
|
129
|
+
this.stream = this.request;
|
|
130
|
+
this.stream.on('error', function (err) { return _this.eventBus.emit('response', err); });
|
|
131
|
+
this.stream.on('response', function (response) {
|
|
132
|
+
return _this.eventBus.emit('response', null, response);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Return the response read stream for a request. This will be undefined until
|
|
138
|
+
* a stream-based request has been started.
|
|
139
|
+
*
|
|
140
|
+
* @returns {?ReadableStream} The response stream
|
|
141
|
+
*/
|
|
142
|
+
APIRequest.prototype.getResponseStream = function () {
|
|
143
|
+
return this.stream;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Handle the request response in the callback case.
|
|
147
|
+
*
|
|
148
|
+
* @param {?Error} err An error, if one occurred
|
|
149
|
+
* @param {Object} [response] The full response object, returned by the request module.
|
|
150
|
+
* Contains information about the request & response, including the response body itself.
|
|
151
|
+
* @returns {void}
|
|
152
|
+
* @private
|
|
153
|
+
*/
|
|
154
|
+
APIRequest.prototype._handleResponse = function (err /* FIXME */, response /* FIXME */) {
|
|
155
|
+
// Clean sensitive headers here to prevent the user from accidentily using/logging them in prod
|
|
156
|
+
cleanSensitiveHeaders(this.request);
|
|
157
|
+
// If the API connected successfully but responded with a temporary error (like a 5xx code,
|
|
158
|
+
// a rate limited response, etc.) then this is considered an error as well.
|
|
159
|
+
if (!err && isTemporaryError(response)) {
|
|
160
|
+
var errorMessage = response.statusCode + " - " + http_status_1.default[response.statusCode];
|
|
161
|
+
err = new Error(errorMessage);
|
|
162
|
+
}
|
|
163
|
+
if (err) {
|
|
164
|
+
// Attach request & response information to the error object
|
|
165
|
+
err.request = this.request;
|
|
166
|
+
if (response) {
|
|
167
|
+
err.response = response;
|
|
168
|
+
err.statusCode = response.statusCode;
|
|
169
|
+
}
|
|
170
|
+
// Have the SDK emit the error response
|
|
171
|
+
this.eventBus.emit('response', err);
|
|
172
|
+
var isJWT = false;
|
|
173
|
+
if (this.config.request.hasOwnProperty('form') &&
|
|
174
|
+
this.config.request.form.hasOwnProperty('grant_type') &&
|
|
175
|
+
this.config.request.form.grant_type ===
|
|
176
|
+
'urn:ietf:params:oauth:grant-type:jwt-bearer') {
|
|
177
|
+
isJWT = true;
|
|
178
|
+
}
|
|
179
|
+
// If our APIRequest instance is retryable, attempt a retry. Otherwise, finish and propagate the error. Doesn't retry when the request is for JWT authentication, since that is handled in retryJWTGrant.
|
|
180
|
+
if (this.isRetryable && !isJWT) {
|
|
181
|
+
this._retry(err);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
this._finish(err);
|
|
185
|
+
}
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
// If the request was successful, emit & propagate the response!
|
|
189
|
+
this.eventBus.emit('response', null, response);
|
|
190
|
+
this._finish(null, response);
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* Attempt a retry. If the request hasn't exceeded it's maximum number of retries,
|
|
194
|
+
* re-execute the request (after the retry interval). Otherwise, propagate a new error.
|
|
195
|
+
*
|
|
196
|
+
* @param {?Error} err An error, if one occurred
|
|
197
|
+
* @returns {void}
|
|
198
|
+
* @private
|
|
199
|
+
*/
|
|
200
|
+
APIRequest.prototype._retry = function (err /* FIXME */) {
|
|
201
|
+
this.numRetries = this.numRetries || 0;
|
|
202
|
+
if (this.numRetries < this.config.numMaxRetries) {
|
|
203
|
+
var retryTimeout;
|
|
204
|
+
this.numRetries += 1;
|
|
205
|
+
// If the retry strategy is defined, then use it to determine the time (in ms) until the next retry or to
|
|
206
|
+
// propagate an error to the user.
|
|
207
|
+
if (this.config.retryStrategy) {
|
|
208
|
+
// Get the total elapsed time so far since the request was executed
|
|
209
|
+
var totalElapsedTime = process.hrtime(asyncRequestTimer);
|
|
210
|
+
var totalElapsedTimeMS = totalElapsedTime[0] * 1000 + totalElapsedTime[1] / 1000000;
|
|
211
|
+
var retryOptions = {
|
|
212
|
+
error: err,
|
|
213
|
+
numRetryAttempts: this.numRetries,
|
|
214
|
+
numMaxRetries: this.config.numMaxRetries,
|
|
215
|
+
retryIntervalMS: this.config.retryIntervalMS,
|
|
216
|
+
totalElapsedTimeMS: totalElapsedTimeMS,
|
|
217
|
+
};
|
|
218
|
+
retryTimeout = this.config.retryStrategy(retryOptions);
|
|
219
|
+
// If the retry strategy doesn't return a number/time in ms, then propagate the response error to the user.
|
|
220
|
+
// However, if the retry strategy returns its own error, this will be propagated to the user instead.
|
|
221
|
+
if (typeof retryTimeout !== 'number') {
|
|
222
|
+
if (retryTimeout instanceof Error) {
|
|
223
|
+
err = retryTimeout;
|
|
224
|
+
}
|
|
225
|
+
this._finish(err);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else if (err.hasOwnProperty('response') &&
|
|
230
|
+
err.response.hasOwnProperty('headers') &&
|
|
231
|
+
err.response.headers.hasOwnProperty('retry-after')) {
|
|
232
|
+
retryTimeout = err.response.headers['retry-after'] * 1000;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
retryTimeout = exponential_backoff_1.default(this.numRetries, this.config.retryIntervalMS);
|
|
236
|
+
}
|
|
237
|
+
setTimeout(this.execute.bind(this), retryTimeout);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
err.maxRetriesExceeded = true;
|
|
241
|
+
this._finish(err);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* Propagate the response to the provided callback.
|
|
246
|
+
*
|
|
247
|
+
* @param {?Error} err An error, if one occurred
|
|
248
|
+
* @param {APIRequest~ResponseObject} response Information about the request & response
|
|
249
|
+
* @returns {void}
|
|
250
|
+
* @private
|
|
251
|
+
*/
|
|
252
|
+
APIRequest.prototype._finish = function (err, response) {
|
|
253
|
+
var callback = this._callback;
|
|
254
|
+
process.nextTick(function () {
|
|
255
|
+
if (err) {
|
|
256
|
+
callback(err);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
callback(null, response);
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
return APIRequest;
|
|
263
|
+
}());
|
|
344
264
|
module.exports = APIRequest;
|
|
265
|
+
//# sourceMappingURL=api-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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