UNPKG

@jsstudio/development-api-interceptor

Version:
103 lines 5.46 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Methods = void 0; const constants_1 = require("../constants"); const error_handling_1 = require("../error-handling"); const indexed_db_1 = __importDefault(require("../indexed-db")); const api_response_1 = require("../utils/api-response"); const logger_1 = require("../utils/logger"); const validation_1 = require("../validation"); class Methods { constructor() { this.validate = new validation_1.Validation(); } getAll(tableName) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield indexed_db_1.default.getAll(tableName); return api_response_1.getResponse({ success: true, message: constants_1.DATA_CRUD.FETCHED_SUCCESSFULLY, data: response, status: 200 }); } catch (err) { logger_1.logger(constants_1.INDEXED_DB_ERROR, err.message); return api_response_1.getResponse({ status: err.status, message: err.message, data: {}, success: false }); } }); } // Handles INSERT Operations create(tableName, request, method) { return __awaiter(this, void 0, void 0, function* () { try { this.validate.apiRequest(tableName, method, request); const response = yield indexed_db_1.default.create(tableName, request); const message = constants_1.DATA_CRUD.CREATED_SUCCESSFULLY; return api_response_1.getResponse({ message, data: response, status: constants_1.RESPONSE_CODE.SUCCESS, success: true }); } catch (err) { logger_1.logger(constants_1.INDEXED_DB_ERROR, err.message); return api_response_1.getResponse({ status: err.status, message: err.message, data: {}, success: false }); } }); } // Handles UPDATE Operations update(tableName, request, id, method) { return __awaiter(this, void 0, void 0, function* () { try { this.validate.apiRequest(tableName, method, request); const response = yield indexed_db_1.default.update(tableName, request, id); if (!response) { throw new error_handling_1.ApiError(constants_1.ERRORS.ID_NOT_FOUND, constants_1.RESPONSE_CODE.NOT_FOUND); } const message = constants_1.DATA_CRUD.UPDATED_SUCCESSFULLY; return api_response_1.getResponse({ message, data: response, status: constants_1.RESPONSE_CODE.SUCCESS, success: true }); } catch (err) { logger_1.logger(constants_1.INDEXED_DB_ERROR, err.message); return api_response_1.getResponse({ status: err.status, message: err.message, data: {}, success: false }); } }); } getById(tableName, id) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield indexed_db_1.default.getById(tableName, id); if (!response) { throw new error_handling_1.ApiError(constants_1.ERRORS.ID_NOT_FOUND, constants_1.RESPONSE_CODE.NOT_FOUND); } return api_response_1.getResponse({ message: constants_1.DATA_CRUD.FETCHED_SUCCESSFULLY, data: response, status: constants_1.RESPONSE_CODE.SUCCESS, success: true }); } catch (err) { logger_1.logger(constants_1.INDEXED_DB_ERROR, err.message); return api_response_1.getResponse({ status: err.status, message: err.message, data: {}, success: false }); } }); } delete(tableName, id) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield indexed_db_1.default.delete(tableName, id); if (!response) { throw new error_handling_1.ApiError(constants_1.ERRORS.ID_NOT_FOUND, constants_1.RESPONSE_CODE.NOT_FOUND); } return api_response_1.getResponse({ message: constants_1.DATA_CRUD.DELETED_SUCCESSFULLY, data: response, status: constants_1.RESPONSE_CODE.SUCCESS, success: true }); } catch (err) { logger_1.logger(constants_1.INDEXED_DB_ERROR, err.message); return api_response_1.getResponse({ status: err.status, message: err.message, data: {}, success: false }); } }); } } exports.Methods = Methods; //# sourceMappingURL=Methods.js.map