UNPKG

zod-config

Version:

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

5 lines (4 loc) 3.43 kB
import _ from"assert";import O from"path";function a(e){return!e||typeof e!="object"?!1:Object.getPrototypeOf(e)===Object.prototype||Object.getPrototypeOf(e)===null}function u(e,...r){if(!r.length)return e;let t=r.shift();return t===void 0?e:(a(e)&&a(t)&&Object.keys(t).forEach(n=>{if(t[n]===void 0)return;if(!a(t[n])){e[n]=t[n];return}let o=e[n];if(!a(o)){e[n]=u({},t[n]);return}u(o,t[n])}),u(e,...r))}function l(e,r){let{regex:t}=r;return t?E(e,t):e}var E=(e,r)=>{if(e==null)return{};if(!a(e))throw new TypeError(`Cannot filter ${e} by regex as it is not a record-like object`);return Object.keys(e).filter(t=>r.test(t)).reduce((t,n)=>(t[n]=e[n],t),{})};import{ZodObject as L}from"zod/v3";import*as g from"zod/v4/core";var Y={strict:m(e=>e),lenient:m(j)};function j(e){return e.replace(/[^a-z0-9]/gi,"").toLowerCase()}function m(e){return(r,t)=>e(r)===e(t)}function y(e){let r=new Map;for(let{extensions:t,adapterFactory:n}of e)for(let o of t){if(r.has(o))throw new Error(`Ambiguous adapter mapping for file extension ${o} - please ensure file extensions are specified at most once across all adapter specifiers.`);r.set(o,n)}return r}import{hostname as T}from"os";function Z(){let{NODE_CONFIG_ENV:e,NODE_ENV:r}=process.env;return e!==void 0?e:r!==void 0?r:"development"}function z(){let{NODE_APP_INSTANCE:e}=process.env;return e!==void 0?e:null}function x(){let{HOST:e,HOSTNAME:r}=process.env;return e!==void 0?e:r!==void 0?r:T()}function F(){let e=x(),r=e.indexOf(".");return r===-1?e:e.substring(0,r)}function A(){return{deploymentName:Z(),instanceName:z(),hostname:x(),shortHostname:F()}}function R(){let{instanceName:e,deploymentName:r,shortHostname:t,hostname:n}=A();return e===null?["default",r,t,`${t}-${r}`,n,`${n}-${r}`,"local",`local-${r}`]:["default",`default-${e}`,r,`${r}-${e}`,t,`${t}-${r}`,`${t}-${e}`,`${t}-${r}-${e}`,n,`${n}-${r}`,`${n}-${e}`,`${n}-${r}-${e}`,"local",`local-${r}`,`local-${e}`,`local-${r}-${e}`]}import M from"assert";import{readdir as N}from"fs/promises";import{basename as D,extname as S}from"path";async function $({path:e,allowedFilenames:r,allowedExtensions:t}){let n=await N(e),o=[];for(let i of n){let s=S(i),f=D(i,s);!r.includes(f)||!t.includes(s)||o.push({dir:e,name:f,ext:s})}return o}async function b({paths:e,allowedFilenames:r,allowedExtensions:t}){return Array.isArray(e)?(await Promise.all(e.map(o=>$({path:o,allowedFilenames:r,allowedExtensions:t})))).flat():await $({path:e,allowedFilenames:r,allowedExtensions:t})}function w(e,r,t){e.sort((n,o)=>{let i=r.indexOf(n.name),s=r.indexOf(o.name);if(i!==s)return i-s;if(n.dir===o.dir)return 0;M(Array.isArray(t));let f=t.indexOf(n.dir),d=t.indexOf(o.dir);return f-d})}var v="directory adapter",ge=({paths:e,adapters:r,silent:t,regex:n})=>({name:v,read:async()=>{try{let o=y(Array.isArray(r)?r:[r]),i=R(),s=Array.from(o.keys()),f=await b({paths:e,allowedFilenames:i,allowedExtensions:s});w(f,i,e);let h=f.map(p=>{let c=o.get(p.ext);return _(c!==void 0),{...p,adapter:c(O.format(p))}}).map(async p=>{try{return await p.adapter.read()}catch(c){throw new Error(`Cannot read data from ${p.adapter.name} for ${O.format(p)}: ${c instanceof Error?c.message:c}`)}}),P=await Promise.all(h),C=u({},...P);return l(C,{regex:n})}catch(o){throw new Error(`Failed to read config from some of the following directories: - ${Array.isArray(e)?e.join(` - `):e} Reason: ${o instanceof Error?o.message:o}`)}},silent:t});export{ge as directoryAdapter};