generated from MapColonies/ts-npm-package-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor!: get options programmatically be the user * chore: more work on integration * chore: changed version to original * fix: removed unneeded call for config * refactor: changed version to serviceVersion * fix: removed commented line
- Loading branch information
1 parent
d1b779c
commit 8599539
Showing
12 changed files
with
323 additions
and
114 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,97 @@ | ||
import { hostname } from 'os'; | ||
import * as env from 'env-var'; | ||
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; | ||
version: 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; | ||
|
||
const getCommonConfig = (): CommonConfig => { | ||
function getCommonConfig(options: Partial<CommonConfig>): CommonConfig { | ||
if (commonConfig) { | ||
return commonConfig; | ||
} | ||
|
||
const packageConfig = loadPackageInfo(); | ||
commonConfig = { | ||
serviceName: env.get('TELEMETRY_SERVICE_NAME').asString() ?? packageConfig.name, | ||
hostname: env.get('TELEMETRY_HOST_NAME').asString() ?? hostname(), | ||
version: env.get('TELEMETRY_SERVICE_VERSION').asString() ?? packageConfig.version, | ||
}; | ||
commonConfig = mergeAndValidateConfig<CommonConfig>(options, envCommonConfig, commonConfigSchema); | ||
|
||
return commonConfig; | ||
}; | ||
} | ||
|
||
export { CommonConfig, getCommonConfig }; | ||
export { CommonConfig, getCommonConfig, mergeAndValidateConfig }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
} & {}; |
Oops, something went wrong.