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