Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-fishes-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t3-oss/env-core": minor
---

support multiple client prefixes
5 changes: 3 additions & 2 deletions docs/src/app/docs/core/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ export const env = createEnv({

/**
* The prefix that client-side variables must have. This is enforced both at
* a type-level and at runtime.
* a type-level and at runtime. Can be a single string or an array of strings
* if you want to use multiple client prefixes.
*/
clientPrefix: "PUBLIC_",
clientPrefix: "PUBLIC_", // or ["PUBLIC_", "NEXT_PUBLIC_"],

client: {
PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
Expand Down
31 changes: 25 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@
* Common examples of prefixes are `NEXT_PUBLIC_`, `NUXT_PUBLIC` or `PUBLIC_`.
*/
export interface ClientOptions<
TPrefix extends string | undefined,
TClient extends StandardSchemaDictionary,
TPrefix extends string | string[] | undefined,
TClient extends Record<string, StandardSchemaV1>,
> {
/**
* The prefix that client-side variables must have. This is enforced both at
Expand All @@ -212,9 +212,21 @@
* built with invalid env vars.
*/
client: Partial<{
[TKey in keyof TClient]: TKey extends `${TPrefix}${string}`
? TClient[TKey]
: ErrorMessage<`${TKey extends string ? TKey : never} is not prefixed with ${TPrefix}.`>;
[TKey in keyof TClient]: TPrefix extends string
? TKey extends `${TPrefix}${string}`
? TClient[TKey]
: ErrorMessage<`${TKey extends string
? TKey
: never} is not prefixed with ${TPrefix}.`>
: TPrefix extends string[]
? TKey extends `${infer Prefix}${string}`
? Prefix extends TPrefix[number]
? TClient[TKey]
: ErrorMessage<`${TKey extends string
? TKey
: never} is not prefixed with any of ${TPrefix[number]}.`>
: never
: never;
}>;
}

Expand Down Expand Up @@ -368,7 +380,7 @@
const onInvalidAccess =
opts.onInvalidAccess ??
(() => {
throw new Error("❌ Attempted to access a server-side environment variable on the client");

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-valibot.test.ts > shared can be accessed on both server and client > client

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-valibot.test.ts:381:16

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > with built-in preset

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-arktype.test.ts:804:14

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > overriding preset env var

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-arktype.test.ts:784:14

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > empty 'extends' array should not cause type errors

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-arktype.test.ts:757:15

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > createFinalSchema > custom schema combiner

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > extending presets > multiple presets > client

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-arktype.test.ts:619:18

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > extending presets > single preset > client

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-arktype.test.ts:530:18

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > envs are readonly

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46

Check failure on line 383 in packages/core/src/index.ts

View workflow job for this annotation

GitHub Actions / test

packages/core/test/smoke-arktype.test.ts > shared can be accessed on both server and client > client

Error: ❌ Attempted to access a server-side environment variable on the client ❯ packages/core/src/index.ts:383:13 ❯ Object.get packages/core/src/index.ts:417:46 ❯ packages/core/test/smoke-arktype.test.ts:381:16
});

if (parsed.issues) {
Expand All @@ -377,7 +389,14 @@

const isServerAccess = (prop: string) => {
if (!opts.clientPrefix) return true;
return !prop.startsWith(opts.clientPrefix) && !(prop in _shared);

const prefixes = Array.isArray(opts.clientPrefix)
? opts.clientPrefix
: [opts.clientPrefix];

return !prefixes.some(
(prefix) => prop.startsWith(prefix) && prop in _shared,
);
};
const isValidServerAccess = (prop: string) => {
return isServer || !isServerAccess(prop);
Expand Down
Loading