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.57 kB
import{copy as e}from"../i18n/copy.mjs";import{hookExecutionKinds as t}from"./hook-map.mjs";import{createDraft as n,finishDraft as r}from"immer";const i=e=>typeof e!=`object`||!e?!1:`data`in e,a=e=>{let t=[],n=0;for(let r=0;r<e.config.hooks.length;r++){let i=e.config.hooks[r];i!==void 0&&(i.service!==e.service||i.event!==e.event||t.push({handler:i.handler,priority:`priority`in i?i.priority:void 0,order:n++}))}if(e.collectionInstance?.config.hooks===void 0)return t;for(let r=0;r<e.collectionInstance.config.hooks.length;r++){let i=e.collectionInstance.config.hooks[r];i!==void 0&&i.service===e.service&&i.event===e.event&&t.push({handler:i.handler,priority:`priority`in i?i.priority:void 0,order:n++})}return t},o=e=>a(e).sort((e,t)=>{let n=(e.priority??0)-(t.priority??0);return n===0?e.order-t.order:n}),s=async(e,t)=>{for(let n of a(e)){let e=await n.handler(...t);if(e.error)return e}return{error:void 0,data:void 0}},c=async(t,a)=>{let s=a[1];if(!i(s))return{error:{type:`basic`,name:e(`server:core.hooks.execution.error.name`),message:e(`server:core.hooks.transform.payload.error.message`,{data:{service:String(t.service),event:String(t.event)}}),status:500},data:void 0};let c=s.data;for(let e of o(t)){let t=n(c),i=[a[0],{...s,data:t}],o=await e.handler(...i);if(o.error)return o;if(o.data===void 0||o.data===t){c=r(t);continue}r(t),c=o.data}return{error:void 0,data:c}},l=async(e,n,...r)=>{let i=[e,...r];return(t[String(n.service)]?.[String(n.event)]??`effect`)===`transform`?c(n,i):s(n,i)};export{l as default}; //# sourceMappingURL=execute-hooks.mjs.map