1+ import { safeParse } from "@/utils/base" ;
12import { SERVER_URL_KEY } from "@/utils/constants" ;
2- import { Elysia , t } from "elysia" ;
3+ import { Type as t } from "@sinclair/typebox" ;
4+ import { TypeCompiler } from "@sinclair/typebox/compiler" ;
35
4- const {
5- models : { serverSchema } ,
6- } = new Elysia ( ) . model ( {
7- serverSchema : t . Object ( {
8- DATABASE_URL : t . String ( { minLength : 1 , error : "DATABASE_URL not set!" } ) ,
9- SECRET : t . String ( { minLength : 1 , error : "SECRET not set!" } ) ,
10- NODE_ENV : t . Union (
11- [ t . Literal ( "development" ) , t . Literal ( "test" ) , t . Literal ( "production" ) ] ,
12- {
13- error : "NODE_ENV not set!" ,
14- } ,
15- ) ,
16- AUTH_COOKIE : t . Literal ( "auth" , { error : "AUTH_COOKIE not set!" } ) ,
17- SERVER_URL_KEY : t . Literal ( SERVER_URL_KEY , { error : "SERVER_URL not set!" } ) ,
18- SEVEN_DAYS : t . Integer ( { minimum : 1 , error : "SEVEN_DAYS not set!" } ) ,
19- } ) ,
6+ const serverSchema = t . Object ( {
7+ DATABASE_URL : t . String ( { minLength : 1 , error : "DATABASE_URL not set!" } ) ,
8+ SECRET : t . String ( { minLength : 1 , error : "SECRET not set!" } ) ,
9+ NODE_ENV : t . Union (
10+ [ t . Literal ( "development" ) , t . Literal ( "test" ) , t . Literal ( "production" ) ] ,
11+ {
12+ error : "NODE_ENV not set!" ,
13+ } ,
14+ ) ,
15+ AUTH_COOKIE : t . Literal ( "auth" , { error : "AUTH_COOKIE not set!" } ) ,
16+ SERVER_URL_KEY : t . Literal ( SERVER_URL_KEY , { error : "SERVER_URL not set!" } ) ,
17+ SEVEN_DAYS : t . Integer ( { minimum : 1 , error : "SEVEN_DAYS not set!" } ) ,
2018} ) ;
2119
22- const serverEnvResult = serverSchema . safeParse ( {
20+ const serverSchemaChecker = TypeCompiler . Compile ( serverSchema ) ;
21+
22+ const serverEnvResult = safeParse ( serverSchemaChecker , {
2323 DATABASE_URL : process . env . DATABASE_URL ,
2424 SECRET : process . env . SECRET ,
2525 NODE_ENV : process . env . NODE_ENV ,
@@ -28,13 +28,14 @@ const serverEnvResult = serverSchema.safeParse({
2828 SEVEN_DAYS : 60 * 60 * 24 * 7 , // 7 days in seconds
2929} ) ;
3030
31- if ( ! serverEnvResult . data ) {
31+ if ( ! serverEnvResult . success ) {
3232 const firstError = serverEnvResult . errors [ 0 ] ;
33- if ( firstError )
33+ if ( firstError ) {
3434 throw new Error (
35- `Invalid server environment variable ${ firstError . path . slice ( 1 ) } : ${ firstError . summary . replaceAll ( " " , " " ) } ` ,
35+ `Invalid server environment variable: ${ firstError . message } ` ,
3636 ) ;
37- else throw new Error ( `Invalid server environment ${ serverEnvResult . error } ` ) ;
37+ }
38+ throw new Error ( `Invalid server environment validation failed` ) ;
3839}
3940
4041export const serverEnv = serverEnvResult . data ;
0 commit comments