@@ -2,18 +2,78 @@ import { HttpLibrary, HttpConfiguration, RequestContext, ZstdCompressorCallback
2
2
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
3
3
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
4
4
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
5
+ import { logger } from "../../logger";
5
6
6
- export interface Configuration {
7
+ export class Configuration {
7
8
readonly baseServer?: BaseServerConfiguration;
8
9
readonly serverIndex: number;
9
- readonly operationServerIndices: { [ name: string ]: number };
10
+ readonly operationServerIndices: { [name: string]: number };
10
11
readonly httpApi: HttpLibrary;
11
12
readonly authMethods: AuthMethods;
12
13
readonly httpConfig: HttpConfiguration;
13
14
readonly debug: boolean | undefined;
14
15
unstableOperations: { [name: string]: boolean };
15
- }
16
+ servers: BaseServerConfiguration[];
17
+ operationServers: { [endpoint: string]: BaseServerConfiguration[] };
18
+
19
+ public constructor(
20
+ baseServer: BaseServerConfiguration | undefined,
21
+ serverIndex: number,
22
+ operationServerIndices: { [name: string]: number },
23
+ httpApi: HttpLibrary,
24
+ authMethods: AuthMethods,
25
+ httpConfig: HttpConfiguration,
26
+ debug: boolean | undefined,
27
+ unstableOperations: { [name: string]: boolean },
28
+ ) {
29
+ this.baseServer = baseServer;
30
+ this.serverIndex = serverIndex;
31
+ this.operationServerIndices = operationServerIndices;
32
+ this.httpApi = httpApi;
33
+ this.authMethods = authMethods;
34
+ this.httpConfig = httpConfig;
35
+ this.debug = debug;
36
+ this.unstableOperations = unstableOperations;
37
+ this.servers = [];
38
+ for (const server of servers) {
39
+ this.servers.push(server.clone());
40
+ }
41
+ this.operationServers = {};
42
+ for (const endpoint in operationServers) {
43
+ this.operationServers[endpoint] = [];
44
+ for (const server of operationServers[endpoint]) {
45
+ this.operationServers[endpoint].push(server.clone());
46
+ }
47
+ }
48
+ }
16
49
50
+ setServerVariables(serverVariables: { [key: string]: string }): void {
51
+ if (this.baseServer !== undefined) {
52
+ this.baseServer.setVariables(serverVariables);
53
+ return;
54
+ }
55
+
56
+ const index = this.serverIndex;
57
+ this.servers[index].setVariables(serverVariables);
58
+
59
+ for (const op in this.operationServers) {
60
+ this.operationServers[op][0].setVariables(serverVariables);
61
+ }
62
+ }
63
+
64
+ getServer(endpoint: string): BaseServerConfiguration {
65
+ if (this.baseServer !== undefined) {
66
+ return this.baseServer;
67
+ }
68
+ const index =
69
+ endpoint in this.operationServerIndices
70
+ ? this.operationServerIndices[endpoint]
71
+ : this.serverIndex;
72
+ return endpoint in operationServers
73
+ ? this.operationServers[endpoint][index]
74
+ : this.servers[index];
75
+ }
76
+ }
17
77
18
78
/**
19
79
* Interface with which a configuration object can be configured.
@@ -85,11 +145,15 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
85
145
{% - endif %}
86
146
{% - endfor %}
87
147
88
- const configuration: Configuration = {
89
- baseServer: conf.baseServer,
90
- serverIndex: conf.serverIndex || 0,
91
- operationServerIndices: conf.operationServerIndices || {},
92
- unstableOperations: {
148
+ const configuration = new Configuration(
149
+ conf.baseServer,
150
+ conf.serverIndex || 0,
151
+ conf.operationServerIndices || {},
152
+ conf.httpApi || new DefaultHttpLibrary(),
153
+ configureAuthMethods(authMethods),
154
+ conf.httpConfig || {},
155
+ conf.debug,
156
+ {
93
157
{% - for version , api in apis .items () %}
94
158
{% - for operations in api .values () %}
95
159
{% - for _ , _ , operation in operations |sort (attribute ="2.operationId" ) %}
@@ -100,22 +164,15 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
100
164
{% - endfor %}
101
165
{% - endfor %}
102
166
},
103
- httpApi: conf.httpApi || new DefaultHttpLibrary(),
104
- authMethods: configureAuthMethods(authMethods),
105
- httpConfig: conf.httpConfig || {},
106
- debug: conf.debug
107
- };
167
+ );
108
168
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback
109
169
configuration.httpApi.debug = configuration.debug;
110
170
return configuration;
111
171
}
112
172
113
173
export function getServer(conf: Configuration, endpoint: string): BaseServerConfiguration {
114
- if (conf.baseServer !== undefined) {
115
- return conf.baseServer;
116
- }
117
- const index = (endpoint in conf.operationServerIndices) ? conf.operationServerIndices[endpoint] : conf.serverIndex;
118
- return (endpoint in operationServers) ? operationServers[endpoint][index] : servers[index];
174
+ logger.warn("getServer is deprecated, please use Configuration.getServer instead.");
175
+ return conf.getServer(endpoint);
119
176
}
120
177
121
178
/**
@@ -124,17 +181,8 @@ export function getServer(conf: Configuration, endpoint: string): BaseServerConf
124
181
* @param serverVariables key/value object representing the server variables (site, name, protocol, ...)
125
182
*/
126
183
export function setServerVariables(conf: Configuration, serverVariables: { [key: string]: string }): void {
127
- if (conf.baseServer !== undefined) {
128
- conf.baseServer.setVariables(serverVariables);
129
- return ;
130
- }
131
-
132
- const index = conf.serverIndex;
133
- servers[index].setVariables(serverVariables);
134
-
135
- for (const op in operationServers) {
136
- operationServers[op][0].setVariables(serverVariables);
137
- }
184
+ logger.warn("setServerVariables is deprecated, please use Configuration.setServerVariables instead.");
185
+ return conf.setServerVariables(serverVariables);
138
186
}
139
187
140
188
/**
0 commit comments