@commercelayer/sdk 5.34.2 → 6.0.0-alfa.1

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 (146) hide show
  1. package/lib/cjs/client.js +5 -5
  2. package/lib/cjs/commercelayer.js +3 -3
  3. package/lib/cjs/common.js +1 -1
  4. package/lib/cjs/config.d.ts +0 -3
  5. package/lib/cjs/config.js +0 -3
  6. package/lib/cjs/error.js +1 -2
  7. package/lib/cjs/jsonapi.js +5 -10
  8. package/lib/cjs/query.d.ts +1 -1
  9. package/lib/cjs/query.js +1 -18
  10. package/lib/cjs/resource.d.ts +2 -5
  11. package/lib/cjs/resource.js +27 -37
  12. package/lib/esm/client.js +19 -30
  13. package/lib/esm/commercelayer.js +260 -149
  14. package/lib/esm/common.js +1 -1
  15. package/lib/esm/config.d.ts +0 -3
  16. package/lib/esm/config.js +0 -3
  17. package/lib/esm/error.js +10 -5
  18. package/lib/esm/jsonapi.js +5 -10
  19. package/lib/esm/query.d.ts +1 -1
  20. package/lib/esm/query.js +1 -18
  21. package/lib/esm/resource.d.ts +2 -5
  22. package/lib/esm/resource.js +42 -62
  23. package/lib/esm/resources/addresses.js +1 -1
  24. package/lib/esm/resources/adjustments.js +1 -1
  25. package/lib/esm/resources/adyen_gateways.js +1 -1
  26. package/lib/esm/resources/adyen_payments.js +1 -1
  27. package/lib/esm/resources/application.js +1 -1
  28. package/lib/esm/resources/attachments.js +1 -1
  29. package/lib/esm/resources/authorizations.js +1 -1
  30. package/lib/esm/resources/avalara_accounts.js +1 -1
  31. package/lib/esm/resources/axerve_gateways.js +1 -1
  32. package/lib/esm/resources/axerve_payments.js +1 -1
  33. package/lib/esm/resources/billing_info_validation_rules.js +1 -1
  34. package/lib/esm/resources/bing_geocoders.js +1 -1
  35. package/lib/esm/resources/braintree_gateways.js +1 -1
  36. package/lib/esm/resources/braintree_payments.js +1 -1
  37. package/lib/esm/resources/bundles.js +1 -1
  38. package/lib/esm/resources/buy_x_pay_y_promotions.js +1 -1
  39. package/lib/esm/resources/captures.js +1 -1
  40. package/lib/esm/resources/carrier_accounts.js +1 -1
  41. package/lib/esm/resources/checkout_com_gateways.js +1 -1
  42. package/lib/esm/resources/checkout_com_payments.js +1 -1
  43. package/lib/esm/resources/cleanups.js +1 -1
  44. package/lib/esm/resources/coupon_codes_promotion_rules.js +1 -1
  45. package/lib/esm/resources/coupon_recipients.js +1 -1
  46. package/lib/esm/resources/coupons.js +1 -1
  47. package/lib/esm/resources/custom_promotion_rules.js +1 -1
  48. package/lib/esm/resources/customer_addresses.js +1 -1
  49. package/lib/esm/resources/customer_groups.js +1 -1
  50. package/lib/esm/resources/customer_password_resets.js +1 -1
  51. package/lib/esm/resources/customer_payment_sources.js +1 -1
  52. package/lib/esm/resources/customer_subscriptions.js +1 -1
  53. package/lib/esm/resources/customers.js +1 -1
  54. package/lib/esm/resources/delivery_lead_times.js +1 -1
  55. package/lib/esm/resources/event_callbacks.js +1 -1
  56. package/lib/esm/resources/events.js +1 -1
  57. package/lib/esm/resources/exports.js +1 -1
  58. package/lib/esm/resources/external_gateways.js +1 -1
  59. package/lib/esm/resources/external_payments.js +1 -1
  60. package/lib/esm/resources/external_promotions.js +1 -1
  61. package/lib/esm/resources/external_tax_calculators.js +1 -1
  62. package/lib/esm/resources/fixed_amount_promotions.js +1 -1
  63. package/lib/esm/resources/fixed_price_promotions.js +1 -1
  64. package/lib/esm/resources/free_gift_promotions.js +1 -1
  65. package/lib/esm/resources/free_shipping_promotions.js +1 -1
  66. package/lib/esm/resources/geocoders.js +1 -1
  67. package/lib/esm/resources/gift_card_recipients.js +1 -1
  68. package/lib/esm/resources/gift_cards.js +1 -1
  69. package/lib/esm/resources/google_geocoders.js +1 -1
  70. package/lib/esm/resources/imports.js +1 -1
  71. package/lib/esm/resources/in_stock_subscriptions.js +1 -1
  72. package/lib/esm/resources/inventory_models.js +1 -1
  73. package/lib/esm/resources/inventory_return_locations.js +1 -1
  74. package/lib/esm/resources/inventory_stock_locations.js +1 -1
  75. package/lib/esm/resources/klarna_gateways.js +1 -1
  76. package/lib/esm/resources/klarna_payments.js +1 -1
  77. package/lib/esm/resources/line_item_options.js +1 -1
  78. package/lib/esm/resources/line_items.js +1 -1
  79. package/lib/esm/resources/manual_gateways.js +1 -1
  80. package/lib/esm/resources/manual_tax_calculators.js +1 -1
  81. package/lib/esm/resources/markets.js +1 -1
  82. package/lib/esm/resources/merchants.js +1 -1
  83. package/lib/esm/resources/order_amount_promotion_rules.js +1 -1
  84. package/lib/esm/resources/order_copies.js +1 -1
  85. package/lib/esm/resources/order_factories.js +1 -1
  86. package/lib/esm/resources/order_subscription_items.js +1 -1
  87. package/lib/esm/resources/order_subscriptions.js +1 -1
  88. package/lib/esm/resources/order_validation_rules.js +1 -1
  89. package/lib/esm/resources/orders.js +1 -1
  90. package/lib/esm/resources/organization.js +1 -1
  91. package/lib/esm/resources/packages.js +1 -1
  92. package/lib/esm/resources/parcel_line_items.js +1 -1
  93. package/lib/esm/resources/parcels.js +1 -1
  94. package/lib/esm/resources/payment_gateways.js +1 -1
  95. package/lib/esm/resources/payment_methods.js +1 -1
  96. package/lib/esm/resources/payment_options.js +1 -1
  97. package/lib/esm/resources/paypal_gateways.js +1 -1
  98. package/lib/esm/resources/paypal_payments.js +1 -1
  99. package/lib/esm/resources/percentage_discount_promotions.js +1 -1
  100. package/lib/esm/resources/price_frequency_tiers.js +1 -1
  101. package/lib/esm/resources/price_lists.js +1 -1
  102. package/lib/esm/resources/price_tiers.js +1 -1
  103. package/lib/esm/resources/price_volume_tiers.js +1 -1
  104. package/lib/esm/resources/prices.js +1 -1
  105. package/lib/esm/resources/promotion_rules.js +1 -1
  106. package/lib/esm/resources/promotions.js +1 -1
  107. package/lib/esm/resources/recurring_order_copies.js +1 -1
  108. package/lib/esm/resources/refunds.js +1 -1
  109. package/lib/esm/resources/reserved_stocks.js +1 -1
  110. package/lib/esm/resources/resource_errors.js +1 -1
  111. package/lib/esm/resources/return_line_items.js +1 -1
  112. package/lib/esm/resources/returns.js +1 -1
  113. package/lib/esm/resources/satispay_gateways.js +1 -1
  114. package/lib/esm/resources/satispay_payments.js +1 -1
  115. package/lib/esm/resources/shipments.js +1 -1
  116. package/lib/esm/resources/shipping_categories.js +1 -1
  117. package/lib/esm/resources/shipping_method_tiers.js +1 -1
  118. package/lib/esm/resources/shipping_methods.js +1 -1
  119. package/lib/esm/resources/shipping_weight_tiers.js +1 -1
  120. package/lib/esm/resources/shipping_zones.js +1 -1
  121. package/lib/esm/resources/sku_list_items.js +1 -1
  122. package/lib/esm/resources/sku_list_promotion_rules.js +1 -1
  123. package/lib/esm/resources/sku_lists.js +1 -1
  124. package/lib/esm/resources/sku_options.js +1 -1
  125. package/lib/esm/resources/skus.js +1 -1
  126. package/lib/esm/resources/stock_items.js +1 -1
  127. package/lib/esm/resources/stock_line_items.js +1 -1
  128. package/lib/esm/resources/stock_locations.js +1 -1
  129. package/lib/esm/resources/stock_reservations.js +1 -1
  130. package/lib/esm/resources/stock_transfers.js +1 -1
  131. package/lib/esm/resources/stripe_gateways.js +1 -1
  132. package/lib/esm/resources/stripe_payments.js +1 -1
  133. package/lib/esm/resources/subscription_models.js +1 -1
  134. package/lib/esm/resources/tags.js +1 -1
  135. package/lib/esm/resources/tax_calculators.js +1 -1
  136. package/lib/esm/resources/tax_categories.js +1 -1
  137. package/lib/esm/resources/tax_rules.js +1 -1
  138. package/lib/esm/resources/taxjar_accounts.js +1 -1
  139. package/lib/esm/resources/transactions.js +1 -1
  140. package/lib/esm/resources/versions.js +1 -1
  141. package/lib/esm/resources/voids.js +1 -1
  142. package/lib/esm/resources/webhooks.js +1 -1
  143. package/lib/esm/resources/wire_transfers.js +1 -1
  144. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  145. package/lib/tsconfig.tsbuildinfo +1 -1
  146. package/package.json +8 -8
