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

env-to-t3

v0.0.11

Published

This is a CLI to generate TypeScript code to safely use environment variables with [T3 env](https://env.t3.gg).

Downloads

24

Readme

env-to-t3

This is a CLI to generate TypeScript code to safely use environment variables with T3 env.

Why?

Direct access to environment variables (e.g. process.env.API_KEY) in your TypeScript code is not safe. If you forget to add the environment variable, your code may break. That's why a tool like T3 Env can be very helpful. You can use your environment variables in a safe and fully typed way thanks to Zod.

But writing the code is a bit tedious. For example, you have 3 environment variables that you want to use in your code. During development, you need to write the .env file like this

DATABASE_URL=postgres://localhost:5432/my-app
OPEN_AI_API_KEY=1234567890
NEXT_PUBLIC_PUBLISHABLE_KEY=1234567890

Then write the code like this:

import { createEnv } from '@t3-oss/env-nextjs'
import { z } from 'zod'

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url(),
    OPEN_AI_API_KEY: z.string().min(1),
  },
  client: {
    NEXT_PUBLIC_PUBLISHABLE_KEY: z.string().min(1),
  },
  experimental__runtimeEnv: {
    NEXT_PUBLIC_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_PUBLISHABLE_KEY,
  },
})

Note that you have to write NEXT_PUBLIC_PUBLISHABLE_KEY four times in total.

By using this script, you don't have to write the code by hand anymore. Just run the script and it will generate the code for you.

Features

  • By default, all environment variables are considered to be strings.
  • If the environment variable ends with _URL, it will be converted to a z.string().url() type.
  • If the environment variable has a NEXT_PUBLIC_ prefix, it is considered a client-side environment variable. You can override this prefix by using the --client-prefix flag.
  • If the environment variable has a # required comment, it has a .min(1) constraint.
  • If the environment variable has a # number comment, it will have a .number({ coerce: true }) constraint.
  • If the environment variable has the # default comment, it will have a .default()constraint. The value of the default will be the value of the environment variable if it exists, or 0 if it is a number, or an empty string if it is a string.

For example, you have the following .env file:

DATABASE_URL=abcd
OPEN_AI_API_KEY=1 # number required
NEXT_PUBLIC_PUBLISHABLE_KEY=abcd
MINIMUM_DAYS=1 # number
MINIMUM_MEMBERS=10 # number default required

Running the env-to-t3 script will produce the following env.ts file:

  server: {
    DATABASE_URL: z.string().optional(),
    OPEN_AI_API_KEY: z.number({ coerce: true }).min(1),
    MINIMUM_DAYS: z.number({ coerce: true }).default(1).optional(),
    MINIMUM_MEMBERS: z.number({ coerce: true }).default(10),
  },
  client: {
    NEXT_PUBLIC_PUBLISHABLE_KEY: z.string().optional(),
  },
  experimental__runtimeEnv: {
    NEXT_PUBLIC_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_PUBLISHABLE_KEY,
  }
});

Install

npm i -g env-to-t3

Or you can run the command directly:

npx env-to-t3 -i .env

CLI

  Usage
    $ env-to-t3 [input]

  Options
    --input, -i <type> The path to the environment file.  [Default: .env]
    --output, -o The path to write the output. [Default: env.ts]
    --client-prefix, -cp The prefix for client-side environment variables. [Default: NEXT_PUBLIC_]

  Examples
    $ env-to-t3 --input .env

Notes

Development

Run

npx tsx source/cli.tsx -i "./__tests__/.env"

License

MIT

Contact

Nico Prananta