UNPKG

zod-config

Version:

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

5 lines (4 loc) 3.48 kB
import T from"assert";import w from"path";var F=(e,r)=>{if(e==null)return{};if(!p(e))throw new TypeError(`Cannot filter ${e} by prefix key as it is not a record-like object`);return Object.keys(e).filter(t=>t.startsWith(r)).reduce((t,n)=>(t[n]=e[n],t),{})},h=(e,r)=>{if(e==null)return{};if(!p(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),{})};function u(e,...r){if(!r.length)return e;let t=r.shift();return t===void 0?e:(p(e)&&p(t)&&Object.keys(t).forEach(n=>{if(t[n]===void 0)return;if(!p(t[n])){e[n]=t[n];return}let o=e[n];if(!p(o)){e[n]=u({},t[n]);return}u(o,t[n])}),u(e,...r))}function p(e){return!e||typeof e!="object"?!1:Object.getPrototypeOf(e)===Object.prototype||Object.getPrototypeOf(e)===null}function d(e,r){let{regex:t,prefixKey:n}=r;return t?h(e,t):n?F(e,n):e}function m(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 N}from"os";function b(){let{NODE_CONFIG_ENV:e,NODE_ENV:r}=process.env;return e!==void 0?e:r!==void 0?r:"development"}function O(){let{NODE_APP_INSTANCE:e}=process.env;return e!==void 0?e:null}function g(){let{HOST:e,HOSTNAME:r}=process.env;return e!==void 0?e:r!==void 0?r:N()}function D(){let e=g(),r=e.indexOf(".");return r===-1?e:e.substring(0,r)}function y(){return{deploymentName:b(),instanceName:O(),hostname:g(),shortHostname:D()}}function x(){let{instanceName:e,deploymentName:r,shortHostname:t,hostname:n}=y();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 v from"assert";import{readdir as M}from"fs/promises";import{basename as j,extname as I}from"path";async function A({path:e,allowedFilenames:r,allowedExtensions:t}){let n=await M(e),o=[];for(let i of n){let s=I(i),f=j(i,s);!r.includes(f)||!t.includes(s)||o.push({dir:e,name:f,ext:s})}return o}async function R({paths:e,allowedFilenames:r,allowedExtensions:t}){return Array.isArray(e)?(await Promise.all(e.map(o=>A({path:o,allowedFilenames:r,allowedExtensions:t})))).flat():await A({path:e,allowedFilenames:r,allowedExtensions:t})}function $(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;v(Array.isArray(t));let f=t.indexOf(n.dir),c=t.indexOf(o.dir);return f-c})}var k="directory adapter",ee=({paths:e,adapters:r,prefixKey:t,silent:n,regex:o})=>({name:k,read:async()=>{try{let i=m(Array.isArray(r)?r:[r]),s=x(),f=Array.from(i.keys()),c=await R({paths:e,allowedFilenames:s,allowedExtensions:f});$(c,s,e);let C=c.map(a=>{let l=i.get(a.ext);return T(l!==void 0),{...a,adapter:l(w.format(a))}}).map(async a=>{try{return await a.adapter.read()}catch(l){throw new Error(`Cannot read data from ${a.adapter.name} for ${w.format(a)}: ${l instanceof Error?l.message:l}`)}}),P=await Promise.all(C),E=u({},...P);return d(E,{prefixKey:t,regex:o})}catch(i){throw new Error(`Failed to read config from some of the following directories: - ${Array.isArray(e)?e.join(` - `):e} Reason: ${i instanceof Error?i.message:i}`)}},silent:n});export{ee as directoryAdapter};