@commercelayer/sdk 6.0.0-alfa.4 → 6.0.0-alfa.5
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.d.ts +6 -14
- package/lib/cjs/client.js +57 -68
- package/lib/cjs/commercelayer.d.ts +126 -125
- package/lib/cjs/commercelayer.js +277 -143
- package/lib/cjs/error.d.ts +2 -3
- package/lib/cjs/error.js +40 -33
- package/lib/cjs/fetch.d.ts +13 -0
- package/lib/cjs/fetch.js +67 -0
- package/lib/cjs/interceptor.d.ts +24 -12
- package/lib/cjs/query.d.ts +2 -1
- package/lib/cjs/query.js +8 -2
- package/lib/cjs/resource.d.ts +0 -5
- package/lib/cjs/resource.js +36 -30
- package/lib/esm/client.d.ts +6 -14
- package/lib/esm/client.js +55 -68
- package/lib/esm/commercelayer.d.ts +126 -125
- package/lib/esm/commercelayer.js +274 -264
- package/lib/esm/error.d.ts +2 -3
- package/lib/esm/error.js +40 -31
- package/lib/esm/fetch.d.ts +13 -0
- package/lib/esm/fetch.js +46 -0
- package/lib/esm/interceptor.d.ts +24 -12
- package/lib/esm/query.d.ts +2 -1
- package/lib/esm/query.js +7 -2
- package/lib/esm/resource.d.ts +0 -5
- package/lib/esm/resource.js +36 -30
- package/lib/esm/util.js +5 -5
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -12
package/lib/cjs/error.js
CHANGED
@@ -1,10 +1,7 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.handleError = exports.ErrorType = exports.ApiError = exports.SdkError = void 0;
|
7
|
-
const
|
4
|
+
const fetch_1 = require("./fetch");
|
8
5
|
var ErrorType;
|
9
6
|
(function (ErrorType) {
|
10
7
|
ErrorType["CLIENT"] = "client";
|
@@ -12,7 +9,7 @@ var ErrorType;
|
|
12
9
|
ErrorType["RESPONSE"] = "response";
|
13
10
|
ErrorType["CANCEL"] = "cancel";
|
14
11
|
ErrorType["PARSE"] = "parse";
|
15
|
-
ErrorType["
|
12
|
+
ErrorType["TIMEOUT"] = "timeout"; // Timeout error
|
16
13
|
})(ErrorType || (exports.ErrorType = ErrorType = {}));
|
17
14
|
class SdkError extends Error {
|
18
15
|
static isSdkError(error) {
|
@@ -20,8 +17,8 @@ class SdkError extends Error {
|
|
20
17
|
}
|
21
18
|
constructor(error) {
|
22
19
|
super(error.message);
|
23
|
-
this.name = SdkError.NAME;
|
24
|
-
this.type = error.type || ErrorType.
|
20
|
+
this.name = SdkError.NAME;
|
21
|
+
this.type = error.type || ErrorType.CLIENT;
|
25
22
|
}
|
26
23
|
}
|
27
24
|
exports.SdkError = SdkError;
|
@@ -33,7 +30,7 @@ class ApiError extends SdkError {
|
|
33
30
|
constructor(error) {
|
34
31
|
super({ ...error, type: ErrorType.RESPONSE });
|
35
32
|
this.errors = [];
|
36
|
-
this.name = ApiError.NAME;
|
33
|
+
this.name = ApiError.NAME;
|
37
34
|
}
|
38
35
|
first() {
|
39
36
|
return (this.errors?.length > 0) ? this.errors[0] : undefined;
|
@@ -41,36 +38,46 @@ class ApiError extends SdkError {
|
|
41
38
|
}
|
42
39
|
exports.ApiError = ApiError;
|
43
40
|
ApiError.NAME = 'ApiError';
|
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
|
+
};
|
44
50
|
const handleError = (error) => {
|
45
51
|
let sdkError = new SdkError({ message: error.message });
|
46
|
-
if (
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
sdkError = apiError;
|
58
|
-
}
|
59
|
-
else if (error.request) {
|
60
|
-
// The request was made but no response was received
|
61
|
-
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
|
62
|
-
sdkError.type = ErrorType.REQUEST;
|
63
|
-
sdkError.request = error.request;
|
64
|
-
}
|
65
|
-
else {
|
66
|
-
// Something happened in setting up the request that triggered an Error
|
67
|
-
sdkError.type = ErrorType.CLIENT;
|
68
|
-
}
|
52
|
+
if (fetch_1.FetchError.isFetchError(error)) {
|
53
|
+
// console.log('********** FetchError')
|
54
|
+
const apiError = new ApiError(sdkError);
|
55
|
+
apiError.type = ErrorType.RESPONSE;
|
56
|
+
apiError.status = error.status;
|
57
|
+
apiError.statusText = error.statusText;
|
58
|
+
apiError.code = String(apiError.status);
|
59
|
+
apiError.errors = error.errors || [];
|
60
|
+
if (!apiError.message && apiError.statusText)
|
61
|
+
apiError.message = apiError.statusText;
|
62
|
+
sdkError = apiError;
|
69
63
|
}
|
70
|
-
else if (
|
64
|
+
else if (isRequestError(error)) {
|
65
|
+
// console.log('********** RequestError')
|
66
|
+
sdkError.type = ErrorType.REQUEST;
|
67
|
+
}
|
68
|
+
else if (isCancelError(error)) {
|
69
|
+
// console.log('********** CancelError')
|
71
70
|
sdkError.type = ErrorType.CANCEL;
|
72
|
-
|
71
|
+
}
|
72
|
+
else if (isTimeoutError(error)) {
|
73
|
+
// console.log('********** TimeoutError')
|
74
|
+
sdkError.type = ErrorType.TIMEOUT;
|
75
|
+
}
|
76
|
+
else {
|
77
|
+
// console.log('********** ClientError')
|
78
|
+
sdkError.type = ErrorType.CLIENT;
|
73
79
|
sdkError.source = error;
|
80
|
+
}
|
74
81
|
throw sdkError;
|
75
82
|
};
|
76
83
|
exports.handleError = 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/cjs/fetch.js
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
5
|
+
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");
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
7
|
+
};
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
10
|
+
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");
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
12
|
+
};
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
15
|
+
};
|
16
|
+
var _FetchError_errors, _FetchError_status, _FetchError_statusText;
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
18
|
+
exports.fetchURL = exports.FetchError = void 0;
|
19
|
+
const debug_1 = __importDefault(require("./debug"));
|
20
|
+
const debug = (0, debug_1.default)('fetch');
|
21
|
+
class FetchError extends Error {
|
22
|
+
constructor(status, statusText, body) {
|
23
|
+
super(statusText);
|
24
|
+
_FetchError_errors.set(this, void 0);
|
25
|
+
_FetchError_status.set(this, void 0);
|
26
|
+
_FetchError_statusText.set(this, void 0);
|
27
|
+
__classPrivateFieldSet(this, _FetchError_status, status, "f");
|
28
|
+
__classPrivateFieldSet(this, _FetchError_statusText, statusText, "f");
|
29
|
+
if (body)
|
30
|
+
__classPrivateFieldSet(this, _FetchError_errors, body.errors, "f");
|
31
|
+
}
|
32
|
+
get errors() { return __classPrivateFieldGet(this, _FetchError_errors, "f"); }
|
33
|
+
get status() { return __classPrivateFieldGet(this, _FetchError_status, "f"); }
|
34
|
+
get statusText() { return __classPrivateFieldGet(this, _FetchError_statusText, "f"); }
|
35
|
+
}
|
36
|
+
exports.FetchError = FetchError;
|
37
|
+
_FetchError_errors = new WeakMap(), _FetchError_status = new WeakMap(), _FetchError_statusText = new WeakMap();
|
38
|
+
FetchError.isFetchError = (error) => {
|
39
|
+
return error instanceof FetchError;
|
40
|
+
};
|
41
|
+
const fetchURL = async (url, options, interceptors) => {
|
42
|
+
debug('fetch: %s, %O', url, options || {});
|
43
|
+
if (interceptors?.request?.onSuccess)
|
44
|
+
({ url, options } = await interceptors.request.onSuccess({ url, options }));
|
45
|
+
// const request: Request = new Request(url, options)
|
46
|
+
let response = await fetch(url, options);
|
47
|
+
if (response.ok) {
|
48
|
+
if (interceptors?.rawReader?.onSuccess)
|
49
|
+
await interceptors.rawReader.onSuccess(response);
|
50
|
+
if (interceptors?.response?.onSuccess)
|
51
|
+
response = await interceptors.response.onSuccess(response);
|
52
|
+
}
|
53
|
+
else {
|
54
|
+
if (interceptors?.rawReader?.onFailure)
|
55
|
+
await interceptors.rawReader.onFailure(response);
|
56
|
+
}
|
57
|
+
const responseBody = await response.json().catch(() => { });
|
58
|
+
if (!response.ok) {
|
59
|
+
let error = new FetchError(response.status, response.statusText, responseBody);
|
60
|
+
if (interceptors?.response?.onFailure)
|
61
|
+
error = await interceptors.response.onFailure(error);
|
62
|
+
if (error)
|
63
|
+
throw error;
|
64
|
+
}
|
65
|
+
return responseBody;
|
66
|
+
};
|
67
|
+
exports.fetchURL = fetchURL;
|
package/lib/cjs/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/cjs/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/cjs/query.js
CHANGED
@@ -3,9 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.isParamsList = exports.generateQueryStringParams = void 0;
|
7
|
-
const debug_1 = __importDefault(require("./debug"));
|
6
|
+
exports.generateSearchString = exports.isParamsList = exports.generateQueryStringParams = void 0;
|
8
7
|
const error_1 = require("./error");
|
8
|
+
const debug_1 = __importDefault(require("./debug"));
|
9
9
|
const debug = (0, debug_1.default)('query');
|
10
10
|
const arrayFilters = ['_any', '_all', '_in'];
|
11
11
|
const objectFilters = ['_jcont'];
|
@@ -67,3 +67,9 @@ const generateQueryStringParams = (params, res) => {
|
|
67
67
|
return qp;
|
68
68
|
};
|
69
69
|
exports.generateQueryStringParams = generateQueryStringParams;
|
70
|
+
const generateSearchString = (params, questionMark = true) => {
|
71
|
+
if (!params || (Object.keys(params).length === 0))
|
72
|
+
return '';
|
73
|
+
return `${questionMark ? '?' : ''}${Object.entries(params).map(([key, val]) => `${key}=${String(val)}`).join('&')}`;
|
74
|
+
};
|
75
|
+
exports.generateSearchString = generateSearchString;
|
package/lib/cjs/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/cjs/resource.js
CHANGED
@@ -20,7 +20,6 @@ const client_1 = __importDefault(require("./client"));
|
|
20
20
|
const jsonapi_1 = require("./jsonapi");
|
21
21
|
const query_1 = require("./query");
|
22
22
|
const config_1 = __importDefault(require("./config"));
|
23
|
-
const error_1 = require("./error");
|
24
23
|
const debug_1 = __importDefault(require("./debug"));
|
25
24
|
const debug = (0, debug_1.default)('resource');
|
26
25
|
class ListResponse extends Array {
|
@@ -45,7 +44,6 @@ class ResourceAdapter {
|
|
45
44
|
__classPrivateFieldSet(this, _ResourceAdapter_client, client_1.default.create(config), "f");
|
46
45
|
this.localConfig(config);
|
47
46
|
}
|
48
|
-
get interceptors() { return __classPrivateFieldGet(this, _ResourceAdapter_client, "f").interceptors; }
|
49
47
|
localConfig(config) {
|
50
48
|
// if (typeof config.xyz !== 'undefined') this.#config.xyz = config.xyz
|
51
49
|
}
|
@@ -65,7 +63,7 @@ class ResourceAdapter {
|
|
65
63
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
66
64
|
if (options?.params)
|
67
65
|
Object.assign(queryParams, options?.params);
|
68
|
-
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
66
|
+
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('GET', `${resource.type}`, undefined, { ...options, params: queryParams });
|
69
67
|
const r = (0, jsonapi_1.denormalize)(res);
|
70
68
|
return r;
|
71
69
|
}
|
@@ -74,7 +72,7 @@ class ResourceAdapter {
|
|
74
72
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
75
73
|
if (options?.params)
|
76
74
|
Object.assign(queryParams, options?.params);
|
77
|
-
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
75
|
+
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('GET', `${resource.type}/${resource.id}`, undefined, { ...options, params: queryParams });
|
78
76
|
const r = (0, jsonapi_1.denormalize)(res);
|
79
77
|
return r;
|
80
78
|
}
|
@@ -83,7 +81,7 @@ class ResourceAdapter {
|
|
83
81
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
84
82
|
if (options?.params)
|
85
83
|
Object.assign(queryParams, options?.params);
|
86
|
-
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
84
|
+
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('GET', `${resource.type}`, undefined, { ...options, params: queryParams });
|
87
85
|
const r = (0, jsonapi_1.denormalize)(res);
|
88
86
|
const meta = {
|
89
87
|
pageCount: Number(res.meta?.page_count),
|
@@ -99,7 +97,7 @@ class ResourceAdapter {
|
|
99
97
|
if (options?.params)
|
100
98
|
Object.assign(queryParams, options?.params);
|
101
99
|
const data = (0, jsonapi_1.normalize)(resource);
|
102
|
-
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
100
|
+
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('POST', resource.type, data, { ...options, params: queryParams });
|
103
101
|
const r = (0, jsonapi_1.denormalize)(res);
|
104
102
|
return r;
|
105
103
|
}
|
@@ -109,20 +107,20 @@ class ResourceAdapter {
|
|
109
107
|
if (options?.params)
|
110
108
|
Object.assign(queryParams, options?.params);
|
111
109
|
const data = (0, jsonapi_1.normalize)(resource);
|
112
|
-
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
110
|
+
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('PATCH', `${resource.type}/${resource.id}`, data, { ...options, params: queryParams });
|
113
111
|
const r = (0, jsonapi_1.denormalize)(res);
|
114
112
|
return r;
|
115
113
|
}
|
116
114
|
async delete(resource, options) {
|
117
115
|
debug('delete: %o, %O', resource, options || {});
|
118
|
-
await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
116
|
+
await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('DELETE', `${resource.type}/${resource.id}`, undefined, options);
|
119
117
|
}
|
120
118
|
async fetch(resource, path, params, options) {
|
121
119
|
debug('fetch: %o, %O, %O', path, params || {}, options || {});
|
122
120
|
const queryParams = (0, query_1.generateQueryStringParams)(params, resource);
|
123
121
|
if (options?.params)
|
124
122
|
Object.assign(queryParams, options?.params);
|
125
|
-
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('
|
123
|
+
const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('GET', path, undefined, { ...options, params: queryParams });
|
126
124
|
const r = (0, jsonapi_1.denormalize)(res);
|
127
125
|
if (Array.isArray(r)) {
|
128
126
|
const p = params;
|
@@ -143,6 +141,7 @@ class ApiResourceBase {
|
|
143
141
|
constructor(adapter) {
|
144
142
|
debug('new resource instance: %s', this.type());
|
145
143
|
this.resources = adapter;
|
144
|
+
console.log('CONSTRUCTOR ' + this.type());
|
146
145
|
}
|
147
146
|
relationshipOneToOne(id) {
|
148
147
|
return (((id === null) || (typeof id === 'string')) ? { id, type: this.type() } : { id: id.id, type: this.type() });
|
@@ -150,29 +149,36 @@ class ApiResourceBase {
|
|
150
149
|
relationshipOneToMany(...ids) {
|
151
150
|
return (((ids === null) || (ids.length === 0) || (ids[0] === null)) ? [{ id: null, type: this.type() }] : ids.map(id => { return { id, type: this.type() }; }));
|
152
151
|
}
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
152
|
+
/*
|
153
|
+
parse(resource: string, options?: { ignoreSlug?: boolean }): R | R[] {
|
154
|
+
|
155
|
+
try {
|
156
|
+
|
157
|
+
const res = JSON.parse(resource)
|
158
|
+
|
159
|
+
// Resource type always checked
|
160
|
+
const rtype = res.data?.type
|
161
|
+
if (rtype !== this.type()) throw new SdkError({ message: `Invalid resource type [${rtype}]`, type: ErrorType.PARSE })
|
162
|
+
|
163
|
+
// Parse options
|
164
|
+
const { ignoreSlug } = options || {}
|
165
|
+
|
166
|
+
if (!ignoreSlug) {
|
167
|
+
const links = res.data.links.self
|
168
|
+
if (!links || !String(links).match(`^${this.resources.client.baseUrl}/${this.type()}/*`))
|
169
|
+
throw new SdkError({ message: `Resource contains invalid links [${links}]`, type: ErrorType.PARSE })
|
170
|
+
}
|
171
|
+
|
172
|
+
|
173
|
+
return denormalize<R>(res as DocWithData)
|
174
|
+
|
175
|
+
} catch (error: any) {
|
176
|
+
if (SdkError.isSdkError(error)) throw error
|
177
|
+
else throw new SdkError({ message: `Payload parse error [${error.message}]`, type: ErrorType.PARSE })
|
166
178
|
}
|
167
|
-
|
179
|
+
|
168
180
|
}
|
169
|
-
|
170
|
-
if (error_1.SdkError.isSdkError(error))
|
171
|
-
throw error;
|
172
|
-
else
|
173
|
-
throw new error_1.SdkError({ message: `Payload parse error [${error.message}]`, type: error_1.ErrorType.PARSE });
|
174
|
-
}
|
175
|
-
}
|
181
|
+
*/
|
176
182
|
// reference, reference_origin and metadata attributes are always updatable
|
177
183
|
async update(resource, params, options) {
|
178
184
|
return this.resources.update({ ...resource, type: this.type() }, params, options);
|
package/lib/esm/client.d.ts
CHANGED
@@ -1,19 +1,11 @@
|
|
1
|
-
import type { AxiosAdapter, AxiosProxyConfig, Method } from 'axios';
|
2
1
|
import type { InterceptorManager } from './interceptor';
|
3
|
-
type
|
4
|
-
type Adapter = AxiosAdapter;
|
2
|
+
import { type FetchResponse } from './fetch';
|
5
3
|
type RequestParams = Record<string, string | number | boolean>;
|
6
4
|
type RequestHeaders = Record<string, string>;
|
7
5
|
type RequestConfig = {
|
8
6
|
timeout?: number;
|
9
7
|
params?: RequestParams;
|
10
|
-
httpAgent?: any;
|
11
|
-
httpsAgent?: any;
|
12
|
-
proxy?: ProxyConfig;
|
13
8
|
headers?: RequestHeaders;
|
14
|
-
};
|
15
|
-
type RequestConfigExtra = {
|
16
|
-
adapter?: Adapter;
|
17
9
|
userAgent?: string;
|
18
10
|
};
|
19
11
|
type ApiConfig = {
|
@@ -21,18 +13,18 @@ type ApiConfig = {
|
|
21
13
|
domain?: string;
|
22
14
|
accessToken: string;
|
23
15
|
};
|
24
|
-
type ApiClientInitConfig = ApiConfig & RequestConfig
|
16
|
+
type ApiClientInitConfig = ApiConfig & RequestConfig;
|
25
17
|
type ApiClientConfig = Partial<ApiClientInitConfig>;
|
18
|
+
export type Method = 'GET' | 'DELETE' | 'POST' | 'PUT' | 'PATCH';
|
26
19
|
declare class ApiClient {
|
27
20
|
#private;
|
28
21
|
static create(options: ApiClientInitConfig): ApiClient;
|
29
|
-
baseUrl: string;
|
30
|
-
interceptors: InterceptorManager;
|
31
22
|
private constructor();
|
23
|
+
get interceptors(): InterceptorManager;
|
24
|
+
get requestHeaders(): RequestHeaders;
|
32
25
|
config(config: ApiClientConfig): ApiClient;
|
33
26
|
userAgent(userAgent: string): ApiClient;
|
34
|
-
|
35
|
-
request(method: Method, path: string, body?: any, options?: ApiClientConfig): Promise<any>;
|
27
|
+
request(method: Method, path: string, body?: any, options?: ApiClientConfig): Promise<FetchResponse>;
|
36
28
|
private customHeaders;
|
37
29
|
}
|
38
30
|
export default ApiClient;
|