Skip to content

Commit

Permalink
feat(core): expose isServer value to reuse across app (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guria committed Aug 13, 2024
1 parent b55ca31 commit 70995c1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/great-eyes-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t3-oss/env-core": minor
---

expose isServer value to reuse across app
2 changes: 2 additions & 0 deletions examples/astro/src/t3-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export const env = createEnv({
skipValidation: import.meta.env.SKIP_ENV_VALIDATION === "development",
clientPrefix: "PUBLIC_",
});

const isServer = env.isServer;
2 changes: 2 additions & 0 deletions examples/nextjs/app/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export const env = createEnv({
},
extends: [vercel()],
});

const isServer = env.isServer;
2 changes: 2 additions & 0 deletions examples/nuxt/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const env = createEnv({
NUXT_PUBLIC_GREETING: z.string(),
},
});

const isServer = env.isServer;
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export type CreateEnv<
TypeOf<ZodObject<TClient>> &
TypeOf<ZodObject<TShared>> &
UnReadonlyObject<Reduce<TExtends>>
>
> & { isServer: boolean }
>;

export function createEnv<
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,25 @@ describe("extending presets", () => {
});
});
});

describe("isServer is correctly exposed", () => {
test("for server", () => {
const env = createEnv({
server: {},
runtimeEnv: {},
isServer: true,
});

expect(env.isServer).toBe(true);
});

test("for client", () => {
const env = createEnv({
server: {},
runtimeEnv: {},
isServer: false,
});

expect(env.isServer).toBe(false);
});
});

0 comments on commit 70995c1

Please sign in to comment.