@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.
Files changed (293) hide show
  1. package/lib/api.cjs +791 -7460
  2. package/lib/api.js +131 -7331
  3. package/lib/client.cjs +31 -215
  4. package/lib/client.js +23 -212
  5. package/lib/commercelayer.cjs +30 -778
  6. package/lib/commercelayer.js +22 -774
  7. package/lib/common.cjs +6 -143
  8. package/lib/common.js +4 -141
  9. package/lib/config.cjs +1 -2
  10. package/lib/config.js +1 -2
  11. package/lib/debug.cjs +5 -13
  12. package/lib/debug.js +5 -13
  13. package/lib/enum.cjs +8 -9
  14. package/lib/enum.js +8 -9
  15. package/lib/error.cjs +15 -67
  16. package/lib/error.js +14 -66
  17. package/lib/fetch.cjs +13 -72
  18. package/lib/fetch.js +11 -74
  19. package/lib/index.cjs +22 -8104
  20. package/lib/index.js +3 -7977
  21. package/lib/jsonapi.cjs +20 -188
  22. package/lib/jsonapi.js +13 -186
  23. package/lib/query.cjs +16 -75
  24. package/lib/query.js +14 -77
  25. package/lib/resource.cjs +57 -651
  26. package/lib/resource.js +37 -637
  27. package/lib/resources/addresses.cjs +11 -801
  28. package/lib/resources/addresses.js +11 -801
  29. package/lib/resources/adjustments.cjs +9 -799
  30. package/lib/resources/adjustments.js +9 -799
  31. package/lib/resources/adyen_gateways.cjs +9 -799
  32. package/lib/resources/adyen_gateways.js +9 -799
  33. package/lib/resources/adyen_payments.cjs +11 -801
  34. package/lib/resources/adyen_payments.js +11 -801
  35. package/lib/resources/applications.cjs +6 -788
  36. package/lib/resources/applications.js +6 -788
  37. package/lib/resources/attachments.cjs +9 -799
  38. package/lib/resources/attachments.js +9 -799
  39. package/lib/resources/authorizations.cjs +12 -802
  40. package/lib/resources/authorizations.js +12 -802
  41. package/lib/resources/avalara_accounts.cjs +9 -799
  42. package/lib/resources/avalara_accounts.js +9 -799
  43. package/lib/resources/axerve_gateways.cjs +9 -799
  44. package/lib/resources/axerve_gateways.js +9 -799
  45. package/lib/resources/axerve_payments.cjs +10 -800
  46. package/lib/resources/axerve_payments.js +10 -800
  47. package/lib/resources/bing_geocoders.cjs +9 -799
  48. package/lib/resources/bing_geocoders.js +9 -799
  49. package/lib/resources/braintree_gateways.cjs +9 -799
  50. package/lib/resources/braintree_gateways.js +9 -799
  51. package/lib/resources/braintree_payments.cjs +9 -799
  52. package/lib/resources/braintree_payments.js +9 -799
  53. package/lib/resources/bundles.cjs +13 -803
  54. package/lib/resources/bundles.js +13 -803
  55. package/lib/resources/buy_x_pay_y_promotions.cjs +13 -803
  56. package/lib/resources/buy_x_pay_y_promotions.js +13 -803
  57. package/lib/resources/captures.cjs +11 -801
  58. package/lib/resources/captures.js +11 -801
  59. package/lib/resources/carrier_accounts.cjs +9 -799
  60. package/lib/resources/carrier_accounts.js +9 -799
  61. package/lib/resources/checkout_com_gateways.cjs +9 -799
  62. package/lib/resources/checkout_com_gateways.js +9 -799
  63. package/lib/resources/checkout_com_payments.cjs +11 -801
  64. package/lib/resources/checkout_com_payments.js +11 -801
  65. package/lib/resources/cleanups.cjs +10 -800
  66. package/lib/resources/cleanups.js +10 -800
  67. package/lib/resources/coupon_codes_promotion_rules.cjs +9 -799
  68. package/lib/resources/coupon_codes_promotion_rules.js +9 -799
  69. package/lib/resources/coupon_recipients.cjs +9 -799
  70. package/lib/resources/coupon_recipients.js +9 -799
  71. package/lib/resources/coupons.cjs +11 -801
  72. package/lib/resources/coupons.js +11 -801
  73. package/lib/resources/custom_promotion_rules.cjs +9 -799
  74. package/lib/resources/custom_promotion_rules.js +9 -799
  75. package/lib/resources/customer_addresses.cjs +9 -799
  76. package/lib/resources/customer_addresses.js +9 -799
  77. package/lib/resources/customer_groups.cjs +9 -799
  78. package/lib/resources/customer_groups.js +9 -799
  79. package/lib/resources/customer_password_resets.cjs +10 -800
  80. package/lib/resources/customer_password_resets.js +10 -800
  81. package/lib/resources/customer_payment_sources.cjs +9 -799
  82. package/lib/resources/customer_payment_sources.js +9 -799
  83. package/lib/resources/customer_subscriptions.cjs +9 -799
  84. package/lib/resources/customer_subscriptions.js +9 -799
  85. package/lib/resources/customers.cjs +11 -801
  86. package/lib/resources/customers.js +11 -801
  87. package/lib/resources/delivery_lead_times.cjs +9 -799
  88. package/lib/resources/delivery_lead_times.js +9 -799
  89. package/lib/resources/discount_engine_items.cjs +6 -796
  90. package/lib/resources/discount_engine_items.js +6 -796
  91. package/lib/resources/discount_engines.cjs +6 -796
  92. package/lib/resources/discount_engines.js +6 -796
  93. package/lib/resources/easypost_pickups.cjs +10 -800
  94. package/lib/resources/easypost_pickups.js +10 -800
  95. package/lib/resources/event_callbacks.cjs +6 -796
  96. package/lib/resources/event_callbacks.js +6 -796
  97. package/lib/resources/events.cjs +8 -798
  98. package/lib/resources/events.js +8 -798
  99. package/lib/resources/exports.cjs +10 -800
  100. package/lib/resources/exports.js +10 -800
  101. package/lib/resources/external_gateways.cjs +10 -800
  102. package/lib/resources/external_gateways.js +10 -800
  103. package/lib/resources/external_payments.cjs +9 -799
  104. package/lib/resources/external_payments.js +9 -799
  105. package/lib/resources/external_promotions.cjs +14 -804
  106. package/lib/resources/external_promotions.js +14 -804
  107. package/lib/resources/external_tax_calculators.cjs +10 -800
  108. package/lib/resources/external_tax_calculators.js +10 -800
  109. package/lib/resources/fixed_amount_promotions.cjs +13 -803
  110. package/lib/resources/fixed_amount_promotions.js +13 -803
  111. package/lib/resources/fixed_price_promotions.cjs +13 -803
  112. package/lib/resources/fixed_price_promotions.js +13 -803
  113. package/lib/resources/flex_promotions.cjs +13 -803
  114. package/lib/resources/flex_promotions.js +13 -803
  115. package/lib/resources/free_gift_promotions.cjs +13 -803
  116. package/lib/resources/free_gift_promotions.js +13 -803
  117. package/lib/resources/free_shipping_promotions.cjs +13 -803
  118. package/lib/resources/free_shipping_promotions.js +13 -803
  119. package/lib/resources/geocoders.cjs +6 -796
  120. package/lib/resources/geocoders.js +6 -796
  121. package/lib/resources/gift_card_recipients.cjs +9 -799
  122. package/lib/resources/gift_card_recipients.js +9 -799
  123. package/lib/resources/gift_cards.cjs +15 -805
  124. package/lib/resources/gift_cards.js +15 -805
  125. package/lib/resources/google_geocoders.cjs +9 -799
  126. package/lib/resources/google_geocoders.js +9 -799
  127. package/lib/resources/imports.cjs +10 -800
  128. package/lib/resources/imports.js +10 -800
  129. package/lib/resources/in_stock_subscriptions.cjs +11 -801
  130. package/lib/resources/in_stock_subscriptions.js +11 -801
  131. package/lib/resources/inventory_models.cjs +9 -799
  132. package/lib/resources/inventory_models.js +9 -799
  133. package/lib/resources/inventory_return_locations.cjs +9 -799
  134. package/lib/resources/inventory_return_locations.js +9 -799
  135. package/lib/resources/inventory_stock_locations.cjs +9 -799
  136. package/lib/resources/inventory_stock_locations.js +9 -799
  137. package/lib/resources/klarna_gateways.cjs +9 -799
  138. package/lib/resources/klarna_gateways.js +9 -799
  139. package/lib/resources/klarna_payments.cjs +10 -800
  140. package/lib/resources/klarna_payments.js +10 -800
  141. package/lib/resources/line_item_options.cjs +11 -801
  142. package/lib/resources/line_item_options.js +11 -801
  143. package/lib/resources/line_items.cjs +14 -804
  144. package/lib/resources/line_items.js +14 -804
  145. package/lib/resources/links.cjs +11 -801
  146. package/lib/resources/links.js +11 -801
  147. package/lib/resources/manual_gateways.cjs +9 -799
  148. package/lib/resources/manual_gateways.js +9 -799
  149. package/lib/resources/manual_tax_calculators.cjs +9 -799
  150. package/lib/resources/manual_tax_calculators.js +9 -799
  151. package/lib/resources/markets.cjs +11 -801
  152. package/lib/resources/markets.js +11 -801
  153. package/lib/resources/merchants.cjs +9 -799
  154. package/lib/resources/merchants.js +9 -799
  155. package/lib/resources/notifications.cjs +9 -799
  156. package/lib/resources/notifications.js +9 -799
  157. package/lib/resources/order_amount_promotion_rules.cjs +9 -799
  158. package/lib/resources/order_amount_promotion_rules.js +9 -799
  159. package/lib/resources/order_copies.cjs +9 -799
  160. package/lib/resources/order_copies.js +9 -799
  161. package/lib/resources/order_factories.cjs +6 -796
  162. package/lib/resources/order_factories.js +6 -796
  163. package/lib/resources/order_subscription_items.cjs +9 -799
  164. package/lib/resources/order_subscription_items.js +9 -799
  165. package/lib/resources/order_subscriptions.cjs +15 -805
  166. package/lib/resources/order_subscriptions.js +15 -805
  167. package/lib/resources/orders.cjs +42 -832
  168. package/lib/resources/orders.js +42 -832
  169. package/lib/resources/organizations.cjs +6 -788
  170. package/lib/resources/organizations.js +6 -788
  171. package/lib/resources/packages.cjs +9 -799
  172. package/lib/resources/packages.js +9 -799
  173. package/lib/resources/parcel_line_items.cjs +9 -799
  174. package/lib/resources/parcel_line_items.js +9 -799
  175. package/lib/resources/parcels.cjs +9 -799
  176. package/lib/resources/parcels.js +9 -799
  177. package/lib/resources/payment_gateways.cjs +6 -796
  178. package/lib/resources/payment_gateways.js +6 -796
  179. package/lib/resources/payment_methods.cjs +11 -801
  180. package/lib/resources/payment_methods.js +11 -801
  181. package/lib/resources/payment_options.cjs +9 -799
  182. package/lib/resources/payment_options.js +9 -799
  183. package/lib/resources/paypal_gateways.cjs +9 -799
  184. package/lib/resources/paypal_gateways.js +9 -799
  185. package/lib/resources/paypal_payments.cjs +9 -799
  186. package/lib/resources/paypal_payments.js +9 -799
  187. package/lib/resources/percentage_discount_promotions.cjs +13 -803
  188. package/lib/resources/percentage_discount_promotions.js +13 -803
  189. package/lib/resources/pickups.cjs +6 -796
  190. package/lib/resources/pickups.js +6 -796
  191. package/lib/resources/price_frequency_tiers.cjs +9 -799
  192. package/lib/resources/price_frequency_tiers.js +9 -799
  193. package/lib/resources/price_list_schedulers.cjs +11 -801
  194. package/lib/resources/price_list_schedulers.js +11 -801
  195. package/lib/resources/price_lists.cjs +9 -799
  196. package/lib/resources/price_lists.js +9 -799
  197. package/lib/resources/price_tiers.cjs +6 -796
  198. package/lib/resources/price_tiers.js +6 -796
  199. package/lib/resources/price_volume_tiers.cjs +9 -799
  200. package/lib/resources/price_volume_tiers.js +9 -799
  201. package/lib/resources/prices.cjs +9 -799
  202. package/lib/resources/prices.js +9 -799
  203. package/lib/resources/promotion_rules.cjs +6 -796
  204. package/lib/resources/promotion_rules.js +6 -796
  205. package/lib/resources/promotions.cjs +6 -796
  206. package/lib/resources/promotions.js +6 -796
  207. package/lib/resources/recurring_order_copies.cjs +9 -799
  208. package/lib/resources/recurring_order_copies.js +9 -799
  209. package/lib/resources/refunds.cjs +8 -798
  210. package/lib/resources/refunds.js +8 -798
  211. package/lib/resources/reserved_stocks.cjs +6 -796
  212. package/lib/resources/reserved_stocks.js +6 -796
  213. package/lib/resources/resource_errors.cjs +6 -796
  214. package/lib/resources/resource_errors.js +6 -796
  215. package/lib/resources/return_line_items.cjs +10 -800
  216. package/lib/resources/return_line_items.js +10 -800
  217. package/lib/resources/returns.cjs +22 -812
  218. package/lib/resources/returns.js +22 -812
  219. package/lib/resources/satispay_gateways.cjs +9 -799
  220. package/lib/resources/satispay_gateways.js +9 -799
  221. package/lib/resources/satispay_payments.cjs +10 -800
  222. package/lib/resources/satispay_payments.js +10 -800
  223. package/lib/resources/shipments.cjs +24 -814
  224. package/lib/resources/shipments.js +24 -814
  225. package/lib/resources/shipping_categories.cjs +9 -799
  226. package/lib/resources/shipping_categories.js +9 -799
  227. package/lib/resources/shipping_method_tiers.cjs +6 -796
  228. package/lib/resources/shipping_method_tiers.js +6 -796
  229. package/lib/resources/shipping_methods.cjs +12 -802
  230. package/lib/resources/shipping_methods.js +12 -802
  231. package/lib/resources/shipping_weight_tiers.cjs +9 -799
  232. package/lib/resources/shipping_weight_tiers.js +9 -799
  233. package/lib/resources/shipping_zones.cjs +9 -799
  234. package/lib/resources/shipping_zones.js +9 -799
  235. package/lib/resources/sku_list_items.cjs +9 -799
  236. package/lib/resources/sku_list_items.js +9 -799
  237. package/lib/resources/sku_list_promotion_rules.cjs +9 -799
  238. package/lib/resources/sku_list_promotion_rules.js +9 -799
  239. package/lib/resources/sku_lists.cjs +9 -799
  240. package/lib/resources/sku_lists.js +9 -799
  241. package/lib/resources/sku_options.cjs +11 -801
  242. package/lib/resources/sku_options.js +11 -801
  243. package/lib/resources/skus.cjs +11 -801
  244. package/lib/resources/skus.js +11 -801
  245. package/lib/resources/stock_items.cjs +10 -800
  246. package/lib/resources/stock_items.js +10 -800
  247. package/lib/resources/stock_line_items.cjs +12 -802
  248. package/lib/resources/stock_line_items.js +12 -802
  249. package/lib/resources/stock_locations.cjs +9 -799
  250. package/lib/resources/stock_locations.js +9 -799
  251. package/lib/resources/stock_reservations.cjs +10 -800
  252. package/lib/resources/stock_reservations.js +10 -800
  253. package/lib/resources/stock_transfers.cjs +15 -805
  254. package/lib/resources/stock_transfers.js +15 -805
  255. package/lib/resources/stores.cjs +9 -799
  256. package/lib/resources/stores.js +9 -799
  257. package/lib/resources/stripe_gateways.cjs +9 -799
  258. package/lib/resources/stripe_gateways.js +9 -799
  259. package/lib/resources/stripe_payments.cjs +11 -801
  260. package/lib/resources/stripe_payments.js +11 -801
  261. package/lib/resources/stripe_tax_accounts.cjs +9 -799
  262. package/lib/resources/stripe_tax_accounts.js +9 -799
  263. package/lib/resources/subscription_models.cjs +9 -799
  264. package/lib/resources/subscription_models.js +9 -799
  265. package/lib/resources/tags.cjs +9 -799
  266. package/lib/resources/tags.js +9 -799
  267. package/lib/resources/talon_one_accounts.cjs +9 -799
  268. package/lib/resources/talon_one_accounts.js +9 -799
  269. package/lib/resources/tax_calculators.cjs +6 -796
  270. package/lib/resources/tax_calculators.js +6 -796
  271. package/lib/resources/tax_categories.cjs +9 -799
  272. package/lib/resources/tax_categories.js +9 -799
  273. package/lib/resources/tax_rules.cjs +9 -799
  274. package/lib/resources/tax_rules.js +9 -799
  275. package/lib/resources/taxjar_accounts.cjs +9 -799
  276. package/lib/resources/taxjar_accounts.js +9 -799
  277. package/lib/resources/transactions.cjs +6 -796
  278. package/lib/resources/transactions.js +6 -796
  279. package/lib/resources/versions.cjs +6 -796
  280. package/lib/resources/versions.js +6 -796
  281. package/lib/resources/vertex_accounts.cjs +10 -800
  282. package/lib/resources/vertex_accounts.js +10 -800
  283. package/lib/resources/voids.cjs +8 -798
  284. package/lib/resources/voids.js +8 -798
  285. package/lib/resources/webhooks.cjs +12 -802
  286. package/lib/resources/webhooks.js +12 -802
  287. package/lib/resources/wire_transfers.cjs +9 -799
  288. package/lib/resources/wire_transfers.js +9 -799
  289. package/lib/static.cjs +19 -753
  290. package/lib/static.js +5 -739
  291. package/lib/util.cjs +4 -5
  292. package/lib/util.js +4 -5
  293. package/package.json +4 -4
package/lib/client.cjs CHANGED
@@ -1,214 +1,30 @@
1
1
  'use strict';
2
2
 
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
- });
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
- // 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;
9
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
24
10
 
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
- };
11
+ var config__default = /*#__PURE__*/_interopDefault(config);
12
+ var Debug__default = /*#__PURE__*/_interopDefault(Debug);
81
13
 
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
- }
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
- 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
- };
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 config_default.client.requiredAttributes)
210
- if (!options[attr]) throw new SdkError({ message: `Undefined '${attr}' parameter` });
211
- return new _ApiClient(options);
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
- debug3("new client instance %O", options);
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 || config_default.client.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
- debug3("fetch config: %O", fetchConfig);
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
- debug3("config %o", config2);
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
- debug3("request %s %s, %O, %O", method, path, body || {}, options || {});
279
- if (options?.userAgent) debug3("User-Agent header ignored in request config");
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 debug3("Timeout not set. Undefined function: %s", "AbortSignal.timeout");
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
- debug3("Access token has expired");
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
- debug3("Refresh token error: %s", e.message);
308
- const tokenError = new SdkError({ message: "Error refreshing access token", type: "token-refresh" /* TOKEN_REFRESH */ });
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
- debug3("Access token refreshed");
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
- 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
- });
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
- // src/debug.ts
9
- var debuggerFunction = (_pattern, ..._args) => {
7
+ const debug = Debug("client");
8
+ const baseURL = (organization, domain) => {
9
+ return `https://${organization.toLowerCase()}.${domain || config.default.domain}/api`;
10
10
  };
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 {
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 config_default.client.requiredAttributes)
18
+ for (const attr of config.client.requiredAttributes)
208
19
  if (!options[attr]) throw new SdkError({ message: `Undefined '${attr}' parameter` });
209
- return new _ApiClient(options);
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
- debug3("new client instance %O", options);
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 || config_default.client.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
- debug3("fetch config: %O", fetchConfig);
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
- debug3("config %o", config2);
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
- debug3("request %s %s, %O, %O", method, path, body || {}, options || {});
277
- if (options?.userAgent) debug3("User-Agent header ignored in request config");
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 debug3("Timeout not set. Undefined function: %s", "AbortSignal.timeout");
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
- debug3("Access token has expired");
114
+ debug("Access token has expired");
304
115
  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 */ });
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
- debug3("Access token refreshed");
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 };