-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathget-all-request-builder-base.ts
126 lines (120 loc) · 4.04 KB
/
get-all-request-builder-base.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
import { variadicArgumentToArray } from '@sap-cloud-sdk/util';
import { Constructable, EntityBase } from '../entity';
import { Selectable } from '../selectable/selectable';
import { Orderable } from '../order/orderable';
import { ODataGetAllRequestConfig } from '../request/odata-get-all-request-config';
import {
DestinationOptions,
Destination,
DestinationNameAndJwt
} from '../../connectivity/scp-cf';
import { CountRequestBuilder } from '../request-builder/count-request-builder';
import { EntityDeserializer } from '../entity-deserializer';
import { ResponseDataAccessor } from '../response-data-accessor';
import { GetRequestBuilderBase } from './get-request-builder-base';
/**
* Base class for the get all request builders [[GetAllRequestBuilderV2]] and [[GetAllRequestBuilderV4]]
*
* @typeparam EntityT - Type of the entity to be requested
*/
export abstract class GetAllRequestBuilderBase<
EntityT extends EntityBase
> extends GetRequestBuilderBase<EntityT, ODataGetAllRequestConfig<EntityT>> {
/**
* Creates an instance of GetAllRequestBuilder.
*
* @param entityConstructor - Constructor of the entity to create the request for
* @param getAllRequestConfig - Request config of the get all request.
*/
constructor(
entityConstructor: Constructable<EntityT>,
getAllRequestConfig: ODataGetAllRequestConfig<EntityT>,
readonly entityDeserializer: EntityDeserializer,
readonly dataAccessor: ResponseDataAccessor
) {
super(entityConstructor, getAllRequestConfig);
}
/**
* Restrict the response to the given selection of properties in the request.
*
* @param selects - Fields to select in the request
* @returns The request builder itself, to facilitate method chaining
*/
select(...selects: Selectable<EntityT>[]): this;
select(selects: Selectable<EntityT>[]): this;
select(
first: undefined | Selectable<EntityT> | Selectable<EntityT>[],
...rest: Selectable<EntityT>[]
): this {
this.requestConfig.selects = variadicArgumentToArray(first, rest);
return this;
}
/**
* Add order-by statements to the request.
*
* @param orderBy - OrderBy statements to order the response by
* @returns The request builder itself, to facilitate method chaining
*/
orderBy(orderBy: Orderable<EntityT>[]): this;
orderBy(...orderBy: Orderable<EntityT>[]): this;
orderBy(
first: undefined | Orderable<EntityT> | Orderable<EntityT>[],
...rest: Orderable<EntityT>[]
): this {
this.requestConfig.orderBy = variadicArgumentToArray(first, rest);
return this;
}
/**
* Limit number of returned entities.
*
* @param top - Maximum number of entities to return in the response. Can be less, if less entities match the request
* @returns The request builder itself, to facilitate method chaining
*/
top(top: number): this {
this.requestConfig.top = top;
return this;
}
/**
* Skip number of entities.
*
* @param skip - Number of matching entities to skip. Useful for paging
* @returns The request builder itself, to facilitate method chaining
*/
skip(skip: number): this {
this.requestConfig.skip = skip;
return this;
}
/**
* Count the number of entities.
*
* @returns A count request builder for execution
*/
count(): CountRequestBuilder<EntityT> {
return new CountRequestBuilder(this);
}
/**
* Execute request.
*
* @param destination - Destination to execute the request against
* @param options - Options to employ when fetching destinations
* @returns A promise resolving to the requested entities
*/
async execute(
destination: Destination | DestinationNameAndJwt,
options?: DestinationOptions
): Promise<EntityT[]> {
return this.build(destination, options)
.then(request => request.execute())
.then(response =>
this.dataAccessor
.getCollectionResult(response.data)
.map(json =>
this.entityDeserializer.deserializeEntity(
json,
this._entityConstructor,
response.headers
)
)
);
}
}