-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathupdate-request-builder.ts
254 lines (228 loc) · 7.86 KB
/
update-request-builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { errorWithCause, variadicArgumentToArray } from '@sap-cloud-sdk/util';
import { Constructable, EntityIdentifiable, Selectable } from '../../common';
import { EntityV4 } from '../entity';
import { MethodRequestBuilderBase } from '../../common/request-builder/request-builder-base';
import { ODataUpdateRequestConfig } from '../../common/request/odata-update-request-config';
import {
serializeEntityV4,
serializeEntityNonCustomFieldsV4
} from '../entity-serializer';
import { DestinationOptions } from '../../../scp-cf';
import {
Destination,
DestinationNameAndJwt
} from '../../../scp-cf/destination/destination-service-types';
import { oDataUriV4 } from '../uri-conversion';
import { extractEtagFromHeader } from '../../common/entity-deserializer';
import { extractODataEtagV4 } from '../extract-odata-etag';
/**
* Create OData query to update an entity.
*
* @typeparam EntityT - Type of the entity to be updated
*/
export class UpdateRequestBuilderV4<EntityT extends EntityV4>
extends MethodRequestBuilderBase<ODataUpdateRequestConfig<EntityT>>
implements EntityIdentifiable<EntityT> {
private ignored: Set<string>;
private required: Set<string>;
/**
* Creates an instance of UpdateRequestBuilder.
*
* @param _entityConstructor - Constructor type of the entity to be updated
* @param _entity - Entity to be updated
*/
constructor(
readonly _entityConstructor: Constructable<EntityT>,
readonly _entity: EntityT
) {
super(new ODataUpdateRequestConfig(_entityConstructor, oDataUriV4));
this.requestConfig.eTag = _entity.versionIdentifier;
this.required = new Set<string>();
this.ignored = new Set<string>();
this.requestConfig.keys = oDataUriV4.getEntityKeys(
this._entity,
this._entityConstructor
);
this.requestConfig.payload = this.getPayload();
}
get entity(): EntityT {
return this._entity;
}
/**
* @deprecated Since v1.29.0. This method should never be called, it has severe side effects.
* Builds the payload and the entity keys of the query.
* @returns the builder itself
*/
prepare(): this {
this.requestConfig.keys = oDataUriV4.getEntityKeys(
this._entity,
this._entityConstructor
);
this.requestConfig.payload = this.getPayload();
return this;
}
/**
* Executes the query.
*
* @param destination - Destination to execute the request against
* @param options - Options to employ when fetching destinations
* @returns A promise resolving to the entity once it was updated
*/
async execute(
destination: Destination | DestinationNameAndJwt,
options?: DestinationOptions
): Promise<EntityT> {
if (this.isEmptyObject(this.requestConfig.payload)) {
return this._entity;
}
return (
this.build(destination, options)
.then(request => request.execute())
// Update returns 204 hence the data from the request is used to build entity for return
.then(response => {
const eTag =
extractEtagFromHeader(response.headers) ||
extractODataEtagV4(response.data) ||
this.requestConfig.eTag;
return this._entity
.setOrInitializeRemoteState()
.setVersionIdentifier(this.requestConfig.eTag);
})
.catch(error =>
Promise.reject(errorWithCause('OData update request failed!', error))
)
);
}
/**
* Explicitly configure 'PUT' as the method of the update request. By default, only the properties that have changed compared to the last known remote state are sent using 'PATCH', while with 'PUT', the whole entity is sent.
*
* @returns The entity itself, to facilitate method chaining
*/
replaceWholeEntityWithPut(): this {
this.requestConfig.updateWithPut();
this.requestConfig.payload = this.getPayload();
return this;
}
/**
* Specifies required entity keys for the update request.
*
* @param fields - Enumeration of the fields to be required
* @returns The entity itself, to facilitate method chaining
*/
requiredFields(...fields: Selectable<EntityT>[]): this;
requiredFields(fields: Selectable<EntityT>[]): this;
requiredFields(
first: undefined | Selectable<EntityT> | Selectable<EntityT>[],
...rest: Selectable<EntityT>[]
): this {
this.required = this.toSet(variadicArgumentToArray(first, rest));
this.requestConfig.payload = this.getPayload();
return this;
}
/**
* Specifies entity fields to ignore by the update request.
*
* @param fields - Enumeration of the fields to be ignored
* @returns The entity itself, to facilitate method chaining
*/
ignoredFields(...fields: Selectable<EntityT>[]): this;
ignoredFields(fields: Selectable<EntityT>[]): this;
ignoredFields(
first: undefined | Selectable<EntityT> | Selectable<EntityT>[],
...rest: Selectable<EntityT>[]
): this {
this.ignored = this.toSet(variadicArgumentToArray(first, rest));
this.requestConfig.payload = this.getPayload();
return this;
}
/**
* Instructs the request to force an overwrite of the entity by sending an 'If-Match: *' header instead of sending the ETag version identifier.
*
* @returns this The request itself to ease chaining while executing the request
*/
ignoreVersionIdentifier(): this {
this.requestConfig.versionIdentifierIgnored = true;
return this;
}
/**
* Specifies a custom ETag version identifier of the entity to update.
*
* @param etag - Custom ETag version identifier to be sent in the header of the request
* @returns The request itself to ease chaining while executing the request
*/
withCustomVersionIdentifier(etag: string): this {
this.requestConfig.eTag = etag;
return this;
}
private getPayload(): Record<string, any> {
const serializedBody = serializeEntityV4(
this._entity,
this._entityConstructor
);
if (this.requestConfig.method === 'patch') {
let body = this.serializedDiff();
body = this.removeKeyFields(body);
body = this.addRequiredFields(serializedBody, body);
body = this.removeIgnoredFields(body);
return body;
}
return serializedBody;
}
private serializedDiff(): Record<string, any> {
return {
...serializeEntityNonCustomFieldsV4(
this._entity.getUpdatedProperties(),
this._entityConstructor
),
...this._entity.getUpdatedCustomFields()
};
}
private removeKeyFields(body: Record<string, any>): Record<string, any> {
return removePropertyOnCondition(([key, val]) =>
this.getKeyFieldNames().includes(key)
)(body);
}
private removeIgnoredFields(body: Record<string, any>): Record<string, any> {
return removePropertyOnCondition(([key, val]) => this.ignored.has(key))(
body
);
}
private addRequiredFields(
completeBody: Record<string, any>,
body: Record<string, any>
): Record<string, any> {
return Array.from(this.required).reduce((resultBody, requiredField) => {
if (Object.keys(resultBody).includes(requiredField)) {
return resultBody;
}
return { ...resultBody, [requiredField]: completeBody[requiredField] };
}, body);
}
private getKeyFieldNames(): string[] {
return Object.keys(this._entityConstructor._keys);
}
private toSet(fields: Selectable<EntityT>[]) {
const set = new Set<string>();
Object.values(fields).forEach(field => {
set.add(field._fieldName);
});
return set;
}
private isEmptyObject(obj: any): boolean {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
}
const removePropertyOnCondition = (
condition: (objectEntry: [string, any]) => boolean
) => (body: Record<string, any>): Record<string, any> =>
Object.entries(body).reduce((resultBody, [key, val]) => {
if (condition([key, val])) {
return resultBody;
}
return { ...resultBody, [key]: val };
}, {});