refactor: better env handling (#5)
Reviewed-on: #5 Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #5.
This commit is contained in:
53
src/env.ts
Normal file
53
src/env.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { from } from "env-var";
|
||||
|
||||
import type { LogLevel, Mode } from "./types";
|
||||
|
||||
const envVar = from(process.env, {
|
||||
asLogLevel: (value): LogLevel => {
|
||||
const logLevels = ["error", "info", "debug"];
|
||||
if (logLevels.includes(value)) {
|
||||
return value as LogLevel;
|
||||
} else {
|
||||
throw new Error("Invalid log level");
|
||||
}
|
||||
},
|
||||
asTs3GotifyMode: (value): Mode => {
|
||||
const modes = ["connect", "disconnect", "moved", "message"];
|
||||
if (modes.includes(value)) {
|
||||
return value as Mode;
|
||||
} else {
|
||||
throw new Error("Invalid mode");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export const LOG_LEVEL = envVar.get("LOG_LEVEL").default("info").asLogLevel();
|
||||
|
||||
export const TS3_HOST = envVar.get("TS3_HOST").required().asString();
|
||||
export const TS3_QUERY_PORT = envVar
|
||||
.get("TS3_QUERY_PORT")
|
||||
.default(10011)
|
||||
.asPortNumber();
|
||||
export const TS3_SERVER_PORT = envVar
|
||||
.get("TS3_SERVER_PORT")
|
||||
.default(9987)
|
||||
.asPortNumber();
|
||||
export const TS3_USERNAME = envVar.get("TS3_USERNAME").required().asString();
|
||||
export const TS3_PASSWORD = envVar.get("TS3_PASSWORD").required().asString();
|
||||
export const TS3_NICKNAME = envVar
|
||||
.get("TS3_NICKNAME")
|
||||
.default("ts3gotify")
|
||||
.asString();
|
||||
|
||||
export const GOTIFY_URL = envVar.get("GOTIFY_URL").required().asUrlString();
|
||||
export const GOTIFY_TOKEN = envVar.get("GOTIFY_TOKEN").required().asString();
|
||||
export const GOTIFY_TITLE = envVar
|
||||
.get("GOTIFY_TITLE")
|
||||
.default("ts3gotify")
|
||||
.asString();
|
||||
|
||||
export const MODE = envVar
|
||||
.get("MODE")
|
||||
.default("['connect']")
|
||||
.asJsonArray()
|
||||
.map((value) => value.asTs3GotifyMode());
|
||||
Reference in New Issue
Block a user