package/lib/cjs/client.js CHANGED
@@ -120,16 +120,16 @@ class ApiClient {
120
120
  async request(method, path, body, options) {
121
121
  debug('request %s %s, %O, %O', method, path, body || {}, options || {});
122
122
  // Ignored params alerts (in debug mode)
123
- if (options === null || options === void 0 ? void 0 : options.adapter)
123
+ if (options?.adapter)
124
124
  debug('Adapter ignored in request config');
125
- if (options === null || options === void 0 ? void 0 : options.userAgent)
125
+ if (options?.userAgent)
126
126
  debug('User-Agent header ignored in request config');
127
127
  const data = body ? { data: body } : undefined;
128
128
  const url = path;
129
129
  // Runtime request parameters
130
- const baseUrl = (options === null || options === void 0 ? void 0 : options.organization) ? baseURL(options.organization, options.domain) : undefined;
131
- const accessToken = (options === null || options === void 0 ? void 0 : options.accessToken) || __classPrivateFieldGet(this, _ApiClient_accessToken, "f");
132
- const headers = this.customHeaders(options === null || options === void 0 ? void 0 : options.headers);
130
+ const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : undefined;
131
+ const accessToken = options?.accessToken || __classPrivateFieldGet(this, _ApiClient_accessToken, "f");
132
+ const headers = this.customHeaders(options?.headers);
133
133
  if (accessToken)
134
134
  headers.Authorization = 'Bearer ' + accessToken;
135
135
  const requestParams = { method, baseURL: baseUrl, url, data, ...options, headers };
@@ -230,8 +230,8 @@ class CommerceLayerClient {
230
230
  headers: undefined,
231
231
  };
232
232
  function rawResponseInterceptor(response) {
233
- reader.rawResponse = response === null || response === void 0 ? void 0 : response.data;
234
- if (options === null || options === void 0 ? void 0 : options.headers)
233
+ reader.rawResponse = response?.data;
234
+ if (options?.headers)
235
235
  reader.headers = response.headers;
236
236
  return response;
237
237
  }
@@ -240,7 +240,7 @@ class CommerceLayerClient {
240
240
  return reader;
241
241
  }
242
242
  removeRawResponseReader(reader) {
243
- const id = (typeof reader === 'number') ? reader : reader === null || reader === void 0 ? void 0 : reader.id;
243
+ const id = (typeof reader === 'number') ? reader : reader?.id;
244
244
  if (id && (id >= 0))
245
245
  this.removeInterceptor('response', id);
246
246
  }
package/lib/cjs/common.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isResourceType = exports.isResourceId = void 0;
4
4
  const api_1 = require("./api");
5
5
  const isResourceId = (resource) => {
6
- return ((resource === null || resource === void 0 ? void 0 : resource.type) && resource.id) && api_1.resourceList.includes(resource.type);
6
+ return (resource?.type && resource.id) && api_1.resourceList.includes(resource.type);
7
7
  };
8
8
  exports.isResourceId = isResourceId;
9
9
  const isResourceType = (resource) => {
@@ -8,8 +8,5 @@ declare const config: {
8
8
  readonly timeout: 15000;
9
9
  readonly requiredAttributes: readonly ["organization", "accessToken"];
10
10
  };
11
- readonly jsonapi: {
12
- readonly maxResourceIncluded: 2;
13
- };
14
11
  };
15
12
  export default config;
package/lib/cjs/config.js CHANGED
@@ -11,9 +11,6 @@ const config = {
11
11
  client: {
12
12
  timeout: 15000,
13
13
  requiredAttributes: ['organization', 'accessToken'],
14
- },
15
- jsonapi: {
16
- maxResourceIncluded: 2
17
14
  }
18
15
  };
19
16
  exports.default = config;
package/lib/cjs/error.js CHANGED
@@ -36,8 +36,7 @@ class ApiError extends SdkError {
36
36
  this.name = ApiError.NAME; // this.constructor.name
37
37
  }
38
38
  first() {
39
- var _a;
40
- return (((_a = this.errors) === null || _a === void 0 ? void 0 : _a.length) > 0) ? this.errors[0] : undefined;
39
+ return (this.errors?.length > 0) ? this.errors[0] : undefined;
41
40
  }
42
41
  }
43
42
  exports.ApiError = ApiError;
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.normalize = exports.denormalize = void 0;
7
7
  const common_1 = require("./common");
8
- const config_1 = __importDefault(require("./config"));
9
8
  const debug_1 = __importDefault(require("./debug"));
10
9
  const debug = (0, debug_1.default)('jsonapi');
11
10
  // DENORMALIZATION
@@ -32,7 +31,7 @@ const findIncluded = (rel, included = []) => {
32
31
  });
33
32
  return inc || rel;
34
33
  };
35
- const denormalizeResource = (res, included, chain = []) => {
34
+ const denormalizeResource = (res, included) => {
36
35
  debug('denormalize resource: %O, %o', res, included || {});
37
36
  if (!res)
38
37
  return res;
@@ -45,14 +44,10 @@ const denormalizeResource = (res, included, chain = []) => {
45
44
  Object.keys(res.relationships).forEach(key => {
46
45
  const rel = res.relationships[key].data;
47
46
  if (rel) {
48
- if (chain.filter(r => (r.id === rel.id) && (r.type === rel.type)).length >= config_1.default.jsonapi.maxResourceIncluded)
49
- resource[key] = rel;
50
- else {
51
- if (Array.isArray(rel))
52
- resource[key] = rel.map((r) => denormalizeResource(findIncluded(r, included), included, [...chain, r]));
53
- else
54
- resource[key] = denormalizeResource(findIncluded(rel, included), included, [...chain, rel]);
55
- }
47
+ if (Array.isArray(rel))
48
+ resource[key] = rel.map((r) => denormalizeResource(findIncluded(r, included), included));
49
+ else
50
+ resource[key] = denormalizeResource(findIncluded(rel, included), included);
56
51
  }
57
52
  else if (rel === null)
58
53
  resource[key] = null;
@@ -1,5 +1,5 @@
1
1
  import type { ResourceType } from "./resource";
2
- type QueryFilter = Record<string, string | number | boolean | object | Array<string | number>>;
2
+ type QueryFilter = Record<string, string | number | boolean | object>;
3
3
  interface QueryParamsRetrieve {
4
4
  include?: string[];
5
5
  fields?: string[] | Record<string, string[]>;
package/lib/cjs/query.js CHANGED
@@ -5,10 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.isParamsList = exports.generateQueryStringParams = void 0;
7
7
  const debug_1 = __importDefault(require("./debug"));
8
- const error_1 = require("./error");
9
8
  const debug = (0, debug_1.default)('query');
10
- const arrayFilters = ['_any', '_all', '_in'];
11
- const objectFilters = ['_jcont'];
12
9
  const isParamsList = (params) => {
13
10
  return params && (params.filters || params.pageNumber || params.pageSize || params.sort);
14
11
  };
@@ -45,21 +42,7 @@ const generateQueryStringParams = (params, res) => {
45
42
  // Filters
46
43
  if (params.filters) {
47
44
  Object.entries(params.filters).forEach(([p, v]) => {
48
- const filter = p.substring(p.lastIndexOf('_'));
49
- let val;
50
- if (Array.isArray(v)) {
51
- if (!arrayFilters.includes(filter))
52
- throw new error_1.SdkError({ message: `Wrong ${filter} filter: Array value is supported only for the following filters: ${arrayFilters.join(', ')}`, type: error_1.ErrorType.REQUEST });
53
- val = v.join(',');
54
- }
55
- else if (typeof v === 'object') {
56
- if (!objectFilters.includes(filter))
57
- throw new error_1.SdkError({ message: `Wrong ${filter} filter: Object value is supported only for the following filters: ${objectFilters.join(', ')}`, type: error_1.ErrorType.REQUEST });
58
- val = JSON.stringify(v);
59
- }
60
- else
61
- val = String(v);
62
- qp[`filter[q][${p}]`] = val;
45
+ qp[`filter[q][${p}]`] = (typeof v === 'object') ? JSON.stringify(v) : String(v);
63
46
  });
64
47
  }
65
48
  }
@@ -1,4 +1,4 @@
1
- import ApiClient, { type ApiClientInitConfig } from './client';
1
+ import { type ApiClientInitConfig } from './client';
2
2
  import type { QueryParamsRetrieve, QueryParamsList, QueryFilter, QueryParams } from './query';
3
3
  import type { ResourceTypeLock } from './api';
4
4
  import type { InterceptorManager } from './interceptor';
@@ -56,7 +56,6 @@ declare class ResourceAdapter {
56
56
  get interceptors(): InterceptorManager;
57
57
  private localConfig;
58
58
  config(config: ResourcesConfig): ResourceAdapter;
59
- get client(): Readonly<ApiClient>;
60
59
  singleton<R extends Resource>(resource: ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
61
60
  retrieve<R extends Resource>(resource: ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
62
61
  list<R extends Resource>(resource: ResourceType, params?: QueryParamsList, options?: ResourcesConfig): Promise<ListResponse<R>>;
@@ -71,9 +70,7 @@ declare abstract class ApiResourceBase<R extends Resource> {
71
70
  constructor(adapter: ResourceAdapter);
72
71
  abstract relationship(id: string | ResourceId | null): ResourceRel;
73
72
  abstract type(): ResourceTypeLock;
74
- parse(resource: string, options?: {
75
- ignoreSlug?: boolean;
76
- }): R | R[];
73
+ parse(resource: string): R | R[];
77
74
  update(resource: ResourceUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
78
75
  }
79
76
  declare abstract class ApiResource<R extends Resource> extends ApiResourceBase<R> {
@@ -57,14 +57,16 @@ class ResourceAdapter {
57
57
  __classPrivateFieldGet(this, _ResourceAdapter_client, "f").config(config);
58
58
  return this;
59
59
  }
60
- get client() {
61
- return __classPrivateFieldGet(this, _ResourceAdapter_client, "f");
60
+ /*
61
+ get clientInstance(): ApiClient {
62
+ return this.#client
62
63
  }
64
+ */
63
65
  async singleton(resource, params, options) {
64
66
  debug('singleton: %o, %O, %O', resource, params || {}, options || {});
65
67
  const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
66
- if (options === null || options === void 0 ? void 0 : options.params)
67
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
68
+ if (options?.params)
69
+ Object.assign(queryParams, options?.params);
68
70
  const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}`, undefined, { ...options, params: queryParams });
69
71
  const r = (0, jsonapi_1.denormalize)(res);
70
72
  return r;
@@ -72,33 +74,32 @@ class ResourceAdapter {
72
74
  async retrieve(resource, params, options) {
73
75
  debug('retrieve: %o, %O, %O', resource, params || {}, options || {});
74
76
  const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
75
- if (options === null || options === void 0 ? void 0 : options.params)
76
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
77
+ if (options?.params)
78
+ Object.assign(queryParams, options?.params);
77
79
  const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}/${resource.id}`, undefined, { ...options, params: queryParams });
78
80
  const r = (0, jsonapi_1.denormalize)(res);
79
81
  return r;
80
82
  }
81
83
  async list(resource, params, options) {
82
- var _a, _b;
83
84
  debug('list: %o, %O, %O', resource, params || {}, options || {});
84
85
  const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
85
- if (options === null || options === void 0 ? void 0 : options.params)
86
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
86
+ if (options?.params)
87
+ Object.assign(queryParams, options?.params);
87
88
  const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}`, undefined, { ...options, params: queryParams });
88
89
  const r = (0, jsonapi_1.denormalize)(res);
89
90
  const meta = {
90
- pageCount: Number((_a = res.meta) === null || _a === void 0 ? void 0 : _a.page_count),
91
- recordCount: Number((_b = res.meta) === null || _b === void 0 ? void 0 : _b.record_count),
92
- currentPage: (params === null || params === void 0 ? void 0 : params.pageNumber) || config_1.default.default.pageNumber,
93
- recordsPerPage: (params === null || params === void 0 ? void 0 : params.pageSize) || config_1.default.default.pageSize
91
+ pageCount: Number(res.meta?.page_count),
92
+ recordCount: Number(res.meta?.record_count),
93
+ currentPage: params?.pageNumber || config_1.default.default.pageNumber,
94
+ recordsPerPage: params?.pageSize || config_1.default.default.pageSize
94
95
  };
95
96
  return new ListResponse(meta, r);
96
97
  }
97
98
  async create(resource, params, options) {
98
99
  debug('create: %o, %O, %O', resource, params || {}, options || {});
99
100
  const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
100
- if (options === null || options === void 0 ? void 0 : options.params)
101
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
101
+ if (options?.params)
102
+ Object.assign(queryParams, options?.params);
102
103
  const data = (0, jsonapi_1.normalize)(resource);
103
104
  const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('post', resource.type, data, { ...options, params: queryParams });
104
105
  const r = (0, jsonapi_1.denormalize)(res);
@@ -107,8 +108,8 @@ class ResourceAdapter {
107
108
  async update(resource, params, options) {
108
109
  debug('update: %o, %O, %O', resource, params || {}, options || {});
109
110
  const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
110
- if (options === null || options === void 0 ? void 0 : options.params)
111
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
111
+ if (options?.params)
112
+ Object.assign(queryParams, options?.params);
112
113
  const data = (0, jsonapi_1.normalize)(resource);
113
114
  const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('patch', `${resource.type}/${resource.id}`, data, { ...options, params: queryParams });
114
115
  const r = (0, jsonapi_1.denormalize)(res);
@@ -119,20 +120,19 @@ class ResourceAdapter {
119
120
  await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('delete', `${resource.type}/${resource.id}`, undefined, options);
120
121
  }
121
122
  async fetch(resource, path, params, options) {
122
- var _a, _b;
123
123
  debug('fetch: %o, %O, %O', path, params || {}, options || {});
124
124
  const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
125
- if (options === null || options === void 0 ? void 0 : options.params)
126
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
125
+ if (options?.params)
126
+ Object.assign(queryParams, options?.params);
127
127
  const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', path, undefined, { ...options, params: queryParams });
128
128
  const r = (0, jsonapi_1.denormalize)(res);
129
129
  if (Array.isArray(r)) {
130
130
  const p = params;
131
131
  const meta = {
132
- pageCount: Number((_a = res.meta) === null || _a === void 0 ? void 0 : _a.page_count),
133
- recordCount: Number((_b = res.meta) === null || _b === void 0 ? void 0 : _b.record_count),
134
- currentPage: (p === null || p === void 0 ? void 0 : p.pageNumber) || config_1.default.default.pageNumber,
135
- recordsPerPage: (p === null || p === void 0 ? void 0 : p.pageSize) || config_1.default.default.pageSize
132
+ pageCount: Number(res.meta?.page_count),
133
+ recordCount: Number(res.meta?.record_count),
134
+ currentPage: p?.pageNumber || config_1.default.default.pageNumber,
135
+ recordsPerPage: p?.pageSize || config_1.default.default.pageSize
136
136
  };
137
137
  return new ListResponse(meta, r);
138
138
  }
@@ -146,21 +146,11 @@ class ApiResourceBase {
146
146
  debug('new resource instance: %s', this.type());
147
147
  this.resources = adapter;
148
148
  }
149
- parse(resource, options) {
150
- var _a;
149
+ parse(resource) {
151
150
  try {
152
151
  const res = JSON.parse(resource);
153
- // Resource type always checked
154
- const rtype = (_a = res.data) === null || _a === void 0 ? void 0 : _a.type;
155
- if (rtype !== this.type())
156
- throw new error_1.SdkError({ message: `Invalid resource type [${rtype}]`, type: error_1.ErrorType.PARSE });
157
- // Parse options
158
- const { ignoreSlug } = options || {};
159
- if (!ignoreSlug) {
160
- const links = res.data.links.self;
161
- if (!links || !String(links).match(`^${this.resources.client.baseUrl}/${this.type()}/*`))
162
- throw new error_1.SdkError({ message: `Resource contains invalid links [${links}]`, type: error_1.ErrorType.PARSE });
163
- }
152
+ if (res.data?.type !== this.type())
153
+ throw new error_1.SdkError({ message: `Invalid resource type [${res.data?.type}]`, type: error_1.ErrorType.PARSE });
164
154
  return (0, jsonapi_1.denormalize)(res);
165
155
  }
166
156
  catch (error) {
package/lib/esm/client.js CHANGED
@@ -1,15 +1,3 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _ApiClient_accessToken, _ApiClient_client;
13
1
  import axios from 'axios';
14
2
  import { SdkError, handleError } from './error';
15
3
  import config from './config';
@@ -28,12 +16,14 @@ class ApiClient {
28
16
  throw new SdkError({ message: `Undefined '${attr}' parameter` });
29
17
  return new ApiClient(options);
30
18
  }
19
+ baseUrl;
20
+ #accessToken;
21
+ #client;
22
+ interceptors;
31
23
  constructor(options) {
32
- _ApiClient_accessToken.set(this, void 0);
33
- _ApiClient_client.set(this, void 0);
34
24
  debug('new client instance %O', options);
35
25
  this.baseUrl = baseURL(options.organization, options.domain);
36
- __classPrivateFieldSet(this, _ApiClient_accessToken, options.accessToken, "f");
26
+ this.#accessToken = options.accessToken;
37
27
  const axiosConfig = {
38
28
  timeout: options.timeout || config.client.timeout,
39
29
  proxy: options.proxy,
@@ -47,7 +37,7 @@ class ApiClient {
47
37
  ...customHeaders,
48
38
  'Accept': 'application/vnd.api+json',
49
39
  'Content-Type': 'application/vnd.api+json',
50
- 'Authorization': 'Bearer ' + __classPrivateFieldGet(this, _ApiClient_accessToken, "f")
40
+ 'Authorization': 'Bearer ' + this.#accessToken
51
41
  };
52
42
  // Set User-Agent
53
43
  let userAgent = options.userAgent; // || `SDK-core axios/${axios.VERSION}`
@@ -65,12 +55,12 @@ class ApiClient {
65
55
  if (options.adapter)
66
56
  axiosOptions.adapter = options.adapter;
67
57
  debug('axios options: %O', axiosOptions);
68
- __classPrivateFieldSet(this, _ApiClient_client, axios.create(axiosOptions), "f");
69
- this.interceptors = __classPrivateFieldGet(this, _ApiClient_client, "f").interceptors;
58
+ this.#client = axios.create(axiosOptions);
59
+ this.interceptors = this.#client.interceptors;
70
60
  }
71
61
  config(config) {
72
62
  debug('config %o', config);
73
- const def = __classPrivateFieldGet(this, _ApiClient_client, "f").defaults;
63
+ const def = this.#client.defaults;
74
64
  // Axios config
75
65
  if (config.timeout)
76
66
  def.timeout = config.timeout;
@@ -88,8 +78,8 @@ class ApiClient {
88
78
  if (config.organization)
89
79
  this.baseUrl = baseURL(config.organization, config.domain);
90
80
  if (config.accessToken) {
91
- __classPrivateFieldSet(this, _ApiClient_accessToken, config.accessToken, "f");
92
- def.headers.common.Authorization = 'Bearer ' + __classPrivateFieldGet(this, _ApiClient_accessToken, "f");
81
+ this.#accessToken = config.accessToken;
82
+ def.headers.common.Authorization = 'Bearer ' + this.#accessToken;
93
83
  }
94
84
  if (config.headers)
95
85
  def.headers.common = this.customHeaders(config.headers);
@@ -103,34 +93,34 @@ class ApiClient {
103
93
  if (axios.VERSION)
104
94
  ua += ` axios/${axios.VERSION}`;
105
95
  }
106
- __classPrivateFieldGet(this, _ApiClient_client, "f").defaults.headers['User-Agent'] = ua;
96
+ this.#client.defaults.headers['User-Agent'] = ua;
107
97
  }
108
98
  return this;
109
99
  }
110
100
  adapter(adapter) {
111
101
  if (adapter)
112
- __classPrivateFieldGet(this, _ApiClient_client, "f").defaults.adapter = adapter;
102
+ this.#client.defaults.adapter = adapter;
113
103
  return this;
114
104
  }
115
105
  async request(method, path, body, options) {
116
106
  debug('request %s %s, %O, %O', method, path, body || {}, options || {});
117
107
  // Ignored params alerts (in debug mode)
118
- if (options === null || options === void 0 ? void 0 : options.adapter)
108
+ if (options?.adapter)
119
109
  debug('Adapter ignored in request config');
120
- if (options === null || options === void 0 ? void 0 : options.userAgent)
110
+ if (options?.userAgent)
121
111
  debug('User-Agent header ignored in request config');
122
112
  const data = body ? { data: body } : undefined;
123
113
  const url = path;
124
114
  // Runtime request parameters
125
- const baseUrl = (options === null || options === void 0 ? void 0 : options.organization) ? baseURL(options.organization, options.domain) : undefined;
126
- const accessToken = (options === null || options === void 0 ? void 0 : options.accessToken) || __classPrivateFieldGet(this, _ApiClient_accessToken, "f");
127
- const headers = this.customHeaders(options === null || options === void 0 ? void 0 : options.headers);
115
+ const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : undefined;
116
+ const accessToken = options?.accessToken || this.#accessToken;
117
+ const headers = this.customHeaders(options?.headers);
128
118
  if (accessToken)
129
119
  headers.Authorization = 'Bearer ' + accessToken;
130
120
  const requestParams = { method, baseURL: baseUrl, url, data, ...options, headers };
131
121
  debug('request params: %O', requestParams);
132
122
  // const start = Date.now()
133
- return __classPrivateFieldGet(this, _ApiClient_client, "f").request(requestParams)
123
+ return this.#client.request(requestParams)
134
124
  .then(response => response.data)
135
125
  .catch((error) => handleError(error));
136
126
  // .finally(() => console.log(`<<-- ${method} ${path} ${Date.now() - start}`))
@@ -145,5 +135,4 @@ class ApiClient {
145
135
  return customHeaders;
146
136
  }
147
137
  }
148
- _ApiClient_accessToken = new WeakMap(), _ApiClient_client = new WeakMap();
149
138
  export default ApiClient;