2.6.3 • Published 4 months ago

envey v2.6.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

envey

CI status license npm version

Envey is a library designed to simplify the process of managing and validating environment variables in Node.js applications. It provides a fully type-safe solution for defining and parsing configuration schemas, leveraging the power of Zod's excellent type system and validation features.

As of v2.6.0, it also supports nested objects. See here for more details.

Why

I was looking for something like convict, but with the type safety and validation features of Zod. Hence, I decided to create this library.

Installation

pnpm i -E zod envey

Usage

import { z } from 'zod'
import { createConfig } from 'envey'

const result = createConfig(
    z,
    {
        databaseUrl: {
            env: 'DATABASE_URL',
            format: z.string(),
        },
        port: {
            env: 'PORT',
            format: z.coerce.number().int().positive().max(65535),
        },
    },
    { validate: true },
)

if (!result.success) {
    console.error(result.error.issues)
    // Handle error
}

const { config } = result
//    ^? {
//           readonly databaseUrl: string;
//           readonly port: number;
//       }

Supports schema type inference, similar to Zod's infer:

const schema = {
    logLevel: {
        env: 'LOG_LEVEL',
        format: z.enum([
            'fatal',
            'error',
            'warn',
            'info',
            'debug',
            'trace',
            'silent',
        ]),
    },
} satisfies EnveySchema

type Config = InferEnveyConfig<typeof schema>
//   ^? {
//          readonly logLevel:  "fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent"
//      }

Nested objects

import { z } from 'zod'
import { createConfig } from 'envey'

const result = createConfig(
    z,
    {
        postgres: {
            host: {
                env: 'PG_HOST',
                format: z.string().default('localhost'),
            },
            port: {
                env: 'PG_PORT',
                format: z.coerce.number().int().positive().max(65535).default(5432),
            }
            user: {
                env: 'PG_USER',
                format: z.string().default('postgres'),
            },
            password: {
                env: 'PG_PASSWORD',
                format: z.string().min(1),
            },
            database: {
                env: 'PG_DATABASE',
                format: z.string().min(1),
            },
        }
    },
    { validate: true },
)

if (!result.success) {
    console.error(result.error.issues)
    // Handle error
}

const { postgres } = result.config
//    ^? {
//           readonly host: string;
//           readonly port: number;
//           readonly user: string;
//           readonly password: string;
//           readonly database: string;
//       }

License

MIT

2.6.1

4 months ago

2.6.0

4 months ago

2.6.3

4 months ago

2.6.2

4 months ago

2.5.0

5 months ago

2.4.0

12 months ago

2.3.0

1 year ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

1.2.0

2 years ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago