light icon indicating copy to clipboard operation
light copied to clipboard

Make configuring ENV variables easier

Open nahtnam opened this issue 3 years ago • 1 comments

One of the biggest annoyances with node apps is that you don't actually know what ENV variables you need to define, which have defaults, what the options are, etc. A lot of the time you typecast without assertions, etc.

I found this library: https://github.com/af/envalid which I think we can incorporate into light.

Instead of doing:

export const API_URL = process.env.API_URL as string;
// or
export const API_URL = process.env.API_URL || 'http://localhost:3000'

we can do something like this:

import { createEnv } from 'light'
const { env, str, json, port, ...rest } = createEnv()

str('API_URL', {
    devDefault: 'https://example.com/graphql',
});
export default env

Will provide a more in-depth solution with types in a comment. Theres absolutely no reason for this to be a closure/react hook form other than consistency

nahtnam avatar Dec 20 '21 04:12 nahtnam

I'm thinking a full example can look like this:

Usage:

import { useEnv } from 'light';

const { API_URL } = useEnv()

src/env.ts

const { env, str, json, port, ...rest } = createEnv()

str('API_URL', {
    devDefault: 'https://example.com/graphql',
});
export default env

light.d.ts

// TODO

nahtnam avatar Dec 20 '21 05:12 nahtnam