Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | 21x 21x 21x 21x 21x 35x 2x 1x 1x 2x 4x 8x 10x 8x 3x 3x 2x 2x 1x 2x 5x 1x 1x 2x 2x 2x 2x 1x 1x 7x 7x 2x 1x 2x 5x 1x 1x 4x 7x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 2x 21x | "use strict";
const Base = require("./Base");
const Request = require("./Request");
const ChangeSet = require("./ChangeSet");
const _ = require("lodash");
const responseType = require("../../engine/responseType");
/**
* Batch class implements OData batch request and response processing
*
* @public
* @class Batch
*/
class Batch extends Base {
/**
* Initialize instance of the Batch class
*
* @public
* @memberof Batch
*/
constructor() {
super("requests", "batch");
}
/**
* Add new item to the batch list object
*
* @param {String} httpMethod name of the HTTP method
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the post request
* @param {Object} payload data which is converted to the JSON string and passed as body of POST request in batch
* @param {ChangeSet} [changeSet] object which defines the changeset which
* contains the requests if the parameter is not defined the batch will try
* to find the active changeset in the batch automatically
*
* @returns {Object} new instance of Request class
*
* @private
* @memberof Batch
*/
addRequest(httpMethod, inputUrl, headers, payload, changeSet) {
let request;
if (changeSet) {
request = changeSet.addRequest(httpMethod, inputUrl, headers, payload);
} else {
request = this.add(Request, httpMethod, inputUrl, headers, payload);
}
return request;
}
/**
* Create request in batch with payload
*
* @param {String} httpMethod name of the HTTP method
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {Object} payload data which is converted to the JSON string and passed as body of POST request
* @param {ChangeSet} changeSet which contains newly created request
*
* @returns {Request} instance of batch Request
*
* @private
* @memberof Agent
*/
addRequestWithPayload(httpMethod, inputUrl, headers, payload, changeSet) {
return this.addRequest(
httpMethod,
inputUrl,
_.assign(
{
"sap-contextid-accept": "header",
Accept: "application/json",
DataServiceVersion: "2.0",
MaxDataServiceVersion: "2.0",
"Content-Type": "application/json",
"sap-message-scope": "BusinessObject",
},
headers
),
payload,
changeSet
);
}
/**
* Try to find passed changeSet in the current batch. If changeSet is not
* defined and the batch contains only one batch. Use it.
*
* @private
*
* @param {ChangeSet} [changeSet] object which defines the changeset
*
* @returns {ChangeSet} correctly found changeSet from the batch or undefined
*
* @memberof Batch
*/
get defaultChangeSet() {
let changeSets = _.filter(this.requests, (batchItem) => {
return batchItem instanceof ChangeSet && !batchItem.commited;
});
return changeSets.length > 0 ? changeSets[0] : undefined;
}
/**
*
* The indexOf method returns the first index at which a given element can be
* found in the array, or -1 if it is not present.
*
* @public
*
* @param {ChangeSet|Request} batchItem part of the batch a request or a changeset
*
* @returns {Number} index of the batchItem or -1
*
* @memberof Batch
*/
indexOf(batchItem) {
let requestList = this.requests;
let index;
if (batchItem instanceof ChangeSet || batchItem instanceof Request) {
index = _.findIndex(requestList, (batchItemFromList) => {
return batchItem === batchItemFromList;
});
} else {
throw new Error("Invalid type of batch item.");
}
return index;
}
/**
*
* Create new changeset
*
* @public
*
* @returns {ChangeSet} created change set
*
* @memberof Batch
*/
createChangeSet() {
return this.add(ChangeSet);
}
/**
* Generate multipart/mixed content for the OData batch
*
* @param {String} csrfToken passed to create valid particular request in batch payload
*
* @returns {String} boundary used by the batch response
*
* @private
* @memberof Batch
*/
payload(csrfToken) {
let boundary = `--${this.boundary()}`;
return _.concat([
boundary,
_.map(this.requests, (request) => request.payload(csrfToken)).join(
`\n${boundary}\n`
),
`${boundary}--`,
]).join("\n");
}
/**
* Parse response from OData response and resolve/reject promises of the particular
* batch requests.
*
* @param {String} batchResponse - content of the response from http batch request
*
* @returns {Promise} promise which is resolved by the particular responses inside the batch
*
* @private
* @memberof Batch
*/
process(batchResponse) {
let promise;
let promises = [];
let boundary = this.boundaryFromResponse(batchResponse);
if (boundary) {
promise = batchResponse.text().then((batchResponseText) => {
_.chain(batchResponseText)
.split("\n")
.map((row) => row.trim())
.reduce((acc, row) => {
if (row === `--${boundary}`) {
if (acc.length) {
promises.push(
this.requests[acc.length - 1].process(acc[acc.length - 1])
);
}
acc.push([]);
} else if (row === `--${boundary}--`) {
Eif (acc.length) {
promises.push(
this.requests[acc.length - 1].process(acc[acc.length - 1])
);
}
} else {
acc[acc.length - 1].push(row);
}
return acc;
}, [])
.value();
return Promise.all(promises);
});
} else {
promise = Promise.reject(
new Error('Boundary not found in the "Content-Type" header')
);
}
return promise;
}
/**
* Create GET request in batch
*
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {ChangeSet} changeSet which contains newly created request
* @param {Number} useResponseType requested type of response constant defined
* in lib/engine/responseType
*
* @returns {Request} instance of batch Request
*
* @memberof Agent
*/
get(inputUrl, headers, changeSet, useResponseType) {
const batchRequest = this.addRequest(
"GET",
inputUrl,
headers,
undefined,
changeSet
);
batchRequest.responseType = useResponseType;
return batchRequest;
}
/**
* Create POST request in batch
*
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {Object} payload data which is converted to the JSON string and passed as body of POST request
* @param {ChangeSet} changeSet which contains newly created request
*
* @returns {Request} instance of batch Request
*
* @public
* @memberof Agent
*/
post(...args) {
const batchRequest = this.addRequestWithPayload("POST", ...args);
batchRequest.responseType = responseType.ENTITY;
return batchRequest;
}
/**
* Create PUT request in batch. The PUT request replaces entity by OData protocol
*
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {Object} payload data which is converted to the JSON string and passed as body of PUT request
* @param {ChangeSet} changeSet which contains newly created request
*
* @returns {Request} instance of batch Request
*
* @public
* @memberof Agent
*/
put(...args) {
return this.addRequestWithPayload("PUT", ...args);
}
/**
* Create MERGE request in batch. MERGE updates the entity.
* It is supported by OData protocol 1.0 and 2.0
*
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {Object} payload data which is converted to the JSON string and passed as body of MERGE request in batch
* @param {ChangeSet} changeSet which contains newly created request
*
* @returns {Request} instance of batch Request
*
* @public
* @memberof Agent
*/
merge(...args) {
return this.addRequestWithPayload("MERGE", ...args);
}
/**
* Create PATCH request in batch. Patch updates the entity.
* It is supported by OData protocol version 3.0 and later.
*
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {Object} payload data which is converted to the JSON string and passed as body of MERGE request in batch
* @param {ChangeSet} changeSet which contains newly created request
*
* @returns {Request} instance of batch Request
*
* @public
* @memberof Agent
*/
patch(...args) {
return this.addRequestWithPayload("PATCH", ...args);
}
/**
* Create DELETE request in batch
*
* @param {String} inputUrl relative path in the service
* @param {Object} headers object which contains headers used for the GET request
* @param {ChangeSet} changeSet which contains newly created request
*
* @returns {Request} instance of batch Request
*
* @memberof Agent
*/
delete(inputUrl, headers, changeSet) {
return this.addRequest("DELETE", inputUrl, headers, undefined, changeSet);
}
/**
* Determine boundary from from headers
*
* @param {String} batchResponse - content of the response from http batch request
*
* @returns {String} instance of batch Request
*
* @memberof Batch
*/
boundaryFromResponse(batchResponse) {
let boundary;
let contentType = batchResponse.headers.get("content-type");
if (_.isString(contentType)) {
boundary = _.get(
contentType.match(/^multipart\/mixed; boundary=([^\s]+)/),
1
);
}
return boundary;
}
}
module.exports = Batch;
|