refactor: better env handling (#5)
Some checks failed
CD / Test (push) Successful in 1m0s
CD / Build and push (amd64) (push) Has been cancelled
CD / Build and push (arm64) (push) Has been cancelled

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:
2025-01-07 19:46:32 +01:00
committed by t.behrendt
parent 94b03a97d4
commit 990e7b1dbe
6 changed files with 162 additions and 93 deletions

53
src/env.ts Normal file
View 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());