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