-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
125 lines (110 loc) · 3.4 KB
/
config.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
import { env } from 'node:process';
import { Agent, setGlobalDispatcher } from 'undici';
import 'dotenv/config';
// Need to activate HTTP/2 for APN Request with native fetch
// cf: https://github.com/nodejs/undici/issues/2750
setGlobalDispatcher(new Agent({
allowH2: true,
}));
function isFeatureEnabled(environmentVariable) {
return environmentVariable === 'true';
}
function getParsedJson(environmentVariable) {
if (environmentVariable === undefined) {
return undefined;
}
return JSON.parse(environmentVariable);
}
function buildConfiguration() {
const config = {
environment: env.NODE_ENV || 'development',
port: env.PORT || 4000,
baseURL: env.BASE_URL || 'http://example.net',
secret: env.SECRET,
logging: {
enabled: isFeatureEnabled(env.LOG_ENABLED),
logLevel: env.LOG_LEVEL || 'info',
},
cronTime: env.CRON_TIME || '* */5 * * * *',
gymlib: {
imapConfig: {
host: env.GYMLIB_MAIL_RECEIVER_IMAP_HOST,
port: env.GYMLIB_MAIL_RECEIVER_IMAP_PORT,
user: env.GYMLIB_MAIL_RECEIVER_IMAP_USER,
password: env.GYMLIB_MAIL_RECEIVER_IMAP_PASSWORD,
},
searchQuery: getParsedJson(env.GYMLIB_MAIL_RECEIVER_IMAP_SEARCH_QUERY),
},
ucpa: {
imapConfig: {
host: env.UCPA_MAIL_RECEIVER_IMAP_HOST,
port: env.UCPA_MAIL_RECEIVER_IMAP_PORT,
user: env.UCPA_MAIL_RECEIVER_IMAP_USER,
password: env.UCPA_MAIL_RECEIVER_IMAP_PASSWORD,
},
searchQuery: getParsedJson(env.UCPA_MAIL_RECEIVER_IMAP_SEARCH_QUERY),
formInfo: getParsedJson(env.FORM_RESPONSE),
formSubmit: isFeatureEnabled(env.FORM_SUBMIT_ENABLED),
areaId: env.UCPA_AREA_ID,
},
notifications: {
ntfy: {
url: env.NOTIFICATIONS_NTFY_URL,
token: env.NOTIFICATIONS_NTFY_TOKEN,
topic: env.NOTIFICATIONS_NTFY_TOPIC,
},
apple: {
url: env.NOTIFICATIONS_APPLE_URL,
topic: env.APPLE_PASS_TYPE_IDENTIFIER,
},
},
calendar: {
name: env.CALENDAR_NAME,
id: env.CALENDAR_ID,
},
timeSlotsPreferences: getParsedJson(env.TIME_SLOTS_PREFERENCES),
certificates: {
signerKeyPassphrase: env.CERTIFICATES_SIGNER_KEY_PASSPHRASE,
},
apple: {
teamIdentifier: env.APPLE_TEAM_IDENTIFIER,
passTypeIdentifier: env.APPLE_PASS_TYPE_IDENTIFIER,
},
browser: {
browserWSEndpoint: env.BROWSER_WS_ENDPOINT,
},
};
if (config.environment === 'test') {
config.logging.enabled = false;
config.secret = 'SECRET_FOR_TESTS';
config.apple.passTypeIdentifier = 'pass-identifier';
}
if (!verifyConfig(config)) {
throw new Error('Invalid config');
}
return config;
}
function verifyConfig(config) {
if (env.NODE_ENV === 'test') {
return true;
}
let allKeysHaveValues = true;
function checkDeep(object, path) {
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
const value = object[key];
const currentPath = path ? `${path}.${key}` : key;
if (typeof value === 'object' && value !== null) {
checkDeep(value, currentPath);
}
else if (value === null || value === undefined) {
console.error(`Key "${currentPath}" does not have a valid value.`);
allKeysHaveValues = false;
}
}
}
}
checkDeep(config, '');
return allKeysHaveValues;
}
export const config = buildConfiguration();