@commercetools/ts-client 3.1.0 → 3.2.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @commercetools/ts-client
2
2
 
3
+ ## 3.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#950](https://github.com/commercetools/commercetools-sdk-typescript/pull/950) [`e5d107d`](https://github.com/commercetools/commercetools-sdk-typescript/commit/e5d107df12aed11cf9486e9da976330b5664fe25) Thanks [@ajimae](https://github.com/ajimae)! - Add retry for 401 errors
8
+
9
+ ## 3.1.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#961](https://github.com/commercetools/commercetools-sdk-typescript/pull/961) [`28f0578`](https://github.com/commercetools/commercetools-sdk-typescript/commit/28f057841fcfd26b30ff41167dc88ada3c143371) Thanks [@ajimae](https://github.com/ajimae)! - Release changes for type modification
14
+
3
15
  ## 3.1.0
4
16
 
5
17
  ### Minor Changes
@@ -720,6 +720,63 @@ async function executeRequest$1(options) {
720
720
  }
721
721
  }
722
722
 
723
+ async function authProcessor(request, tokenFetchPromise = null, tokenCacheObject, tokenCacheKey, tokenCache, builder, options, next) {
724
+ // prepare request options
725
+ const requestOptions = {
726
+ request,
727
+ tokenCache,
728
+ httpClient: options.httpClient || fetch,
729
+ httpClientOptions: options.httpClientOptions,
730
+ ...builder(options),
731
+ userOption: options,
732
+ next
733
+ };
734
+
735
+ /**
736
+ * check to see if the response for 401 errors
737
+ * @param r MiddlewareResponse
738
+ * @returns response
739
+ */
740
+ const checkAndRetryUnauthorizedError = async r => {
741
+ let _response = Object.assign({}, r);
742
+ if (_response.statusCode == 401) {
743
+ tokenFetchPromise = executeRequest$1(requestOptions);
744
+ await tokenFetchPromise;
745
+ tokenFetchPromise = null;
746
+ tokenCacheObject = tokenCache.get(tokenCacheKey);
747
+ _response = await next(mergeAuthHeader(tokenCacheObject.token, request));
748
+ }
749
+ return _response;
750
+ };
751
+ if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
752
+ // move on
753
+ return checkAndRetryUnauthorizedError(await next(request));
754
+ }
755
+
756
+ /**
757
+ * If there is a token in the tokenCache, and it's not
758
+ * expired, append the token in the `Authorization` header.
759
+ */
760
+ tokenCacheObject = tokenCache.get(tokenCacheKey);
761
+ if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
762
+ return checkAndRetryUnauthorizedError(await next(mergeAuthHeader(tokenCacheObject.token, request)));
763
+ }
764
+
765
+ // If a token is already being fetched, wait for it to finish
766
+ if (tokenFetchPromise) {
767
+ await tokenFetchPromise;
768
+ } else {
769
+ // Otherwise, fetch the token and let others wait for this process to complete
770
+ tokenFetchPromise = executeRequest$1(requestOptions);
771
+ await tokenFetchPromise;
772
+ tokenFetchPromise = null;
773
+ }
774
+
775
+ // Now the token is present in the tokenCache and can be accessed
776
+ tokenCacheObject = tokenCache.get(tokenCacheKey);
777
+ return next(mergeAuthHeader(tokenCacheObject.token, request));
778
+ }
779
+
723
780
  function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
