1.0.1 • Published 2 years ago

dotenv-ajv v1.0.1

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
2 years ago

dotenv-ajv

npm version npm downloads

dotenv-ajv is a JS/TS library use to load and validate your env files using JSON schema

Installation

npm i dotenv-ajv

Usage

First create a .env file

NODE_ENV=development
TOKEN=yourapptoken

Next create JSON schema using typebox in a env.type.ts file

import {Static, Type} from "@sinclair/typebox"; // `npm i @sinclair/typebox` is required

export enum NodeEnv {
    dev= 'development',
    prod = 'production',
    staging = 'staging'
}

export const EnvSchema = Type.Object({
    NODE_ENV: Type.Enum(NodeEnv, {
        default: NodeEnv.dev
    }),
    PORT: Type.String({
        default: '3000'
    }),
    TOKEN: Type.String()
});

export type EnvType = Static<typeof EnvSchema>;

// Create this function if you want auto-complete
export function ENV(): EnvType {
    return process.env as EnvType;
}

Finally, in the index.ts load and validate your env file

import {loadAndValidateEnv} from "dotenv-ajv";
import {EnvSchema, ENV} from "./env.type.ts";

loadAndValidateEnv(EnvSchema); // Throws an error if your .env file isn't valid

// Now you can use the ENV function
console.log(ENV().PORT); // 3000
console.log(ENV().NODE_ENV); // development
console.log(ENV().TOKEN); // yourapptoken
1.0.1

2 years ago

1.0.0

2 years ago