UNPKG

zod-config

Version:

Load configuration variables from multiple sources with flexible adapters, ensuring type safety with Zod.

2 lines (1 loc) 4.34 kB
function p(e){return!e||typeof e!="object"?!1:Object.getPrototypeOf(e)===Object.prototype||Object.getPrototypeOf(e)===null}function g(e,...r){if(!r.length)return e;let n=r.shift();return n===void 0?e:(p(e)&&p(n)&&Object.keys(n).forEach(t=>{if(n[t]===void 0)return;if(!p(n[t])){e[t]=n[t];return}let a=e[t];if(!p(a)){e[t]=g({},n[t]);return}g(a,n[t])}),g(e,...r))}import{ZodDefault as O,ZodObject as k,ZodOptional as A}from"zod/v3";import*as d from"zod/v4/core";var z={strict:C(e=>e),lenient:C(M)},$=150;function h(e,r,n,t=0,a=$){if(t>=a||!e||!r||Object.keys(e).length===0||Object.keys(r).length===0)return e;let f=z[n];return Object.fromEntries(Object.entries(e).map(([c,o])=>{let i=Object.keys(r).find(j=>f(j,c))??c;if(!p(o))return[i,o];let u=r[i],s=b(u);return s?[i,h(o,s,n,t+1)]:[i,o]}))}function M(e){return e.replace(/[^a-z0-9]/gi,"").toLowerCase()}function w(e){return e instanceof d.$ZodObject}function R(e){return e instanceof d.$ZodDefault}function D(e){return e instanceof d.$ZodOptional}function P(e){return e instanceof k}function E(e){return e instanceof O}function K(e){return e instanceof A}function I(e){return e instanceof d.$ZodPipe&&w(e._zod.def.in)}function _(e){return e instanceof d.$ZodPipe&&w(e._zod.def.out)}function C(e){return(r,n)=>e(r)===e(n)}function T(e){if(R(e)||D(e))return T(e._zod.def.innerType);if(w(e))return e._zod.def.shape;if(I(e))return e._zod.def.in._zod.def.shape;if(_(e))return e._zod.def.out._zod.def.shape}function S(e){if(E(e)||K(e))return S(e._def.innerType);if(P(e))return e.shape}var b=e=>{if(e)return"_zod"in e?T(e):S(e)};function x(e,r,n,t){if(!t){e[r]=n;return}let a=r.split(t),f=e;for(let c=0;c<a.length;c++){let o=a[c];if(c===a.length-1){if(o in f&&typeof f[o]=="object"&&f[o]!==null)throw new Error(`Nested key conflict: "${r}" cannot be assigned because "${a.slice(0,c+1).join(t)}" already exists as an object (created by another key)`);f[o]=n}else{if(o in f){if(typeof f[o]!="object"||f[o]===null){let i=a.slice(0,c+1).join(t);throw new Error(`Nested key conflict: Cannot create nested object at "${i}" because it already exists as a primitive value. Conflicting key: "${r}" and "${i}"`)}}else f[o]={};f=f[o]}}}var Z=(e,r,n)=>!r&&!n?e:Object.entries(e).reduce((t,[a,f])=>{let c=a,o=f;if(r){let i=r({key:a,value:f});if(i===!1)return t;if(i&&typeof i=="object"&&"key"in i&&"value"in i)c=i.key,o=i.value;else throw new Error(`Invalid transform result for key "${a}": expected { key: string, value: unknown } or false, received: ${JSON.stringify(i)}`)}return n?x(t,c,o,n):t[c]=o,t},{});var l=(e,r,n,t,a)=>{let f=e;if((t||a)&&(f=Z(f,t,a)),n==="strict")return f;let c=b(r);return c?h(f,c,n):f};var y=()=>typeof process<"u"?{...process.env}:{};import{safeParseAsync as v}from"zod/v4/core";var m=(e,r,n,t)=>({keyMatching:e.keyMatching??r??"strict",silent:e.silent??n,transform:e.transform??t,nestingSeparator:e.nestingSeparator});var L=async e=>{let{schema:r,adapters:n,onError:t,onSuccess:a,keyMatching:f,silent:c,transform:o}=e,i=e.logger??console,u=await V(Array.isArray(n)?n:n?[n]:[],i,r,f,c,o),s;if("_zod"in r?s=await v(r,u):s=await r.safeParseAsync(u),!s.success){if(t)return t(s.error),{};throw s.error}return a&&a(s.data),s.data},V=async(e,r,n,t,a,f)=>{if(!e||e.length===0)return y();let c=await Promise.all(e.map(async o=>{let i=m(o,t,a,f),u;try{if(u=await o.read(),!u)return{}}catch(s){return i.silent||r.warn(`Cannot read data from ${o.name}: ${s instanceof Error?s.message:s}`),{}}return l(u,n,i.keyMatching,i.transform,i.nestingSeparator)}));return g({},...c)};import{safeParse as N}from"zod/v4/core";var H=e=>{let{schema:r,adapters:n,onError:t,onSuccess:a,keyMatching:f,silent:c,transform:o}=e,i=e.logger??console,u=B(Array.isArray(n)?n:n?[n]:[],i,r,f,c,o),s;if("_zod"in r?s=N(r,u):s=r.safeParse(u),!s.success){if(t)return t(s.error),{};throw s.error}return a&&a(s.data),s.data},B=(e,r,n,t,a,f)=>{if(!e||e.length===0)return y();let c=e.map(o=>{let i=m(o,t,a,f),u;try{if(u=o.read(),!u)return{}}catch(s){return i.silent||r.warn(`Cannot read data from ${o.name}: ${s instanceof Error?s.message:s}`),{}}if(u instanceof Promise)throw new Error(`Data returned from ${o.name} is a Promise. Use loadConfig instead of loadConfigSync to use asynchronous adapters.`);return l(u,n,i.keyMatching,i.transform,i.nestingSeparator)});return g({},...c)};export{L as loadConfig,H as loadConfigSync};