Skip to content

Commit

Permalink
feat: construct serverEnv, like we do with clientEnv #1000 (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
iduuck authored Jan 8, 2023
1 parent d973f18 commit 78dfdc9
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-tomatoes-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

construct `serverEnv`, like we do with `clientEnv` #1000
9 changes: 9 additions & 0 deletions cli/template/base/src/env/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export const serverSchema = z.object({
NODE_ENV: z.enum(["development", "test", "production"]),
});

/**
* You can't destruct `process.env` as a regular object in the Next.js
* middleware, so you have to do it manually here.
* @type {{ [k in keyof z.infer<typeof serverSchema>]: z.infer<typeof serverSchema>[k] | undefined }}
*/
export const serverEnv = {
NODE_ENV: process.env.NODE_ENV,
};

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
Expand Down
4 changes: 2 additions & 2 deletions cli/template/base/src/env/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* 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 { serverSchema } from "./schema.mjs";
import { serverSchema, serverEnv } from "./schema.mjs";
import { env as clientEnv, formatErrors } from "./client.mjs";

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

if (!_serverEnv.success) {
console.error(
Expand Down
14 changes: 14 additions & 0 deletions cli/template/extras/src/env/schema/with-auth-prisma.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export const serverSchema = z.object({
DISCORD_CLIENT_SECRET: z.string(),
});

/**
* You can't destruct `process.env` as a regular object in the Next.js
* middleware, so you have to do it manually here.
* @type {{ [k in keyof z.infer<typeof serverSchema>]: z.infer<typeof serverSchema>[k] | undefined }}
*/
export const serverEnv = {
DATABASE_URL: process.env.DATABASE_URL,
NODE_ENV: process.env.NODE_ENV,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
};

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
Expand Down
13 changes: 13 additions & 0 deletions cli/template/extras/src/env/schema/with-auth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ export const serverSchema = z.object({
DISCORD_CLIENT_SECRET: z.string(),
});

/**
* You can't destruct `process.env` as a regular object in the Next.js
* middleware, so you have to do it manually here.
* @type {{ [k in keyof z.infer<typeof serverSchema>]: z.infer<typeof serverSchema>[k] | undefined }}
*/
export const serverEnv = {
NODE_ENV: process.env.NODE_ENV,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
};

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
Expand Down
10 changes: 10 additions & 0 deletions cli/template/extras/src/env/schema/with-prisma.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export const serverSchema = z.object({
NODE_ENV: z.enum(["development", "test", "production"]),
});

/**
* You can't destruct `process.env` as a regular object in the Next.js
* middleware, so you have to do it manually here.
* @type {{ [k in keyof z.infer<typeof serverSchema>]: z.infer<typeof serverSchema>[k] | undefined }}
*/
export const serverEnv = {
DATABASE_URL: process.env.DATABASE_URL,
NODE_ENV: process.env.NODE_ENV,
};

/**
* Specify your client-side environment variables schema here.
* This way you can ensure the app isn't built with invalid env vars.
Expand Down
9 changes: 9 additions & 0 deletions www/src/pages/ar/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -105,6 +109,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

**ملحوطة:** النص الفارغ تتعامل معة zod علي أنه نص صحيح، إذا ما اردت ان تُغيّر هذا الاسلوب فإستخدم `z.string().min(1)`
Expand Down
11 changes: 10 additions & 1 deletion www/src/pages/en/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ _TLDR; If you want to add a new environment variable, you must add it to both yo

## schema.mjs

This is the file you will actually touch. It contains two schemas, one for server-side environment variables and one for client-side as well as a `clientEnv` object.
This is the file you will actually touch. It contains two schemas & environment objects, one for server-side environment variables and one for client-side.

```ts:env/schema.mjs
export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -116,6 +120,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

_**NOTE:** An empty string is still a string, so `z.string()` will accept an empty string as a valid value. If you want to make sure that the environment variable is not empty, you can use `z.string().min(1)`._
Expand Down
9 changes: 9 additions & 0 deletions www/src/pages/fr/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -117,6 +121,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

_**NOTE:** Une chaîne vide est toujours une chaîne, donc `z.string()` acceptera une chaîne vide comme valeur valide. Si vous voulez vous assurer que la variable d'environnement n'est pas vide, vous pouvez utiliser `z.string().min(1)`._
Expand Down
9 changes: 9 additions & 0 deletions www/src/pages/no/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -114,6 +118,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

_**MERK:** En tom streng er fortsatt en streng, så `z.string()` vil godta en tom streng som en gyldig verdi. Hvis du vil forsikre deg om at miljøvariabelen ikke er tom, kan du bruke `z.string().min(1)`._
Expand Down
9 changes: 9 additions & 0 deletions www/src/pages/pt/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -114,6 +118,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

_**NOTA:** Uma string vazia ainda é uma string, então `z.string()` aceitará uma string vazia como um valor válido. Se você quiser ter certeza de que a variável de ambiente não está vazia, você pode usar `z.string().min(1)`._
Expand Down
9 changes: 9 additions & 0 deletions www/src/pages/ru/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -115,6 +119,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

_**Обратите внимание:** Пустая строка все еще является строкой, поэтому `z.string()` примет пустую строку как допустимое значение. Если вы хотите убедиться, что переменная среды не пуста, вы можете использовать `z.string().min(1)`._
Expand Down
9 changes: 9 additions & 0 deletions www/src/pages/zh-hans/usage/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const serverSchema = z.object({
// DATABASE_URL: z.string().url(),
});

export const serverEnv = {
// DATABASE_URL: process.env.DATABASE_URL,
};

export const clientSchema = z.object({
// NEXT_PUBLIC_WS_KEY: z.string(),
});
Expand Down Expand Up @@ -116,6 +120,11 @@ export const serverSchema = z.object({
// ...
TWITTER_API_TOKEN: z.string(),
});

export const serverEnv = {
// ...
TWITTER_API_TOKEN: process.env.TWITTER_API_TOKEN,
};
```

_**注意:** 一个空的字符串仍然是一个字符串,所以 `z.string()` 会认为它是一个有效值。如果你想要确保环境变量的值不能为空,你可以使用 `z.string().min(1)`_
Expand Down

1 comment on commit 78dfdc9

@vercel
Copy link

@vercel vercel bot commented on 78dfdc9 Jan 8, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.