-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconfiguration.ts
More file actions
265 lines (255 loc) · 7.71 KB
/
configuration.ts
File metadata and controls
265 lines (255 loc) · 7.71 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {
HttpLibrary,
HttpConfiguration,
RequestContext,
ZstdCompressorCallback,
} from "./http/http";
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
import { BaseServerConfiguration, server1, servers } from "./servers";
import {
configureAuthMethods,
AuthMethods,
AuthMethodsConfiguration,
} from "./auth";
import { logger } from "./logger";
export class Configuration {
readonly baseServer?: BaseServerConfiguration;
readonly serverIndex: number;
readonly serverVariables: { [key: string]: string };
readonly operationServerIndices: { [name: string]: number };
readonly operationServerVariables: {
[name: string]: { [key: string]: string };
};
readonly httpApi: HttpLibrary;
readonly authMethods: AuthMethods;
readonly httpConfig: HttpConfiguration;
readonly debug: boolean | undefined;
readonly enableRetry: boolean | undefined;
readonly maxRetries: number | undefined;
readonly backoffBase: number | undefined;
readonly backoffMultiplier: number | undefined;
unstableOperations: { [name: string]: boolean };
servers: BaseServerConfiguration[];
public constructor(
baseServer: BaseServerConfiguration | undefined,
serverIndex: number,
serverVariables: { [key: string]: string },
operationServerIndices: { [name: string]: number },
operationServerVariables: { [name: string]: { [key: string]: string } },
httpApi: HttpLibrary,
authMethods: AuthMethods,
httpConfig: HttpConfiguration,
debug: boolean | undefined,
enableRetry: boolean | undefined,
maxRetries: number | undefined,
backoffBase: number | undefined,
backoffMultiplier: number | undefined,
unstableOperations: { [name: string]: boolean },
) {
this.baseServer = baseServer;
this.serverIndex = serverIndex;
this.serverVariables = serverVariables;
this.operationServerIndices = operationServerIndices;
this.operationServerVariables = operationServerVariables;
this.httpApi = httpApi;
this.authMethods = authMethods;
this.httpConfig = httpConfig;
this.debug = debug;
this.enableRetry = enableRetry;
this.maxRetries = maxRetries;
this.backoffBase = backoffBase;
this.backoffMultiplier = backoffMultiplier;
this.unstableOperations = unstableOperations;
this.servers = [];
for (const server of servers) {
this.servers.push(server.clone());
}
if (backoffBase && backoffBase < 2) {
throw new Error("Backoff base must be at least 2");
}
}
getServerAndOverrides(
key: string,
operationServers?: { [key: string]: BaseServerConfiguration[] },
): {
server: BaseServerConfiguration;
overrides?: { [key: string]: string };
} {
if (this.baseServer !== undefined) {
return { server: this.baseServer, overrides: this.serverVariables };
}
let server: BaseServerConfiguration;
let overrides: { [key: string]: string } | undefined;
if (operationServers && key in operationServers) {
const index = this.operationServerIndices[key] || 0;
server = operationServers[key][index];
overrides = this.operationServerVariables[key];
} else {
const index = this.serverIndex;
server = this.servers[index];
overrides = this.serverVariables;
}
return { server, overrides };
}
}
/**
* Interface with which a configuration object can be configured.
*/
export interface ConfigurationParameters {
/**
* Default server to use (takes preference over serverIndex and operationServerIndices)
*/
baseServer?: BaseServerConfiguration;
/**
* Default index of a server to use from the predefined server list
*/
serverIndex?: number;
/**
* Default server variables to override the default server variables
* Example:
* ```
* {
* "site": "datadoghq.com",
* }
*/
serverVariables?: { [name: string]: string };
/**
* Default index of a server to use for an operation from the API server list
* Key is the `{ApiName}.{ApiVersion}.{OperationName}`, value is the index of the server to use. Example:
* ```
* {
* "IPRangesApi.v1.getIPRanges": 0,
* }
*/
operationServerIndices?: { [name: string]: number };
/**
* Operation servers. Key is the `{ApiName}.{ApiVersion}.{OperationName}`, value is the object of variables to use. Example:
* ```
* {
* "IPRangesApi.v1.getIPRanges": { site: "datadoghq.com" },
* }
*/
operationServerVariables?: { [name: string]: { [key: string]: string } };
/**
* Custom `fetch` function
*/
fetch?: any;
/**
* HTTP library to use e.g. IsomorphicFetch
*/
httpApi?: HttpLibrary;
/**
* Configuration for the available authentication methods
*/
authMethods?: AuthMethodsConfiguration;
/**
* Configuration for HTTP transport
*/
httpConfig?: HttpConfiguration;
/**
* Flag to enable requests tracing
*/
debug?: boolean;
/**
* Callback method to compress string body with zstd
*/
zstdCompressorCallback?: ZstdCompressorCallback;
/**
* Maximum of retry attempts allowed
*/
maxRetries?: number;
/**
* Backoff base
*/
backoffBase?: number;
/**
* Backoff multiplier
*/
backoffMultiplier?: number;
/**
* Enable retry on status code 429 or 5xx
*/
enableRetry?: boolean;
}
/**
* Configuration factory function
*
* If a property is not included in conf, a default is used:
* - baseServer: null
* - serverIndex: 0
* - serverVariables: {}
* - operationServerIndices: {}
* - operationServerVariables: {}
* - httpApi: IsomorphicFetchHttpLibrary
* - authMethods: {}
* - httpConfig: {}
* - debug: false
*
* @param conf partial configuration
*/
export function createConfiguration(
conf: ConfigurationParameters = {},
): Configuration {
if (typeof process !== "undefined" && process.env && process.env.DD_SITE) {
const serverConf = server1.getConfiguration();
server1.setVariables({ site: process.env.DD_SITE } as typeof serverConf);
}
const authMethods = conf.authMethods || {};
if (
!("apiKeyAuth" in authMethods) &&
typeof process !== "undefined" &&
process.env &&
process.env.DD_API_KEY
) {
authMethods["apiKeyAuth"] = process.env.DD_API_KEY;
}
if (
!("appKeyAuth" in authMethods) &&
typeof process !== "undefined" &&
process.env &&
process.env.DD_APP_KEY
) {
authMethods["appKeyAuth"] = process.env.DD_APP_KEY;
}
const configuration = new Configuration(
conf.baseServer,
conf.serverIndex || 0,
conf.serverVariables || {},
conf.operationServerIndices || {},
conf.operationServerVariables || {},
conf.httpApi || new DefaultHttpLibrary(),
configureAuthMethods(authMethods),
conf.httpConfig || {},
conf.debug,
conf.enableRetry || false,
conf.maxRetries || 3,
conf.backoffBase || 2,
conf.backoffMultiplier || 2,
{},
);
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback;
configuration.httpApi.debug = configuration.debug;
configuration.httpApi.enableRetry = configuration.enableRetry;
configuration.httpApi.maxRetries = configuration.maxRetries;
configuration.httpApi.backoffBase = configuration.backoffBase;
configuration.httpApi.backoffMultiplier = configuration.backoffMultiplier;
configuration.httpApi.fetch = conf.fetch;
return configuration;
}
/**
* Apply given security authentication method if avaiable in configuration.
*/
export function applySecurityAuthentication<
AuthMethodKey extends keyof AuthMethods,
>(
conf: Configuration,
requestContext: RequestContext,
authMethods: AuthMethodKey[],
): void {
for (const authMethodName of authMethods) {
const authMethod = conf.authMethods[authMethodName];
if (authMethod) {
authMethod.applySecurityAuthentication(requestContext);
}
}
}