|
1 | 1 | import { z } from 'zod' |
2 | 2 |
|
3 | 3 | export const publicEnvSchema = z.object({ |
4 | | - NODE_ENV: z.literal('production').or(z.literal('development')).default('production'), |
5 | | - NEXT_PUBLIC_SHOW_STAGING_DISCLAIMER_MODAL: z.literal('true').or(z.literal('false')).optional(), |
6 | | - NEXT_PUBLIC_FEATURES_FEED_URL: z.string().url().default('https://cdn.helpwave.de/feed.json'), |
7 | | - NEXT_PUBLIC_IMPRINT_URL: z.string().url().default('https://cdn.helpwave.de/imprint.html'), |
8 | | - NEXT_PUBLIC_PRIVACY_URL: z.string().url().default('https://cdn.helpwave.de/privacy.html'), |
9 | | - NEXT_PUBLIC_GRAPHQL_ENDPOINT: z.string().url().default('http://localhost:8000/graphql'), |
10 | | - NEXT_PUBLIC_ISSUER_URI: z.string().url().default('http://localhost:8080/realms/tasks'), |
11 | | - NEXT_PUBLIC_CLIENT_ID: z.string().min(1).default('tasks-web'), |
12 | | - NEXT_PUBLIC_REDIRECT_URI: z.string().min(1).default('http://localhost:3000/auth/callback'), |
13 | | - NEXT_PUBLIC_POST_LOGOUT_REDIRECT_URI: z.string().min(1).default('http://localhost:3000/'), |
| 4 | + NODE_ENV: z.string().default('production'), |
| 5 | + RUNTIME_SHOW_STAGING_DISCLAIMER_MODAL: z.literal('true').or(z.literal('false')).optional(), |
| 6 | + RUNTIME_FEATURES_FEED_URL: z.string().url().default('https://cdn.helpwave.de/feed.json'), |
| 7 | + RUNTIME_IMPRINT_URL: z.string().url().default('https://cdn.helpwave.de/imprint.html'), |
| 8 | + RUNTIME_PRIVACY_URL: z.string().url().default('https://cdn.helpwave.de/privacy.html'), |
| 9 | + RUNTIME_GRAPHQL_ENDPOINT: z.string().url().default('http://localhost:8000/graphql'), |
| 10 | + RUNTIME_ISSUER_URI: z.string().url().default('http://localhost:8080/realms/tasks'), |
| 11 | + RUNTIME_CLIENT_ID: z.string().min(1).default('tasks-web'), |
| 12 | + RUNTIME_REDIRECT_URI: z.string().min(1).default('http://localhost:3000/auth/callback'), |
| 13 | + RUNTIME_POST_LOGOUT_REDIRECT_URI: z.string().min(1).default('http://localhost:3000/'), |
14 | 14 | }) |
15 | 15 |
|
16 | 16 | const configSchema = publicEnvSchema.transform(obj => ({ |
17 | 17 | env: obj.NODE_ENV, |
18 | | - showStagingDisclaimerModal: obj.NEXT_PUBLIC_SHOW_STAGING_DISCLAIMER_MODAL === 'true', |
19 | | - featuresFeedUrl: obj.NEXT_PUBLIC_FEATURES_FEED_URL, |
20 | | - imprintUrl: obj.NEXT_PUBLIC_IMPRINT_URL, |
21 | | - privacyUrl: obj.NEXT_PUBLIC_PRIVACY_URL, |
22 | | - graphqlEndpoint: obj.NEXT_PUBLIC_GRAPHQL_ENDPOINT, |
| 18 | + showStagingDisclaimerModal: obj.RUNTIME_SHOW_STAGING_DISCLAIMER_MODAL === 'true', |
| 19 | + featuresFeedUrl: obj.RUNTIME_FEATURES_FEED_URL, |
| 20 | + imprintUrl: obj.RUNTIME_IMPRINT_URL, |
| 21 | + privacyUrl: obj.RUNTIME_PRIVACY_URL, |
| 22 | + graphqlEndpoint: obj.RUNTIME_GRAPHQL_ENDPOINT, |
23 | 23 | auth: { |
24 | | - issuer: obj.NEXT_PUBLIC_ISSUER_URI, |
25 | | - clientId: obj.NEXT_PUBLIC_CLIENT_ID, |
26 | | - redirect_uri: obj.NEXT_PUBLIC_REDIRECT_URI, |
27 | | - post_logout_redirect_uri: obj.NEXT_PUBLIC_POST_LOGOUT_REDIRECT_URI, |
| 24 | + issuer: obj.RUNTIME_ISSUER_URI, |
| 25 | + clientId: obj.RUNTIME_CLIENT_ID, |
| 26 | + redirect_uri: obj.RUNTIME_REDIRECT_URI, |
| 27 | + post_logout_redirect_uri: obj.RUNTIME_POST_LOGOUT_REDIRECT_URI, |
28 | 28 | } |
29 | 29 | })) |
30 | 30 |
|
31 | 31 | const getConfig = () => { |
32 | | - const localOverrides: Partial<z.output<typeof configSchema>> = {} |
33 | | - |
34 | | - const source = (typeof window !== 'undefined' && window.__ENV) |
35 | | - ? window.__ENV |
36 | | - : process.env |
| 32 | + if (typeof window !== 'undefined' && window.__ENV) { |
| 33 | + return configSchema.parse({ |
| 34 | + ...window.__ENV, |
| 35 | + NODE_ENV: process.env.NODE_ENV |
| 36 | + }) |
| 37 | + } |
37 | 38 |
|
38 | | - const maybeConfig = configSchema.safeParse(source) |
39 | 39 |
|
40 | | - if (!maybeConfig.success) { |
41 | | - throw new Error(`Invalid environment variables:\n${maybeConfig.error}`) |
42 | | - } else { |
43 | | - return Object.assign(maybeConfig.data, localOverrides) |
44 | | - } |
| 40 | + const result = publicEnvSchema.safeParse(process.env) |
| 41 | + return configSchema.parse(result.data) |
45 | 42 | } |
46 | 43 |
|
47 | 44 | export { getConfig } |
0 commit comments