UNPKG

navidev-abap-gw-api

Version:

API para la extracción de datos de servicios SAP GW

125 lines (124 loc) 6.59 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 }); const Result_1 = require("navidev-adt-api/dist/shared/core/Result"); const crudOperationsApp_1 = __importDefault(require("./crudOperationsApp")); const queryUtils_1 = __importDefault(require("./queryUtils")); const postUtils_1 = __importDefault(require("./postUtils")); class CrudOperationsV2App extends crudOperationsApp_1.default { query(entity, parameters) { return __awaiter(this, void 0, void 0, function* () { this.readEntityInfo(entity); if (!this.entityInfo) return Result_1.Result.fail(this.adtExceptionController.fromText("Entity no valid.")); const queryUtils = new queryUtils_1.default(this.entityInfo, this.serviceBindingInfo.versionOdata); let query = queryUtils.buildQuery(parameters); const response = yield this.connectionController.request(`${this.serviceBindingInfo.serviceUrl}/${entity}${query}`, { method: "GET", }); if (response.isSuccess) { let httpResponse = response.getValue(); let json = JSON.parse(httpResponse.body); return Result_1.Result.ok(queryUtils.mappingSAPJSON2Array(json.d.results, parameters === null || parameters === void 0 ? void 0 : parameters.select)); } else { return Result_1.Result.fail(response.getErrorValue()); } }); } read(entity, keys) { return __awaiter(this, void 0, void 0, function* () { this.readEntityInfo(entity); if (!this.entityInfo) return Result_1.Result.fail(this.adtExceptionController.fromText("Entity no valid.")); const queryUtils = new queryUtils_1.default(this.entityInfo, this.serviceBindingInfo.versionOdata); let query = queryUtils.buildReadQuery(keys); const response = yield this.connectionController.request(`${this.serviceBindingInfo.serviceUrl}/${entity}${query}`, { method: "GET", }); if (response.isSuccess) { let httpResponse = response.getValue(); let json = JSON.parse(httpResponse.body); return Result_1.Result.ok(queryUtils.mappingSAPJSON2Object(json.d)); } else { return Result_1.Result.fail(response.getErrorValue()); } }); } post(entity, values, parameters) { return __awaiter(this, void 0, void 0, function* () { this.readEntityInfo(entity); if (!this.entityInfo) return Result_1.Result.fail(this.adtExceptionController.fromText("Entity no valid.")); const postUtils = new postUtils_1.default(this.entityInfo, this.serviceBindingInfo.versionOdata); const body = postUtils.mappingObject2SAPJSON(values); const responsePost = yield this.connectionController.request(`${this.serviceBindingInfo.serviceUrl}/${entity}`, { method: "POST", headers: { "content-type": "application/json", Accept: "application/json", }, body: body, qs: Object.assign({}, parameters === null || parameters === void 0 ? void 0 : parameters.urlParameters), }); if (responsePost.isFailure) return Result_1.Result.fail(responsePost.getErrorValue()); const response = responsePost.getValue(); let valuesCreated = JSON.parse(response.body); return Result_1.Result.ok(postUtils.mappingSAPJSON2Object(valuesCreated.d)); }); } patch(entity, keys, values) { return __awaiter(this, void 0, void 0, function* () { this.readEntityInfo(entity); if (!this.entityInfo) return Result_1.Result.fail(this.adtExceptionController.fromText("Entity no valid.")); const postUtils = new postUtils_1.default(this.entityInfo, this.serviceBindingInfo.versionOdata); const body = postUtils.mappingObject2SAPJSON(values); let query = postUtils.buildPatchQuery(keys); const responsePatch = yield this.connectionController.request(`${this.serviceBindingInfo.serviceUrl}/${entity}${query}`, { method: "PATCH", headers: { "content-type": "application/json", Accept: "application/json", }, body: body, }); if (responsePatch.isFailure) return Result_1.Result.fail(responsePatch.getErrorValue()); return Result_1.Result.ok(); }); } delete(entity, keys) { return __awaiter(this, void 0, void 0, function* () { this.readEntityInfo(entity); if (!this.entityInfo) return Result_1.Result.fail(this.adtExceptionController.fromText("Entity no valid.")); const postUtils = new postUtils_1.default(this.entityInfo, this.serviceBindingInfo.versionOdata); let query = postUtils.buildPatchQuery(keys); const responseDelete = yield this.connectionController.request(`${this.serviceBindingInfo.serviceUrl}/${entity}${query}`, { method: "DELETE", headers: { "content-type": "application/json", Accept: "application/json", }, }); if (responseDelete.isFailure) return Result_1.Result.fail(responseDelete.getErrorValue()); return Result_1.Result.ok(); }); } } exports.default = CrudOperationsV2App;