-
Notifications
You must be signed in to change notification settings - Fork 52
Multi environment support #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1690b30
e0114b0
2aefff0
ebabb76
3513579
9436606
9522901
aae74e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
import dotenvx from '@dotenvx/dotenvx'; | ||
import { z, ZodError } from 'zod'; | ||
|
||
dotenv.config(); | ||
dotenvx.config(); | ||
|
||
const prettyPrintErrors = (errObj: ZodError) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider making prettyPrintErrors more flexible Instead of logging directly to the console, consider having the function return a formatted string. This would make it more reusable in different contexts, allowing the caller to decide how to handle the output.
|
||
const formattedError = errObj.format(); | ||
Object.entries(formattedError).forEach(([key, value]) => { | ||
if (key !== '_errors') { | ||
console.log(`${key}:`); | ||
if (Array.isArray(value?._errors)) { | ||
value._errors.forEach((errorMessage: string) => { | ||
console.log(` - ${errorMessage}`); | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
// Remove .optional() from requried schema properties | ||
|
||
const configSchema = z.object({ | ||
REDIS_URL: z.string().url(), | ||
PORT: z.string().regex(/^\d+$/).transform(Number), | ||
DATABASE_URL: z.string().url().optional(), | ||
MONGO_DATABASE_URL: z.string().url(), | ||
SMTP_HOST: z.string().min(1).optional(), | ||
SMTP_PORT: z.string().regex(/^\d+$/).transform(Number).optional(), | ||
|
@@ -34,6 +47,14 @@ const configSchema = z.object({ | |
|
||
export type Config = z.infer<typeof configSchema>; | ||
|
||
const config = configSchema.parse(process.env); | ||
const config = configSchema.safeParse(process.env); | ||
|
||
if (!config.success) { | ||
console.log('------------------------------------------------'); | ||
console.log('There is an error with the environment variables'); | ||
console.log('------------------------------------------------'); | ||
console.log(prettyPrintErrors(config.error)); | ||
process.exit(1); | ||
} | ||
|
||
export default config; | ||
export default config.data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (documentation): Consider explaining the reason for separate development and production environment files.
While separating environments is a good practice, a brief explanation of why you're using two files now might help users understand the change better.