Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of original proxies for keys, so handling can be passed back to them #316

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
35 changes: 34 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ export type CreateEnv<
>
>;

const makeInternalProp = <T>(name: string) => {
const symbol = Symbol(name);
return {
matches: (prop: string | symbol) => prop === symbol,
getValue: (obj: Record<string, unknown>) =>
(obj as { [symbol]?: T })[symbol],
};
};

const serverKeysProp = makeInternalProp<string[]>("serverKeys");

export function createEnv<
TPrefix extends TPrefixFormat,
TServer extends TServerFormat = NonNullable<unknown>,
Expand Down Expand Up @@ -287,14 +298,36 @@ export function createEnv<
return prop === "__esModule" || prop === "$$typeof";
};

const serverKeys = isServer ? undefined : Object.keys(_server);

// a map of keys to the preset we got them from
const presetsByKey: Record<string, TExtendsFormat[number] | undefined> = {};

const extendedObj = (opts.extends ?? []).reduce((acc, curr) => {
return Object.assign(acc, curr);
for (const key in curr) {
presetsByKey[key] = curr;
acc[key] = curr[key];
}
const presetServerKeys = serverKeysProp.getValue(curr);
if (presetServerKeys) {
// these are keys that the proxy should handle but won't be exposed on the client
for (const key of presetServerKeys) {
presetsByKey[key] = curr;
}
}
return acc;
}, {});

const fullObj = Object.assign(parsed.value, extendedObj);

const env = new Proxy(fullObj, {
get(target, prop) {
// we need to expose these on the client side so we know to pass them off to the right proxy
if (serverKeysProp.matches(prop)) return serverKeys;
if (typeof prop !== "string") return undefined;
// pass off handling to the original proxy from the preset
const preset = presetsByKey[prop];
if (preset) return preset[prop];
if (ignoreProp(prop)) return undefined;
if (!isValidServerAccess(prop)) return onInvalidAccess(prop);
return Reflect.get(target, prop);
Expand Down
39 changes: 32 additions & 7 deletions packages/core/test/smoke-valibot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ describe("extending presets", () => {
});
describe("single preset", () => {
const processEnv = {
PRESET_ENV: "preset",
PRESET_SERVER_ENV: "server_preset",
PRESET_SHARED_ENV: "shared_preset",
PRESET_CLIENT_ENV: "client_preset",
SHARED_ENV: "shared",
SERVER_ENV: "server",
CLIENT_ENV: "client",
Expand All @@ -475,7 +477,19 @@ describe("extending presets", () => {
function lazyCreateEnv() {
const preset = createEnv({
server: {
PRESET_ENV: v.picklist(["preset"]),
PRESET_SERVER_ENV: v.literal("server_preset"),
},
clientPrefix: "PRESET_CLIENT_",
shared: {
PRESET_SHARED_ENV: v.string(),
},
client: {
PRESET_CLIENT_ENV: v.string(),
},
onInvalidAccess(variable) {
throw new Error(
`Attempted to access preset variable ${variable} on the client`,
);
},
runtimeEnv: processEnv,
});
Expand All @@ -491,6 +505,11 @@ describe("extending presets", () => {
client: {
CLIENT_ENV: v.string(),
},
onInvalidAccess(variable) {
throw new Error(
`Attempted to access variable ${variable} on the client`,
);
},
extends: [preset],
runtimeEnv: processEnv,
});
Expand All @@ -501,7 +520,9 @@ describe("extending presets", () => {
SERVER_ENV: string;
SHARED_ENV: string;
CLIENT_ENV: string;
PRESET_ENV: "preset";
PRESET_SERVER_ENV: "server_preset";
PRESET_SHARED_ENV: string;
PRESET_CLIENT_ENV: string;
}>
>();

Expand All @@ -515,7 +536,9 @@ describe("extending presets", () => {
SERVER_ENV: "server",
SHARED_ENV: "shared",
CLIENT_ENV: "client",
PRESET_ENV: "preset",
PRESET_SERVER_ENV: "server_preset",
PRESET_SHARED_ENV: "shared_preset",
PRESET_CLIENT_ENV: "client_preset",
});

globalThis.window = window;
Expand All @@ -528,13 +551,15 @@ describe("extending presets", () => {
const env = lazyCreateEnv();

expect(() => env.SERVER_ENV).toThrow(
"Attempted to access a server-side environment variable on the client",
"Attempted to access variable SERVER_ENV on the client",
);
expect(() => env.PRESET_ENV).toThrow(
"Attempted to access a server-side environment variable on the client",
expect(() => env.PRESET_SERVER_ENV).toThrow(
"Attempted to access preset variable PRESET_SERVER_ENV on the client",
);
expect(env.SHARED_ENV).toBe("shared");
expect(env.CLIENT_ENV).toBe("client");
expect(env.PRESET_SHARED_ENV).toBe("shared_preset");
expect(env.PRESET_CLIENT_ENV).toBe("client_preset");

globalThis.window = window;
});
Expand Down
39 changes: 32 additions & 7 deletions packages/core/test/smoke-zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ describe("extending presets", () => {
});
describe("single preset", () => {
const processEnv = {
PRESET_ENV: "preset",
PRESET_SERVER_ENV: "server_preset",
PRESET_SHARED_ENV: "shared_preset",
PRESET_CLIENT_ENV: "client_preset",
SHARED_ENV: "shared",
SERVER_ENV: "server",
CLIENT_ENV: "client",
Expand All @@ -473,7 +475,19 @@ describe("extending presets", () => {
function lazyCreateEnv() {
const preset = createEnv({
server: {
PRESET_ENV: z.enum(["preset"]),
PRESET_SERVER_ENV: z.literal("server_preset"),
},
shared: {
PRESET_SHARED_ENV: z.string(),
},
clientPrefix: "PRESET_CLIENT_",
client: {
PRESET_CLIENT_ENV: z.string(),
},
onInvalidAccess(variable) {
throw new Error(
`Attempted to access preset variable ${variable} on the client`,
);
},
runtimeEnv: processEnv,
});
Expand All @@ -489,6 +503,11 @@ describe("extending presets", () => {
client: {
CLIENT_ENV: z.string(),
},
onInvalidAccess(variable) {
throw new Error(
`Attempted to access variable ${variable} on the client`,
);
},
extends: [preset],
runtimeEnv: processEnv,
});
Expand All @@ -499,7 +518,9 @@ describe("extending presets", () => {
SERVER_ENV: string;
SHARED_ENV: string;
CLIENT_ENV: string;
PRESET_ENV: "preset";
PRESET_SERVER_ENV: "server_preset";
PRESET_SHARED_ENV: string;
PRESET_CLIENT_ENV: string;
}>
>();

Expand All @@ -513,7 +534,9 @@ describe("extending presets", () => {
SERVER_ENV: "server",
SHARED_ENV: "shared",
CLIENT_ENV: "client",
PRESET_ENV: "preset",
PRESET_SERVER_ENV: "server_preset",
PRESET_SHARED_ENV: "shared_preset",
PRESET_CLIENT_ENV: "client_preset",
});

globalThis.window = window;
Expand All @@ -526,13 +549,15 @@ describe("extending presets", () => {
const env = lazyCreateEnv();

expect(() => env.SERVER_ENV).toThrow(
"Attempted to access a server-side environment variable on the client",
"Attempted to access variable SERVER_ENV on the client",
);
expect(() => env.PRESET_ENV).toThrow(
"Attempted to access a server-side environment variable on the client",
expect(() => env.PRESET_SERVER_ENV).toThrow(
"Attempted to access preset variable PRESET_SERVER_ENV on the client",
);
expect(env.SHARED_ENV).toBe("shared");
expect(env.CLIENT_ENV).toBe("client");
expect(env.PRESET_SHARED_ENV).toBe("shared_preset");
expect(env.PRESET_CLIENT_ENV).toBe("client_preset");

globalThis.window = window;
});
Expand Down