UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 1.98 kB
import{copy as e}from"../../i18n/copy.mjs";import t from"../../../constants/constants.mjs";import n from"../../../utils/errors/lucid-api-error.mjs";import r from"zod";const i=e=>{let t=e.sort;if(t)return t.split(`,`).map(e=>e.startsWith(`-`)?{key:e.slice(1),direction:`desc`}:{key:e,direction:`asc`})},a=(e,t)=>t===`in`||t===`not-in`?e.split(`,`):t!==void 0&&t!==``?e:e.includes(`,`)?e.split(`,`):e,o=(e,t)=>Object.entries(e).reduce((e,[n,r])=>{if(n.includes(`filter[`)){let i=n.match(/^filter\[([^\]:]+):?([^\]]*)\]$/);if(!i)return e;let[,o,s]=i;if(!o)return e;let c=a(r,s);t?.includes(o)&&c===``&&(c=null),e[o]={value:c,operator:s===``||s===void 0?void 0:s}}return e},{}),s=(e,t)=>{let n=new Map;for(let[r,i]of Object.entries(e)){let e=r.match(/^filter\[or\]\[(\d+)\]\[([^\]:]+):?([^\]]*)\]$/);if(!e)continue;let[,o,s,c]=e;if(!o||!s)continue;let l=Number.parseInt(o,10);if(Number.isNaN(l))continue;let u=a(i,c);t?.includes(s)&&u===``&&(u=null);let d=n.get(l)??[];d.push({key:s,value:u,operator:c===``||c===void 0?void 0:c}),n.set(l,d)}let r=Array.from(n.entries()).sort(([e],[t])=>e-t).map(([,e])=>e).filter(e=>e.length>0);return r.length>0?r:void 0},c=e=>{let n=e.page;return n?Number.parseInt(n,10):t.query.page},l=e=>{let n=e.perPage;return n?Number.parseInt(n,10):t.query.perPage},u=e=>{let t=e.include;if(t)return t.split(`,`)},d=e=>{let t=e.exclude;if(t)return t.split(`,`)},f=e=>Object.fromEntries(Object.entries(e).filter(([e])=>![`include`,`exclude`,`filter`,`sort`,`page`,`perPage`].includes(e))),p=async(t,a,p)=>{let m=a??r.object({}),h=t.req.query(),g={sort:i(h),filter:o(h,p?.nullableFields),filterOr:s(h,p?.nullableFields),include:u(h),exclude:d(h),page:c(h),perPage:l(h),...f(h)},_=await m.safeParseAsync(g);if(!_.success)throw new n({type:`validation`,message:e(`server:core.requests.query.validation.message`),zod:_.error});return{..._.data,...g.filterOr===void 0?{}:{filterOr:g.filterOr}}};export{p as default}; //# sourceMappingURL=build-formatted-query.mjs.map