UNPKG

@lucidcms/core

Version:

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

2 lines 3.23 kB
import{constants_default as e,logger_default as t,lucid_api_error_default as n,translations_default as r}from"./errors-RyYO6qM6.js";import{service_wrapper_default as i}from"./service-wrapper-Duby87GY.js";import{services_default as a}from"./services-CB4SBOvq.js";import{createMiddleware as o}from"hono/factory";import{validator as s}from"hono/validator";const c=(e,t,n,r)=>s(e,async(i,a)=>{let o=i;if(e===`header`&&`_def`in t){let e=Object.keys(t.shape),n=Object.fromEntries(e.map(e=>[e.toLowerCase(),e]));o=Object.fromEntries(Object.entries(i).map(([e,t])=>[n[e]||e,t]))}let s=r?.validationFunction?await r.validationFunction(t,o):await t.safeParseAsync(o);if(n){let t=await n({data:o,...s,target:e},a);if(t){if(t instanceof Response)return t;if(`response`in t)return t.response}}return s.success?s.data:a.json(s,400)}),l=(e,t)=>c(e,t,(e,t)=>{if(!e.success)throw new n({type:`validation`,message:r(`validation_error_message`),zod:e.error})});var u=l;const d=o(async(e,t)=>{let r=a.auth.csrf.verifyToken(e);if(r.error)throw new n(r.error);return await t()});var f=d;const p=o(async(e,t)=>{let r=await a.auth.accessToken.verifyToken(e);if(r.error)throw new n(r.error);return e.set(`auth`,r.data),await t()});var m=p;const h=(e,t)=>{let i=e.get(`auth`).permissions;if(!e.get(`auth`).superAdmin){if(i===void 0)throw new n({type:`basic`,name:r(`permission_error_name`),message:r(`you_do_not_have_permission_to_perform_this_action`),status:403});if(t){for(let e of t)if(!i.includes(e))throw new n({type:`basic`,name:r(`permission_error_name`),message:r(`you_do_not_have_permission_to_perform_this_action`),status:403})}}},g=e=>o(async(t,n)=>(h(t,e),await n()));var _=g;const v=o(async(e,t)=>{let o=e.req.header(`Authorization`),s=e.get(`config`);if(!o)throw new n({type:`authorisation`,message:r(`client_integration_api_key_missing`),status:401});let c=await i(a.clientIntegrations.verifyApiKey,{transaction:!1,defaultError:{type:`authorisation`,message:r(`client_integration_error`),status:401}})({db:s.db.client,config:s,services:a},{apiKey:o});if(c.error)throw new n(c.error);return e.set(`clientIntegrationAuth`,c.data),await t()});var y=v;const b=o(async(t,r)=>{let o=t.req.header(e.headers.contentLocale),s=t.get(`config`),c=await i(a.locale.getSingleFallback,{transaction:!1})({db:s.db.client,config:s,services:a},{code:Array.isArray(o)?o[0]:o});if(c.error)throw new n(c.error);return t.set(`locale`,c.data),await r()});var x=b;const S=e=>{let[t,n]=[`,`,`.`],r=e.map(e=>e.replace(/(\d)(?=(\d\d\d)+(?!\d))/g,`$1${t}`));return r.join(n)},C=e=>{let t=Date.now()-e;return S([t<1e3?`${t}ms`:`${Math.round(t/1e3)}s`])},w=o(async(e,n)=>{let r=Date.now(),i=e.req.method,a=e.req.path,o=e.req.header(`user-agent`);t.info({message:`→ ${i} ${a}`,scope:`http`,data:{method:i,path:a,userAgent:o,timestamp:r,type:`request`}}),await n();let s=e.res.status;t.info({message:`← ${i} ${a} ${s} - ${C(r)}`,scope:`http`,data:{method:i,path:a,status:s,userAgent:o,type:`response`}})});var T=w;export{m as authenticate_default,y as client_authenticate_default,x as content_locale_default,T as log_route_default,h as permissionCheck,_ as permissions_default,f as validate_csrf_default,u as validate_default}; //# sourceMappingURL=log-route-DYL_LxgN.js.map