@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.
@@ -1,5 +1,5 @@
1
1
  import * as api from './api';
2
- import ApiError from './error';
2
+ import { ApiError } from './error';
3
3
  import type { ErrorInterceptor, InterceptorType, RawResponseReader, RequestInterceptor, ResponseInterceptor } from './interceptor';
4
4
  import { ResourcesConfig, ResourcesInitConfig } from './resource';
5
5
  declare type SdkConfig = {};
package/lib/error.d.ts CHANGED
@@ -2,19 +2,25 @@ declare enum ErrorType {
2
2
  CLIENT = "client",
3
3
  REQUEST = "request",
4
4
  RESPONSE = "response",
5
- GENERIC = "generic"
5
+ GENERIC = "generic",
6
+ CANCEL = "cancel"
6
7
  }
7
- declare class ApiError extends Error {
8
- static isApiError(error: any): error is ApiError;
8
+ declare class SdkError extends Error {
9
+ static isSdkError(error: any): error is ApiError;
9
10
  type: string;
10
- errors: any[];
11
- status?: number;
12
11
  code?: string;
12
+ source?: Error;
13
+ request?: any;
13
14
  constructor(error: {
14
15
  message: string;
15
16
  type?: ErrorType;
16
17
  });
18
+ }
19
+ declare class ApiError extends SdkError {
20
+ static isApiError(error: any): error is ApiError;
21
+ errors: any[];
22
+ status?: number;
23
+ constructor(error: SdkError);
17
24
  first(): any;
18
25
  }
19
- export default ApiError;
20
- export { ErrorType };
26
+ export { SdkError, ApiError, ErrorType };
@@ -949,24 +949,46 @@ var ErrorType;
949
949
  ErrorType["REQUEST"] = "request";
950
950
  ErrorType["RESPONSE"] = "response";
951
951
  ErrorType["GENERIC"] = "generic";
952
+ ErrorType["CANCEL"] = "cancel";
952
953
  })(ErrorType || (ErrorType = {}));
953
954
 
954
- var ApiError = /*#__PURE__*/function (_Error) {
955
- _inheritsLoose(ApiError, _Error);
955
+ var SdkError = /*#__PURE__*/function (_Error) {
956
+ _inheritsLoose(SdkError, _Error);
956
957
 
957
- function ApiError(error) {
958
+ function SdkError(error) {
958
959
  var _this;
959
960
 
960
961
  _this = _Error.call(this, error.message) || this;
961
- _this.errors = [];
962
962
  _this.name = _this.constructor.name;
963
963
  _this.type = error.type || ErrorType.GENERIC;
964
964
  return _this;
965
965
  } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
966
966
 
967
967
 
968
+ SdkError.isSdkError = function isSdkError(error) {
969
+ return error && ['SdkError', 'ApiError'].includes(error.name) && Object.values(ErrorType).includes(error.type);
970
+ };
971
+
972
+ return SdkError;
973
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
974
+
975
+ var ApiError = /*#__PURE__*/function (_SdkError) {
976
+ _inheritsLoose(ApiError, _SdkError);
977
+
978
+ function ApiError(error) {
979
+ var _this2;
980
+
981
+ _this2 = _SdkError.call(this, _extends({}, error, {
982
+ type: ErrorType.RESPONSE
983
+ })) || this;
984
+ _this2.errors = [];
985
+ _this2.name = _this2.constructor.name;
986
+ return _this2;
987
+ } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
988
+
989
+
968
990
  ApiError.isApiError = function isApiError(error) {
969
- return error && error.name === 'ApiError' && error.type === ErrorType.RESPONSE;
991
+ return SdkError.isSdkError(error) && error.name === 'ApiError' && error.type === ErrorType.RESPONSE;
970
992
  };
971
993
 
972
994
  var _proto = ApiError.prototype;
@@ -976,14 +998,14 @@ var ApiError = /*#__PURE__*/function (_Error) {
976
998
  };
977
999
 
978
1000
  return ApiError;
979
- }( /*#__PURE__*/_wrapNativeSuper(Error));
1001
+ }(SdkError);
980
1002
 
981
1003
  var baseURL = function baseURL(organization, domain) {
982
1004
  return "https://" + organization.toLowerCase() + "." + (domain ? domain : 'commercelayer.io') + "/api";
983
1005
  };
984
1006
 
985
1007
  var handleError = function handleError(error) {
986
- var apiError = new ApiError({
1008
+ var sdkError = new SdkError({
987
1009
  message: error.message,
988
1010
  type: ErrorType.GENERIC
989
1011
  });
@@ -991,21 +1013,24 @@ var handleError = function handleError(error) {
991
1013
  if (axios.isAxiosError(error)) {
992
1014
  if (error.response) {
993
1015
  // The request was made and the server responded with a status code that falls out of the range of 2xx
1016
+ var apiError = new ApiError(sdkError);
994
1017
  apiError.type = ErrorType.RESPONSE;
995
1018
  apiError.status = error.response.status;
996
1019
  apiError.code = String(apiError.status);
997
1020
  apiError.errors = error.response.data.errors;
1021
+ sdkError = apiError;
998
1022
  } else if (error.request) {
999
1023
  // The request was made but no response was received
1000
1024
  // `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
1001
- apiError.type = ErrorType.REQUEST;
1025
+ sdkError.type = ErrorType.REQUEST;
1026
+ sdkError.request = error.request;
1002
1027
  } else {
1003
1028
  // Something happened in setting up the request that triggered an Error
1004
- apiError.type = ErrorType.CLIENT;
1029
+ sdkError.type = ErrorType.CLIENT;
1005
1030
  }
1006
- }
1031
+ } else if (axios.isCancel(error)) sdkError.type = ErrorType.CANCEL;else sdkError.source = error;
1007
1032
 
1008
- throw apiError;
1033
+ throw sdkError;
1009
1034
  };
1010
1035
 
1011
1036
  var _accessToken = /*#__PURE__*/_classPrivateFieldLooseKey("accessToken");
@@ -16932,6 +16957,9 @@ var CommerceLayerStatic = {
16932
16957
  resources: function resources() {
16933
16958
  return resourceList;
16934
16959
  },
16960
+ isSdkError: function isSdkError(error) {
16961
+ return SdkError.isSdkError(error);
16962
+ },
16935
16963
  isApiError: function isApiError(error) {
16936
16964
  return ApiError.isApiError(error);
16937
16965
  }