UNPKG

@liplum/env

Version:

Reading and parsing environment variables from "process.env"

135 lines 4.21 kB
import { createLateinitGetter } from "./shared.js"; import { getValueFromStore, missingEnvError } from "./utils.js"; import { NodeEnv } from "./node.js"; import { ArrayEnv } from "./env/array.js"; import { BoolEnv } from "./env/bool.js"; import { FloatEnv } from "./env/float.js"; import { IntEnv } from "./env/int.js"; import { PortEnv } from "./env/port.js"; import { StringEnv } from "./env/string.js"; import { UrlEnv } from "./env/url.js"; import { NextPhase } from "./nextjs.js"; export * from "./model.js"; const mixinWithValueEnvs = (Base) => { return class MixinWithValueEnvs extends Base { /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ string = (options) => { const { default: defaultValue } = options ?? {}; return new StringEnv(this, createLateinitGetter(defaultValue)); }; /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ bool = (options) => { const { default: defaultValue } = options ?? {}; return new BoolEnv(this, createLateinitGetter(defaultValue)); }; /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ int = (options) => { const { default: defaultValue } = options ?? {}; return new IntEnv(this, createLateinitGetter(defaultValue)); }; /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ float = (options) => { const { default: defaultValue } = options ?? {}; return new FloatEnv(this, createLateinitGetter(defaultValue)); }; /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ port = (options) => { const { default: defaultValue } = options ?? {}; return new PortEnv(this, createLateinitGetter(defaultValue)); }; /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ array = (options) => { const { default: defaultValue } = options ?? {}; return new ArrayEnv(this, createLateinitGetter(defaultValue)); }; /** * * @param options If a default value lazy callback is provided, it will be called only once. * @returns */ url = (options) => { const { default: defaultValue } = options ?? {}; return new UrlEnv(this, createLateinitGetter(defaultValue)); }; }; }; const Env = mixinWithValueEnvs(class { key; store; constructor({ key, store }) { this.key = key; this.store = store; } from = (store) => { return new Env({ key: this.key, store: store, }); }; get = () => { const raw = this.getOrNull(); if (raw === undefined) { throw missingEnvError(this.key); } return raw; }; getOrNull = () => { return getValueFromStore({ key: this.key, store: this.store, }); }; }); const EnvFromValue = mixinWithValueEnvs(class { key; value; constructor({ value }) { this.value = value; } get = () => { const raw = this.getOrNull(); if (raw === undefined) { throw missingEnvError(this.key); } return raw; }; getOrNull = () => { return this.value; }; }); const env = (key) => { return new Env({ key }); }; env.NODE_ENV = new NodeEnv(); env.NEXT_PHASE = new NextPhase(); env.fromValue = (value) => { return new EnvFromValue({ value }); }; export const NODE_ENV = env.NODE_ENV; export const NEXT_PHASE = env.NEXT_PHASE; export default env; //# sourceMappingURL=index.js.map