724
781
  const tokenCache = options.tokenCache || store({
725
782
  token: '',
@@ -730,46 +787,7 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
730
787
  const tokenCacheKey = buildTokenCacheKey(options);
731
788
  return next => {
732
789
  return async request => {
733
- // if here is a token in the header, then move on to the next middleware
734
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
735
- // move on
736
- return next(request);
737
- }
738
-
739
- /**
740
- * If there is a token in the tokenCache, and it's not
741
- * expired, append the token in the `Authorization` header.
742
- */
743
- tokenCacheObject = tokenCache.get(tokenCacheKey);
744
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
745
- return next(mergeAuthHeader(tokenCacheObject.token, request));
746
- }
747
-
748
- // prepare request options
749
- const requestOptions = {
750
- request,
751
- tokenCache,
752
- tokenCacheKey,
753
- httpClient: options.httpClient || fetch,
754
- httpClientOptions: options.httpClientOptions,
755
- ...buildRequestForAnonymousSessionFlow(options),
756
- userOption: options,
757
- next
758
- };
759
-
760
- // If a token is already being fetched, wait for it to finish
761
- if (tokenFetchPromise) {
762
- await tokenFetchPromise;
763
- } else {
764
- // Otherwise, fetch the token and let others wait for this process to complete
765
- tokenFetchPromise = executeRequest$1(requestOptions);
766
- await tokenFetchPromise;
767
- tokenFetchPromise = null;
768
- }
769
-
770
- // Now the token is present in the tokenCache and can be accessed
771
- tokenCacheObject = tokenCache.get(tokenCacheKey);
772
- return next(mergeAuthHeader(tokenCacheObject.token, request));
790
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForAnonymousSessionFlow, options, next);
773
791
  };
774
792
  };
775
793
  }
@@ -784,46 +802,7 @@ function createAuthMiddlewareForClientCredentialsFlow$1(options) {
784
802
  const tokenCacheKey = buildTokenCacheKey(options);
785
803
  return next => {
786
804
  return async request => {
787
- // if here is a token in the header, then move on to the next middleware
788
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
789
- // move on
790
- return next(request);
791
- }
792
-
793
- /**
794
- * If there is a token in the tokenCache, and it's not
795
- * expired, append the token in the `Authorization` header.
796
- */
797
- tokenCacheObject = tokenCache.get(tokenCacheKey);
798
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
799
- return next(mergeAuthHeader(tokenCacheObject.token, request));
800
- }
801
-
802
- // prepare request options
803
- const requestOptions = {
804
- request,
805
- tokenCache,
806
- tokenCacheKey,
807
- tokenCacheObject,
808
- httpClient: options.httpClient || fetch,
809
- httpClientOptions: options.httpClientOptions,
810
- ...buildRequestForClientCredentialsFlow(options),
811
- next
812
- };
813
-
814
- // If a token is already being fetched, wait for it to finish
815
- if (tokenFetchPromise) {
816
- await tokenFetchPromise;
817
- } else {
818
- // Otherwise, fetch the token and let others wait for this process to complete
819
- tokenFetchPromise = executeRequest$1(requestOptions);
820
- await tokenFetchPromise;
821
- tokenFetchPromise = null;
822
- }
823
-
824
- // Now the token is present in the tokenCache
825
- tokenCacheObject = tokenCache.get(tokenCacheKey);
826
- return next(mergeAuthHeader(tokenCacheObject.token, request));
805
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForClientCredentialsFlow, options, next);
827
806
  };
828
807
  };
829
808
  }
@@ -864,43 +843,7 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
864
843
  const tokenCacheKey = buildTokenCacheKey(options);
865
844
  return next => {
866
845
  return async request => {
867
- // if here is a token in the header, then move on to the next middleware
868
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
869
- return next(request);
870
- }
871
-
872
- /**
873
- * If there is a token in the tokenCache, and it's not
874
- * expired, append the token in the `Authorization` header.
875
- */
876
- tokenCacheObject = tokenCache.get(tokenCacheKey);
877
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
878
- return next(mergeAuthHeader(tokenCacheObject.token, request));
879
- }
880
- const requestOptions = {
881
- request,
882
- tokenCache,
883
- tokenCacheKey,
884
- httpClient: options.httpClient || fetch,
885
- httpClientOptions: options.httpClientOptions,
886
- ...buildRequestForPasswordFlow(options),
887
- userOption: options,
888
- next
889
- };
890
-
891
- // If a token is already being fetched, wait for it to finish
892
- if (tokenFetchPromise) {
893
- await tokenFetchPromise;
894
- } else {
895
- // Otherwise, fetch the token and let others wait for this process to complete
896
- tokenFetchPromise = executeRequest$1(requestOptions);
897
- await tokenFetchPromise;
898
- tokenFetchPromise = null;
899
- }
900
-
901
- // Now the token is present in the tokenCache
902
- tokenCacheObject = tokenCache.get(tokenCacheKey);
903
- return next(mergeAuthHeader(tokenCacheObject.token, request));
846
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForPasswordFlow, options, next);
904
847
  };
