@commercelayer/sdk 2.3.0 → 2.3.3-beta.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/lib/sdk.esm.js CHANGED
@@ -943,24 +943,46 @@ var ErrorType;
943
943
  ErrorType["REQUEST"] = "request";
944
944
  ErrorType["RESPONSE"] = "response";
945
945
  ErrorType["GENERIC"] = "generic";
946
+ ErrorType["CANCEL"] = "cancel";
946
947
  })(ErrorType || (ErrorType = {}));
947
948
 
948
- var ApiError = /*#__PURE__*/function (_Error) {
949
- _inheritsLoose(ApiError, _Error);
949
+ var SdkError = /*#__PURE__*/function (_Error) {
950
+ _inheritsLoose(SdkError, _Error);
950
951
 
951
- function ApiError(error) {
952
+ function SdkError(error) {
952
953
  var _this;
953
954
 
954
955
  _this = _Error.call(this, error.message) || this;
955
- _this.errors = [];
956
956
  _this.name = _this.constructor.name;
957
957
  _this.type = error.type || ErrorType.GENERIC;
958
958
  return _this;
959
959
  } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
960
960
 
961
961
 
962
+ SdkError.isSdkError = function isSdkError(error) {
963
+ return error && ['SdkError', 'ApiError'].includes(error.name) && Object.values(ErrorType).includes(error.type);
964
+ };
965
+
966
+ return SdkError;
967
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
968
+
969
+ var ApiError = /*#__PURE__*/function (_SdkError) {
970
+ _inheritsLoose(ApiError, _SdkError);
971
+
972
+ function ApiError(error) {
973
+ var _this2;
974
+
975
+ _this2 = _SdkError.call(this, _extends({}, error, {
976
+ type: ErrorType.RESPONSE
977
+ })) || this;
978
+ _this2.errors = [];
979
+ _this2.name = _this2.constructor.name;
980
+ return _this2;
981
+ } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
982
+
983
+
962
984
  ApiError.isApiError = function isApiError(error) {
963
- return error && error.name === 'ApiError' && error.type === ErrorType.RESPONSE;
985
+ return SdkError.isSdkError(error) && error.name === 'ApiError' && error.type === ErrorType.RESPONSE;
964
986
  };
965
987
 
966
988
  var _proto = ApiError.prototype;
@@ -970,14 +992,14 @@ var ApiError = /*#__PURE__*/function (_Error) {
970
992
  };
971
993
 
972
994
  return ApiError;
973
- }( /*#__PURE__*/_wrapNativeSuper(Error));
995
+ }(SdkError);
974
996
 
975
997
  var baseURL = function baseURL(organization, domain) {
976
998
  return "https://" + organization.toLowerCase() + "." + (domain ? domain : 'commercelayer.io') + "/api";
977
999
  };
978
1000
 
979
1001
  var handleError = function handleError(error) {
980
- var apiError = new ApiError({
1002
+ var sdkError = new SdkError({
981
1003
  message: error.message,
982
1004
  type: ErrorType.GENERIC
983
1005
  });
@@ -985,21 +1007,24 @@ var handleError = function handleError(error) {
985
1007
  if (axios.isAxiosError(error)) {
986
1008
  if (error.response) {
987
1009
  // The request was made and the server responded with a status code that falls out of the range of 2xx
1010
+ var apiError = new ApiError(sdkError);
988
1011
  apiError.type = ErrorType.RESPONSE;
989
1012
  apiError.status = error.response.status;
990
1013
  apiError.code = String(apiError.status);
991
1014
  apiError.errors = error.response.data.errors;
1015
+ sdkError = apiError;
992
1016
  } else if (error.request) {
993
1017
  // The request was made but no response was received
994
1018
  // `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
995
- apiError.type = ErrorType.REQUEST;
1019
+ sdkError.type = ErrorType.REQUEST;
1020
+ sdkError.request = error.request;
996
1021
  } else {
997
1022
  // Something happened in setting up the request that triggered an Error
998
- apiError.type = ErrorType.CLIENT;
1023
+ sdkError.type = ErrorType.CLIENT;
999
1024
  }
1000
- }
1025
+ } else if (axios.isCancel(error)) sdkError.type = ErrorType.CANCEL;else sdkError.source = error;
1001
1026
 
1002
- throw apiError;
1027
+ throw sdkError;
1003
1028
  };
1004
1029
 
1005
1030
  var _accessToken = /*#__PURE__*/_classPrivateFieldLooseKey("accessToken");
@@ -16926,6 +16951,9 @@ var CommerceLayerStatic = {
16926
16951
  resources: function resources() {
16927
16952
  return resourceList;
16928
16953
  },
16954
+ isSdkError: function isSdkError(error) {
16955
+ return SdkError.isSdkError(error);
16956
+ },
16929
16957
  isApiError: function isApiError(error) {
16930
16958
  return ApiError.isApiError(error);
16931
16959
  }