@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.
@@ -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
  },
@@ -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
  },