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 @@ -58,9 +58,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: 24 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export interface StrictOptions<
}

export interface ClientOptions<
TPrefix extends string | undefined,
TPrefix extends string | string[] | undefined,
TClient extends Record<string, StandardSchemaV1>,
> {
/**
Expand All @@ -144,11 +144,21 @@ export interface ClientOptions<
* 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 @@ -278,7 +288,14 @@ export function createEnv<

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