@shipengine/js-api 2.3.1 → 2.5.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 CHANGED
@@ -72,12 +72,23 @@ export interface ShipEngineAPIConfig {
72
72
  */
73
73
  export declare class ShipEngineAPI {
74
74
  private client;
75
- private sandboxToken?;
75
+ private getSandboxToken?;
76
+ sandboxToken?: string;
76
77
  constructor(token: string, { baseURL, headers, getToken, getSandboxToken, onApiError }: ShipEngineAPIConfig);
77
78
  /**
78
79
  * The `token` method takes in a string and sets it as the Authorization header for all requests.
79
80
  */
80
81
  set token(token: string);
82
+ get token(): string;
83
+ /**
84
+ * The `getTenant` method get the current token (based on `isSandbox` prop) and will get the Tenant ID from
85
+ * that token (also known as Seller ID)
86
+ */
87
+ getTenant(isSandbox?: boolean): Promise<string | null>;
88
+ /**
89
+ * The `getTenantFromToken` method receives a token and returns the Tenant ID associated with it.
90
+ */
91
+ private getTenantFromToken;
81
92
  /**
82
93
  * The `accountSettings` method provides access to the Account Settings endpoints
83
94
  * in ShipEngine API.
package/index.js CHANGED
@@ -127,6 +127,8 @@ var CustomsNonDeliveryType = /* @__PURE__ */ ((CustomsNonDeliveryType2) => {
127
127
  })(CustomsNonDeliveryType || {});
128
128
  var InsuranceProviderType = /* @__PURE__ */ ((InsuranceProviderType2) => {
129
129
  InsuranceProviderType2["FUNDING_SOURCE"] = "funding_source";
130
+ InsuranceProviderType2["PARCELGUARD"] = "parcelguard";
131
+ InsuranceProviderType2["X_COVER"] = "x_cover";
130
132
  InsuranceProviderType2["SHIPSURANCE"] = "shipsurance";
131
133
  InsuranceProviderType2["CARRIER"] = "carrier";
132
134
  InsuranceProviderType2["THIRD_PARTY"] = "third_party";
@@ -973,7 +975,7 @@ var ipaddr = {
973
975
  }).call(commonjsGlobal);
974
976
  } (ipaddr));
975
977
 
976
- var __async$5 = (__this, __arguments, generator) => {
978
+ var __async$6 = (__this, __arguments, generator) => {
977
979
  return new Promise((resolve, reject) => {
978
980
  var fulfilled = (value) => {
979
981
  try {
@@ -993,7 +995,7 @@ var __async$5 = (__this, __arguments, generator) => {
993
995
  step((generator = generator.apply(__this, __arguments)).next());
994
996
  });
995
997
  };
996
- const getEndUserIpAddress = () => __async$5(void 0, null, function* () {
998
+ const getEndUserIpAddress = () => __async$6(void 0, null, function* () {
997
999
  try {
998
1000
  const response = yield axios.get("https://api.ipify.org/?format=json");
999
1001
  if (response.data.ip && ipaddrExports.isValid(response.data.ip)) {
@@ -1036,7 +1038,7 @@ var __objRest = (source, exclude) => {
1036
1038
  }
1037
1039
  return target;
1038
1040
  };
1039
- var __async$4 = (__this, __arguments, generator) => {
1041
+ var __async$5 = (__this, __arguments, generator) => {
1040
1042
  return new Promise((resolve, reject) => {
1041
1043
  var fulfilled = (value) => {
1042
1044
  try {
@@ -1076,7 +1078,7 @@ class CarriersAPI {
1076
1078
  /**
1077
1079
  * The `connect` method connects a carrier account to a user's ShipEngine account.
1078
1080
  */
1079
- this.connect = (_a) => __async$4(this, null, function* () {
1081
+ this.connect = (_a) => __async$5(this, null, function* () {
1080
1082
  var _b = _a, { carrierCode } = _b, connection = __objRest(_b, ["carrierCode"]);
1081
1083
  const endUserIpAddress = yield getEndUserIpAddress();
1082
1084
  if (!endUserIpAddress)
@@ -1167,7 +1169,7 @@ class CarriersAPI {
1167
1169
  }
1168
1170
  }
1169
1171
 
1170
- var __async$3 = (__this, __arguments, generator) => {
1172
+ var __async$4 = (__this, __arguments, generator) => {
1171
1173
  return new Promise((resolve, reject) => {
1172
1174
  var fulfilled = (value) => {
1173
1175
  try {
@@ -1209,7 +1211,7 @@ class ConnectionsAPI {
1209
1211
  /**
1210
1212
  * The `connectCarrier` method connects a carrier to account.
1211
1213
  */
1212
- this.connectCarrier = (carrierName, formData) => __async$3(this, null, function* () {
1214
+ this.connectCarrier = (carrierName, formData) => __async$4(this, null, function* () {
1213
1215
  return yield this.client.post(
1214
1216
  `/v1/connections/carriers/${carrierName}`,
1215
1217
  formData,
@@ -1395,6 +1397,64 @@ var humps = {
1395
1397
  })(commonjsGlobal);
1396
1398
  } (humps));
1397
1399
 
1400
+ class InvalidTokenError extends Error {
1401
+ }
1402
+ InvalidTokenError.prototype.name = "InvalidTokenError";
1403
+ function b64DecodeUnicode(str) {
1404
+ return decodeURIComponent(atob(str).replace(/(.)/g, (m, p) => {
1405
+ let code = p.charCodeAt(0).toString(16).toUpperCase();
1406
+ if (code.length < 2) {
1407
+ code = "0" + code;
1408
+ }
1409
+ return "%" + code;
1410
+ }));
1411
+ }
1412
+ function base64UrlDecode(str) {
1413
+ let output = str.replace(/-/g, "+").replace(/_/g, "/");
1414
+ switch (output.length % 4) {
1415
+ case 0:
1416
+ break;
1417
+ case 2:
1418
+ output += "==";
1419
+ break;
1420
+ case 3:
1421
+ output += "=";
1422
+ break;
1423
+ default:
1424
+ throw new Error("base64 string is not of the correct length");
1425
+ }
1426
+ try {
1427
+ return b64DecodeUnicode(output);
1428
+ }
1429
+ catch (err) {
1430
+ return atob(output);
1431
+ }
1432
+ }
1433
+ function jwtDecode(token, options) {
1434
+ if (typeof token !== "string") {
1435
+ throw new InvalidTokenError("Invalid token specified: must be a string");
1436
+ }
1437
+ options || (options = {});
1438
+ const pos = options.header === true ? 0 : 1;
1439
+ const part = token.split(".")[pos];
1440
+ if (typeof part !== "string") {
1441
+ throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
1442
+ }
1443
+ let decoded;
1444
+ try {
1445
+ decoded = base64UrlDecode(part);
1446
+ }
1447
+ catch (e) {
1448
+ throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`);
1449
+ }
1450
+ try {
1451
+ return JSON.parse(decoded);
1452
+ }
1453
+ catch (e) {
1454
+ throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`);
1455
+ }
1456
+ }
1457
+
1398
1458
  /* eslint complexity: [2, 18], max-statements: [2, 33] */
1399
1459
  var shams = function hasSymbols() {
1400
1460
  if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }
@@ -3480,7 +3540,7 @@ var __spreadValues$3 = (a, b) => {
3480
3540
  }
3481
3541
  return a;
3482
3542
  };
3483
- var __async$2 = (__this, __arguments, generator) => {
3543
+ var __async$3 = (__this, __arguments, generator) => {
3484
3544
  return new Promise((resolve, reject) => {
3485
3545
  var fulfilled = (value) => {
3486
3546
  try {
@@ -3519,7 +3579,7 @@ class FundingSourcesAPI {
3519
3579
  * The `create` method creates a new funding source for a given user. This requires
3520
3580
  * payment information to be collected from the user.
3521
3581
  */
3522
- this.create = (createFundingSource) => __async$2(this, null, function* () {
3582
+ this.create = (createFundingSource) => __async$3(this, null, function* () {
3523
3583
  const endUserIpAddress = yield getEndUserIpAddress();
3524
3584
  if (!endUserIpAddress) {
3525
3585
  return Promise.reject([new CodedError("Unable to get IP address")]);
@@ -3533,7 +3593,7 @@ class FundingSourcesAPI {
3533
3593
  * user to update the billing address or payment information associated with the
3534
3594
  * funding source.
3535
3595
  */
3536
- this.update = (billingInfo, creditCardInfo, fundingSourceId) => __async$2(this, null, function* () {
3596
+ this.update = (billingInfo, creditCardInfo, fundingSourceId) => __async$3(this, null, function* () {
3537
3597
  const endUserIpAddress = yield getEndUserIpAddress();
3538
3598
  if (!endUserIpAddress) {
3539
3599
  return Promise.reject([new CodedError("Unable to get IP address")]);
@@ -3551,7 +3611,7 @@ class FundingSourcesAPI {
3551
3611
  * The `registerCarrier` method registers a carrier account and associates
3552
3612
  * it with a given funding source.
3553
3613
  */
3554
- this.registerCarrier = (carrier) => __async$2(this, null, function* () {
3614
+ this.registerCarrier = (carrier) => __async$3(this, null, function* () {
3555
3615
  const endUserIpAddress = yield getEndUserIpAddress();
3556
3616
  if (!endUserIpAddress) {
3557
3617
  return Promise.reject([new CodedError("Unable to get IP address")]);
@@ -3563,7 +3623,7 @@ class FundingSourcesAPI {
3563
3623
  /**
3564
3624
  * The `addFunds` method allows you to add funds to a funding source.
3565
3625
  */
3566
- this.addFunds = (amount, fundingSourceId) => __async$2(this, null, function* () {
3626
+ this.addFunds = (amount, fundingSourceId) => __async$3(this, null, function* () {
3567
3627
  return yield this.client.put(
3568
3628
  `/v1/funding_sources/${fundingSourceId}/add_funds`,
3569
3629
  amount
@@ -3573,7 +3633,7 @@ class FundingSourcesAPI {
3573
3633
  * The `metadata` method returns seller-specific requirements for creating funding sources
3574
3634
  * and attaching carriers
3575
3635
  */
3576
- this.metadata = () => __async$2(this, null, function* () {
3636
+ this.metadata = () => __async$3(this, null, function* () {
3577
3637
  return yield this.client.get("/v1/funding_sources/metadata");
3578
3638
  });
3579
3639
  /**
@@ -3872,6 +3932,26 @@ class SalesOrdersAPI {
3872
3932
  }
3873
3933
  }
3874
3934
 
3935
+ var __async$2 = (__this, __arguments, generator) => {
3936
+ return new Promise((resolve, reject) => {
3937
+ var fulfilled = (value) => {
3938
+ try {
3939
+ step(generator.next(value));
3940
+ } catch (e) {
3941
+ reject(e);
3942
+ }
3943
+ };
3944
+ var rejected = (value) => {
3945
+ try {
3946
+ step(generator.throw(value));
3947
+ } catch (e) {
3948
+ reject(e);
3949
+ }
3950
+ };
3951
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
3952
+ step((generator = generator.apply(__this, __arguments)).next());
3953
+ });
3954
+ };
3875
3955
  class SellersAPI {
3876
3956
  constructor(client) {
3877
3957
  /**
@@ -3880,6 +3960,55 @@ class SellersAPI {
3880
3960
  this.listSandboxSellerIds = ({ sellerId }) => {
3881
3961
  return this.client.get(`/v1/sellers/sandbox_sellers/${sellerId}`);
3882
3962
  };
3963
+ /**
3964
+ * Given a seller ID, returns a list of API Keys created for that seller.
3965
+ */
3966
+ this.listSellerApiKeys = (props) => {
3967
+ return this.client.get(`/v1/sellers/api_keys?sellerId=${props.sellerId}`, {
3968
+ isSandbox: props.isSandbox
3969
+ });
3970
+ };
3971
+ /**
3972
+ * Creates an API Key for a Seller ID
3973
+ */
3974
+ this.createSellerApiKey = ({
3975
+ description,
3976
+ sellerId,
3977
+ isSandbox
3978
+ }) => {
3979
+ return this.client.post(
3980
+ "/v1/sellers/create_key",
3981
+ {
3982
+ description,
3983
+ sellerId
3984
+ },
3985
+ {
3986
+ isSandbox
3987
+ }
3988
+ );
3989
+ };
3990
+ /**
3991
+ * Deletes an API Key
3992
+ */
3993
+ this.deleteSellerApiKey = (_0) => __async$2(this, [_0], function* ({
3994
+ encryptedApiKey,
3995
+ sellerId,
3996
+ isSandbox
3997
+ }) {
3998
+ const sellerApiKeys = yield this.listSellerApiKeys({ isSandbox, sellerId });
3999
+ if (!sellerApiKeys.data.find((sellerApiKey) => sellerApiKey.encryptedApiKey === encryptedApiKey)) {
4000
+ return Promise.reject([
4001
+ new CodedError("Missing Seller ID from Token", {
4002
+ errorCode: "unauthorized",
4003
+ errorSource: "client",
4004
+ errorType: "security"
4005
+ })
4006
+ ]);
4007
+ }
4008
+ return this.client.delete("/v1/sellers", {
4009
+ data: encryptedApiKey
4010
+ });
4011
+ });
3883
4012
  /**
3884
4013
  * Creates a sandbox seller account for a given seller ID
3885
4014
  * you can pass in a flag to try to reuse the carrier connection.
@@ -21443,6 +21572,7 @@ const logger = E({
21443
21572
  });
21444
21573
  class ShipEngineAPI {
21445
21574
  constructor(token, { baseURL, headers, getToken, getSandboxToken, onApiError }) {
21575
+ this.getSandboxToken = getSandboxToken;
21446
21576
  const client = axios.create({
21447
21577
  baseURL,
21448
21578
  headers: __spreadProps(__spreadValues({}, headers), {
@@ -21546,6 +21676,35 @@ class ShipEngineAPI {
21546
21676
  set token(token) {
21547
21677
  this.client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
21548
21678
  }
21679
+ get token() {
21680
+ return this.client.defaults.headers.common["Authorization"];
21681
+ }
21682
+ /**
21683
+ * The `getTenant` method get the current token (based on `isSandbox` prop) and will get the Tenant ID from
21684
+ * that token (also known as Seller ID)
21685
+ */
21686
+ getTenant(isSandbox) {
21687
+ return __async(this, null, function* () {
21688
+ var _a;
21689
+ if (!isSandbox) {
21690
+ return this.getTenantFromToken(this.token);
21691
+ }
21692
+ if (!this.sandboxToken) {
21693
+ this.sandboxToken = yield (_a = this.getSandboxToken) == null ? void 0 : _a.call(this);
21694
+ }
21695
+ return this.getTenantFromToken(this.sandboxToken);
21696
+ });
21697
+ }
21698
+ /**
21699
+ * The `getTenantFromToken` method receives a token and returns the Tenant ID associated with it.
21700
+ */
21701
+ getTenantFromToken(token) {
21702
+ try {
21703
+ return jwtDecode(token).tenant;
21704
+ } catch (e) {
21705
+ return null;
21706
+ }
21707
+ }
21549
21708
  /**
21550
21709
  * The `accountSettings` method provides access to the Account Settings endpoints
21551
21710
  * in ShipEngine API.
package/index.mjs CHANGED
@@ -123,6 +123,8 @@ var CustomsNonDeliveryType = /* @__PURE__ */ ((CustomsNonDeliveryType2) => {
123
123
  })(CustomsNonDeliveryType || {});
124
124
  var InsuranceProviderType = /* @__PURE__ */ ((InsuranceProviderType2) => {
125
125
  InsuranceProviderType2["FUNDING_SOURCE"] = "funding_source";
126
+ InsuranceProviderType2["PARCELGUARD"] = "parcelguard";
127
+ InsuranceProviderType2["X_COVER"] = "x_cover";
126
128
  InsuranceProviderType2["SHIPSURANCE"] = "shipsurance";
127
129
  InsuranceProviderType2["CARRIER"] = "carrier";
128
130
  InsuranceProviderType2["THIRD_PARTY"] = "third_party";
@@ -969,7 +971,7 @@ var ipaddr = {
969
971
  }).call(commonjsGlobal);
970
972
  } (ipaddr));
971
973
 
972
- var __async$5 = (__this, __arguments, generator) => {
974
+ var __async$6 = (__this, __arguments, generator) => {
973
975
  return new Promise((resolve, reject) => {
974
976
  var fulfilled = (value) => {
975
977
  try {
@@ -989,7 +991,7 @@ var __async$5 = (__this, __arguments, generator) => {
989
991
  step((generator = generator.apply(__this, __arguments)).next());
990
992
  });
991
993
  };
992
- const getEndUserIpAddress = () => __async$5(void 0, null, function* () {
994
+ const getEndUserIpAddress = () => __async$6(void 0, null, function* () {
993
995
  try {
994
996
  const response = yield axios.get("https://api.ipify.org/?format=json");
995
997
  if (response.data.ip && ipaddrExports.isValid(response.data.ip)) {
@@ -1032,7 +1034,7 @@ var __objRest = (source, exclude) => {
1032
1034
  }
1033
1035
  return target;
1034
1036
  };
1035
- var __async$4 = (__this, __arguments, generator) => {
1037
+ var __async$5 = (__this, __arguments, generator) => {
1036
1038
  return new Promise((resolve, reject) => {
1037
1039
  var fulfilled = (value) => {
1038
1040
  try {
@@ -1072,7 +1074,7 @@ class CarriersAPI {
1072
1074
  /**
1073
1075
  * The `connect` method connects a carrier account to a user's ShipEngine account.
1074
1076
  */
1075
- this.connect = (_a) => __async$4(this, null, function* () {
1077
+ this.connect = (_a) => __async$5(this, null, function* () {
1076
1078
  var _b = _a, { carrierCode } = _b, connection = __objRest(_b, ["carrierCode"]);
1077
1079
  const endUserIpAddress = yield getEndUserIpAddress();
1078
1080
  if (!endUserIpAddress)
@@ -1163,7 +1165,7 @@ class CarriersAPI {
1163
1165
  }
1164
1166
  }
1165
1167
 
1166
- var __async$3 = (__this, __arguments, generator) => {
1168
+ var __async$4 = (__this, __arguments, generator) => {
1167
1169
  return new Promise((resolve, reject) => {
1168
1170
  var fulfilled = (value) => {
1169
1171
  try {
@@ -1205,7 +1207,7 @@ class ConnectionsAPI {
1205
1207
  /**
1206
1208
  * The `connectCarrier` method connects a carrier to account.
1207
1209
  */
1208
- this.connectCarrier = (carrierName, formData) => __async$3(this, null, function* () {
1210
+ this.connectCarrier = (carrierName, formData) => __async$4(this, null, function* () {
1209
1211
  return yield this.client.post(
1210
1212
  `/v1/connections/carriers/${carrierName}`,
1211
1213
  formData,
@@ -1391,6 +1393,64 @@ var humps = {
1391
1393
  })(commonjsGlobal);
1392
1394
  } (humps));
1393
1395
 
1396
+ class InvalidTokenError extends Error {
1397
+ }
1398
+ InvalidTokenError.prototype.name = "InvalidTokenError";
1399
+ function b64DecodeUnicode(str) {
1400
+ return decodeURIComponent(atob(str).replace(/(.)/g, (m, p) => {
1401
+ let code = p.charCodeAt(0).toString(16).toUpperCase();
1402
+ if (code.length < 2) {
1403
+ code = "0" + code;
1404
+ }
1405
+ return "%" + code;
1406
+ }));
1407
+ }
1408
+ function base64UrlDecode(str) {
1409
+ let output = str.replace(/-/g, "+").replace(/_/g, "/");
1410
+ switch (output.length % 4) {
1411
+ case 0:
1412
+ break;
1413
+ case 2:
1414
+ output += "==";
1415
+ break;
1416
+ case 3:
1417
+ output += "=";
1418
+ break;
1419
+ default:
1420
+ throw new Error("base64 string is not of the correct length");
1421
+ }
1422
+ try {
1423
+ return b64DecodeUnicode(output);
1424
+ }
1425
+ catch (err) {
1426
+ return atob(output);
1427
+ }
1428
+ }
1429
+ function jwtDecode(token, options) {
1430
+ if (typeof token !== "string") {
1431
+ throw new InvalidTokenError("Invalid token specified: must be a string");
1432
+ }
1433
+ options || (options = {});
1434
+ const pos = options.header === true ? 0 : 1;
1435
+ const part = token.split(".")[pos];
1436
+ if (typeof part !== "string") {
1437
+ throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
1438
+ }
1439
+ let decoded;
1440
+ try {
1441
+ decoded = base64UrlDecode(part);
1442
+ }
1443
+ catch (e) {
1444
+ throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`);
1445
+ }
1446
+ try {
1447
+ return JSON.parse(decoded);
1448
+ }
1449
+ catch (e) {
1450
+ throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`);
1451
+ }
1452
+ }
1453
+
1394
1454
  /* eslint complexity: [2, 18], max-statements: [2, 33] */
1395
1455
  var shams = function hasSymbols() {
1396
1456
  if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }
@@ -3476,7 +3536,7 @@ var __spreadValues$3 = (a, b) => {
3476
3536
  }
3477
3537
  return a;
3478
3538
  };
3479
- var __async$2 = (__this, __arguments, generator) => {
3539
+ var __async$3 = (__this, __arguments, generator) => {
3480
3540
  return new Promise((resolve, reject) => {
3481
3541
  var fulfilled = (value) => {
3482
3542
  try {
@@ -3515,7 +3575,7 @@ class FundingSourcesAPI {
3515
3575
  * The `create` method creates a new funding source for a given user. This requires
3516
3576
  * payment information to be collected from the user.
3517
3577
  */
3518
- this.create = (createFundingSource) => __async$2(this, null, function* () {
3578
+ this.create = (createFundingSource) => __async$3(this, null, function* () {
3519
3579
  const endUserIpAddress = yield getEndUserIpAddress();
3520
3580
  if (!endUserIpAddress) {
3521
3581
  return Promise.reject([new CodedError("Unable to get IP address")]);
@@ -3529,7 +3589,7 @@ class FundingSourcesAPI {
3529
3589
  * user to update the billing address or payment information associated with the
3530
3590
  * funding source.
3531
3591
  */
3532
- this.update = (billingInfo, creditCardInfo, fundingSourceId) => __async$2(this, null, function* () {
3592
+ this.update = (billingInfo, creditCardInfo, fundingSourceId) => __async$3(this, null, function* () {
3533
3593
  const endUserIpAddress = yield getEndUserIpAddress();
3534
3594
  if (!endUserIpAddress) {
3535
3595
  return Promise.reject([new CodedError("Unable to get IP address")]);
@@ -3547,7 +3607,7 @@ class FundingSourcesAPI {
3547
3607
  * The `registerCarrier` method registers a carrier account and associates
3548
3608
  * it with a given funding source.
3549
3609
  */
3550
- this.registerCarrier = (carrier) => __async$2(this, null, function* () {
3610
+ this.registerCarrier = (carrier) => __async$3(this, null, function* () {
3551
3611
  const endUserIpAddress = yield getEndUserIpAddress();
3552
3612
  if (!endUserIpAddress) {
3553
3613
  return Promise.reject([new CodedError("Unable to get IP address")]);
@@ -3559,7 +3619,7 @@ class FundingSourcesAPI {
3559
3619
  /**
3560
3620
  * The `addFunds` method allows you to add funds to a funding source.
3561
3621
  */
3562
- this.addFunds = (amount, fundingSourceId) => __async$2(this, null, function* () {
3622
+ this.addFunds = (amount, fundingSourceId) => __async$3(this, null, function* () {
3563
3623
  return yield this.client.put(
3564
3624
  `/v1/funding_sources/${fundingSourceId}/add_funds`,
3565
3625
  amount
@@ -3569,7 +3629,7 @@ class FundingSourcesAPI {
3569
3629
  * The `metadata` method returns seller-specific requirements for creating funding sources
3570
3630
  * and attaching carriers
3571
3631
  */
3572
- this.metadata = () => __async$2(this, null, function* () {
3632
+ this.metadata = () => __async$3(this, null, function* () {
3573
3633
  return yield this.client.get("/v1/funding_sources/metadata");
3574
3634
  });
3575
3635
  /**
@@ -3868,6 +3928,26 @@ class SalesOrdersAPI {
3868
3928
  }
3869
3929
  }
3870
3930
 
3931
+ var __async$2 = (__this, __arguments, generator) => {
3932
+ return new Promise((resolve, reject) => {
3933
+ var fulfilled = (value) => {
3934
+ try {
3935
+ step(generator.next(value));
3936
+ } catch (e) {
3937
+ reject(e);
3938
+ }
3939
+ };
3940
+ var rejected = (value) => {
3941
+ try {
3942
+ step(generator.throw(value));
3943
+ } catch (e) {
3944
+ reject(e);
3945
+ }
3946
+ };
3947
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
3948
+ step((generator = generator.apply(__this, __arguments)).next());
3949
+ });
3950
+ };
3871
3951
  class SellersAPI {
3872
3952
  constructor(client) {
3873
3953
  /**
@@ -3876,6 +3956,55 @@ class SellersAPI {
3876
3956
  this.listSandboxSellerIds = ({ sellerId }) => {
3877
3957
  return this.client.get(`/v1/sellers/sandbox_sellers/${sellerId}`);
3878
3958
  };
3959
+ /**
3960
+ * Given a seller ID, returns a list of API Keys created for that seller.
3961
+ */
3962
+ this.listSellerApiKeys = (props) => {
3963
+ return this.client.get(`/v1/sellers/api_keys?sellerId=${props.sellerId}`, {
3964
+ isSandbox: props.isSandbox
3965
+ });
3966
+ };
3967
+ /**
3968
+ * Creates an API Key for a Seller ID
3969
+ */
3970
+ this.createSellerApiKey = ({
3971
+ description,
3972
+ sellerId,
3973
+ isSandbox
3974
+ }) => {
3975
+ return this.client.post(
3976
+ "/v1/sellers/create_key",
3977
+ {
3978
+ description,
3979
+ sellerId
3980
+ },
3981
+ {
3982
+ isSandbox
3983
+ }
3984
+ );
3985
+ };
3986
+ /**
3987
+ * Deletes an API Key
3988
+ */
3989
+ this.deleteSellerApiKey = (_0) => __async$2(this, [_0], function* ({
3990
+ encryptedApiKey,
3991
+ sellerId,
3992
+ isSandbox
3993
+ }) {
3994
+ const sellerApiKeys = yield this.listSellerApiKeys({ isSandbox, sellerId });
3995
+ if (!sellerApiKeys.data.find((sellerApiKey) => sellerApiKey.encryptedApiKey === encryptedApiKey)) {
3996
+ return Promise.reject([
3997
+ new CodedError("Missing Seller ID from Token", {
3998
+ errorCode: "unauthorized",
3999
+ errorSource: "client",
4000
+ errorType: "security"
4001
+ })
4002
+ ]);
4003
+ }
4004
+ return this.client.delete("/v1/sellers", {
4005
+ data: encryptedApiKey
4006
+ });
4007
+ });
3879
4008
  /**
3880
4009
  * Creates a sandbox seller account for a given seller ID
3881
4010
  * you can pass in a flag to try to reuse the carrier connection.
@@ -21439,6 +21568,7 @@ const logger = E({
21439
21568
  });
21440
21569
  class ShipEngineAPI {
21441
21570
  constructor(token, { baseURL, headers, getToken, getSandboxToken, onApiError }) {
21571
+ this.getSandboxToken = getSandboxToken;
21442
21572
  const client = axios.create({
21443
21573
  baseURL,
21444
21574
  headers: __spreadProps(__spreadValues({}, headers), {
@@ -21542,6 +21672,35 @@ class ShipEngineAPI {
21542
21672
  set token(token) {
21543
21673
  this.client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
21544
21674
  }
21675
+ get token() {
21676
+ return this.client.defaults.headers.common["Authorization"];
21677
+ }
21678
+ /**
21679
+ * The `getTenant` method get the current token (based on `isSandbox` prop) and will get the Tenant ID from
21680
+ * that token (also known as Seller ID)
21681
+ */
21682
+ getTenant(isSandbox) {
21683
+ return __async(this, null, function* () {
21684
+ var _a;
21685
+ if (!isSandbox) {
21686
+ return this.getTenantFromToken(this.token);
21687
+ }
21688
+ if (!this.sandboxToken) {
21689
+ this.sandboxToken = yield (_a = this.getSandboxToken) == null ? void 0 : _a.call(this);
21690
+ }
21691
+ return this.getTenantFromToken(this.sandboxToken);
21692
+ });
21693
+ }
21694
+ /**
21695
+ * The `getTenantFromToken` method receives a token and returns the Tenant ID associated with it.
21696
+ */
21697
+ getTenantFromToken(token) {
21698
+ try {
21699
+ return jwtDecode(token).tenant;
21700
+ } catch (e) {
21701
+ return null;
21702
+ }
21703
+ }
21545
21704
  /**
21546
21705
  * The `accountSettings` method provides access to the Account Settings endpoints
21547
21706
  * in ShipEngine API.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shipengine/js-api",
3
- "version": "2.3.1",
3
+ "version": "2.5.0",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -17,6 +17,7 @@
17
17
  "tsconfig": "./tsconfig.lib.json"
18
18
  },
19
19
  "peerDependencies": {
20
- "axios": "^0.26.1"
20
+ "axios": "^0.26.1",
21
+ "jwt-decode": "^4.0.0"
21
22
  }
22
23
  }
package/sellers/api.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AxiosInstance } from "axios";
2
- import { SandboxableMutation } from "../resources";
3
- import type { ListSandboxSellersParams, SandboxMethodParams } from "./types";
2
+ import { SandboxableMutation, SandboxableQuery } from "../resources";
3
+ import type { CreateSellerApiKeyParams, DeleteSellerApiKeyParams, ListSandboxSellersParams, SandboxMethodParams, SellerApiKey } from "./types";
4
4
  /**
5
5
  * Sellers API endpoints
6
6
  */
@@ -11,6 +11,20 @@ export declare class SellersAPI {
11
11
  * Given a seller ID, returns a list of sandbox seller IDs for that seller.
12
12
  */
13
13
  listSandboxSellerIds: ({ sellerId }: ListSandboxSellersParams) => Promise<import("axios").AxiosResponse<string[], any>>;
14
+ /**
15
+ * Given a seller ID, returns a list of API Keys created for that seller.
16
+ */
17
+ listSellerApiKeys: (props: SandboxableQuery<{
18
+ sellerId: string;
19
+ }>) => Promise<import("axios").AxiosResponse<SellerApiKey[], any>>;
20
+ /**
21
+ * Creates an API Key for a Seller ID
22
+ */
23
+ createSellerApiKey: ({ description, sellerId, isSandbox, }: SandboxableMutation<CreateSellerApiKeyParams>) => Promise<import("axios").AxiosResponse<SellerApiKey, any>>;
24
+ /**
25
+ * Deletes an API Key
26
+ */
27
+ deleteSellerApiKey: ({ encryptedApiKey, sellerId, isSandbox, }: SandboxableMutation<DeleteSellerApiKeyParams>) => Promise<import("axios").AxiosResponse<void, any>>;
14
28
  /**
15
29
  * Creates a sandbox seller account for a given seller ID
16
30
  * you can pass in a flag to try to reuse the carrier connection.
@@ -1,7 +1,25 @@
1
1
  export type ListSandboxSellersParams = {
2
2
  sellerId: string;
3
3
  };
4
+ export type ListSellerApiKeysParams = {
5
+ sellerId: string;
6
+ };
4
7
  export type SandboxMethodParams = {
5
8
  sellerId: string;
6
9
  tryReuseCarrierConnection?: boolean;
7
10
  };
11
+ export type DeleteSellerApiKeyParams = {
12
+ encryptedApiKey: string;
13
+ sellerId: string;
14
+ };
15
+ export type CreateSellerApiKeyParams = {
16
+ description?: string;
17
+ sellerId: string;
18
+ };
19
+ export type SellerApiKey = {
20
+ accountId: number;
21
+ apiKeyId: number;
22
+ createdAt: string;
23
+ description?: string;
24
+ encryptedApiKey: string;
25
+ };
@@ -74,6 +74,8 @@ export type DangerousGoodsRegulationLevel = "lightly_regulated" | "fully_regulat
74
74
  export type DangerousGoodsTransportMean = "ground" | "water" | "cargo_aircraft_only" | "passenger_aircraft";
75
75
  export declare enum InsuranceProviderType {
76
76
  FUNDING_SOURCE = "funding_source",
77
+ PARCELGUARD = "parcelguard",
78
+ X_COVER = "x_cover",
77
79
  SHIPSURANCE = "shipsurance",
78
80
  CARRIER = "carrier",
79
81
  THIRD_PARTY = "third_party",
@@ -172,6 +174,7 @@ export interface Shipment {
172
174
  billToCountryCode?: string;
173
175
  billToParty?: BillToParties;
174
176
  billToPostalCode?: string;
177
+ canadaDeliveredDuty?: "sender_prepay" | null;
175
178
  collectOnDelivery?: {
176
179
  paymentAmount: Money;
177
180
  paymentType: PaymentTypes;