-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenv.mjs
76 lines (65 loc) · 2.71 KB
/
env.mjs
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
71
72
73
74
75
76
// @ts-check
import { z } from "zod";
// Coerces a string to true if it's "true" or "1", false if "false" or "0"
const coerceBoolean = z
.enum(["0", "1", "true", "false"])
.catch("false")
// eslint-disable-next-line eqeqeq
.transform((value) => value == "true" || value == "1");
// Same as UI envs, Azure has prefix on the server variables
const ServerSchema = z.object({
// Uploading sourcemaps to Sentry requires an auth token - Required on CI
SENTRY_AUTH_TOKEN: z.string().optional(),
// Sentry DSN is used for error tracking - Required during runtime
SENTRY_DSN: z.string().optional(),
// TODO enum?
SENTRY_ENVIRONMENT: z.string().optional(),
ENABLE_FETCH_HACK: coerceBoolean,
SKIP_ENV_VALIDATION: coerceBoolean,
MAPBOX_TOKEN: z.string().optional(),
COOKIEHUB_ENABLED: coerceBoolean,
HOTJAR_ENABLED: coerceBoolean,
MATOMO_ENABLED: coerceBoolean,
PROFILE_UI_URL: z.string().url().or(z.string().length(0)).optional(),
// mandatory because the SSR can't connect to the API without it
// frontend SSR is running on a different host than the backend
TILAVARAUS_API_URL: z.string().url(),
EMAIL_VARAAMO_EXT_LINK: z.string().url().optional(),
});
// NOTE if you add a new variable to client it will be fixed in the build
// and the same build image will be deployed to all environments
const ClientSchema = z.object({
NEXT_PUBLIC_BASE_URL: z.string().optional(),
NEXT_PUBLIC_SOURCE_BRANCH_NAME: z.string().optional(),
NEXT_PUBLIC_SOURCE_VERSION: z.string().optional(),
});
function createEnv() {
const skipValidation = coerceBoolean.parse(process.env.SKIP_ENV_VALIDATION);
const isServer = typeof window === "undefined";
const serverConfig = isServer
? skipValidation
? ServerSchema.partial().safeParse(process.env)
: ServerSchema.safeParse(process.env)
: null;
if (isServer && !serverConfig?.success) {
// eslint-disable-next-line no-console
console.error("Server env validation failed", serverConfig?.error);
}
const clientSchema = skipValidation ? ClientSchema.partial() : ClientSchema;
// NOTE NextJs does substitutions for process.env. Not using the full variable breaks it!
const clientConfig = clientSchema.safeParse({
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
NEXT_PUBLIC_SOURCE_BRANCH_NAME: process.env.NEXT_PUBLIC_SOURCE_BRANCH_NAME,
NEXT_PUBLIC_SOURCE_VERSION: process.env.NEXT_PUBLIC_SOURCE_VERSION,
});
if (!clientConfig.success) {
// eslint-disable-next-line no-console
console.error("Client env validation failed", clientConfig.error);
}
return {
...(isServer && serverConfig?.success ? serverConfig.data : {}),
...(clientConfig.success ? clientConfig.data : {}),
};
}
const env = createEnv();
export { env };