@ptenn/config-provider
v1.2.0
Published
### Installation
Downloads
6
Readme
config-provider
Installation
Run yarn add @ptenn/config-provider
or npm add @ptenn/config-provider
.
Usage
To set up a config provider, use the following as a guideline:
// config/version.ts
import { Config, ExtractValueFn } from '@redicycle/config-provider';
export interface VersionConfigShape {
version: string;
}
export default(ExtractValue: ExtractValueFn) => {
return new Config<VersionConfigShape>({
version: ExtractValue({
name: 'version',
value: 'unknown',
}).string,
});
}
// config/server.ts
import { Config, ExtractValueFn } from '@redicycle/config-provider';
export interface ServerConfigShape {
port: number;
}
export default(ExtractValue: ExtractValueFn) => {
return new Config<ServerConfigShape>({
port: ExtractValue({
name: 'port',
value: 8888,
}).int,
});
}
// config/index.ts
import { ConfigProvider } from '@redicycle/config-provider';
import { inspect } from 'util';
import version, { VersionConfigShape } from './version';
import server, { ServerConfigShape } from './server';
export interface ConfigShape {
version: VersionConfigShape,
server: ServerConfigShape,
}
const provider = new ConfigProvider({
environmentPrefix: 'serviceName',
delimiter: '_', // optional, defaults to '_'
environmentVariableName: 'NODE_ENV', // optional, defaults to 'NODE_ENV'
});
provider.addConfig<VersionConfigShape>(version, 'version');
provider.addConfig<ServerConfigShape>(server, 'server');
const config = provider.getConfig();
if (process.env.SHOW_CONFIG === 'true') {
console.log(inspect(config, true, 5, true));
}
export default config;
Publishing
Publish by running npm publish --access=restricted