-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.ts
94 lines (80 loc) · 2.74 KB
/
config.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import Joi from 'joi'
import { Level } from 'pino'
/**
* ! Configuration in this file should ONLY be used in server side code!
*/
if (typeof window !== 'undefined') {
throw new Error('server config should only be used server side')
}
export type ServerConfig = ReturnType<typeof getServerConfig>
export type EnvSchema = typeof envSchema
const envSchema = {
NODE_ENV: asString(
Joi.string()
.valid('production', 'development', 'test', 'preview')
.required()
) as 'production' | 'development' | 'test' | 'preview',
APP_ENV: asString(Joi.string()),
TESTING: asBoolean(Joi.boolean().default(false)),
LOG_LEVEL: asString(Joi.string().default('silent')) as Level,
MONGO_DB_URI: asString(Joi.string().required()),
MONGO_DB_NAME: asString(Joi.string().required()),
GH_ID: asString(Joi.string().required()),
GH_SECRET: asString(Joi.string().required()),
GOOGLE_ID: asString(Joi.string().required()),
GOOGLE_SECRET: asString(Joi.string().required()),
AUTH_SIGN_SECRET: asString(Joi.string().required()),
REVALIDATE: asNumber(Joi.number().default(3600)),
CUSTOM_SEARCH_LIMIT: asNumber(Joi.number().default(2000))
}
const serverConfigSchema = Joi.object<EnvSchema>().keys(envSchema).unknown()
export function getServerConfig(env: EnvSchema) {
const { value: envData, error } = serverConfigSchema.validate(env)
if (error) {
throw new Error(`Server config validation error: ${error.message}`)
}
const isProduction = envData.NODE_ENV === 'production'
const isDevelopment = envData.NODE_ENV === 'development'
const isTest = envData.TESTING
return Object.freeze({
env: envData.NODE_ENV,
isProduction,
isDevelopment,
isTest,
isPreview: envData.APP_ENV === 'preview',
logLevel: envData.LOG_LEVEL,
mongoDb: {
uri: envData.MONGO_DB_URI,
dbName: envData.MONGO_DB_NAME,
maxRadioCollectionLimit: 100,
clientOptions: {
retryWrites: isProduction
}
},
auth: {
github: {
clientId: envData.GH_ID,
clientSecret: envData.GH_SECRET,
type: 'oauth' as const
},
google: {
clientId: envData.GOOGLE_ID,
clientSecret: envData.GOOGLE_SECRET
},
signSecret: envData.AUTH_SIGN_SECRET,
debug: isDevelopment
},
// the amount in seconds after which page re-generation can occur
revalidate: envData.REVALIDATE,
customSearchStationLimit: envData.CUSTOM_SEARCH_LIMIT
})
}
export function asString(value: Joi.ValidationResult | Joi.AnySchema) {
return value as unknown as string
}
export function asNumber(value: Joi.ValidationResult | Joi.AnySchema) {
return value as unknown as number
}
export function asBoolean(value: Joi.ValidationResult | Joi.AnySchema) {
return value as unknown as boolean
}