-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathodata-request-config.ts
132 lines (117 loc) · 3.83 KB
/
odata-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
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
import {
createLogger,
mergeIgnoreCase,
VALUE_IS_UNDEFINED
} from '@sap-cloud-sdk/util';
export type RequestMethodType = 'get' | 'post' | 'patch' | 'delete' | 'put';
const logger = createLogger({
package: '@sap-cloud-sdk/core',
messageContext: 'odata-request-config'
});
/**
* @hidden
*/
export abstract class ODataRequestConfig {
payload: Record<string, any> | string;
customServicePath: string;
readonly defaultHeaders: Record<string, any> = {
'content-type': 'application/json',
accept: 'application/json'
};
private _customHeaders: Record<string, string> = {};
private _customQueryParameters: Record<string, string> = {};
/**
* @deprecated Since v1.30.0. Use [[defaultHeaders]] instead.
*/
get contentType() {
return this.defaultHeaders['content-type'];
}
/**
* @deprecated Since v1.30.0.
*/
constructor(
method: RequestMethodType,
defaultServicePath: string,
contentType: string
);
constructor(
method: RequestMethodType,
defaultServicePath: string,
defaultHeaders?: Record<string, any>
);
/**
* Creates an instance of ODataRequest.
*
* @param method - HTTP method of the request
* @param defaultServicePath - default path of the according service
* @param defaultHeadersOrContentType - The default headers of the given request as an object. When passing a string only set the content type header will be set. Setting the content type only is deprecated since v1.30.0.
*/
constructor(
public method: RequestMethodType,
readonly defaultServicePath: string,
defaultHeadersOrContentType?: Record<string, any> | string
) {
if (defaultServicePath === VALUE_IS_UNDEFINED) {
logger.warn('The service path is undefined in "_defaultServicePath".');
}
if (typeof defaultHeadersOrContentType === 'string') {
this.defaultHeaders['content-type'] = defaultHeadersOrContentType;
} else {
this.defaultHeaders = mergeIgnoreCase(
this.defaultHeaders,
defaultHeadersOrContentType
);
}
}
set customHeaders(headers: Record<string, string>) {
this._customHeaders = {};
this.addCustomHeaders(headers);
}
get customHeaders(): Record<string, string> {
return this._customHeaders;
}
set customQueryParameters(queryParameters: Record<string, string>) {
this._customQueryParameters = {};
this.addCustomQueryParameters(queryParameters);
}
get customQueryParameters(): Record<string, string> {
return this._customQueryParameters;
}
/**
* Add custom headers to the request. This is useful in case you want to provide your own authorization headers for example.
*
* @param headers - Key-value pairs where the key is the name of a header property and the value is the respective value
*/
addCustomHeaders(headers: Record<string, string>): void {
Object.entries(headers).forEach(([key, value]) => {
// Enforce lower case as HTTP headers are case-insensitive
this.customHeaders[key.toLowerCase()] = value;
});
}
/**
* Add custom query parameters to the request. This is useful in case your OData service allows non-standard query parameters.
*
* @param queryParameters - Key-value pairs where the key is the name of a query parameter and the value is the respective value
*/
addCustomQueryParameters(queryParameters: Record<string, string>): void {
Object.entries(queryParameters).forEach(([key, value]) => {
this.customQueryParameters[key] = value;
});
}
protected prependDollarToQueryParameters(
params: Record<string, any>
): Record<string, any> {
return Object.entries(params).reduce((newParams, [key, value]) => {
newParams[`$${key}`] = value;
return newParams;
}, {});
}
/**
* @hidden
*/
abstract resourcePath(): string;
/**
* @hidden
*/
abstract queryParameters(): Record<string, any>;
}