-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
150 lines (143 loc) · 3.57 KB
/
util.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import fs from 'fs'
import _ from 'lodash'
import yaml from 'js-yaml'
import * as v from 'valibot'
const NonEmptyString = v.pipe(v.string(), v.nonEmpty())
/**
* Informatino to allow us to display sanely worded messages about entities.
*
* - `base` is the part that doesn't change
* - `variable` should be specified in singular, and can be potentially turned into plural
* (by application code or template) when displayed.
*/
const EntityDisplay = v.object({
base: v.optional(NonEmptyString),
variable: NonEmptyString
})
export const ConfigSchema = v.object({
port: v.pipe(v.integer(), v.minValue(1)),
asyncRequestApi: v.object({
url: v.url(),
port: v.pipe(v.integer(), v.minValue(1)),
requestsEndpoint: NonEmptyString,
requestTimeout: v.number()
}),
maintenance: v.object({
serviceUnavailable: v.boolean(),
upTime: NonEmptyString
}),
aws: v.object({
region: v.string(),
bucket: v.string(),
s3ForcePathStyle: v.boolean()
}),
redis: v.optional(
v.object({
secure: v.boolean(),
host: NonEmptyString,
port: v.number()
})
),
url: v.url(),
mainWebsiteUrl: v.url(),
serviceName: NonEmptyString,
serviceNames: v.object({
check: NonEmptyString,
submit: NonEmptyString,
manage: NonEmptyString
}),
checkService: v.object({
userAgent: NonEmptyString
}),
templateContent: v.object({
feedbackLink: v.url(),
homepageUrl: NonEmptyString // relative link, e.g. '/manage
}),
email: v.object({
templates: v.object({
RequesetTemplateId: v.uuid(),
AcknowledgementTemplateId: v.uuid()
}),
dataManagementEmail: v.pipe(v.string(), v.email())
}),
datasetsConfig: v.object(
[
'article-4-direction',
'article-4-direction-area',
'brownfield-land',
'conservation-area',
'conservation-area-document',
'tree-preservation-order',
'tree-preservation-zone',
'tree',
'listed-building',
'listed-building-outline'
].reduce((acc, key) => {
acc[key] = v.object({
guidanceUrl: v.string(),
entityDisplayName: EntityDisplay
})
return acc
}, {})
),
smartlook: v.optional(
v.object({
key: v.string(),
region: v.string()
})
),
googleAnalytics: v.optional(
v.object({
measurementId: v.string()
})
),
tablePageLength: v.number(),
contact: v.object({
issues: v.object({
email: v.pipe(v.string(), v.email())
})
}),
features: v.optional(v.record(
NonEmptyString, v.object({
enabled: v.boolean()
})))
})
const readConfig = (config) => {
console.assert(config, 'config not specified')
return yaml.load(fs.readFileSync(`./config/${config}.yaml`, 'utf8'))
}
/**
* @typedef {Object} Config
*/
/**
* Reads configs from disk, based on env variables
* when 'environment' option not specified.
*
* @returns {Config}
*/
export function combineConfigs (environment) {
console.assert(environment, 'environment not specified')
const defaultConfig = readConfig('default')
const customConfig = readConfig(environment)
return _.merge({}, defaultConfig, customConfig)
}
/**
* Validates the config object against the ConfigSchema.
*
* @param {*} config
* @returns {Config}
* @throws {ValibotError}
*/
export const validateConfig = (config) => {
try {
return v.parse(ConfigSchema, config)
} catch (error) {
console.error('invalid config', error.message)
for (const issue of error.issues) {
console.info(
`issue under path: [${issue.path.map((elem) => elem.key).join(', ')}]`
)
}
throw error
}
}