UNPKG

@darkobits/env

Version:

Functional environment variable getter/parser.

42 lines (41 loc) 1.49 kB
const parsedEnv = {}; const env = (variableName, strict = false) => { if (typeof variableName !== "string" && typeof variableName !== "number") { throw new TypeError(`[env] Expected first argument to be of type "string" or "number", got "${typeof variableName}".`); } if (typeof process === "undefined") { throw new TypeError('[env] Global "process" does not exist.'); } if (typeof process !== "object") { throw new TypeError(`[env] Expected "process" to be of type "object", got "${typeof process}".`); } if (!Reflect.has(process, "env")) { throw new Error('[env] "env" does not exist in object "process".'); } if (typeof process.env !== "object") { throw new TypeError(`[env] Expected "process.env" to be of type "object", got "${typeof process.env}".`); } const rawValue = process.env[variableName]; if (strict && rawValue === void 0) { throw new Error(`[env] (Strict) "${variableName}" does not exist in object "process.env".`); } try { parsedEnv[variableName] = JSON.parse(rawValue); } catch { parsedEnv[variableName] = rawValue; } return parsedEnv[variableName]; }; env.has = (variableName) => { return env(variableName) !== void 0; }; env.eq = (variableName, value, strict = false) => { if (typeof value === "object") { return JSON.stringify(value) === JSON.stringify(env(variableName, strict)); } return env(variableName, strict) === value; }; export { env as default }; //# sourceMappingURL=env.js.map