2.1.1 • Published 1 month ago

paranoid-config v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

paranoid-config

Config loader with type safety and validations

About

This package is based on config, so you should already be familiar with it. This package is best used with typescript and @sinclair/typebox for schema definition as it also provides types.

Warning: This package is native ESM and doesn't provide a CommonJS export. If your project uses CommonJS, you will have to convert to ESM.

Usage

npm install --save paranoid-config @sinclair/typebox

In your project config/local.json:

{
  "db": {
    "host": "localhost",
    "port": 3000,
    "credentials": {}
  }
}

config.ts

import {Static, Type} from "@sinclair/typebox"
import getConfig from "paranoid-config";

const schema = Type.Object({
    db: Type.Object({
        host: Type.String(),
        port: Type.Number({minimum: 0}),
        credentials: Type.Object({
            user: Type.String(),
            pass: Type.String()
        })
    })
})

type ConfigType = Static<typeof schema>

export default getConfig<ConfigType>(schema);

getConfig will validate the configuration object provided by config package and you can be sure your application does not fail because of missing required configurations.

Using typebox you also benefit from having types extracted from your schema.

You can also add defaults and other validations using typebox

import {Type} from "@sinclair/typebox"

Type.String({
    minLength: 3,
    default: "localhost"
})