-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛠️Add environment configuration with validation using Zod; create env…
….ts for managing environment variables and defaults; implement error handling for invalid configurations.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { z } from "zod"; | ||
import * as dotenv from "dotenv"; | ||
|
||
dotenv.config({ path: "./.env" }); | ||
|
||
const EnvSchema = z.object({ | ||
BETTER_AUTH_SECRET: z.string(), | ||
BETTER_AUTH_URL: z.string().url().default("http://localhost:3000"), | ||
ALLOWED_ORIGINS: z | ||
.string() | ||
.default("http://localhost:3000,http://localhost:5173"), | ||
|
||
GOOGLE_CLIENT_ID: z.string(), | ||
GOOGLE_CLIENT_SECRET: z.string(), | ||
GITHUB_CLIENT_ID: z.string(), | ||
GITHUB_CLIENT_SECRET: z.string(), | ||
|
||
EMAIL_VERIFICATION_CALLBACK_URL: z | ||
.string() | ||
.default("http://localhost:3000/email-verified"), | ||
RESEND_API: z.string(), | ||
TURNSTILE_SECRET_KEY: z.string(), | ||
TURNSTILE_VERIFY_URL: z | ||
.string() | ||
.url() | ||
.default("https://challenges.cloudflare.com/turnstile/v0/siteverify"), | ||
|
||
DB_FILE_NAME: z.string().default("file:database.sqlite"), | ||
|
||
PG_PASSWORD: z.string(), | ||
PG_USER: z.string().default("postgres"), | ||
PG_DATABASE: z.string(), | ||
PG_PORT: z.coerce.number().default(5432), | ||
|
||
NODE_ENV: z.string().default("development"), | ||
}); | ||
|
||
export type env = z.infer<typeof EnvSchema>; | ||
|
||
const { data: env, error } = EnvSchema.safeParse(process.env); | ||
|
||
if (error) { | ||
console.error("🔥Invalid env: "); | ||
console.error(JSON.stringify(error.flatten().fieldErrors, null, 2)); | ||
process.exit(1); | ||
} | ||
|
||
export default env!; |