UNPKG

@eleva-io/erp-sdk

Version:

SDK oficial para el ERP de Eleva

72 lines 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CRUD = void 0; const errors_1 = require("../errors"); const guards_1 = require("./guards"); const DEFAULT_METHODS_CONFIG = { isCreateEnabled: true, isReadEnabled: true, isUpdateEnabled: true, isDeleteEnabled: true, isFindEnabled: true, }; 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 errors_1.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 errors_1.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 errors_1.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 errors_1.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 errors_1.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; (0, guards_1.requireField)('id', idParam); return idParam; } } exports.CRUD = CRUD; //# sourceMappingURL=crud.js.map