34 lines
821 B
TypeScript
34 lines
821 B
TypeScript
interface IStringMap { [key: string]: string }
|
|
|
|
export default class Env {
|
|
|
|
public static get(key: string) {
|
|
return process.env[key]
|
|
}
|
|
public static getAs<T>(key: string, mapper: (val: string) => T): T | undefined {
|
|
const val = this.get(key)
|
|
return val ? mapper(val) : undefined
|
|
}
|
|
|
|
public static orElse(key: string, fallback: string) {
|
|
return this.get(key) || fallback
|
|
}
|
|
|
|
public static orFail(key: string) {
|
|
const val = this.get(key)
|
|
if (val) {
|
|
return val
|
|
}
|
|
throw Error(`Required config key: ${key}`)
|
|
}
|
|
|
|
public static map(map: IStringMap) {
|
|
const res = {} as IStringMap
|
|
for (const [key, val] of Object.entries(map)) {
|
|
res[key] = this.get(val)
|
|
}
|
|
return res
|
|
}
|
|
|
|
}
|