UNPKG

@becklyn/contentful-adapter

Version:

[![CI](https://github.com/Becklyn-Studios/contentful-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/Becklyn-Studios/contentful-adapter/actions/workflows/ci.yml)

134 lines (133 loc) 5.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeDynamicDataConfigData = exports.normalizeRelationTypeData = void 0; const util_1 = require("./util"); const ui_types_1 = require("@becklyn/ui-types"); const asset_1 = require("./asset"); const reference_1 = require("./reference"); const normalizer_1 = require("./normalizer"); const api_1 = require("../contentful/api"); const normalizeRelationTypeData = async (dataType, data, service) => { if ((0, util_1.isSingleRelationType)(dataType)) { return await normalizeSingleRelationTypeData(dataType, data, service); } return (0, util_1.isArrayRelationType)(dataType) ? normalizeArrayRelationTypeData(dataType, data, service) : null; }; exports.normalizeRelationTypeData = normalizeRelationTypeData; const normalizeSingleRelationTypeData = async (dataType, data, service) => { const relatedType = dataType.data; if (!relatedType || (0, util_1.isArray)(data)) { return null; } if ((0, util_1.isBaseComponentConfig)(relatedType)) { return await (0, normalizer_1.normalizeDataForDataConfig)(data, relatedType.data || {}, service); } return (0, util_1.isArray)(relatedType) ? await (0, exports.normalizeDynamicDataConfigData)(relatedType, data, service) : null; }; const normalizeArrayRelationTypeData = async (dataType, data, service) => { const relatedType = dataType.data; if (!relatedType || !(0, util_1.isArray)(data)) { return null; } if ((0, util_1.isBaseComponentConfig)(relatedType)) { const normalizedData = []; for (let i = 0; i < data.length; i++) { const normalized = await (0, normalizer_1.normalizeDataForDataConfig)(data[i], relatedType.data || {}, service); if (normalized) { normalizedData.push(normalized); } } return normalizedData; } if ((0, util_1.isArray)(relatedType)) { const normalizedData = []; for (let i = 0; i < data.length; i++) { const normalized = await (0, exports.normalizeDynamicDataConfigData)(relatedType, data[i], service); if (normalized) { normalizedData.push(normalized); } const normalizedDataCustomNormalizer = await normalizeDynamicDataCustomNormalizer(relatedType, data[i], service); if (normalizedDataCustomNormalizer) { normalizedData.push(normalizedDataCustomNormalizer); } } return normalizedData; } const normalizer = "string" === typeof relatedType ? service.getCustomNormalizer(relatedType) : null; if (normalizer) { const normalizedData = []; for (let i = 0; i < data.length; i++) { const value = await normalizer(data[i], service); if (value) { normalizedData.push(value); } } return normalizedData; } if (ui_types_1.TYPE_STRING === relatedType) { return data.filter(element => "string" === typeof element); } if (ui_types_1.TYPE_ASSET === relatedType) { const normalizedData = []; for (let i = 0; i < data.length; i++) { const asset = await (0, asset_1.normalizeAssetData)(data[i], service); if (asset) { normalizedData.push(asset); } } return normalizedData; } if (ui_types_1.TYPE_LABELED_LINK === relatedType) { const normalizedData = []; for (let i = 0; i < data.length; i++) { const labeledLink = await (0, reference_1.normalizeLabeledLink)(data[i], service); if (labeledLink) { normalizedData.push(labeledLink); } } return normalizedData; } return null; }; const normalizeDynamicDataConfigData = async (dataTypes, fieldData, service) => { if (!fieldData.sys) { return null; } const allowedContentTypes = dataTypes.map(componentKey => "string" === typeof componentKey ? (0, util_1.getContentTypeFromComponentKey)(componentKey, service) : componentKey.key); if (!fieldData.fields) { // load missing data const newData = await (0, api_1.findOneEntryBySys)(fieldData.sys, service.client, { depth: 10 }); if (null === newData) { return null; } fieldData = newData; } const contentType = (0, util_1.getContentTypeFromData)(fieldData); if (null === contentType || !allowedContentTypes.includes(contentType)) { return null; } const dataConfig = (0, util_1.getDataConfigForContentType)(contentType, service); if (null === dataConfig) { return null; } return await (0, normalizer_1.normalizeDataForDataConfig)(fieldData, dataConfig, service); }; exports.normalizeDynamicDataConfigData = normalizeDynamicDataConfigData; const normalizeDynamicDataCustomNormalizer = async (dataTypes, fieldData, service) => { for (const dataType of dataTypes) { const normalizer = "string" === typeof dataType ? service.getCustomNormalizer(dataType) : null; if (normalizer) { const value = await normalizer(fieldData, service); if (value) { return value; } } } return null; };