@commercelayer/sdk 6.0.0-alfa.4 → 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.d.ts +6 -14
- package/lib/cjs/client.js +39 -73
- package/lib/cjs/commercelayer.d.ts +126 -125
- package/lib/cjs/commercelayer.js +267 -155
- 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 +6 -7
- package/lib/cjs/error.js +35 -33
- package/lib/cjs/fetch.d.ts +13 -0
- package/lib/cjs/fetch.js +67 -0
- package/lib/cjs/index.js +0 -2
- package/lib/cjs/interceptor.d.ts +24 -12
- 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.d.ts +2 -1
- package/lib/cjs/query.js +8 -7
- package/lib/cjs/resource.d.ts +0 -5
- package/lib/cjs/resource.js +7 -36
- 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.d.ts +6 -14
- package/lib/esm/client.js +37 -73
- package/lib/esm/commercelayer.d.ts +126 -125
- package/lib/esm/commercelayer.js +265 -277
- 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 +6 -7
- package/lib/esm/error.js +35 -31
- package/lib/esm/fetch.d.ts +13 -0
- package/lib/esm/fetch.js +46 -0
- package/lib/esm/index.js +0 -2
- package/lib/esm/interceptor.d.ts +24 -12
- 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.d.ts +2 -1
- package/lib/esm/query.js +7 -7
- package/lib/esm/resource.d.ts +0 -5
- package/lib/esm/resource.js +7 -36
- 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 +15 -13
package/lib/esm/error.d.ts
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
declare enum ErrorType {
|
2
|
-
CLIENT = "client"
|
3
|
-
REQUEST = "request"
|
4
|
-
RESPONSE = "response"
|
5
|
-
CANCEL = "cancel"
|
6
|
-
PARSE = "parse"
|
7
|
-
|
2
|
+
CLIENT = "client",
|
3
|
+
REQUEST = "request",
|
4
|
+
RESPONSE = "response",
|
5
|
+
CANCEL = "cancel",
|
6
|
+
PARSE = "parse",
|
7
|
+
TIMEOUT = "timeout"
|
8
8
|
}
|
9
9
|
declare class SdkError extends Error {
|
10
10
|
static NAME: string;
|
@@ -12,7 +12,6 @@ declare class SdkError extends Error {
|
|
12
12
|
type: ErrorType;
|
13
13
|
code?: string;
|
14
14
|
source?: Error;
|
15
|
-
request?: any;
|
16
15
|
constructor(error: {
|
17
16
|
message: string;
|
18
17
|
type?: ErrorType;
|
package/lib/esm/error.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import
|
1
|
+
import { FetchError } from './fetch';
|
2
2
|
var ErrorType;
|
3
3
|
(function (ErrorType) {
|
4
4
|
ErrorType["CLIENT"] = "client";
|
@@ -6,7 +6,7 @@ var ErrorType;
|
|
6
6
|
ErrorType["RESPONSE"] = "response";
|
7
7
|
ErrorType["CANCEL"] = "cancel";
|
8
8
|
ErrorType["PARSE"] = "parse";
|
9
|
-
ErrorType["
|
9
|
+
ErrorType["TIMEOUT"] = "timeout";
|
10
10
|
})(ErrorType || (ErrorType = {}));
|
11
11
|
class SdkError extends Error {
|
12
12
|
static NAME = 'SdkError';
|
@@ -16,11 +16,10 @@ class SdkError extends Error {
|
|
16
16
|
type;
|
17
17
|
code;
|
18
18
|
source;
|
19
|
-
request;
|
20
19
|
constructor(error) {
|
21
20
|
super(error.message);
|
22
|
-
this.name = SdkError.NAME;
|
23
|
-
this.type = error.type || ErrorType.
|
21
|
+
this.name = SdkError.NAME;
|
22
|
+
this.type = error.type || ErrorType.CLIENT;
|
24
23
|
}
|
25
24
|
}
|
26
25
|
class ApiError extends SdkError {
|
@@ -33,42 +32,47 @@ class ApiError extends SdkError {
|
|
33
32
|
statusText;
|
34
33
|
constructor(error) {
|
35
34
|
super({ ...error, type: ErrorType.RESPONSE });
|
36
|
-
this.name = ApiError.NAME;
|
35
|
+
this.name = ApiError.NAME;
|
37
36
|
}
|
38
37
|
first() {
|
39
38
|
return (this.errors?.length > 0) ? this.errors[0] : undefined;
|
40
39
|
}
|
41
40
|
}
|
41
|
+
const isRequestError = (error) => {
|
42
|
+
return error instanceof TypeError;
|
43
|
+
};
|
44
|
+
const isCancelError = (error) => {
|
45
|
+
return (error instanceof DOMException) && (error.name === 'AbortError');
|
46
|
+
};
|
47
|
+
const isTimeoutError = (error) => {
|
48
|
+
return (error instanceof DOMException) && (error.name === 'TimeoutError');
|
49
|
+
};
|
42
50
|
const handleError = (error) => {
|
43
51
|
let sdkError = new SdkError({ message: error.message });
|
44
|
-
if (
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
apiError.
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
else if (error.request) {
|
58
|
-
// The request was made but no response was received
|
59
|
-
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
|
60
|
-
sdkError.type = ErrorType.REQUEST;
|
61
|
-
sdkError.request = error.request;
|
62
|
-
}
|
63
|
-
else {
|
64
|
-
// Something happened in setting up the request that triggered an Error
|
65
|
-
sdkError.type = ErrorType.CLIENT;
|
66
|
-
}
|
52
|
+
if (FetchError.isFetchError(error)) {
|
53
|
+
const apiError = new ApiError(sdkError);
|
54
|
+
apiError.type = ErrorType.RESPONSE;
|
55
|
+
apiError.status = error.status;
|
56
|
+
apiError.statusText = error.statusText;
|
57
|
+
apiError.code = String(apiError.status);
|
58
|
+
apiError.errors = error.errors || [];
|
59
|
+
if (!apiError.message && apiError.statusText)
|
60
|
+
apiError.message = apiError.statusText;
|
61
|
+
sdkError = apiError;
|
62
|
+
}
|
63
|
+
else if (isRequestError(error)) {
|
64
|
+
sdkError.type = ErrorType.REQUEST;
|
67
65
|
}
|
68
|
-
else if (
|
66
|
+
else if (isCancelError(error)) {
|
69
67
|
sdkError.type = ErrorType.CANCEL;
|
70
|
-
|
68
|
+
}
|
69
|
+
else if (isTimeoutError(error)) {
|
70
|
+
sdkError.type = ErrorType.TIMEOUT;
|
71
|
+
}
|
72
|
+
else {
|
73
|
+
sdkError.type = ErrorType.CLIENT;
|
71
74
|
sdkError.source = error;
|
75
|
+
}
|
72
76
|
throw sdkError;
|
73
77
|
};
|
74
78
|
export { SdkError, ApiError, ErrorType, handleError };
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import type { DocWithData } from 'jsonapi-typescript';
|
2
|
+
import type { InterceptorManager } from './interceptor';
|
3
|
+
export type FetchResponse = DocWithData;
|
4
|
+
export type FetchOptions = RequestInit;
|
5
|
+
export declare class FetchError extends Error {
|
6
|
+
#private;
|
7
|
+
static isFetchError: (error: any) => error is FetchError;
|
8
|
+
constructor(status: number, statusText: string, body?: any);
|
9
|
+
get errors(): any[] | undefined;
|
10
|
+
get status(): number;
|
11
|
+
get statusText(): string;
|
12
|
+
}
|
13
|
+
export declare const fetchURL: (url: URL, options: FetchOptions, interceptors?: InterceptorManager) => Promise<FetchResponse>;
|
package/lib/esm/fetch.js
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
import Debug from './debug';
|
2
|
+
const debug = Debug('fetch');
|
3
|
+
export class FetchError extends Error {
|
4
|
+
static isFetchError = (error) => {
|
5
|
+
return error instanceof FetchError;
|
6
|
+
};
|
7
|
+
#errors;
|
8
|
+
#status;
|
9
|
+
#statusText;
|
10
|
+
constructor(status, statusText, body) {
|
11
|
+
super(statusText);
|
12
|
+
this.#status = status;
|
13
|
+
this.#statusText = statusText;
|
14
|
+
if (body)
|
15
|
+
this.#errors = body.errors;
|
16
|
+
}
|
17
|
+
get errors() { return this.#errors; }
|
18
|
+
get status() { return this.#status; }
|
19
|
+
get statusText() { return this.#statusText; }
|
20
|
+
}
|
21
|
+
export const fetchURL = async (url, options, interceptors) => {
|
22
|
+
debug('fetch: %s, %O', url, options || {});
|
23
|
+
if (interceptors?.request?.onSuccess)
|
24
|
+
({ url, options } = await interceptors.request.onSuccess({ url, options }));
|
25
|
+
const request = new Request(url, options);
|
26
|
+
let response = await fetch(request);
|
27
|
+
if (response.ok) {
|
28
|
+
if (interceptors?.rawReader?.onSuccess)
|
29
|
+
await interceptors.rawReader.onSuccess(response);
|
30
|
+
if (interceptors?.response?.onSuccess)
|
31
|
+
response = await interceptors.response.onSuccess(response);
|
32
|
+
}
|
33
|
+
else {
|
34
|
+
if (interceptors?.rawReader?.onFailure)
|
35
|
+
await interceptors.rawReader.onFailure(response);
|
36
|
+
}
|
37
|
+
const responseBody = await response.json().catch(() => { });
|
38
|
+
if (!response.ok) {
|
39
|
+
let error = new FetchError(response.status, response.statusText, responseBody);
|
40
|
+
if (interceptors?.response?.onFailure)
|
41
|
+
error = await interceptors.response.onFailure(error);
|
42
|
+
if (error)
|
43
|
+
throw error;
|
44
|
+
}
|
45
|
+
return responseBody;
|
46
|
+
};
|
package/lib/esm/index.js
CHANGED
package/lib/esm/interceptor.d.ts
CHANGED
@@ -1,25 +1,37 @@
|
|
1
|
-
import type {
|
1
|
+
import type { FetchError, FetchOptions } from "./fetch";
|
2
|
+
type InterceptorEventManager<S extends (RequestInterceptor | ResponseInterceptor), F extends (ErrorInterceptor | ResponseInterceptor)> = {
|
3
|
+
onSuccess?: S;
|
4
|
+
onFailure?: F;
|
5
|
+
};
|
6
|
+
type RequestEventManager = InterceptorEventManager<RequestInterceptor, ErrorInterceptor>;
|
7
|
+
type ResponseEventManager = InterceptorEventManager<ResponseInterceptor, ErrorInterceptor>;
|
8
|
+
type ErrorEventManager = InterceptorEventManager<ResponseInterceptor, ResponseInterceptor>;
|
2
9
|
type InterceptorManager = {
|
3
|
-
request
|
4
|
-
response
|
10
|
+
request?: RequestEventManager;
|
11
|
+
response?: ResponseEventManager;
|
12
|
+
rawReader?: ErrorEventManager;
|
13
|
+
};
|
14
|
+
type RequestObj = {
|
15
|
+
url: URL;
|
16
|
+
options: FetchOptions;
|
5
17
|
};
|
6
|
-
type RequestObj = AxiosRequestConfig;
|
7
18
|
type RequestInterceptor = (request: RequestObj) => RequestObj | Promise<RequestObj>;
|
8
|
-
type ResponseObj =
|
9
|
-
type ResponseInterceptor = (response: ResponseObj) => ResponseObj
|
19
|
+
type ResponseObj = Response;
|
20
|
+
type ResponseInterceptor = (response: ResponseObj) => ResponseObj | Promise<ResponseObj>;
|
10
21
|
type ApiHeadersList = 'x-ratelimit-limit' | 'x-ratelimit-count' | 'x-ratelimit-period' | 'x-ratelimit-interval' | 'x-ratelimit-remaining';
|
11
22
|
type ApiHeaders = {
|
12
23
|
[key in ApiHeadersList]: string | number | boolean;
|
13
24
|
};
|
14
|
-
type HeadersObj =
|
15
|
-
type ErrorObj =
|
16
|
-
type ErrorInterceptor = (error: ErrorObj) => ErrorObj
|
25
|
+
type HeadersObj = Record<string, string> | ApiHeaders;
|
26
|
+
type ErrorObj = FetchError;
|
27
|
+
type ErrorInterceptor = (error: ErrorObj) => ErrorObj | Promise<ErrorObj>;
|
17
28
|
type InterceptorType = 'request' | 'response';
|
18
29
|
export type { InterceptorManager, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, InterceptorType };
|
19
30
|
export type { RequestObj, ResponseObj, ErrorObj, HeadersObj };
|
20
31
|
type RawResponseReader = {
|
21
|
-
id
|
22
|
-
rawResponse
|
23
|
-
headers
|
32
|
+
id?: number;
|
33
|
+
rawResponse?: any;
|
34
|
+
headers?: HeadersObj;
|
35
|
+
ok: boolean;
|
24
36
|
};
|
25
37
|
export type { RawResponseReader };
|
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.d.ts
CHANGED
@@ -14,4 +14,5 @@ type QueryParams = QueryParamsRetrieve | QueryParamsList;
|
|
14
14
|
export type { QueryParamsRetrieve, QueryParamsList, QueryParams, QueryFilter };
|
15
15
|
declare const isParamsList: (params: any) => params is QueryParamsList;
|
16
16
|
declare const generateQueryStringParams: (params: QueryParamsRetrieve | QueryParamsList | undefined, res: string | ResourceType) => Record<string, string>;
|
17
|
-
|
17
|
+
declare const generateSearchString: (params?: QueryParams, questionMark?: boolean) => string;
|
18
|
+
export { generateQueryStringParams, isParamsList, generateSearchString };
|
package/lib/esm/query.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import Debug from './debug';
|
2
1
|
import { ErrorType, SdkError } from "./error";
|
2
|
+
import Debug from './debug';
|
3
3
|
const debug = Debug('query');
|
4
4
|
const arrayFilters = ['_any', '_all', '_in'];
|
5
5
|
const objectFilters = ['_jcont'];
|
@@ -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('_'));
|
@@ -59,4 +54,9 @@ const generateQueryStringParams = (params, res) => {
|
|
59
54
|
debug('query string params: %O', qp);
|
60
55
|
return qp;
|
61
56
|
};
|
62
|
-
|
57
|
+
const generateSearchString = (params, questionMark = true) => {
|
58
|
+
if (!params || (Object.keys(params).length === 0))
|
59
|
+
return '';
|
60
|
+
return `${questionMark ? '?' : ''}${Object.entries(params).map(([key, val]) => `${key}=${String(val)}`).join('&')}`;
|
61
|
+
};
|
62
|
+
export { generateQueryStringParams, isParamsList, generateSearchString };
|
package/lib/esm/resource.d.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import ApiClient, { type ApiClientInitConfig } from './client';
|
2
2
|
import type { QueryParamsRetrieve, QueryParamsList, QueryFilter, QueryParams } from './query';
|
3
3
|
import type { ResourceTypeLock } from './api';
|
4
|
-
import type { InterceptorManager } from './interceptor';
|
5
4
|
type ResourceNull = {
|
6
5
|
id: null;
|
7
6
|
} & ResourceType;
|
@@ -53,7 +52,6 @@ type ResourcesConfig = Partial<ResourcesInitConfig>;
|
|
53
52
|
declare class ResourceAdapter {
|
54
53
|
#private;
|
55
54
|
constructor(config: ResourcesInitConfig);
|
56
|
-
get interceptors(): InterceptorManager;
|
57
55
|
private localConfig;
|
58
56
|
config(config: ResourcesConfig): ResourceAdapter;
|
59
57
|
get client(): Readonly<ApiClient>;
|
@@ -73,9 +71,6 @@ declare abstract class ApiResourceBase<R extends Resource> {
|
|
73
71
|
protected relationshipOneToOne<RR extends ResourceRel>(id: string | ResourceId | null): RR;
|
74
72
|
protected relationshipOneToMany<RR extends ResourceRel>(...ids: string[]): RR[];
|
75
73
|
abstract type(): ResourceTypeLock;
|
76
|
-
parse(resource: string, options?: {
|
77
|
-
ignoreSlug?: boolean;
|
78
|
-
}): R | R[];
|
79
74
|
update(resource: ResourceUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
|
80
75
|
}
|
81
76
|
declare abstract class ApiResource<R extends Resource> extends ApiResourceBase<R> {
|
package/lib/esm/resource.js
CHANGED
@@ -2,7 +2,6 @@ import ApiClient from './client';
|
|
2
2
|
import { denormalize, normalize } from './jsonapi';
|
3
3
|
import { generateQueryStringParams, isParamsList } from './query';
|
4
4
|
import config from './config';
|
5
|
-
import { ErrorType, SdkError } from './error';
|
6
5
|
import Debug from './debug';
|
7
6
|
const debug = Debug('resource');
|
8
7
|
class ListResponse extends Array {
|
@@ -28,15 +27,11 @@ class ResourceAdapter {
|
|
28
27
|
this.#client = ApiClient.create(config);
|
29
28
|
this.localConfig(config);
|
30
29
|
}
|
31
|
-
get interceptors() { return this.#client.interceptors; }
|
32
30
|
localConfig(config) {
|
33
|
-
// if (typeof config.xyz !== 'undefined') this.#config.xyz = config.xyz
|
34
31
|
}
|
35
32
|
config(config) {
|
36
33
|
debug('config %o', config);
|
37
|
-
// ResourceAdapter config
|
38
34
|
this.localConfig(config);
|
39
|
-
// Client config
|
40
35
|
this.#client.config(config);
|
41
36
|
return this;
|
42
37
|
}
|
@@ -48,7 +43,7 @@ class ResourceAdapter {
|
|
48
43
|
const queryParams = generateQueryStringParams(params, resource);
|
49
44
|
if (options?.params)
|
50
45
|
Object.assign(queryParams, options?.params);
|
51
|
-
const res = await this.#client.request('
|
46
|
+
const res = await this.#client.request('GET', `${resource.type}`, undefined, { ...options, params: queryParams });
|
52
47
|
const r = denormalize(res);
|
53
48
|
return r;
|
54
49
|
}
|
@@ -57,7 +52,7 @@ class ResourceAdapter {
|
|
57
52
|
const queryParams = generateQueryStringParams(params, resource);
|
58
53
|
if (options?.params)
|
59
54
|
Object.assign(queryParams, options?.params);
|
60
|
-
const res = await this.#client.request('
|
55
|
+
const res = await this.#client.request('GET', `${resource.type}/${resource.id}`, undefined, { ...options, params: queryParams });
|
61
56
|
const r = denormalize(res);
|
62
57
|
return r;
|
63
58
|
}
|
@@ -66,7 +61,7 @@ class ResourceAdapter {
|
|
66
61
|
const queryParams = generateQueryStringParams(params, resource);
|
67
62
|
if (options?.params)
|
68
63
|
Object.assign(queryParams, options?.params);
|
69
|
-
const res = await this.#client.request('
|
64
|
+
const res = await this.#client.request('GET', `${resource.type}`, undefined, { ...options, params: queryParams });
|
70
65
|
const r = denormalize(res);
|
71
66
|
const meta = {
|
72
67
|
pageCount: Number(res.meta?.page_count),
|
@@ -82,7 +77,7 @@ class ResourceAdapter {
|
|
82
77
|
if (options?.params)
|
83
78
|
Object.assign(queryParams, options?.params);
|
84
79
|
const data = normalize(resource);
|
85
|
-
const res = await this.#client.request('
|
80
|
+
const res = await this.#client.request('POST', resource.type, data, { ...options, params: queryParams });
|
86
81
|
const r = denormalize(res);
|
87
82
|
return r;
|
88
83
|
}
|
@@ -92,20 +87,20 @@ class ResourceAdapter {
|
|
92
87
|
if (options?.params)
|
93
88
|
Object.assign(queryParams, options?.params);
|
94
89
|
const data = normalize(resource);
|
95
|
-
const res = await this.#client.request('
|
90
|
+
const res = await this.#client.request('PATCH', `${resource.type}/${resource.id}`, data, { ...options, params: queryParams });
|
96
91
|
const r = denormalize(res);
|
97
92
|
return r;
|
98
93
|
}
|
99
94
|
async delete(resource, options) {
|
100
95
|
debug('delete: %o, %O', resource, options || {});
|
101
|
-
await this.#client.request('
|
96
|
+
await this.#client.request('DELETE', `${resource.type}/${resource.id}`, undefined, options);
|
102
97
|
}
|
103
98
|
async fetch(resource, path, params, options) {
|
104
99
|
debug('fetch: %o, %O, %O', path, params || {}, options || {});
|
105
100
|
const queryParams = generateQueryStringParams(params, resource);
|
106
101
|
if (options?.params)
|
107
102
|
Object.assign(queryParams, options?.params);
|
108
|
-
const res = await this.#client.request('
|
103
|
+
const res = await this.#client.request('GET', path, undefined, { ...options, params: queryParams });
|
109
104
|
const r = denormalize(res);
|
110
105
|
if (Array.isArray(r)) {
|
111
106
|
const p = params;
|
@@ -134,30 +129,6 @@ class ApiResourceBase {
|
|
134
129
|
relationshipOneToMany(...ids) {
|
135
130
|
return (((ids === null) || (ids.length === 0) || (ids[0] === null)) ? [{ id: null, type: this.type() }] : ids.map(id => { return { id, type: this.type() }; }));
|
136
131
|
}
|
137
|
-
parse(resource, options) {
|
138
|
-
try {
|
139
|
-
const res = JSON.parse(resource);
|
140
|
-
// Resource type always checked
|
141
|
-
const rtype = res.data?.type;
|
142
|
-
if (rtype !== this.type())
|
143
|
-
throw new SdkError({ message: `Invalid resource type [${rtype}]`, type: ErrorType.PARSE });
|
144
|
-
// Parse options
|
145
|
-
const { ignoreSlug } = options || {};
|
146
|
-
if (!ignoreSlug) {
|
147
|
-
const links = res.data.links.self;
|
148
|
-
if (!links || !String(links).match(`^${this.resources.client.baseUrl}/${this.type()}/*`))
|
149
|
-
throw new SdkError({ message: `Resource contains invalid links [${links}]`, type: ErrorType.PARSE });
|
150
|
-
}
|
151
|
-
return denormalize(res);
|
152
|
-
}
|
153
|
-
catch (error) {
|
154
|
-
if (SdkError.isSdkError(error))
|
155
|
-
throw error;
|
156
|
-
else
|
157
|
-
throw new SdkError({ message: `Payload parse error [${error.message}]`, type: ErrorType.PARSE });
|
158
|
-
}
|
159
|
-
}
|
160
|
-
// reference, reference_origin and metadata attributes are always updatable
|
161
132
|
async update(resource, params, options) {
|
162
133
|
return this.resources.update({ ...resource, type: this.type() }, params, options);
|
163
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, };
|