From 3e3a4229a9fbe7d57b5600f655089e930e0f1227 Mon Sep 17 00:00:00 2001 From: Aleksei Gurianov Date: Tue, 13 Aug 2024 23:12:51 +0300 Subject: [PATCH] Expose internal value for isServer Fixes #258 Expose the internal value for `isServer` for reuse across the app. * **packages/core/src/index.ts** - Add `isServer` property to the `CreateEnv` return type. - Update the `createEnv` function to include `isServer` in the proxy handler. * **examples/astro/src/t3-env.ts** - Import `isServer` value from the `env` object. * **examples/nextjs/app/env.ts** - Import `isServer` value from the `env` object. * **examples/nuxt/env.ts** - Import `isServer` value from the `env` object. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/t3-oss/t3-env/issues/258?shareId=XXXX-XXXX-XXXX-XXXX). --- examples/astro/src/t3-env.ts | 2 ++ examples/nextjs/app/env.ts | 2 ++ examples/nuxt/env.ts | 2 ++ packages/core/src/index.ts | 3 ++- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/astro/src/t3-env.ts b/examples/astro/src/t3-env.ts index 868e3eee..a6f0164b 100644 --- a/examples/astro/src/t3-env.ts +++ b/examples/astro/src/t3-env.ts @@ -15,3 +15,5 @@ export const env = createEnv({ skipValidation: import.meta.env.SKIP_ENV_VALIDATION === "development", clientPrefix: "PUBLIC_", }); + +const isServer = env.isServer; diff --git a/examples/nextjs/app/env.ts b/examples/nextjs/app/env.ts index ec089fe9..c2106415 100644 --- a/examples/nextjs/app/env.ts +++ b/examples/nextjs/app/env.ts @@ -18,3 +18,5 @@ export const env = createEnv({ }, extends: [vercel()], }); + +const isServer = env.isServer; diff --git a/examples/nuxt/env.ts b/examples/nuxt/env.ts index ce3023f6..d91e381b 100644 --- a/examples/nuxt/env.ts +++ b/examples/nuxt/env.ts @@ -9,3 +9,5 @@ export const env = createEnv({ NUXT_PUBLIC_GREETING: z.string(), }, }); + +const isServer = env.isServer; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 72770be2..8eacbbd9 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -209,7 +209,7 @@ export type CreateEnv< TypeOf> & TypeOf> & UnReadonlyObject> - > + > & { isServer: boolean } >; export function createEnv< @@ -294,6 +294,7 @@ export function createEnv< if (typeof prop !== "string") return undefined; if (ignoreProp(prop)) return undefined; if (!isValidServerAccess(prop)) return onInvalidAccess(prop); + if (prop === "isServer") return isServer; return Reflect.get(target, prop); }, // Maybe reconsider this in the future: