@shipengine/js-api 0.52.1 → 0.53.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/carriers/api.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AxiosInstance } from "axios";
2
2
  import { Currency, Money } from "../payments";
3
- import { Carrier, CarrierAutoFundingSettings, CarrierAutoFundingSettingsResponse, CarrierConnection, CarrierService, CarrierZone, PackageRatingType, WalletTransactionHistory } from "./types";
3
+ import { Carrier, CarrierAutoFundingSettings, CarrierAutoFundingSettingsResponse, CarrierConnection, CarrierService, CarrierZone, PackageRatingType } from "./types";
4
4
  /**
5
5
  * # Carriers API module - /v1/carriers
6
6
  */
@@ -46,11 +46,6 @@ export declare class CarriersAPI {
46
46
  * values for auto-funding. Auto-funding is disabled by default.
47
47
  */
48
48
  getAutoFunding: (carrierId: string) => Promise<import("axios").AxiosResponse<CarrierAutoFundingSettingsResponse, any>>;
49
- /**
50
- * The `getWalletHistory` method retrieves the wallet transaction history for
51
- * a ShipEngine wallet account.
52
- */
53
- getWalletHistory: (startDate: Date, endDate: Date, page: number) => Promise<import("axios").AxiosResponse<WalletTransactionHistory, any>>;
54
49
  /**
55
50
  * The `getServices` method retrieves a list of shipping services that a given carrier offers.
56
51
  */
package/client.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { AxiosRequestHeaders } from "axios";
1
+ import { AxiosError, AxiosRequestHeaders } from "axios";
2
2
  import { AccountBillingPlanAPI } from "./account-billing-plan";
3
3
  import { AccountSettingsAPI } from "./account-settings";
4
4
  import { AddressesAPI } from "./addresses";
5
5
  import { CarriersAPI } from "./carriers";
6
6
  import { CustomPackagesAPI } from "./custom-packages";
7
+ import { CodedError } from "./errors";
7
8
  import { FundingSourcesAPI } from "./funding-sources";
8
9
  import { InsuranceAPI } from "./insurance";
9
10
  import { LabelsAPI } from "./labels";
@@ -20,6 +21,12 @@ import { WarehousesAPI } from "./warehouses";
20
21
  * # ShipEngine API Client Headers
21
22
  */
22
23
  export type ShipEngineAPIHeaders = AxiosRequestHeaders;
24
+ /**
25
+ * # ShipEngine API axios error response
26
+ */
27
+ export type ApiError = AxiosError<{
28
+ errors: CodedError[];
29
+ } | CodedError[] | CodedError | string>;
23
30
  /**
24
31
  * # ShipEngine API Client Configuration
25
32
  */
@@ -36,6 +43,10 @@ export interface ShipEngineAPIConfig {
36
43
  * `headers` are optional HTTP headers to be sent with all requests.
37
44
  */
38
45
  headers?: ShipEngineAPIHeaders;
46
+ /**
47
+ * `onApiError` is an optional callback function that will be executed whenever there is an error.
48
+ */
49
+ onApiError?: (errors: CodedError[], error: ApiError) => void;
39
50
  }
40
51
  /**
41
52
  * # ShipEngine API Client
@@ -45,7 +56,7 @@ export interface ShipEngineAPIConfig {
45
56
  */
46
57
  export declare class ShipEngineAPI {
47
58
  private client;
48
- constructor(token: string, { baseURL, headers, getToken }: ShipEngineAPIConfig);
59
+ constructor(token: string, { baseURL, headers, getToken, onApiError }: ShipEngineAPIConfig);
49
60
  /**
50
61
  * The `token` method takes in a string and sets it as the Authorization header for all requests.
51
62
  */
package/errors/utils.d.ts CHANGED
@@ -1,3 +1,7 @@
1
1
  import { CodedError } from "./types";
2
2
  export declare const isCodedErrors: (errs: any) => errs is CodedError[];
3
3
  export declare const isCodedError: (err: any) => err is CodedError;
4
+ export declare const isDataCodedErrors: (data: any) => data is {
5
+ errors: CodedError[];
6
+ };
7
+ export declare const parseError: (err: any) => CodedError[];
package/index.js CHANGED
@@ -229,6 +229,21 @@ class AddressesAPI {
229
229
 
230
230
  const isCodedErrors = (errs) => Array.isArray(errs) && errs.every((err) => isCodedError(err));
231
231
  const isCodedError = (err) => !!err.errorCode;
232
+ const isDataCodedErrors = (data) => !!data.errors && isCodedErrors(data.errors);
233
+ const parseError = (err) => {
234
+ var _a;
235
+ if (!((_a = err.response) == null ? void 0 : _a.data))
236
+ return [new CodedError(err.message)];
237
+ if (isDataCodedErrors(err.response.data)) {
238
+ return err.response.data.errors.map((err2) => CodedError.fromObject(err2));
239
+ } else if (isCodedErrors(err.response.data)) {
240
+ return err.response.data.map((err2) => CodedError.fromObject(err2));
241
+ } else if (isCodedError(err.response.data)) {
242
+ return [CodedError.fromObject(err.response.data)];
243
+ } else {
244
+ return [new CodedError(err.message)];
245
+ }
246
+ };
232
247
 
233
248
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
234
249
 
@@ -1078,19 +1093,6 @@ class CarriersAPI {
1078
1093
  `/v1/carriers/${carrierId}/auto_funding`
1079
1094
  );
1080
1095
  };
1081
- /**
1082
- * The `getWalletHistory` method retrieves the wallet transaction history for
1083
- * a ShipEngine wallet account.
1084
- */
1085
- this.getWalletHistory = (startDate, endDate, page) => {
1086
- return this.client.get(`/v1/carriers/wallet_history`, {
1087
- params: {
1088
- endDate: endDate.toISOString(),
1089
- page,
1090
- startDate: startDate.toISOString()
1091
- }
1092
- });
1093
- };
1094
1096
  /**
1095
1097
  * The `getServices` method retrieves a list of shipping services that a given carrier offers.
1096
1098
  */
@@ -3877,6 +3879,7 @@ var __async = (__this, __arguments, generator) => {
3877
3879
  });
3878
3880
  };
