Skip to content

Commit 3f8ce77

Browse files
authored
feat: t3 env addon (#83)
t3 env addon
1 parent f0005ca commit 3f8ce77

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## T3Env
2+
3+
- You can use T3Env to add type safety to your environment variables.
4+
- Add Environment variables to the `src/env.mjs` file.
5+
- Use the environment variables in your code.
6+
7+
### Usage
8+
9+
```ts
10+
import { env } from "@/env";
11+
12+
console.log(env.VITE_APP_TITLE);
13+
```
14+
15+
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createEnv } from "@t3-oss/env-core";
2+
import { z } from "zod";
3+
4+
export const env = createEnv({
5+
server: {
6+
SERVER_URL: z.string().url().optional(),
7+
},
8+
9+
/**
10+
* The prefix that client-side variables must have. This is enforced both at
11+
* a type-level and at runtime.
12+
*/
13+
clientPrefix: "VITE_",
14+
15+
client: {
16+
VITE_APP_TITLE: z.string().min(1).optional(),
17+
},
18+
19+
/**
20+
* What object holds the environment variables at runtime. This is usually
21+
* `process.env` or `import.meta.env`.
22+
*/
23+
runtimeEnv: import.meta.env,
24+
25+
/**
26+
* By default, this library will feed the environment variables directly to
27+
* the Zod validator.
28+
*
29+
* This means that if you have an empty string for a value that is supposed
30+
* to be a number (e.g. `PORT=` in a ".env" file), Zod will incorrectly flag
31+
* it as a type mismatch violation. Additionally, if you have an empty string
32+
* for a value that is supposed to be a string with a default value (e.g.
33+
* `DOMAIN=` in an ".env" file), the default value will never be applied.
34+
*
35+
* In order to solve these issues, we recommend that all new projects
36+
* explicitly specify this option as true.
37+
*/
38+
emptyStringAsUndefined: true,
39+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "T3Env",
3+
"description": "Add type safety to your environment variables",
4+
"phase": "add-on",
5+
"link": "https://github.com/t3-oss/t3-env",
6+
"templates": [
7+
"file-router",
8+
"code-router"
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"zod": "^3.24.2",
4+
"@t3-oss/env-core": "^0.12.0"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## T3Env
2+
3+
- You can use T3Env to add type safety to your environment variables.
4+
- Add Environment variables to the `src/env.mjs` file.
5+
- Use the environment variables in your code.
6+
7+
### Usage
8+
9+
```ts
10+
import { env } from "@/env";
11+
12+
console.log(env.VITE_APP_TITLE);
13+
```
14+
15+
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createEnv } from "@t3-oss/env-core";
2+
import { z } from "zod";
3+
4+
export const env = createEnv({
5+
server: {
6+
SERVER_URL: z.string().url().optional(),
7+
},
8+
9+
/**
10+
* The prefix that client-side variables must have. This is enforced both at
11+
* a type-level and at runtime.
12+
*/
13+
clientPrefix: "VITE_",
14+
15+
client: {
16+
VITE_APP_TITLE: z.string().min(1).optional(),
17+
},
18+
19+
/**
20+
* What object holds the environment variables at runtime. This is usually
21+
* `process.env` or `import.meta.env`.
22+
*/
23+
runtimeEnv: import.meta.env,
24+
25+
/**
26+
* By default, this library will feed the environment variables directly to
27+
* the Zod validator.
28+
*
29+
* This means that if you have an empty string for a value that is supposed
30+
* to be a number (e.g. `PORT=` in a ".env" file), Zod will incorrectly flag
31+
* it as a type mismatch violation. Additionally, if you have an empty string
32+
* for a value that is supposed to be a string with a default value (e.g.
33+
* `DOMAIN=` in an ".env" file), the default value will never be applied.
34+
*
35+
* In order to solve these issues, we recommend that all new projects
36+
* explicitly specify this option as true.
37+
*/
38+
emptyStringAsUndefined: true,
39+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "T3Env",
3+
"description": "Add type safety to your environment variables",
4+
"phase": "add-on",
5+
"link": "https://github.com/t3-oss/t3-env",
6+
"templates": [
7+
"file-router",
8+
"code-router"
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"zod": "^3.24.2",
4+
"@t3-oss/env-core": "^0.12.0"
5+
}
6+
}

0 commit comments

Comments
 (0)