@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
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import fetch$1 from 'node-fetch';
|
|
2
|
-
import { Mutex } from 'async-mutex';
|
|
3
|
-
import { v4 } from 'uuid';
|
|
4
|
-
import { Buffer } from 'buffer/';
|
|
5
2
|
import AbortController from 'abort-controller';
|
|
6
3
|
|
|
7
|
-
function
|
|
4
|
+
function _toPrimitive(t, r) {
|
|
8
5
|
if ("object" != typeof t || !t) return t;
|
|
9
6
|
var e = t[Symbol.toPrimitive];
|
|
10
7
|
if (void 0 !== e) {
|
|
@@ -15,24 +12,18 @@ function toPrimitive(t, r) {
|
|
|
15
12
|
return ("string" === r ? String : Number)(t);
|
|
16
13
|
}
|
|
17
14
|
|
|
18
|
-
function
|
|
19
|
-
var i =
|
|
15
|
+
function _toPropertyKey(t) {
|
|
16
|
+
var i = _toPrimitive(t, "string");
|
|
20
17
|
return "symbol" == typeof i ? i : i + "";
|
|
21
18
|
}
|
|
22
19
|
|
|
23
|
-
function _defineProperty(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
writable: true
|
|
31
|
-
});
|
|
32
|
-
} else {
|
|
33
|
-
obj[key] = value;
|
|
34
|
-
}
|
|
35
|
-
return obj;
|
|
20
|
+
function _defineProperty(e, r, t) {
|
|
21
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
22
|
+
value: t,
|
|
23
|
+
enumerable: !0,
|
|
24
|
+
configurable: !0,
|
|
25
|
+
writable: !0
|
|
26
|
+
}) : e[r] = t, e;
|
|
36
27
|
}
|
|
37
28
|
|
|
38
29
|
const HEADERS_CONTENT_TYPES = ['application/json', 'application/graphql'];
|
|
@@ -184,10 +175,12 @@ async function executor(request) {
|
|
|
184
175
|
let _response = {};
|
|
185
176
|
try {
|
|
186
177
|
_response = await execute();
|
|
187
|
-
if (_response.status >
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
178
|
+
if (_response.status > 399 && hasResponseRetryCode(retryCodes, _response)) {
|
|
179
|
+
return {
|
|
180
|
+
_response,
|
|
181
|
+
shouldRetry: true
|
|
182
|
+
};
|
|
183
|
+
}
|
|
191
184
|
} catch (e) {
|
|
192
185
|
if (e.name.includes('AbortError') && retryWhenAborted) {
|
|
193
186
|
return {
|
|
@@ -232,7 +225,7 @@ async function executor(request) {
|
|
|
232
225
|
try {
|
|
233
226
|
// try to parse the `fetch` response as text
|
|
234
227
|
if (response.text && typeof response.text == 'function') {
|
|
235
|
-
result = await response.text();
|
|
228
|
+
result = (await response.text()) || response[Object.getOwnPropertySymbols(response)[1]];
|
|
236
229
|
data = JSON.parse(result);
|
|
237
230
|
} else {
|
|
238
231
|
// axios response
|
|
@@ -257,8 +250,16 @@ async function executor(request) {
|
|
|
257
250
|
return data;
|
|
258
251
|
}
|
|
259
252
|
|
|
253
|
+
// import { v4 as uuidv4 } from 'uuid'
|
|
254
|
+
|
|
255
|
+
// export default function generateID() {
|
|
256
|
+
// return uuidv4()
|
|
257
|
+
// }
|
|
258
|
+
|
|
260
259
|
function generateID() {
|
|
261
|
-
|
|
260
|
+
// @ts-ignore
|
|
261
|
+
const str = ([1e6] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
|
|
262
|
+
return 'abcdef'[Math.floor(Math.random() * 'abcdef'.length)] + '' + str;
|
|
262
263
|
}
|
|
263
264
|
|
|
264
265
|
function parse(headers) {
|
|
@@ -447,6 +448,13 @@ function createUserAgent(options) {
|
|
|
447
448
|
return [baseInfo, systemInfo, libraryInfo, contactInfo, customAgent].filter(Boolean).join(' ');
|
|
448
449
|
}
|
|
449
450
|
|
|
451
|
+
function byteLength(body) {
|
|
452
|
+
if (typeof body === 'string') return body.length.toString();
|
|
453
|
+
if (body && typeof body === 'object') return new TextEncoder().encode(JSON.stringify(body)).length.toString();
|
|
454
|
+
if (body instanceof Uint8Array) return body.length.toString();
|
|
455
|
+
return '0';
|
|
456
|
+
}
|
|
457
|
+
|
|
450
458
|
/**
|
|
451
459
|
* validate some essential http options
|
|
452
460
|
* @param options
|
|
@@ -507,7 +515,8 @@ function buildRequestForClientCredentialsFlow(options) {
|
|
|
507
515
|
} = options.credentials || {};
|
|
508
516
|
if (!(clientId && clientSecret)) throw new Error('Missing required credentials (clientId, clientSecret)');
|
|
509
517
|
const scope = options.scopes ? options.scopes.join(' ') : undefined;
|
|
510
|
-
const basicAuth =
|
|
518
|
+
const basicAuth = btoa(`${clientId}:${clientSecret}`);
|
|
519
|
+
|
|
511
520
|
// This is mostly useful for internal testing purposes to be able to check
|
|
512
521
|
// other oauth endpoints.
|
|
513
522
|
const oauthUri = options.oauthUri || '/oauth/token';
|
|
@@ -553,9 +562,10 @@ function buildRequestForRefreshTokenFlow(options) {
|
|
|
553
562
|
clientSecret
|
|
554
563
|
} = options.credentials;
|
|
555
564
|
if (!(clientId && clientSecret)) throw new Error('Missing required credentials (clientId, clientSecret)');
|
|
556
|
-
const basicAuth =
|
|
557
|
-
|
|
558
|
-
//
|
|
565
|
+
const basicAuth = btoa(`${clientId}:${clientSecret}`);
|
|
566
|
+
|
|
567
|
+
// This is mostly useful for internal testing
|
|
568
|
+
// purposes to be able to check other oauth endpoints.
|
|
559
569
|
const oauthUri = options.oauthUri || '/oauth/token';
|
|
560
570
|
const url = options.host.replace(/\/$/, '') + oauthUri;
|
|
561
571
|
const body = `grant_type=refresh_token&refresh_token=${encodeURIComponent(options.refreshToken)}`;
|
|
@@ -589,7 +599,7 @@ function buildRequestForPasswordFlow(options) {
|
|
|
589
599
|
if (!(username && password)) throw new Error('Missing required user credentials (username, password)');
|
|
590
600
|
const scope = (options.scopes || []).join(' ');
|
|
591
601
|
const scopeStr = scope ? `&scope=${scope}` : '';
|
|
592
|
-
const basicAuth =
|
|
602
|
+
const basicAuth = btoa(`${clientId}:${clientSecret}`);
|
|
593
603
|
|
|
594
604
|
/**
|
|
595
605
|
* This is mostly useful for internal testing purposes to be able to check
|
|
@@ -609,44 +619,16 @@ function buildRequestForPasswordFlow(options) {
|
|
|
609
619
|
|
|
610
620
|
async function executeRequest$1(options) {
|
|
611
621
|
const {
|
|
612
|
-
request,
|
|
613
622
|
httpClient,
|
|
614
623
|
tokenCache,
|
|
615
|
-
tokenCacheKey,
|
|
616
624
|
userOption,
|
|
617
|
-
|
|
625
|
+
tokenCacheObject
|
|
618
626
|
} = options;
|
|
619
627
|
let url = options.url;
|
|
620
628
|
let body = options.body;
|
|
621
629
|
let basicAuth = options.basicAuth;
|
|
622
|
-
|
|
623
|
-
// get the pending object from option
|
|
624
|
-
let pendingTasks = options.pendingTasks;
|
|
625
630
|
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.');
|
|
626
631
|
|
|
627
|
-
/**
|
|
628
|
-
* If there is a token in the tokenCache, and it's not
|
|
629
|
-
* expired, append the token in the `Authorization` header.
|
|
630
|
-
*/
|
|
631
|
-
const tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
632
|
-
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
633
|
-
const requestWithAuth = mergeAuthHeader(tokenCacheObject.token, request);
|
|
634
|
-
return {
|
|
635
|
-
...requestWithAuth
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Keep pending tasks until a token is fetched
|
|
641
|
-
* Save next function as well, to call it once the token has been fetched, which prevents
|
|
642
|
-
* unexpected behaviour in a context in which the next function uses global vars
|
|
643
|
-
* or Promises to capture the token to hand it to other libraries, e.g. Apollo
|
|
644
|
-
*/
|
|
645
|
-
pendingTasks.push({
|
|
646
|
-
request,
|
|
647
|
-
next
|
|
648
|
-
});
|
|
649
|
-
|
|
650
632
|
/**
|
|
651
633
|
* use refreshToken flow if there is refresh-token
|
|
652
634
|
* and there's either no token or the token is expired
|
|
@@ -677,7 +659,7 @@ async function executeRequest$1(options) {
|
|
|
677
659
|
headers: {
|
|
678
660
|
Authorization: `Basic ${basicAuth}`,
|
|
679
661
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
680
|
-
'Content-Length':
|
|
662
|
+
'Content-Length': byteLength(body)
|
|
681
663
|
},
|
|
682
664
|
httpClient,
|
|
683
665
|
body
|
|
@@ -698,73 +680,29 @@ async function executeRequest$1(options) {
|
|
|
698
680
|
expirationTime,
|
|
699
681
|
refreshToken
|
|
700
682
|
});
|
|
701
|
-
|
|
702
|
-
/**
|
|
703
|
-
* Freeze and copy pending queue, reset
|
|
704
|
-
* original one for accepting new pending tasks
|
|
705
|
-
*/
|
|
706
|
-
const requestQueue = pendingTasks.slice();
|
|
707
|
-
|
|
708
|
-
// reset pendingTask queue
|
|
709
|
-
pendingTasks = [];
|
|
710
|
-
if (requestQueue.length === 1) {
|
|
711
|
-
return mergeAuthHeader(token, requestQueue.pop().request);
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
// execute all pending tasks if any
|
|
715
|
-
for (let i = 0; i < requestQueue.length; i++) {
|
|
716
|
-
const task = requestQueue[i];
|
|
717
|
-
const requestWithAuth = mergeAuthHeader(token, task.request);
|
|
718
|
-
|
|
719
|
-
// execute task
|
|
720
|
-
task.next(requestWithAuth);
|
|
721
|
-
}
|
|
722
|
-
return;
|
|
683
|
+
return Promise.resolve(true);
|
|
723
684
|
}
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
headers: {
|
|
732
|
-
...request.headers
|
|
733
|
-
},
|
|
734
|
-
response: {
|
|
735
|
-
statusCode: response.statusCode || response.data.statusCode,
|
|
736
|
-
error: {
|
|
737
|
-
error,
|
|
738
|
-
body: response
|
|
739
|
-
}
|
|
740
|
-
}
|
|
685
|
+
|
|
686
|
+
// bubble up the error for the catch block
|
|
687
|
+
throw createError({
|
|
688
|
+
code: response.data.error,
|
|
689
|
+
statusCode: response.data.statusCode,
|
|
690
|
+
message: response.data.message,
|
|
691
|
+
error: response.data.errors
|
|
741
692
|
});
|
|
742
693
|
} catch (error) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
headers: {
|
|
746
|
-
...request.headers
|
|
747
|
-
},
|
|
748
|
-
response: {
|
|
749
|
-
body: null,
|
|
750
|
-
statusCode: error.statusCode || 0,
|
|
751
|
-
error: {
|
|
752
|
-
...response,
|
|
753
|
-
error,
|
|
754
|
-
body: response
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
};
|
|
694
|
+
// throw error and free up the middleware chain
|
|
695
|
+
throw error;
|
|
758
696
|
}
|
|
759
697
|
}
|
|
760
698
|
|
|
761
699
|
function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
762
|
-
const pendingTasks = [];
|
|
763
|
-
const requestState = new Mutex();
|
|
764
700
|
const tokenCache = options.tokenCache || store({
|
|
765
701
|
token: '',
|
|
766
702
|
expirationTime: -1
|
|
767
703
|
});
|
|
704
|
+
let tokenCacheObject;
|
|
705
|
+
let tokenFetchPromise = null;
|
|
768
706
|
const tokenCacheKey = buildTokenCacheKey(options);
|
|
769
707
|
return next => {
|
|
770
708
|
return async request => {
|
|
@@ -774,12 +712,19 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
|
774
712
|
return next(request);
|
|
775
713
|
}
|
|
776
714
|
|
|
715
|
+
/**
|
|
716
|
+
* If there is a token in the tokenCache, and it's not
|
|
717
|
+
* expired, append the token in the `Authorization` header.
|
|
718
|
+
*/
|
|
719
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
720
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
721
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
722
|
+
}
|
|
723
|
+
|
|
777
724
|
// prepare request options
|
|
778
725
|
const requestOptions = {
|
|
779
726
|
request,
|
|
780
|
-
requestState,
|
|
781
727
|
tokenCache,
|
|
782
|
-
pendingTasks,
|
|
783
728
|
tokenCacheKey,
|
|
784
729
|
httpClient: options.httpClient || fetch$1,
|
|
785
730
|
...buildRequestForAnonymousSessionFlow(options),
|
|
@@ -787,28 +732,32 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
|
|
|
787
732
|
next
|
|
788
733
|
};
|
|
789
734
|
|
|
790
|
-
//
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
735
|
+
// If a token is already being fetched, wait for it to finish
|
|
736
|
+
if (tokenFetchPromise) {
|
|
737
|
+
await tokenFetchPromise;
|
|
738
|
+
} else {
|
|
739
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
740
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
741
|
+
await tokenFetchPromise;
|
|
742
|
+
tokenFetchPromise = null;
|
|
797
743
|
}
|
|
798
|
-
|
|
799
|
-
|
|
744
|
+
|
|
745
|
+
// Now the token is present in the tokenCache
|
|
746
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
747
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
748
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
800
749
|
}
|
|
801
750
|
};
|
|
802
751
|
};
|
|
803
752
|
}
|
|
804
753
|
|
|
805
754
|
function createAuthMiddlewareForClientCredentialsFlow$1(options) {
|
|
806
|
-
const requestState = new Mutex();
|
|
807
|
-
const pendingTasks = [];
|
|
808
755
|
const tokenCache = options.tokenCache || store({
|
|
809
756
|
token: '',
|
|
810
757
|
expirationTime: -1
|
|
811
758
|
});
|
|
759
|
+
let tokenCacheObject;
|
|
760
|
+
let tokenFetchPromise = null;
|
|
812
761
|
const tokenCacheKey = buildTokenCacheKey(options);
|
|
813
762
|
return next => {
|
|
814
763
|
return async request => {
|
|
@@ -818,29 +767,40 @@ function createAuthMiddlewareForClientCredentialsFlow$1(options) {
|
|
|
818
767
|
return next(request);
|
|
819
768
|
}
|
|
820
769
|
|
|
770
|
+
/**
|
|
771
|
+
* If there is a token in the tokenCache, and it's not
|
|
772
|
+
* expired, append the token in the `Authorization` header.
|
|
773
|
+
*/
|
|
774
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
775
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
776
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
777
|
+
}
|
|
778
|
+
|
|
821
779
|
// prepare request options
|
|
822
780
|
const requestOptions = {
|
|
823
781
|
request,
|
|
824
|
-
requestState,
|
|
825
782
|
tokenCache,
|
|
826
|
-
pendingTasks,
|
|
827
783
|
tokenCacheKey,
|
|
784
|
+
tokenCacheObject,
|
|
828
785
|
httpClient: options.httpClient || fetch$1,
|
|
829
786
|
...buildRequestForClientCredentialsFlow(options),
|
|
830
787
|
next
|
|
831
788
|
};
|
|
832
789
|
|
|
833
|
-
//
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
790
|
+
// If a token is already being fetched, wait for it to finish
|
|
791
|
+
if (tokenFetchPromise) {
|
|
792
|
+
await tokenFetchPromise;
|
|
793
|
+
} else {
|
|
794
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
795
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
796
|
+
await tokenFetchPromise;
|
|
797
|
+
tokenFetchPromise = null;
|
|
840
798
|
}
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
799
|
+
|
|
800
|
+
// Now the token is present in the tokenCache
|
|
801
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
802
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
803
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
844
804
|
}
|
|
845
805
|
};
|
|
846
806
|
};
|
|
@@ -877,19 +837,27 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
|
|
|
877
837
|
token: '',
|
|
878
838
|
expirationTime: -1
|
|
879
839
|
});
|
|
880
|
-
|
|
881
|
-
|
|
840
|
+
let tokenCacheObject;
|
|
841
|
+
let tokenFetchPromise = null;
|
|
882
842
|
const tokenCacheKey = buildTokenCacheKey(options);
|
|
883
843
|
return next => {
|
|
884
844
|
return async request => {
|
|
845
|
+
// if here is a token in the header, then move on to the next middleware
|
|
885
846
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
886
847
|
return next(request);
|
|
887
848
|
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* If there is a token in the tokenCache, and it's not
|
|
852
|
+
* expired, append the token in the `Authorization` header.
|
|
853
|
+
*/
|
|
854
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
855
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
856
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
857
|
+
}
|
|
888
858
|
const requestOptions = {
|
|
889
859
|
request,
|
|
890
|
-
requestState,
|
|
891
860
|
tokenCache,
|
|
892
|
-
pendingTasks,
|
|
893
861
|
tokenCacheKey,
|
|
894
862
|
httpClient: options.httpClient || fetch$1,
|
|
895
863
|
...buildRequestForPasswordFlow(options),
|
|
@@ -897,16 +865,20 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
|
|
|
897
865
|
next
|
|
898
866
|
};
|
|
899
867
|
|
|
900
|
-
//
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
868
|
+
// If a token is already being fetched, wait for it to finish
|
|
869
|
+
if (tokenFetchPromise) {
|
|
870
|
+
await tokenFetchPromise;
|
|
871
|
+
} else {
|
|
872
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
873
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
874
|
+
await tokenFetchPromise;
|
|
875
|
+
tokenFetchPromise = null;
|
|
907
876
|
}
|
|
908
|
-
|
|
909
|
-
|
|
877
|
+
|
|
878
|
+
// Now the token is present in the tokenCache
|
|
879
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
880
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
881
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
910
882
|
}
|
|
911
883
|
};
|
|
912
884
|
};
|
|
@@ -917,35 +889,47 @@ function createAuthMiddlewareForRefreshTokenFlow$1(options) {
|
|
|
917
889
|
token: '',
|
|
918
890
|
tokenCacheKey: null
|
|
919
891
|
});
|
|
920
|
-
|
|
921
|
-
|
|
892
|
+
let tokenCacheObject;
|
|
893
|
+
let tokenFetchPromise = null;
|
|
894
|
+
const tokenCacheKey = buildTokenCacheKey(options);
|
|
922
895
|
return next => {
|
|
923
896
|
return async request => {
|
|
924
897
|
if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
|
|
925
898
|
return next(request);
|
|
926
899
|
}
|
|
927
900
|
|
|
901
|
+
/**
|
|
902
|
+
* If there is a token in the tokenCache, and it's not
|
|
903
|
+
* expired, append the token in the `Authorization` header.
|
|
904
|
+
*/
|
|
905
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
906
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
907
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
908
|
+
}
|
|
909
|
+
|
|
928
910
|
// prepare request options
|
|
929
911
|
const requestOptions = {
|
|
930
912
|
request,
|
|
931
|
-
requestState,
|
|
932
913
|
tokenCache,
|
|
933
|
-
pendingTasks,
|
|
934
914
|
httpClient: options.httpClient || fetch,
|
|
935
915
|
...buildRequestForRefreshTokenFlow(options),
|
|
936
916
|
next
|
|
937
917
|
};
|
|
938
918
|
|
|
939
|
-
//
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
919
|
+
// If a token is already being fetched, wait for it to finish
|
|
920
|
+
if (tokenFetchPromise) {
|
|
921
|
+
await tokenFetchPromise;
|
|
922
|
+
} else {
|
|
923
|
+
// Otherwise, fetch the token and let others wait for this process to complete
|
|
924
|
+
tokenFetchPromise = executeRequest$1(requestOptions);
|
|
925
|
+
await tokenFetchPromise;
|
|
926
|
+
tokenFetchPromise = null;
|
|
946
927
|
}
|
|
947
|
-
|
|
948
|
-
|
|
928
|
+
|
|
929
|
+
// Now the token is present in the tokenCache
|
|
930
|
+
tokenCacheObject = tokenCache.get(tokenCacheKey);
|
|
931
|
+
if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
|
|
932
|
+
return next(mergeAuthHeader(tokenCacheObject.token, request));
|
|
949
933
|
}
|
|
950
934
|
};
|
|
951
935
|
};
|
|
@@ -1108,7 +1092,7 @@ async function executeRequest({
|
|
|
1108
1092
|
uri: request.uri
|
|
1109
1093
|
})
|
|
1110
1094
|
});
|
|
1111
|
-
|
|
1095
|
+
throw {
|
|
1112
1096
|
body: error,
|
|
1113
1097
|
error
|
|
1114
1098
|
};
|
|
@@ -1155,7 +1139,7 @@ function createHttpMiddleware$1(options) {
|
|
|
1155
1139
|
// Ensure body is a string if content type is application/{json|graphql}
|
|
1156
1140
|
const body = HEADERS_CONTENT_TYPES.indexOf(requestHeader['Content-Type']) > -1 && typeof request.body === 'string' || isBuffer(request.body) ? request.body : JSON.stringify(request.body || undefined);
|
|
1157
1141
|
if (body && (typeof body === 'string' || isBuffer(body))) {
|
|
1158
|
-
requestHeader['Content-Length'] =
|
|
1142
|
+
requestHeader['Content-Length'] = byteLength(body);
|
|
1159
1143
|
}
|
|
1160
1144
|
const clientOptions = {
|
|
1161
1145
|
enableRetry,
|
|
@@ -1219,49 +1203,52 @@ function createQueueMiddleware$1({
|
|
|
1219
1203
|
concurrency = 20
|
|
1220
1204
|
}) {
|
|
1221
1205
|
let runningCount = 0;
|
|
1222
|
-
const
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
const
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
});
|
|
1236
|
-
return next => request => {
|
|
1237
|
-
// wrap and override resolve and reject functions
|
|
1238
|
-
const patchedRequest = {
|
|
1239
|
-
...request,
|
|
1240
|
-
resolve(data) {
|
|
1241
|
-
request.resolve(data);
|
|
1242
|
-
dequeue(next);
|
|
1243
|
-
},
|
|
1244
|
-
reject(error) {
|
|
1245
|
-
request.reject(error);
|
|
1246
|
-
dequeue(next);
|
|
1247
|
-
}
|
|
1248
|
-
};
|
|
1206
|
+
const waitForSlot = () => {
|
|
1207
|
+
// dont allow negative or 0 value for concurrency
|
|
1208
|
+
if (0 >= concurrency) return Promise.resolve();
|
|
1209
|
+
return new Promise(resolve => {
|
|
1210
|
+
const tryNext = () => {
|
|
1211
|
+
if (runningCount < concurrency) {
|
|
1212
|
+
runningCount++;
|
|
1213
|
+
resolve();
|
|
1214
|
+
} else {
|
|
1215
|
+
// Retry until a slot becomes available
|
|
1216
|
+
setTimeout(tryNext, 10);
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1249
1219
|
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
request: patchedRequest
|
|
1220
|
+
// start
|
|
1221
|
+
tryNext();
|
|
1253
1222
|
});
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
return
|
|
1258
|
-
|
|
1223
|
+
};
|
|
1224
|
+
return next => {
|
|
1225
|
+
return async request => {
|
|
1226
|
+
return waitForSlot().then(async () => {
|
|
1227
|
+
// Patch the request to override resolve and reject
|
|
1228
|
+
const patchedRequest = {
|
|
1229
|
+
...request,
|
|
1230
|
+
resolve(data) {
|
|
1231
|
+
request.resolve(data);
|
|
1232
|
+
runningCount--;
|
|
1233
|
+
},
|
|
1234
|
+
reject(error) {
|
|
1235
|
+
request.reject(error);
|
|
1236
|
+
runningCount--;
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
try {
|
|
1240
|
+
return next(patchedRequest);
|
|
1241
|
+
} finally {
|
|
1242
|
+
runningCount--;
|
|
1243
|
+
}
|
|
1244
|
+
});
|
|
1245
|
+
};
|
|
1259
1246
|
};
|
|
1260
1247
|
}
|
|
1261
1248
|
|
|
1262
1249
|
var packageJson = {
|
|
1263
1250
|
name: "@commercetools/ts-client",
|
|
1264
|
-
version: "2.0.
|
|
1251
|
+
version: "2.1.0-alpha.1",
|
|
1265
1252
|
engines: {
|
|
1266
1253
|
node: ">=14"
|
|
1267
1254
|
},
|
|
@@ -1295,14 +1282,14 @@ var packageJson = {
|
|
|
1295
1282
|
},
|
|
1296
1283
|
dependencies: {
|
|
1297
1284
|
"abort-controller": "3.0.0",
|
|
1298
|
-
"
|
|
1299
|
-
buffer: "^6.0.3",
|
|
1300
|
-
"node-fetch": "^2.6.1",
|
|
1301
|
-
uuid: "10.0.0"
|
|
1285
|
+
"node-fetch": "^2.6.1"
|
|
1302
1286
|
},
|
|
1303
1287
|
files: [
|
|
1304
1288
|
"dist",
|
|
1305
|
-
"CHANGELOG.md"
|
|
1289
|
+
"CHANGELOG.md",
|
|
1290
|
+
"LICENSE",
|
|
1291
|
+
"README.md",
|
|
1292
|
+
"package.json"
|
|
1306
1293
|
],
|
|
1307
1294
|
author: "Chukwuemeka Ajima <meeky.ae@gmail.com>",
|
|
1308
1295
|
main: "dist/commercetools-ts-client.cjs.js",
|