-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.ts
70 lines (66 loc) · 2.21 KB
/
env.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { load } from "@std/dotenv";
import { z } from "zod";
await load({ export: true });
export const EnvSchema = z.object({
RUNTIME_ENV: z.enum(["prod", "stage", "local"]).default("local"),
API_PORT: z
.number({ coerce: true })
.positive()
.default(3000)
.describe("Port to run the API on"),
API_HOST: z
.string()
.default("http://localhost"),
UI_URL: z
.string()
.optional(),
API_ENDPOINT_AUTH_GOOGLE_SIGNIN: z.string().default(
"/api/auth/google-email",
),
API_ENDPOINT_AUTH_GOOGLE_SIGNOUT: z.string().default(
"/api/auth/google-signout",
),
API_ENDPOINT_AUTH_AUTHORIZATION_G_DRIVE: z.string().default(
"/api/auth/google-drive",
),
API_ENDPOINT_AUTH_CALLBACK_GOOGLE: z
.string()
.regex(/google-callback/)
.default("/api/auth/google-callback"),
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
AUTH_SESSION_MAX_SILENCE_DURATION_IN_SECONDS: z.number({ coerce: true })
.positive().default(60),
}).transform((input) => {
return {
...input,
API_URL: input.API_HOST.match("http://localhost")
? `${input.API_HOST}:${input.API_PORT}`
: input.API_HOST,
};
}).superRefine((input, ctx) => {
if (input.RUNTIME_ENV === "local") {
input.UI_URL || (input.UI_URL = "http://localhost:5173");
} else if (
input.RUNTIME_ENV === "prod" || input.RUNTIME_ENV === "stage"
) {
// deno-lint-ignore no-inner-declarations
function in_runtime_required(envVar: keyof typeof input) {
if (!input[envVar]) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: "undefined",
path: [envVar],
message:
`Must be set when RUNTIME_ENV is '${input.RUNTIME_ENV}'`,
});
}
}
([
"UI_URL",
] as (keyof typeof input)[]).forEach(in_runtime_required);
}
});
export type Env = z.infer<typeof EnvSchema>;
export const Env = await EnvSchema.parseAsync(Deno.env.toObject());