npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tonik/env-plus

v0.1.10

Published

Environment variables toolkit for node applications

Downloads

120

Readme

Env+

Toolkit for managing type-safe environment variables with the feature flags built-in support. Heavily influenced by t3-env.

Features

  • Full type safety
  • API compatible with @t3-oss/env-core
  • Built-in feature flags support
  • Full schema transformation support

Installation

pnpm add @tonik/env-plus zod

How to define env schema

// src/env.mjs
// @ts-check

import { createEnv } from "@tonik/env-plus";
import { z } from "zod";

export const env = createEnv({
  /**
   * Example settings for Next.js
   */
  isServer: typeof window === "undefined"
  clientPrefix: "NEXT_PUBLIC_"
  /**
   * All envs here will be avaliable to read when isServer is true
   */
  server: {
    DATABASE_URL: z.string().url(),
    SERVER_ENV: z.string(),
    /**
     * You can define feature flag schema here.
     * You can use any input here as long it returns boolean.
     */
    GOOGLE_AUTH_ENABLED: z
      .enum(["true", "false"])
      .default("false")
      .transform((e) => e === "true"),

    /**
     * Here are variables that will become required when flag is set to "true".
     * For now define them as optionals.
     */
    GOOGLE_CLIENT_ID: z.string().min(1).optional(),
    GOOGLE_CLIENT_SECRET: z.string().min(1).optional()
  },
  /**
   * All envs here will be avaliable to read when isServer is false.
   *
   * You can set here anything prefixed with value in `clientPrefix`
   */
  client: {
    NEXT_PUBLIC_PLASMIC_PUBLIC_KEY: z.string().min(1),
    NEXT_PUBLIC_CLIENT_ENV: z.string().min(1)
  },
  /**
   * Custom env transformation. Usefull when you want to apply custom
   * logic based on shared, server, or client schema.
   *
   * It can define new keys that will be available for featureFlags definitions.
   */
  transform: (env) => {
    return {
        CUSTOM_FLAG: true,
        SOME_DYNAMIC_ENV: `${env.NEXT_PUBLIC_CLIENT_ENV}${env.SERVER_ENV}`
    }
  },
  /**
   * Define relations between envs here.
   * 1st level - boolean envs.
   * 2nd level - all envs that will drop `optional` when 1st level env is set to true.
   *
   * Only `true` as a value allows droping `optional` schema right now. Negation isn't implemented.
   */
  featureFlags: {
    CUSTOM_FLAG: {
        SOME_DYNAMIC_ENV: true
    },
    GOOGLE_AUTH_ENABLED: {
        GOOGLE_CLIENT_ID: true,
        GOOGLE_CLIENT_SECRET: true
    }
  },
  runtimeEnv: {
    DATABASE_URL: process.env.DATABASE_URL,
    SERVER_ENV: process.env.SERVER_ENV,
    NEXT_PUBLIC_PLASMIC_PUBLIC_KEY:
      process.env.NEXT_PUBLIC_PLASMIC_PUBLIC_KEY,
    NEXT_PUBLIC_CLIENT_ENV: process.env.NEXT_PUBLIC_CLIENT_ENV,
    GOOGLE_AUTH_ENABLED: process.env.GOOGLE_AUTH_ENABLED,
    GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
    GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET
  },
});

How to use env in the application

import { env } from "./src/env.mjs";

// all defined envs are typed and runtime avaliable
env.DATABASE_URL; // type string
env.CUSTOM_FLAG; // type boolean
env.SOME_DYNAMIC_VALUE; // type string

env.GOOGLE_CLIENT_ID; // type string | undefined
env.GOOGLE_CLIENT_SECRET; // type string | undefined

if (env.GOOGLE_AUTH_ENABLED) {
  env.GOOGLE_CLIENT_ID; // type string (undefined dropped)
  env.GOOGLE_CLIENT_SECRET; // type string (undefined dropped)
}

Enable type-checking by including env.mjs file in your tsconfig.json and turning on allowJs flag

// tsconfig.json
{
  "compilerOptions": {
    "allowJs": true
  },
  "include": ["src/env.mjs"]
}

License

Licensed under the MIT license.