@eleva-io/erp-sdk
Version:
SDK oficial para el ERP de Eleva
68 lines • 2.31 kB
JavaScript
import { ElevaError } from '../errors';
import { requireField } from './guards';
const DEFAULT_METHODS_CONFIG = {
isCreateEnabled: true,
isReadEnabled: true,
isUpdateEnabled: true,
isDeleteEnabled: true,
isFindEnabled: true,
};
export class CRUD {
_httpClient;
_config;
constructor(_httpClient, _config) {
this._httpClient = _httpClient;
this._config = _config;
this._config = {
...this._config,
methods: {
...DEFAULT_METHODS_CONFIG,
..._config.methods,
},
};
}
async get(id) {
if (!this._config.methods.isReadEnabled) {
throw ElevaError.badRequest({ message: 'Read method is not enabled' });
}
const idParam = this._getIdParam(id);
const path = `${this._config.basePath}/${idParam}`;
return this._httpClient.get(path);
}
async create(data) {
if (!this._config.methods.isCreateEnabled) {
throw ElevaError.badRequest({ message: 'Create method is not enabled' });
}
const path = `${this._config.basePath}`;
return this._httpClient.post(path, data);
}
async update(id, data) {
if (!this._config.methods.isUpdateEnabled) {
throw ElevaError.badRequest({ message: 'Update method is not enabled' });
}
const idParam = this._getIdParam(id);
const path = `${this._config.basePath}/${idParam}`;
return this._httpClient.patch(path, data);
}
async delete(id) {
if (!this._config.methods.isDeleteEnabled) {
throw ElevaError.badRequest({ message: 'Delete method is not enabled' });
}
const idParam = this._getIdParam(id);
const path = `${this._config.basePath}/${idParam}`;
return this._httpClient.delete(path);
}
async find(query) {
if (!this._config.methods.isFindEnabled) {
throw ElevaError.badRequest({ message: 'Find method is not enabled' });
}
const path = `${this._config.basePath}`;
return this._httpClient.get(path, query);
}
_getIdParam(id) {
const idParam = id || this._config.id;
requireField('id', idParam);
return idParam;
}
}
//# sourceMappingURL=crud.js.map