905
848
  };
906
849
  }
@@ -915,42 +858,7 @@ function createAuthMiddlewareForRefreshTokenFlow$1(options) {
915
858
  const tokenCacheKey = buildTokenCacheKey(options);
916
859
  return next => {
917
860
  return async request => {
918
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
919
- return next(request);
920
- }
921
-
922
- /**
923
- * If there is a token in the tokenCache, and it's not
924
- * expired, append the token in the `Authorization` header.
925
- */
926
- tokenCacheObject = tokenCache.get(tokenCacheKey);
927
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
928
- return next(mergeAuthHeader(tokenCacheObject.token, request));
929
- }
930
-
931
- // prepare request options
932
- const requestOptions = {
933
- request,
934
- tokenCache,
935
- httpClient: options.httpClient || fetch,
936
- httpClientOptions: options.httpClientOptions,
937
- ...buildRequestForRefreshTokenFlow(options),
938
- next
939
- };
940
-
941
- // If a token is already being fetched, wait for it to finish
942
- if (tokenFetchPromise) {
943
- await tokenFetchPromise;
944
- } else {
945
- // Otherwise, fetch the token and let others wait for this process to complete
946
- tokenFetchPromise = executeRequest$1(requestOptions);
947
- await tokenFetchPromise;
948
- tokenFetchPromise = null;
949
- }
950
-
951
- // Now the token is present in the tokenCache
952
- tokenCacheObject = tokenCache.get(tokenCacheKey);
953
- return next(mergeAuthHeader(tokenCacheObject.token, request));
861
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForRefreshTokenFlow, options, next);
954
862
  };
955
863
  };
956
864
  }
@@ -1261,7 +1169,7 @@ function createQueueMiddleware$1({
1261
1169
 
1262
1170
  var packageJson = {
1263
1171
  name: "@commercetools/ts-client",
1264
- version: "3.1.0",
1172
+ version: "3.2.0",
1265
1173
  engines: {
1266
1174
  node: ">=18"
1267
1175
  },
@@ -716,6 +716,63 @@ async function executeRequest$1(options) {
716
716
  }
717
717
  }
718
718
 
719
+ async function authProcessor(request, tokenFetchPromise = null, tokenCacheObject, tokenCacheKey, tokenCache, builder, options, next) {
720
+ // prepare request options
721
+ const requestOptions = {
722
+ request,
723
+ tokenCache,
724
+ httpClient: options.httpClient || fetch,
725
+ httpClientOptions: options.httpClientOptions,
726
+ ...builder(options),
727
+ userOption: options,
728
+ next
729
+ };
730
+
731
+ /**
732
+ * check to see if the response for 401 errors
733
+ * @param r MiddlewareResponse
734
+ * @returns response
735
+ */
736
+ const checkAndRetryUnauthorizedError = async r => {
737
+ let _response = Object.assign({}, r);
738
+ if (_response.statusCode == 401) {
739
+ tokenFetchPromise = executeRequest$1(requestOptions);
740
+ await tokenFetchPromise;
741
+ tokenFetchPromise = null;
742
+ tokenCacheObject = tokenCache.get(tokenCacheKey);
743
+ _response = await next(mergeAuthHeader(tokenCacheObject.token, request));
744
+ }
745
+ return _response;
746
+ };
747
+ if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
748
+ // move on
749
+ return checkAndRetryUnauthorizedError(await next(request));
750
+ }
751
+
752
+ /**
753
+ * If there is a token in the tokenCache, and it's not
754
+ * expired, append the token in the `Authorization` header.
755
+ */
756
+ tokenCacheObject = tokenCache.get(tokenCacheKey);
757
+ if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
758
+ return checkAndRetryUnauthorizedError(await next(mergeAuthHeader(tokenCacheObject.token, request)));
759
+ }
760
+
761
+ // If a token is already being fetched, wait for it to finish
762
+ if (tokenFetchPromise) {
763
+ await tokenFetchPromise;
764
+ } else {
765
+ // Otherwise, fetch the token and let others wait for this process to complete
766
+ tokenFetchPromise = executeRequest$1(requestOptions);
767
+ await tokenFetchPromise;
768
+ tokenFetchPromise = null;
769
+ }
770
+
771
+ // Now the token is present in the tokenCache and can be accessed
772
+ tokenCacheObject = tokenCache.get(tokenCacheKey);
773
+ return next(mergeAuthHeader(tokenCacheObject.token, request));
774
+ }
775
+
719
776
  function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
