Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samialdury/envey
Type-safe configuration schemas for Node.js.
https://github.com/samialdury/envey
config configuration env envey environment node nodejs schema validation zod
Last synced: about 1 month ago
JSON representation
Type-safe configuration schemas for Node.js.
- Host: GitHub
- URL: https://github.com/samialdury/envey
- Owner: samialdury
- License: mit
- Created: 2023-04-07T10:09:53.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-14T13:43:58.000Z (4 months ago)
- Last Synced: 2024-12-24T12:15:06.178Z (about 1 month ago)
- Topics: config, configuration, env, envey, environment, node, nodejs, schema, validation, zod
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/envey
- Size: 1020 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `envey`
[![CI status](https://github.com/samialdury/envey/actions/workflows/ci.yml/badge.svg)](https://github.com/samialdury/envey/actions/workflows/ci.yml)
[![license](https://img.shields.io/github/license/samialdury/envey)](LICENSE)
[![npm version](https://img.shields.io/npm/v/envey)](https://www.npmjs.com/package/envey)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](https://zod.dev/)'s excellent type system.
## Installation
```sh
pnpm i -E zod envey
```## Usage
```ts
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](https://zod.dev/?id=type-inference):
```ts
const schema = {
logLevel: {
env: 'LOG_LEVEL',
format: z.enum([
'fatal',
'error',
'warn',
'info',
'debug',
'trace',
'silent',
]),
},
} satisfies EnveySchematype Config = InferEnveyConfig
// ^? {
// readonly logLevel: "fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent"
// }
```## License
[MIT](LICENSE)