@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.
- package/lib/cjs/client.js +5 -5
- package/lib/cjs/commercelayer.js +3 -3
- package/lib/cjs/common.js +1 -1
- package/lib/cjs/config.d.ts +0 -3
- package/lib/cjs/config.js +0 -3
- package/lib/cjs/error.js +1 -2
- package/lib/cjs/jsonapi.js +5 -10
- package/lib/cjs/query.d.ts +1 -1
- package/lib/cjs/query.js +1 -18
- package/lib/cjs/resource.d.ts +2 -5
- package/lib/cjs/resource.js +27 -37
- package/lib/esm/client.js +19 -30
- package/lib/esm/commercelayer.js +260 -149
- package/lib/esm/common.js +1 -1
- package/lib/esm/config.d.ts +0 -3
- package/lib/esm/config.js +0 -3
- package/lib/esm/error.js +10 -5
- package/lib/esm/jsonapi.js +5 -10
- package/lib/esm/query.d.ts +1 -1
- package/lib/esm/query.js +1 -18
- package/lib/esm/resource.d.ts +2 -5
- package/lib/esm/resource.js +42 -62
- 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 +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
|
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 };
|
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/config.d.ts
CHANGED
package/lib/cjs/config.js
CHANGED
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/jsonapi.js
CHANGED
@@ -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
|
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 (
|
49
|
-
resource[key] = rel;
|
50
|
-
else
|
51
|
-
|
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;
|
package/lib/cjs/query.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { ResourceType } from "./resource";
|
2
|
-
type QueryFilter = Record<string, string | number | boolean | object
|
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
|
-
|
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
|
}
|
package/lib/cjs/resource.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import
|
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
|
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> {
|
package/lib/cjs/resource.js
CHANGED
@@ -57,14 +57,16 @@ class ResourceAdapter {
|
|
57
57
|
__classPrivateFieldGet(this, _ResourceAdapter_client, "f").config(config);
|
58
58
|
return this;
|
59
59
|
}
|
60
|
-
|
61
|
-
|
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
|
67
|
-
Object.assign(queryParams, options
|
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
|
76
|
-
Object.assign(queryParams, options
|
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
|
86
|
-
Object.assign(queryParams, options
|
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(
|
91
|
-
recordCount: Number(
|
92
|
-
currentPage:
|
93
|
-
recordsPerPage:
|
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
|
101
|
-
Object.assign(queryParams, options
|
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
|
111
|
-
Object.assign(queryParams, options
|
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
|
126
|
-
Object.assign(queryParams, options
|
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(
|
133
|
-
recordCount: Number(
|
134
|
-
currentPage:
|
135
|
-
recordsPerPage:
|
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
|
150
|
-
var _a;
|
149
|
+
parse(resource) {
|
151
150
|
try {
|
152
151
|
const res = JSON.parse(resource);
|
153
|
-
|
154
|
-
|
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
|
-
|
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;
|