@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 3.3 kB
Source Map (JSON)
{"version":3,"file":"resolve-document-includes.mjs","names":[],"sources":["../../../../src/services/documents/helpers/resolve-document-includes.ts"],"sourcesContent":["import registeredFields, {\n\tregisteredFieldTypes,\n} from \"../../../libs/collection/custom-fields/registered-fields.js\";\nimport type { FieldTypes } from \"../../../types.js\";\n\ntype DocumentRefInclude = \"refs\" | `refs.${string}`;\ntype DocumentInclude = \"bricks\" | \"meta\" | DocumentRefInclude;\n\nexport type ResolvedDocumentIncludes = {\n\tbricks: boolean;\n\trefs: boolean;\n\tmeta: boolean;\n\trefTypes?: FieldTypes[];\n};\n\nconst refsPrefix = \"refs.\";\n\n/**\n * Returns true when a typed ref include maps to a registered custom field that\n * can both fetch and format hydrated reference data.\n */\nconst isSupportedRefFieldType = (value: string): value is FieldTypes => {\n\tif (!registeredFieldTypes.includes(value as FieldTypes)) return false;\n\n\tconst field = registeredFields[value as FieldTypes];\n\treturn field.fetchRefs !== null && field.formatRef !== null;\n};\n\n/**\n * Matches the generic typed ref include syntax, such as `refs.relation` or a\n * future custom field include like `refs.product`.\n */\nconst isTypedRefInclude = (value: string): value is `refs.${string}` => {\n\treturn value.startsWith(refsPrefix) && value.length > refsPrefix.length;\n};\n\n/**\n * Normalizes document include query values into service flags.\n *\n * `refs` fetches every supported ref-capable custom field, while `refs.<type>`\n * enables refs but narrows fetching to registered ref-capable field types only.\n * Unknown typed ref includes are accepted by the query layer but resolve to an\n * empty type list, so no ref rows are fetched for unsupported custom fields.\n */\nconst resolveDocumentIncludes = (\n\tinclude?: DocumentInclude[] | string[],\n): ResolvedDocumentIncludes => {\n\tconst includeValues = include ?? [];\n\tconst includesAllRefs = includeValues.includes(\"refs\");\n\tconst requestedRefTypes = new Set<FieldTypes>();\n\tlet hasTypedRefInclude = false;\n\n\tfor (const includeValue of includeValues) {\n\t\tif (!isTypedRefInclude(includeValue)) continue;\n\n\t\thasTypedRefInclude = true;\n\t\tconst fieldType = includeValue.slice(refsPrefix.length);\n\t\tif (isSupportedRefFieldType(fieldType)) {\n\t\t\trequestedRefTypes.add(fieldType);\n\t\t}\n\t}\n\n\treturn {\n\t\tbricks: includeValues.includes(\"bricks\"),\n\t\trefs: includesAllRefs || hasTypedRefInclude,\n\t\tmeta: includeValues.includes(\"meta\"),\n\t\trefTypes: includesAllRefs\n\t\t\t? undefined\n\t\t\t: hasTypedRefInclude\n\t\t\t\t? [...requestedRefTypes]\n\t\t\t\t: undefined,\n\t};\n};\n\nexport default resolveDocumentIncludes;\n"],"mappings":"uGAeA,MAMM,EAA2B,GAAuC,CACvE,GAAI,CAAC,EAAqB,SAAS,CAAmB,EAAG,MAAO,GAEhE,IAAM,EAAQ,EAAiB,GAC/B,OAAO,EAAM,YAAc,MAAQ,EAAM,YAAc,IACxD,EAMM,EAAqB,GACnB,EAAM,WAAW,OAAU,GAAK,EAAM,OAAS,EAWjD,EACL,GAC8B,CAC9B,IAAM,EAAgB,GAAW,CAAC,EAC5B,EAAkB,EAAc,SAAS,MAAM,EAC/C,EAAoB,IAAI,IAC1B,EAAqB,GAEzB,IAAK,IAAM,KAAgB,EAAe,CACzC,GAAI,CAAC,EAAkB,CAAY,EAAG,SAEtC,EAAqB,GACrB,IAAM,EAAY,EAAa,MAAM,CAAiB,EAClD,EAAwB,CAAS,GACpC,EAAkB,IAAI,CAAS,CAEjC,CAEA,MAAO,CACN,OAAQ,EAAc,SAAS,QAAQ,EACvC,KAAM,GAAmB,EACzB,KAAM,EAAc,SAAS,MAAM,EACnC,SAAU,EACP,IAAA,GACA,EACC,CAAC,GAAG,CAAiB,EACrB,IAAA,EACL,CACD"}