typedconf
v1.1.0
Published
Properly typed configuration for your TypeScript project
Downloads
19
Maintainers
Readme
TypedConf - Your config, perfectly typed
Fully Typed Configuration for your Typescript project.
Just access your configuration keys like you would any other object in your code, typechecking included!
Hello :
const db = cfg.easy.key.access.db; //{host:string..}
const maybe = cfg.safe.undefined?.access // string | undefined
Goodbye :
const dbHost : string = cfg.get("key.which.might.have.been.renamed.host")
const dbPort : number = cfg.get("key.which.might.have.been.renamed.port")
const dbDatabase : string = cfg.get("key.which.might.have.been.renamed.db")
const dbSchema : string = cfg.get("key.which.might.have.been.renamed.schema")
const key : number = cfg.get("key.uuuh.....")
const other : string = cfg.get("key.oh.no.typo")
Result:
✔️ Always know all keys which are safe to access
✔️ Always know when a key has been removed or renamed
✔️ Always know what config keys exist
Getting Started
npm install typedconf
Example
import { ConfigBuilder } from 'typedconf';
interface ConfigSchema {
database: {
host: string;
port: number;
schemas: string[];
enabled: boolean;
};
api: {
host: string;
port: number;
};
}
export const cfg = new ConfigBuilder<ConfigSchema>()
// Apply a default config
.applyStaticConfig({
database: {
host: 'localhost',
port: 5432,
schemas: ['public'],
enabled: true,
},
})
// Load environment variables, Loads all variables prefixed with "MY_APP_" using "_" as a delimiter
// E.g "MY_APP_DATABASE_HOST" will be loaded to cfg.database.host
// Every value will be parsed with JSON.parse()
.loadEnv(env, 'MY_APP_', '_')
// [Only where "fs" is available] Loads a json file from disk - will use "json5" if available. File can be optional or required.
.loadJsonFile("./config.json", true)
.buildConfig();
// Now our typechecker knows all fields that might be undefined and ensures that they are handled properly.
cfg.database.host; // defined
cfg.api?.port; // possibly undefined
// We can use the `require` method to ensure that required fields are defined and throw an error if they are not.
const checkedcfg = cfg.require({
api: {
port: true,
host: true,
},
});
checkedcfg.api.port; // now defined
// Even dynamic config access is typechecked
cfg.get('database.enabled'); // Defined Boolean
//cfg.get('database.something'); // Type Error - "database.something" does not extend config type.
cfg.get<bool>('database.something', true); // bool | undefined - Allows accessing paths dynamically in e.g. functions
more examples can be found in the examples
directory
Modules
This Project uses microbundle to generate ESM, CJS and UMD modules and should work in browser and node environments
License
Licensed under the MIT. See the LICENSE file for details.