forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget-config-value.ts
55 lines (43 loc) · 1.17 KB
/
get-config-value.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
import rawSiteConfig from '../site.config'
import { SiteConfig } from './site-config'
if (!rawSiteConfig) {
throw new Error(`Config error: invalid site.config.ts`)
}
// allow environment variables to override site.config.ts
let siteConfigOverrides: SiteConfig
try {
if (process.env.NEXT_PUBLIC_SITE_CONFIG) {
siteConfigOverrides = JSON.parse(process.env.NEXT_PUBLIC_SITE_CONFIG)
}
} catch (err) {
console.error('Invalid config "NEXT_PUBLIC_SITE_CONFIG" failed to parse')
throw err
}
const siteConfig: SiteConfig = {
...rawSiteConfig,
...siteConfigOverrides
}
export function getSiteConfig<T>(key: string, defaultValue?: T): T {
const value = siteConfig[key]
if (value !== undefined) {
return value
}
if (defaultValue !== undefined) {
return defaultValue
}
throw new Error(`Config error: missing required site config value "${key}"`)
}
export function getEnv(
key: string,
defaultValue?: string,
env = process.env
): string {
const value = env[key]
if (value !== undefined) {
return value
}
if (defaultValue !== undefined) {
return defaultValue
}
throw new Error(`Config error: missing required env variable "${key}"`)
}