720
777
  const tokenCache = options.tokenCache || store({
721
778
  token: '',
@@ -726,46 +783,7 @@ function createAuthMiddlewareForAnonymousSessionFlow$1(options) {
726
783
  const tokenCacheKey = buildTokenCacheKey(options);
727
784
  return next => {
728
785
  return async request => {
729
- // if here is a token in the header, then move on to the next middleware
730
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
731
- // move on
732
- return next(request);
733
- }
734
-
735
- /**
736
- * If there is a token in the tokenCache, and it's not
737
- * expired, append the token in the `Authorization` header.
738
- */
739
- tokenCacheObject = tokenCache.get(tokenCacheKey);
740
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
741
- return next(mergeAuthHeader(tokenCacheObject.token, request));
742
- }
743
-
744
- // prepare request options
745
- const requestOptions = {
746
- request,
747
- tokenCache,
748
- tokenCacheKey,
749
- httpClient: options.httpClient || fetch,
750
- httpClientOptions: options.httpClientOptions,
751
- ...buildRequestForAnonymousSessionFlow(options),
752
- userOption: options,
753
- next
754
- };
755
-
756
- // If a token is already being fetched, wait for it to finish
757
- if (tokenFetchPromise) {
758
- await tokenFetchPromise;
759
- } else {
760
- // Otherwise, fetch the token and let others wait for this process to complete
761
- tokenFetchPromise = executeRequest$1(requestOptions);
762
- await tokenFetchPromise;
763
- tokenFetchPromise = null;
764
- }
765
-
766
- // Now the token is present in the tokenCache and can be accessed
767
- tokenCacheObject = tokenCache.get(tokenCacheKey);
768
- return next(mergeAuthHeader(tokenCacheObject.token, request));
786
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForAnonymousSessionFlow, options, next);
769
787
  };
770
788
  };
771
789
  }
@@ -780,46 +798,7 @@ function createAuthMiddlewareForClientCredentialsFlow$1(options) {
780
798
  const tokenCacheKey = buildTokenCacheKey(options);
781
799
  return next => {
782
800
  return async request => {
783
- // if here is a token in the header, then move on to the next middleware
784
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
785
- // move on
786
- return next(request);
787
- }
788
-
789
- /**
790
- * If there is a token in the tokenCache, and it's not
791
- * expired, append the token in the `Authorization` header.
792
- */
793
- tokenCacheObject = tokenCache.get(tokenCacheKey);
794
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
795
- return next(mergeAuthHeader(tokenCacheObject.token, request));
796
- }
797
-
798
- // prepare request options
799
- const requestOptions = {
800
- request,
801
- tokenCache,
802
- tokenCacheKey,
803
- tokenCacheObject,
804
- httpClient: options.httpClient || fetch,
805
- httpClientOptions: options.httpClientOptions,
806
- ...buildRequestForClientCredentialsFlow(options),
807
- next
808
- };
809
-
810
- // If a token is already being fetched, wait for it to finish
811
- if (tokenFetchPromise) {
812
- await tokenFetchPromise;
813
- } else {
814
- // Otherwise, fetch the token and let others wait for this process to complete
815
- tokenFetchPromise = executeRequest$1(requestOptions);
816
- await tokenFetchPromise;
817
- tokenFetchPromise = null;
818
- }
819
-
820
- // Now the token is present in the tokenCache
821
- tokenCacheObject = tokenCache.get(tokenCacheKey);
822
- return next(mergeAuthHeader(tokenCacheObject.token, request));
801
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForClientCredentialsFlow, options, next);
823
802
  };
