JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1325
  • Score
    100M100P100Q104931F
  • License MIT

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (env-to-t3) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    env-to-t3

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

    Why?

    Accessing environment variables directly (e.g., process.env.API_KEY) in your TypeScript code is not safe. If you forget to add the environment variable, your code might break. That's why a tool like T3 Env can be very helpful. You can use your environment variables in a safe way and fully typed 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 the 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,
      },
    })

    Notice that you need to write NEXT_PUBLIC_PUBLISHABLE_KEY four times in total.

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

    Features

    • By default, all environment variables will be considered as string.
    • When the environment variable ends with _URL, it will be converted to a z.string().url() type.
    • When the environment variable has NEXT_PUBLIC_ prefix, it will be considered as a client-side environment variable.
    • When the environment variable has # required comment, it will be has a .min(1) constraint.
    • When the environment variable has # number comment, it will be has a .number() constraint.

    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

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

    import { createEnv } from '@t3-oss/env-nextjs'
    import { z } from 'zod'
    
    export const env = createEnv({
      server: {
        DATABASE_URL: z.string(),
        OPEN_AI_API_KEY: z.number().min(1),
        MINIMUM_DAYS: z.number(),
      },
      client: {
        NEXT_PUBLIC_PUBLISHABLE_KEY: z.string(),
      },
      experimental__runtimeEnv: {
        NEXT_PUBLIC_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_PUBLISHABLE_KEY,
      },
    })

    Install

    npm i -g env-to-t3

    Or you can also directly execute the command:

    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 --url "https://example.com"

    License

    MIT

    Contact

    Nico Prananta