-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathapiSettings.ts
177 lines (157 loc) · 5.35 KB
/
apiSettings.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
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
/*
MIT License
Copyright (c) 2021 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import type { ITransportSettings } from './transport';
import { agentPrefix, defaultTimeout } from './transport';
import { boolDefault, isTrue, unquote } from './constants';
/** Used for reading configuration name/value pairs */
export interface IValueSettings {
[name: string]: string;
}
/**
* Gets list of environment variable names for config values
* @param envPrefix Environment naming prefix to use. Pass an empty string to return no variable keys
* @constructor
*/
export const ApiConfigMap = (envPrefix: string): IValueSettings => {
if (!envPrefix) return {} as IValueSettings;
return {
base_url: `${envPrefix}_BASE_URL`,
client_id: `${envPrefix}_CLIENT_ID`,
client_secret: `${envPrefix}_CLIENT_SECRET`,
timeout: `${envPrefix}_TIMEOUT`,
verify_ssl: `${envPrefix}_VERIFY_SSL`,
};
};
export const strBadConfiguration = `${agentPrefix} configuration error:
Missing required configuration values like base_url
`;
export interface IApiSection {
[key: string]: string;
}
export interface IApiSettings extends ITransportSettings {
/**
* Reading API settings on demand from some configuration source
*/
readConfig(section?: string): IApiSection;
/**
* Checks to see if minimal configuration values are assigned.
*
* If this function returns `false`, the Api configuration class will typically throw a run-time
* error so the implementer knows required configuration values are missing
*
* @returns true or false
*/
isConfigured(): boolean;
}
/**
* default the runtime configuration settings
* @constructor
*
*/
export const DefaultSettings = () =>
({
agentTag: agentPrefix,
base_url: '',
timeout: defaultTimeout,
verify_ssl: true,
} as IApiSettings);
/**
* Return environment variable name value first, otherwise config name value
* @param {IValueSettings} values
* @param {string} name of config variable
* @param envKey key collection of environment variables
* @returns {string} environment value
*/
export const configValue = (
values: IValueSettings,
name: string,
envKey: IValueSettings
) => {
const val = values[envKey[name]] || values[name];
return typeof val === 'string' ? unquote(val) : val;
};
/**
* Read any key/value collection for environment variable names and return as IApiSettings
* @constructor
*
* The keys for the values are:
* - <environmentPrefix>_BASE_URL or `base_url`
* - <environmentPrefix>_CLIENT_ID or `client_id`
* - <environmentPrefix>_CLIENT_SECRET or `client_secret`
* - <environmentPrefix>_VERIFY_SSL or `verify_ssl`
* - <environmentPrefix>_TIMEOUT or `timeout`
*/
export const ValueSettings = (
values: IValueSettings,
envPrefix: string
): IApiSettings => {
const settings = DefaultSettings();
const envKey = ApiConfigMap(envPrefix);
settings.base_url =
configValue(values, 'base_url', envKey) || settings.base_url;
settings.verify_ssl = boolDefault(
configValue(values, 'verify_ssl', envKey),
true
);
settings.agentTag = `TS-SDK`;
const timeout = configValue(values, 'timeout', envKey);
settings.timeout = timeout ? parseInt(timeout, 10) : defaultTimeout;
return settings;
};
/**
* @class ApiSettings
*
* .ini Configuration initializer
*/
export class ApiSettings implements IApiSettings {
base_url = '';
verify_ssl = true;
timeout: number = defaultTimeout;
agentTag = agentPrefix;
constructor(settings: Partial<IApiSettings>) {
// coerce types to declared types since some paths could have non-conforming settings values
this.base_url =
'base_url' in settings ? unquote(settings.base_url) : this.base_url;
this.verify_ssl =
'verify_ssl' in settings
? isTrue(unquote(settings.verify_ssl?.toString()))
: this.verify_ssl;
this.timeout =
'timeout' in settings
? parseInt(unquote(settings.timeout?.toString()), 10)
: this.timeout;
if ('agentTag' in settings && settings.agentTag)
this.agentTag = settings.agentTag;
if (!this.isConfigured()) {
throw new Error(strBadConfiguration);
}
}
isConfigured() {
return !!this.base_url;
}
/**
* Default dynamic configuration reader
* @param _section key/name of configuration section to read
* @returns an empty `IAPISection`
*/
readConfig(_section?: string): IApiSection {
return {};
}
}