@commercetools/ts-client 2.0.5 → 2.1.0-alpha.1
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/dist/commercetools-ts-client.browser.cjs.js +197 -210
- package/dist/commercetools-ts-client.browser.esm.js +197 -210
- package/dist/commercetools-ts-client.cjs.dev.js +197 -210
- package/dist/commercetools-ts-client.cjs.prod.js +197 -210
- package/dist/commercetools-ts-client.esm.js +197 -210
- package/dist/commercetools-ts-client.umd.js +1 -14
- package/dist/declarations/src/middleware/auth-middleware/anonymous-session-flow.d.ts.map +1 -1
- package/dist/declarations/src/middleware/auth-middleware/client-credentials-flow.d.ts.map +1 -1
- package/dist/declarations/src/middleware/auth-middleware/password-flow.d.ts.map +1 -1
- package/dist/declarations/src/middleware/auth-middleware/refresh-token-flow.d.ts.map +1 -1
- package/dist/declarations/src/middleware/create-concurrent-modification-middleware.d.ts +1 -2
- package/dist/declarations/src/middleware/create-concurrent-modification-middleware.d.ts.map +1 -1
- package/dist/declarations/src/middleware/create-http-middleware.d.ts.map +1 -1
- package/dist/declarations/src/middleware/create-queue-middleware.d.ts.map +1 -1
- package/dist/declarations/src/types/types.d.ts +9 -9
- package/package.json +10 -7
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var fetch$1 = require('node-fetch');
|
|
6
|
-
var asyncMutex = require('async-mutex');
|
|
7
|
-
var uuid = require('uuid');
|
|
8
|
-
var _ = require('buffer/');
|
|
9
6
|
var AbortController = require('abort-controller');
|
|
10
7
|
|
|
11
8
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
@@ -13,7 +10,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e };
|
|
|
13
10
|
var fetch__default = /*#__PURE__*/_interopDefault(fetch$1);
|
|
14
11
|
var AbortController__default = /*#__PURE__*/_interopDefault(AbortController);
|
|
15
12
|
|
|
16
|
-
function
|
|
13
|
+
function _toPrimitive(t, r) {
|
|
17
14
|
if ("object" != typeof t || !t) return t;
|
|
18
15
|
var e = t[Symbol.toPrimitive];
|
|
19
16
|
if (void 0 !== e) {
|
|
@@ -24,24 +21,18 @@ function toPrimitive(t, r) {
|
|
|
24
21
|
return ("string" === r ? String : Number)(t);
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
function
|
|
28
|
-
var i =
|
|
24
|
+
function _toPropertyKey(t) {
|
|
25
|
+
var i = _toPrimitive(t, "string");
|
|
29
26
|
return "symbol" == typeof i ? i : i + "";
|
|
30
27
|
}
|
|
31
28
|
|
|
32
|
-
function _defineProperty(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
writable: true
|
|
40
|
-
});
|
|
41
|
-
} else {
|
|
42
|
-
obj[key] = value;
|
|
43
|
-
}
|
|
44
|
-
return obj;
|
|
29
|
+
function _defineProperty(e, r, t) {
|
|
30
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
31
|
+
value: t,
|
|
32
|
+
enumerable: !0,
|
|
33
|
+
configurable: !0,
|
|
34
|
+
writable: !0
|
|
35
|
+
}) : e[r] = t, e;
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
const HEADERS_CONTENT_TYPES = ['application/json', 'application/graphql'];
|
|
@@ -193,10 +184,12 @@ async function executor(request) {
|
|
|
193
184
|
let _response = {};
|
|
194
185
|
try {
|
|
195
186
|
_response = await execute();
|
|
196
|
-
if (_response.status >
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
187
|
+
if (_response.status > 399 && hasResponseRetryCode(retryCodes, _response)) {
|
|
188
|
+
return {
|
|
189
|
+
_response,
|
|
190
|
+
shouldRetry: true
|
|
191
|
+
};
|
|
192
|
+
}
|
|
200
193
|
} catch (e) {
|
|
201
194
|
if (e.name.includes('AbortError') && retryWhenAborted) {
|
|
202
195
|
return {
|
|
@@ -241,7 +234,7 @@ async function executor(request) {
|
|
|
241
234
|
try {
|
|
242
235
|
// try to parse the `fetch` response as text
|
|
243
236
|
if (response.text && typeof response.text == 'function') {
|
|
244
|
-
result = await response.text();
|
|
237
|
+
result = (await response.text()) || response[Object.getOwnPropertySymbols(response)[1]];
|
|
245
238
|
data = JSON.parse(result);
|
|
246
239
|
} else {
|
|
247
240
|
// axios response
|
|
@@ -266,8 +259,16 @@ async function executor(request) {
|
|
|
266
259
|
return data;
|
|
267
260
|
}
|
|
268
261
|
|
|
262
|
+
// import { v4 as uuidv4 } from 'uuid'
|
|
263
|
+
|
|
264
|
+
// export default function generateID() {
|
|
265
|
+
// return uuidv4()
|
|
266
|
+
// }
|
|
267
|
+
|
|
269
268
|
function generateID() {
|
|
270
|
-
|
|
269
|
+
// @ts-ignore
|
|
270
|
+
const str = ([1e6] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
|
|
271
|
+
return 'abcdef'[Math.floor(Math.random() * 'abcdef'.length)] + '' + str;
|
|
271
272
|
}
|
|
272
273
|
|
|
273
274
|
function parse(headers) {
|
|
@@ -456,6 +457,13 @@ function createUserAgent(options) {
|
|
|
456
457
|
return [baseInfo, systemInfo, libraryInfo, contactInfo, customAgent].filter(Boolean).join(' ');
|
|
457
458
|
}
|
|
458
459
|
|
|
460
|
+
function byteLength(body) {
|
|
461
|
+
if (typeof body === 'string') return body.length.toString();
|
|
462
|
+
if (body && typeof body === 'object') return new TextEncoder().encode(JSON.stringify(body)).length.toString();
|
|
463
|
+
if (body instanceof Uint8Array) return body.length.toString();
|
|
464
|
+
return '0';
|
|
465
|
+
}
|
|
466
|
+
|
|
459
467
|
/**
|
|
460
468
|
* validate some essential http options
|
|
461
469
|
* @param options
|
|
@@ -516,7 +524,8 @@ function buildRequestForClientCredentialsFlow(options) {
|
|
|
516
524
|
} = options.credentials || {};
|
|
517
525
|
if (!(clientId && clientSecret)) throw new Error('Missing required credentials (clientId, clientSecret)');
|
|
518
526
|
const scope = options.scopes ? options.scopes.join(' ') : undefined;
|
|
519
|
-
const basicAuth =
|
|
527
|
+
const basicAuth = btoa(`${clientId}:${clientSecret}`);
|
|
528
|
+
|
|
520
529
|
// This is mostly useful for internal testing purposes to be able to check
|
|
521
530
|
// other oauth endpoints.
|
|
522
531
|
const oauthUri = options.oauthUri || '/oauth/token';
|
|
@@ -562,9 +571,10 @@ function buildRequestForRefreshTokenFlow(options) {
|
|
|
562
571
|
clientSecret
|
|
563
572
|
} = options.credentials;
|
|
564
573
|
if (!(clientId && clientSecret)) throw new Error('Missing required credentials (clientId, clientSecret)');
|
|
565
|
-
const basicAuth =
|
|
566
|
-
|
|
567
|
-
//
|
|
574
|
+
const basicAuth = btoa(`${clientId}:${clientSecret}`);
|
|
575
|
+
|
|
576
|
+
// This is mostly useful for internal testing
|
|
577
|
+
// purposes to be able to check other oauth endpoints.
|
|
568
578
|
const oauthUri = options.oauthUri || '/oauth/token';
|
|
569
579
|
const url = options.host.replace(/\/$/, '') + oauthUri;
|
|
570
580
|
const body = `grant_type=refresh_token&refresh_token=${encodeURIComponent(options.refreshToken)}`;
|
|
@@ -598,7 +608,7 @@ function buildRequestForPasswordFlow(options) {
|
|
|
598
608
|
if (!(username && password)) throw new Error('Missing required user credentials (username, password)');
|
|
599
609
|
const scope = (options.scopes || []).join(' ');
|
|
600
610
|
const scopeStr = scope ? `&scope=${scope}` : '';
|
|
601
|
-
const basicAuth =
|
|
611
|
+
const basicAuth = btoa(`${clientId}:${clientSecret}`);
|
|
602
612
|
|
|
603
613
|
/**
|
|
604
614
|
* This is mostly useful for internal testing purposes to be able to check
|
|
@@ -618,44 +628,16 @@ function buildRequestForPasswordFlow(options) {
|
|
|
618
628
|
|
|
619
629
|
async function executeRequest$1(options) {
|
|
620
630
|
const {
|
|
621
|
-
request,
|
|
622
631
|
httpClient,
|
|
623
632
|
tokenCache,
|
|
624
|
-
tokenCacheKey,
|
|
625
633
|
userOption,
|
|
626
|
-
|
|
634
|
+
tokenCacheObject
|
|
627
635
|
} = options;
|
|
628
636
|
let url = options.url;
|
|
629
637
|
let body = options.body;
|
|
630
638
|
let basicAuth = options.basicAuth;
|
|
631
|
-
|
|
632
|
-
// get the pending object from option
|
|
633
|
-
let pendingTasks = options.pendingTasks;
|
|
634
639
|
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.');
|
|
635
640
|
|
|
636
|
-
/**
|
|
637
|
-
* If there is a token in the tokenCache, and it's not
|
|
638
|
-
* expired, append the token in the `Authorization` header.
|
|
639
|
-
*/
|
|
640
|
-
const tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
641
|
-
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
642
|
-
const requestWithAuth = mergeAuthHeader(tokenCacheObject.token, request);
|
|
643
|
-
return {
|
|
644
|
-
...requestWithAuth
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
/**
|
|
649
|
-
* Keep pending tasks until a token is fetched
|
|
650
|
-
* Save next function as well, to call it once the token has been fetched, which prevents
|
|
651
|
-
* unexpected behaviour in a context in which the next function uses global vars
|
|
652
|
-
* or Promises to capture the token to hand it to other libraries, e.g. Apollo
|
|
653
|
-
*/
|
|
654
|
-
pendingTasks.push({
|
|
655
|
-
request,
|
|
656
|
-
next
|
|
657
|
-
});
|
|
658
|
-
|
|
659
641
|
/**
|
|
660
642
|
* use refreshToken flow if there is refresh-token
|
|
661
643
|
* and there's either no token or the token is expired
|
|
@@ -686,7 +668,7 @@ async function executeRequest$1(options) {
|
|
|
686
668
|
headers: {
|
|
687
669
|
Authorization: `Basic ${basicAuth}`,
|
|
688
670
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
689
|
-
'Content-Length':
|
|
671
|
+
'Content-Length': byteLength(body)
|
|
690
672
|
},
|
|
691
673
|
httpClient,
|
|
692
674
|
body
|
|
@@ -707,73 +689,29 @@ async function executeRequest$1(options) {
|
|
|
707
689
|
expirationTime,
|
|
708
690
|
refreshToken
|
|
709
691
|
});
|
|
710
|
-
|
|
711
|
-
/**
|
|
712
|
-
* Freeze and copy pending queue, reset
|
|
713
|
-
* original one for accepting new pending tasks
|
|
714
|
-
*/
|
|
715
|
-
const requestQueue = pendingTasks.slice();
|
|
716
|
-
|
|
717
|
-
// reset pendingTask queue
|
|
718
|
-
pendingTasks = [];
|
|
719
|
-
if (requestQueue.length === 1) {
|
|
720
|
-
return mergeAuthHeader(token, requestQueue.pop().request);
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
// execute all pending tasks if any
|
|
724
|
-
for (let i = 0; i < requestQueue.length; i++) {
|
|
725
|
-
const task = requestQueue[i];
|
|
726
|
-
const requestWithAuth = mergeAuthHeader(token, task.request);
|
|
727
|
-
|
|
728
|
-
// execute task
|
|
729
|
-
task.next(requestWithAuth);
|
|
730
|
-
}
|
|
731
|
-
return;
|
|
692
|
+
return Promise.resolve(true);
|
|
732
693
|
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
headers: {
|
|
741
|
-
...request.headers
|
|
742
|
-
},
|
|
743
|
-
response: {
|
|
744
|
-
statusCode: response.statusCode || response.data.statusCode,
|
|
745
|
-
error: {
|
|
746
|
-
error,
|
|
747
|
-
body: response
|
|
748
|
-
}
|
|
749
|
-
}
|
|
694
|
+
|
|
695
|
+
// bubble up the error for the catch block
|
|
696
|
+
throw createError({
|
|
697
|
+
code: response.data.error,
|
|
698
|
+
statusCode: response.data.statusCode,
|
|
699
|
+
message: response.data.message,
|
|
700
|
+
error: response.data.errors
|
|
750
701
|
});
|
|
751
702
|
} catch (error) {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
headers: {
|
|
755
|
-
...request.headers
|
|
756
|
-
},
|
|
757
|
-
response: {
|
|
758
|
-
body: null,
|
|
759
|
-
statusCode: error.statusCode || 0,
|
|
760
|
-
error: {
|
|
761
|
-
...response,
|
|
762
|
-
error,
|
|
763
|
-
body: response
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
};
|
|
703
|
+
// throw error and free up the middleware chain
|
|
704
|
+
throw error;
|
|
767
705
|
}
|
|
768
706
|
}
|
|
769
707
|
|
|
770
708
|
function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
771
|
-
const pendingTasks = [];
|
|
772
|
-
const requestState = new asyncMutex.Mutex();
|
|
773
709
|
const tokenCache = options.tokenCache || store({
|
|
774
710
|
token: '',
|
|
775
711
|
expirationTime: -1
|
|
776
712
|
});
|
|
713
|
+
let tokenCacheObject;
|
|
714
|
+
let tokenFetchPromise = null;
|
|
777
715
|
const tokenCacheKey = buildTokenCacheKey(options);
|
|
778
716
|
return next => {
|
|
779
717
|
return async request => {
|
|
@@ -783,12 +721,19 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
|
783
721
|
return next(request);
|
|
784
722
|
}
|
|
785
723
|
|
|
724
|
+
/**
|
|
725
|
+
* If there is a token in the tokenCache, and it's not
|
|
726
|
+
* expired, append the token in the `Authorization` header.
|
|
727
|
+
*/
|
|
728
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
729
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
730
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
731
|
+
}
|
|
732
|
+
|
|
786
733
|
// prepare request options
|
|
787
734
|
const requestOptions = {
|
|
788
735
|
request,
|
|
789
|
-
requestState,
|
|
790
736
|
tokenCache,
|
|
791
|
-
pendingTasks,
|
|
792
737
|
tokenCacheKey,
|
|
793
738
|
httpClient: options.httpClient || fetch__default["default"],
|
|
794
739
|
...buildRequestForAnonymousSessionFlow(options),
|
|
@@ -796,28 +741,32 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
|
796
741
|
next
|
|
797
742
|
};
|
|
798
743
|
|
|
799
|
-
//
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
744
|
+
// If a token is already being fetched, wait for it to finish
|
|
745
|
+
if (tokenFetchPromise) {
|
|
746
|
+
await tokenFetchPromise;
|
|
747
|
+
} else {
|
|
748
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
749
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
750
|
+
await tokenFetchPromise;
|
|
751
|
+
tokenFetchPromise = null;
|
|
806
752
|
}
|
|
807
|
-
|
|
808
|
-
|
|
753
|
+
|
|
754
|
+
// Now the token is present in the tokenCache
|
|
755
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
756
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
757
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
809
758
|
}
|
|
810
759
|
};
|
|
811
760
|
};
|
|
812
761
|
}
|
|
813
762
|
|
|
814
763
|
function createAuthMiddlewareForClientCredentialsFlow$1(options) {
|
|
815
|
-
const requestState = new asyncMutex.Mutex();
|
|
816
|
-
const pendingTasks = [];
|
|
817
764
|
const tokenCache = options.tokenCache || store({
|
|
818
765
|
token: '',
|
|
819
766
|
expirationTime: -1
|
|
820
767
|
});
|
|
768
|
+
let tokenCacheObject;
|
|
769
|
+
let tokenFetchPromise = null;
|
|
821
770
|
const tokenCacheKey = buildTokenCacheKey(options);
|
|
822
771
|
return next => {
|
|
823
772
|
return async request => {
|
|
@@ -827,29 +776,40 @@ function createAuthMiddlewareForClientCredentialsFlow$1(options) {
|
|
|
827
776
|
return next(request);
|
|
828
777
|
}
|
|
829
778
|
|
|
779
|
+
/**
|
|
780
|
+
* If there is a token in the tokenCache, and it's not
|
|
781
|
+
* expired, append the token in the `Authorization` header.
|
|
782
|
+
*/
|
|
783
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
784
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
785
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
786
|
+
}
|
|
787
|
+
|
|
830
788
|
// prepare request options
|
|
831
789
|
const requestOptions = {
|
|
832
790
|
request,
|
|
833
|
-
requestState,
|
|
834
791
|
tokenCache,
|
|
835
|
-
pendingTasks,
|
|
836
792
|
tokenCacheKey,
|
|
793
|
+
tokenCacheObject,
|
|
837
794
|
httpClient: options.httpClient || fetch__default["default"],
|
|
838
795
|
...buildRequestForClientCredentialsFlow(options),
|
|
839
796
|
next
|
|
840
797
|
};
|
|
841
798
|
|
|
842
|
-
//
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
799
|
+
// If a token is already being fetched, wait for it to finish
|
|
800
|
+
if (tokenFetchPromise) {
|
|
801
|
+
await tokenFetchPromise;
|
|
802
|
+
} else {
|
|
803
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
804
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
805
|
+
await tokenFetchPromise;
|
|
806
|
+
tokenFetchPromise = null;
|
|
849
807
|
}
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
808
|
+
|
|
809
|
+
// Now the token is present in the tokenCache
|
|
810
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
811
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
812
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
853
813
|
}
|
|
854
814
|
};
|
|
855
815
|
};
|
|
@@ -886,19 +846,27 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
|
|
|
886
846
|
token: '',
|
|
887
847
|
expirationTime: -1
|
|
888
848
|
});
|
|
889
|
-
|
|
890
|
-
|
|
849
|
+
let tokenCacheObject;
|
|
850
|
+
let tokenFetchPromise = null;
|
|
891
851
|
const tokenCacheKey = buildTokenCacheKey(options);
|
|
892
852
|
return next => {
|
|
893
853
|
return async request => {
|
|
854
|
+
// if here is a token in the header, then move on to the next middleware
|
|
894
855
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
895
856
|
return next(request);
|
|
896
857
|
}
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* If there is a token in the tokenCache, and it's not
|
|
861
|
+
* expired, append the token in the `Authorization` header.
|
|
862
|
+
*/
|
|
863
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
864
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
865
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
866
|
+
}
|
|
897
867
|
const requestOptions = {
|
|
898
868
|
request,
|
|
899
|
-
requestState,
|
|
900
869
|
tokenCache,
|
|
901
|
-
pendingTasks,
|
|
902
870
|
tokenCacheKey,
|
|
903
871
|
httpClient: options.httpClient || fetch__default["default"],
|
|
904
872
|
...buildRequestForPasswordFlow(options),
|
|
@@ -906,16 +874,20 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
|
|
|
906
874
|
next
|
|
907
875
|
};
|
|
908
876
|
|
|
909
|
-
//
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
877
|
+
// If a token is already being fetched, wait for it to finish
|
|
878
|
+
if (tokenFetchPromise) {
|
|
879
|
+
await tokenFetchPromise;
|
|
880
|
+
} else {
|
|
881
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
882
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
883
|
+
await tokenFetchPromise;
|
|
884
|
+
tokenFetchPromise = null;
|
|
916
885
|
}
|
|
917
|
-
|
|
918
|
-
|
|
886
|
+
|
|
887
|
+
// Now the token is present in the tokenCache
|
|
888
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
889
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
890
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
919
891
|
}
|
|
920
892
|
};
|
|
921
893
|
};
|
|
@@ -926,35 +898,47 @@ function createAuthMiddlewareForRefreshTokenFlow$1(options) {
|
|
|
926
898
|
token: '',
|
|
927
899
|
tokenCacheKey: null
|
|
928
900
|
});
|
|
929
|
-
|
|
930
|
-
|
|
901
|
+
let tokenCacheObject;
|
|
902
|
+
let tokenFetchPromise = null;
|
|
903
|
+
const tokenCacheKey = buildTokenCacheKey(options);
|
|
931
904
|
return next => {
|
|
932
905
|
return async request => {
|
|
933
906
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
934
907
|
return next(request);
|
|
935
908
|
}
|
|
936
909
|
|
|
910
|
+
/**
|
|
911
|
+
* If there is a token in the tokenCache, and it's not
|
|
912
|
+
* expired, append the token in the `Authorization` header.
|
|
913
|
+
*/
|
|
914
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
915
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
916
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
917
|
+
}
|
|
918
|
+
|
|
937
919
|
// prepare request options
|
|
938
920
|
const requestOptions = {
|
|
939
921
|
request,
|
|
940
|
-
requestState,
|
|
941
922
|
tokenCache,
|
|
942
|
-
pendingTasks,
|
|
943
923
|
httpClient: options.httpClient || fetch,
|
|
944
924
|
...buildRequestForRefreshTokenFlow(options),
|
|
945
925
|
next
|
|
946
926
|
};
|
|
947
927
|
|
|
948
|
-
//
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
928
|
+
// If a token is already being fetched, wait for it to finish
|
|
929
|
+
if (tokenFetchPromise) {
|
|
930
|
+
await tokenFetchPromise;
|
|
931
|
+
} else {
|
|
932
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
933
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
934
|
+
await tokenFetchPromise;
|
|
935
|
+
tokenFetchPromise = null;
|
|
955
936
|
}
|
|
956
|
-
|
|
957
|
-
|
|
937
|
+
|
|
938
|
+
// Now the token is present in the tokenCache
|
|
939
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
940
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
941
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
958
942
|
}
|
|
959
943
|
};
|
|
960
944
|
};
|
|
@@ -1117,7 +1101,7 @@ async function executeRequest({
|
|
|
1117
1101
|
uri: request.uri
|
|
1118
1102
|
})
|
|
1119
1103
|
});
|
|
1120
|
-
|
|
1104
|
+
throw {
|
|
1121
1105
|
body: error,
|
|
1122
1106
|
error
|
|
1123
1107
|
};
|
|
@@ -1164,7 +1148,7 @@ function createHttpMiddleware$1(options) {
|
|
|
1164
1148
|
// Ensure body is a string if content type is application/{json|graphql}
|
|
1165
1149
|
const body = HEADERS_CONTENT_TYPES.indexOf(requestHeader['Content-Type']) > -1 && typeof request.body === 'string' || isBuffer(request.body) ? request.body : JSON.stringify(request.body || undefined);
|
|
1166
1150
|
if (body && (typeof body === 'string' || isBuffer(body))) {
|
|
1167
|
-
requestHeader['Content-Length'] =
|
|
1151
|
+
requestHeader['Content-Length'] = byteLength(body);
|
|
1168
1152
|
}
|
|
1169
1153
|
const clientOptions = {
|
|
1170
1154
|
enableRetry,
|
|
@@ -1228,49 +1212,52 @@ function createQueueMiddleware$1({
|
|
|
1228
1212
|
concurrency = 20
|
|
1229
1213
|
}) {
|
|
1230
1214
|
let runningCount = 0;
|
|
1231
|
-
const
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
const
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
});
|
|
1245
|
-
return next => request => {
|
|
1246
|
-
// wrap and override resolve and reject functions
|
|
1247
|
-
const patchedRequest = {
|
|
1248
|
-
...request,
|
|
1249
|
-
resolve(data) {
|
|
1250
|
-
request.resolve(data);
|
|
1251
|
-
dequeue(next);
|
|
1252
|
-
},
|
|
1253
|
-
reject(error) {
|
|
1254
|
-
request.reject(error);
|
|
1255
|
-
dequeue(next);
|
|
1256
|
-
}
|
|
1257
|
-
};
|
|
1215
|
+
const waitForSlot = () => {
|
|
1216
|
+
// dont allow negative or 0 value for concurrency
|
|
1217
|
+
if (0 >= concurrency) return Promise.resolve();
|
|
1218
|
+
return new Promise(resolve => {
|
|
1219
|
+
const tryNext = () => {
|
|
1220
|
+
if (runningCount < concurrency) {
|
|
1221
|
+
runningCount++;
|
|
1222
|
+
resolve();
|
|
1223
|
+
} else {
|
|
1224
|
+
// Retry until a slot becomes available
|
|
1225
|
+
setTimeout(tryNext, 10);
|
|
1226
|
+
}
|
|
1227
|
+
};
|
|
1258
1228
|
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
request: patchedRequest
|
|
1229
|
+
// start
|
|
1230
|
+
tryNext();
|
|
1262
1231
|
});
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
return
|
|
1267
|
-
|
|
1232
|
+
};
|
|
1233
|
+
return next => {
|
|
1234
|
+
return async request => {
|
|
1235
|
+
return waitForSlot().then(async () => {
|
|
1236
|
+
// Patch the request to override resolve and reject
|
|
1237
|
+
const patchedRequest = {
|
|
1238
|
+
...request,
|
|
1239
|
+
resolve(data) {
|
|
1240
|
+
request.resolve(data);
|
|
1241
|
+
runningCount--;
|
|
1242
|
+
},
|
|
1243
|
+
reject(error) {
|
|
1244
|
+
request.reject(error);
|
|
1245
|
+
runningCount--;
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
try {
|
|
1249
|
+
return next(patchedRequest);
|
|
1250
|
+
} finally {
|
|
1251
|
+
runningCount--;
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
};
|
|
1268
1255
|
};
|
|
1269
1256
|
}
|
|
1270
1257
|
|
|
1271
1258
|
var packageJson = {
|
|
1272
1259
|
name: "@commercetools/ts-client",
|
|
1273
|
-
version: "2.0.
|
|
1260
|
+
version: "2.1.0-alpha.1",
|
|
1274
1261
|
engines: {
|
|
1275
1262
|
node: ">=14"
|
|
1276
1263
|
},
|
|
@@ -1304,14 +1291,14 @@ var packageJson = {
|
|
|
1304
1291
|
},
|
|
1305
1292
|
dependencies: {
|
|
1306
1293
|
"abort-controller": "3.0.0",
|
|
1307
|
-
"
|
|
1308
|
-
buffer: "^6.0.3",
|
|
1309
|
-
"node-fetch": "^2.6.1",
|
|
1310
|
-
uuid: "10.0.0"
|
|
1294
|
+
"node-fetch": "^2.6.1"
|
|
1311
1295
|
},
|
|
1312
1296
|
files: [
|
|
1313
1297
|
"dist",
|
|
1314
|
-
"CHANGELOG.md"
|
|
1298
|
+
"CHANGELOG.md",
|
|
1299
|
+
"LICENSE",
|
|
1300
|
+
"README.md",
|
|
1301
|
+
"package.json"
|
|
1315
1302
|
],
|
|
1316
1303
|
author: "Chukwuemeka Ajima <meeky.ae@gmail.com>",
|
|
1317
1304
|
main: "dist/commercetools-ts-client.cjs.js",
|