t3-oss / t3-env

https://env.t3.gg
MIT License
2.82k stars 88 forks source link

Support both process.env and import.meta.env #177

Open g3-tin opened 10 months ago

g3-tin commented 10 months ago
export const appClientEnv = createEnv({
  client: {
    NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID: z
      .string(),
    NEXT_PUBLIC_BLOCKCHAIN_CONFIG: z.preprocess(
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      (val) => JSON.parse(val),
      z.record(z.string(), networkConfigSchema)
    ),
    NEXT_PUBLIC_PRIVY_APP_ID: z.string(),
    NEXT_PUBLIC_G3_ENV: z
      .enum(["development", "staging", "production"])
      .default("development"),
    NEXT_PUBLIC_BASE_CHAIN: z.string().default("sepolia"),
    NEXT_PUBLIC_ALCHEMY_API_KEY: z
      .string(),
    NEXT_PUBLIC_INTERNAL_BACKEND_URL: z.string().url().optional(),
    NEXT_PUBLIC_DEFAULT_IMAGE: z
      .string()
      .default("https://placehold.co/600x600?text=No+avatar"),
  },
  clientPrefix: "NEXT_PUBLIC_",
  runtimeEnvStrict: {
    NEXT_PUBLIC_BASE_CHAIN:
      process.env.NEXT_PUBLIC_BASE_CHAIN ?? import.meta.env.VITE_BASE_CHAIN,
    NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID:
      process.env.NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID ??
      import.meta.env.VITE_WALLETCONNECT_PROJECT_ID,
    NEXT_PUBLIC_BLOCKCHAIN_CONFIG:
      process.env.NEXT_PUBLIC_BLOCKCHAIN_CONFIG ??
      import.meta.env.VITE_BLOCKCHAIN_CONFIG,
    NEXT_PUBLIC_PRIVY_APP_ID:
      process.env.NEXT_PUBLIC_PRIVY_APP_ID ?? import.meta.env.VITE_PRIVY_APP_ID,
    NEXT_PUBLIC_G3_ENV:
      process.env.NEXT_PUBLIC_G3_ENV ?? import.meta.env.VITE_G3_ENV,
    NEXT_PUBLIC_ALCHEMY_API_KEY:
      process.env.NEXT_PUBLIC_ALCHEMY_API_KEY ??
      import.meta.env.VITE_ALCHEMY_API_KEY,
    NEXT_PUBLIC_INTERNAL_BACKEND_URL:
      process.env.NEXT_PUBLIC_INTERNAL_BACKEND_URL ??
      import.meta.env.VITE_INTERNAL_BACKEND_URL,
    NEXT_PUBLIC_DEFAULT_IMAGE:
      process.env.NEXT_PUBLIC_DEFAULT_IMAGE ??
      import.meta.env.VITE_DEFAULT_IMAGE,
  },
});

I have a schema like this. This will be very bad if I use process.env and import.env the same time. But since I share this environment cross over nextjs repo and vite repo. So I want it to support this kind.

Is there any better ways?

juliusmarminge commented 9 months ago

Similar to https://github.com/unjs/std-env/issues/115

juliusmarminge commented 8 months ago

I think we should contribute to std-env and handle this there (see the linked issue above) - and then just add a recommendation for people depending on import.meta.env to use std-env