Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
g3-tin opened this issue Jan 30, 2024 · 2 comments
Open

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

g3-tin opened this issue Jan 30, 2024 · 2 comments
Labels
PRs Accepted Feel free to pick this up and make a PR

Comments

@g3-tin
Copy link

g3-tin commented Jan 30, 2024

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
Copy link
Member

Similar to unjs/std-env#115

@juliusmarminge juliusmarminge added the PRs Accepted Feel free to pick this up and make a PR label Mar 24, 2024
@juliusmarminge
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PRs Accepted Feel free to pick this up and make a PR
Projects
None yet
Development

No branches or pull requests

2 participants