-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment.ts
26 lines (20 loc) · 797 Bytes
/
environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { registerAs } from '@nestjs/config';
import { z } from 'zod';
export default registerAs('app', () => ({
env: process.env.NODE_ENV,
host: process.env.HOST,
port: parseInt(process.env.PORT ?? '7501', 10),
}));
const EnvironmentVariablesSchema = z.object({
HOST: z.string(),
NODE_ENV: z.union([z.literal('development'), z.literal('production'), z.literal('test')]),
PORT: z.number({ coerce: true }).min(7000).max(8000),
});
export type EnvironmentVariables = z.infer<typeof EnvironmentVariablesSchema>;
export const validate = (config: Record<string, unknown>): EnvironmentVariables => {
const result = EnvironmentVariablesSchema.safeParse(config);
if (!result.success) {
throw new Error(JSON.stringify(result.error.format(), null, 2));
}
return result.data;
};