-
Notifications
You must be signed in to change notification settings - Fork 2
/
servers.ts
55 lines (47 loc) · 1.86 KB
/
servers.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
import { RequestContext, HttpMethod } from "./http/http";
export interface BaseServerConfiguration {
makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext;
}
/**
*
* Represents the configuration of a server including its
* url template and variable configuration based on the url.
*
*/
export class ServerConfiguration<T extends { [key: string]: string }> implements BaseServerConfiguration {
public constructor(private url: string, private variableConfiguration: T) {}
/**
* Sets the value of the variables of this server. Variables are included in
* the `url` of this ServerConfiguration in the form `{variableName}`
*
* @param variableConfiguration a partial variable configuration for the
* variables contained in the url
*/
public setVariables(variableConfiguration: Partial<T>) {
Object.assign(this.variableConfiguration, variableConfiguration);
}
public getConfiguration(): T {
return this.variableConfiguration
}
private getUrl() {
let replacedUrl = this.url;
for (const key in this.variableConfiguration) {
var re = new RegExp("{" + key + "}","g");
replacedUrl = replacedUrl.replace(re, this.variableConfiguration[key]);
}
return replacedUrl
}
/**
* Creates a new request context for this server using the url with variables
* replaced with their respective values and the endpoint of the request appended.
*
* @param endpoint the endpoint to be queried on the server
* @param httpMethod httpMethod to be used
*
*/
public makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext {
return new RequestContext(this.getUrl() + endpoint, httpMethod);
}
}
export const server1 = new ServerConfiguration<{ }>("", { })
export const servers = [server1];