UNPKG

@lucidcms/core

Version:

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

1 lines 3.08 kB
{"version":3,"file":"format-ref.mjs","names":["documentBricksFormatter","documentFieldsFormatter"],"sources":["../../../../../../src/libs/collection/custom-fields/fields/relation/format-ref.ts"],"sourcesContent":["import {\n\tdocumentBricksFormatter,\n\tdocumentFieldsFormatter,\n} from \"../../../../formatters/index.js\";\nimport type { BrickQueryResponse } from \"../../../../repositories/document-bricks.js\";\nimport type { CFResponse, FieldRefParams } from \"../../types.js\";\n\nconst isBrickQueryResponse = (value: unknown): value is BrickQueryResponse => {\n\tif (typeof value !== \"object\" || value === null) return false;\n\n\treturn \"id\" in value && \"document_id\" in value && \"collection_key\" in value;\n};\n\nconst formatRelationRef = (\n\tvalue: unknown,\n\tparams: FieldRefParams,\n): CFResponse<\"relation\">[\"ref\"] | null => {\n\tif (!isBrickQueryResponse(value)) return null;\n\n\tconst targetCollectionKey = value.collection_key;\n\tconst collection = params.config.collections.find(\n\t\t(c) => c.key === targetCollectionKey,\n\t);\n\n\tconst targetBricksSchema =\n\t\ttargetCollectionKey === params.collection.key\n\t\t\t? params.bricksTableSchema\n\t\t\t: params.relationRefMeta?.fieldsSchemaByCollection?.[targetCollectionKey]\n\t\t\t\t? [params.relationRefMeta.fieldsSchemaByCollection[targetCollectionKey]]\n\t\t\t\t: undefined;\n\n\tif (!collection || !targetBricksSchema) {\n\t\treturn {\n\t\t\tid: value.document_id,\n\t\t\tversionId: value.id,\n\t\t\tcollectionKey: targetCollectionKey,\n\t\t\tfields: null,\n\t\t};\n\t}\n\n\tconst formattedFields = documentBricksFormatter.formatDocumentFields({\n\t\tbricksQuery: value,\n\t\tbricksSchema: targetBricksSchema,\n\t\trefData: { data: {} },\n\t\tcollection: collection,\n\t\tconfig: params.config,\n\t\thost: params.host,\n\t});\n\tconst documentFields = params.flattenRelationRefFields\n\t\t? documentFieldsFormatter.flattenFields(\n\t\t\t\tformattedFields,\n\t\t\t\tcollection.clientFieldTree,\n\t\t\t)\n\t\t: documentFieldsFormatter.objectifyFields(formattedFields);\n\n\treturn {\n\t\tid: value.document_id,\n\t\tversionId: value.id,\n\t\tcollectionKey: targetCollectionKey,\n\t\tfields: Object.keys(documentFields).length > 0 ? documentFields : null,\n\t} satisfies CFResponse<\"relation\">[\"ref\"];\n};\n\nexport default formatRelationRef;\n"],"mappings":"oHAOA,MAAM,EAAwB,GACzB,OAAO,GAAU,WAAY,EAAuB,GAEjD,OAAQ,GAAS,gBAAiB,GAAS,mBAAoB,EAGjE,GACL,EACA,IAC0C,CAC1C,GAAI,CAAC,EAAqB,CAAK,EAAG,OAAO,KAEzC,IAAM,EAAsB,EAAM,eAC5B,EAAa,EAAO,OAAO,YAAY,KAC3C,GAAM,EAAE,MAAQ,CAClB,EAEM,EACL,IAAwB,EAAO,WAAW,IACvC,EAAO,kBACP,EAAO,iBAAiB,2BAA2B,GAClD,CAAC,EAAO,gBAAgB,yBAAyB,EAAoB,EACrE,IAAA,GAEL,GAAI,CAAC,GAAc,CAAC,EACnB,MAAO,CACN,GAAI,EAAM,YACV,UAAW,EAAM,GACjB,cAAe,EACf,OAAQ,IACT,EAGD,IAAM,EAAkBA,EAAwB,qBAAqB,CACpE,YAAa,EACb,aAAc,EACd,QAAS,CAAE,KAAM,CAAC,CAAE,EACR,aACZ,OAAQ,EAAO,OACf,KAAM,EAAO,IACd,CAAC,EACK,EAAiB,EAAO,yBAC3BC,EAAwB,cACxB,EACA,EAAW,eACZ,EACCA,EAAwB,gBAAgB,CAAe,EAE1D,MAAO,CACN,GAAI,EAAM,YACV,UAAW,EAAM,GACjB,cAAe,EACf,OAAQ,OAAO,KAAK,CAAc,CAAC,CAAC,OAAS,EAAI,EAAiB,IACnE,CACD"}