@commercelayer/sdk 7.0.0-beta.17 → 7.0.0-beta.19
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/api.cjs +0 -1
- package/lib/api.js +0 -1
- package/lib/client.cjs +45 -54
- package/lib/client.js +45 -54
- package/lib/commercelayer.cjs +11 -13
- package/lib/commercelayer.js +11 -13
- package/lib/common.cjs +0 -1
- package/lib/common.js +0 -1
- package/lib/config.cjs +0 -2
- package/lib/config.js +0 -2
- package/lib/debug.cjs +0 -2
- package/lib/debug.js +0 -2
- package/lib/enum.cjs +0 -2
- package/lib/enum.js +0 -2
- package/lib/error.cjs +19 -22
- package/lib/error.js +19 -22
- package/lib/fetch.cjs +17 -24
- package/lib/fetch.js +17 -24
- package/lib/index.cjs +0 -1
- package/lib/index.d.cts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -1
- package/lib/jsonapi.cjs +0 -1
- package/lib/jsonapi.js +0 -1
- package/lib/query.cjs +0 -1
- package/lib/query.js +0 -1
- package/lib/resource.cjs +21 -26
- package/lib/resource.js +21 -26
- package/lib/resources/addresses.cjs +10 -12
- package/lib/resources/addresses.js +10 -12
- package/lib/resources/adjustments.cjs +8 -10
- package/lib/resources/adjustments.js +8 -10
- package/lib/resources/adyen_gateways.cjs +8 -10
- package/lib/resources/adyen_gateways.js +8 -10
- package/lib/resources/adyen_payments.cjs +10 -12
- package/lib/resources/adyen_payments.js +10 -12
- package/lib/resources/applications.cjs +5 -7
- package/lib/resources/applications.js +5 -7
- package/lib/resources/attachments.cjs +8 -10
- package/lib/resources/attachments.js +8 -10
- package/lib/resources/authorizations.cjs +11 -13
- package/lib/resources/authorizations.js +11 -13
- package/lib/resources/avalara_accounts.cjs +8 -10
- package/lib/resources/avalara_accounts.js +8 -10
- package/lib/resources/axerve_gateways.cjs +8 -10
- package/lib/resources/axerve_gateways.js +8 -10
- package/lib/resources/axerve_payments.cjs +9 -11
- package/lib/resources/axerve_payments.js +9 -11
- package/lib/resources/bing_geocoders.cjs +8 -10
- package/lib/resources/bing_geocoders.js +8 -10
- package/lib/resources/braintree_gateways.cjs +8 -10
- package/lib/resources/braintree_gateways.js +8 -10
- package/lib/resources/braintree_payments.cjs +8 -10
- package/lib/resources/braintree_payments.js +8 -10
- package/lib/resources/bundles.cjs +12 -14
- package/lib/resources/bundles.js +12 -14
- package/lib/resources/buy_x_pay_y_promotions.cjs +12 -14
- package/lib/resources/buy_x_pay_y_promotions.js +12 -14
- package/lib/resources/captures.cjs +10 -12
- package/lib/resources/captures.js +10 -12
- package/lib/resources/carrier_accounts.cjs +8 -10
- package/lib/resources/carrier_accounts.js +8 -10
- package/lib/resources/checkout_com_gateways.cjs +8 -10
- package/lib/resources/checkout_com_gateways.js +8 -10
- package/lib/resources/checkout_com_payments.cjs +10 -12
- package/lib/resources/checkout_com_payments.js +10 -12
- package/lib/resources/cleanups.cjs +9 -11
- package/lib/resources/cleanups.js +9 -11
- package/lib/resources/coupon_codes_promotion_rules.cjs +8 -10
- package/lib/resources/coupon_codes_promotion_rules.js +8 -10
- package/lib/resources/coupon_recipients.cjs +8 -10
- package/lib/resources/coupon_recipients.js +8 -10
- package/lib/resources/coupons.cjs +10 -12
- package/lib/resources/coupons.js +10 -12
- package/lib/resources/custom_promotion_rules.cjs +8 -10
- package/lib/resources/custom_promotion_rules.js +8 -10
- package/lib/resources/customer_addresses.cjs +8 -10
- package/lib/resources/customer_addresses.js +8 -10
- package/lib/resources/customer_groups.cjs +8 -10
- package/lib/resources/customer_groups.js +8 -10
- package/lib/resources/customer_password_resets.cjs +9 -11
- package/lib/resources/customer_password_resets.js +9 -11
- package/lib/resources/customer_payment_sources.cjs +8 -10
- package/lib/resources/customer_payment_sources.js +8 -10
- package/lib/resources/customer_subscriptions.cjs +8 -10
- package/lib/resources/customer_subscriptions.js +8 -10
- package/lib/resources/customers.cjs +10 -12
- package/lib/resources/customers.js +10 -12
- package/lib/resources/delivery_lead_times.cjs +8 -10
- package/lib/resources/delivery_lead_times.js +8 -10
- package/lib/resources/discount_engine_items.cjs +5 -7
- package/lib/resources/discount_engine_items.js +5 -7
- package/lib/resources/discount_engines.cjs +5 -7
- package/lib/resources/discount_engines.js +5 -7
- package/lib/resources/easypost_pickups.cjs +9 -11
- package/lib/resources/easypost_pickups.js +9 -11
- package/lib/resources/event_callbacks.cjs +5 -7
- package/lib/resources/event_callbacks.js +5 -7
- package/lib/resources/events.cjs +7 -9
- package/lib/resources/events.js +7 -9
- package/lib/resources/exports.cjs +9 -11
- package/lib/resources/exports.js +9 -11
- package/lib/resources/external_gateways.cjs +9 -11
- package/lib/resources/external_gateways.js +9 -11
- package/lib/resources/external_payments.cjs +8 -10
- package/lib/resources/external_payments.js +8 -10
- package/lib/resources/external_promotions.cjs +13 -15
- package/lib/resources/external_promotions.js +13 -15
- package/lib/resources/external_tax_calculators.cjs +9 -11
- package/lib/resources/external_tax_calculators.js +9 -11
- package/lib/resources/fixed_amount_promotions.cjs +12 -14
- package/lib/resources/fixed_amount_promotions.js +12 -14
- package/lib/resources/fixed_price_promotions.cjs +12 -14
- package/lib/resources/fixed_price_promotions.js +12 -14
- package/lib/resources/flex_promotions.cjs +12 -14
- package/lib/resources/flex_promotions.js +12 -14
- package/lib/resources/free_gift_promotions.cjs +12 -14
- package/lib/resources/free_gift_promotions.js +12 -14
- package/lib/resources/free_shipping_promotions.cjs +12 -14
- package/lib/resources/free_shipping_promotions.js +12 -14
- package/lib/resources/geocoders.cjs +5 -7
- package/lib/resources/geocoders.js +5 -7
- package/lib/resources/gift_card_recipients.cjs +8 -10
- package/lib/resources/gift_card_recipients.js +8 -10
- package/lib/resources/gift_cards.cjs +14 -16
- package/lib/resources/gift_cards.js +14 -16
- package/lib/resources/google_geocoders.cjs +8 -10
- package/lib/resources/google_geocoders.js +8 -10
- package/lib/resources/imports.cjs +9 -11
- package/lib/resources/imports.js +9 -11
- package/lib/resources/in_stock_subscriptions.cjs +10 -12
- package/lib/resources/in_stock_subscriptions.js +10 -12
- package/lib/resources/inventory_models.cjs +8 -10
- package/lib/resources/inventory_models.js +8 -10
- package/lib/resources/inventory_return_locations.cjs +8 -10
- package/lib/resources/inventory_return_locations.js +8 -10
- package/lib/resources/inventory_stock_locations.cjs +8 -10
- package/lib/resources/inventory_stock_locations.js +8 -10
- package/lib/resources/klarna_gateways.cjs +8 -10
- package/lib/resources/klarna_gateways.js +8 -10
- package/lib/resources/klarna_payments.cjs +9 -11
- package/lib/resources/klarna_payments.js +9 -11
- package/lib/resources/line_item_options.cjs +10 -12
- package/lib/resources/line_item_options.js +10 -12
- package/lib/resources/line_items.cjs +13 -15
- package/lib/resources/line_items.js +13 -15
- package/lib/resources/links.cjs +10 -12
- package/lib/resources/links.js +10 -12
- package/lib/resources/manual_gateways.cjs +8 -10
- package/lib/resources/manual_gateways.js +8 -10
- package/lib/resources/manual_tax_calculators.cjs +8 -10
- package/lib/resources/manual_tax_calculators.js +8 -10
- package/lib/resources/markets.cjs +10 -12
- package/lib/resources/markets.js +10 -12
- package/lib/resources/merchants.cjs +8 -10
- package/lib/resources/merchants.js +8 -10
- package/lib/resources/notifications.cjs +8 -10
- package/lib/resources/notifications.js +8 -10
- package/lib/resources/order_amount_promotion_rules.cjs +8 -10
- package/lib/resources/order_amount_promotion_rules.js +8 -10
- package/lib/resources/order_copies.cjs +8 -10
- package/lib/resources/order_copies.js +8 -10
- package/lib/resources/order_factories.cjs +5 -7
- package/lib/resources/order_factories.js +5 -7
- package/lib/resources/order_subscription_items.cjs +8 -10
- package/lib/resources/order_subscription_items.js +8 -10
- package/lib/resources/order_subscriptions.cjs +14 -16
- package/lib/resources/order_subscriptions.js +14 -16
- package/lib/resources/orders.cjs +41 -43
- package/lib/resources/orders.js +41 -43
- package/lib/resources/organizations.cjs +5 -7
- package/lib/resources/organizations.js +5 -7
- package/lib/resources/packages.cjs +8 -10
- package/lib/resources/packages.js +8 -10
- package/lib/resources/parcel_line_items.cjs +8 -10
- package/lib/resources/parcel_line_items.js +8 -10
- package/lib/resources/parcels.cjs +8 -10
- package/lib/resources/parcels.js +8 -10
- package/lib/resources/payment_gateways.cjs +5 -7
- package/lib/resources/payment_gateways.js +5 -7
- package/lib/resources/payment_methods.cjs +10 -12
- package/lib/resources/payment_methods.js +10 -12
- package/lib/resources/payment_options.cjs +8 -10
- package/lib/resources/payment_options.js +8 -10
- package/lib/resources/paypal_gateways.cjs +8 -10
- package/lib/resources/paypal_gateways.js +8 -10
- package/lib/resources/paypal_payments.cjs +8 -10
- package/lib/resources/paypal_payments.js +8 -10
- package/lib/resources/percentage_discount_promotions.cjs +12 -14
- package/lib/resources/percentage_discount_promotions.js +12 -14
- package/lib/resources/pickups.cjs +5 -7
- package/lib/resources/pickups.js +5 -7
- package/lib/resources/price_frequency_tiers.cjs +8 -10
- package/lib/resources/price_frequency_tiers.js +8 -10
- package/lib/resources/price_list_schedulers.cjs +10 -12
- package/lib/resources/price_list_schedulers.js +10 -12
- package/lib/resources/price_lists.cjs +8 -10
- package/lib/resources/price_lists.js +8 -10
- package/lib/resources/price_tiers.cjs +5 -7
- package/lib/resources/price_tiers.js +5 -7
- package/lib/resources/price_volume_tiers.cjs +8 -10
- package/lib/resources/price_volume_tiers.js +8 -10
- package/lib/resources/prices.cjs +8 -10
- package/lib/resources/prices.js +8 -10
- package/lib/resources/promotion_rules.cjs +5 -7
- package/lib/resources/promotion_rules.js +5 -7
- package/lib/resources/promotions.cjs +5 -7
- package/lib/resources/promotions.js +5 -7
- package/lib/resources/recurring_order_copies.cjs +8 -10
- package/lib/resources/recurring_order_copies.js +8 -10
- package/lib/resources/refunds.cjs +7 -9
- package/lib/resources/refunds.js +7 -9
- package/lib/resources/reserved_stocks.cjs +5 -7
- package/lib/resources/reserved_stocks.js +5 -7
- package/lib/resources/resource_errors.cjs +5 -7
- package/lib/resources/resource_errors.js +5 -7
- package/lib/resources/return_line_items.cjs +9 -11
- package/lib/resources/return_line_items.js +9 -11
- package/lib/resources/returns.cjs +21 -23
- package/lib/resources/returns.js +21 -23
- package/lib/resources/satispay_gateways.cjs +8 -10
- package/lib/resources/satispay_gateways.js +8 -10
- package/lib/resources/satispay_payments.cjs +9 -11
- package/lib/resources/satispay_payments.js +9 -11
- package/lib/resources/shipments.cjs +23 -25
- package/lib/resources/shipments.js +23 -25
- package/lib/resources/shipping_categories.cjs +8 -10
- package/lib/resources/shipping_categories.js +8 -10
- package/lib/resources/shipping_method_tiers.cjs +5 -7
- package/lib/resources/shipping_method_tiers.js +5 -7
- package/lib/resources/shipping_methods.cjs +11 -13
- package/lib/resources/shipping_methods.js +11 -13
- package/lib/resources/shipping_weight_tiers.cjs +8 -10
- package/lib/resources/shipping_weight_tiers.js +8 -10
- package/lib/resources/shipping_zones.cjs +8 -10
- package/lib/resources/shipping_zones.js +8 -10
- package/lib/resources/sku_list_items.cjs +8 -10
- package/lib/resources/sku_list_items.js +8 -10
- package/lib/resources/sku_list_promotion_rules.cjs +8 -10
- package/lib/resources/sku_list_promotion_rules.js +8 -10
- package/lib/resources/sku_lists.cjs +8 -10
- package/lib/resources/sku_lists.js +8 -10
- package/lib/resources/sku_options.cjs +10 -12
- package/lib/resources/sku_options.js +10 -12
- package/lib/resources/skus.cjs +10 -12
- package/lib/resources/skus.js +10 -12
- package/lib/resources/stock_items.cjs +9 -11
- package/lib/resources/stock_items.js +9 -11
- package/lib/resources/stock_line_items.cjs +11 -13
- package/lib/resources/stock_line_items.js +11 -13
- package/lib/resources/stock_locations.cjs +8 -10
- package/lib/resources/stock_locations.js +8 -10
- package/lib/resources/stock_reservations.cjs +9 -11
- package/lib/resources/stock_reservations.js +9 -11
- package/lib/resources/stock_transfers.cjs +14 -16
- package/lib/resources/stock_transfers.js +14 -16
- package/lib/resources/stores.cjs +8 -10
- package/lib/resources/stores.js +8 -10
- package/lib/resources/stripe_gateways.cjs +8 -10
- package/lib/resources/stripe_gateways.js +8 -10
- package/lib/resources/stripe_payments.cjs +10 -12
- package/lib/resources/stripe_payments.js +10 -12
- package/lib/resources/stripe_tax_accounts.cjs +8 -10
- package/lib/resources/stripe_tax_accounts.js +8 -10
- package/lib/resources/subscription_models.cjs +8 -10
- package/lib/resources/subscription_models.js +8 -10
- package/lib/resources/tags.cjs +8 -10
- package/lib/resources/tags.js +8 -10
- package/lib/resources/talon_one_accounts.cjs +8 -10
- package/lib/resources/talon_one_accounts.js +8 -10
- package/lib/resources/tax_calculators.cjs +5 -7
- package/lib/resources/tax_calculators.js +5 -7
- package/lib/resources/tax_categories.cjs +8 -10
- package/lib/resources/tax_categories.js +8 -10
- package/lib/resources/tax_rules.cjs +8 -10
- package/lib/resources/tax_rules.js +8 -10
- package/lib/resources/taxjar_accounts.cjs +8 -10
- package/lib/resources/taxjar_accounts.js +8 -10
- package/lib/resources/transactions.cjs +5 -7
- package/lib/resources/transactions.js +5 -7
- package/lib/resources/versions.cjs +5 -7
- package/lib/resources/versions.js +5 -7
- package/lib/resources/vertex_accounts.cjs +9 -11
- package/lib/resources/vertex_accounts.js +9 -11
- package/lib/resources/voids.cjs +7 -9
- package/lib/resources/voids.js +7 -9
- package/lib/resources/webhooks.cjs +11 -13
- package/lib/resources/webhooks.js +11 -13
- package/lib/resources/wire_transfers.cjs +8 -10
- package/lib/resources/wire_transfers.js +8 -10
- package/lib/static.cjs +0 -1
- package/lib/static.js +0 -1
- package/lib/util.cjs +0 -2
- package/lib/util.js +0 -2
- package/package.json +4 -4
- package/lib/chunk-AQ5VUG5P.js +0 -12
- package/lib/chunk-JE77HYXB.cjs +0 -17
package/lib/api.cjs
CHANGED
package/lib/api.js
CHANGED
package/lib/client.cjs
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var chunkJE77HYXB_cjs = require('./chunk-JE77HYXB.cjs');
|
4
3
|
var error = require('./error');
|
5
4
|
var config = require('./config');
|
6
5
|
var fetch = require('./fetch');
|
@@ -12,24 +11,33 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
11
|
var config__default = /*#__PURE__*/_interopDefault(config);
|
13
12
|
var Debug__default = /*#__PURE__*/_interopDefault(Debug);
|
14
13
|
|
15
|
-
var _baseUrl, _accessToken, _organization, _domain, _clientConfig, _interceptors;
|
16
14
|
const debug = Debug__default.default("client");
|
17
15
|
const baseURL = (organization, domain) => {
|
18
16
|
return `https://${organization.toLowerCase()}.${domain || config__default.default.default.domain}/api`;
|
19
17
|
};
|
20
|
-
|
18
|
+
class ApiClient {
|
19
|
+
static create(options) {
|
20
|
+
if ((!options.organization || !options.domain) && options.accessToken) {
|
21
|
+
const tokenData = util.extractTokenData(options.accessToken);
|
22
|
+
if (!options.organization && tokenData?.organization) options.organization = tokenData.organization;
|
23
|
+
if (!options.domain && tokenData?.domain) options.domain = tokenData.domain;
|
24
|
+
}
|
25
|
+
for (const attr of config__default.default.client.requiredAttributes)
|
26
|
+
if (!options[attr]) throw new error.SdkError({ message: `Undefined '${attr}' parameter` });
|
27
|
+
return new ApiClient(options);
|
28
|
+
}
|
29
|
+
#baseUrl;
|
30
|
+
#accessToken;
|
31
|
+
#organization;
|
32
|
+
#domain;
|
33
|
+
#clientConfig;
|
34
|
+
#interceptors;
|
21
35
|
constructor(options) {
|
22
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _baseUrl);
|
23
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _accessToken);
|
24
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _organization);
|
25
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _domain);
|
26
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _clientConfig);
|
27
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _interceptors);
|
28
36
|
debug("new client instance %O", options);
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
37
|
+
this.#baseUrl = baseURL(options.organization ?? "", options.domain);
|
38
|
+
this.#accessToken = options.accessToken;
|
39
|
+
this.#organization = options.organization ?? "";
|
40
|
+
this.#domain = options.domain;
|
33
41
|
const fetchConfig = {
|
34
42
|
timeout: options.timeout || config__default.default.client.timeout,
|
35
43
|
fetch: options.fetch,
|
@@ -40,30 +48,20 @@ const _ApiClient = class _ApiClient {
|
|
40
48
|
...customHeaders,
|
41
49
|
"Accept": "application/vnd.api+json",
|
42
50
|
"Content-Type": "application/vnd.api+json",
|
43
|
-
"Authorization": "Bearer " +
|
51
|
+
"Authorization": "Bearer " + this.#accessToken
|
44
52
|
};
|
45
53
|
if (options.userAgent) headers["User-Agent"] = options.userAgent;
|
46
54
|
fetchConfig.headers = headers;
|
47
|
-
|
55
|
+
this.#clientConfig = fetchConfig;
|
48
56
|
debug("fetch config: %O", fetchConfig);
|
49
|
-
|
50
|
-
}
|
51
|
-
static create(options) {
|
52
|
-
if ((!options.organization || !options.domain) && options.accessToken) {
|
53
|
-
const tokenData = util.extractTokenData(options.accessToken);
|
54
|
-
if (!options.organization && tokenData?.organization) options.organization = tokenData.organization;
|
55
|
-
if (!options.domain && tokenData?.domain) options.domain = tokenData.domain;
|
56
|
-
}
|
57
|
-
for (const attr of config__default.default.client.requiredAttributes)
|
58
|
-
if (!options[attr]) throw new error.SdkError({ message: `Undefined '${attr}' parameter` });
|
59
|
-
return new _ApiClient(options);
|
57
|
+
this.#interceptors = {};
|
60
58
|
}
|
61
59
|
get interceptors() {
|
62
|
-
return
|
60
|
+
return this.#interceptors;
|
63
61
|
}
|
64
62
|
get requestHeaders() {
|
65
|
-
if (!
|
66
|
-
return
|
63
|
+
if (!this.#clientConfig.headers) this.#clientConfig.headers = {};
|
64
|
+
return this.#clientConfig.headers;
|
67
65
|
}
|
68
66
|
/*
|
69
67
|
set requestHeaders(headers: RequestHeaders) {
|
@@ -72,18 +70,18 @@ const _ApiClient = class _ApiClient {
|
|
72
70
|
*/
|
73
71
|
config(config2) {
|
74
72
|
debug("config %o", config2);
|
75
|
-
const def =
|
73
|
+
const def = this.#clientConfig;
|
76
74
|
if (!def.headers) def.headers = {};
|
77
75
|
if (config2.timeout) def.timeout = config2.timeout;
|
78
76
|
if (config2.userAgent) this.userAgent(config2.userAgent);
|
79
|
-
if (config2.fetch)
|
80
|
-
if (config2.refreshToken)
|
81
|
-
if (config2.organization || config2.domain)
|
82
|
-
if (config2.organization)
|
83
|
-
if (config2.domain)
|
77
|
+
if (config2.fetch) this.#clientConfig.fetch = config2.fetch;
|
78
|
+
if (config2.refreshToken) this.#clientConfig.refreshToken = config2.refreshToken;
|
79
|
+
if (config2.organization || config2.domain) this.#baseUrl = baseURL(config2.organization || this.#organization, config2.domain || this.#domain);
|
80
|
+
if (config2.organization) this.#organization = config2.organization;
|
81
|
+
if (config2.domain) this.#domain = config2.domain;
|
84
82
|
if (config2.accessToken) {
|
85
|
-
|
86
|
-
def.headers.Authorization = "Bearer " +
|
83
|
+
this.#accessToken = config2.accessToken;
|
84
|
+
def.headers.Authorization = "Bearer " + this.#accessToken;
|
87
85
|
}
|
88
86
|
if (config2.headers) def.headers = { ...def.headers, ...this.customHeaders(config2.headers) };
|
89
87
|
return this;
|
@@ -95,16 +93,16 @@ const _ApiClient = class _ApiClient {
|
|
95
93
|
async request(method, path, body, options) {
|
96
94
|
debug("request %s %s, %O, %O", method, path, body || {}, options || {});
|
97
95
|
if (options?.userAgent) debug("User-Agent header ignored in request config");
|
98
|
-
const baseUrl = options?.organization ? baseURL(options.organization, options.domain) :
|
96
|
+
const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : this.#baseUrl;
|
99
97
|
const url = new URL(`${baseUrl}/${path}`);
|
100
98
|
const bodyData = body ? JSON.stringify({ data: body }) : void 0;
|
101
99
|
const headers = { ...this.requestHeaders, ...this.customHeaders(options?.headers) };
|
102
|
-
const accessToken = options?.accessToken ||
|
100
|
+
const accessToken = options?.accessToken || this.#accessToken;
|
103
101
|
if (accessToken) headers.Authorization = "Bearer " + accessToken;
|
104
|
-
const refreshToken = options?.refreshToken ||
|
105
|
-
const fetchFunction = options?.fetch ||
|
102
|
+
const refreshToken = options?.refreshToken || this.#clientConfig.refreshToken;
|
103
|
+
const fetchFunction = options?.fetch || this.#clientConfig.fetch;
|
106
104
|
const requestOptions = { method, body: bodyData, headers };
|
107
|
-
const timeout = options?.timeout ||
|
105
|
+
const timeout = options?.timeout || this.#clientConfig.timeout;
|
108
106
|
if (timeout) {
|
109
107
|
if (AbortSignal?.timeout) requestOptions.signal = AbortSignal.timeout(timeout);
|
110
108
|
else debug("Timeout not set. Undefined function: %s", "AbortSignal.timeout");
|
@@ -121,7 +119,7 @@ const _ApiClient = class _ApiClient {
|
|
121
119
|
} catch (err) {
|
122
120
|
if (error.isExpiredTokenError(err) && refreshToken && util.isTokenExpired(accessToken)) {
|
123
121
|
debug("Access token has expired");
|
124
|
-
const newAccessToken = await refreshToken(
|
122
|
+
const newAccessToken = await refreshToken(this.#accessToken).catch((e) => {
|
125
123
|
debug("Refresh token error: %s", e.message);
|
126
124
|
const tokenError = new error.SdkError({ message: "Error refreshing access token", type: error.ErrorType.TOKEN_REFRESH });
|
127
125
|
tokenError.source = e;
|
@@ -130,7 +128,7 @@ const _ApiClient = class _ApiClient {
|
|
130
128
|
if (newAccessToken) {
|
131
129
|
debug("Access token refreshed");
|
132
130
|
this.config({ accessToken: newAccessToken });
|
133
|
-
|
131
|
+
this.#accessToken = newAccessToken;
|
134
132
|
if (requestOptions.headers) requestOptions.headers.Authorization = `Bearer ${newAccessToken}`;
|
135
133
|
const response = await fetch.fetchURL(url, requestOptions, clientOptions).catch((error$1) => error.handleError(error$1));
|
136
134
|
return response;
|
@@ -147,19 +145,12 @@ const _ApiClient = class _ApiClient {
|
|
147
145
|
return customHeaders;
|
148
146
|
}
|
149
147
|
get currentAccessToken() {
|
150
|
-
return
|
148
|
+
return this.#accessToken;
|
151
149
|
}
|
152
150
|
get currentOrganization() {
|
153
|
-
return
|
151
|
+
return this.#organization;
|
154
152
|
}
|
155
|
-
}
|
156
|
-
_baseUrl = new WeakMap();
|
157
|
-
_accessToken = new WeakMap();
|
158
|
-
_organization = new WeakMap();
|
159
|
-
_domain = new WeakMap();
|
160
|
-
_clientConfig = new WeakMap();
|
161
|
-
_interceptors = new WeakMap();
|
162
|
-
let ApiClient = _ApiClient;
|
153
|
+
}
|
163
154
|
var client_default = ApiClient;
|
164
155
|
|
165
156
|
module.exports = client_default;
|
package/lib/client.js
CHANGED
@@ -1,28 +1,36 @@
|
|
1
|
-
import { __privateAdd, __privateSet, __privateGet } from './chunk-AQ5VUG5P.js';
|
2
1
|
import { SdkError, handleError, isExpiredTokenError, ErrorType } from './error';
|
3
2
|
import config from './config';
|
4
3
|
import { fetchURL } from './fetch';
|
5
4
|
import { extractTokenData, isTokenExpired } from './util';
|
6
5
|
import Debug from './debug';
|
7
6
|
|
8
|
-
var _baseUrl, _accessToken, _organization, _domain, _clientConfig, _interceptors;
|
9
7
|
const debug = Debug("client");
|
10
8
|
const baseURL = (organization, domain) => {
|
11
9
|
return `https://${organization.toLowerCase()}.${domain || config.default.domain}/api`;
|
12
10
|
};
|
13
|
-
|
11
|
+
class ApiClient {
|
12
|
+
static create(options) {
|
13
|
+
if ((!options.organization || !options.domain) && options.accessToken) {
|
14
|
+
const tokenData = extractTokenData(options.accessToken);
|
15
|
+
if (!options.organization && tokenData?.organization) options.organization = tokenData.organization;
|
16
|
+
if (!options.domain && tokenData?.domain) options.domain = tokenData.domain;
|
17
|
+
}
|
18
|
+
for (const attr of config.client.requiredAttributes)
|
19
|
+
if (!options[attr]) throw new SdkError({ message: `Undefined '${attr}' parameter` });
|
20
|
+
return new ApiClient(options);
|
21
|
+
}
|
22
|
+
#baseUrl;
|
23
|
+
#accessToken;
|
24
|
+
#organization;
|
25
|
+
#domain;
|
26
|
+
#clientConfig;
|
27
|
+
#interceptors;
|
14
28
|
constructor(options) {
|
15
|
-
__privateAdd(this, _baseUrl);
|
16
|
-
__privateAdd(this, _accessToken);
|
17
|
-
__privateAdd(this, _organization);
|
18
|
-
__privateAdd(this, _domain);
|
19
|
-
__privateAdd(this, _clientConfig);
|
20
|
-
__privateAdd(this, _interceptors);
|
21
29
|
debug("new client instance %O", options);
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
30
|
+
this.#baseUrl = baseURL(options.organization ?? "", options.domain);
|
31
|
+
this.#accessToken = options.accessToken;
|
32
|
+
this.#organization = options.organization ?? "";
|
33
|
+
this.#domain = options.domain;
|
26
34
|
const fetchConfig = {
|
27
35
|
timeout: options.timeout || config.client.timeout,
|
28
36
|
fetch: options.fetch,
|
@@ -33,30 +41,20 @@ const _ApiClient = class _ApiClient {
|
|
33
41
|
...customHeaders,
|
34
42
|
"Accept": "application/vnd.api+json",
|
35
43
|
"Content-Type": "application/vnd.api+json",
|
36
|
-
"Authorization": "Bearer " +
|
44
|
+
"Authorization": "Bearer " + this.#accessToken
|
37
45
|
};
|
38
46
|
if (options.userAgent) headers["User-Agent"] = options.userAgent;
|
39
47
|
fetchConfig.headers = headers;
|
40
|
-
|
48
|
+
this.#clientConfig = fetchConfig;
|
41
49
|
debug("fetch config: %O", fetchConfig);
|
42
|
-
|
43
|
-
}
|
44
|
-
static create(options) {
|
45
|
-
if ((!options.organization || !options.domain) && options.accessToken) {
|
46
|
-
const tokenData = extractTokenData(options.accessToken);
|
47
|
-
if (!options.organization && tokenData?.organization) options.organization = tokenData.organization;
|
48
|
-
if (!options.domain && tokenData?.domain) options.domain = tokenData.domain;
|
49
|
-
}
|
50
|
-
for (const attr of config.client.requiredAttributes)
|
51
|
-
if (!options[attr]) throw new SdkError({ message: `Undefined '${attr}' parameter` });
|
52
|
-
return new _ApiClient(options);
|
50
|
+
this.#interceptors = {};
|
53
51
|
}
|
54
52
|
get interceptors() {
|
55
|
-
return
|
53
|
+
return this.#interceptors;
|
56
54
|
}
|
57
55
|
get requestHeaders() {
|
58
|
-
if (!
|
59
|
-
return
|
56
|
+
if (!this.#clientConfig.headers) this.#clientConfig.headers = {};
|
57
|
+
return this.#clientConfig.headers;
|
60
58
|
}
|
61
59
|
/*
|
62
60
|
set requestHeaders(headers: RequestHeaders) {
|
@@ -65,18 +63,18 @@ const _ApiClient = class _ApiClient {
|
|
65
63
|
*/
|
66
64
|
config(config2) {
|
67
65
|
debug("config %o", config2);
|
68
|
-
const def =
|
66
|
+
const def = this.#clientConfig;
|
69
67
|
if (!def.headers) def.headers = {};
|
70
68
|
if (config2.timeout) def.timeout = config2.timeout;
|
71
69
|
if (config2.userAgent) this.userAgent(config2.userAgent);
|
72
|
-
if (config2.fetch)
|
73
|
-
if (config2.refreshToken)
|
74
|
-
if (config2.organization || config2.domain)
|
75
|
-
if (config2.organization)
|
76
|
-
if (config2.domain)
|
70
|
+
if (config2.fetch) this.#clientConfig.fetch = config2.fetch;
|
71
|
+
if (config2.refreshToken) this.#clientConfig.refreshToken = config2.refreshToken;
|
72
|
+
if (config2.organization || config2.domain) this.#baseUrl = baseURL(config2.organization || this.#organization, config2.domain || this.#domain);
|
73
|
+
if (config2.organization) this.#organization = config2.organization;
|
74
|
+
if (config2.domain) this.#domain = config2.domain;
|
77
75
|
if (config2.accessToken) {
|
78
|
-
|
79
|
-
def.headers.Authorization = "Bearer " +
|
76
|
+
this.#accessToken = config2.accessToken;
|
77
|
+
def.headers.Authorization = "Bearer " + this.#accessToken;
|
80
78
|
}
|
81
79
|
if (config2.headers) def.headers = { ...def.headers, ...this.customHeaders(config2.headers) };
|
82
80
|
return this;
|
@@ -88,16 +86,16 @@ const _ApiClient = class _ApiClient {
|
|
88
86
|
async request(method, path, body, options) {
|
89
87
|
debug("request %s %s, %O, %O", method, path, body || {}, options || {});
|
90
88
|
if (options?.userAgent) debug("User-Agent header ignored in request config");
|
91
|
-
const baseUrl = options?.organization ? baseURL(options.organization, options.domain) :
|
89
|
+
const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : this.#baseUrl;
|
92
90
|
const url = new URL(`${baseUrl}/${path}`);
|
93
91
|
const bodyData = body ? JSON.stringify({ data: body }) : void 0;
|
94
92
|
const headers = { ...this.requestHeaders, ...this.customHeaders(options?.headers) };
|
95
|
-
const accessToken = options?.accessToken ||
|
93
|
+
const accessToken = options?.accessToken || this.#accessToken;
|
96
94
|
if (accessToken) headers.Authorization = "Bearer " + accessToken;
|
97
|
-
const refreshToken = options?.refreshToken ||
|
98
|
-
const fetchFunction = options?.fetch ||
|
95
|
+
const refreshToken = options?.refreshToken || this.#clientConfig.refreshToken;
|
96
|
+
const fetchFunction = options?.fetch || this.#clientConfig.fetch;
|
99
97
|
const requestOptions = { method, body: bodyData, headers };
|
100
|
-
const timeout = options?.timeout ||
|
98
|
+
const timeout = options?.timeout || this.#clientConfig.timeout;
|
101
99
|
if (timeout) {
|
102
100
|
if (AbortSignal?.timeout) requestOptions.signal = AbortSignal.timeout(timeout);
|
103
101
|
else debug("Timeout not set. Undefined function: %s", "AbortSignal.timeout");
|
@@ -114,7 +112,7 @@ const _ApiClient = class _ApiClient {
|
|
114
112
|
} catch (err) {
|
115
113
|
if (isExpiredTokenError(err) && refreshToken && isTokenExpired(accessToken)) {
|
116
114
|
debug("Access token has expired");
|
117
|
-
const newAccessToken = await refreshToken(
|
115
|
+
const newAccessToken = await refreshToken(this.#accessToken).catch((e) => {
|
118
116
|
debug("Refresh token error: %s", e.message);
|
119
117
|
const tokenError = new SdkError({ message: "Error refreshing access token", type: ErrorType.TOKEN_REFRESH });
|
120
118
|
tokenError.source = e;
|
@@ -123,7 +121,7 @@ const _ApiClient = class _ApiClient {
|
|
123
121
|
if (newAccessToken) {
|
124
122
|
debug("Access token refreshed");
|
125
123
|
this.config({ accessToken: newAccessToken });
|
126
|
-
|
124
|
+
this.#accessToken = newAccessToken;
|
127
125
|
if (requestOptions.headers) requestOptions.headers.Authorization = `Bearer ${newAccessToken}`;
|
128
126
|
const response = await fetchURL(url, requestOptions, clientOptions).catch((error) => handleError(error));
|
129
127
|
return response;
|
@@ -140,19 +138,12 @@ const _ApiClient = class _ApiClient {
|
|
140
138
|
return customHeaders;
|
141
139
|
}
|
142
140
|
get currentAccessToken() {
|
143
|
-
return
|
141
|
+
return this.#accessToken;
|
144
142
|
}
|
145
143
|
get currentOrganization() {
|
146
|
-
return
|
144
|
+
return this.#organization;
|
147
145
|
}
|
148
|
-
}
|
149
|
-
_baseUrl = new WeakMap();
|
150
|
-
_accessToken = new WeakMap();
|
151
|
-
_organization = new WeakMap();
|
152
|
-
_domain = new WeakMap();
|
153
|
-
_clientConfig = new WeakMap();
|
154
|
-
_interceptors = new WeakMap();
|
155
|
-
let ApiClient = _ApiClient;
|
146
|
+
}
|
156
147
|
var client_default = ApiClient;
|
157
148
|
|
158
149
|
export { client_default as default };
|
package/lib/commercelayer.cjs
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var chunkJE77HYXB_cjs = require('./chunk-JE77HYXB.cjs');
|
6
5
|
var _enum = require('./enum');
|
7
6
|
var error = require('./error');
|
8
7
|
var resource = require('./resource');
|
@@ -14,22 +13,23 @@ var Debug__default = /*#__PURE__*/_interopDefault(Debug);
|
|
14
13
|
|
15
14
|
const debug = Debug__default.default("commercelayer");
|
16
15
|
const OPEN_API_SCHEMA_VERSION = "7.8.3";
|
17
|
-
|
18
|
-
constructor(config) {
|
19
|
-
chunkJE77HYXB_cjs.__publicField(this, "openApiSchemaVersion", OPEN_API_SCHEMA_VERSION);
|
20
|
-
debug("new commercelayer instance %O", config);
|
21
|
-
resource.ApiResourceAdapter.init(config);
|
22
|
-
}
|
16
|
+
class CommerceLayerClient {
|
23
17
|
static get openApiSchemaVersion() {
|
24
18
|
return OPEN_API_SCHEMA_VERSION;
|
25
19
|
}
|
20
|
+
openApiSchemaVersion = OPEN_API_SCHEMA_VERSION;
|
21
|
+
static cl;
|
26
22
|
// ##__CL_RESOURCES_DEF_START__##
|
27
23
|
// ##__CL_RESOURCES_DEF_TEMPLATE:: ##__TAB__#####__RESOURCE_TYPE__##?: api.##__RESOURCE_CLASS__##
|
28
24
|
// ##__CL_RESOURCES_DEF_STOP__##
|
29
25
|
static get(config) {
|
30
|
-
if (config) return
|
31
|
-
else if (!
|
32
|
-
return
|
26
|
+
if (config) return CommerceLayerClient.cl = new CommerceLayerClient(config);
|
27
|
+
else if (!CommerceLayerClient.cl) throw new Error("CommerceLayer client not initialized");
|
28
|
+
return CommerceLayerClient.cl;
|
29
|
+
}
|
30
|
+
constructor(config) {
|
31
|
+
debug("new commercelayer instance %O", config);
|
32
|
+
resource.ApiResourceAdapter.init(config);
|
33
33
|
}
|
34
34
|
// ##__CL_RESOURCES_LEAZY_LOADING_START__##
|
35
35
|
// ##__CL_RESOURCES_LEAZY_LOADING_TEMPLATE:: ##__TAB__##get ##__RESOURCE_TYPE__##(): api.##__RESOURCE_CLASS__## { return this.###__RESOURCE_TYPE__## || (this.###__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)) }
|
@@ -101,9 +101,7 @@ const _CommerceLayerClient = class _CommerceLayerClient {
|
|
101
101
|
removeRawResponseReader() {
|
102
102
|
this.interceptors.rawReader = void 0;
|
103
103
|
}
|
104
|
-
}
|
105
|
-
chunkJE77HYXB_cjs.__publicField(_CommerceLayerClient, "cl");
|
106
|
-
let CommerceLayerClient = _CommerceLayerClient;
|
104
|
+
}
|
107
105
|
const CommerceLayer = (config) => {
|
108
106
|
return CommerceLayerClient.get(config);
|
109
107
|
};
|
package/lib/commercelayer.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import { __publicField } from './chunk-AQ5VUG5P.js';
|
2
1
|
import { getResources } from './enum';
|
3
2
|
import { isApiError } from './error';
|
4
3
|
import { ApiResourceAdapter } from './resource';
|
@@ -6,22 +5,23 @@ import Debug from './debug';
|
|
6
5
|
|
7
6
|
const debug = Debug("commercelayer");
|
8
7
|
const OPEN_API_SCHEMA_VERSION = "7.8.3";
|
9
|
-
|
10
|
-
constructor(config) {
|
11
|
-
__publicField(this, "openApiSchemaVersion", OPEN_API_SCHEMA_VERSION);
|
12
|
-
debug("new commercelayer instance %O", config);
|
13
|
-
ApiResourceAdapter.init(config);
|
14
|
-
}
|
8
|
+
class CommerceLayerClient {
|
15
9
|
static get openApiSchemaVersion() {
|
16
10
|
return OPEN_API_SCHEMA_VERSION;
|
17
11
|
}
|
12
|
+
openApiSchemaVersion = OPEN_API_SCHEMA_VERSION;
|
13
|
+
static cl;
|
18
14
|
// ##__CL_RESOURCES_DEF_START__##
|
19
15
|
// ##__CL_RESOURCES_DEF_TEMPLATE:: ##__TAB__#####__RESOURCE_TYPE__##?: api.##__RESOURCE_CLASS__##
|
20
16
|
// ##__CL_RESOURCES_DEF_STOP__##
|
21
17
|
static get(config) {
|
22
|
-
if (config) return
|
23
|
-
else if (!
|
24
|
-
return
|
18
|
+
if (config) return CommerceLayerClient.cl = new CommerceLayerClient(config);
|
19
|
+
else if (!CommerceLayerClient.cl) throw new Error("CommerceLayer client not initialized");
|
20
|
+
return CommerceLayerClient.cl;
|
21
|
+
}
|
22
|
+
constructor(config) {
|
23
|
+
debug("new commercelayer instance %O", config);
|
24
|
+
ApiResourceAdapter.init(config);
|
25
25
|
}
|
26
26
|
// ##__CL_RESOURCES_LEAZY_LOADING_START__##
|
27
27
|
// ##__CL_RESOURCES_LEAZY_LOADING_TEMPLATE:: ##__TAB__##get ##__RESOURCE_TYPE__##(): api.##__RESOURCE_CLASS__## { return this.###__RESOURCE_TYPE__## || (this.###__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)) }
|
@@ -93,9 +93,7 @@ const _CommerceLayerClient = class _CommerceLayerClient {
|
|
93
93
|
removeRawResponseReader() {
|
94
94
|
this.interceptors.rawReader = void 0;
|
95
95
|
}
|
96
|
-
}
|
97
|
-
__publicField(_CommerceLayerClient, "cl");
|
98
|
-
let CommerceLayerClient = _CommerceLayerClient;
|
96
|
+
}
|
99
97
|
const CommerceLayer = (config) => {
|
100
98
|
return CommerceLayerClient.get(config);
|
101
99
|
};
|
package/lib/common.cjs
CHANGED
package/lib/common.js
CHANGED
package/lib/config.cjs
CHANGED
package/lib/config.js
CHANGED
package/lib/debug.cjs
CHANGED
package/lib/debug.js
CHANGED
package/lib/enum.cjs
CHANGED
package/lib/enum.js
CHANGED
package/lib/error.cjs
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var chunkJE77HYXB_cjs = require('./chunk-JE77HYXB.cjs');
|
4
3
|
var fetch = require('./fetch');
|
5
4
|
|
6
5
|
var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
|
@@ -13,39 +12,37 @@ var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
|
|
13
12
|
ErrorType2["TOKEN_REFRESH"] = "token-refresh";
|
14
13
|
return ErrorType2;
|
15
14
|
})(ErrorType || {});
|
16
|
-
|
15
|
+
class SdkError extends Error {
|
16
|
+
static NAME = "SdkError";
|
17
|
+
static isSdkError(error) {
|
18
|
+
return error && [SdkError.NAME, ApiError.NAME].includes(error.name) && Object.values(ErrorType).includes(error.type);
|
19
|
+
}
|
20
|
+
type;
|
21
|
+
code;
|
22
|
+
source;
|
17
23
|
constructor(error) {
|
18
24
|
super(error.message);
|
19
|
-
|
20
|
-
chunkJE77HYXB_cjs.__publicField(this, "code");
|
21
|
-
chunkJE77HYXB_cjs.__publicField(this, "source");
|
22
|
-
this.name = _SdkError.NAME;
|
25
|
+
this.name = SdkError.NAME;
|
23
26
|
this.type = error.type || "client" /* CLIENT */;
|
24
27
|
}
|
25
|
-
|
26
|
-
|
28
|
+
}
|
29
|
+
class ApiError extends SdkError {
|
30
|
+
static NAME = "ApiError";
|
31
|
+
static isApiError(error) {
|
32
|
+
return SdkError.isSdkError(error) && error.name === ApiError.NAME && error.type === "response" /* RESPONSE */;
|
27
33
|
}
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
const _ApiError = class _ApiError extends SdkError {
|
34
|
+
errors = [];
|
35
|
+
status;
|
36
|
+
statusText;
|
32
37
|
// constructor(error: SdkError)
|
33
38
|
constructor(error) {
|
34
39
|
super({ ...error, type: "response" /* RESPONSE */ });
|
35
|
-
|
36
|
-
chunkJE77HYXB_cjs.__publicField(this, "status");
|
37
|
-
chunkJE77HYXB_cjs.__publicField(this, "statusText");
|
38
|
-
this.name = _ApiError.NAME;
|
39
|
-
}
|
40
|
-
static isApiError(error) {
|
41
|
-
return SdkError.isSdkError(error) && error.name === _ApiError.NAME && error.type === "response" /* RESPONSE */;
|
40
|
+
this.name = ApiError.NAME;
|
42
41
|
}
|
43
42
|
first() {
|
44
43
|
return this.errors?.length > 0 ? this.errors[0] : void 0;
|
45
44
|
}
|
46
|
-
}
|
47
|
-
chunkJE77HYXB_cjs.__publicField(_ApiError, "NAME", "ApiError");
|
48
|
-
let ApiError = _ApiError;
|
45
|
+
}
|
49
46
|
const isRequestError = (error) => {
|
50
47
|
return error instanceof TypeError && error.message !== "fetch failed";
|
51
48
|
};
|