Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@arundo/typed-env

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arundo/typed-env

Typed environment variables made easy 🚀

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@arundo/typed-env

Typed environment variables using Zod.

Installation

npm install @arundo/typed-env

# or

yarn add @arundo/typed-env

# or

pnpm add @arundo/typed-env

Usage

Create a environment file with a schema and use typeEnvironment to create a typed environment object:

// environment.ts
import { z } from 'zod';
import { typeEnvironment } from '@arundo/typed-env';

export const envSchema = z.object({
  NODE_ENV: z.enum(['test', 'development', 'production']),
  PORT: z.coerce.number().int().default(3000),
});

export const environment = typeEnvironment(envSchema);

Import the environment object and use it:

// server.ts
import { environment } from './environment';

console.log(environment.NODE_ENV); // 'development' - type string
console.log(environment.PORT); // 3000 - type number

Set naming convention of environment variables:

// environment.ts

/* ... as usual ... */

export const environment = typeEnvironment(envSchema, { transform: 'camelcase' });
// server.ts
import { environment } from './environment';

console.log(environment.nodeEnv); // 'development' - type string
console.log(environment.port); // 3000 - type number

If you for some reason need to access the raw environment object, you can add types like this:

// environment.ts
import { z } from 'zod';
import { typeEnvironment } from '@arundo/typed-env';

export const envSchema = z.object({
  PORT: z.coerce.number().int().default(3000),
});

export const environment = typeEnvironment(envSchema, { transform: 'camelcase' });

declare global {
  namespace NodeJS {
    interface ProcessEnv extends Record<keyof z.infer<typeof envSchema>, string | undefined> {}
  }
}

console.log(process.env.PORT); // '3000' - type string | undefined

Keywords

FAQs

Package last updated on 10 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc