@soluzioni-futura/config-component
v4.1.1
Published
## Quick start
Downloads
114
Readme
config-component
Quick start
Install the package
npm i @soluzioni-futura/config-component
Usage
Simple setup example
import ConfigComponent, { ProcessEnvConfigDriver } from "@soluzioni-futura/config-component"
type ConfigState = {
SECRET: string
}
const config = new ConfigComponent<ConfigState>([
new ProcessEnvConfigDriver()
])
await config.fetch()
console.log(config.state.SECRET)
With validation schema
import ConfigComponent, { ProcessEnvDriver } from "@soluzioni-futura/config-component"
type ConfigState = {
SECRET: string
}
const config = new ConfigComponent<ConfigState>([
new ProcessEnvDriver(),
validationSchema: {
type: "object",
additionalProperties: true,
properties: {
SECRET: { type: "string" }
},
required: ["SECRET"]
}
])
await config.fetch()
console.log(config.state.SECRET)
Complete setup example
import ConfigComponent, { ProcessEnvDriver, SSMConfigDriver } from "@soluzioni-futura/config-component"
const { NODE_ENV = "local" } = process.env
const validationSchema = {
type: "object",
additionalProperties: true,
properties: {
SECRET: { type: "string" }
},
required: ["SECRET"]
}
const drivers = ["staging", "production"].includes(NODE_ENV) ?
[
new SSMConfigDriver({
keys: ["ENV"],
prefix: `/secrets/${NODE_ENV}`
ssm: new SSM({
region: "eu-west-1"
})
})
] : [
new ProcessEnvDriver()
]
const config = new ConfigComponent(drivers, validationSchema)
await config.fetch()