@commercelayer/provisioning-sdk 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +415 -0
- package/lib/cjs/api.d.ts +41 -0
- package/lib/cjs/api.js +37 -0
- package/lib/cjs/client.d.ts +39 -0
- package/lib/cjs/client.js +121 -0
- package/lib/cjs/commercelayer.d.ts +36 -0
- package/lib/cjs/commercelayer.js +126 -0
- package/lib/cjs/common.d.ts +5 -0
- package/lib/cjs/common.js +12 -0
- package/lib/cjs/config.d.ts +12 -0
- package/lib/cjs/config.js +16 -0
- package/lib/cjs/debug.d.ts +3 -0
- package/lib/cjs/debug.js +38 -0
- package/lib/cjs/error.d.ts +31 -0
- package/lib/cjs/error.js +77 -0
- package/lib/cjs/index.d.ts +7 -0
- package/lib/cjs/index.js +13 -0
- package/lib/cjs/interceptor.d.ts +25 -0
- package/lib/cjs/interceptor.js +2 -0
- package/lib/cjs/jsonapi.d.ts +5 -0
- package/lib/cjs/jsonapi.js +89 -0
- package/lib/cjs/model.d.ts +10 -0
- package/lib/cjs/model.js +3 -0
- package/lib/cjs/query.d.ts +17 -0
- package/lib/cjs/query.js +52 -0
- package/lib/cjs/resource.d.ts +86 -0
- package/lib/cjs/resource.js +191 -0
- package/lib/cjs/resources/api_credentials.d.ts +55 -0
- package/lib/cjs/resources/api_credentials.js +33 -0
- package/lib/cjs/resources/memberships.d.ts +46 -0
- package/lib/cjs/resources/memberships.js +37 -0
- package/lib/cjs/resources/organizations.d.ts +78 -0
- package/lib/cjs/resources/organizations.js +34 -0
- package/lib/cjs/resources/permissions.d.ts +52 -0
- package/lib/cjs/resources/permissions.js +34 -0
- package/lib/cjs/resources/roles.d.ts +47 -0
- package/lib/cjs/resources/roles.js +42 -0
- package/lib/cjs/resources/user.d.ts +29 -0
- package/lib/cjs/resources/user.js +19 -0
- package/lib/cjs/resources/versions.d.ts +22 -0
- package/lib/cjs/resources/versions.js +16 -0
- package/lib/cjs/static.d.ts +9 -0
- package/lib/cjs/static.js +45 -0
- package/lib/cjs/util.d.ts +9 -0
- package/lib/cjs/util.js +15 -0
- package/lib/esm/api.d.ts +41 -0
- package/lib/esm/api.js +24 -0
- package/lib/esm/client.d.ts +39 -0
- package/lib/esm/client.js +116 -0
- package/lib/esm/commercelayer.d.ts +36 -0
- package/lib/esm/commercelayer.js +97 -0
- package/lib/esm/common.d.ts +5 -0
- package/lib/esm/common.js +8 -0
- package/lib/esm/config.d.ts +12 -0
- package/lib/esm/config.js +14 -0
- package/lib/esm/debug.d.ts +3 -0
- package/lib/esm/debug.js +36 -0
- package/lib/esm/error.d.ts +31 -0
- package/lib/esm/error.js +69 -0
- package/lib/esm/index.d.ts +7 -0
- package/lib/esm/index.js +4 -0
- package/lib/esm/interceptor.d.ts +25 -0
- package/lib/esm/interceptor.js +1 -0
- package/lib/esm/jsonapi.d.ts +5 -0
- package/lib/esm/jsonapi.js +82 -0
- package/lib/esm/model.d.ts +10 -0
- package/lib/esm/model.js +2 -0
- package/lib/esm/query.d.ts +17 -0
- package/lib/esm/query.js +45 -0
- package/lib/esm/resource.d.ts +86 -0
- package/lib/esm/resource.js +184 -0
- package/lib/esm/resources/api_credentials.d.ts +55 -0
- package/lib/esm/resources/api_credentials.js +31 -0
- package/lib/esm/resources/memberships.d.ts +46 -0
- package/lib/esm/resources/memberships.js +35 -0
- package/lib/esm/resources/organizations.d.ts +78 -0
- package/lib/esm/resources/organizations.js +32 -0
- package/lib/esm/resources/permissions.d.ts +52 -0
- package/lib/esm/resources/permissions.js +32 -0
- package/lib/esm/resources/roles.d.ts +47 -0
- package/lib/esm/resources/roles.js +40 -0
- package/lib/esm/resources/user.d.ts +29 -0
- package/lib/esm/resources/user.js +17 -0
- package/lib/esm/resources/versions.d.ts +22 -0
- package/lib/esm/resources/versions.js +14 -0
- package/lib/esm/static.d.ts +9 -0
- package/lib/esm/static.js +19 -0
- package/lib/esm/util.d.ts +9 -0
- package/lib/esm/util.js +11 -0
- package/lib/tsconfig.esm.tsbuildinfo +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +69 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as api from './api';
|
|
2
|
+
import type { ApiError } from './error';
|
|
3
|
+
import type { ErrorInterceptor, InterceptorType, RawResponseReader, RequestInterceptor, ResponseInterceptor } from './interceptor';
|
|
4
|
+
import { type ResourcesInitConfig } from './resource';
|
|
5
|
+
declare const OPEN_API_SCHEMA_VERSION = "1.0.0";
|
|
6
|
+
export { OPEN_API_SCHEMA_VERSION };
|
|
7
|
+
type SdkConfig = {};
|
|
8
|
+
type CommerceLayerInitConfig = SdkConfig & ResourcesInitConfig;
|
|
9
|
+
type CommerceLayerConfig = Partial<CommerceLayerInitConfig>;
|
|
10
|
+
declare class CommerceLayerProvisioningClient {
|
|
11
|
+
#private;
|
|
12
|
+
readonly openApiSchemaVersion = "1.0.0";
|
|
13
|
+
api_credentials: api.ApiCredentials;
|
|
14
|
+
memberships: api.Memberships;
|
|
15
|
+
organizations: api.Organizations;
|
|
16
|
+
permissions: api.Permissions;
|
|
17
|
+
roles: api.Roles;
|
|
18
|
+
user: api.Users;
|
|
19
|
+
versions: api.Versions;
|
|
20
|
+
constructor(config: CommerceLayerInitConfig);
|
|
21
|
+
private localConfig;
|
|
22
|
+
config(config: CommerceLayerConfig): CommerceLayerProvisioningClient;
|
|
23
|
+
resources(): readonly string[];
|
|
24
|
+
isApiError(error: any): error is ApiError;
|
|
25
|
+
addRequestInterceptor(onFulfilled?: RequestInterceptor, onRejected?: ErrorInterceptor): number;
|
|
26
|
+
addResponseInterceptor(onFulfilled?: ResponseInterceptor, onRejected?: ErrorInterceptor): number;
|
|
27
|
+
removeInterceptor(type: InterceptorType, id: number): void;
|
|
28
|
+
addRawResponseReader(options?: {
|
|
29
|
+
headers: boolean;
|
|
30
|
+
}): RawResponseReader;
|
|
31
|
+
removeRawResponseReader(reader: number | RawResponseReader): void;
|
|
32
|
+
}
|
|
33
|
+
declare const CommerceLayerProvisioning: (config: CommerceLayerInitConfig) => CommerceLayerProvisioningClient;
|
|
34
|
+
export default CommerceLayerProvisioning;
|
|
35
|
+
export { CommerceLayerProvisioning };
|
|
36
|
+
export type { CommerceLayerProvisioningClient, CommerceLayerConfig, CommerceLayerInitConfig };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
26
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
27
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
28
|
+
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");
|
|
29
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
30
|
+
};
|
|
31
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
32
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
33
|
+
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");
|
|
34
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
35
|
+
};
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
var _CommerceLayerProvisioningClient_adapter;
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.CommerceLayerProvisioning = exports.OPEN_API_SCHEMA_VERSION = void 0;
|
|
42
|
+
const api = __importStar(require("./api"));
|
|
43
|
+
const static_1 = require("./static");
|
|
44
|
+
const resource_1 = __importDefault(require("./resource"));
|
|
45
|
+
const debug_1 = __importDefault(require("./debug"));
|
|
46
|
+
const debug = (0, debug_1.default)('commercelayer');
|
|
47
|
+
// Autogenerated schema version number, do not remove this line
|
|
48
|
+
const OPEN_API_SCHEMA_VERSION = '1.0.0';
|
|
49
|
+
exports.OPEN_API_SCHEMA_VERSION = OPEN_API_SCHEMA_VERSION;
|
|
50
|
+
class CommerceLayerProvisioningClient {
|
|
51
|
+
// ##__CL_RESOURCES_DEF_STOP__##
|
|
52
|
+
constructor(config) {
|
|
53
|
+
// static get openApiSchemaVersion(): string { return OPEN_API_SCHEMA_VERSION }
|
|
54
|
+
this.openApiSchemaVersion = OPEN_API_SCHEMA_VERSION;
|
|
55
|
+
_CommerceLayerProvisioningClient_adapter.set(this, void 0);
|
|
56
|
+
debug('new commercelayer provisioning instance %O', config);
|
|
57
|
+
__classPrivateFieldSet(this, _CommerceLayerProvisioningClient_adapter, new resource_1.default(config), "f");
|
|
58
|
+
// this.#environment = 'test'
|
|
59
|
+
// ##__CL_RESOURCES_INIT_START__##
|
|
60
|
+
// ##__CL_RESOURCES_INIT_TEMPLATE:: ##__TAB__####__TAB__##this.##__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)
|
|
61
|
+
this.api_credentials = new api.ApiCredentials(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
62
|
+
this.memberships = new api.Memberships(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
63
|
+
this.organizations = new api.Organizations(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
64
|
+
this.permissions = new api.Permissions(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
65
|
+
this.roles = new api.Roles(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
66
|
+
this.user = new api.Users(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
67
|
+
this.versions = new api.Versions(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
|
|
68
|
+
// ##__CL_RESOURCES_INIT_STOP__##
|
|
69
|
+
}
|
|
70
|
+
// get environment(): ApiMode { return this.#environment }
|
|
71
|
+
localConfig(config /* & { organization?: string } */) {
|
|
72
|
+
}
|
|
73
|
+
config(config) {
|
|
74
|
+
debug('config %o', config);
|
|
75
|
+
// CommerceLayer config
|
|
76
|
+
this.localConfig(config);
|
|
77
|
+
// ResourceAdapter config
|
|
78
|
+
// To rebuild baseUrl in client in case only the domain is defined
|
|
79
|
+
// if (!config.organization) config.organization = this.currentOrganization
|
|
80
|
+
__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f").config(config);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
resources() {
|
|
84
|
+
return static_1.CommerceLayerProvisioningStatic.resources();
|
|
85
|
+
}
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
|
|
87
|
+
isApiError(error) {
|
|
88
|
+
return static_1.CommerceLayerProvisioningStatic.isApiError(error);
|
|
89
|
+
}
|
|
90
|
+
addRequestInterceptor(onFulfilled, onRejected) {
|
|
91
|
+
return __classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f").interceptors.request.use(onFulfilled, onRejected);
|
|
92
|
+
}
|
|
93
|
+
addResponseInterceptor(onFulfilled, onRejected) {
|
|
94
|
+
return __classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f").interceptors.response.use(onFulfilled, onRejected);
|
|
95
|
+
}
|
|
96
|
+
removeInterceptor(type, id) {
|
|
97
|
+
__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f").interceptors[type].eject(id);
|
|
98
|
+
}
|
|
99
|
+
addRawResponseReader(options) {
|
|
100
|
+
const reader = {
|
|
101
|
+
id: undefined,
|
|
102
|
+
rawResponse: undefined,
|
|
103
|
+
headers: undefined,
|
|
104
|
+
};
|
|
105
|
+
function rawResponseInterceptor(response) {
|
|
106
|
+
reader.rawResponse = response === null || response === void 0 ? void 0 : response.data;
|
|
107
|
+
if (options === null || options === void 0 ? void 0 : options.headers)
|
|
108
|
+
reader.headers = response.headers;
|
|
109
|
+
return response;
|
|
110
|
+
}
|
|
111
|
+
const interceptor = this.addResponseInterceptor(rawResponseInterceptor);
|
|
112
|
+
reader.id = interceptor;
|
|
113
|
+
return reader;
|
|
114
|
+
}
|
|
115
|
+
removeRawResponseReader(reader) {
|
|
116
|
+
const id = (typeof reader === 'number') ? reader : reader === null || reader === void 0 ? void 0 : reader.id;
|
|
117
|
+
if (id && (id >= 0))
|
|
118
|
+
this.removeInterceptor('response', id);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
_CommerceLayerProvisioningClient_adapter = new WeakMap();
|
|
122
|
+
const CommerceLayerProvisioning = (config) => {
|
|
123
|
+
return new CommerceLayerProvisioningClient(config);
|
|
124
|
+
};
|
|
125
|
+
exports.CommerceLayerProvisioning = CommerceLayerProvisioning;
|
|
126
|
+
exports.default = CommerceLayerProvisioning;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ResourceId, ResourceType } from './resource';
|
|
2
|
+
declare const isResourceId: (resource: any) => resource is ResourceId;
|
|
3
|
+
declare const isResourceType: (resource: any) => resource is ResourceType;
|
|
4
|
+
export { isResourceId, isResourceType };
|
|
5
|
+
export type ObjectType = Record<string, any>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isResourceType = exports.isResourceId = void 0;
|
|
4
|
+
const api_1 = require("./api");
|
|
5
|
+
const isResourceId = (resource) => {
|
|
6
|
+
return ((resource === null || resource === void 0 ? void 0 : resource.type) && resource.id) && api_1.resourceList.includes(resource.type);
|
|
7
|
+
};
|
|
8
|
+
exports.isResourceId = isResourceId;
|
|
9
|
+
const isResourceType = (resource) => {
|
|
10
|
+
return resource && (typeof resource.type !== 'undefined') && resource.type && api_1.resourceList.includes(resource.type);
|
|
11
|
+
};
|
|
12
|
+
exports.isResourceType = isResourceType;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare const config: {
|
|
2
|
+
readonly default: {
|
|
3
|
+
readonly domain: "commercelayer.io";
|
|
4
|
+
readonly pageNumber: 1;
|
|
5
|
+
readonly pageSize: 10;
|
|
6
|
+
};
|
|
7
|
+
readonly client: {
|
|
8
|
+
readonly timeout: 15000;
|
|
9
|
+
readonly requiredAttributes: readonly ["accessToken"];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export default config;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// export type ApiMode = 'test' | 'live'
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const config = {
|
|
5
|
+
default: {
|
|
6
|
+
// environment: 'test',
|
|
7
|
+
domain: 'commercelayer.io',
|
|
8
|
+
pageNumber: 1,
|
|
9
|
+
pageSize: 10,
|
|
10
|
+
},
|
|
11
|
+
client: {
|
|
12
|
+
timeout: 15000,
|
|
13
|
+
requiredAttributes: ['accessToken'],
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.default = config;
|
package/lib/cjs/debug.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* Nope debugger */
|
|
4
|
+
const debuggerFunction = (_pattern, ..._args) => {
|
|
5
|
+
// console.log(_pattern)
|
|
6
|
+
};
|
|
7
|
+
let debuggerFactory = (_namespace) => debuggerFunction;
|
|
8
|
+
/* Try loading 'debug' module */
|
|
9
|
+
try {
|
|
10
|
+
const debugModule = require('debug');
|
|
11
|
+
if (debugModule && (typeof debugModule === 'function'))
|
|
12
|
+
debuggerFactory = debugModule;
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
//
|
|
16
|
+
}
|
|
17
|
+
const debugPrefix = 'clsdk';
|
|
18
|
+
/* Retrieve the name of the caller 'module' */
|
|
19
|
+
/*
|
|
20
|
+
const caller = (): string => {
|
|
21
|
+
|
|
22
|
+
const err = new Error()
|
|
23
|
+
|
|
24
|
+
Error.prepareStackTrace = (_, stack) => stack
|
|
25
|
+
const stack = err.stack as unknown as NodeJS.CallSite[]
|
|
26
|
+
Error.prepareStackTrace = undefined
|
|
27
|
+
|
|
28
|
+
const fileName = stack[2].getFileName() || '/'
|
|
29
|
+
|
|
30
|
+
return fileName.replace(/^.*[\\/]/, '').replace('.ts', '')
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
*/
|
|
34
|
+
/* Return a debugger for the defined namespace */
|
|
35
|
+
const debug = (namespace) => {
|
|
36
|
+
return debuggerFactory(`${debugPrefix}:${namespace}`);
|
|
37
|
+
};
|
|
38
|
+
exports.default = debug;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
declare enum ErrorType {
|
|
2
|
+
CLIENT = "client",
|
|
3
|
+
REQUEST = "request",
|
|
4
|
+
RESPONSE = "response",
|
|
5
|
+
CANCEL = "cancel",
|
|
6
|
+
PARSE = "parse",
|
|
7
|
+
GENERIC = "generic"
|
|
8
|
+
}
|
|
9
|
+
declare class SdkError extends Error {
|
|
10
|
+
static NAME: string;
|
|
11
|
+
static isSdkError(error: any): error is ApiError;
|
|
12
|
+
type: string;
|
|
13
|
+
code?: string;
|
|
14
|
+
source?: Error;
|
|
15
|
+
request?: any;
|
|
16
|
+
constructor(error: {
|
|
17
|
+
message: string;
|
|
18
|
+
type?: ErrorType;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
declare class ApiError extends SdkError {
|
|
22
|
+
static NAME: string;
|
|
23
|
+
static isApiError(error: any): error is ApiError;
|
|
24
|
+
errors: any[];
|
|
25
|
+
status?: number;
|
|
26
|
+
statusText?: string;
|
|
27
|
+
constructor(error: SdkError);
|
|
28
|
+
first(): any;
|
|
29
|
+
}
|
|
30
|
+
declare const handleError: (error: Error) => never;
|
|
31
|
+
export { SdkError, ApiError, ErrorType, handleError };
|
package/lib/cjs/error.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.handleError = exports.ErrorType = exports.ApiError = exports.SdkError = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
var ErrorType;
|
|
9
|
+
(function (ErrorType) {
|
|
10
|
+
ErrorType["CLIENT"] = "client";
|
|
11
|
+
ErrorType["REQUEST"] = "request";
|
|
12
|
+
ErrorType["RESPONSE"] = "response";
|
|
13
|
+
ErrorType["CANCEL"] = "cancel";
|
|
14
|
+
ErrorType["PARSE"] = "parse";
|
|
15
|
+
ErrorType["GENERIC"] = "generic";
|
|
16
|
+
})(ErrorType || (exports.ErrorType = ErrorType = {}));
|
|
17
|
+
class SdkError extends Error {
|
|
18
|
+
static isSdkError(error) {
|
|
19
|
+
return error && [SdkError.NAME, ApiError.NAME].includes(error.name) && Object.values(ErrorType).includes(error.type);
|
|
20
|
+
}
|
|
21
|
+
constructor(error) {
|
|
22
|
+
super(error.message);
|
|
23
|
+
this.name = SdkError.NAME; // this.constructor.name
|
|
24
|
+
this.type = error.type || ErrorType.GENERIC;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.SdkError = SdkError;
|
|
28
|
+
SdkError.NAME = 'SdkError';
|
|
29
|
+
class ApiError extends SdkError {
|
|
30
|
+
static isApiError(error) {
|
|
31
|
+
return SdkError.isSdkError(error) && (error.name === ApiError.NAME) && (error.type === ErrorType.RESPONSE);
|
|
32
|
+
}
|
|
33
|
+
constructor(error) {
|
|
34
|
+
super({ ...error, type: ErrorType.RESPONSE });
|
|
35
|
+
this.errors = [];
|
|
36
|
+
this.name = ApiError.NAME; // this.constructor.name
|
|
37
|
+
}
|
|
38
|
+
first() {
|
|
39
|
+
var _a;
|
|
40
|
+
return (((_a = this.errors) === null || _a === void 0 ? void 0 : _a.length) > 0) ? this.errors[0] : undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.ApiError = ApiError;
|
|
44
|
+
ApiError.NAME = 'ApiError';
|
|
45
|
+
const handleError = (error) => {
|
|
46
|
+
let sdkError = new SdkError({ message: error.message });
|
|
47
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
48
|
+
if (error.response) {
|
|
49
|
+
// The request was made and the server responded with a status code that falls out of the range of 2xx
|
|
50
|
+
const apiError = new ApiError(sdkError);
|
|
51
|
+
apiError.type = ErrorType.RESPONSE;
|
|
52
|
+
apiError.status = error.response.status;
|
|
53
|
+
apiError.statusText = error.response.statusText;
|
|
54
|
+
apiError.code = String(apiError.status);
|
|
55
|
+
apiError.errors = error.response.data.errors;
|
|
56
|
+
if (!apiError.message && apiError.statusText)
|
|
57
|
+
apiError.message = apiError.statusText;
|
|
58
|
+
sdkError = apiError;
|
|
59
|
+
}
|
|
60
|
+
else if (error.request) {
|
|
61
|
+
// The request was made but no response was received
|
|
62
|
+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
|
|
63
|
+
sdkError.type = ErrorType.REQUEST;
|
|
64
|
+
sdkError.request = error.request;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// Something happened in setting up the request that triggered an Error
|
|
68
|
+
sdkError.type = ErrorType.CLIENT;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (axios_1.default.isCancel(error))
|
|
72
|
+
sdkError.type = ErrorType.CANCEL;
|
|
73
|
+
else
|
|
74
|
+
sdkError.source = error;
|
|
75
|
+
throw sdkError;
|
|
76
|
+
};
|
|
77
|
+
exports.handleError = handleError;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default, CommerceLayerProvisioning } from './commercelayer';
|
|
2
|
+
export type { CommerceLayerProvisioningClient } from './commercelayer';
|
|
3
|
+
export { CommerceLayerProvisioningStatic } from './static';
|
|
4
|
+
export type { QueryParamsRetrieve, QueryParamsList, QueryParams } from './query';
|
|
5
|
+
export type * from './model';
|
|
6
|
+
export type { RequestObj, ResponseObj, ErrorObj, HeadersObj } from './interceptor';
|
|
7
|
+
export type { SdkError, ApiError, ErrorType } from './error';
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CommerceLayerProvisioningStatic = exports.CommerceLayerProvisioning = exports.default = void 0;
|
|
7
|
+
// SDK
|
|
8
|
+
var commercelayer_1 = require("./commercelayer");
|
|
9
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(commercelayer_1).default; } });
|
|
10
|
+
Object.defineProperty(exports, "CommerceLayerProvisioning", { enumerable: true, get: function () { return commercelayer_1.CommerceLayerProvisioning; } });
|
|
11
|
+
// Commerce Layer static functions
|
|
12
|
+
var static_1 = require("./static");
|
|
13
|
+
Object.defineProperty(exports, "CommerceLayerProvisioningStatic", { enumerable: true, get: function () { return static_1.CommerceLayerProvisioningStatic; } });
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AxiosError, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders, RawAxiosResponseHeaders } from 'axios';
|
|
2
|
+
type InterceptorManager = {
|
|
3
|
+
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
|
4
|
+
response: AxiosInterceptorManager<any>;
|
|
5
|
+
};
|
|
6
|
+
type RequestObj = AxiosRequestConfig;
|
|
7
|
+
type RequestInterceptor = (request: RequestObj) => RequestObj | Promise<RequestObj>;
|
|
8
|
+
type ResponseObj = AxiosResponse;
|
|
9
|
+
type ResponseInterceptor = (response: ResponseObj) => ResponseObj;
|
|
10
|
+
type ApiHeadersList = 'x-ratelimit-limit' | 'x-ratelimit-count' | 'x-ratelimit-period' | 'x-ratelimit-interval' | 'x-ratelimit-remaining';
|
|
11
|
+
type ApiHeaders = {
|
|
12
|
+
[key in ApiHeadersList]: string | number | boolean;
|
|
13
|
+
};
|
|
14
|
+
type HeadersObj = (AxiosResponseHeaders | RawAxiosResponseHeaders) & ApiHeaders;
|
|
15
|
+
type ErrorObj = AxiosError;
|
|
16
|
+
type ErrorInterceptor = (error: ErrorObj) => ErrorObj;
|
|
17
|
+
type InterceptorType = 'request' | 'response';
|
|
18
|
+
export type { InterceptorManager, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, InterceptorType };
|
|
19
|
+
export type { RequestObj, ResponseObj, ErrorObj, HeadersObj };
|
|
20
|
+
type RawResponseReader = {
|
|
21
|
+
id: number | undefined;
|
|
22
|
+
rawResponse: ResponseObj | undefined;
|
|
23
|
+
headers: HeadersObj | undefined;
|
|
24
|
+
};
|
|
25
|
+
export type { RawResponseReader };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DocWithData, ResourceObject as JSONAPIObject } from 'jsonapi-typescript';
|
|
2
|
+
import type { ResourceCreate, ResourceUpdate, ResourceId, ResourceType, Resource } from './resource';
|
|
3
|
+
declare const denormalize: <R extends Resource>(response: DocWithData) => R | R[];
|
|
4
|
+
declare const normalize: (resource: (ResourceCreate & ResourceType) | (ResourceUpdate & ResourceId)) => JSONAPIObject;
|
|
5
|
+
export { denormalize, normalize };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.normalize = exports.denormalize = void 0;
|
|
7
|
+
const common_1 = require("./common");
|
|
8
|
+
const debug_1 = __importDefault(require("./debug"));
|
|
9
|
+
const debug = (0, debug_1.default)('jsonapi');
|
|
10
|
+
// DENORMALIZATION
|
|
11
|
+
const denormalize = (response) => {
|
|
12
|
+
let denormalizedResponse;
|
|
13
|
+
if (response.links)
|
|
14
|
+
delete response.links;
|
|
15
|
+
const data = response.data;
|
|
16
|
+
const included = response.included;
|
|
17
|
+
if (!data)
|
|
18
|
+
denormalizedResponse = data;
|
|
19
|
+
else {
|
|
20
|
+
if (Array.isArray(data))
|
|
21
|
+
denormalizedResponse = data.map(res => denormalizeResource(res, included));
|
|
22
|
+
else
|
|
23
|
+
denormalizedResponse = denormalizeResource(data, included);
|
|
24
|
+
}
|
|
25
|
+
return denormalizedResponse;
|
|
26
|
+
};
|
|
27
|
+
exports.denormalize = denormalize;
|
|
28
|
+
const findIncluded = (rel, included = []) => {
|
|
29
|
+
const inc = included.find(inc => {
|
|
30
|
+
return (rel.id === inc.id) && (rel.type === inc.type);
|
|
31
|
+
});
|
|
32
|
+
return inc || rel;
|
|
33
|
+
};
|
|
34
|
+
const denormalizeResource = (res, included) => {
|
|
35
|
+
debug('denormalize resource: %O, %o', res, included || {});
|
|
36
|
+
if (!res)
|
|
37
|
+
return res;
|
|
38
|
+
const resource = {
|
|
39
|
+
id: res.id,
|
|
40
|
+
type: res.type,
|
|
41
|
+
...res.attributes,
|
|
42
|
+
};
|
|
43
|
+
if (res.relationships)
|
|
44
|
+
Object.keys(res.relationships).forEach(key => {
|
|
45
|
+
const rel = res.relationships[key].data;
|
|
46
|
+
if (rel) {
|
|
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);
|
|
51
|
+
}
|
|
52
|
+
else if (rel === null)
|
|
53
|
+
resource[key] = null;
|
|
54
|
+
});
|
|
55
|
+
debug('denormalized resource: %O', resource);
|
|
56
|
+
return resource;
|
|
57
|
+
};
|
|
58
|
+
// NORMALIZATION
|
|
59
|
+
const normalize = (resource) => {
|
|
60
|
+
debug('normalize resource: %O', resource);
|
|
61
|
+
const attributes = {};
|
|
62
|
+
const relationships = {};
|
|
63
|
+
for (const field in resource) {
|
|
64
|
+
if (['type', 'id'].includes(field))
|
|
65
|
+
continue;
|
|
66
|
+
const value = resource[field];
|
|
67
|
+
if (Array.isArray(value) && (value.length === 1) && (0, common_1.isResourceType)(value[0]) && (value[0].id === null)) {
|
|
68
|
+
relationships[field] = { data: [] };
|
|
69
|
+
}
|
|
70
|
+
else if (value && (0, common_1.isResourceType)(value) && (value.id === null)) {
|
|
71
|
+
relationships[field] = { data: null };
|
|
72
|
+
}
|
|
73
|
+
else if (value && ((0, common_1.isResourceId)(value) || (Array.isArray(value) && (0, common_1.isResourceId)(value[0])))) {
|
|
74
|
+
relationships[field] = { data: value };
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
attributes[field] = value;
|
|
78
|
+
}
|
|
79
|
+
const normalized = {
|
|
80
|
+
type: resource.type,
|
|
81
|
+
attributes,
|
|
82
|
+
relationships
|
|
83
|
+
};
|
|
84
|
+
if ((0, common_1.isResourceId)(resource))
|
|
85
|
+
normalized.id = resource.id;
|
|
86
|
+
debug('normalized resource: %O', normalized);
|
|
87
|
+
return normalized;
|
|
88
|
+
};
|
|
89
|
+
exports.normalize = normalize;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ©2023 Commerce Layer Inc.
|
|
3
|
+
**/
|
|
4
|
+
export type { ApiCredential, ApiCredentialCreate, ApiCredentialUpdate } from './resources/api_credentials';
|
|
5
|
+
export type { Membership, MembershipCreate, MembershipUpdate } from './resources/memberships';
|
|
6
|
+
export type { Organization, OrganizationCreate, OrganizationUpdate } from './resources/organizations';
|
|
7
|
+
export type { Permission, PermissionCreate, PermissionUpdate } from './resources/permissions';
|
|
8
|
+
export type { Role, RoleCreate, RoleUpdate } from './resources/roles';
|
|
9
|
+
export type { User, UserUpdate } from './resources/user';
|
|
10
|
+
export type { Version } from './resources/versions';
|
package/lib/cjs/model.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ResourceType } from "./resource";
|
|
2
|
+
type QueryFilter = Record<string, string | number | boolean>;
|
|
3
|
+
interface QueryParamsRetrieve {
|
|
4
|
+
include?: string[];
|
|
5
|
+
fields?: string[] | Record<string, string[]>;
|
|
6
|
+
}
|
|
7
|
+
interface QueryParamsList extends QueryParamsRetrieve {
|
|
8
|
+
sort?: string[] | Record<string, 'asc' | 'desc'>;
|
|
9
|
+
filters?: QueryFilter;
|
|
10
|
+
pageNumber?: number;
|
|
11
|
+
pageSize?: number;
|
|
12
|
+
}
|
|
13
|
+
type QueryParams = QueryParamsRetrieve | QueryParamsList;
|
|
14
|
+
export type { QueryParamsRetrieve, QueryParamsList, QueryParams, QueryFilter };
|
|
15
|
+
declare const isParamsList: (params: any) => params is QueryParamsList;
|
|
16
|
+
declare const generateQueryStringParams: (params: QueryParamsRetrieve | QueryParamsList | undefined, res: string | ResourceType) => Record<string, string>;
|
|
17
|
+
export { generateQueryStringParams, isParamsList };
|
package/lib/cjs/query.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isParamsList = exports.generateQueryStringParams = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("./debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('query');
|
|
9
|
+
const isParamsList = (params) => {
|
|
10
|
+
return params && (params.filters || params.pageNumber || params.pageSize || params.sort);
|
|
11
|
+
};
|
|
12
|
+
exports.isParamsList = isParamsList;
|
|
13
|
+
const generateQueryStringParams = (params, res) => {
|
|
14
|
+
debug('generate query string params: %O, %O', params, res);
|
|
15
|
+
const qp = {};
|
|
16
|
+
if (!params)
|
|
17
|
+
return qp;
|
|
18
|
+
// Include
|
|
19
|
+
if (params.include)
|
|
20
|
+
qp.include = params.include.join(',');
|
|
21
|
+
// Fields
|
|
22
|
+
if (params.fields) {
|
|
23
|
+
if (Array.isArray(params.fields))
|
|
24
|
+
params.fields = { [res.type || res]: params.fields };
|
|
25
|
+
Object.entries(params.fields).forEach(([p, v]) => {
|
|
26
|
+
qp[`fields[${p}]`] = v.join(',');
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (isParamsList(params)) {
|
|
30
|
+
// Sort
|
|
31
|
+
if (params.sort) {
|
|
32
|
+
if (Array.isArray(params.sort))
|
|
33
|
+
qp.sort = params.sort.join(',');
|
|
34
|
+
else
|
|
35
|
+
qp.sort = Object.entries(params.sort).map(([k, v]) => `${v === 'desc' ? '-' : ''}${k}`).join(',');
|
|
36
|
+
}
|
|
37
|
+
// Page
|
|
38
|
+
if (params.pageNumber)
|
|
39
|
+
qp['page[number]'] = String(params.pageNumber);
|
|
40
|
+
if (params.pageSize)
|
|
41
|
+
qp['page[size]'] = String(params.pageSize);
|
|
42
|
+
// Filters
|
|
43
|
+
if (params.filters) {
|
|
44
|
+
Object.entries(params.filters).forEach(([p, v]) => {
|
|
45
|
+
qp[`filter[q][${p}]`] = String(v);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
debug('query string params: %O', qp);
|
|
50
|
+
return qp;
|
|
51
|
+
};
|
|
52
|
+
exports.generateQueryStringParams = generateQueryStringParams;
|