@commercelayer/sdk
Version:
Commerce Layer Javascript SDK
102 lines (100 loc) • 3.46 kB
JavaScript
import { ApiResourceAdapter } from './chunk-XKERR6OR.js';
import { getResources } from './chunk-4MDMKEMH.js';
import { isApiError } from './chunk-ZU7GFVQ6.js';
import { debug_default } from './chunk-XD7GS4QW.js';
// src/commercelayer.ts
var debug = debug_default("commercelayer");
var OPEN_API_SCHEMA_VERSION = "7.9.5";
var CommerceLayerClient = class _CommerceLayerClient {
openApiSchemaVersion = OPEN_API_SCHEMA_VERSION;
static cl;
// ##__CL_RESOURCES_DEF_START__##
// ##__CL_RESOURCES_DEF_TEMPLATE:: ##__TAB__#####__RESOURCE_TYPE__##?: api.##__RESOURCE_CLASS__##
// ##__CL_RESOURCES_DEF_STOP__##
static get(config) {
if (config) return _CommerceLayerClient.cl = new _CommerceLayerClient(config);
else if (!_CommerceLayerClient.cl) throw new Error("CommerceLayer client not initialized");
return _CommerceLayerClient.cl;
}
constructor(config) {
debug("new commercelayer instance %O", config);
ApiResourceAdapter.init(config);
}
// ##__CL_RESOURCES_LEAZY_LOADING_START__##
// ##__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)) }
// ##__CL_RESOURCES_LEAZY_LOADING_STOP__##
// private get adapter(): ResourceAdapter { return ApiResourceAdapter.get() }
get currentOrganization() {
return ApiResourceAdapter.get().client?.currentOrganization;
}
get currentAccessToken() {
return ApiResourceAdapter.get().client?.currentAccessToken;
}
get interceptors() {
return ApiResourceAdapter.get().client?.interceptors;
}
localConfig(config) {
}
config(config) {
debug("config %o", config);
this.localConfig(config);
ApiResourceAdapter.config(config);
return this;
}
resources(sort) {
return getResources(sort);
}
isApiError(error) {
return isApiError(error);
}
addRequestInterceptor(onSuccess, onFailure) {
this.interceptors.request = { onSuccess, onFailure };
return 1;
}
addResponseInterceptor(onSuccess, onFailure) {
this.interceptors.response = { onSuccess, onFailure };
return 1;
}
removeInterceptor(type, id = 1) {
this.interceptors[type] = void 0;
}
removeInterceptors() {
this.removeInterceptor("request");
this.removeInterceptor("response");
this.removeRawResponseReader();
}
addRawResponseReader(options) {
const reader = {
id: 0,
rawResponse: void 0,
headers: void 0,
ok: true
};
async function rawResponseInterceptor(response) {
reader.rawResponse = await response?.clone().json().catch(() => {
});
reader.ok = response.ok;
if (options?.headers) {
const ho = {};
response.headers.forEach((value, key) => {
ho[key] = value;
});
reader.headers = ho;
}
return response;
}
this.interceptors.rawReader = { onSuccess: rawResponseInterceptor, onFailure: rawResponseInterceptor };
reader.id = 1;
return reader;
}
removeRawResponseReader() {
this.interceptors.rawReader = void 0;
}
};
var CommerceLayer = (config) => {
return CommerceLayerClient.get(config);
};
var commercelayer_default = CommerceLayer;
export { CommerceLayer, CommerceLayerClient, OPEN_API_SCHEMA_VERSION, commercelayer_default };
//# sourceMappingURL=chunk-N54ON7TD.js.map
//# sourceMappingURL=chunk-N54ON7TD.js.map