Skip to content

EnvNest is a TypeScript library for NestJS that provides type-safe environment variable validation.

License

Notifications You must be signed in to change notification settings

broisnischal/nestenv

Repository files navigation

envnest is a TypeScript library for NestJS that provides type-safe environment variable validation and access using Zod schemas.

Installation

npm install envnest

Usage

1. Define your environment schema

Create a file to define your environment schema using Zod:

// src/config/env.config.ts
import { createEnvConfig, z } from "envnest";

const envSchema = z.object({
  NODE_ENV: z.enum(["development", "production"]).default("development"),
  PORT: z.string().transform(Number).default("3000"),
  DATABASE_URL: z.string().url(),
  TEST: z.string(),
});

export const envService = createEnvConfig(envSchema);

declare global {
  namespace NodeJS {
    interface ProcessEnv extends z.infer<typeof envSchema> {}
  }
}

2. Configure NestJS ConfigModule

In your app.module.ts file, set up the ConfigModule using the envService:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { envService } from './config/env.config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      validate: envService.validateConfig,
    }),
    // other imports...
  ],
  // ...
})
export class AppModule {}

3. Use the validated environment variables

You can now use the validated environment variables in your services:

import { Injectable } from '@nestjs/common';
import { envService } from './config/env.config';

@Injectable()
export class AppService {
  getHello(): string {
    const url = envService.config.get("DATABASE_URL");
    return `testing ${url} ${process.env.PORT}`;
  }
}

Features

  • Type-safe environment variable access
  • Automatic validation of environment variables
  • Default values support
  • Global type augmentation for process.env
  • Built-in presets for common configurations
  • Custom preset support

API Reference

  • createEnvConfig(schema: ZodSchema): Creates a configuration service with validation
  • envService.config: Typed ConfigService instance
  • envService.validateConfig: Validation function for use with NestJS ConfigModule

License

This project is licensed under the MIT License. See the LICENSE file for more details.

About

EnvNest is a TypeScript library for NestJS that provides type-safe environment variable validation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •