@commercelayer/sdk 5.35.0 → 6.0.0-alfa.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/client.js +5 -5
- package/lib/cjs/commercelayer.d.ts +2 -2
- package/lib/cjs/commercelayer.js +3 -3
- package/lib/cjs/common.js +1 -1
- package/lib/cjs/error.js +1 -2
- package/lib/cjs/interceptor.d.ts +2 -2
- package/lib/cjs/resource.js +21 -24
- package/lib/esm/client.js +19 -30
- package/lib/esm/commercelayer.d.ts +2 -2
- package/lib/esm/commercelayer.js +260 -149
- package/lib/esm/common.js +1 -1
- package/lib/esm/error.js +10 -5
- package/lib/esm/interceptor.d.ts +2 -2
- package/lib/esm/resource.js +37 -50
- package/lib/esm/resources/addresses.js +1 -1
- package/lib/esm/resources/adjustments.js +1 -1
- package/lib/esm/resources/adyen_gateways.js +1 -1
- package/lib/esm/resources/adyen_payments.js +1 -1
- package/lib/esm/resources/application.js +1 -1
- package/lib/esm/resources/attachments.js +1 -1
- package/lib/esm/resources/authorizations.js +1 -1
- package/lib/esm/resources/avalara_accounts.js +1 -1
- package/lib/esm/resources/axerve_gateways.js +1 -1
- package/lib/esm/resources/axerve_payments.js +1 -1
- package/lib/esm/resources/billing_info_validation_rules.js +1 -1
- package/lib/esm/resources/bing_geocoders.js +1 -1
- package/lib/esm/resources/braintree_gateways.js +1 -1
- package/lib/esm/resources/braintree_payments.js +1 -1
- package/lib/esm/resources/bundles.js +1 -1
- package/lib/esm/resources/buy_x_pay_y_promotions.js +1 -1
- package/lib/esm/resources/captures.js +1 -1
- package/lib/esm/resources/carrier_accounts.js +1 -1
- package/lib/esm/resources/checkout_com_gateways.js +1 -1
- package/lib/esm/resources/checkout_com_payments.js +1 -1
- package/lib/esm/resources/cleanups.js +1 -1
- package/lib/esm/resources/coupon_codes_promotion_rules.js +1 -1
- package/lib/esm/resources/coupon_recipients.js +1 -1
- package/lib/esm/resources/coupons.js +1 -1
- package/lib/esm/resources/custom_promotion_rules.js +1 -1
- package/lib/esm/resources/customer_addresses.js +1 -1
- package/lib/esm/resources/customer_groups.js +1 -1
- package/lib/esm/resources/customer_password_resets.js +1 -1
- package/lib/esm/resources/customer_payment_sources.js +1 -1
- package/lib/esm/resources/customer_subscriptions.js +1 -1
- package/lib/esm/resources/customers.js +1 -1
- package/lib/esm/resources/delivery_lead_times.js +1 -1
- package/lib/esm/resources/event_callbacks.js +1 -1
- package/lib/esm/resources/events.js +1 -1
- package/lib/esm/resources/exports.js +1 -1
- package/lib/esm/resources/external_gateways.js +1 -1
- package/lib/esm/resources/external_payments.js +1 -1
- package/lib/esm/resources/external_promotions.js +1 -1
- package/lib/esm/resources/external_tax_calculators.js +1 -1
- package/lib/esm/resources/fixed_amount_promotions.js +1 -1
- package/lib/esm/resources/fixed_price_promotions.js +1 -1
- package/lib/esm/resources/free_gift_promotions.js +1 -1
- package/lib/esm/resources/free_shipping_promotions.js +1 -1
- package/lib/esm/resources/geocoders.js +1 -1
- package/lib/esm/resources/gift_card_recipients.js +1 -1
- package/lib/esm/resources/gift_cards.js +1 -1
- package/lib/esm/resources/google_geocoders.js +1 -1
- package/lib/esm/resources/imports.js +1 -1
- package/lib/esm/resources/in_stock_subscriptions.js +1 -1
- package/lib/esm/resources/inventory_models.js +1 -1
- package/lib/esm/resources/inventory_return_locations.js +1 -1
- package/lib/esm/resources/inventory_stock_locations.js +1 -1
- package/lib/esm/resources/klarna_gateways.js +1 -1
- package/lib/esm/resources/klarna_payments.js +1 -1
- package/lib/esm/resources/line_item_options.js +1 -1
- package/lib/esm/resources/line_items.js +1 -1
- package/lib/esm/resources/manual_gateways.js +1 -1
- package/lib/esm/resources/manual_tax_calculators.js +1 -1
- package/lib/esm/resources/markets.js +1 -1
- package/lib/esm/resources/merchants.js +1 -1
- package/lib/esm/resources/order_amount_promotion_rules.js +1 -1
- package/lib/esm/resources/order_copies.js +1 -1
- package/lib/esm/resources/order_factories.js +1 -1
- package/lib/esm/resources/order_subscription_items.js +1 -1
- package/lib/esm/resources/order_subscriptions.js +1 -1
- package/lib/esm/resources/order_validation_rules.js +1 -1
- package/lib/esm/resources/orders.js +1 -1
- package/lib/esm/resources/organization.js +1 -1
- package/lib/esm/resources/packages.js +1 -1
- package/lib/esm/resources/parcel_line_items.js +1 -1
- package/lib/esm/resources/parcels.js +1 -1
- package/lib/esm/resources/payment_gateways.js +1 -1
- package/lib/esm/resources/payment_methods.js +1 -1
- package/lib/esm/resources/payment_options.js +1 -1
- package/lib/esm/resources/paypal_gateways.js +1 -1
- package/lib/esm/resources/paypal_payments.js +1 -1
- package/lib/esm/resources/percentage_discount_promotions.js +1 -1
- package/lib/esm/resources/price_frequency_tiers.js +1 -1
- package/lib/esm/resources/price_lists.js +1 -1
- package/lib/esm/resources/price_tiers.js +1 -1
- package/lib/esm/resources/price_volume_tiers.js +1 -1
- package/lib/esm/resources/prices.js +1 -1
- package/lib/esm/resources/promotion_rules.js +1 -1
- package/lib/esm/resources/promotions.js +1 -1
- package/lib/esm/resources/recurring_order_copies.js +1 -1
- package/lib/esm/resources/refunds.js +1 -1
- package/lib/esm/resources/reserved_stocks.js +1 -1
- package/lib/esm/resources/resource_errors.js +1 -1
- package/lib/esm/resources/return_line_items.js +1 -1
- package/lib/esm/resources/returns.js +1 -1
- package/lib/esm/resources/satispay_gateways.js +1 -1
- package/lib/esm/resources/satispay_payments.js +1 -1
- package/lib/esm/resources/shipments.js +1 -1
- package/lib/esm/resources/shipping_categories.js +1 -1
- package/lib/esm/resources/shipping_method_tiers.js +1 -1
- package/lib/esm/resources/shipping_methods.js +1 -1
- package/lib/esm/resources/shipping_weight_tiers.js +1 -1
- package/lib/esm/resources/shipping_zones.js +1 -1
- package/lib/esm/resources/sku_list_items.js +1 -1
- package/lib/esm/resources/sku_list_promotion_rules.js +1 -1
- package/lib/esm/resources/sku_lists.js +1 -1
- package/lib/esm/resources/sku_options.js +1 -1
- package/lib/esm/resources/skus.js +1 -1
- package/lib/esm/resources/stock_items.js +1 -1
- package/lib/esm/resources/stock_line_items.js +1 -1
- package/lib/esm/resources/stock_locations.js +1 -1
- package/lib/esm/resources/stock_reservations.js +1 -1
- package/lib/esm/resources/stock_transfers.js +1 -1
- package/lib/esm/resources/stripe_gateways.js +1 -1
- package/lib/esm/resources/stripe_payments.js +1 -1
- package/lib/esm/resources/subscription_models.js +1 -1
- package/lib/esm/resources/tags.js +1 -1
- package/lib/esm/resources/tax_calculators.js +1 -1
- package/lib/esm/resources/tax_categories.js +1 -1
- package/lib/esm/resources/tax_rules.js +1 -1
- package/lib/esm/resources/taxjar_accounts.js +1 -1
- package/lib/esm/resources/transactions.js +1 -1
- package/lib/esm/resources/versions.js +1 -1
- package/lib/esm/resources/voids.js +1 -1
- package/lib/esm/resources/webhooks.js +1 -1
- package/lib/esm/resources/wire_transfers.js +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
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
|
123
|
+
if (options?.adapter)
|
124
124
|
debug('Adapter ignored in request config');
|
125
|
-
if (options
|
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 =
|
131
|
-
const accessToken =
|
132
|
-
const headers = this.customHeaders(options
|
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 };
|
@@ -139,8 +139,8 @@ declare class CommerceLayerClient {
|
|
139
139
|
singletons(): readonly string[];
|
140
140
|
isSingleton(resource: api.ResourceTypeLock): boolean;
|
141
141
|
isApiError(error: any): error is ApiError;
|
142
|
-
addRequestInterceptor(onFulfilled?: RequestInterceptor
|
143
|
-
addResponseInterceptor(onFulfilled?: ResponseInterceptor
|
142
|
+
addRequestInterceptor(onFulfilled?: RequestInterceptor, onRejected?: ErrorInterceptor): number;
|
143
|
+
addResponseInterceptor(onFulfilled?: ResponseInterceptor, onRejected?: ErrorInterceptor): number;
|
144
144
|
removeInterceptor(type: InterceptorType, id: number): void;
|
145
145
|
addRawResponseReader(options?: {
|
146
146
|
headers: boolean;
|
package/lib/cjs/commercelayer.js
CHANGED
@@ -230,8 +230,8 @@ class CommerceLayerClient {
|
|
230
230
|
headers: undefined,
|
231
231
|
};
|
232
232
|
function rawResponseInterceptor(response) {
|
233
|
-
reader.rawResponse = response
|
234
|
-
if (options
|
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
|
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 (
|
6
|
+
return (resource?.type && resource.id) && api_1.resourceList.includes(resource.type);
|
7
7
|
};
|
8
8
|
exports.isResourceId = isResourceId;
|
9
9
|
const isResourceType = (resource) => {
|
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
|
-
|
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;
|
package/lib/cjs/interceptor.d.ts
CHANGED
@@ -6,14 +6,14 @@ type InterceptorManager = {
|
|
6
6
|
type RequestObj = AxiosRequestConfig;
|
7
7
|
type RequestInterceptor = (request: RequestObj) => RequestObj | Promise<RequestObj>;
|
8
8
|
type ResponseObj = AxiosResponse;
|
9
|
-
type ResponseInterceptor = (response: ResponseObj) => ResponseObj
|
9
|
+
type ResponseInterceptor = (response: ResponseObj) => ResponseObj;
|
10
10
|
type ApiHeadersList = 'x-ratelimit-limit' | 'x-ratelimit-count' | 'x-ratelimit-period' | 'x-ratelimit-interval' | 'x-ratelimit-remaining';
|
11
11
|
type ApiHeaders = {
|
12
12
|
[key in ApiHeadersList]: string | number | boolean;
|
13
13
|
};
|
14
14
|
type HeadersObj = (AxiosResponseHeaders | RawAxiosResponseHeaders) & ApiHeaders;
|
15
15
|
type ErrorObj = AxiosError;
|
16
|
-
type ErrorInterceptor = (error: ErrorObj) => ErrorObj
|
16
|
+
type ErrorInterceptor = (error: ErrorObj) => ErrorObj;
|
17
17
|
type InterceptorType = 'request' | 'response';
|
18
18
|
export type { InterceptorManager, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, InterceptorType };
|
19
19
|
export type { RequestObj, ResponseObj, ErrorObj, HeadersObj };
|
package/lib/cjs/resource.js
CHANGED
@@ -63,8 +63,8 @@ class ResourceAdapter {
|
|
63
63
|
async singleton(resource, params, options) {
|
64
64
|
debug('singleton: %o, %O, %O', resource, params || {}, options || {});
|
65
65
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
66
|
-
if (options
|
67
|
-
Object.assign(queryParams, options
|
66
|
+
if (options?.params)
|
67
|
+
Object.assign(queryParams, options?.params);
|
68
68
|
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}`, undefined, { ...options, params: queryParams });
|
69
69
|
const r = (0, jsonapi_1.denormalize)(res);
|
70
70
|
return r;
|
@@ -72,33 +72,32 @@ class ResourceAdapter {
|
|
72
72
|
async retrieve(resource, params, options) {
|
73
73
|
debug('retrieve: %o, %O, %O', resource, params || {}, options || {});
|
74
74
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
75
|
-
if (options
|
76
|
-
Object.assign(queryParams, options
|
75
|
+
if (options?.params)
|
76
|
+
Object.assign(queryParams, options?.params);
|
77
77
|
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}/${resource.id}`, undefined, { ...options, params: queryParams });
|
78
78
|
const r = (0, jsonapi_1.denormalize)(res);
|
79
79
|
return r;
|
80
80
|
}
|
81
81
|
async list(resource, params, options) {
|
82
|
-
var _a, _b;
|
83
82
|
debug('list: %o, %O, %O', resource, params || {}, options || {});
|
84
83
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
85
|
-
if (options
|
86
|
-
Object.assign(queryParams, options
|
84
|
+
if (options?.params)
|
85
|
+
Object.assign(queryParams, options?.params);
|
87
86
|
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}`, undefined, { ...options, params: queryParams });
|
88
87
|
const r = (0, jsonapi_1.denormalize)(res);
|
89
88
|
const meta = {
|
90
|
-
pageCount: Number(
|
91
|
-
recordCount: Number(
|
92
|
-
currentPage:
|
93
|
-
recordsPerPage:
|
89
|
+
pageCount: Number(res.meta?.page_count),
|
90
|
+
recordCount: Number(res.meta?.record_count),
|
91
|
+
currentPage: params?.pageNumber || config_1.default.default.pageNumber,
|
92
|
+
recordsPerPage: params?.pageSize || config_1.default.default.pageSize
|
94
93
|
};
|
95
94
|
return new ListResponse(meta, r);
|
96
95
|
}
|
97
96
|
async create(resource, params, options) {
|
98
97
|
debug('create: %o, %O, %O', resource, params || {}, options || {});
|
99
98
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
100
|
-
if (options
|
101
|
-
Object.assign(queryParams, options
|
99
|
+
if (options?.params)
|
100
|
+
Object.assign(queryParams, options?.params);
|
102
101
|
const data = (0, jsonapi_1.normalize)(resource);
|
103
102
|
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('post', resource.type, data, { ...options, params: queryParams });
|
104
103
|
const r = (0, jsonapi_1.denormalize)(res);
|
@@ -107,8 +106,8 @@ class ResourceAdapter {
|
|
107
106
|
async update(resource, params, options) {
|
108
107
|
debug('update: %o, %O, %O', resource, params || {}, options || {});
|
109
108
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
110
|
-
if (options
|
111
|
-
Object.assign(queryParams, options
|
109
|
+
if (options?.params)
|
110
|
+
Object.assign(queryParams, options?.params);
|
112
111
|
const data = (0, jsonapi_1.normalize)(resource);
|
113
112
|
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('patch', `${resource.type}/${resource.id}`, data, { ...options, params: queryParams });
|
114
113
|
const r = (0, jsonapi_1.denormalize)(res);
|
@@ -119,20 +118,19 @@ class ResourceAdapter {
|
|
119
118
|
await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('delete', `${resource.type}/${resource.id}`, undefined, options);
|
120
119
|
}
|
121
120
|
async fetch(resource, path, params, options) {
|
122
|
-
var _a, _b;
|
123
121
|
debug('fetch: %o, %O, %O', path, params || {}, options || {});
|
124
122
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
125
|
-
if (options
|
126
|
-
Object.assign(queryParams, options
|
123
|
+
if (options?.params)
|
124
|
+
Object.assign(queryParams, options?.params);
|
127
125
|
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', path, undefined, { ...options, params: queryParams });
|
128
126
|
const r = (0, jsonapi_1.denormalize)(res);
|
129
127
|
if (Array.isArray(r)) {
|
130
128
|
const p = params;
|
131
129
|
const meta = {
|
132
|
-
pageCount: Number(
|
133
|
-
recordCount: Number(
|
134
|
-
currentPage:
|
135
|
-
recordsPerPage:
|
130
|
+
pageCount: Number(res.meta?.page_count),
|
131
|
+
recordCount: Number(res.meta?.record_count),
|
132
|
+
currentPage: p?.pageNumber || config_1.default.default.pageNumber,
|
133
|
+
recordsPerPage: p?.pageSize || config_1.default.default.pageSize
|
136
134
|
};
|
137
135
|
return new ListResponse(meta, r);
|
138
136
|
}
|
@@ -147,11 +145,10 @@ class ApiResourceBase {
|
|
147
145
|
this.resources = adapter;
|
148
146
|
}
|
149
147
|
parse(resource, options) {
|
150
|
-
var _a;
|
151
148
|
try {
|
152
149
|
const res = JSON.parse(resource);
|
153
150
|
// Resource type always checked
|
154
|
-
const rtype =
|
151
|
+
const rtype = res.data?.type;
|
155
152
|
if (rtype !== this.type())
|
156
153
|
throw new error_1.SdkError({ message: `Invalid resource type [${rtype}]`, type: error_1.ErrorType.PARSE });
|
157
154
|
// Parse options
|
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
|
-
|
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 ' +
|
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
|
-
|
69
|
-
this.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 =
|
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
|
-
|
92
|
-
def.headers.common.Authorization = 'Bearer ' +
|
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
|
-
|
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
|
-
|
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
|
108
|
+
if (options?.adapter)
|
119
109
|
debug('Adapter ignored in request config');
|
120
|
-
if (options
|
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 =
|
126
|
-
const accessToken =
|
127
|
-
const headers = this.customHeaders(options
|
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
|
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;
|
@@ -139,8 +139,8 @@ declare class CommerceLayerClient {
|
|
139
139
|
singletons(): readonly string[];
|
140
140
|
isSingleton(resource: api.ResourceTypeLock): boolean;
|
141
141
|
isApiError(error: any): error is ApiError;
|
142
|
-
addRequestInterceptor(onFulfilled?: RequestInterceptor
|
143
|
-
addResponseInterceptor(onFulfilled?: ResponseInterceptor
|
142
|
+
addRequestInterceptor(onFulfilled?: RequestInterceptor, onRejected?: ErrorInterceptor): number;
|
143
|
+
addResponseInterceptor(onFulfilled?: ResponseInterceptor, onRejected?: ErrorInterceptor): number;
|
144
144
|
removeInterceptor(type: InterceptorType, id: number): void;
|
145
145
|
addRawResponseReader(options?: {
|
146
146
|
headers: boolean;
|