-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.ts
More file actions
43 lines (38 loc) · 1.02 KB
/
env.ts
File metadata and controls
43 lines (38 loc) · 1.02 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { z } from "zod"
const PROD_ENV = "production"
function isProduction() {
return (Deno.env.get("NODE_ENV") || "") === PROD_ENV
}
export const envSchema = z.object({
NODE_ENV: z.enum(["development", PROD_ENV, "test"]).default("development"),
PORT: z.preprocess(
(val) => {
if (val) return val
return isProduction() ? "8000" : "8080"
},
z.coerce.number().int().min(1).max(65535),
),
HOST: z.preprocess(
(val) => {
if (val) return val
return isProduction() ? "0.0.0.0" : "localhost"
},
z.string(),
),
LOG_LEVEL: z.preprocess(
(val) => {
if (val) return val
return isProduction() ? "info" : "debug"
},
z.enum(["trace", "debug", "info", "warning", "error", "fatal"]),
),
})
export type EnvConfig = z.infer<typeof envSchema>
export function getEnvConfig(): EnvConfig {
return envSchema.parse({
NODE_ENV: Deno.env.get("NODE_ENV"),
PORT: Deno.env.get("PORT"),
HOST: Deno.env.get("HOST"),
LOG_LEVEL: Deno.env.get("LOG_LEVEL"),
})
}