Skip to content

Commit b0a3c03

Browse files
api-clients-generation-pipeline[bot]NouemanKHALci.datadog-api-spec
authored
Improve typescript server management (#322)
* add a setServerVariables helper function * fix comment * update configuration * remove unused import * update comments * use setVariables * change baseServer to be of type ServerConfiguration * change BaseServerConfiguration and ServerConfiguration * remove templates from BaseServer * fix class extension and variableConfiguration type * export setServerVariables function * remove any type * add return type for getConfiguration * Regenerate client from commit e0d002a of spec repo Co-authored-by: NouemanKHAL <[email protected]> Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com> Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 0608599 commit b0a3c03

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

.apigentools-info

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"spec_versions": {
55
"v1": {
66
"apigentools_version": "1.5.1.dev2",
7-
"regenerated": "2021-10-28 15:03:03.917351",
8-
"spec_repo_commit": "f510509"
7+
"regenerated": "2021-10-29 09:17:36.074895",
8+
"spec_repo_commit": "e0d002a"
99
},
1010
"v2": {
1111
"apigentools_version": "1.5.1.dev2",
12-
"regenerated": "2021-10-28 15:03:03.938056",
13-
"spec_repo_commit": "f510509"
12+
"regenerated": "2021-10-29 09:17:36.104666",
13+
"spec_repo_commit": "e0d002a"
1414
}
1515
}
1616
}

.generator/templates/configuration.mustache

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
8080
let serverConf = server1.getConfiguration();
8181
server1.setVariables({"site": process.env.DD_SITE} as (typeof serverConf));
8282
for (const op in operationServers) {
83-
(operationServers[op][0] as any).setVariables({ site: process.env.DD_SITE });
83+
operationServers[op][0].setVariables({ site: process.env.DD_SITE });
8484
}
8585
}
8686

@@ -120,3 +120,22 @@ export function getServer(conf: Configuration, endpoint: string): BaseServerConf
120120
let index = (endpoint in conf.operationServerIndices) ? conf.operationServerIndices[endpoint] : conf.serverIndex;
121121
return (endpoint in operationServers) ? operationServers[endpoint][index] : servers[index];
122122
}
123+
124+
/**
125+
* Sets the server variables.
126+
*
127+
* @param serverVariables key/value object representing the server variables (site, name, protocol, ...)
128+
*/
129+
export function setServerVariables(conf: Configuration, serverVariables: { [key: string]: string }) {
130+
if (conf.baseServer !== undefined) {
131+
conf.baseServer.setVariables(serverVariables);
132+
return ;
133+
}
134+
135+
let index = conf.serverIndex;
136+
servers[index].setVariables(serverVariables);
137+
138+
for (const op in operationServers) {
139+
operationServers[op][0].setVariables(serverVariables);
140+
}
141+
}

.generator/templates/http/servers.mustache

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
import { RequestContext, HttpMethod } from "./http/http{{extensionForDeno}}";
22

3-
export interface BaseServerConfiguration {
4-
makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext;
5-
}
6-
73
/**
84
*
9-
* Represents the configuration of a server including its
10-
* url template and variable configuration based on the url.
5+
* Represents the configuration of a server
116
*
127
*/
13-
export class ServerConfiguration<T extends { [key: string]: string }> implements BaseServerConfiguration {
14-
public constructor(private url: string, private variableConfiguration: T) {}
8+
export class BaseServerConfiguration {
9+
public constructor(private url: string, private variableConfiguration: { [key: string]: string }) {}
1510

1611
/**
1712
* Sets the value of the variables of this server.
1813
*
1914
* @param variableConfiguration a partial variable configuration for the variables contained in the url
2015
*/
21-
public setVariables(variableConfiguration: Partial<T>) {
16+
public setVariables(variableConfiguration: { [key: string]: string }) {
2217
Object.assign(this.variableConfiguration, variableConfiguration);
2318
}
2419

25-
public getConfiguration(): T {
20+
public getConfiguration(): { [key: string]: string } {
2621
return this.variableConfiguration
2722
}
2823

@@ -48,6 +43,14 @@ export class ServerConfiguration<T extends { [key: string]: string }> implements
4843
}
4944
}
5045

46+
/**
47+
*
48+
* Represents the configuration of a server including its
49+
* url template and variable configuration based on the url.
50+
*
51+
*/
52+
export class ServerConfiguration<T extends { [key: string]: string }> extends BaseServerConfiguration {}
53+
5154
{{#servers}}
5255
export const server{{-index}} = new ServerConfiguration<{ {{#variables}}
5356
"{{name}}": {{#enumValues}}"{{.}}"{{^-last}} | {{/-last}}{{/enumValues}}{{^enumValues}}string{{/enumValues}}{{^-last}},{{/-last}}

.generator/templates/index.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export * from "./http/http{{extensionForDeno}}";
22
export * from "./auth/auth{{extensionForDeno}}";
33
export { createConfiguration } from "./configuration{{extensionForDeno}}"
4+
export { setServerVariables } from "./configuration{{extensionForDeno}}"
5+
46
export{{#platforms}}{{#deno}} type{{/deno}}{{/platforms}} { Configuration } from "./configuration{{extensionForDeno}}"
57
export * from "./apis/exception{{extensionForDeno}}";
68
export * from "./servers{{extensionForDeno}}";

0 commit comments

Comments
 (0)