-
Notifications
You must be signed in to change notification settings - Fork 1
/
configuration.ts
70 lines (65 loc) · 1.4 KB
/
configuration.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
import { z } from "zod"
type LoggingDestination =
| {
type: "stdout"
}
| {
type: "file"
path: string
}
const stringToLoggingDestination = (str: string): LoggingDestination => {
if (str === "stdout") {
return {
type: "stdout",
}
} else {
return {
type: "file",
path: str,
}
}
}
const configuration = z.object({
log: z.object({
level: z.string().default("info"),
destination: z.string().transform(stringToLoggingDestination),
}),
bluOs: z
.object({
ip: z.string(),
port: z.string().transform(Number),
})
.optional(),
lastFm: z.object({
apiKey: z.string(),
apiSecret: z.string(),
}),
session: z.object({
filePath: z.string().default(".blu-hawaii-session"),
}),
})
export type Configuration = z.infer<typeof configuration>
export const parseConfiguration = (
source: Partial<Record<string, string>>,
): Configuration => {
return configuration.parse({
log: {
level: source["LOG_LEVEL"],
destination: source["LOG_DEST"],
},
logLevel: source["LOG_LEVEL"],
bluOs:
(source["BLUOS_IP"] && {
ip: source["BLUOS_IP"],
port: source["BLUOS_PORT"],
}) ||
undefined,
lastFm: {
apiKey: source["LAST_FM_API_KEY"],
apiSecret: source["LAST_FM_API_SECRET"],
},
session: {
filePath: source["SESSION_FILE_PATH"],
},
})
}