@commercelayer/sdk 7.0.0-beta.17 → 7.0.0-beta.18
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 +7460 -792
- package/lib/api.js +7331 -132
- package/lib/client.cjs +253 -78
- package/lib/client.js +252 -72
- package/lib/commercelayer.cjs +777 -31
- package/lib/commercelayer.js +773 -23
- package/lib/common.cjs +143 -7
- package/lib/common.js +141 -5
- package/lib/config.cjs +2 -3
- package/lib/config.js +2 -3
- package/lib/debug.cjs +12 -6
- package/lib/debug.js +12 -6
- package/lib/enum.cjs +9 -10
- package/lib/enum.js +9 -10
- package/lib/error.cjs +75 -26
- package/lib/error.js +74 -25
- package/lib/fetch.cjs +85 -33
- package/lib/fetch.js +87 -31
- package/lib/index.cjs +8104 -23
- package/lib/index.d.cts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +7977 -4
- package/lib/jsonapi.cjs +188 -21
- package/lib/jsonapi.js +186 -14
- package/lib/query.cjs +75 -17
- package/lib/query.js +77 -15
- package/lib/resource.cjs +660 -71
- package/lib/resource.js +646 -51
- package/lib/resources/addresses.cjs +794 -6
- package/lib/resources/addresses.js +794 -6
- package/lib/resources/adjustments.cjs +794 -6
- package/lib/resources/adjustments.js +794 -6
- package/lib/resources/adyen_gateways.cjs +794 -6
- package/lib/resources/adyen_gateways.js +794 -6
- package/lib/resources/adyen_payments.cjs +794 -6
- package/lib/resources/adyen_payments.js +794 -6
- package/lib/resources/applications.cjs +786 -6
- package/lib/resources/applications.js +786 -6
- package/lib/resources/attachments.cjs +794 -6
- package/lib/resources/attachments.js +794 -6
- package/lib/resources/authorizations.cjs +794 -6
- package/lib/resources/authorizations.js +794 -6
- package/lib/resources/avalara_accounts.cjs +794 -6
- package/lib/resources/avalara_accounts.js +794 -6
- package/lib/resources/axerve_gateways.cjs +794 -6
- package/lib/resources/axerve_gateways.js +794 -6
- package/lib/resources/axerve_payments.cjs +794 -6
- package/lib/resources/axerve_payments.js +794 -6
- package/lib/resources/bing_geocoders.cjs +794 -6
- package/lib/resources/bing_geocoders.js +794 -6
- package/lib/resources/braintree_gateways.cjs +794 -6
- package/lib/resources/braintree_gateways.js +794 -6
- package/lib/resources/braintree_payments.cjs +794 -6
- package/lib/resources/braintree_payments.js +794 -6
- package/lib/resources/bundles.cjs +794 -6
- package/lib/resources/bundles.js +794 -6
- package/lib/resources/buy_x_pay_y_promotions.cjs +794 -6
- package/lib/resources/buy_x_pay_y_promotions.js +794 -6
- package/lib/resources/captures.cjs +794 -6
- package/lib/resources/captures.js +794 -6
- package/lib/resources/carrier_accounts.cjs +794 -6
- package/lib/resources/carrier_accounts.js +794 -6
- package/lib/resources/checkout_com_gateways.cjs +794 -6
- package/lib/resources/checkout_com_gateways.js +794 -6
- package/lib/resources/checkout_com_payments.cjs +794 -6
- package/lib/resources/checkout_com_payments.js +794 -6
- package/lib/resources/cleanups.cjs +794 -6
- package/lib/resources/cleanups.js +794 -6
- package/lib/resources/coupon_codes_promotion_rules.cjs +794 -6
- package/lib/resources/coupon_codes_promotion_rules.js +794 -6
- package/lib/resources/coupon_recipients.cjs +794 -6
- package/lib/resources/coupon_recipients.js +794 -6
- package/lib/resources/coupons.cjs +794 -6
- package/lib/resources/coupons.js +794 -6
- package/lib/resources/custom_promotion_rules.cjs +794 -6
- package/lib/resources/custom_promotion_rules.js +794 -6
- package/lib/resources/customer_addresses.cjs +794 -6
- package/lib/resources/customer_addresses.js +794 -6
- package/lib/resources/customer_groups.cjs +794 -6
- package/lib/resources/customer_groups.js +794 -6
- package/lib/resources/customer_password_resets.cjs +794 -6
- package/lib/resources/customer_password_resets.js +794 -6
- package/lib/resources/customer_payment_sources.cjs +794 -6
- package/lib/resources/customer_payment_sources.js +794 -6
- package/lib/resources/customer_subscriptions.cjs +794 -6
- package/lib/resources/customer_subscriptions.js +794 -6
- package/lib/resources/customers.cjs +794 -6
- package/lib/resources/customers.js +794 -6
- package/lib/resources/delivery_lead_times.cjs +794 -6
- package/lib/resources/delivery_lead_times.js +794 -6
- package/lib/resources/discount_engine_items.cjs +794 -6
- package/lib/resources/discount_engine_items.js +794 -6
- package/lib/resources/discount_engines.cjs +794 -6
- package/lib/resources/discount_engines.js +794 -6
- package/lib/resources/easypost_pickups.cjs +794 -6
- package/lib/resources/easypost_pickups.js +794 -6
- package/lib/resources/event_callbacks.cjs +794 -6
- package/lib/resources/event_callbacks.js +794 -6
- package/lib/resources/events.cjs +794 -6
- package/lib/resources/events.js +794 -6
- package/lib/resources/exports.cjs +794 -6
- package/lib/resources/exports.js +794 -6
- package/lib/resources/external_gateways.cjs +794 -6
- package/lib/resources/external_gateways.js +794 -6
- package/lib/resources/external_payments.cjs +794 -6
- package/lib/resources/external_payments.js +794 -6
- package/lib/resources/external_promotions.cjs +794 -6
- package/lib/resources/external_promotions.js +794 -6
- package/lib/resources/external_tax_calculators.cjs +794 -6
- package/lib/resources/external_tax_calculators.js +794 -6
- package/lib/resources/fixed_amount_promotions.cjs +794 -6
- package/lib/resources/fixed_amount_promotions.js +794 -6
- package/lib/resources/fixed_price_promotions.cjs +794 -6
- package/lib/resources/fixed_price_promotions.js +794 -6
- package/lib/resources/flex_promotions.cjs +794 -6
- package/lib/resources/flex_promotions.js +794 -6
- package/lib/resources/free_gift_promotions.cjs +794 -6
- package/lib/resources/free_gift_promotions.js +794 -6
- package/lib/resources/free_shipping_promotions.cjs +794 -6
- package/lib/resources/free_shipping_promotions.js +794 -6
- package/lib/resources/geocoders.cjs +794 -6
- package/lib/resources/geocoders.js +794 -6
- package/lib/resources/gift_card_recipients.cjs +794 -6
- package/lib/resources/gift_card_recipients.js +794 -6
- package/lib/resources/gift_cards.cjs +794 -6
- package/lib/resources/gift_cards.js +794 -6
- package/lib/resources/google_geocoders.cjs +794 -6
- package/lib/resources/google_geocoders.js +794 -6
- package/lib/resources/imports.cjs +794 -6
- package/lib/resources/imports.js +794 -6
- package/lib/resources/in_stock_subscriptions.cjs +794 -6
- package/lib/resources/in_stock_subscriptions.js +794 -6
- package/lib/resources/inventory_models.cjs +794 -6
- package/lib/resources/inventory_models.js +794 -6
- package/lib/resources/inventory_return_locations.cjs +794 -6
- package/lib/resources/inventory_return_locations.js +794 -6
- package/lib/resources/inventory_stock_locations.cjs +794 -6
- package/lib/resources/inventory_stock_locations.js +794 -6
- package/lib/resources/klarna_gateways.cjs +794 -6
- package/lib/resources/klarna_gateways.js +794 -6
- package/lib/resources/klarna_payments.cjs +794 -6
- package/lib/resources/klarna_payments.js +794 -6
- package/lib/resources/line_item_options.cjs +794 -6
- package/lib/resources/line_item_options.js +794 -6
- package/lib/resources/line_items.cjs +794 -6
- package/lib/resources/line_items.js +794 -6
- package/lib/resources/links.cjs +794 -6
- package/lib/resources/links.js +794 -6
- package/lib/resources/manual_gateways.cjs +794 -6
- package/lib/resources/manual_gateways.js +794 -6
- package/lib/resources/manual_tax_calculators.cjs +794 -6
- package/lib/resources/manual_tax_calculators.js +794 -6
- package/lib/resources/markets.cjs +794 -6
- package/lib/resources/markets.js +794 -6
- package/lib/resources/merchants.cjs +794 -6
- package/lib/resources/merchants.js +794 -6
- package/lib/resources/notifications.cjs +794 -6
- package/lib/resources/notifications.js +794 -6
- package/lib/resources/order_amount_promotion_rules.cjs +794 -6
- package/lib/resources/order_amount_promotion_rules.js +794 -6
- package/lib/resources/order_copies.cjs +794 -6
- package/lib/resources/order_copies.js +794 -6
- package/lib/resources/order_factories.cjs +794 -6
- package/lib/resources/order_factories.js +794 -6
- package/lib/resources/order_subscription_items.cjs +794 -6
- package/lib/resources/order_subscription_items.js +794 -6
- package/lib/resources/order_subscriptions.cjs +794 -6
- package/lib/resources/order_subscriptions.js +794 -6
- package/lib/resources/orders.cjs +794 -6
- package/lib/resources/orders.js +794 -6
- package/lib/resources/organizations.cjs +786 -6
- package/lib/resources/organizations.js +786 -6
- package/lib/resources/packages.cjs +794 -6
- package/lib/resources/packages.js +794 -6
- package/lib/resources/parcel_line_items.cjs +794 -6
- package/lib/resources/parcel_line_items.js +794 -6
- package/lib/resources/parcels.cjs +794 -6
- package/lib/resources/parcels.js +794 -6
- package/lib/resources/payment_gateways.cjs +794 -6
- package/lib/resources/payment_gateways.js +794 -6
- package/lib/resources/payment_methods.cjs +794 -6
- package/lib/resources/payment_methods.js +794 -6
- package/lib/resources/payment_options.cjs +794 -6
- package/lib/resources/payment_options.js +794 -6
- package/lib/resources/paypal_gateways.cjs +794 -6
- package/lib/resources/paypal_gateways.js +794 -6
- package/lib/resources/paypal_payments.cjs +794 -6
- package/lib/resources/paypal_payments.js +794 -6
- package/lib/resources/percentage_discount_promotions.cjs +794 -6
- package/lib/resources/percentage_discount_promotions.js +794 -6
- package/lib/resources/pickups.cjs +794 -6
- package/lib/resources/pickups.js +794 -6
- package/lib/resources/price_frequency_tiers.cjs +794 -6
- package/lib/resources/price_frequency_tiers.js +794 -6
- package/lib/resources/price_list_schedulers.cjs +794 -6
- package/lib/resources/price_list_schedulers.js +794 -6
- package/lib/resources/price_lists.cjs +794 -6
- package/lib/resources/price_lists.js +794 -6
- package/lib/resources/price_tiers.cjs +794 -6
- package/lib/resources/price_tiers.js +794 -6
- package/lib/resources/price_volume_tiers.cjs +794 -6
- package/lib/resources/price_volume_tiers.js +794 -6
- package/lib/resources/prices.cjs +794 -6
- package/lib/resources/prices.js +794 -6
- package/lib/resources/promotion_rules.cjs +794 -6
- package/lib/resources/promotion_rules.js +794 -6
- package/lib/resources/promotions.cjs +794 -6
- package/lib/resources/promotions.js +794 -6
- package/lib/resources/recurring_order_copies.cjs +794 -6
- package/lib/resources/recurring_order_copies.js +794 -6
- package/lib/resources/refunds.cjs +794 -6
- package/lib/resources/refunds.js +794 -6
- package/lib/resources/reserved_stocks.cjs +794 -6
- package/lib/resources/reserved_stocks.js +794 -6
- package/lib/resources/resource_errors.cjs +794 -6
- package/lib/resources/resource_errors.js +794 -6
- package/lib/resources/return_line_items.cjs +794 -6
- package/lib/resources/return_line_items.js +794 -6
- package/lib/resources/returns.cjs +794 -6
- package/lib/resources/returns.js +794 -6
- package/lib/resources/satispay_gateways.cjs +794 -6
- package/lib/resources/satispay_gateways.js +794 -6
- package/lib/resources/satispay_payments.cjs +794 -6
- package/lib/resources/satispay_payments.js +794 -6
- package/lib/resources/shipments.cjs +794 -6
- package/lib/resources/shipments.js +794 -6
- package/lib/resources/shipping_categories.cjs +794 -6
- package/lib/resources/shipping_categories.js +794 -6
- package/lib/resources/shipping_method_tiers.cjs +794 -6
- package/lib/resources/shipping_method_tiers.js +794 -6
- package/lib/resources/shipping_methods.cjs +794 -6
- package/lib/resources/shipping_methods.js +794 -6
- package/lib/resources/shipping_weight_tiers.cjs +794 -6
- package/lib/resources/shipping_weight_tiers.js +794 -6
- package/lib/resources/shipping_zones.cjs +794 -6
- package/lib/resources/shipping_zones.js +794 -6
- package/lib/resources/sku_list_items.cjs +794 -6
- package/lib/resources/sku_list_items.js +794 -6
- package/lib/resources/sku_list_promotion_rules.cjs +794 -6
- package/lib/resources/sku_list_promotion_rules.js +794 -6
- package/lib/resources/sku_lists.cjs +794 -6
- package/lib/resources/sku_lists.js +794 -6
- package/lib/resources/sku_options.cjs +794 -6
- package/lib/resources/sku_options.js +794 -6
- package/lib/resources/skus.cjs +794 -6
- package/lib/resources/skus.js +794 -6
- package/lib/resources/stock_items.cjs +794 -6
- package/lib/resources/stock_items.js +794 -6
- package/lib/resources/stock_line_items.cjs +794 -6
- package/lib/resources/stock_line_items.js +794 -6
- package/lib/resources/stock_locations.cjs +794 -6
- package/lib/resources/stock_locations.js +794 -6
- package/lib/resources/stock_reservations.cjs +794 -6
- package/lib/resources/stock_reservations.js +794 -6
- package/lib/resources/stock_transfers.cjs +794 -6
- package/lib/resources/stock_transfers.js +794 -6
- package/lib/resources/stores.cjs +794 -6
- package/lib/resources/stores.js +794 -6
- package/lib/resources/stripe_gateways.cjs +794 -6
- package/lib/resources/stripe_gateways.js +794 -6
- package/lib/resources/stripe_payments.cjs +794 -6
- package/lib/resources/stripe_payments.js +794 -6
- package/lib/resources/stripe_tax_accounts.cjs +794 -6
- package/lib/resources/stripe_tax_accounts.js +794 -6
- package/lib/resources/subscription_models.cjs +794 -6
- package/lib/resources/subscription_models.js +794 -6
- package/lib/resources/tags.cjs +794 -6
- package/lib/resources/tags.js +794 -6
- package/lib/resources/talon_one_accounts.cjs +794 -6
- package/lib/resources/talon_one_accounts.js +794 -6
- package/lib/resources/tax_calculators.cjs +794 -6
- package/lib/resources/tax_calculators.js +794 -6
- package/lib/resources/tax_categories.cjs +794 -6
- package/lib/resources/tax_categories.js +794 -6
- package/lib/resources/tax_rules.cjs +794 -6
- package/lib/resources/tax_rules.js +794 -6
- package/lib/resources/taxjar_accounts.cjs +794 -6
- package/lib/resources/taxjar_accounts.js +794 -6
- package/lib/resources/transactions.cjs +794 -6
- package/lib/resources/transactions.js +794 -6
- package/lib/resources/versions.cjs +794 -6
- package/lib/resources/versions.js +794 -6
- package/lib/resources/vertex_accounts.cjs +794 -6
- package/lib/resources/vertex_accounts.js +794 -6
- package/lib/resources/voids.cjs +794 -6
- package/lib/resources/voids.js +794 -6
- package/lib/resources/webhooks.cjs +794 -6
- package/lib/resources/webhooks.js +794 -6
- package/lib/resources/wire_transfers.cjs +794 -6
- package/lib/resources/wire_transfers.js +794 -6
- package/lib/static.cjs +753 -20
- package/lib/static.js +739 -6
- package/lib/util.cjs +5 -6
- package/lib/util.js +5 -6
- package/package.json +1 -1
- package/lib/chunk-AQ5VUG5P.js +0 -12
- package/lib/chunk-JE77HYXB.cjs +0 -17
package/lib/client.cjs
CHANGED
@@ -1,37 +1,229 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
4
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
5
|
+
}) : x)(function(x) {
|
6
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
8
|
+
});
|
9
9
|
|
10
|
-
|
10
|
+
// src/debug.ts
|
11
|
+
var debuggerFunction = (_pattern, ..._args) => {
|
12
|
+
};
|
13
|
+
var debuggerFactory = (_namespace) => debuggerFunction;
|
14
|
+
try {
|
15
|
+
const debugModule = __require("debug");
|
16
|
+
if (debugModule && typeof debugModule === "function") debuggerFactory = debugModule;
|
17
|
+
} catch (error) {
|
18
|
+
}
|
19
|
+
var debugPrefix = "clsdk";
|
20
|
+
var debug = (namespace) => {
|
21
|
+
return debuggerFactory(`${debugPrefix}:${namespace}`);
|
22
|
+
};
|
23
|
+
var debug_default = debug;
|
24
|
+
|
25
|
+
// src/fetch.ts
|
26
|
+
var debug2 = debug_default("fetch");
|
27
|
+
var FetchError = class _FetchError extends Error {
|
28
|
+
static isFetchError = (error) => {
|
29
|
+
return error instanceof _FetchError;
|
30
|
+
};
|
31
|
+
#errors;
|
32
|
+
#status;
|
33
|
+
#statusText;
|
34
|
+
#request;
|
35
|
+
constructor(status, statusText, body, request) {
|
36
|
+
super(statusText);
|
37
|
+
this.#status = status;
|
38
|
+
this.#statusText = statusText;
|
39
|
+
if (body) this.#errors = body.errors;
|
40
|
+
if (request) this.#request = request;
|
41
|
+
}
|
42
|
+
get errors() {
|
43
|
+
return this.#errors;
|
44
|
+
}
|
45
|
+
get status() {
|
46
|
+
return this.#status;
|
47
|
+
}
|
48
|
+
get statusText() {
|
49
|
+
return this.#statusText;
|
50
|
+
}
|
51
|
+
get request() {
|
52
|
+
return this.#request;
|
53
|
+
}
|
54
|
+
};
|
55
|
+
var fetchURL = async (url, requestOptions, clientOptions) => {
|
56
|
+
debug2("fetch: %s, %O, native[%s]", url, requestOptions || {}, clientOptions?.fetch ? "no" : "yes");
|
57
|
+
const interceptors = clientOptions?.interceptors;
|
58
|
+
if (interceptors?.request?.onSuccess) ({ url, options: requestOptions } = await interceptors.request.onSuccess({ url, options: requestOptions }));
|
59
|
+
const fetchClient = clientOptions?.fetch || fetch;
|
60
|
+
let response = await fetchClient(url, requestOptions);
|
61
|
+
if (response.ok) {
|
62
|
+
if (interceptors?.rawReader?.onSuccess) await interceptors.rawReader.onSuccess(response);
|
63
|
+
if (interceptors?.response?.onSuccess) response = await interceptors.response.onSuccess(response);
|
64
|
+
} else {
|
65
|
+
if (interceptors?.rawReader?.onFailure) await interceptors.rawReader.onFailure(response);
|
66
|
+
}
|
67
|
+
const responseBody = response.body && response.status !== 204 ? await response.json().then((json) => {
|
68
|
+
debug2("response: %O", json);
|
69
|
+
return json;
|
70
|
+
}).catch((err) => {
|
71
|
+
debug2("error: %s", err.message);
|
72
|
+
if (response.ok) throw new SdkError({ message: "Error parsing API response body", type: "parse" /* PARSE */ });
|
73
|
+
}) : void 0;
|
74
|
+
if (!response.ok) {
|
75
|
+
let error = new FetchError(response.status, response.statusText, responseBody, requestOptions);
|
76
|
+
if (interceptors?.response?.onFailure) error = await interceptors.response.onFailure(error);
|
77
|
+
if (error) throw error;
|
78
|
+
}
|
79
|
+
return responseBody;
|
80
|
+
};
|
81
|
+
|
82
|
+
// src/error.ts
|
83
|
+
var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
|
84
|
+
ErrorType2["CLIENT"] = "client";
|
85
|
+
ErrorType2["REQUEST"] = "request";
|
86
|
+
ErrorType2["RESPONSE"] = "response";
|
87
|
+
ErrorType2["CANCEL"] = "cancel";
|
88
|
+
ErrorType2["PARSE"] = "parse";
|
89
|
+
ErrorType2["TIMEOUT"] = "timeout";
|
90
|
+
ErrorType2["TOKEN_REFRESH"] = "token-refresh";
|
91
|
+
return ErrorType2;
|
92
|
+
})(ErrorType || {});
|
93
|
+
var SdkError = class _SdkError extends Error {
|
94
|
+
static NAME = "SdkError";
|
95
|
+
static isSdkError(error) {
|
96
|
+
return error && [_SdkError.NAME, ApiError.NAME].includes(error.name) && Object.values(ErrorType).includes(error.type);
|
97
|
+
}
|
98
|
+
type;
|
99
|
+
code;
|
100
|
+
source;
|
101
|
+
constructor(error) {
|
102
|
+
super(error.message);
|
103
|
+
this.name = _SdkError.NAME;
|
104
|
+
this.type = error.type || "client" /* CLIENT */;
|
105
|
+
}
|
106
|
+
};
|
107
|
+
var ApiError = class _ApiError extends SdkError {
|
108
|
+
static NAME = "ApiError";
|
109
|
+
static isApiError(error) {
|
110
|
+
return SdkError.isSdkError(error) && error.name === _ApiError.NAME && error.type === "response" /* RESPONSE */;
|
111
|
+
}
|
112
|
+
errors = [];
|
113
|
+
status;
|
114
|
+
statusText;
|
115
|
+
// constructor(error: SdkError)
|
116
|
+
constructor(error) {
|
117
|
+
super({ ...error, type: "response" /* RESPONSE */ });
|
118
|
+
this.name = _ApiError.NAME;
|
119
|
+
}
|
120
|
+
first() {
|
121
|
+
return this.errors?.length > 0 ? this.errors[0] : void 0;
|
122
|
+
}
|
123
|
+
};
|
124
|
+
var isRequestError = (error) => {
|
125
|
+
return error instanceof TypeError && error.message !== "fetch failed";
|
126
|
+
};
|
127
|
+
var isCancelError = (error) => {
|
128
|
+
return error instanceof DOMException && error.name === "AbortError";
|
129
|
+
};
|
130
|
+
var isTimeoutError = (error) => {
|
131
|
+
return error instanceof DOMException && error.name === "TimeoutError";
|
132
|
+
};
|
133
|
+
var isExpiredTokenError = (error) => {
|
134
|
+
const invalidTokenCodes = [
|
135
|
+
"INVALID_TOKEN"
|
136
|
+
/*, 'UNAUTHORIZED' */
|
137
|
+
];
|
138
|
+
return ApiError.isApiError(error) && error.status === 401 && (invalidTokenCodes.includes(error.code || "") || error.errors && error.errors.length > 0 && invalidTokenCodes.includes(error.errors[0].code));
|
139
|
+
};
|
140
|
+
var handleError = (error) => {
|
141
|
+
if (SdkError.isSdkError(error) || ApiError.isApiError(error)) throw error;
|
142
|
+
let sdkError = new SdkError({ message: error.message });
|
143
|
+
if (FetchError.isFetchError(error)) {
|
144
|
+
const apiError = new ApiError(sdkError);
|
145
|
+
apiError.type = "response" /* RESPONSE */;
|
146
|
+
apiError.status = error.status;
|
147
|
+
apiError.statusText = error.statusText;
|
148
|
+
apiError.code = String(apiError.status);
|
149
|
+
apiError.errors = error.errors || [];
|
150
|
+
if (!apiError.message && apiError.statusText) apiError.message = apiError.statusText;
|
151
|
+
sdkError = apiError;
|
152
|
+
} else if (isRequestError(error)) {
|
153
|
+
sdkError.type = "request" /* REQUEST */;
|
154
|
+
} else if (isCancelError(error)) {
|
155
|
+
sdkError.type = "cancel" /* CANCEL */;
|
156
|
+
} else if (isTimeoutError(error)) {
|
157
|
+
sdkError.type = "timeout" /* TIMEOUT */;
|
158
|
+
} else {
|
159
|
+
sdkError.type = "client" /* CLIENT */;
|
160
|
+
sdkError.source = error;
|
161
|
+
}
|
162
|
+
throw sdkError;
|
163
|
+
};
|
11
164
|
|
12
|
-
|
13
|
-
var
|
165
|
+
// src/config.ts
|
166
|
+
var config = {
|
167
|
+
default: {
|
168
|
+
domain: "commercelayer.io"},
|
169
|
+
client: {
|
170
|
+
timeout: 15e3,
|
171
|
+
requiredAttributes: ["organization", "accessToken"]
|
172
|
+
}};
|
173
|
+
var config_default = config;
|
174
|
+
|
175
|
+
// src/util.ts
|
176
|
+
var extractTokenData = (token) => {
|
177
|
+
try {
|
178
|
+
const data = JSON.parse(atob(token.split(".")[1]));
|
179
|
+
return {
|
180
|
+
organization: data.organization.slug,
|
181
|
+
domain: data.iss ? String(data.iss).replace("https://auth.", "") : void 0,
|
182
|
+
expiration: data.exp
|
183
|
+
};
|
184
|
+
} catch (err) {
|
185
|
+
return void 0;
|
186
|
+
}
|
187
|
+
};
|
188
|
+
var isTokenExpired = (token) => {
|
189
|
+
try {
|
190
|
+
const tokenData = extractTokenData(token);
|
191
|
+
return tokenData?.expiration ? tokenData.expiration * 1e3 - Date.now() < 0 : false;
|
192
|
+
} catch (err) {
|
193
|
+
return false;
|
194
|
+
}
|
195
|
+
};
|
14
196
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
return `https://${organization.toLowerCase()}.${domain ||
|
197
|
+
// src/client.ts
|
198
|
+
var debug3 = debug_default("client");
|
199
|
+
var baseURL = (organization, domain) => {
|
200
|
+
return `https://${organization.toLowerCase()}.${domain || config_default.default.domain}/api`;
|
19
201
|
};
|
20
|
-
|
202
|
+
var ApiClient = class _ApiClient {
|
203
|
+
static create(options) {
|
204
|
+
if ((!options.organization || !options.domain) && options.accessToken) {
|
205
|
+
const tokenData = extractTokenData(options.accessToken);
|
206
|
+
if (!options.organization && tokenData?.organization) options.organization = tokenData.organization;
|
207
|
+
if (!options.domain && tokenData?.domain) options.domain = tokenData.domain;
|
208
|
+
}
|
209
|
+
for (const attr of config_default.client.requiredAttributes)
|
210
|
+
if (!options[attr]) throw new SdkError({ message: `Undefined '${attr}' parameter` });
|
211
|
+
return new _ApiClient(options);
|
212
|
+
}
|
213
|
+
#baseUrl;
|
214
|
+
#accessToken;
|
215
|
+
#organization;
|
216
|
+
#domain;
|
217
|
+
#clientConfig;
|
218
|
+
#interceptors;
|
21
219
|
constructor(options) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
chunkJE77HYXB_cjs.__privateAdd(this, _interceptors);
|
28
|
-
debug("new client instance %O", options);
|
29
|
-
chunkJE77HYXB_cjs.__privateSet(this, _baseUrl, baseURL(options.organization ?? "", options.domain));
|
30
|
-
chunkJE77HYXB_cjs.__privateSet(this, _accessToken, options.accessToken);
|
31
|
-
chunkJE77HYXB_cjs.__privateSet(this, _organization, options.organization ?? "");
|
32
|
-
chunkJE77HYXB_cjs.__privateSet(this, _domain, options.domain);
|
220
|
+
debug3("new client instance %O", options);
|
221
|
+
this.#baseUrl = baseURL(options.organization ?? "", options.domain);
|
222
|
+
this.#accessToken = options.accessToken;
|
223
|
+
this.#organization = options.organization ?? "";
|
224
|
+
this.#domain = options.domain;
|
33
225
|
const fetchConfig = {
|
34
|
-
timeout: options.timeout ||
|
226
|
+
timeout: options.timeout || config_default.client.timeout,
|
35
227
|
fetch: options.fetch,
|
36
228
|
refreshToken: options.refreshToken
|
37
229
|
};
|
@@ -40,30 +232,20 @@ const _ApiClient = class _ApiClient {
|
|
40
232
|
...customHeaders,
|
41
233
|
"Accept": "application/vnd.api+json",
|
42
234
|
"Content-Type": "application/vnd.api+json",
|
43
|
-
"Authorization": "Bearer " +
|
235
|
+
"Authorization": "Bearer " + this.#accessToken
|
44
236
|
};
|
45
237
|
if (options.userAgent) headers["User-Agent"] = options.userAgent;
|
46
238
|
fetchConfig.headers = headers;
|
47
|
-
|
48
|
-
|
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);
|
239
|
+
this.#clientConfig = fetchConfig;
|
240
|
+
debug3("fetch config: %O", fetchConfig);
|
241
|
+
this.#interceptors = {};
|
60
242
|
}
|
61
243
|
get interceptors() {
|
62
|
-
return
|
244
|
+
return this.#interceptors;
|
63
245
|
}
|
64
246
|
get requestHeaders() {
|
65
|
-
if (!
|
66
|
-
return
|
247
|
+
if (!this.#clientConfig.headers) this.#clientConfig.headers = {};
|
248
|
+
return this.#clientConfig.headers;
|
67
249
|
}
|
68
250
|
/*
|
69
251
|
set requestHeaders(headers: RequestHeaders) {
|
@@ -71,19 +253,19 @@ const _ApiClient = class _ApiClient {
|
|
71
253
|
}
|
72
254
|
*/
|
73
255
|
config(config2) {
|
74
|
-
|
75
|
-
const def =
|
256
|
+
debug3("config %o", config2);
|
257
|
+
const def = this.#clientConfig;
|
76
258
|
if (!def.headers) def.headers = {};
|
77
259
|
if (config2.timeout) def.timeout = config2.timeout;
|
78
260
|
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)
|
261
|
+
if (config2.fetch) this.#clientConfig.fetch = config2.fetch;
|
262
|
+
if (config2.refreshToken) this.#clientConfig.refreshToken = config2.refreshToken;
|
263
|
+
if (config2.organization || config2.domain) this.#baseUrl = baseURL(config2.organization || this.#organization, config2.domain || this.#domain);
|
264
|
+
if (config2.organization) this.#organization = config2.organization;
|
265
|
+
if (config2.domain) this.#domain = config2.domain;
|
84
266
|
if (config2.accessToken) {
|
85
|
-
|
86
|
-
def.headers.Authorization = "Bearer " +
|
267
|
+
this.#accessToken = config2.accessToken;
|
268
|
+
def.headers.Authorization = "Bearer " + this.#accessToken;
|
87
269
|
}
|
88
270
|
if (config2.headers) def.headers = { ...def.headers, ...this.customHeaders(config2.headers) };
|
89
271
|
return this;
|
@@ -93,21 +275,21 @@ const _ApiClient = class _ApiClient {
|
|
93
275
|
return this;
|
94
276
|
}
|
95
277
|
async request(method, path, body, options) {
|
96
|
-
|
97
|
-
if (options?.userAgent)
|
98
|
-
const baseUrl = options?.organization ? baseURL(options.organization, options.domain) :
|
278
|
+
debug3("request %s %s, %O, %O", method, path, body || {}, options || {});
|
279
|
+
if (options?.userAgent) debug3("User-Agent header ignored in request config");
|
280
|
+
const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : this.#baseUrl;
|
99
281
|
const url = new URL(`${baseUrl}/${path}`);
|
100
282
|
const bodyData = body ? JSON.stringify({ data: body }) : void 0;
|
101
283
|
const headers = { ...this.requestHeaders, ...this.customHeaders(options?.headers) };
|
102
|
-
const accessToken = options?.accessToken ||
|
284
|
+
const accessToken = options?.accessToken || this.#accessToken;
|
103
285
|
if (accessToken) headers.Authorization = "Bearer " + accessToken;
|
104
|
-
const refreshToken = options?.refreshToken ||
|
105
|
-
const fetchFunction = options?.fetch ||
|
286
|
+
const refreshToken = options?.refreshToken || this.#clientConfig.refreshToken;
|
287
|
+
const fetchFunction = options?.fetch || this.#clientConfig.fetch;
|
106
288
|
const requestOptions = { method, body: bodyData, headers };
|
107
|
-
const timeout = options?.timeout ||
|
289
|
+
const timeout = options?.timeout || this.#clientConfig.timeout;
|
108
290
|
if (timeout) {
|
109
291
|
if (AbortSignal?.timeout) requestOptions.signal = AbortSignal.timeout(timeout);
|
110
|
-
else
|
292
|
+
else debug3("Timeout not set. Undefined function: %s", "AbortSignal.timeout");
|
111
293
|
}
|
112
294
|
if (options?.params) Object.entries(options?.params).forEach(([name, value]) => {
|
113
295
|
url.searchParams.append(name, String(value));
|
@@ -117,22 +299,22 @@ const _ApiClient = class _ApiClient {
|
|
117
299
|
fetch: fetchFunction
|
118
300
|
};
|
119
301
|
try {
|
120
|
-
return await
|
302
|
+
return await fetchURL(url, requestOptions, clientOptions).catch((error) => handleError(error));
|
121
303
|
} catch (err) {
|
122
|
-
if (
|
123
|
-
|
124
|
-
const newAccessToken = await refreshToken(
|
125
|
-
|
126
|
-
const tokenError = new
|
304
|
+
if (isExpiredTokenError(err) && refreshToken && isTokenExpired(accessToken)) {
|
305
|
+
debug3("Access token has expired");
|
306
|
+
const newAccessToken = await refreshToken(this.#accessToken).catch((e) => {
|
307
|
+
debug3("Refresh token error: %s", e.message);
|
308
|
+
const tokenError = new SdkError({ message: "Error refreshing access token", type: "token-refresh" /* TOKEN_REFRESH */ });
|
127
309
|
tokenError.source = e;
|
128
310
|
throw tokenError;
|
129
311
|
});
|
130
312
|
if (newAccessToken) {
|
131
|
-
|
313
|
+
debug3("Access token refreshed");
|
132
314
|
this.config({ accessToken: newAccessToken });
|
133
|
-
|
315
|
+
this.#accessToken = newAccessToken;
|
134
316
|
if (requestOptions.headers) requestOptions.headers.Authorization = `Bearer ${newAccessToken}`;
|
135
|
-
const response = await
|
317
|
+
const response = await fetchURL(url, requestOptions, clientOptions).catch((error) => handleError(error));
|
136
318
|
return response;
|
137
319
|
}
|
138
320
|
} else throw err;
|
@@ -147,19 +329,12 @@ const _ApiClient = class _ApiClient {
|
|
147
329
|
return customHeaders;
|
148
330
|
}
|
149
331
|
get currentAccessToken() {
|
150
|
-
return
|
332
|
+
return this.#accessToken;
|
151
333
|
}
|
152
334
|
get currentOrganization() {
|
153
|
-
return
|
335
|
+
return this.#organization;
|
154
336
|
}
|
155
337
|
};
|
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;
|
163
338
|
var client_default = ApiClient;
|
164
339
|
|
165
340
|
module.exports = client_default;
|