-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathodata-create-request-config.ts
57 lines (50 loc) · 1.56 KB
/
odata-create-request-config.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
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { Constructable, EntityBase } from '../entity';
import { Link } from '../selectable';
import { ODataUri } from '../uri-conversion';
import { ODataRequestConfig } from './odata-request-config';
/**
* OData create request configuration for an entity type.
*
* @typeparam EntityT - Type of the entity to setup a request for
*/
export class ODataCreateRequestConfig<
EntityT extends EntityBase
> extends ODataRequestConfig {
/**
* Keys of the parent of the entity to create. Defined only when attempting to create child entities.
*/
parentKeys: Record<string, any>;
/**
* Field that links the parent entity class to the child entity class.
*/
childField: Link<EntityBase, EntityT>;
/**
* Creates an instance of ODataRequest.
* @param _entityConstructor - Constructor type of the entity to create a configuration for
*/
constructor(
readonly _entityConstructor: Constructable<EntityT>,
private oDataUri: ODataUri
) {
super('post', _entityConstructor._defaultServicePath);
}
resourcePath(): string {
return this.parentKeys === undefined
? this._entityConstructor._entityName
: this.resourcePathAsChild();
}
queryParameters(): Record<string, any> {
return this.prependDollarToQueryParameters({});
}
protected resourcePathAsChild() {
return (
this.oDataUri.getResourcePathForKeys(
this.parentKeys,
this.childField._entityConstructor
) +
'/' +
this.childField._fieldName
);
}
}