Skip to content

Commit

Permalink
Prevent client prefix on server (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
chungweileong94 committed May 22, 2023
1 parent af0d17d commit b35b8e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-pears-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t3-oss/env-core": patch
---

Prevent client prefix on server
17 changes: 14 additions & 3 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export interface BaseOptions<
* Specify your server-side environment variables schema here. This way you can ensure the app isn't
* built with invalid env vars.
*/
server: TServer;
server: {
[TKey in keyof TServer]: TKey extends `${TPrefix}${string}`
? ErrorMessage<`${TKey extends `${TPrefix}${string}`
? TKey
: never} should not prefixed with ${TPrefix}.`>
: TServer[TKey];
};

/**
* Specify your client-side environment variables schema here. This way you can ensure the app isn't
Expand Down Expand Up @@ -87,7 +93,11 @@ export interface StrictOptions<
? TKey
: never;
}[keyof TClient]
| keyof TServer,
| {
[TKey in keyof TServer]: TKey extends `${TPrefix}${string}`
? never
: TKey;
}[keyof TServer],
string | boolean | number | undefined
>;
runtimeEnv?: never;
Expand All @@ -109,8 +119,9 @@ export function createEnv<
if (skip) return runtimeEnv as any;

const _client = typeof opts.client === "object" ? opts.client : {};
const _server = typeof opts.server === "object" ? opts.server : {};
const client = z.object(_client);
const server = z.object(opts.server);
const server = z.object(_server);
const isServer = opts.isServer ?? typeof window === "undefined";

const merged = server.merge(client);
Expand Down
15 changes: 15 additions & 0 deletions packages/core/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ function ignoreErrors(cb: () => void) {
}
}

test("server vars should not be prefixed", () => {
ignoreErrors(() => {
createEnv({
clientPrefix: "FOO_",
server: {
// @ts-expect-error - server should not have FOO_ prefix
FOO_BAR: z.string(),
BAR: z.string(),
},
client: {},
runtimeEnv: {},
});
});
});

test("client vars should be correctly prefixed", () => {
ignoreErrors(() => {
createEnv({
Expand Down
16 changes: 16 additions & 0 deletions packages/nextjs/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ function ignoreErrors(cb: () => void) {
}
}

test("server vars should not be prefixed", () => {
ignoreErrors(() => {
createEnv({
server: {
// @ts-expect-error - server should not have NEXT_PUBLIC_ prefix
NEXT_PUBLIC_BAR: z.string(),
BAR: z.string(),
},
client: {},
runtimeEnv: {
BAR: "foo",
},
});
});
});

test("client vars should be correctly prefixed", () => {
ignoreErrors(() => {
createEnv({
Expand Down

1 comment on commit b35b8e8

@vercel
Copy link

@vercel vercel bot commented on b35b8e8 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

t3-env – ./

t3-env-git-main-t3-oss.vercel.app
t3-env.vercel.app
t3-env-t3-oss.vercel.app
env.t3.gg

Please sign in to comment.