1 | "use strict";
|
2 | var __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 | })();
|
17 | var __assign = (this && this.__assign) || function () {
|
18 | __assign = Object.assign || function(t) {
|
19 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
20 | s = arguments[i];
|
21 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
22 | t[p] = s[p];
|
23 | }
|
24 | return t;
|
25 | };
|
26 | return __assign.apply(this, arguments);
|
27 | };
|
28 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
29 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
30 | return new (P || (P = Promise))(function (resolve, reject) {
|
31 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
32 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
33 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
34 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
35 | });
|
36 | };
|
37 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
38 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
39 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
40 | function verb(n) { return function (v) { return step([n, v]); }; }
|
41 | function step(op) {
|
42 | if (f) throw new TypeError("Generator is already executing.");
|
43 | while (_) try {
|
44 | 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;
|
45 | if (y = 0, t) op = [op[0] & 2, t.value];
|
46 | switch (op[0]) {
|
47 | case 0: case 1: t = op; break;
|
48 | case 4: _.label++; return { value: op[1], done: false };
|
49 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
50 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
51 | default:
|
52 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
53 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
54 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
55 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
56 | if (t[2]) _.ops.pop();
|
57 | _.trys.pop(); continue;
|
58 | }
|
59 | op = body.call(thisArg, _);
|
60 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
61 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
62 | }
|
63 | };
|
64 | Object.defineProperty(exports, "__esModule", { value: true });
|
65 | exports.UpdateRequestBuilderBase = exports.removePropertyOnCondition = exports.UpdateRequestBuilder = void 0;
|
66 | var util_1 = require("@sap-cloud-sdk/util");
|
67 | var request_1 = require("../request");
|
68 | var entity_deserializer_1 = require("../entity-deserializer");
|
69 | var request_builder_base_1 = require("./request-builder-base");
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | var UpdateRequestBuilder = (function (_super) {
|
75 | __extends(UpdateRequestBuilder, _super);
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function UpdateRequestBuilder(_entityConstructor, _entity, oDataUri, entitySerializer, extractODataEtag, payloadManipulator) {
|
86 | var _this = _super.call(this, new request_1.ODataUpdateRequestConfig(_entityConstructor, oDataUri)) || this;
|
87 | _this._entityConstructor = _entityConstructor;
|
88 | _this._entity = _entity;
|
89 | _this.oDataUri = oDataUri;
|
90 | _this.entitySerializer = entitySerializer;
|
91 | _this.extractODataEtag = extractODataEtag;
|
92 | _this.payloadManipulator = payloadManipulator;
|
93 | _this.requestConfig.eTag = _entity.versionIdentifier;
|
94 | _this.required = new Set();
|
95 | _this.ignored = new Set();
|
96 | _this.requestConfig.keys = _this.oDataUri.getEntityKeys(_this._entity, _this._entityConstructor);
|
97 | _this.requestConfig.payload = _this.getPayload();
|
98 | return _this;
|
99 | }
|
100 | Object.defineProperty(UpdateRequestBuilder.prototype, "entity", {
|
101 | get: function () {
|
102 | return this._entity;
|
103 | },
|
104 | enumerable: false,
|
105 | configurable: true
|
106 | });
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 | UpdateRequestBuilder.prototype.prepare = function () {
|
113 | this.requestConfig.keys = this.oDataUri.getEntityKeys(this._entity, this._entityConstructor);
|
114 | this.requestConfig.payload = this.getPayload();
|
115 | return this;
|
116 | };
|
117 | |
118 |
|
119 |
|
120 |
|
121 | UpdateRequestBuilder.prototype.replaceWholeEntityWithPut = function () {
|
122 | this.requestConfig.updateWithPut();
|
123 | this.requestConfig.payload = this.getPayload();
|
124 | return this;
|
125 | };
|
126 | UpdateRequestBuilder.prototype.requiredFields = function (first) {
|
127 | var rest = [];
|
128 | for (var _i = 1; _i < arguments.length; _i++) {
|
129 | rest[_i - 1] = arguments[_i];
|
130 | }
|
131 | this.required = this.toSet((0, util_1.variadicArgumentToArray)(first, rest));
|
132 | this.requestConfig.payload = this.getPayload();
|
133 | return this;
|
134 | };
|
135 | UpdateRequestBuilder.prototype.setRequiredFields = function (first) {
|
136 | var rest = [];
|
137 | for (var _i = 1; _i < arguments.length; _i++) {
|
138 | rest[_i - 1] = arguments[_i];
|
139 | }
|
140 | this.required = this.toSet((0, util_1.variadicArgumentToArray)(first, rest));
|
141 | this.requestConfig.payload = this.getPayload();
|
142 | return this;
|
143 | };
|
144 | UpdateRequestBuilder.prototype.ignoredFields = function (first) {
|
145 | var rest = [];
|
146 | for (var _i = 1; _i < arguments.length; _i++) {
|
147 | rest[_i - 1] = arguments[_i];
|
148 | }
|
149 | this.ignored = this.toSet((0, util_1.variadicArgumentToArray)(first, rest));
|
150 | this.requestConfig.payload = this.getPayload();
|
151 | return this;
|
152 | };
|
153 | UpdateRequestBuilder.prototype.setIgnoredFields = function (first) {
|
154 | var rest = [];
|
155 | for (var _i = 1; _i < arguments.length; _i++) {
|
156 | rest[_i - 1] = arguments[_i];
|
157 | }
|
158 | this.ignored = this.toSet((0, util_1.variadicArgumentToArray)(first, rest));
|
159 | this.requestConfig.payload = this.getPayload();
|
160 | return this;
|
161 | };
|
162 | |
163 |
|
164 |
|
165 |
|
166 | UpdateRequestBuilder.prototype.ignoreVersionIdentifier = function () {
|
167 | this.requestConfig.versionIdentifierIgnored = true;
|
168 | return this;
|
169 | };
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | UpdateRequestBuilder.prototype.withCustomVersionIdentifier = function (etag) {
|
177 | this.requestConfig.eTag = etag;
|
178 | return this;
|
179 | };
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 | UpdateRequestBuilder.prototype.setVersionIdentifier = function (etag) {
|
186 | this.requestConfig.eTag = etag;
|
187 | return this;
|
188 | };
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 | UpdateRequestBuilder.prototype.executeRequest = function (request) {
|
195 | return __awaiter(this, void 0, void 0, function () {
|
196 | var _this = this;
|
197 | return __generator(this, function (_a) {
|
198 | return [2 , (this.executeRequestRaw(request)
|
199 |
|
200 | .then(function (response) {
|
201 | var eTag = (0, entity_deserializer_1.extractEtagFromHeader)(response.headers) ||
|
202 | _this.extractODataEtag(response.data) ||
|
203 | _this.requestConfig.eTag;
|
204 | return _this._entity
|
205 | .setOrInitializeRemoteState()
|
206 | .setVersionIdentifier(eTag);
|
207 | })
|
208 | .catch(function (error) {
|
209 | throw new util_1.ErrorWithCause('OData update request failed!', error);
|
210 | }))];
|
211 | });
|
212 | });
|
213 | };
|
214 | UpdateRequestBuilder.prototype.executeRequestRaw = function (request) {
|
215 | return __awaiter(this, void 0, void 0, function () {
|
216 | return __generator(this, function (_a) {
|
217 | return [2 , request.execute()];
|
218 | });
|
219 | });
|
220 | };
|
221 | UpdateRequestBuilder.prototype.getPayload = function () {
|
222 | var serializedBody = this.entitySerializer.serializeEntity(this._entity, this._entityConstructor);
|
223 | if (this.requestConfig.method === 'patch') {
|
224 | var body = this.serializedDiff();
|
225 | body = this.payloadManipulator(body);
|
226 | body = this.removeKeyFields(body);
|
227 | body = this.addRequiredFields(serializedBody, body);
|
228 | body = this.removeIgnoredFields(body);
|
229 | return body;
|
230 | }
|
231 | return serializedBody;
|
232 | };
|
233 | UpdateRequestBuilder.prototype.isEmptyObject = function (obj) {
|
234 | for (var key in obj) {
|
235 | if (obj.hasOwnProperty(key)) {
|
236 | return false;
|
237 | }
|
238 | }
|
239 | return true;
|
240 | };
|
241 | UpdateRequestBuilder.prototype.addRequiredFields = function (completeBody, body) {
|
242 | return Array.from(this.required).reduce(function (resultBody, requiredField) {
|
243 | var _a;
|
244 | if (Object.keys(resultBody).includes(requiredField)) {
|
245 | return resultBody;
|
246 | }
|
247 | return __assign(__assign({}, resultBody), (_a = {}, _a[requiredField] = completeBody[requiredField], _a));
|
248 | }, body);
|
249 | };
|
250 | UpdateRequestBuilder.prototype.getKeyFieldNames = function () {
|
251 | return Object.keys(this._entityConstructor._keys);
|
252 | };
|
253 | UpdateRequestBuilder.prototype.toSet = function (fields) {
|
254 | var set = new Set();
|
255 | Object.values(fields).forEach(function (field) {
|
256 | set.add(field._fieldName);
|
257 | });
|
258 | return set;
|
259 | };
|
260 | UpdateRequestBuilder.prototype.serializedDiff = function () {
|
261 | return __assign({}, this.entitySerializer.serializeEntity(this._entity, this._entityConstructor, true));
|
262 | };
|
263 | UpdateRequestBuilder.prototype.removeKeyFields = function (body) {
|
264 | var _this = this;
|
265 | return removePropertyOnCondition(function (_a) {
|
266 | var key = _a[0];
|
267 | return _this.getKeyFieldNames().includes(key);
|
268 | }, body);
|
269 | };
|
270 | UpdateRequestBuilder.prototype.removeIgnoredFields = function (body) {
|
271 | var _this = this;
|
272 | return removePropertyOnCondition(function (_a) {
|
273 | var key = _a[0];
|
274 | return _this.ignored.has(key);
|
275 | }, body);
|
276 | };
|
277 | return UpdateRequestBuilder;
|
278 | }(request_builder_base_1.MethodRequestBuilder));
|
279 | exports.UpdateRequestBuilder = UpdateRequestBuilder;
|
280 | exports.UpdateRequestBuilderBase = UpdateRequestBuilder;
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | function removePropertyOnCondition(condition, body) {
|
286 | return Object.entries(body).reduce(function (resultBody, _a) {
|
287 | var _b;
|
288 | var key = _a[0], val = _a[1];
|
289 | if (condition([key, val])) {
|
290 | return resultBody;
|
291 | }
|
292 | return __assign(__assign({}, resultBody), (_b = {}, _b[key] = val, _b));
|
293 | }, {});
|
294 | }
|
295 | exports.removePropertyOnCondition = removePropertyOnCondition;
|
296 |
|
\ | No newline at end of file |