UNPKG

@layerfig/config

Version:

Layer and runtime-validate type-safe configs for JavaScript apps.

40 lines (38 loc) 1.43 kB
import { Source } from "../../source-BvmiqUNz.js"; import { set } from "../../set-LPILcL1N.js"; import { z } from "zod/v4"; //#region src/sources/env-var.ts const EnvironmentVariableSourceOptions = z.object({ prefix: z.string().optional().default("APP"), prefixSeparator: z.string().optional().default("_"), separator: z.string().optional().default("__") }); const PartialEnvironmentVariableSourceOptions = EnvironmentVariableSourceOptions.partial(); var EnvironmentVariableSource = class extends Source { #options; #prefixWithSeparator; constructor(options = {}) { super(); this.#options = EnvironmentVariableSourceOptions.parse(options); this.#prefixWithSeparator = `${this.#options.prefix}${this.#options.prefixSeparator}`; } loadSource({ runtimeEnv, slotPrefix }) { const envKeys = Object.keys(runtimeEnv).filter((key) => key.startsWith(this.#prefixWithSeparator)); const tempObject = {}; for (const envKey of envKeys) { const envVarValue = runtimeEnv[envKey]; if (envVarValue === void 0) continue; const keyWithoutPrefix = envKey.replace(this.#prefixWithSeparator, ""); const keyParts = keyWithoutPrefix.split(this.#options.separator).join("."); set(tempObject, keyParts, this.maybeReplaceSlotFromValue({ value: envVarValue, runtimeEnv, slotPrefix })); } return tempObject; } }; //#endregion export { EnvironmentVariableSource }; //# sourceMappingURL=index.js.map