generated from MapColonies/ts-npm-package-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ts
97 lines (81 loc) · 2.68 KB
/
config.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
import { hostname } from 'os';
import Ajv, { type JSONSchemaType } from 'ajv';
import addFormats from 'ajv-formats';
import { betterAjvErrors } from '@apideck/better-ajv-errors';
import { loadPackageInfo } from '../common/packageInfoLoader';
/**
* Represents the configuration for common settings.
*/
interface CommonConfig {
/**
* The name of the service to put as attribute.
* By default will be read from the package.json file.
*/
serviceName: string;
/**
* The value of the hostname attribute to use, will override the hostname.
*/
hostname: string;
/**
* The version of the service to put as attribute.
* By default will be read from the package.json file.
*/
serviceVersion: string;
}
const ajv = addFormats(
new Ajv({
useDefaults: true,
coerceTypes: true,
allErrors: true,
verbose: true,
})
);
const JSON_INDENTATION = 2;
function mergeAndValidateConfig<T extends Record<keyof T, string | boolean | undefined | null | number>>(
inputConfig: Partial<T>,
envConfig: Partial<Record<keyof T, string>>,
schema: JSONSchemaType<T>
): T {
// clean up undefined values so that they don't override the defaults
for (const key in envConfig) {
if (envConfig[key] === undefined) {
delete envConfig[key];
}
}
const mergedConfig = { ...inputConfig, ...envConfig };
const isValid = ajv.validate(schema, mergedConfig);
if (!isValid) {
const betterErrors = betterAjvErrors({
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
schema: schema as Parameters<typeof betterAjvErrors>[0]['schema'],
data: mergedConfig,
errors: ajv.errors,
});
throw new Error(JSON.stringify(betterErrors, null, JSON_INDENTATION));
}
return mergedConfig;
}
const packageJsonInfo = loadPackageInfo();
const commonConfigSchema: JSONSchemaType<CommonConfig> = {
type: 'object',
properties: {
serviceName: { type: 'string', default: packageJsonInfo.name },
hostname: { type: 'string', default: hostname() },
serviceVersion: { type: 'string', default: packageJsonInfo.version },
},
required: ['serviceName', 'hostname', 'serviceVersion'],
};
const envCommonConfig: Partial<CommonConfig> = {
serviceName: process.env.TELEMETRY_SERVICE_NAME,
hostname: process.env.TELEMETRY_HOST_NAME,
serviceVersion: process.env.TELEMETRY_SERVICE_VERSION,
};
let commonConfig: CommonConfig | undefined;
function getCommonConfig(options: Partial<CommonConfig>): CommonConfig {
if (commonConfig) {
return commonConfig;
}
commonConfig = mergeAndValidateConfig<CommonConfig>(options, envCommonConfig, commonConfigSchema);
return commonConfig;
}
export { CommonConfig, getCommonConfig, mergeAndValidateConfig };