Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Redesign node-config-ts #86

Open
tusharmath opened this issue Feb 4, 2023 · 3 comments
Open

Feature: Redesign node-config-ts #86

tusharmath opened this issue Feb 4, 2023 · 3 comments

Comments

@tusharmath
Copy link
Owner

It's been long, and I think this library needs a re-write :)

Here is the first draft of the proposed design.

Define your config using the following signature. This is better than a pre-build step

import { Schema } from 'node-config-ts'

const schema = Schema.type({
    port: Schema.number,
    maxRetries: Schema.number,
    secret: Schema.optional(Schema.string)
})

Then we create Config Sources. Sources are just a way to read arbitrary data and convert it into a JSON format.

import {Source} from 'node-config-ts'
const yml: Source = Source.json("./default.yml")

We can create any arbitrary source also using the make constructor.

const cli: Source = Source.make(() => minimist.parse(process.args))
const defaults: Source= Source.make(() => ({port: 8090, maxRetries: 20}))

We can merge the sources as follows:

const source = cli.orElse(yml).orElse(defaults)

Once a source is ready we can convert to config using the schema

export const config = source.as(schema)
export type Config = typeof config

In your application you can now use config to access value, and use Config as it's type.

@tusharmath
Copy link
Owner Author

Instead of building Schema from scratch, we could also use https://github.com/tusharmath/tts-schema

@mbwhite
Copy link

mbwhite commented May 27, 2024

Hi @tusharmath; I've just been looking for a typescript library to handle a more complex configuration; this is one of the first I came across. Looks promising but I missed the typing I had in say env-var. So I like the approach above.

I can assist at all..

@tusharmath
Copy link
Owner Author

Perfect! Let me know if you need anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants