Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Make configuring ENV variables easier #1054

Open
nahtnam opened this issue Dec 20, 2021 · 1 comment
Open

Make configuring ENV variables easier #1054

nahtnam opened this issue Dec 20, 2021 · 1 comment

Comments

@nahtnam
Copy link
Contributor

nahtnam commented Dec 20, 2021

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
Copy link
Contributor Author

nahtnam commented Dec 20, 2021

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

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

No branches or pull requests

1 participant