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