0.0.5 • Published 12 months ago

devaseel-aura v0.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

Before starting, please note that Aura is currently in beta and is not production-ready.

About the Library

Aura is a library for validating environment variables at runtime.

Installation

To install the library, you need to run the following:

npm install devaseel-aura zod dotenv

Usage

To use the library start by creating a new env.ts file, and add the following:

import { z } from "zod";
import { config } from "dotenv";
import { validateEnv } from "./validate";

config();

const envSchema = z.object({
  // specify the schema of your env variables here
});

export const env: z.infer<typeof envSchema> = {
  // import the env variables here
};

validateEnv(envSchema, env);

Examples

Here's an example of how to use the library:

import { z } from "zod";
import { config } from "dotenv";
import { validateEnv } from "./validate";

config();

const envSchema = z.object({
  API_KEY: z.string().nonempty(),
  DATABASE_URL: z.string().url(),
  PORT: z.number(),
});

export const env: z.infer<typeof envSchema> = {
  // add '!' for strict types
  API_KEY: process.env.API_KEY!,
  DATABASE_URL: process.env.DATABASE_URL!,
  PORT: parseInt(process.env.PORT as string),
};

validateEnv(envSchema, env);

Running validations

To run the validations, you can either run the file env.ts or include it before application run step in package.json:

"start": "node dist/env.js && ..."

Results

Success:

✅ Environment variables are valid!

Faliure:

❌ Invalid environment variables:
DATABASE_URL: Invalid url
❌ Invalid environment variables:
API_KEY: Required

Usage in developments

To use the environment varaibles, you need to import env to your file:

import { env } from "./env";

this would allow you to access the env vraibles along with its types:

env variable usage in development

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago