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

fix: clarify some comments and rename some files in env #245

Merged
merged 7 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions src/installers/envVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ export const envVariablesInstaller: Installer = async ({

switch (true) {
case usingAuth && usingPrisma:
envFile = "env-prisma-auth.mjs";
envFile = "auth-prisma-schema.mjs";
break;
case usingAuth:
envFile = "env-auth.mjs";
envFile = "auth-schema.mjs";
break;
case usingPrisma:
envFile = "env-prisma.mjs";
envFile = "prisma-schema.mjs";
break;
}

if (!envFile) return;

const envSchemaSrc = path.join(envAssetDir, envFile);
const envSchemaDest = path.join(projectDir, "src/env/env-schema.mjs");
const envSchemaDest = path.join(projectDir, "src/env/schema.mjs");

const envExample = path.join(projectDir, ".env-example");
const envExampleSrc = path.join(projectDir, ".env-example");
const envDest = path.join(projectDir, ".env");

await Promise.all([
fs.copy(envSchemaSrc, envSchemaDest, { overwrite: true }),
fs.rename(envExample, envDest),
fs.rename(envExampleSrc, envDest),
]);
};
33 changes: 33 additions & 0 deletions template/addons/env/auth-prisma-schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check
import { z } from "zod";

/**
* Specify your server-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
*/
export const serverEnvSchema = z.object({
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
NEXTAUTH_SECRET: z.string(),
NEXTAUTH_URL: z.string().url(),
DISCORD_CLIENT_ID: z.string(),
DISCORD_CLIENT_SECRET: z.string(),
});

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
*/
export const clientSchema = z.object({
// NEXT_PUBLIC_BAR: z.string(),
});

/**
* You can't destruct `process.env` as a regular object, so you have to do
* it manually here. This is because Next.js evaluates this at build time,
* and only used environment variables are included in the build.
*/
export const clientEnv = {
// NEXT_PUBLIC_BAR: process.env.NEXT_PUBLIC_BAR,
};
31 changes: 31 additions & 0 deletions template/addons/env/auth-schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-check
import { z } from "zod";

/**
* Specify your server-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
*/
export const serverEnvSchema = z.object({
NEXTAUTH_SECRET: z.string(),
NEXTAUTH_URL: z.string().url(),
DISCORD_CLIENT_ID: z.string(),
DISCORD_CLIENT_SECRET: z.string(),
});

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
*/
export const clientSchema = z.object({
// NEXT_PUBLIC_BAR: z.string(),
});

/**
* You can't destruct `process.env` as a regular object, so you have to do
* it manually here. This is because Next.js evaluates this at build time,
* and only used environment variables are included in the build.
*/
export const clientEnv = {
// NEXT_PUBLIC_BAR: process.env.NEXT_PUBLIC_BAR,
};
25 changes: 0 additions & 25 deletions template/addons/env/env-auth.mjs

This file was deleted.

27 changes: 0 additions & 27 deletions template/addons/env/env-prisma-auth.mjs

This file was deleted.

23 changes: 0 additions & 23 deletions template/addons/env/env-prisma.mjs

This file was deleted.

29 changes: 29 additions & 0 deletions template/addons/env/prisma-schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @ts-check
import { z } from "zod";

/**
* Specify your server-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
*/
export const serverEnvSchema = z.object({
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
});

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
*/
export const clientSchema = z.object({
// NEXT_PUBLIC_BAR: z.string(),
});

/**
* You can't destruct `process.env` as a regular object, so you have to do
* it manually here. This is because Next.js evaluates this at build time,
* and only used environment variables are included in the build.
*/
export const clientEnv = {
// NEXT_PUBLIC_BAR: process.env.NEXT_PUBLIC_BAR,
};
37 changes: 0 additions & 37 deletions template/base/src/env/client-env.mjs

This file was deleted.

36 changes: 36 additions & 0 deletions template/base/src/env/client.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @ts-check
import { clientEnv, clientSchema } from "./schema.mjs";

const _clientEnv = clientSchema.safeParse(clientEnv);

export const formatErrors = (
/** @type {import('zod').ZodFormattedError<Map<string,string>,string>} */
errors,
) =>
Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value)
return `${name}: ${value._errors.join(", ")}\n`;
})
.filter(Boolean);

if (!_clientEnv.success) {
console.error(
"❌ Invalid environment variables:\n",
...formatErrors(_clientEnv.error.format()),
);
throw new Error("Invalid environment variables");
}

/**
* Validate that client-side environment variables are exposed to the client.
*/
for (let key of Object.keys(_clientEnv.data)) {
if (!key.startsWith("NEXT_PUBLIC_")) {
console.warn("❌ Invalid public environment variable name:", key);

throw new Error("Invalid public environment variable name");
}
}

export const env = _clientEnv.data;
22 changes: 0 additions & 22 deletions template/base/src/env/env-schema.mjs

This file was deleted.

28 changes: 28 additions & 0 deletions template/base/src/env/schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check
import { z } from "zod";

/**
* Specify your server-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
*/
export const serverSchema = z.object({
// FOO: z.string(),
});

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
*/
export const clientSchema = z.object({
// NEXT_PUBLIC_BAR: z.string(),
});

/**
* You can't destruct `process.env` as a regular object, so you have to do
* it manually here. This is because Next.js evaluates this at build time,
* and only used environment variables are included in the build.
*/
export const clientEnv = {
nexxeln marked this conversation as resolved.
Show resolved Hide resolved
// NEXT_PUBLIC_BAR: process.env.NEXT_PUBLIC_BAR,
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* This file is included in `/next.config.mjs` which ensures the app isn't built with invalid env vars.
* It has to be a `.mjs`-file to be imported there.
*/
import { serverEnvSchema } from "./env-schema.mjs";
import { env as clientEnv, formatErrors } from "./client-env.mjs";
import { serverSchema } from "./schema.mjs";
import { env as clientEnv, formatErrors } from "./client.mjs";

const _serverEnv = serverSchema.safeParse(process.env);

const _serverEnv = serverEnvSchema.safeParse(process.env);

if (!_serverEnv.success) {
console.error(
"❌ Invalid environment variables:\n",
Expand All @@ -16,14 +16,15 @@ if (!_serverEnv.success) {
throw new Error("Invalid environment variables");
}

/**
* Validate that server-side environment variables are not exposed to the client.
*/
for (let key of Object.keys(_serverEnv.data)) {
if (key.startsWith("NEXT_PUBLIC_")) {
console.warn(
"❌ You are exposing a server-side env-variable:", key
);
console.warn("❌ You are exposing a server-side env-variable:", key);

throw new Error("You are exposing a server-side env-variable");
}
}

export const env = {..._serverEnv.data, ...clientEnv};
export const env = { ..._serverEnv.data, ...clientEnv };