UNPKG

8.07 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19 return new (P || (P = Promise))(function (resolve, reject) {
20 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23 step((generator = generator.apply(thisArg, _arguments || [])).next());
24 });
25};
26var __generator = (this && this.__generator) || function (thisArg, body) {
27 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29 function verb(n) { return function (v) { return step([n, v]); }; }
30 function step(op) {
31 if (f) throw new TypeError("Generator is already executing.");
32 while (_) try {
33 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34 if (y = 0, t) op = [op[0] & 2, t.value];
35 switch (op[0]) {
36 case 0: case 1: t = op; break;
37 case 4: _.label++; return { value: op[1], done: false };
38 case 5: _.label++; y = op[1]; op = [0]; continue;
39 case 7: op = _.ops.pop(); _.trys.pop(); continue;
40 default:
41 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45 if (t[2]) _.ops.pop();
46 _.trys.pop(); continue;
47 }
48 op = body.call(thisArg, _);
49 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51 }
52};
53Object.defineProperty(exports, "__esModule", { value: true });
54exports.UpdateRequestBuilderV2 = exports.UpdateRequestBuilder = void 0;
55var util_1 = require("@sap-cloud-sdk/util");
56var odata_common_1 = require("../../odata-common");
57var entity_serializer_1 = require("../entity-serializer");
58var uri_conversion_1 = require("../uri-conversion");
59var extract_odata_etag_1 = require("../extract-odata-etag");
60var logger = util_1.createLogger({
61 package: 'core',
62 messageContext: 'update-request-builder-v2'
63});
64/**
65 * Create OData query to update an entity.
66 * @typeparam EntityT - Type of the entity to be updated
67 */
68var UpdateRequestBuilder = /** @class */ (function (_super) {
69 __extends(UpdateRequestBuilder, _super);
70 /**
71 * Creates an instance of UpdateRequestBuilder.
72 * @param _entityConstructor - Constructor type of the entity to be updated
73 * @param _entity - Entity to be updated
74 */
75 function UpdateRequestBuilder(_entityConstructor, _entity) {
76 var _this = _super.call(this, _entityConstructor, _entity, uri_conversion_1.oDataUri, entity_serializer_1.entitySerializer, extract_odata_etag_1.extractODataEtag, removeNavPropsAndComplexTypes) || this;
77 _this._entityConstructor = _entityConstructor;
78 _this._entity = _entity;
79 return _this;
80 }
81 /**
82 * Executes the query.
83 * @param destination - Destination to execute the request against
84 * @param options - Options to employ when fetching destinations
85 * @returns A promise resolving to the entity once it was updated
86 */
87 UpdateRequestBuilder.prototype.execute = function (destination, options) {
88 return __awaiter(this, void 0, void 0, function () {
89 var request;
90 return __generator(this, function (_a) {
91 switch (_a.label) {
92 case 0:
93 if (this.isEmptyObject(this.requestConfig.payload)) {
94 return [2 /*return*/, this._entity];
95 }
96 return [4 /*yield*/, this.build(destination, options)];
97 case 1:
98 request = _a.sent();
99 warnIfNavigation(request, this._entity, this._entityConstructor);
100 return [2 /*return*/, _super.prototype.executeRequest.call(this, request)];
101 }
102 });
103 });
104 };
105 /**
106 * Execute request and return an [[HttpResponse]]. The request is only executed if some properties of the entity are modified.
107 * @param destination - Destination to execute the request against
108 * @param options - Options to employ when fetching destinations
109 * @returns A promise resolving to an [[HttpResponse]] when the request is executed or `undefined` otherwise.
110 */
111 UpdateRequestBuilder.prototype.executeRaw = function (destination, options) {
112 return __awaiter(this, void 0, void 0, function () {
113 var request;
114 return __generator(this, function (_a) {
115 switch (_a.label) {
116 case 0:
117 if (this.isEmptyObject(this.requestConfig.payload)) {
118 logger.info('The request is not executed because no properties of the entity are modified.');
119 return [2 /*return*/];
120 }
121 return [4 /*yield*/, this.build(destination, options)];
122 case 1:
123 request = _a.sent();
124 warnIfNavigation(request, this._entity, this._entityConstructor);
125 return [2 /*return*/, _super.prototype.executeRequestRaw.call(this, request)];
126 }
127 });
128 });
129 };
130 return UpdateRequestBuilder;
131}(odata_common_1.UpdateRequestBuilder));
132exports.UpdateRequestBuilder = UpdateRequestBuilder;
133exports.UpdateRequestBuilderV2 = UpdateRequestBuilder;
134/*
135 * In case the entity contains a navigation to a different entity a warning is printed.
136 */
137function warnIfNavigation(request, entity, entityConstructor) {
138 var setNavigationProperties = Object.keys(entity).filter(function (key) {
139 return !util_1.isNullish(entity[key]) && odata_common_1.isNavigationProperty(key, entityConstructor);
140 });
141 if (setNavigationProperties.length) {
142 logger.warn("The navigation properties " + setNavigationProperties + " have been included in your update request. Update of navigation properties is not supported and will be ignored.");
143 }
144 return request;
145}
146function removeNavPropsAndComplexTypes(body) {
147 return odata_common_1.removePropertyOnCondition(function (_a) {
148 var val = _a[1];
149 return typeof val === 'object';
150 }, body);
151}
152//# sourceMappingURL=update-request-builder.js.map
\No newline at end of file