@commercelayer/sdk 6.0.0-alfa.5 → 6.0.0-alfa.6
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/api.d.ts +0 -3
- package/lib/cjs/api.js +0 -10
- package/lib/cjs/client.js +0 -23
- package/lib/cjs/commercelayer.js +3 -25
- package/lib/cjs/common.js +0 -11
- package/lib/cjs/config.js +0 -2
- package/lib/cjs/debug.js +0 -21
- package/lib/cjs/error.d.ts +5 -5
- package/lib/cjs/error.js +1 -6
- package/lib/cjs/fetch.js +2 -2
- package/lib/cjs/index.js +0 -2
- package/lib/cjs/jsonapi.js +0 -2
- package/lib/cjs/model.d.ts +0 -3
- package/lib/cjs/model.js +0 -1
- package/lib/cjs/query.js +0 -5
- package/lib/cjs/resource.js +0 -35
- package/lib/cjs/static.js +0 -1
- package/lib/cjs/util.js +0 -1
- package/lib/esm/api.d.ts +0 -3
- package/lib/esm/api.js +0 -10
- package/lib/esm/client.js +0 -23
- package/lib/esm/commercelayer.js +3 -25
- package/lib/esm/common.js +0 -11
- package/lib/esm/config.js +0 -2
- package/lib/esm/debug.js +0 -21
- package/lib/esm/error.d.ts +5 -5
- package/lib/esm/error.js +1 -6
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/index.js +0 -2
- package/lib/esm/jsonapi.js +0 -2
- package/lib/esm/model.d.ts +0 -3
- package/lib/esm/model.js +0 -1
- package/lib/esm/query.js +0 -5
- package/lib/esm/resource.js +0 -35
- package/lib/esm/static.js +0 -1
- package/lib/esm/util.js +1 -27
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -2
package/lib/esm/commercelayer.js
CHANGED
@@ -3,17 +3,12 @@ import { CommerceLayerStatic } from './static';
|
|
3
3
|
import ResourceAdapter from './resource';
|
4
4
|
import Debug from './debug';
|
5
5
|
const debug = Debug('commercelayer');
|
6
|
-
// Autogenerated schema version number, do not remove this line
|
7
6
|
const OPEN_API_SCHEMA_VERSION = '5.1.0';
|
8
7
|
export { OPEN_API_SCHEMA_VERSION };
|
9
8
|
class CommerceLayerClient {
|
10
|
-
// static get openApiSchemaVersion(): string { return OPEN_API_SCHEMA_VERSION }
|
11
9
|
openApiSchemaVersion = OPEN_API_SCHEMA_VERSION;
|
12
10
|
#adapter;
|
13
11
|
#slug;
|
14
|
-
// #environment: ApiMode = sdkConfig.default.environment
|
15
|
-
// ##__CL_RESOURCES_DEF_START__##
|
16
|
-
// ##__CL_RESOURCES_DEF_TEMPLATE:: ##__TAB__#####__RESOURCE_TYPE__##?: api.##__RESOURCE_CLASS__##
|
17
12
|
#addresses;
|
18
13
|
#adjustments;
|
19
14
|
#adyen_gateways;
|
@@ -135,18 +130,11 @@ class CommerceLayerClient {
|
|
135
130
|
#voids;
|
136
131
|
#webhooks;
|
137
132
|
#wire_transfers;
|
138
|
-
// ##__CL_RESOURCES_DEF_STOP__##
|
139
133
|
constructor(config) {
|
140
134
|
debug('new commercelayer instance %O', config);
|
141
135
|
this.#adapter = new ResourceAdapter(config);
|
142
136
|
this.#slug = config.organization;
|
143
|
-
// this.#environment = 'test'
|
144
|
-
// ##__CL_RESOURCES_INIT_START__##
|
145
|
-
// ##__CL_RESOURCES_INIT_TEMPLATE:: ##__TAB__####__TAB__##this.##__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)
|
146
|
-
// ##__CL_RESOURCES_INIT_STOP__##
|
147
137
|
}
|
148
|
-
// ##__CL_RESOURCES_LEAZY_LOADING_START__##
|
149
|
-
// ##__CL_RESOURCES_LEAZY_LOADING_TEMPLATE:: ##__TAB__##get ##__RESOURCE_TYPE__##(): api.##__RESOURCE_CLASS__## { return this.###__RESOURCE_TYPE__## || (this.###__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)) }
|
150
138
|
get addresses() { return this.#addresses || (this.#addresses = new api.Addresses(this.#adapter)); }
|
151
139
|
get adjustments() { return this.#adjustments || (this.#adjustments = new api.Adjustments(this.#adapter)); }
|
152
140
|
get adyen_gateways() { return this.#adyen_gateways || (this.#adyen_gateways = new api.AdyenGateways(this.#adapter)); }
|
@@ -268,10 +256,7 @@ class CommerceLayerClient {
|
|
268
256
|
get voids() { return this.#voids || (this.#voids = new api.Voids(this.#adapter)); }
|
269
257
|
get webhooks() { return this.#webhooks || (this.#webhooks = new api.Webhooks(this.#adapter)); }
|
270
258
|
get wire_transfers() { return this.#wire_transfers || (this.#wire_transfers = new api.WireTransfers(this.#adapter)); }
|
271
|
-
// ##__CL_RESOURCES_LEAZY_LOADING_STOP__##
|
272
259
|
get currentOrganization() { return this.#slug; }
|
273
|
-
// get currentAccessToken(): string { return this.#adapter?.clientInstance?.currentAccessToken}
|
274
|
-
// get environment(): ApiMode { return this.#environment }
|
275
260
|
get interceptors() { return this.#adapter.client.interceptors; }
|
276
261
|
localConfig(config) {
|
277
262
|
if (config.organization)
|
@@ -279,10 +264,7 @@ class CommerceLayerClient {
|
|
279
264
|
}
|
280
265
|
config(config) {
|
281
266
|
debug('config %o', config);
|
282
|
-
// CommerceLayer config
|
283
267
|
this.localConfig(config);
|
284
|
-
// ResourceAdapter config
|
285
|
-
// To rebuild baseUrl in client in case only the domain is defined
|
286
268
|
if (!config.organization)
|
287
269
|
config.organization = this.currentOrganization;
|
288
270
|
this.#adapter.config(config);
|
@@ -326,15 +308,11 @@ class CommerceLayerClient {
|
|
326
308
|
}
|
327
309
|
return response;
|
328
310
|
}
|
329
|
-
|
330
|
-
reader.id = 1;
|
311
|
+
this.interceptors.rawReader = { onSuccess: rawResponseInterceptor, onFailure: rawResponseInterceptor };
|
312
|
+
reader.id = 1;
|
331
313
|
return reader;
|
332
314
|
}
|
333
|
-
removeRawResponseReader(
|
334
|
-
/*
|
335
|
-
const id = (typeof reader === 'number') ? reader : reader?.id
|
336
|
-
if (id && (id >= 0)) this.removeInterceptor('response', id)
|
337
|
-
*/
|
315
|
+
removeRawResponseReader() {
|
338
316
|
this.interceptors.rawReader = undefined;
|
339
317
|
}
|
340
318
|
}
|
package/lib/esm/common.js
CHANGED
@@ -6,14 +6,3 @@ const isResourceType = (resource) => {
|
|
6
6
|
return resource && (typeof resource.type !== 'undefined') && resource.type && resourceList.includes(resource.type);
|
7
7
|
};
|
8
8
|
export { isResourceId, isResourceType };
|
9
|
-
/*
|
10
|
-
// Nullable types
|
11
|
-
type StringNullable = string | null
|
12
|
-
type NumberNullable = number | null
|
13
|
-
type BooleanNullable = boolean | null
|
14
|
-
type ResourceNullable<R extends Resource> = R | null
|
15
|
-
type ResourceArrayNullable<R extends Resource> = R[] | null
|
16
|
-
|
17
|
-
|
18
|
-
export type { StringNullable, NumberNullable, BooleanNullable, ResourceNullable, ResourceArrayNullable }
|
19
|
-
*/
|
package/lib/esm/config.js
CHANGED
package/lib/esm/debug.js
CHANGED
@@ -1,35 +1,14 @@
|
|
1
|
-
/* Nope debugger */
|
2
1
|
const debuggerFunction = (_pattern, ..._args) => {
|
3
|
-
// console.log(_pattern)
|
4
2
|
};
|
5
3
|
let debuggerFactory = (_namespace) => debuggerFunction;
|
6
|
-
/* Try loading 'debug' module */
|
7
4
|
try {
|
8
5
|
const debugModule = require('debug');
|
9
6
|
if (debugModule && (typeof debugModule === 'function'))
|
10
7
|
debuggerFactory = debugModule;
|
11
8
|
}
|
12
9
|
catch (error) {
|
13
|
-
//
|
14
10
|
}
|
15
11
|
const debugPrefix = 'clsdk';
|
16
|
-
/* Retrieve the name of the caller 'module' */
|
17
|
-
/*
|
18
|
-
const caller = (): string => {
|
19
|
-
|
20
|
-
const err = new Error()
|
21
|
-
|
22
|
-
Error.prepareStackTrace = (_, stack) => stack
|
23
|
-
const stack = err.stack as unknown as NodeJS.CallSite[]
|
24
|
-
Error.prepareStackTrace = undefined
|
25
|
-
|
26
|
-
const fileName = stack[2].getFileName() || '/'
|
27
|
-
|
28
|
-
return fileName.replace(/^.*[\\/]/, '').replace('.ts', '')
|
29
|
-
|
30
|
-
}
|
31
|
-
*/
|
32
|
-
/* Return a debugger for the defined namespace */
|
33
12
|
const debug = (namespace) => {
|
34
13
|
return debuggerFactory(`${debugPrefix}:${namespace}`);
|
35
14
|
};
|
package/lib/esm/error.d.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
declare enum ErrorType {
|
2
|
-
CLIENT = "client"
|
3
|
-
REQUEST = "request"
|
4
|
-
RESPONSE = "response"
|
5
|
-
CANCEL = "cancel"
|
6
|
-
PARSE = "parse"
|
2
|
+
CLIENT = "client",
|
3
|
+
REQUEST = "request",
|
4
|
+
RESPONSE = "response",
|
5
|
+
CANCEL = "cancel",
|
6
|
+
PARSE = "parse",
|
7
7
|
TIMEOUT = "timeout"
|
8
8
|
}
|
9
9
|
declare class SdkError extends Error {
|
package/lib/esm/error.js
CHANGED
@@ -6,7 +6,7 @@ var ErrorType;
|
|
6
6
|
ErrorType["RESPONSE"] = "response";
|
7
7
|
ErrorType["CANCEL"] = "cancel";
|
8
8
|
ErrorType["PARSE"] = "parse";
|
9
|
-
ErrorType["TIMEOUT"] = "timeout";
|
9
|
+
ErrorType["TIMEOUT"] = "timeout";
|
10
10
|
})(ErrorType || (ErrorType = {}));
|
11
11
|
class SdkError extends Error {
|
12
12
|
static NAME = 'SdkError';
|
@@ -50,7 +50,6 @@ const isTimeoutError = (error) => {
|
|
50
50
|
const handleError = (error) => {
|
51
51
|
let sdkError = new SdkError({ message: error.message });
|
52
52
|
if (FetchError.isFetchError(error)) {
|
53
|
-
// console.log('********** FetchError')
|
54
53
|
const apiError = new ApiError(sdkError);
|
55
54
|
apiError.type = ErrorType.RESPONSE;
|
56
55
|
apiError.status = error.status;
|
@@ -62,19 +61,15 @@ const handleError = (error) => {
|
|
62
61
|
sdkError = apiError;
|
63
62
|
}
|
64
63
|
else if (isRequestError(error)) {
|
65
|
-
// console.log('********** RequestError')
|
66
64
|
sdkError.type = ErrorType.REQUEST;
|
67
65
|
}
|
68
66
|
else if (isCancelError(error)) {
|
69
|
-
// console.log('********** CancelError')
|
70
67
|
sdkError.type = ErrorType.CANCEL;
|
71
68
|
}
|
72
69
|
else if (isTimeoutError(error)) {
|
73
|
-
// console.log('********** TimeoutError')
|
74
70
|
sdkError.type = ErrorType.TIMEOUT;
|
75
71
|
}
|
76
72
|
else {
|
77
|
-
// console.log('********** ClientError')
|
78
73
|
sdkError.type = ErrorType.CLIENT;
|
79
74
|
sdkError.source = error;
|
80
75
|
}
|
package/lib/esm/fetch.js
CHANGED
@@ -22,8 +22,8 @@ export const fetchURL = async (url, options, interceptors) => {
|
|
22
22
|
debug('fetch: %s, %O', url, options || {});
|
23
23
|
if (interceptors?.request?.onSuccess)
|
24
24
|
({ url, options } = await interceptors.request.onSuccess({ url, options }));
|
25
|
-
|
26
|
-
let response = await fetch(
|
25
|
+
const request = new Request(url, options);
|
26
|
+
let response = await fetch(request);
|
27
27
|
if (response.ok) {
|
28
28
|
if (interceptors?.rawReader?.onSuccess)
|
29
29
|
await interceptors.rawReader.onSuccess(response);
|
package/lib/esm/index.js
CHANGED
package/lib/esm/jsonapi.js
CHANGED
@@ -2,7 +2,6 @@ import { isResourceId, isResourceType } from './common';
|
|
2
2
|
import config from './config';
|
3
3
|
import Debug from './debug';
|
4
4
|
const debug = Debug('jsonapi');
|
5
|
-
// DENORMALIZATION
|
6
5
|
const denormalize = (response) => {
|
7
6
|
let denormalizedResponse;
|
8
7
|
if (response.links)
|
@@ -53,7 +52,6 @@ const denormalizeResource = (res, included, chain = []) => {
|
|
53
52
|
debug('denormalized resource: %O', resource);
|
54
53
|
return resource;
|
55
54
|
};
|
56
|
-
// NORMALIZATION
|
57
55
|
const normalize = (resource) => {
|
58
56
|
debug('normalize resource: %O', resource);
|
59
57
|
const attributes = {};
|
package/lib/esm/model.d.ts
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* ©2024 Commerce Layer Inc.
|
3
|
-
**/
|
4
1
|
export type { Address, AddressCreate, AddressUpdate } from './resources/addresses';
|
5
2
|
export type { Adjustment, AdjustmentCreate, AdjustmentUpdate } from './resources/adjustments';
|
6
3
|
export type { AdyenGateway, AdyenGatewayCreate, AdyenGatewayUpdate } from './resources/adyen_gateways';
|
package/lib/esm/model.js
CHANGED
package/lib/esm/query.js
CHANGED
@@ -11,10 +11,8 @@ const generateQueryStringParams = (params, res) => {
|
|
11
11
|
const qp = {};
|
12
12
|
if (!params)
|
13
13
|
return qp;
|
14
|
-
// Include
|
15
14
|
if (params.include)
|
16
15
|
qp.include = params.include.join(',');
|
17
|
-
// Fields
|
18
16
|
if (params.fields) {
|
19
17
|
if (Array.isArray(params.fields))
|
20
18
|
params.fields = { [res.type || res]: params.fields };
|
@@ -23,19 +21,16 @@ const generateQueryStringParams = (params, res) => {
|
|
23
21
|
});
|
24
22
|
}
|
25
23
|
if (isParamsList(params)) {
|
26
|
-
// Sort
|
27
24
|
if (params.sort) {
|
28
25
|
if (Array.isArray(params.sort))
|
29
26
|
qp.sort = params.sort.join(',');
|
30
27
|
else
|
31
28
|
qp.sort = Object.entries(params.sort).map(([k, v]) => `${v === 'desc' ? '-' : ''}${k}`).join(',');
|
32
29
|
}
|
33
|
-
// Page
|
34
30
|
if (params.pageNumber)
|
35
31
|
qp['page[number]'] = String(params.pageNumber);
|
36
32
|
if (params.pageSize)
|
37
33
|
qp['page[size]'] = String(params.pageSize);
|
38
|
-
// Filters
|
39
34
|
if (params.filters) {
|
40
35
|
Object.entries(params.filters).forEach(([p, v]) => {
|
41
36
|
const filter = p.substring(p.lastIndexOf('_'));
|
package/lib/esm/resource.js
CHANGED
@@ -28,13 +28,10 @@ class ResourceAdapter {
|
|
28
28
|
this.localConfig(config);
|
29
29
|
}
|
30
30
|
localConfig(config) {
|
31
|
-
// if (typeof config.xyz !== 'undefined') this.#config.xyz = config.xyz
|
32
31
|
}
|
33
32
|
config(config) {
|
34
33
|
debug('config %o', config);
|
35
|
-
// ResourceAdapter config
|
36
34
|
this.localConfig(config);
|
37
|
-
// Client config
|
38
35
|
this.#client.config(config);
|
39
36
|
return this;
|
40
37
|
}
|
@@ -125,7 +122,6 @@ class ApiResourceBase {
|
|
125
122
|
constructor(adapter) {
|
126
123
|
debug('new resource instance: %s', this.type());
|
127
124
|
this.resources = adapter;
|
128
|
-
console.log('CONSTRUCTOR ' + this.type());
|
129
125
|
}
|
130
126
|
relationshipOneToOne(id) {
|
131
127
|
return (((id === null) || (typeof id === 'string')) ? { id, type: this.type() } : { id: id.id, type: this.type() });
|
@@ -133,37 +129,6 @@ class ApiResourceBase {
|
|
133
129
|
relationshipOneToMany(...ids) {
|
134
130
|
return (((ids === null) || (ids.length === 0) || (ids[0] === null)) ? [{ id: null, type: this.type() }] : ids.map(id => { return { id, type: this.type() }; }));
|
135
131
|
}
|
136
|
-
/*
|
137
|
-
parse(resource: string, options?: { ignoreSlug?: boolean }): R | R[] {
|
138
|
-
|
139
|
-
try {
|
140
|
-
|
141
|
-
const res = JSON.parse(resource)
|
142
|
-
|
143
|
-
// Resource type always checked
|
144
|
-
const rtype = res.data?.type
|
145
|
-
if (rtype !== this.type()) throw new SdkError({ message: `Invalid resource type [${rtype}]`, type: ErrorType.PARSE })
|
146
|
-
|
147
|
-
// Parse options
|
148
|
-
const { ignoreSlug } = options || {}
|
149
|
-
|
150
|
-
if (!ignoreSlug) {
|
151
|
-
const links = res.data.links.self
|
152
|
-
if (!links || !String(links).match(`^${this.resources.client.baseUrl}/${this.type()}/*`))
|
153
|
-
throw new SdkError({ message: `Resource contains invalid links [${links}]`, type: ErrorType.PARSE })
|
154
|
-
}
|
155
|
-
|
156
|
-
|
157
|
-
return denormalize<R>(res as DocWithData)
|
158
|
-
|
159
|
-
} catch (error: any) {
|
160
|
-
if (SdkError.isSdkError(error)) throw error
|
161
|
-
else throw new SdkError({ message: `Payload parse error [${error.message}]`, type: ErrorType.PARSE })
|
162
|
-
}
|
163
|
-
|
164
|
-
}
|
165
|
-
*/
|
166
|
-
// reference, reference_origin and metadata attributes are always updatable
|
167
132
|
async update(resource, params, options) {
|
168
133
|
return this.resources.update({ ...resource, type: this.type() }, params, options);
|
169
134
|
}
|
package/lib/esm/static.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import * as api from './api';
|
2
2
|
import { SdkError, ApiError } from './error';
|
3
3
|
import CommerceLayer, { OPEN_API_SCHEMA_VERSION } from './commercelayer';
|
4
|
-
/* Static functions */
|
5
4
|
export const CommerceLayerStatic = {
|
6
5
|
resources: (sort) => {
|
7
6
|
return sort ? [...api.resourceList].sort() : api.resourceList;
|
package/lib/esm/util.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
// import path from 'node:path'
|
2
1
|
const sleep = async (ms) => {
|
3
2
|
return new Promise(resolve => setTimeout(resolve, ms));
|
4
3
|
};
|
@@ -9,29 +8,4 @@ const sortObjectFields = (obj) => {
|
|
9
8
|
}, {});
|
10
9
|
return sorted;
|
11
10
|
};
|
12
|
-
|
13
|
-
const nestedField = (obj: any, field: string): { key: string, val: any } => {
|
14
|
-
|
15
|
-
let fp = field
|
16
|
-
if (fp.endsWith('.')) fp = fp.substring(0, fp.length-1)
|
17
|
-
|
18
|
-
const dots = field.split(".")
|
19
|
-
|
20
|
-
const key = dots[dots.length-1]
|
21
|
-
let val = obj
|
22
|
-
while (dots.length && (val = val[dots.shift() || '']))
|
23
|
-
|
24
|
-
return { key, val }
|
25
|
-
}
|
26
|
-
*/
|
27
|
-
/*
|
28
|
-
const packageInfo = (fields?: string | string[], options?: any): Record<string, any> => {
|
29
|
-
const pjson = require(path.resolve('./', 'package.json'))
|
30
|
-
return fields? (Array.isArray(fields)? fields : [ fields ]).reduce((info: any, field) => {
|
31
|
-
const nf = nestedField(pjson, field)
|
32
|
-
info[options?.nestedName? nf.key : field] = nf.val
|
33
|
-
return info
|
34
|
-
}, {}) : pjson
|
35
|
-
}
|
36
|
-
*/
|
37
|
-
export { sleep, sortObjectFields, /* packageInfo */ };
|
11
|
+
export { sleep, sortObjectFields, };
|