-
Notifications
You must be signed in to change notification settings - Fork 6
/
envConfig.ts
45 lines (38 loc) · 1.28 KB
/
envConfig.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
import "@std/dotenv/load";
/** Prefix used for environment variable config options */
const ENVIRONMENT_VARIABLE_PREFIX = "DISCORD_EVENTS_SYNC_";
const getEnv = (name: string, required: boolean = true) => {
const envName = ENVIRONMENT_VARIABLE_PREFIX + name;
const value = Deno.env.get(envName);
if (value) {
return value;
}
if (required) {
const message = `Environment {${envName}} not found.`;
throw new Error(message);
}
};
export const loadEnvConfig = () => {
const serviceAccountKeyJson = getEnv(
"GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_JSON",
false,
);
const apiKey = getEnv("GOOGLE_CALENDAR_API_KEY", false);
if (!serviceAccountKeyJson && !apiKey) {
throw new Error(
`Either {${ENVIRONMENT_VARIABLE_PREFIX}GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_JSON} or {${ENVIRONMENT_VARIABLE_PREFIX}GOOGLE_CALENDAR_API_KEY} must be provided.`,
);
}
const config = {
discord: {
guildId: getEnv("DISCORD_GUILD_ID")!,
botToken: getEnv("DISCORD_BOT_TOKEN")!,
applicationId: getEnv("DISCORD_APPLICATION_ID")!,
},
googleCalendar: {
calendarId: getEnv("GOOGLE_CALENDAR_CALENDAR_ID")!,
...(serviceAccountKeyJson ? { serviceAccountKeyJson: serviceAccountKeyJson! } : { apiKey: apiKey! }),
},
};
return config;
};