@shipengine/js-api 0.52.2 → 0.54.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/client.d.ts +13 -2
- package/errors/utils.d.ts +4 -0
- package/index.js +29 -19
- package/index.mjs +28 -20
- package/order-sources/api.d.ts +4 -0
- package/package.json +1 -1
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
|
|
|
@@ -3524,6 +3539,12 @@ class OrderSourcesAPI {
|
|
|
3524
3539
|
this.refresh = (orderSourceId) => {
|
|
3525
3540
|
return this.client.put(`/v-beta/order_sources/${orderSourceId}/refresh`);
|
|
3526
3541
|
};
|
|
3542
|
+
/**
|
|
3543
|
+
* The `deactivate` method remove a specific connected order source by `orderSourceId`.
|
|
3544
|
+
*/
|
|
3545
|
+
this.deactivate = (orderSourceId) => {
|
|
3546
|
+
return this.client.put(`/v-beta/order_sources/${orderSourceId}/deactivate`);
|
|
3547
|
+
};
|
|
3527
3548
|
this.client = client;
|
|
3528
3549
|
}
|
|
3529
3550
|
}
|
|
@@ -3864,6 +3885,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
3864
3885
|
});
|
|
3865
3886
|
};
|
|
3866
3887
|
const logger = E({
|
|
3888
|
+
level: process.env.NODE_ENV === "production" ? "fatal" : "info",
|
|
3867
3889
|
name: "shipengine-api",
|
|
3868
3890
|
serializers: __spreadProps(__spreadValues({}, k), {
|
|
3869
3891
|
req: (req) => ({
|
|
@@ -3885,9 +3907,8 @@ const logger = E({
|
|
|
3885
3907
|
}
|
|
3886
3908
|
]
|
|
3887
3909
|
});
|
|
3888
|
-
const isDataCodedErrors = (data) => !!data.errors && isCodedErrors(data.errors);
|
|
3889
3910
|
class ShipEngineAPI {
|
|
3890
|
-
constructor(token, { baseURL, headers, getToken }) {
|
|
3911
|
+
constructor(token, { baseURL, headers, getToken, onApiError }) {
|
|
3891
3912
|
const client = axios.create({
|
|
3892
3913
|
baseURL,
|
|
3893
3914
|
headers: __spreadProps(__spreadValues({}, headers), {
|
|
@@ -3938,7 +3959,7 @@ class ShipEngineAPI {
|
|
|
3938
3959
|
return res;
|
|
3939
3960
|
},
|
|
3940
3961
|
(err) => __async(this, null, function* () {
|
|
3941
|
-
var _a, _b, _c, _d, _e, _f
|
|
3962
|
+
var _a, _b, _c, _d, _e, _f;
|
|
3942
3963
|
logger.error(
|
|
3943
3964
|
{ err, req: err.config, res: err.response },
|
|
3944
3965
|
"%s %s: %s %s - %s",
|
|
@@ -3957,22 +3978,9 @@ class ShipEngineAPI {
|
|
|
3957
3978
|
}
|
|
3958
3979
|
return axios(config);
|
|
3959
3980
|
}
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
err.response.data.errors.map((err2) => CodedError.fromObject(err2))
|
|
3964
|
-
);
|
|
3965
|
-
}
|
|
3966
|
-
if (isCodedErrors(err.response.data)) {
|
|
3967
|
-
return Promise.reject(err.response.data.map((err2) => CodedError.fromObject(err2)));
|
|
3968
|
-
}
|
|
3969
|
-
if (isCodedError(err.response.data)) {
|
|
3970
|
-
return Promise.reject([CodedError.fromObject(err.response.data)]);
|
|
3971
|
-
}
|
|
3972
|
-
return Promise.reject([new CodedError(err.response.data)]);
|
|
3973
|
-
} else {
|
|
3974
|
-
return Promise.reject([new CodedError(err.message)]);
|
|
3975
|
-
}
|
|
3981
|
+
const codedErrors = parseError(err);
|
|
3982
|
+
onApiError == null ? void 0 : onApiError(codedErrors, err);
|
|
3983
|
+
return Promise.reject(codedErrors);
|
|
3976
3984
|
})
|
|
3977
3985
|
);
|
|
3978
3986
|
this.client = client;
|
|
@@ -4167,3 +4175,5 @@ exports.WarehousesAPI = WarehousesAPI;
|
|
|
4167
4175
|
exports.getEndUserIpAddress = getEndUserIpAddress;
|
|
4168
4176
|
exports.isCodedError = isCodedError;
|
|
4169
4177
|
exports.isCodedErrors = isCodedErrors;
|
|
4178
|
+
exports.isDataCodedErrors = isDataCodedErrors;
|
|
4179
|
+
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
|
|
|
@@ -3520,6 +3535,12 @@ class OrderSourcesAPI {
|
|
|
3520
3535
|
this.refresh = (orderSourceId) => {
|
|
3521
3536
|
return this.client.put(`/v-beta/order_sources/${orderSourceId}/refresh`);
|
|
3522
3537
|
};
|
|
3538
|
+
/**
|
|
3539
|
+
* The `deactivate` method remove a specific connected order source by `orderSourceId`.
|
|
3540
|
+
*/
|
|
3541
|
+
this.deactivate = (orderSourceId) => {
|
|
3542
|
+
return this.client.put(`/v-beta/order_sources/${orderSourceId}/deactivate`);
|
|
3543
|
+
};
|
|
3523
3544
|
this.client = client;
|
|
3524
3545
|
}
|
|
3525
3546
|
}
|
|
@@ -3860,6 +3881,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
3860
3881
|
});
|
|
3861
3882
|
};
|
|
3862
3883
|
const logger = E({
|
|
3884
|
+
level: process.env.NODE_ENV === "production" ? "fatal" : "info",
|
|
3863
3885
|
name: "shipengine-api",
|
|
3864
3886
|
serializers: __spreadProps(__spreadValues({}, k), {
|
|
3865
3887
|
req: (req) => ({
|
|
@@ -3881,9 +3903,8 @@ const logger = E({
|
|
|
3881
3903
|
}
|
|
3882
3904
|
]
|
|
3883
3905
|
});
|
|
3884
|
-
const isDataCodedErrors = (data) => !!data.errors && isCodedErrors(data.errors);
|
|
3885
3906
|
class ShipEngineAPI {
|
|
3886
|
-
constructor(token, { baseURL, headers, getToken }) {
|
|
3907
|
+
constructor(token, { baseURL, headers, getToken, onApiError }) {
|
|
3887
3908
|
const client = axios.create({
|
|
3888
3909
|
baseURL,
|
|
3889
3910
|
headers: __spreadProps(__spreadValues({}, headers), {
|
|
@@ -3934,7 +3955,7 @@ class ShipEngineAPI {
|
|
|
3934
3955
|
return res;
|
|
3935
3956
|
},
|
|
3936
3957
|
(err) => __async(this, null, function* () {
|
|
3937
|
-
var _a, _b, _c, _d, _e, _f
|
|
3958
|
+
var _a, _b, _c, _d, _e, _f;
|
|
3938
3959
|
logger.error(
|
|
3939
3960
|
{ err, req: err.config, res: err.response },
|
|
3940
3961
|
"%s %s: %s %s - %s",
|
|
@@ -3953,22 +3974,9 @@ class ShipEngineAPI {
|
|
|
3953
3974
|
}
|
|
3954
3975
|
return axios(config);
|
|
3955
3976
|
}
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
err.response.data.errors.map((err2) => CodedError.fromObject(err2))
|
|
3960
|
-
);
|
|
3961
|
-
}
|
|
3962
|
-
if (isCodedErrors(err.response.data)) {
|
|
3963
|
-
return Promise.reject(err.response.data.map((err2) => CodedError.fromObject(err2)));
|
|
3964
|
-
}
|
|
3965
|
-
if (isCodedError(err.response.data)) {
|
|
3966
|
-
return Promise.reject([CodedError.fromObject(err.response.data)]);
|
|
3967
|
-
}
|
|
3968
|
-
return Promise.reject([new CodedError(err.response.data)]);
|
|
3969
|
-
} else {
|
|
3970
|
-
return Promise.reject([new CodedError(err.message)]);
|
|
3971
|
-
}
|
|
3977
|
+
const codedErrors = parseError(err);
|
|
3978
|
+
onApiError == null ? void 0 : onApiError(codedErrors, err);
|
|
3979
|
+
return Promise.reject(codedErrors);
|
|
3972
3980
|
})
|
|
3973
3981
|
);
|
|
3974
3982
|
this.client = client;
|
|
@@ -4131,4 +4139,4 @@ class ShipEngineAPI {
|
|
|
4131
4139
|
}
|
|
4132
4140
|
}
|
|
4133
4141
|
|
|
4134
|
-
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 };
|
|
4142
|
+
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/order-sources/api.d.ts
CHANGED
|
@@ -21,4 +21,8 @@ export declare class OrderSourcesAPI {
|
|
|
21
21
|
* This will pull in any `new orders` since the last order import.
|
|
22
22
|
*/
|
|
23
23
|
refresh: (orderSourceId: string) => Promise<import("axios").AxiosResponse<OrderSource, any>>;
|
|
24
|
+
/**
|
|
25
|
+
* The `deactivate` method remove a specific connected order source by `orderSourceId`.
|
|
26
|
+
*/
|
|
27
|
+
deactivate: (orderSourceId: string) => Promise<import("axios").AxiosResponse<any, any>>;
|
|
24
28
|
}
|