3879
3881
  const logger = E({
3882
+ level: process.env.NODE_ENV === "production" ? "fatal" : "info",
3880
3883
  name: "shipengine-api",
3881
3884
  serializers: __spreadProps(__spreadValues({}, k), {
3882
3885
  req: (req) => ({
@@ -3898,9 +3901,8 @@ const logger = E({
3898
3901
  }
3899
3902
  ]
3900
3903
  });
3901
- const isDataCodedErrors = (data) => !!data.errors && isCodedErrors(data.errors);
3902
3904
  class ShipEngineAPI {
3903
- constructor(token, { baseURL, headers, getToken }) {
3905
+ constructor(token, { baseURL, headers, getToken, onApiError }) {
3904
3906
  const client = axios.create({
3905
3907
  baseURL,
3906
3908
  headers: __spreadProps(__spreadValues({}, headers), {
@@ -3951,7 +3953,7 @@ class ShipEngineAPI {
3951
3953
  return res;
3952
3954
  },
3953
3955
  (err) => __async(this, null, function* () {
3954
- var _a, _b, _c, _d, _e, _f, _g;
3956
+ var _a, _b, _c, _d, _e, _f;
3955
3957
  logger.error(
3956
3958
  { err, req: err.config, res: err.response },
3957
3959
  "%s %s: %s %s - %s",
@@ -3970,22 +3972,9 @@ class ShipEngineAPI {
3970
3972
  }
3971
3973
  return axios(config);
3972
3974
  }
3973
- if ((_g = err.response) == null ? void 0 : _g.data) {
3974
- if (isDataCodedErrors(err.response.data)) {
3975
- return Promise.reject(
3976
- err.response.data.errors.map((err2) => CodedError.fromObject(err2))
3977
- );
3978
- }
3979
- if (isCodedErrors(err.response.data)) {
3980
- return Promise.reject(err.response.data.map((err2) => CodedError.fromObject(err2)));
3981
- }
3982
- if (isCodedError(err.response.data)) {
3983
- return Promise.reject([CodedError.fromObject(err.response.data)]);
3984
- }
3985
- return Promise.reject([new CodedError(err.response.data)]);
3986
- } else {
3987
- return Promise.reject([new CodedError(err.message)]);
3988
- }
3975
+ const codedErrors = parseError(err);
3976
+ onApiError == null ? void 0 : onApiError(codedErrors, err);
3977
+ return Promise.reject(codedErrors);
3989
3978
  })
3990
3979
  );
3991
3980
  this.client = client;
@@ -4180,3 +4169,5 @@ exports.WarehousesAPI = WarehousesAPI;
4180
4169
  exports.getEndUserIpAddress = getEndUserIpAddress;
4181
4170
  exports.isCodedError = isCodedError;
4182
4171
  exports.isCodedErrors = isCodedErrors;
4172
+ exports.isDataCodedErrors = isDataCodedErrors;
4173
+ exports.parseError = parseError;
package/index.mjs CHANGED
@@ -225,6 +225,21 @@ class AddressesAPI {
225
225
 
226
226
  const isCodedErrors = (errs) => Array.isArray(errs) && errs.every((err) => isCodedError(err));
227
227
  const isCodedError = (err) => !!err.errorCode;
228
+ const isDataCodedErrors = (data) => !!data.errors && isCodedErrors(data.errors);
229
+ const parseError = (err) => {
230
+ var _a;
231
+ if (!((_a = err.response) == null ? void 0 : _a.data))
232
+ return [new CodedError(err.message)];
233
+ if (isDataCodedErrors(err.response.data)) {
234
+ return err.response.data.errors.map((err2) => CodedError.fromObject(err2));
235
+ } else if (isCodedErrors(err.response.data)) {
236
+ return err.response.data.map((err2) => CodedError.fromObject(err2));
237
+ } else if (isCodedError(err.response.data)) {
238
+ return [CodedError.fromObject(err.response.data)];
239
+ } else {
240
+ return [new CodedError(err.message)];
241
+ }
242
+ };
228
243
 
229
244
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
230
245
 
@@ -1074,19 +1089,6 @@ class CarriersAPI {
1074
1089
  `/v1/carriers/${carrierId}/auto_funding`
1075
1090
  );
1076
1091
  };
1077
- /**
1078
- * The `getWalletHistory` method retrieves the wallet transaction history for
1079
- * a ShipEngine wallet account.
1080
- */
1081
- this.getWalletHistory = (startDate, endDate, page) => {
1082
- return this.client.get(`/v1/carriers/wallet_history`, {
1083
- params: {
1084
- endDate: endDate.toISOString(),
1085
- page,
1086
- startDate: startDate.toISOString()
1087
- }
1088
- });
1089
- };
1090
1092
  /**
1091
1093
  * The `getServices` method retrieves a list of shipping services that a given carrier offers.
1092
1094
  */
@@ -3873,6 +3875,7 @@ var __async = (__this, __arguments, generator) => {
3873
3875
  });
3874
3876
  };
3875
3877
  const logger = E({
3878
+ level: process.env.NODE_ENV === "production" ? "fatal" : "info",
3876
3879
  name: "shipengine-api",
3877
3880
  serializers: __spreadProps(__spreadValues({}, k), {
3878
3881
  req: (req) => ({
@@ -3894,9 +3897,8 @@ const logger = E({
3894
3897
  }
3895
3898
  ]
3896
3899
  });
3897
- const isDataCodedErrors = (data) => !!data.errors && isCodedErrors(data.errors);
3898
3900
  class ShipEngineAPI {
3899
- constructor(token, { baseURL, headers, getToken }) {
3901
+ constructor(token, { baseURL, headers, getToken, onApiError }) {
3900
3902
  const client = axios.create({
3901
3903
  baseURL,
3902
3904
  headers: __spreadProps(__spreadValues({}, headers), {
@@ -3947,7 +3949,7 @@ class ShipEngineAPI {
3947
3949
  return res;
3948
3950
  },
3949
3951
  (err) => __async(this, null, function* () {
3950
- var _a, _b, _c, _d, _e, _f, _g;
3952
+ var _a, _b, _c, _d, _e, _f;
3951
3953
  logger.error(
3952
3954
  { err, req: err.config, res: err.response },
3953
3955
  "%s %s: %s %s - %s",
@@ -3966,22 +3968,9 @@ class ShipEngineAPI {
3966
3968
  }
3967
3969
  return axios(config);
3968
3970
  }
3969
- if ((_g = err.response) == null ? void 0 : _g.data) {
3970
- if (isDataCodedErrors(err.response.data)) {
3971
- return Promise.reject(
3972
- err.response.data.errors.map((err2) => CodedError.fromObject(err2))
3973
- );
3974
- }
3975
- if (isCodedErrors(err.response.data)) {
3976
- return Promise.reject(err.response.data.map((err2) => CodedError.fromObject(err2)));
3977
- }
3978
- if (isCodedError(err.response.data)) {
3979
- return Promise.reject([CodedError.fromObject(err.response.data)]);
3980
- }
3981
- return Promise.reject([new CodedError(err.response.data)]);
3982
- } else {
3983
- return Promise.reject([new CodedError(err.message)]);
3984
- }
3971
+ const codedErrors = parseError(err);
3972
+ onApiError == null ? void 0 : onApiError(codedErrors, err);
3973
+ return Promise.reject(codedErrors);
3985
3974
  })
3986
3975
  );
3987
3976
  this.client = client;
@@ -4144,4 +4133,4 @@ class ShipEngineAPI {
4144
4133
  }
4145
4134
  }
4146
4135
 
4147
- export { AccountSettingsAPI, AddressesAPI, CarriersAPI, CodedError, ConfirmationType, CreditCardVendor, Currency, CustomPackagesAPI, CustomsContentsType, CustomsNonDeliveryType, FundingSourcesAPI, InsuranceAPI, InsuranceProviderType, LabelsAPI, MetadataCapability, MetadataRequirement, MetadataSatisfyingFormTypes, OrderSourcesAPI, RateCardStatus, RateCardsAPI, RatesAPI, types as SE, SalesOrderShipmentsAPI, SalesOrdersAPI, ShipEngineAPI, ShipmentsAPI, ShippingRulesAPI, ThemesAPI, WarehousesAPI, getEndUserIpAddress, isCodedError, isCodedErrors };
4136
+ export { AccountSettingsAPI, AddressesAPI, CarriersAPI, CodedError, ConfirmationType, CreditCardVendor, Currency, CustomPackagesAPI, CustomsContentsType, CustomsNonDeliveryType, FundingSourcesAPI, InsuranceAPI, InsuranceProviderType, LabelsAPI, MetadataCapability, MetadataRequirement, MetadataSatisfyingFormTypes, OrderSourcesAPI, RateCardStatus, RateCardsAPI, RatesAPI, types as SE, SalesOrderShipmentsAPI, SalesOrdersAPI, ShipEngineAPI, ShipmentsAPI, ShippingRulesAPI, ThemesAPI, WarehousesAPI, getEndUserIpAddress, isCodedError, isCodedErrors, isDataCodedErrors, parseError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shipengine/js-api",
3
- "version": "0.52.1",
3
+ "version": "0.53.0",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {