-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcreate-request-builder-base.ts
104 lines (99 loc) · 3.41 KB
/
create-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
import { ErrorWithCause } from '@sap-cloud-sdk/util';
import {
DestinationOptions,
Destination,
DestinationNameAndJwt
} from '../../connectivity/scp-cf';
import type { EntitySerializer } from '../entity-serializer';
import type { ODataUri } from '../uri-conversion/odata-uri';
import type { Constructable, EntityBase, EntityIdentifiable } from '../entity';
import type { EntityDeserializer } from '../entity-deserializer';
import type { ResponseDataAccessor } from '../response-data-accessor';
import { ODataCreateRequestConfig } from '../request';
import type { Link } from '../selectable';
import { MethodRequestBuilderBase } from './request-builder-base';
/**
* Abstract create request class holding the parts shared in OData v2 and v4.
*
* @typeparam EntityT - Type of the entity to be created
*/
export abstract class CreateRequestBuilderBase<EntityT extends EntityBase>
extends MethodRequestBuilderBase<ODataCreateRequestConfig<EntityT>>
implements EntityIdentifiable<EntityT> {
/**
* Creates an instance of CreateRequestBuilder.
*
* @param _entityConstructor - Constructor type of the entity to be created
* @param _entity - Entity to be created
*/
constructor(
readonly _entityConstructor: Constructable<EntityT>,
readonly _entity: EntityT,
readonly odataUri: ODataUri,
readonly serializer: EntitySerializer,
readonly deserializer: EntityDeserializer,
readonly responseDataAccessor: ResponseDataAccessor
) {
super(new ODataCreateRequestConfig(_entityConstructor, odataUri));
this.requestConfig.payload = serializer.serializeEntity(
this._entity,
this._entityConstructor
);
}
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 of the query.
* @returns the builder itself
*/
prepare(): this {
this.requestConfig.payload = this.serializer.serializeEntity(
this._entity,
this._entityConstructor
);
return this;
}
/**
* Specifies the parent of the entity to create.
*
* @param parentEntity - Parent of the entity to create
* @param linkField - Static representation of the navigation property that navigates from the parent entity to the child entity
* @returns The entity itself, to facilitate method chaining
*/
asChildOf<ParentEntityT extends EntityBase>(
parentEntity: ParentEntityT,
linkField: Link<ParentEntityT, EntityT>
): this {
this.requestConfig.parentKeys = this.odataUri.getEntityKeys(
parentEntity,
linkField._entityConstructor
);
this.requestConfig.childField = linkField;
return this;
}
/**
* Execute query.
*
* @param destination - Destination to execute the request against
* @param options - Options to employ when fetching destinations
* @returns A promise resolving to the created entity
*/
async execute(
destination: Destination | DestinationNameAndJwt,
options?: DestinationOptions
): Promise<EntityT> {
return this.build(destination, options)
.then(request => request.execute())
.then(response =>
this.deserializer.deserializeEntity(
this.responseDataAccessor.getSingleResult(response.data),
this._entityConstructor,
response.headers
)
)
.catch(error => {
throw new ErrorWithCause('Create request failed!', error);
});
}
}