1
- import { invariant } from './misc.ts'
2
-
3
- const requiredServerEnvs = [
4
- 'NODE_ENV' ,
5
- 'DATABASE_PATH' ,
6
- 'DATABASE_URL' ,
7
- 'SESSION_SECRET' ,
8
- 'INTERNAL_COMMAND_TOKEN' ,
9
- 'CACHE_DATABASE_PATH' ,
10
- // If you plan to use Resend, uncomment this line
11
- // 'RESEND_API_KEY',
1
+ import { z } from 'zod'
2
+
3
+ const schema = z . object ( {
4
+ NODE_ENV : z . enum ( [ 'production' , 'development' , 'test' ] as const ) ,
5
+ DATABASE_PATH : z . string ( ) ,
6
+ DATABASE_URL : z . string ( ) ,
7
+ SESSION_SECRET : z . string ( ) ,
8
+ INTERNAL_COMMAND_TOKEN : z . string ( ) ,
9
+ CACHE_DATABASE_PATH : z . string ( ) ,
12
10
// If you plan on using Sentry, uncomment this line
13
- // 'SENTRY_DSN',
14
- ] as const
11
+ // SENTRY_DSN: z.string(),
12
+ // If you plan to use Resend, uncomment this line
13
+ // RESEND_API_KEY: z.string(),
14
+ } )
15
15
16
16
declare global {
17
17
namespace NodeJS {
18
- interface ProcessEnv
19
- extends Record < ( typeof requiredServerEnvs ) [ number ] , string > { }
18
+ interface ProcessEnv extends z . infer < typeof schema > { }
20
19
}
21
20
}
22
21
23
22
export function init ( ) {
24
- for ( const env of requiredServerEnvs ) {
25
- invariant ( process . env [ env ] , `${ env } is required` )
23
+ const parsed = schema . safeParse ( process . env )
24
+
25
+ if ( parsed . success === false ) {
26
+ console . error (
27
+ '❌ Invalid environment variables:' ,
28
+ parsed . error . flatten ( ) . fieldErrors ,
29
+ )
30
+
31
+ throw new Error ( 'Invalid envirmonment variables' )
26
32
}
27
33
}
28
34
@@ -36,8 +42,6 @@ export function init() {
36
42
* @returns all public ENV variables
37
43
*/
38
44
export function getEnv ( ) {
39
- invariant ( process . env . NODE_ENV , 'NODE_ENV should be defined' )
40
-
41
45
return {
42
46
MODE : process . env . NODE_ENV ,
43
47
SENTRY_DSN : process . env . SENTRY_DSN ,
0 commit comments