@jsstudio/development-api-interceptor
Version:
development-api-interceptor
96 lines • 4.64 kB
JavaScript
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());
});
};
import { DATA_CRUD, ERRORS, INDEXED_DB_ERROR, RESPONSE_CODE } from '../constants';
import { ApiError } from '../error-handling';
import indexedDb from '../indexed-db';
import { getResponse } from '../utils/api-response';
import { logger } from '../utils/logger';
import { Validation } from '../validation';
export class Methods {
constructor() { this.validate = new Validation(); }
getAll(tableName) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield indexedDb.getAll(tableName);
return getResponse({ success: true, message: DATA_CRUD.FETCHED_SUCCESSFULLY, data: response, status: 200 });
}
catch (err) {
logger(INDEXED_DB_ERROR, err.message);
return 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 indexedDb.create(tableName, request);
const message = DATA_CRUD.CREATED_SUCCESSFULLY;
return getResponse({ message, data: response, status: RESPONSE_CODE.SUCCESS, success: true });
}
catch (err) {
logger(INDEXED_DB_ERROR, err.message);
return 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 indexedDb.update(tableName, request, id);
if (!response) {
throw new ApiError(ERRORS.ID_NOT_FOUND, RESPONSE_CODE.NOT_FOUND);
}
const message = DATA_CRUD.UPDATED_SUCCESSFULLY;
return getResponse({ message, data: response, status: RESPONSE_CODE.SUCCESS, success: true });
}
catch (err) {
logger(INDEXED_DB_ERROR, err.message);
return 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 indexedDb.getById(tableName, id);
if (!response) {
throw new ApiError(ERRORS.ID_NOT_FOUND, RESPONSE_CODE.NOT_FOUND);
}
return getResponse({ message: DATA_CRUD.FETCHED_SUCCESSFULLY,
data: response, status: RESPONSE_CODE.SUCCESS, success: true });
}
catch (err) {
logger(INDEXED_DB_ERROR, err.message);
return 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 indexedDb.delete(tableName, id);
if (!response) {
throw new ApiError(ERRORS.ID_NOT_FOUND, RESPONSE_CODE.NOT_FOUND);
}
return getResponse({ message: DATA_CRUD.DELETED_SUCCESSFULLY,
data: response, status: RESPONSE_CODE.SUCCESS, success: true });
}
catch (err) {
logger(INDEXED_DB_ERROR, err.message);
return getResponse({ status: err.status, message: err.message, data: {}, success: false });
}
});
}
}
//# sourceMappingURL=Methods.js.map