-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathbatch-request-builder.ts
47 lines (45 loc) · 2.2 KB
/
batch-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
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { ErrorWithCause } from '@sap-cloud-sdk/util';
import {
Destination,
DestinationNameAndJwt,
DestinationOptions
} from '../connectivity/scp-cf';
import { BatchResponse } from '../odata-common';
import { parseBatchResponse } from '../odata-common/request-builder/batch/batch-response-parser';
import { BatchRequestBuilder } from '../odata-common/request-builder/batch/batch-request-builder';
import { deserializeBatchResponse } from '../odata-common/request-builder/batch/batch-response-deserializer';
import { responseDataAccessorV4 } from './request-builder/response-data-accessor';
import { entityDeserializerV4 } from './entity-deserializer';
/**
* Create a batch request to invoke multiple requests as a batch. The batch request builder accepts retrieve requests, i. e. [[GetAllRequestBuilder | getAll]] and [[GetByKeyRequestBuilder | getByKey]] requests and change sets, which in turn can contain [[CreateRequestBuilder | create]], [[UpdateRequestBuilder | update]] or [[DeleteRequestBuilder | delete]] requests.
* The retrieve and change sets will be excuted in order, while the order within a change set can vary.
*/
export class ODataBatchRequestBuilderV4 extends BatchRequestBuilder {
/**
* Execute the given request and return the according promise. Please notice: The sub-requests may fail even the main request is successful.
*
* @param destination - Targeted destination on which the request is performed.
* @param options - Options to employ when fetching destinations.
* @returns Promise resolving to the requested data.
*/
async execute(
destination: Destination | DestinationNameAndJwt,
options?: DestinationOptions
): Promise<BatchResponse[]> {
return this.build(destination, options)
.then(request => request.execute())
.then(response => parseBatchResponse(response))
.then(parsedResponse =>
deserializeBatchResponse(
parsedResponse,
this.entityToConstructorMap,
responseDataAccessorV4,
entityDeserializerV4
)
)
.catch(error => {
throw new ErrorWithCause('Batch request failed!', error);
});
}
}