@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2.21 kB
JavaScript
import{copy as e}from"../../../libs/i18n/copy.mjs";import{createFieldRefFetchPlan as t}from"../../../libs/collection/custom-fields/utils/ref-fetch.mjs";import n from"../../../libs/collection/helpers/build-table-name.mjs";import{getCollectionClientScope as r}from"../../../libs/permission/client-scopes.mjs";import i,{registeredFieldTypes as a}from"../../../libs/collection/custom-fields/registered-fields.mjs";const o=e=>e.fetchRefs!==null,s=e=>Object.keys(e).filter(e=>a.includes(e)),c=e=>e.fieldDefinition.planFetchRefs?.({fieldType:e.fieldType,relations:e.relations,versionType:e.versionType,resolveVersionType:e.resolveVersionType,fetchRefs:e.fieldDefinition.fetchRefs})??t({fieldType:e.fieldType,relations:e.relations,versionType:e.versionType,resolveVersionType:e.resolveVersionType,fetchRefs:e.fieldDefinition.fetchRefs}),l=async(t,a)=>{if(a.allowedDocumentCollectionKeys!==void 0){let i=new Map;for(let e of t.config.collections){let r=n(`document`,{collection:e.key},t.config.db.config.tableNameByteLimit);if(r.error)return r;i.set(r.data.name,e.key)}let o=new Set(a.allowedDocumentCollectionKeys),s=Array.from(new Set((a.values.relation??[]).flatMap(e=>{let t=i.get(e.table);return t&&!o.has(t)?[t]:[]})));if(s.length>0){let t=s.map(r);return{error:{type:`authorisation`,name:e(`server:core.client.integrations.scopes.error.name`),message:e(`server:core.client.integrations.scopes.missing.message`,{data:{requiredScopes:t.join(`, `),missingScopes:t.join(`, `)}}),status:403},data:void 0}}}let l={data:{}},u=s(a.values).flatMap(e=>{let t=a.values[e],n=i[e];if(!t||t.length===0||!o(n))return[];let r=c({fieldType:e,fieldDefinition:n,relations:t,versionType:a.versionType,resolveVersionType:a.resolveVersionType});return r?[r]:[]}),d=await Promise.all(u.map(async e=>({fieldType:e.fieldType,res:await e.run(t)})));for(let{fieldType:e,res:t}of d){if(t.error)return{data:void 0,error:t.error};t.data&&(l.data[e]=t.data.rows,t.data.meta?.relation&&(l.meta={...l.meta,relation:{fieldsSchemaByCollection:{...l.meta?.relation?.fieldsSchemaByCollection||{},...t.data.meta.relation.fieldsSchemaByCollection}}}))}return{data:l,error:void 0}};export{l as default};
//# sourceMappingURL=fetch-ref-data.mjs.map