824
803
  };
825
804
  }
@@ -860,43 +839,7 @@ function createAuthMiddlewareForPasswordFlow$1(options) {
860
839
  const tokenCacheKey = buildTokenCacheKey(options);
861
840
  return next => {
862
841
  return async request => {
863
- // if here is a token in the header, then move on to the next middleware
864
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
865
- return next(request);
866
- }
867
-
868
- /**
869
- * If there is a token in the tokenCache, and it's not
870
- * expired, append the token in the `Authorization` header.
871
- */
872
- tokenCacheObject = tokenCache.get(tokenCacheKey);
873
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
874
- return next(mergeAuthHeader(tokenCacheObject.token, request));
875
- }
876
- const requestOptions = {
877
- request,
878
- tokenCache,
879
- tokenCacheKey,
880
- httpClient: options.httpClient || fetch,
881
- httpClientOptions: options.httpClientOptions,
882
- ...buildRequestForPasswordFlow(options),
883
- userOption: options,
884
- next
885
- };
886
-
887
- // If a token is already being fetched, wait for it to finish
888
- if (tokenFetchPromise) {
889
- await tokenFetchPromise;
890
- } else {
891
- // Otherwise, fetch the token and let others wait for this process to complete
892
- tokenFetchPromise = executeRequest$1(requestOptions);
893
- await tokenFetchPromise;
894
- tokenFetchPromise = null;
895
- }
896
-
897
- // Now the token is present in the tokenCache
898
- tokenCacheObject = tokenCache.get(tokenCacheKey);
899
- return next(mergeAuthHeader(tokenCacheObject.token, request));
842
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForPasswordFlow, options, next);
900
843
  };
901
844
  };
902
845
  }
@@ -911,42 +854,7 @@ function createAuthMiddlewareForRefreshTokenFlow$1(options) {
911
854
  const tokenCacheKey = buildTokenCacheKey(options);
912
855
  return next => {
913
856
  return async request => {
914
- if (request.headers && (request.headers.Authorization || request.headers.authorization)) {
915
- return next(request);
916
- }
917
-
918
- /**
919
- * If there is a token in the tokenCache, and it's not
920
- * expired, append the token in the `Authorization` header.
921
- */
922
- tokenCacheObject = tokenCache.get(tokenCacheKey);
923
- if (tokenCacheObject && tokenCacheObject.token && Date.now() < tokenCacheObject.expirationTime) {
924
- return next(mergeAuthHeader(tokenCacheObject.token, request));
925
- }
926
-
927
- // prepare request options
928
- const requestOptions = {
929
- request,
930
- tokenCache,
931
- httpClient: options.httpClient || fetch,
932
- httpClientOptions: options.httpClientOptions,
933
- ...buildRequestForRefreshTokenFlow(options),
934
- next
935
- };
936
-
937
- // If a token is already being fetched, wait for it to finish
938
- if (tokenFetchPromise) {
939
- await tokenFetchPromise;
940
- } else {
941
- // Otherwise, fetch the token and let others wait for this process to complete
942
- tokenFetchPromise = executeRequest$1(requestOptions);
943
- await tokenFetchPromise;
944
- tokenFetchPromise = null;
945
- }
946
-
947
- // Now the token is present in the tokenCache
948
- tokenCacheObject = tokenCache.get(tokenCacheKey);
949
- return next(mergeAuthHeader(tokenCacheObject.token, request));
857
+ return authProcessor(request, tokenFetchPromise, tokenCacheObject, tokenCacheKey, tokenCache, buildRequestForRefreshTokenFlow, options, next);
950
858
  };
951
859
  };
952
860
  }
@@ -1257,7 +1165,7 @@ function createQueueMiddleware$1({
1257
1165
 
1258
1166
  var packageJson = {
1259
1167
  name: "@commercetools/ts-client",
1260
- version: "3.1.0",
1168
+ version: "3.2.0",
1261
1169
  engines: {
1262
1170
  node: ">=18"
1263
1171
  },