@commercetools/ts-client 0.0.0-beta.8 → 1.0.0
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 +24 -0
- package/LICENSE +1 -1
- package/dist/commercetools-ts-client.browser.cjs.js +445 -348
- package/dist/commercetools-ts-client.browser.esm.js +443 -347
- package/dist/commercetools-ts-client.cjs.dev.js +445 -348
- package/dist/commercetools-ts-client.cjs.prod.js +445 -348
- package/dist/commercetools-ts-client.esm.js +443 -347
- package/dist/declarations/src/client/builder.d.ts +1 -3
- package/dist/declarations/src/client/client.d.ts +2 -5
- package/dist/declarations/src/middleware/index.d.ts +0 -1
- package/dist/declarations/src/types/types.d.ts +18 -20
- package/dist/declarations/src/utils/createError.d.ts +8 -0
- package/dist/declarations/src/utils/errors.d.ts +10 -0
- package/dist/declarations/src/utils/executor.d.ts +1 -1
- package/dist/declarations/src/utils/index.d.ts +3 -2
- package/dist/declarations/src/utils/maskAuthData.d.ts +1 -1
- package/dist/declarations/src/utils/retryDelay.d.ts +1 -1
- package/dist/declarations/src/utils/validate.d.ts +1 -6
- package/package.json +5 -8
- package/dist/declarations/src/middleware/create-retry-middleware.d.ts +0 -2
- package/dist/declarations/src/utils/logger.d.ts +0 -2
|
@@ -6,12 +6,14 @@ var qs = require('querystring');
|
|
|
6
6
|
var crytpo = require('crypto');
|
|
7
7
|
var fetch$1 = require('node-fetch');
|
|
8
8
|
var _ = require('buffer/');
|
|
9
|
+
var AbortController = require('abort-controller');
|
|
9
10
|
|
|
10
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
11
12
|
|
|
12
13
|
var qs__default = /*#__PURE__*/_interopDefault(qs);
|
|
13
14
|
var crytpo__default = /*#__PURE__*/_interopDefault(crytpo);
|
|
14
15
|
var fetch__default = /*#__PURE__*/_interopDefault(fetch$1);
|
|
16
|
+
var AbortController__default = /*#__PURE__*/_interopDefault(AbortController);
|
|
15
17
|
|
|
16
18
|
const HEADERS_CONTENT_TYPES = ['application/json', 'application/graphql'];
|
|
17
19
|
const CONCURRENCT_REQUEST = 20;
|
|
@@ -26,13 +28,14 @@ function parse(headers) {
|
|
|
26
28
|
return result;
|
|
27
29
|
}, {});
|
|
28
30
|
}
|
|
29
|
-
|
|
30
31
|
function getHeaders(headers) {
|
|
31
|
-
if (!headers) return null;
|
|
32
|
+
if (!headers) return null;
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
// node-fetch
|
|
35
|
+
if (headers.raw && typeof headers.raw == 'function') return headers.raw();
|
|
34
36
|
|
|
35
|
-
|
|
37
|
+
// Tmp fix for Firefox until it supports iterables
|
|
38
|
+
if (!headers.forEach) return parse(headers);
|
|
36
39
|
return headers.forEach((value, name) => value);
|
|
37
40
|
}
|
|
38
41
|
|
|
@@ -51,14 +54,16 @@ function calculateRetryDelay({
|
|
|
51
54
|
return retryCount !== 0 // do not increase if it's the first retry
|
|
52
55
|
? Math.min(Math.round((Math.random() + 1) * retryDelay * 2 ** retryCount), maxDelay) : retryDelay;
|
|
53
56
|
}
|
|
54
|
-
|
|
55
57
|
return retryDelay;
|
|
56
58
|
}
|
|
57
59
|
|
|
60
|
+
// TODO: Polyfill crypto for browsers
|
|
58
61
|
function generateID() {
|
|
59
62
|
return crytpo__default["default"].randomBytes(32).toString('base64').replace(/[\/\-=+]/gi, '');
|
|
60
63
|
}
|
|
61
64
|
|
|
65
|
+
// import { validateUserAgentOptions } from '../utils'
|
|
66
|
+
|
|
62
67
|
/*
|
|
63
68
|
This is the easiest way, for this use case, to detect if we're running in
|
|
64
69
|
Node.js or in a browser environment. In other cases, this won't be even a
|
|
@@ -68,68 +73,73 @@ function generateID() {
|
|
|
68
73
|
the specific test.
|
|
69
74
|
*/
|
|
70
75
|
const isBrowser = () => typeof window !== 'undefined' && window.document && window.document.nodeType === 9;
|
|
71
|
-
|
|
72
76
|
function getSystemInfo() {
|
|
77
|
+
var _process;
|
|
73
78
|
if (isBrowser()) return window.navigator.userAgent;
|
|
74
|
-
const nodeVersion = process
|
|
75
|
-
|
|
76
|
-
const platformInfo = `(${process.platform}; ${process.arch})`; // return `node.js/${nodeVersion}`
|
|
79
|
+
const nodeVersion = ((_process = process) === null || _process === void 0 ? void 0 : _process.version.slice(1)) || 'unknow'; // unknow environment like React Native etc
|
|
80
|
+
const platformInfo = `(${process.platform}; ${process.arch})`;
|
|
77
81
|
|
|
82
|
+
// return `node.js/${nodeVersion}`
|
|
78
83
|
return `node.js/${nodeVersion} ${platformInfo}`;
|
|
79
84
|
}
|
|
80
|
-
|
|
81
85
|
function createUserAgent(options) {
|
|
82
86
|
let libraryInfo = null;
|
|
83
87
|
let contactInfo = null;
|
|
84
88
|
|
|
89
|
+
// validateUserAgentOptions(options)
|
|
85
90
|
if (!options) {
|
|
86
91
|
throw new Error('Missing required option `name`');
|
|
87
|
-
}
|
|
88
|
-
|
|
92
|
+
}
|
|
89
93
|
|
|
90
|
-
|
|
94
|
+
// Main info
|
|
95
|
+
const baseInfo = options.version ? `${options.name}/${options.version}` : options.name;
|
|
91
96
|
|
|
97
|
+
// Library info
|
|
92
98
|
if (options.libraryName && !options.libraryVersion) {
|
|
93
99
|
libraryInfo = options.libraryName;
|
|
94
100
|
} else if (options.libraryName && options.libraryVersion) {
|
|
95
101
|
libraryInfo = `${options.libraryName}/${options.libraryVersion}`;
|
|
96
|
-
}
|
|
97
|
-
|
|
102
|
+
}
|
|
98
103
|
|
|
104
|
+
// Contact info
|
|
99
105
|
if (options.contactUrl && !options.contactEmail) {
|
|
100
106
|
contactInfo = `(+${options.contactUrl})`;
|
|
101
107
|
} else if (!options.contactUrl && options.contactEmail) {
|
|
102
108
|
contactInfo = `(+${options.contactEmail})`;
|
|
103
109
|
} else if (options.contactUrl && options.contactEmail) {
|
|
104
110
|
contactInfo = `(+${options.contactUrl}; +${options.contactEmail})`;
|
|
105
|
-
}
|
|
106
|
-
|
|
111
|
+
}
|
|
107
112
|
|
|
108
|
-
|
|
113
|
+
// System info
|
|
114
|
+
const systemInfo = getSystemInfo();
|
|
109
115
|
|
|
116
|
+
// customName
|
|
110
117
|
const customAgent = options.customAgent || '';
|
|
111
118
|
return [baseInfo, systemInfo, libraryInfo, contactInfo, customAgent].filter(Boolean).join(' ');
|
|
112
119
|
}
|
|
113
120
|
|
|
114
121
|
function maskAuthData(request) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
const _request = Object.assign({}, request);
|
|
123
|
+
if (_request !== null && _request !== void 0 && _request.headers) {
|
|
124
|
+
if (_request.headers.Authorization) {
|
|
125
|
+
_request.headers['Authorization'] = 'Bearer ********';
|
|
118
126
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
request.headers['authorization'] = 'Bearer ********';
|
|
127
|
+
if (_request.headers.authorization) {
|
|
128
|
+
_request.headers['authorization'] = 'Bearer ********';
|
|
122
129
|
}
|
|
123
130
|
}
|
|
131
|
+
return _request;
|
|
124
132
|
}
|
|
125
133
|
|
|
126
134
|
function calculateExpirationTime(expiresIn) {
|
|
127
|
-
return Date.now() +
|
|
135
|
+
return Date.now() +
|
|
136
|
+
// Add a gap of 5 minutes before expiration time.
|
|
128
137
|
expiresIn * 1000 - 5 * 60 * 1000;
|
|
129
138
|
}
|
|
130
139
|
|
|
131
140
|
function buildTokenCacheKey(options) {
|
|
132
|
-
|
|
141
|
+
var _options$credentials;
|
|
142
|
+
if (!(options !== null && options !== void 0 && (_options$credentials = options.credentials) !== null && _options$credentials !== void 0 && _options$credentials.clientId) || !options.projectKey || !options.host) throw new Error('Missing required options.');
|
|
133
143
|
return {
|
|
134
144
|
clientId: options.credentials.clientId,
|
|
135
145
|
host: options.host,
|
|
@@ -148,34 +158,36 @@ function store(initVal) {
|
|
|
148
158
|
}
|
|
149
159
|
|
|
150
160
|
function mergeAuthHeader(token, req) {
|
|
151
|
-
return {
|
|
152
|
-
|
|
161
|
+
return {
|
|
162
|
+
...req,
|
|
163
|
+
headers: {
|
|
164
|
+
...req.headers,
|
|
153
165
|
Authorization: `Bearer ${token}`
|
|
154
166
|
}
|
|
155
167
|
};
|
|
156
168
|
}
|
|
157
169
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
//
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
function predicate(retryCodes, response) {
|
|
171
|
+
return !
|
|
172
|
+
// retryCodes.includes(response?.error?.message) ||
|
|
173
|
+
[503, ...retryCodes].includes((response === null || response === void 0 ? void 0 : response.status) || (response === null || response === void 0 ? void 0 : response.statusCode));
|
|
174
|
+
}
|
|
163
175
|
async function executeHttpClientRequest(fetcher, config) {
|
|
164
176
|
async function sendRequest() {
|
|
165
|
-
const response = await fetcher({
|
|
166
|
-
|
|
177
|
+
const response = await fetcher({
|
|
178
|
+
...config,
|
|
179
|
+
headers: {
|
|
180
|
+
...config.headers
|
|
167
181
|
}
|
|
168
|
-
});
|
|
182
|
+
});
|
|
169
183
|
|
|
184
|
+
// validations and error handlings can also be done here
|
|
170
185
|
return response;
|
|
171
|
-
}
|
|
172
|
-
|
|
186
|
+
}
|
|
173
187
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
});
|
|
188
|
+
// Attempt to send the request.
|
|
189
|
+
return sendRequest().catch(error => Promise.reject(error));
|
|
177
190
|
}
|
|
178
|
-
|
|
179
191
|
async function executor(request) {
|
|
180
192
|
const {
|
|
181
193
|
url,
|
|
@@ -183,22 +195,79 @@ async function executor(request) {
|
|
|
183
195
|
...rest
|
|
184
196
|
} = request;
|
|
185
197
|
const data = await executeHttpClientRequest(async options => {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
198
|
+
const {
|
|
199
|
+
enableRetry,
|
|
200
|
+
retryConfig
|
|
201
|
+
} = rest;
|
|
202
|
+
const {
|
|
203
|
+
retryCodes = [],
|
|
204
|
+
maxDelay = Infinity,
|
|
205
|
+
maxRetries = 3,
|
|
206
|
+
backoff = true,
|
|
207
|
+
retryDelay = 200
|
|
208
|
+
} = retryConfig || {};
|
|
209
|
+
let result,
|
|
210
|
+
data,
|
|
211
|
+
retryCount = 0;
|
|
212
|
+
|
|
213
|
+
// validate the `retryCodes` option
|
|
214
|
+
validateRetryCodes(retryCodes);
|
|
215
|
+
async function execute() {
|
|
216
|
+
return httpClient(url, {
|
|
217
|
+
...rest,
|
|
218
|
+
...options,
|
|
219
|
+
headers: {
|
|
220
|
+
...rest.headers,
|
|
221
|
+
...options.headers,
|
|
222
|
+
// axios header encoding
|
|
223
|
+
'Accept-Encoding': 'application/json'
|
|
224
|
+
},
|
|
225
|
+
// for axios
|
|
226
|
+
...(rest.body ? {
|
|
227
|
+
data: rest.body
|
|
228
|
+
} : {}),
|
|
229
|
+
withCredentials: options.credentialsMode === 'include'
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async function executeWithRetry() {
|
|
233
|
+
// first attempt
|
|
234
|
+
let _response = await execute();
|
|
235
|
+
if (predicate(retryCodes, _response)) return _response;
|
|
236
|
+
|
|
237
|
+
// retry attempts
|
|
238
|
+
while (enableRetry && retryCount < maxRetries) {
|
|
239
|
+
retryCount++;
|
|
240
|
+
_response = await execute();
|
|
241
|
+
if (predicate(retryCodes, _response)) return _response;
|
|
242
|
+
|
|
243
|
+
// delay next execution
|
|
244
|
+
const timer = calculateRetryDelay({
|
|
245
|
+
retryCount,
|
|
246
|
+
retryDelay,
|
|
247
|
+
maxRetries,
|
|
248
|
+
backoff,
|
|
249
|
+
maxDelay
|
|
250
|
+
});
|
|
251
|
+
await sleep(timer);
|
|
252
|
+
}
|
|
253
|
+
return _response;
|
|
254
|
+
}
|
|
255
|
+
const response = await executeWithRetry();
|
|
256
|
+
try {
|
|
257
|
+
// try to parse the `fetch` response as text
|
|
258
|
+
if (response.text && typeof response.text == 'function') {
|
|
259
|
+
result = await response.text();
|
|
260
|
+
data = JSON.parse(result);
|
|
261
|
+
} else {
|
|
262
|
+
// axios response
|
|
263
|
+
data = response.data || response;
|
|
264
|
+
}
|
|
265
|
+
} catch (err) {
|
|
266
|
+
data = result;
|
|
267
|
+
}
|
|
200
268
|
return {
|
|
201
269
|
data,
|
|
270
|
+
retryCount,
|
|
202
271
|
statusCode: response.status || response.statusCode || data.statusCode,
|
|
203
272
|
headers: response.headers
|
|
204
273
|
};
|
|
@@ -220,6 +289,79 @@ function sleep(ms) {
|
|
|
220
289
|
|
|
221
290
|
var METHODS = ['ACL', 'BIND', 'CHECKOUT', 'CONNECT', 'COPY', 'DELETE', 'GET', 'HEAD', 'LINK', 'LOCK', 'M-SEARCH', 'MERGE', 'MKACTIVITY', 'MKCALENDAR', 'MKCOL', 'MOVE', 'NOTIFY', 'OPTIONS', 'PATCH', 'POST', 'PROPFIND', 'PROPPATCH', 'PURGE', 'PUT', 'REBIND', 'REPORT', 'SEARCH', 'SOURCE', 'SUBSCRIBE', 'TRACE', 'UNBIND', 'UNLINK', 'UNLOCK', 'UNSUBSCRIBE'];
|
|
222
291
|
|
|
292
|
+
function DefineError(statusCode, message, meta = {}) {
|
|
293
|
+
// eslint-disable-next-line no-multi-assign
|
|
294
|
+
this.status = this.statusCode = this.code = statusCode;
|
|
295
|
+
this.message = message;
|
|
296
|
+
Object.assign(this, meta);
|
|
297
|
+
this.name = this.constructor.name;
|
|
298
|
+
// eslint-disable-next-line no-proto
|
|
299
|
+
this.constructor.prototype.__proto__ = Error.prototype;
|
|
300
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor);
|
|
301
|
+
}
|
|
302
|
+
function NetworkError(...args) {
|
|
303
|
+
DefineError.call(this, 0, ...args);
|
|
304
|
+
}
|
|
305
|
+
function HttpError(...args) {
|
|
306
|
+
DefineError.call(this, ...args);
|
|
307
|
+
}
|
|
308
|
+
function BadRequest(...args) {
|
|
309
|
+
DefineError.call(this, 400, ...args);
|
|
310
|
+
}
|
|
311
|
+
function Unauthorized(...args) {
|
|
312
|
+
DefineError.call(this, 401, ...args);
|
|
313
|
+
}
|
|
314
|
+
function Forbidden(...args) {
|
|
315
|
+
DefineError.call(this, 403, ...args);
|
|
316
|
+
}
|
|
317
|
+
function NotFound(...args) {
|
|
318
|
+
DefineError.call(this, 404, ...args);
|
|
319
|
+
}
|
|
320
|
+
function ConcurrentModification(...args) {
|
|
321
|
+
DefineError.call(this, 409, ...args);
|
|
322
|
+
}
|
|
323
|
+
function InternalServerError(...args) {
|
|
324
|
+
DefineError.call(this, 500, ...args);
|
|
325
|
+
}
|
|
326
|
+
function ServiceUnavailable(...args) {
|
|
327
|
+
DefineError.call(this, 503, ...args);
|
|
328
|
+
}
|
|
329
|
+
function getErrorByCode(code) {
|
|
330
|
+
switch (code) {
|
|
331
|
+
case 0:
|
|
332
|
+
return NetworkError;
|
|
333
|
+
case 400:
|
|
334
|
+
return BadRequest;
|
|
335
|
+
case 401:
|
|
336
|
+
return Unauthorized;
|
|
337
|
+
case 403:
|
|
338
|
+
return Forbidden;
|
|
339
|
+
case 404:
|
|
340
|
+
return NotFound;
|
|
341
|
+
case 409:
|
|
342
|
+
return ConcurrentModification;
|
|
343
|
+
case 500:
|
|
344
|
+
return InternalServerError;
|
|
345
|
+
case 503:
|
|
346
|
+
return ServiceUnavailable;
|
|
347
|
+
default:
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function createError({
|
|
353
|
+
statusCode,
|
|
354
|
+
message,
|
|
355
|
+
...rest
|
|
356
|
+
}) {
|
|
357
|
+
var _rest$originalRequest;
|
|
358
|
+
let errorMessage = message || 'Unexpected non-JSON error response';
|
|
359
|
+
if (statusCode === 404) errorMessage = `URI not found: ${((_rest$originalRequest = rest.originalRequest) === null || _rest$originalRequest === void 0 ? void 0 : _rest$originalRequest.uri) || rest.uri}`;
|
|
360
|
+
const ResponseError = getErrorByCode(statusCode);
|
|
361
|
+
if (ResponseError) return new ResponseError(errorMessage, rest);
|
|
362
|
+
return new HttpError(statusCode, errorMessage, rest);
|
|
363
|
+
}
|
|
364
|
+
|
|
223
365
|
/**
|
|
224
366
|
* validate some essential http options
|
|
225
367
|
* @param options
|
|
@@ -229,34 +371,33 @@ function validateHttpOptions(options) {
|
|
|
229
371
|
if (!options.httpClient && typeof options.httpClient !== 'function') throw new Error('An `httpClient` is not available, please pass in a `fetch` or `axios` instance as an option or have them globally available.');
|
|
230
372
|
if (options.timeout && !options.getAbortController) throw new Error('`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.');
|
|
231
373
|
}
|
|
374
|
+
|
|
232
375
|
/**
|
|
233
376
|
*
|
|
234
377
|
* @param retryCodes
|
|
235
378
|
* @example
|
|
236
379
|
* const retryCodes = [500, 504, "ETIMEDOUT"]
|
|
237
380
|
*/
|
|
238
|
-
|
|
239
381
|
function validateRetryCodes(retryCodes) {
|
|
240
382
|
if (!Array.isArray(retryCodes)) {
|
|
241
383
|
throw new Error('`retryCodes` option must be an array of retry status (error) codes and/or messages.');
|
|
242
384
|
}
|
|
243
385
|
}
|
|
386
|
+
|
|
244
387
|
/**
|
|
245
388
|
* @param options
|
|
246
389
|
*/
|
|
247
|
-
|
|
248
390
|
function validateClient(options) {
|
|
249
391
|
if (!options) throw new Error('Missing required options');
|
|
250
392
|
if (options.middlewares && !Array.isArray(options.middlewares)) throw new Error('Middlewares should be an array');
|
|
251
|
-
|
|
252
393
|
if (!options.middlewares || !Array.isArray(options.middlewares) || !options.middlewares.length) {
|
|
253
394
|
throw new Error('You need to provide at least one middleware');
|
|
254
395
|
}
|
|
255
396
|
}
|
|
397
|
+
|
|
256
398
|
/**
|
|
257
399
|
* @param options
|
|
258
400
|
*/
|
|
259
|
-
|
|
260
401
|
function validate(funcName, request, options = {
|
|
261
402
|
allowedMethods: METHODS
|
|
262
403
|
}) {
|
|
@@ -269,21 +410,19 @@ function compose({
|
|
|
269
410
|
middlewares
|
|
270
411
|
}) {
|
|
271
412
|
if (middlewares.length === 1) return middlewares[0];
|
|
272
|
-
|
|
273
413
|
const _middlewares = middlewares.slice();
|
|
274
|
-
|
|
275
414
|
return _middlewares.reduce((ac, cv) => (...args) => ac(cv.apply(null, args)));
|
|
276
|
-
}
|
|
277
|
-
|
|
415
|
+
}
|
|
278
416
|
|
|
417
|
+
// process batch requests
|
|
279
418
|
let _options;
|
|
280
|
-
|
|
281
419
|
function process$1(request, fn, processOpt) {
|
|
282
420
|
validate('process', request, {
|
|
283
421
|
allowedMethods: ['GET']
|
|
284
422
|
});
|
|
285
|
-
if (typeof fn !== 'function') throw new Error('The "process" function accepts a "Function" as a second argument that returns a Promise. See https://commercetools.github.io/nodejs/sdk/api/sdkClient.html#processrequest-processfn-options');
|
|
423
|
+
if (typeof fn !== 'function') throw new Error('The "process" function accepts a "Function" as a second argument that returns a Promise. See https://commercetools.github.io/nodejs/sdk/api/sdkClient.html#processrequest-processfn-options');
|
|
286
424
|
|
|
425
|
+
// Set default process options
|
|
287
426
|
const opt = {
|
|
288
427
|
total: Number.POSITIVE_INFINITY,
|
|
289
428
|
accumulate: true,
|
|
@@ -291,15 +430,14 @@ function process$1(request, fn, processOpt) {
|
|
|
291
430
|
};
|
|
292
431
|
return new Promise((resolve, reject) => {
|
|
293
432
|
let _path,
|
|
294
|
-
|
|
295
|
-
|
|
433
|
+
_queryString = '';
|
|
296
434
|
if (request && request.uri) {
|
|
297
435
|
const [path, queryString] = request.uri.split('?');
|
|
298
436
|
_path = path;
|
|
299
437
|
_queryString = queryString;
|
|
300
438
|
}
|
|
301
|
-
|
|
302
|
-
|
|
439
|
+
const requestQuery = {
|
|
440
|
+
...qs__default["default"].parse(_queryString)
|
|
303
441
|
};
|
|
304
442
|
const query = {
|
|
305
443
|
// defaults
|
|
@@ -309,11 +447,11 @@ function process$1(request, fn, processOpt) {
|
|
|
309
447
|
};
|
|
310
448
|
let itemsToGet = opt.total;
|
|
311
449
|
let hasFirstPageBeenProcessed = false;
|
|
312
|
-
|
|
313
450
|
const processPage = async (lastId, acc = []) => {
|
|
314
451
|
// Use the lesser value between limit and itemsToGet in query
|
|
315
452
|
const limit = query.limit < itemsToGet ? query.limit : itemsToGet;
|
|
316
|
-
const originalQueryString = qs__default["default"].stringify({
|
|
453
|
+
const originalQueryString = qs__default["default"].stringify({
|
|
454
|
+
...query,
|
|
317
455
|
limit
|
|
318
456
|
});
|
|
319
457
|
const enhancedQuery = {
|
|
@@ -324,48 +462,42 @@ function process$1(request, fn, processOpt) {
|
|
|
324
462
|
} : {})
|
|
325
463
|
};
|
|
326
464
|
const enhancedQueryString = qs__default["default"].stringify(enhancedQuery);
|
|
327
|
-
const enhancedRequest = {
|
|
465
|
+
const enhancedRequest = {
|
|
466
|
+
...request,
|
|
328
467
|
uri: `${_path}?${enhancedQueryString}&${originalQueryString}`
|
|
329
468
|
};
|
|
330
|
-
|
|
331
469
|
try {
|
|
332
470
|
const payload = await createClient(_options).execute(enhancedRequest);
|
|
333
471
|
const {
|
|
334
472
|
results,
|
|
335
473
|
count: resultsLength
|
|
336
|
-
} = payload
|
|
337
|
-
|
|
474
|
+
} = (payload === null || payload === void 0 ? void 0 : payload.body) || {};
|
|
338
475
|
if (!resultsLength && hasFirstPageBeenProcessed) {
|
|
339
476
|
return resolve(acc || []);
|
|
340
477
|
}
|
|
341
|
-
|
|
342
|
-
const result = await Promise.resolve(fn(payload)); // const result = await fn(payload)
|
|
343
|
-
|
|
478
|
+
const result = await Promise.resolve(fn(payload));
|
|
344
479
|
let accumulated;
|
|
345
480
|
hasFirstPageBeenProcessed = true;
|
|
346
481
|
if (opt.accumulate) accumulated = acc.concat(result || []);
|
|
347
|
-
itemsToGet -= resultsLength;
|
|
482
|
+
itemsToGet -= resultsLength;
|
|
483
|
+
// If there are no more items to get, it means the total number
|
|
348
484
|
// of items in the original request have been fetched so we
|
|
349
485
|
// resolve the promise.
|
|
350
486
|
// Also, if we get less results in a page then the limit set it
|
|
351
487
|
// means that there are no more pages and that we can finally
|
|
352
488
|
// resolve the promise.
|
|
353
|
-
|
|
354
489
|
if (resultsLength < query.limit || !itemsToGet) {
|
|
355
490
|
return resolve(accumulated || []);
|
|
356
491
|
}
|
|
357
|
-
|
|
358
492
|
const last = results[resultsLength - 1];
|
|
359
493
|
const newLastId = last && last.id;
|
|
360
494
|
processPage(newLastId, accumulated);
|
|
361
495
|
} catch (error) {
|
|
362
|
-
reject(error);
|
|
363
|
-
// error
|
|
364
|
-
// }
|
|
496
|
+
reject(error);
|
|
365
497
|
}
|
|
366
|
-
};
|
|
367
|
-
|
|
498
|
+
};
|
|
368
499
|
|
|
500
|
+
// Start iterating through pages
|
|
369
501
|
processPage();
|
|
370
502
|
});
|
|
371
503
|
}
|
|
@@ -375,38 +507,39 @@ function createClient(middlewares) {
|
|
|
375
507
|
const resolver = {
|
|
376
508
|
async resolve(rs) {
|
|
377
509
|
const {
|
|
378
|
-
reject,
|
|
379
|
-
resolve,
|
|
380
510
|
response,
|
|
511
|
+
includeOriginalRequest,
|
|
512
|
+
maskSensitiveHeaderData,
|
|
513
|
+
...request
|
|
514
|
+
} = rs;
|
|
515
|
+
const {
|
|
381
516
|
retryCount,
|
|
382
517
|
...rest
|
|
383
|
-
} =
|
|
518
|
+
} = response;
|
|
384
519
|
const res = {
|
|
385
520
|
body: null,
|
|
386
521
|
error: null,
|
|
387
|
-
reject,
|
|
388
|
-
resolve,
|
|
389
|
-
...
|
|
390
|
-
|
|
522
|
+
reject: rs.reject,
|
|
523
|
+
resolve: rs.resolve,
|
|
524
|
+
...rest,
|
|
525
|
+
...(includeOriginalRequest ? {
|
|
526
|
+
originalRequest: maskSensitiveHeaderData ? maskAuthData(request) : request
|
|
391
527
|
} : {}),
|
|
392
|
-
|
|
393
|
-
|
|
528
|
+
...(response !== null && response !== void 0 && response.retryCount ? {
|
|
529
|
+
retryCount: response.retryCount
|
|
530
|
+
} : {})
|
|
394
531
|
};
|
|
395
|
-
|
|
396
532
|
if (res.error) {
|
|
397
|
-
res.reject(res);
|
|
533
|
+
res.reject(res.error);
|
|
398
534
|
return res;
|
|
399
535
|
}
|
|
400
|
-
|
|
401
536
|
res.resolve(res);
|
|
402
537
|
return res;
|
|
403
538
|
}
|
|
404
|
-
|
|
405
539
|
};
|
|
406
540
|
const dispatch = compose(middlewares)(resolver.resolve);
|
|
407
541
|
return {
|
|
408
542
|
process: process$1,
|
|
409
|
-
|
|
410
543
|
execute(request) {
|
|
411
544
|
validate('exec', request);
|
|
412
545
|
return new Promise((resolve, reject) => {
|
|
@@ -417,11 +550,27 @@ function createClient(middlewares) {
|
|
|
417
550
|
});
|
|
418
551
|
});
|
|
419
552
|
}
|
|
420
|
-
|
|
421
553
|
};
|
|
422
554
|
}
|
|
423
555
|
|
|
556
|
+
function _toPrimitive(input, hint) {
|
|
557
|
+
if (typeof input !== "object" || input === null) return input;
|
|
558
|
+
var prim = input[Symbol.toPrimitive];
|
|
559
|
+
if (prim !== undefined) {
|
|
560
|
+
var res = prim.call(input, hint || "default");
|
|
561
|
+
if (typeof res !== "object") return res;
|
|
562
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
563
|
+
}
|
|
564
|
+
return (hint === "string" ? String : Number)(input);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
function _toPropertyKey(arg) {
|
|
568
|
+
var key = _toPrimitive(arg, "string");
|
|
569
|
+
return typeof key === "symbol" ? key : String(key);
|
|
570
|
+
}
|
|
571
|
+
|
|
424
572
|
function _defineProperty(obj, key, value) {
|
|
573
|
+
key = _toPropertyKey(key);
|
|
425
574
|
if (key in obj) {
|
|
426
575
|
Object.defineProperty(obj, key, {
|
|
427
576
|
value: value,
|
|
@@ -432,14 +581,15 @@ function _defineProperty(obj, key, value) {
|
|
|
432
581
|
} else {
|
|
433
582
|
obj[key] = value;
|
|
434
583
|
}
|
|
435
|
-
|
|
436
584
|
return obj;
|
|
437
585
|
}
|
|
438
586
|
|
|
439
587
|
function createCorrelationIdMiddleware$1(options) {
|
|
440
588
|
return next => request => {
|
|
441
|
-
const nextRequest = {
|
|
442
|
-
|
|
589
|
+
const nextRequest = {
|
|
590
|
+
...request,
|
|
591
|
+
headers: {
|
|
592
|
+
...request.headers,
|
|
443
593
|
'X-Correlation-ID': options.generate && typeof options.generate == 'function' ? options.generate() : generateID()
|
|
444
594
|
}
|
|
445
595
|
};
|
|
@@ -449,16 +599,19 @@ function createCorrelationIdMiddleware$1(options) {
|
|
|
449
599
|
|
|
450
600
|
async function executeRequest$1({
|
|
451
601
|
url,
|
|
452
|
-
|
|
453
|
-
|
|
602
|
+
httpClient,
|
|
603
|
+
clientOptions
|
|
454
604
|
}) {
|
|
455
605
|
let timer;
|
|
456
606
|
const {
|
|
457
607
|
timeout,
|
|
458
|
-
|
|
608
|
+
request,
|
|
609
|
+
abortController,
|
|
610
|
+
maskSensitiveHeaderData,
|
|
611
|
+
includeRequestInErrorResponse
|
|
459
612
|
} = clientOptions;
|
|
460
|
-
|
|
461
613
|
try {
|
|
614
|
+
var _response$data, _response$data2;
|
|
462
615
|
if (timeout) timer = setTimeout(() => {
|
|
463
616
|
abortController.abort();
|
|
464
617
|
}, timeout);
|
|
@@ -471,43 +624,68 @@ async function executeRequest$1({
|
|
|
471
624
|
body: clientOptions.body
|
|
472
625
|
} : {})
|
|
473
626
|
});
|
|
474
|
-
|
|
475
627
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
476
628
|
if (clientOptions.method == 'HEAD') {
|
|
477
629
|
return {
|
|
478
630
|
body: null,
|
|
479
631
|
statusCode: response.statusCode,
|
|
632
|
+
retryCount: response.retryCount,
|
|
480
633
|
headers: getHeaders(response.headers)
|
|
481
634
|
};
|
|
482
635
|
}
|
|
483
|
-
|
|
484
636
|
return {
|
|
485
637
|
body: response.data,
|
|
486
638
|
statusCode: response.statusCode,
|
|
639
|
+
retryCount: response.retryCount,
|
|
487
640
|
headers: getHeaders(response.headers)
|
|
488
641
|
};
|
|
489
642
|
}
|
|
643
|
+
const error = createError({
|
|
644
|
+
message: (response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.message) || (response === null || response === void 0 ? void 0 : response.message),
|
|
645
|
+
statusCode: response.statusCode || (response === null || response === void 0 ? void 0 : (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.statusCode),
|
|
646
|
+
headers: getHeaders(response.headers),
|
|
647
|
+
method: clientOptions.method,
|
|
648
|
+
body: response.data,
|
|
649
|
+
retryCount: response.retryCount,
|
|
650
|
+
...(includeRequestInErrorResponse ? {
|
|
651
|
+
originalRequest: maskSensitiveHeaderData ? maskAuthData(request) : request
|
|
652
|
+
} : {
|
|
653
|
+
uri: request.uri
|
|
654
|
+
})
|
|
655
|
+
});
|
|
656
|
+
|
|
490
657
|
/**
|
|
491
658
|
* handle non-ok (error) response
|
|
492
659
|
* build error body
|
|
493
660
|
*/
|
|
494
|
-
|
|
495
|
-
|
|
496
661
|
return {
|
|
497
662
|
body: response.data,
|
|
498
663
|
code: response.statusCode,
|
|
499
664
|
statusCode: response.statusCode,
|
|
500
665
|
headers: getHeaders(response.headers),
|
|
501
|
-
error
|
|
502
|
-
statusCode: response.statusCode || response.data.statusCode,
|
|
503
|
-
message: response.data.message,
|
|
504
|
-
method: clientOptions.method,
|
|
505
|
-
...response.data
|
|
506
|
-
}
|
|
666
|
+
error
|
|
507
667
|
};
|
|
508
|
-
} catch (
|
|
668
|
+
} catch (e) {
|
|
669
|
+
var _e$response, _e$response2, _e$response3, _e$response4, _e$response4$data, _e$response5, _e$response6;
|
|
670
|
+
// We know that this is a network error
|
|
671
|
+
const headers = getHeaders((_e$response = e.response) === null || _e$response === void 0 ? void 0 : _e$response.headers);
|
|
672
|
+
const statusCode = ((_e$response2 = e.response) === null || _e$response2 === void 0 ? void 0 : _e$response2.status) || ((_e$response3 = e.response) === null || _e$response3 === void 0 ? void 0 : _e$response3.data0) || 0;
|
|
673
|
+
const message = (_e$response4 = e.response) === null || _e$response4 === void 0 ? void 0 : (_e$response4$data = _e$response4.data) === null || _e$response4$data === void 0 ? void 0 : _e$response4$data.message;
|
|
674
|
+
const error = createError({
|
|
675
|
+
statusCode,
|
|
676
|
+
code: statusCode,
|
|
677
|
+
status: statusCode,
|
|
678
|
+
message: message || e.message,
|
|
679
|
+
headers,
|
|
680
|
+
body: ((_e$response5 = e.response) === null || _e$response5 === void 0 ? void 0 : _e$response5.data) || e,
|
|
681
|
+
error: (_e$response6 = e.response) === null || _e$response6 === void 0 ? void 0 : _e$response6.data,
|
|
682
|
+
...(includeRequestInErrorResponse ? {
|
|
683
|
+
originalRequest: maskSensitiveHeaderData ? maskAuthData(request) : request
|
|
684
|
+
} : {
|
|
685
|
+
uri: request.uri
|
|
686
|
+
})
|
|
687
|
+
});
|
|
509
688
|
return {
|
|
510
|
-
// We know that this is a network error
|
|
511
689
|
body: error,
|
|
512
690
|
error
|
|
513
691
|
};
|
|
@@ -515,7 +693,6 @@ async function executeRequest$1({
|
|
|
515
693
|
clearTimeout(timer);
|
|
516
694
|
}
|
|
517
695
|
}
|
|
518
|
-
|
|
519
696
|
function createHttpMiddleware$1(options) {
|
|
520
697
|
// validate response
|
|
521
698
|
validateHttpOptions(options);
|
|
@@ -524,58 +701,72 @@ function createHttpMiddleware$1(options) {
|
|
|
524
701
|
credentialsMode,
|
|
525
702
|
httpClient,
|
|
526
703
|
timeout,
|
|
704
|
+
enableRetry,
|
|
705
|
+
retryConfig,
|
|
527
706
|
getAbortController,
|
|
707
|
+
includeOriginalRequest,
|
|
708
|
+
includeRequestInErrorResponse,
|
|
709
|
+
maskSensitiveHeaderData,
|
|
528
710
|
httpClientOptions
|
|
529
711
|
} = options;
|
|
530
712
|
return next => {
|
|
531
713
|
return async request => {
|
|
532
714
|
let abortController;
|
|
533
|
-
if (timeout || getAbortController) abortController = (getAbortController ? getAbortController() : null) || new
|
|
715
|
+
if (timeout || getAbortController) abortController = (getAbortController ? getAbortController() : null) || new AbortController__default["default"]();
|
|
534
716
|
const url = host.replace(/\/$/, '') + request.uri;
|
|
535
|
-
const requestHeader = {
|
|
536
|
-
|
|
717
|
+
const requestHeader = {
|
|
718
|
+
...request.headers
|
|
719
|
+
};
|
|
537
720
|
|
|
538
|
-
|
|
721
|
+
// validate header
|
|
722
|
+
if (!(Object.prototype.hasOwnProperty.call(requestHeader, 'Content-Type') || Object.prototype.hasOwnProperty.call(requestHeader, 'content-type'))) {
|
|
539
723
|
requestHeader['Content-Type'] = 'application/json';
|
|
540
|
-
}
|
|
724
|
+
}
|
|
541
725
|
|
|
726
|
+
// Unset the content-type header if explicitly asked to (passing `null` as value).
|
|
727
|
+
if (requestHeader['Content-Type'] === null) {
|
|
728
|
+
delete requestHeader['Content-Type'];
|
|
729
|
+
}
|
|
542
730
|
|
|
731
|
+
// Ensure body is a string if content type is application/{json|graphql}
|
|
543
732
|
const body = HEADERS_CONTENT_TYPES.indexOf(requestHeader['Content-Type']) > -1 && typeof request.body === 'string' || isBuffer(request.body) ? request.body : JSON.stringify(request.body || undefined);
|
|
544
|
-
|
|
545
733
|
if (body && (typeof body === 'string' || isBuffer(body))) {
|
|
546
734
|
requestHeader['Content-Length'] = _.Buffer.byteLength(body).toString();
|
|
547
735
|
}
|
|
548
|
-
|
|
549
736
|
const clientOptions = {
|
|
737
|
+
enableRetry,
|
|
738
|
+
retryConfig,
|
|
739
|
+
request: request,
|
|
550
740
|
method: request.method,
|
|
551
741
|
headers: requestHeader,
|
|
742
|
+
includeRequestInErrorResponse,
|
|
743
|
+
maskSensitiveHeaderData,
|
|
552
744
|
...httpClientOptions
|
|
553
745
|
};
|
|
554
|
-
|
|
555
746
|
if (credentialsMode) {
|
|
556
|
-
clientOptions.credentialsMode =
|
|
747
|
+
clientOptions.credentialsMode = credentialsMode;
|
|
557
748
|
}
|
|
558
|
-
|
|
559
749
|
if (abortController) {
|
|
560
750
|
clientOptions.signal = abortController.signal;
|
|
561
751
|
}
|
|
562
|
-
|
|
563
752
|
if (timeout) {
|
|
564
753
|
clientOptions.timeout = timeout;
|
|
565
754
|
clientOptions.abortController = abortController;
|
|
566
755
|
}
|
|
567
|
-
|
|
568
756
|
if (body) {
|
|
569
757
|
clientOptions.body = body;
|
|
570
|
-
}
|
|
571
|
-
|
|
758
|
+
}
|
|
572
759
|
|
|
760
|
+
// get result from executed request
|
|
573
761
|
const response = await executeRequest$1({
|
|
574
762
|
url,
|
|
575
763
|
clientOptions,
|
|
576
764
|
httpClient
|
|
577
765
|
});
|
|
578
|
-
const responseWithRequest = {
|
|
766
|
+
const responseWithRequest = {
|
|
767
|
+
...request,
|
|
768
|
+
includeOriginalRequest,
|
|
769
|
+
maskSensitiveHeaderData,
|
|
579
770
|
response
|
|
580
771
|
};
|
|
581
772
|
return next(responseWithRequest);
|
|
@@ -588,43 +779,37 @@ function createQueueMiddleware$1({
|
|
|
588
779
|
}) {
|
|
589
780
|
let runningCount = 0;
|
|
590
781
|
const queue = [];
|
|
591
|
-
|
|
592
782
|
const dequeue = next => {
|
|
593
783
|
runningCount--;
|
|
594
|
-
|
|
595
784
|
if (queue.length && runningCount <= concurrency) {
|
|
596
785
|
const nextTask = queue.shift();
|
|
597
786
|
runningCount++;
|
|
598
787
|
return next(nextTask.request);
|
|
599
788
|
}
|
|
600
789
|
};
|
|
601
|
-
|
|
602
790
|
const enqueue = ({
|
|
603
791
|
request
|
|
604
792
|
}) => queue.push({
|
|
605
793
|
request
|
|
606
794
|
});
|
|
607
|
-
|
|
608
795
|
return next => request => {
|
|
609
796
|
// wrap and override resolve and reject functions
|
|
610
|
-
const patchedRequest = {
|
|
611
|
-
|
|
797
|
+
const patchedRequest = {
|
|
798
|
+
...request,
|
|
612
799
|
resolve(data) {
|
|
613
800
|
request.resolve(data);
|
|
614
801
|
dequeue(next);
|
|
615
802
|
},
|
|
616
|
-
|
|
617
803
|
reject(error) {
|
|
618
804
|
request.reject(error);
|
|
619
805
|
dequeue(next);
|
|
620
806
|
}
|
|
807
|
+
};
|
|
621
808
|
|
|
622
|
-
|
|
623
|
-
|
|
809
|
+
// enqueue requests
|
|
624
810
|
enqueue({
|
|
625
811
|
request: patchedRequest
|
|
626
812
|
});
|
|
627
|
-
|
|
628
813
|
if (runningCount < concurrency) {
|
|
629
814
|
runningCount++;
|
|
630
815
|
const nextTask = queue.shift();
|
|
@@ -633,66 +818,7 @@ function createQueueMiddleware$1({
|
|
|
633
818
|
};
|
|
634
819
|
}
|
|
635
820
|
|
|
636
|
-
|
|
637
|
-
return !(retryCodes.includes(response?.error?.message) || [503, ...retryCodes].includes(response?.statusCode));
|
|
638
|
-
} // error, info, warn
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
function createRetryMiddleware$1(options) {
|
|
642
|
-
return next => async request => {
|
|
643
|
-
const {
|
|
644
|
-
retryCodes = [],
|
|
645
|
-
maxDelay = Infinity,
|
|
646
|
-
maxRetries = 3,
|
|
647
|
-
backoff = true,
|
|
648
|
-
retryDelay = 200
|
|
649
|
-
} = options || {};
|
|
650
|
-
let response,
|
|
651
|
-
retryCount = 0; // validate the `retryCodes` option
|
|
652
|
-
|
|
653
|
-
validateRetryCodes(retryCodes);
|
|
654
|
-
|
|
655
|
-
async function executeRequest() {
|
|
656
|
-
// first attenpt
|
|
657
|
-
response = await next(request);
|
|
658
|
-
retryCount++;
|
|
659
|
-
|
|
660
|
-
if (predicate(retryCodes, response)) {
|
|
661
|
-
return { ...response
|
|
662
|
-
};
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
while (retryCount < maxRetries) {
|
|
666
|
-
// check if the response if worth retrying for subsequest calls
|
|
667
|
-
response = await next(request);
|
|
668
|
-
retryCount++;
|
|
669
|
-
|
|
670
|
-
if (predicate(retryCodes, response)) {
|
|
671
|
-
return { ...response
|
|
672
|
-
};
|
|
673
|
-
} // delay next execution
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
const timer = calculateRetryDelay({
|
|
677
|
-
retryCount,
|
|
678
|
-
retryDelay,
|
|
679
|
-
maxRetries,
|
|
680
|
-
backoff,
|
|
681
|
-
maxDelay
|
|
682
|
-
});
|
|
683
|
-
await sleep(timer);
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
return { ...response,
|
|
687
|
-
retryCount
|
|
688
|
-
};
|
|
689
|
-
} // invoke executeRequest
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
return executeRequest();
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
|
|
821
|
+
// error, info, success
|
|
696
822
|
function createLoggerMiddleware$1(options) {
|
|
697
823
|
return next => {
|
|
698
824
|
return async request => {
|
|
@@ -703,14 +829,12 @@ function createLoggerMiddleware$1(options) {
|
|
|
703
829
|
// logLevel = 'ERROR',
|
|
704
830
|
maskSensitiveHeaderData = true,
|
|
705
831
|
includeOriginalRequest = true,
|
|
706
|
-
includeResponseHeaders = true
|
|
707
|
-
|
|
832
|
+
includeResponseHeaders = true
|
|
833
|
+
// includeRequestInErrorResponse
|
|
708
834
|
} = options || {};
|
|
709
|
-
|
|
710
835
|
if (includeOriginalRequest && maskSensitiveHeaderData) {
|
|
711
836
|
maskAuthData(response.request);
|
|
712
837
|
}
|
|
713
|
-
|
|
714
838
|
if (!includeOriginalRequest) {
|
|
715
839
|
const {
|
|
716
840
|
request,
|
|
@@ -718,7 +842,6 @@ function createLoggerMiddleware$1(options) {
|
|
|
718
842
|
} = response;
|
|
719
843
|
response = rest;
|
|
720
844
|
}
|
|
721
|
-
|
|
722
845
|
if (!includeResponseHeaders) {
|
|
723
846
|
const {
|
|
724
847
|
headers,
|
|
@@ -726,13 +849,12 @@ function createLoggerMiddleware$1(options) {
|
|
|
726
849
|
} = response;
|
|
727
850
|
response = rest;
|
|
728
851
|
}
|
|
729
|
-
|
|
730
852
|
if (loggerFn && typeof loggerFn == 'function') {
|
|
731
853
|
loggerFn(response);
|
|
732
|
-
return originalResponse
|
|
733
|
-
}
|
|
734
|
-
|
|
854
|
+
// return originalResponse
|
|
855
|
+
}
|
|
735
856
|
|
|
857
|
+
// console.log({ Response: response })
|
|
736
858
|
return originalResponse;
|
|
737
859
|
};
|
|
738
860
|
};
|
|
@@ -740,7 +862,7 @@ function createLoggerMiddleware$1(options) {
|
|
|
740
862
|
|
|
741
863
|
var packageJson = {
|
|
742
864
|
name: "@commercetools/ts-client",
|
|
743
|
-
version: "
|
|
865
|
+
version: "1.0.0",
|
|
744
866
|
engines: {
|
|
745
867
|
node: ">=14"
|
|
746
868
|
},
|
|
@@ -773,6 +895,7 @@ var packageJson = {
|
|
|
773
895
|
url: "https://github.com/commercetools/commercetools-sdk-typescript/issues"
|
|
774
896
|
},
|
|
775
897
|
dependencies: {
|
|
898
|
+
"abort-controller": "3.0.0",
|
|
776
899
|
buffer: "^6.0.3",
|
|
777
900
|
"node-fetch": "^2.6.1",
|
|
778
901
|
querystring: "^0.2.1"
|
|
@@ -789,10 +912,9 @@ var packageJson = {
|
|
|
789
912
|
"./dist/commercetools-ts-client.esm.js": "./dist/commercetools-ts-client.browser.esm.js"
|
|
790
913
|
},
|
|
791
914
|
devDependencies: {
|
|
792
|
-
"abort-controller": "3.0.0",
|
|
793
915
|
"common-tags": "1.8.2",
|
|
794
|
-
dotenv: "16.0.
|
|
795
|
-
jest: "29.0
|
|
916
|
+
dotenv: "16.0.3",
|
|
917
|
+
jest: "29.5.0",
|
|
796
918
|
nock: "12.0.3",
|
|
797
919
|
"organize-imports-cli": "0.10.0"
|
|
798
920
|
},
|
|
@@ -805,11 +927,14 @@ var packageJson = {
|
|
|
805
927
|
|
|
806
928
|
function createUserAgentMiddleware$1(options) {
|
|
807
929
|
return next => async request => {
|
|
808
|
-
const userAgent = createUserAgent({
|
|
809
|
-
|
|
930
|
+
const userAgent = createUserAgent({
|
|
931
|
+
...options,
|
|
932
|
+
name: `commercetools-sdk-javascript-v3/${packageJson.version}`
|
|
810
933
|
});
|
|
811
|
-
const requestWithUserAgent = {
|
|
812
|
-
|
|
934
|
+
const requestWithUserAgent = {
|
|
935
|
+
...request,
|
|
936
|
+
headers: {
|
|
937
|
+
...request.headers,
|
|
813
938
|
'User-Agent': userAgent
|
|
814
939
|
}
|
|
815
940
|
};
|
|
@@ -821,23 +946,27 @@ function createConcurrentModificationMiddleware$1() {
|
|
|
821
946
|
return next => {
|
|
822
947
|
return async request => {
|
|
823
948
|
const response = await next(request);
|
|
824
|
-
|
|
825
949
|
if (response.statusCode == 409) {
|
|
950
|
+
var _response$error, _response$error$body, _response$error$body$, _response$error$body$2;
|
|
826
951
|
/**
|
|
827
952
|
* extract the currentVersion
|
|
828
953
|
* from the error body and update
|
|
829
954
|
* request with the currentVersion
|
|
830
955
|
*/
|
|
831
|
-
const version = response.error.body.errors[0]
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
956
|
+
const version = (_response$error = response.error) === null || _response$error === void 0 ? void 0 : (_response$error$body = _response$error.body) === null || _response$error$body === void 0 ? void 0 : (_response$error$body$ = _response$error$body.errors) === null || _response$error$body$ === void 0 ? void 0 : (_response$error$body$2 = _response$error$body$[0]) === null || _response$error$body$2 === void 0 ? void 0 : _response$error$body$2.currentVersion;
|
|
957
|
+
|
|
958
|
+
// update the resource version here
|
|
959
|
+
if (version) {
|
|
960
|
+
request.body = typeof request.body == 'string' ? {
|
|
961
|
+
...JSON.parse(request.body),
|
|
962
|
+
version
|
|
963
|
+
} : {
|
|
964
|
+
...request.body,
|
|
965
|
+
version
|
|
966
|
+
};
|
|
967
|
+
return next(request);
|
|
968
|
+
}
|
|
839
969
|
}
|
|
840
|
-
|
|
841
970
|
return response;
|
|
842
971
|
};
|
|
843
972
|
};
|
|
@@ -846,20 +975,20 @@ function createConcurrentModificationMiddleware$1() {
|
|
|
846
975
|
function createErrorMiddleware$1(options) {
|
|
847
976
|
return next => async request => {
|
|
848
977
|
const response = await next(request);
|
|
849
|
-
|
|
850
978
|
if (response.error) {
|
|
851
979
|
const {
|
|
852
980
|
error
|
|
853
981
|
} = response;
|
|
854
|
-
return {
|
|
982
|
+
return {
|
|
983
|
+
...response,
|
|
855
984
|
statusCode: error.statusCode || 0,
|
|
856
985
|
headers: error.headers || getHeaders({}),
|
|
857
|
-
error: {
|
|
986
|
+
error: {
|
|
987
|
+
...error,
|
|
858
988
|
body: error.data || error
|
|
859
989
|
}
|
|
860
990
|
};
|
|
861
991
|
}
|
|
862
|
-
|
|
863
992
|
return response;
|
|
864
993
|
};
|
|
865
994
|
}
|
|
@@ -869,7 +998,6 @@ function createErrorMiddleware$1(options) {
|
|
|
869
998
|
* @param {AuthMiddlewareOptions} options
|
|
870
999
|
* @returns { IBuiltRequestParams } *
|
|
871
1000
|
*/
|
|
872
|
-
|
|
873
1001
|
function buildRequestForClientCredentialsFlow(options) {
|
|
874
1002
|
// Validate options
|
|
875
1003
|
if (!options) throw new Error('Missing required options');
|
|
@@ -882,9 +1010,9 @@ function buildRequestForClientCredentialsFlow(options) {
|
|
|
882
1010
|
} = options.credentials || {};
|
|
883
1011
|
if (!(clientId && clientSecret)) throw new Error('Missing required credentials (clientId, clientSecret)');
|
|
884
1012
|
const scope = options.scopes ? options.scopes.join(' ') : undefined;
|
|
885
|
-
const basicAuth = _.Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
|
|
1013
|
+
const basicAuth = _.Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
|
|
1014
|
+
// This is mostly useful for internal testing purposes to be able to check
|
|
886
1015
|
// other oauth endpoints.
|
|
887
|
-
|
|
888
1016
|
const oauthUri = options.oauthUri || '/oauth/token';
|
|
889
1017
|
const url = options.host.replace(/\/$/, '') + oauthUri;
|
|
890
1018
|
const body = `grant_type=client_credentials${scope ? `&scope=${scope}` : ''}`;
|
|
@@ -894,12 +1022,12 @@ function buildRequestForClientCredentialsFlow(options) {
|
|
|
894
1022
|
basicAuth
|
|
895
1023
|
};
|
|
896
1024
|
}
|
|
1025
|
+
|
|
897
1026
|
/**
|
|
898
1027
|
*
|
|
899
1028
|
* @param {AuthMiddlewareOptions} options
|
|
900
1029
|
* @returns {IBuiltRequestParams} *
|
|
901
1030
|
*/
|
|
902
|
-
|
|
903
1031
|
function buildRequestForAnonymousSessionFlow(options) {
|
|
904
1032
|
if (!options) throw new Error('Missing required options');
|
|
905
1033
|
if (!options.projectKey) throw new Error('Missing required option (projectKey)');
|
|
@@ -907,15 +1035,16 @@ function buildRequestForAnonymousSessionFlow(options) {
|
|
|
907
1035
|
options.oauthUri = options.oauthUri || `/oauth/${projectKey}/anonymous/token`;
|
|
908
1036
|
const result = buildRequestForClientCredentialsFlow(options);
|
|
909
1037
|
if (options.credentials.anonymousId) result.body += `&anonymous_id=${options.credentials.anonymousId}`;
|
|
910
|
-
return {
|
|
1038
|
+
return {
|
|
1039
|
+
...result
|
|
911
1040
|
};
|
|
912
1041
|
}
|
|
1042
|
+
|
|
913
1043
|
/**
|
|
914
1044
|
*
|
|
915
1045
|
* @param {RefreshAuthMiddlewareOptions} options
|
|
916
1046
|
* @returns {IBuiltRequestParams}
|
|
917
1047
|
*/
|
|
918
|
-
|
|
919
1048
|
function buildRequestForRefreshTokenFlow(options) {
|
|
920
1049
|
if (!options) throw new Error('Missing required options');
|
|
921
1050
|
if (!options.host) throw new Error('Missing required option (host)');
|
|
@@ -927,9 +1056,9 @@ function buildRequestForRefreshTokenFlow(options) {
|
|
|
927
1056
|
clientSecret
|
|
928
1057
|
} = options.credentials;
|
|
929
1058
|
if (!(clientId && clientSecret)) throw new Error('Missing required credentials (clientId, clientSecret)');
|
|
930
|
-
const basicAuth = _.Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
|
|
1059
|
+
const basicAuth = _.Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
|
|
1060
|
+
// This is mostly useful for internal testing purposes to be able to check
|
|
931
1061
|
// other oauth endpoints.
|
|
932
|
-
|
|
933
1062
|
const oauthUri = options.oauthUri || '/oauth/token';
|
|
934
1063
|
const url = options.host.replace(/\/$/, '') + oauthUri;
|
|
935
1064
|
const body = `grant_type=refresh_token&refresh_token=${encodeURIComponent(options.refreshToken)}`;
|
|
@@ -939,11 +1068,11 @@ function buildRequestForRefreshTokenFlow(options) {
|
|
|
939
1068
|
body
|
|
940
1069
|
};
|
|
941
1070
|
}
|
|
1071
|
+
|
|
942
1072
|
/**
|
|
943
1073
|
* @param {PasswordAuthMiddlewareOptions} options
|
|
944
1074
|
* @returns {IBuiltRequestParams}
|
|
945
1075
|
*/
|
|
946
|
-
|
|
947
1076
|
function buildRequestForPasswordFlow(options) {
|
|
948
1077
|
if (!options) throw new Error('Missing required options');
|
|
949
1078
|
if (!options.host) throw new Error('Missing required option (host)');
|
|
@@ -964,14 +1093,15 @@ function buildRequestForPasswordFlow(options) {
|
|
|
964
1093
|
const scope = (options.scopes || []).join(' ');
|
|
965
1094
|
const scopeStr = scope ? `&scope=${scope}` : '';
|
|
966
1095
|
const basicAuth = _.Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
|
|
1096
|
+
|
|
967
1097
|
/**
|
|
968
1098
|
* This is mostly useful for internal testing purposes to be able to check
|
|
969
1099
|
* other oauth endpoints.
|
|
970
1100
|
*/
|
|
971
|
-
|
|
972
1101
|
const oauthUri = options.oauthUri || `/oauth/${projectKey}/customers/token`;
|
|
973
|
-
const url = options.host.replace(/\/$/, '') + oauthUri;
|
|
1102
|
+
const url = options.host.replace(/\/$/, '') + oauthUri;
|
|
974
1103
|
|
|
1104
|
+
// encode username and password as requested by the system
|
|
975
1105
|
const body = `grant_type=password&username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}${scopeStr}`;
|
|
976
1106
|
return {
|
|
977
1107
|
basicAuth,
|
|
@@ -992,58 +1122,62 @@ async function executeRequest(options) {
|
|
|
992
1122
|
} = options;
|
|
993
1123
|
let url = options.url;
|
|
994
1124
|
let body = options.body;
|
|
995
|
-
let basicAuth = options.basicAuth;
|
|
1125
|
+
let basicAuth = options.basicAuth;
|
|
996
1126
|
|
|
1127
|
+
// get the pending object from option
|
|
997
1128
|
let pendingTasks = options.pendingTasks;
|
|
998
1129
|
if (!httpClient || typeof httpClient !== 'function') throw new Error('an `httpClient` is not available, please pass in a `fetch` or `axios` instance as an option or have them globally available.');
|
|
1130
|
+
|
|
999
1131
|
/**
|
|
1000
1132
|
* If there is a token in the tokenCache, and it's not
|
|
1001
1133
|
* expired, append the token in the `Authorization` header.
|
|
1002
1134
|
*/
|
|
1003
|
-
|
|
1004
1135
|
const tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
1005
|
-
|
|
1006
1136
|
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
1007
1137
|
const requestWithAuth = mergeAuthHeader(tokenCacheObject.token, request);
|
|
1008
|
-
return {
|
|
1138
|
+
return {
|
|
1139
|
+
...requestWithAuth
|
|
1009
1140
|
};
|
|
1010
1141
|
}
|
|
1142
|
+
|
|
1011
1143
|
/**
|
|
1012
1144
|
* Keep pending tasks until a token is fetched
|
|
1013
1145
|
* Save next function as well, to call it once the token has been fetched, which prevents
|
|
1014
1146
|
* unexpected behaviour in a context in which the next function uses global vars
|
|
1015
1147
|
* or Promises to capture the token to hand it to other libraries, e.g. Apollo
|
|
1016
1148
|
*/
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
1149
|
pendingTasks.push({
|
|
1020
1150
|
request,
|
|
1021
1151
|
next
|
|
1022
|
-
});
|
|
1152
|
+
});
|
|
1023
1153
|
|
|
1024
|
-
|
|
1154
|
+
// if a token is currently being fetched, then wait
|
|
1155
|
+
if (requestState.get()) return;
|
|
1025
1156
|
|
|
1157
|
+
// signal that a token is being fetched
|
|
1026
1158
|
requestState.set(true);
|
|
1159
|
+
|
|
1027
1160
|
/**
|
|
1028
1161
|
* use refreshToken flow if there is refresh-token
|
|
1029
1162
|
* and there's either no token or the token is expired
|
|
1030
1163
|
*/
|
|
1031
|
-
|
|
1032
1164
|
if (tokenCacheObject && tokenCacheObject.refreshToken && (!tokenCacheObject.token || tokenCacheObject.token && Date.now() > tokenCacheObject.expirationTime)) {
|
|
1033
1165
|
if (!userOption) throw new Error('Missing required options.');
|
|
1034
|
-
const opt = {
|
|
1166
|
+
const opt = {
|
|
1167
|
+
...buildRequestForRefreshTokenFlow({
|
|
1168
|
+
...userOption,
|
|
1035
1169
|
refreshToken: tokenCacheObject.refreshToken
|
|
1036
1170
|
})
|
|
1037
|
-
};
|
|
1171
|
+
};
|
|
1038
1172
|
|
|
1173
|
+
// reassign values
|
|
1039
1174
|
url = opt.url;
|
|
1040
1175
|
body = opt.body;
|
|
1041
1176
|
basicAuth = opt.basicAuth;
|
|
1042
|
-
}
|
|
1043
|
-
|
|
1177
|
+
}
|
|
1044
1178
|
|
|
1179
|
+
// request a new token
|
|
1045
1180
|
let response;
|
|
1046
|
-
|
|
1047
1181
|
try {
|
|
1048
1182
|
response = await executor({
|
|
1049
1183
|
url,
|
|
@@ -1055,58 +1189,59 @@ async function executeRequest(options) {
|
|
|
1055
1189
|
},
|
|
1056
1190
|
httpClient,
|
|
1057
1191
|
body
|
|
1058
|
-
});
|
|
1059
|
-
|
|
1192
|
+
});
|
|
1060
1193
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
1194
|
+
var _response;
|
|
1061
1195
|
const {
|
|
1062
1196
|
access_token: token,
|
|
1063
1197
|
expires_in: expiresIn,
|
|
1064
|
-
// expires_at: expiresAt,
|
|
1065
1198
|
refresh_token: refreshToken
|
|
1066
|
-
} = response
|
|
1199
|
+
} = (_response = response) === null || _response === void 0 ? void 0 : _response.data;
|
|
1067
1200
|
|
|
1068
|
-
|
|
1201
|
+
// calculate token expiration time
|
|
1202
|
+
const expirationTime = calculateExpirationTime(expiresIn);
|
|
1069
1203
|
|
|
1204
|
+
// cache new generated token, refreshToken and expiration time
|
|
1070
1205
|
tokenCache.set({
|
|
1071
1206
|
token,
|
|
1072
1207
|
expirationTime,
|
|
1073
1208
|
refreshToken
|
|
1074
|
-
});
|
|
1209
|
+
});
|
|
1075
1210
|
|
|
1211
|
+
// signal that a token fetch is complete
|
|
1076
1212
|
requestState.set(false);
|
|
1213
|
+
|
|
1077
1214
|
/**
|
|
1078
1215
|
* Freeze and copy pending queue, reset
|
|
1079
1216
|
* original one for accepting new pending tasks
|
|
1080
1217
|
*/
|
|
1218
|
+
const requestQueue = pendingTasks.slice();
|
|
1081
1219
|
|
|
1082
|
-
const requestQueue = pendingTasks.slice(); // const lastTask = requestQueue.pop()
|
|
1083
1220
|
// reset pendingTask queue
|
|
1084
|
-
|
|
1085
1221
|
pendingTasks = [];
|
|
1086
|
-
|
|
1087
1222
|
if (requestQueue.length === 1) {
|
|
1088
1223
|
return mergeAuthHeader(token, requestQueue.pop().request);
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1224
|
+
}
|
|
1091
1225
|
|
|
1226
|
+
// execute all pending tasks if any
|
|
1092
1227
|
for (let i = 0; i < requestQueue.length; i++) {
|
|
1093
1228
|
const task = requestQueue[i];
|
|
1094
|
-
const requestWithAuth = mergeAuthHeader(token, task.request);
|
|
1229
|
+
const requestWithAuth = mergeAuthHeader(token, task.request);
|
|
1095
1230
|
|
|
1231
|
+
// execute task
|
|
1096
1232
|
task.next(requestWithAuth);
|
|
1097
1233
|
}
|
|
1098
|
-
|
|
1099
1234
|
return;
|
|
1100
1235
|
}
|
|
1101
|
-
|
|
1102
1236
|
const error = new Error(response.data.message ? response.data.message : JSON.stringify(response.data));
|
|
1103
1237
|
/**
|
|
1104
1238
|
* reject the error immediately
|
|
1105
1239
|
* and free up the middleware chain
|
|
1106
1240
|
*/
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
headers: {
|
|
1241
|
+
request.reject({
|
|
1242
|
+
...request,
|
|
1243
|
+
headers: {
|
|
1244
|
+
...request.headers
|
|
1110
1245
|
},
|
|
1111
1246
|
response: {
|
|
1112
1247
|
statusCode: response.statusCode || response.data.statusCode,
|
|
@@ -1117,13 +1252,16 @@ async function executeRequest(options) {
|
|
|
1117
1252
|
}
|
|
1118
1253
|
});
|
|
1119
1254
|
} catch (error) {
|
|
1120
|
-
return {
|
|
1121
|
-
|
|
1255
|
+
return {
|
|
1256
|
+
...request,
|
|
1257
|
+
headers: {
|
|
1258
|
+
...request.headers
|
|
1122
1259
|
},
|
|
1123
1260
|
response: {
|
|
1124
1261
|
body: null,
|
|
1125
1262
|
statusCode: error.statusCode || 0,
|
|
1126
|
-
error: {
|
|
1263
|
+
error: {
|
|
1264
|
+
...response,
|
|
1127
1265
|
error,
|
|
1128
1266
|
body: response
|
|
1129
1267
|
}
|
|
@@ -1145,7 +1283,6 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
|
|
|
1145
1283
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
1146
1284
|
return next(request);
|
|
1147
1285
|
}
|
|
1148
|
-
|
|
1149
1286
|
const requestOptions = {
|
|
1150
1287
|
request,
|
|
1151
1288
|
requestState,
|
|
@@ -1156,10 +1293,10 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
|
|
|
1156
1293
|
...buildRequestForPasswordFlow(options),
|
|
1157
1294
|
userOption: options,
|
|
1158
1295
|
next
|
|
1159
|
-
};
|
|
1296
|
+
};
|
|
1160
1297
|
|
|
1298
|
+
// make request to coco
|
|
1161
1299
|
const requestWithAuth = await executeRequest(requestOptions);
|
|
1162
|
-
|
|
1163
1300
|
if (requestWithAuth) {
|
|
1164
1301
|
return next(requestWithAuth);
|
|
1165
1302
|
}
|
|
@@ -1181,9 +1318,9 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
|
1181
1318
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
1182
1319
|
// move on
|
|
1183
1320
|
return next(request);
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1321
|
+
}
|
|
1186
1322
|
|
|
1323
|
+
// prepare request options
|
|
1187
1324
|
const requestOptions = {
|
|
1188
1325
|
request,
|
|
1189
1326
|
requestState,
|
|
@@ -1194,10 +1331,10 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
|
1194
1331
|
...buildRequestForAnonymousSessionFlow(options),
|
|
1195
1332
|
userOption: options,
|
|
1196
1333
|
next
|
|
1197
|
-
};
|
|
1334
|
+
};
|
|
1198
1335
|
|
|
1336
|
+
// make request to coco
|
|
1199
1337
|
const requestWithAuth = await executeRequest(requestOptions);
|
|
1200
|
-
|
|
1201
1338
|
if (requestWithAuth) {
|
|
1202
1339
|
return next(requestWithAuth);
|
|
1203
1340
|
}
|
|
@@ -1219,9 +1356,9 @@ function createAuthMiddlewareForClientCredentialsFlow$1(options) {
|
|
|
1219
1356
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
1220
1357
|
// move on
|
|
1221
1358
|
return next(request);
|
|
1222
|
-
}
|
|
1223
|
-
|
|
1359
|
+
}
|
|
1224
1360
|
|
|
1361
|
+
// prepare request options
|
|
1225
1362
|
const requestOptions = {
|
|
1226
1363
|
request,
|
|
1227
1364
|
requestState,
|
|
@@ -1231,10 +1368,10 @@ function createAuthMiddlewareForClientCredentialsFlow$1(options) {
|
|
|
1231
1368
|
httpClient: options.httpClient || fetch__default["default"],
|
|
1232
1369
|
...buildRequestForClientCredentialsFlow(options),
|
|
1233
1370
|
next
|
|
1234
|
-
};
|
|
1371
|
+
};
|
|
1235
1372
|
|
|
1373
|
+
// make request to coco
|
|
1236
1374
|
const requestWithAuth = await executeRequest(requestOptions);
|
|
1237
|
-
|
|
1238
1375
|
if (requestWithAuth) {
|
|
1239
1376
|
// make the request and inject the token into the header
|
|
1240
1377
|
return next(requestWithAuth);
|
|
@@ -1254,9 +1391,9 @@ function createAuthMiddlewareForRefreshTokenFlow$1(options) {
|
|
|
1254
1391
|
return async request => {
|
|
1255
1392
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
1256
1393
|
return next(request);
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1394
|
+
}
|
|
1259
1395
|
|
|
1396
|
+
// prepare request options
|
|
1260
1397
|
const requestOptions = {
|
|
1261
1398
|
request,
|
|
1262
1399
|
requestState,
|
|
@@ -1265,10 +1402,10 @@ function createAuthMiddlewareForRefreshTokenFlow$1(options) {
|
|
|
1265
1402
|
httpClient: options.httpClient || fetch,
|
|
1266
1403
|
...buildRequestForRefreshTokenFlow(options),
|
|
1267
1404
|
next
|
|
1268
|
-
};
|
|
1405
|
+
};
|
|
1269
1406
|
|
|
1407
|
+
// make request to coco
|
|
1270
1408
|
const requestWithAuth = await executeRequest(requestOptions);
|
|
1271
|
-
|
|
1272
1409
|
if (requestWithAuth) {
|
|
1273
1410
|
return next(requestWithAuth);
|
|
1274
1411
|
}
|
|
@@ -1280,19 +1417,20 @@ function createAuthMiddlewareForExistingTokenFlow$1(authorization, options) {
|
|
|
1280
1417
|
return next => {
|
|
1281
1418
|
return async request => {
|
|
1282
1419
|
if (typeof authorization !== 'string') throw new Error('authorization must be a string');
|
|
1283
|
-
const
|
|
1420
|
+
const isForce = (options === null || options === void 0 ? void 0 : options.force) === undefined ? true : options.force;
|
|
1421
|
+
|
|
1284
1422
|
/**
|
|
1285
1423
|
* The request will not be modified if:
|
|
1286
1424
|
* 1. no argument is passed
|
|
1287
1425
|
* 2. force is false and authorization header exists
|
|
1288
1426
|
*/
|
|
1289
|
-
|
|
1290
|
-
if (!authorization || request.headers && (request.headers.Authorization || request.headers.authorization) && force === false) {
|
|
1427
|
+
if (!authorization || request.headers && (request.headers.Authorization || request.headers.authorization) && isForce === false) {
|
|
1291
1428
|
return next(request);
|
|
1292
1429
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
headers: {
|
|
1430
|
+
const requestWithAuth = {
|
|
1431
|
+
...request,
|
|
1432
|
+
headers: {
|
|
1433
|
+
...request.headers,
|
|
1296
1434
|
Authorization: authorization
|
|
1297
1435
|
}
|
|
1298
1436
|
};
|
|
@@ -1306,7 +1444,6 @@ var middleware = /*#__PURE__*/Object.freeze({
|
|
|
1306
1444
|
createCorrelationIdMiddleware: createCorrelationIdMiddleware$1,
|
|
1307
1445
|
createHttpMiddleware: createHttpMiddleware$1,
|
|
1308
1446
|
createQueueMiddleware: createQueueMiddleware$1,
|
|
1309
|
-
createRetryMiddleware: createRetryMiddleware$1,
|
|
1310
1447
|
createLoggerMiddleware: createLoggerMiddleware$1,
|
|
1311
1448
|
createUserAgentMiddleware: createUserAgentMiddleware$1,
|
|
1312
1449
|
createConcurrentModificationMiddleware: createConcurrentModificationMiddleware$1,
|
|
@@ -1325,7 +1462,6 @@ const {
|
|
|
1325
1462
|
createAuthMiddlewareForRefreshTokenFlow,
|
|
1326
1463
|
createAuthMiddlewareForExistingTokenFlow,
|
|
1327
1464
|
createCorrelationIdMiddleware,
|
|
1328
|
-
createRetryMiddleware,
|
|
1329
1465
|
createHttpMiddleware,
|
|
1330
1466
|
createLoggerMiddleware,
|
|
1331
1467
|
createQueueMiddleware,
|
|
@@ -1336,35 +1472,21 @@ const {
|
|
|
1336
1472
|
class ClientBuilder {
|
|
1337
1473
|
constructor() {
|
|
1338
1474
|
_defineProperty(this, "projectKey", void 0);
|
|
1339
|
-
|
|
1340
1475
|
_defineProperty(this, "authMiddleware", void 0);
|
|
1341
|
-
|
|
1342
1476
|
_defineProperty(this, "httpMiddleware", void 0);
|
|
1343
|
-
|
|
1344
1477
|
_defineProperty(this, "userAgentMiddleware", void 0);
|
|
1345
|
-
|
|
1346
1478
|
_defineProperty(this, "correlationIdMiddleware", void 0);
|
|
1347
|
-
|
|
1348
1479
|
_defineProperty(this, "loggerMiddleware", void 0);
|
|
1349
|
-
|
|
1350
1480
|
_defineProperty(this, "queueMiddleware", void 0);
|
|
1351
|
-
|
|
1352
|
-
_defineProperty(this, "retryMiddleware", void 0);
|
|
1353
|
-
|
|
1354
1481
|
_defineProperty(this, "concurrentMiddleware", void 0);
|
|
1355
|
-
|
|
1356
1482
|
_defineProperty(this, "errorMiddleware", void 0);
|
|
1357
|
-
|
|
1358
1483
|
_defineProperty(this, "middlewares", []);
|
|
1359
|
-
|
|
1360
1484
|
this.userAgentMiddleware = createUserAgentMiddleware({});
|
|
1361
1485
|
}
|
|
1362
|
-
|
|
1363
1486
|
withProjectKey(key) {
|
|
1364
1487
|
this.projectKey = key;
|
|
1365
1488
|
return this;
|
|
1366
1489
|
}
|
|
1367
|
-
|
|
1368
1490
|
defaultClient(baseUri, credentials, oauthUri, projectKey, scopes, httpClient) {
|
|
1369
1491
|
return this.withClientCredentialsFlow({
|
|
1370
1492
|
host: oauthUri,
|
|
@@ -1376,17 +1498,14 @@ class ClientBuilder {
|
|
|
1376
1498
|
httpClient: httpClient || fetch__default["default"]
|
|
1377
1499
|
});
|
|
1378
1500
|
}
|
|
1379
|
-
|
|
1380
1501
|
withAuthMiddleware(authMiddleware) {
|
|
1381
1502
|
this.authMiddleware = authMiddleware;
|
|
1382
1503
|
return this;
|
|
1383
1504
|
}
|
|
1384
|
-
|
|
1385
1505
|
withMiddleware(middleware) {
|
|
1386
1506
|
this.middlewares.push(middleware);
|
|
1387
1507
|
return this;
|
|
1388
1508
|
}
|
|
1389
|
-
|
|
1390
1509
|
withClientCredentialsFlow(options) {
|
|
1391
1510
|
return this.withAuthMiddleware(createAuthMiddlewareForClientCredentialsFlow({
|
|
1392
1511
|
host: options.host || CTP_AUTH_URL,
|
|
@@ -1401,7 +1520,6 @@ class ClientBuilder {
|
|
|
1401
1520
|
...options
|
|
1402
1521
|
}));
|
|
1403
1522
|
}
|
|
1404
|
-
|
|
1405
1523
|
withPasswordFlow(options) {
|
|
1406
1524
|
return this.withAuthMiddleware(createAuthMiddlewareForPasswordFlow({
|
|
1407
1525
|
host: options.host || CTP_AUTH_URL,
|
|
@@ -1418,7 +1536,6 @@ class ClientBuilder {
|
|
|
1418
1536
|
...options
|
|
1419
1537
|
}));
|
|
1420
1538
|
}
|
|
1421
|
-
|
|
1422
1539
|
withAnonymousSessionFlow(options) {
|
|
1423
1540
|
return this.withAuthMiddleware(createAuthMiddlewareForAnonymousSessionFlow({
|
|
1424
1541
|
host: options.host || CTP_AUTH_URL,
|
|
@@ -1432,7 +1549,6 @@ class ClientBuilder {
|
|
|
1432
1549
|
...options
|
|
1433
1550
|
}));
|
|
1434
1551
|
}
|
|
1435
|
-
|
|
1436
1552
|
withRefreshTokenFlow(options) {
|
|
1437
1553
|
return this.withAuthMiddleware(createAuthMiddlewareForRefreshTokenFlow({
|
|
1438
1554
|
host: options.host || CTP_AUTH_URL,
|
|
@@ -1446,20 +1562,12 @@ class ClientBuilder {
|
|
|
1446
1562
|
...options
|
|
1447
1563
|
}));
|
|
1448
1564
|
}
|
|
1449
|
-
|
|
1450
|
-
withRetryMiddleware(options) {
|
|
1451
|
-
this.retryMiddleware = createRetryMiddleware({ ...options
|
|
1452
|
-
});
|
|
1453
|
-
return this;
|
|
1454
|
-
}
|
|
1455
|
-
|
|
1456
1565
|
withExistingTokenFlow(authorization, options) {
|
|
1457
1566
|
return this.withAuthMiddleware(createAuthMiddlewareForExistingTokenFlow(authorization, {
|
|
1458
1567
|
force: options.force || true,
|
|
1459
1568
|
...options
|
|
1460
1569
|
}));
|
|
1461
1570
|
}
|
|
1462
|
-
|
|
1463
1571
|
withHttpMiddleware(options) {
|
|
1464
1572
|
this.httpMiddleware = createHttpMiddleware({
|
|
1465
1573
|
host: options.host || CTP_API_URL,
|
|
@@ -1468,12 +1576,10 @@ class ClientBuilder {
|
|
|
1468
1576
|
});
|
|
1469
1577
|
return this;
|
|
1470
1578
|
}
|
|
1471
|
-
|
|
1472
1579
|
withUserAgentMiddleware(options) {
|
|
1473
1580
|
this.userAgentMiddleware = createUserAgentMiddleware(options);
|
|
1474
1581
|
return this;
|
|
1475
1582
|
}
|
|
1476
|
-
|
|
1477
1583
|
withQueueMiddleware(options) {
|
|
1478
1584
|
this.queueMiddleware = createQueueMiddleware({
|
|
1479
1585
|
concurrency: options.concurrency || CONCURRENCT_REQUEST,
|
|
@@ -1481,54 +1587,45 @@ class ClientBuilder {
|
|
|
1481
1587
|
});
|
|
1482
1588
|
return this;
|
|
1483
1589
|
}
|
|
1484
|
-
|
|
1485
1590
|
withLoggerMiddleware(options) {
|
|
1486
1591
|
this.loggerMiddleware = createLoggerMiddleware(options);
|
|
1487
1592
|
return this;
|
|
1488
1593
|
}
|
|
1489
|
-
|
|
1490
1594
|
withCorrelationIdMiddleware(options) {
|
|
1491
1595
|
this.correlationIdMiddleware = createCorrelationIdMiddleware({
|
|
1492
|
-
generate: options
|
|
1596
|
+
generate: options === null || options === void 0 ? void 0 : options.generate,
|
|
1493
1597
|
...options
|
|
1494
1598
|
});
|
|
1495
1599
|
return this;
|
|
1496
1600
|
}
|
|
1497
|
-
|
|
1498
1601
|
withConcurrentModificationMiddleware() {
|
|
1499
1602
|
this.concurrentMiddleware = createConcurrentModificationMiddleware();
|
|
1500
1603
|
return this;
|
|
1501
1604
|
}
|
|
1502
|
-
|
|
1503
1605
|
withErrorMiddleware(options) {
|
|
1504
1606
|
this.errorMiddleware = createErrorMiddleware(options);
|
|
1505
1607
|
return this;
|
|
1506
|
-
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1608
|
+
}
|
|
1509
1609
|
build() {
|
|
1510
1610
|
const middlewares = this.middlewares.slice();
|
|
1611
|
+
|
|
1511
1612
|
/**
|
|
1512
1613
|
* - use default retry policy if not explicity added
|
|
1513
1614
|
* - add retry middleware to be used by concurrent modification
|
|
1514
1615
|
* middleware if not explicitly added as part of the middleware
|
|
1515
1616
|
*/
|
|
1516
|
-
|
|
1517
|
-
if (!this.retryMiddleware && this.concurrentMiddleware) this.withRetryMiddleware({});
|
|
1518
1617
|
if (this.correlationIdMiddleware) middlewares.push(this.correlationIdMiddleware);
|
|
1519
1618
|
if (this.userAgentMiddleware) middlewares.push(this.userAgentMiddleware);
|
|
1520
1619
|
if (this.authMiddleware) middlewares.push(this.authMiddleware);
|
|
1521
1620
|
if (this.queueMiddleware) middlewares.push(this.queueMiddleware);
|
|
1522
1621
|
if (this.loggerMiddleware) middlewares.push(this.loggerMiddleware);
|
|
1523
1622
|
if (this.errorMiddleware) middlewares.push(this.errorMiddleware);
|
|
1524
|
-
if (this.retryMiddleware) middlewares.push(this.retryMiddleware);
|
|
1525
1623
|
if (this.concurrentMiddleware) middlewares.push(this.concurrentMiddleware);
|
|
1526
1624
|
if (this.httpMiddleware) middlewares.push(this.httpMiddleware);
|
|
1527
1625
|
return createClient({
|
|
1528
1626
|
middlewares
|
|
1529
1627
|
});
|
|
1530
1628
|
}
|
|
1531
|
-
|
|
1532
1629
|
}
|
|
1533
1630
|
|
|
1534
1631
|
exports.ClientBuilder = ClientBuilder;
|