UNPKG

@agility/cli

Version:

Agility CLI for working with your content. (Public Beta)

212 lines 9.99 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContentFieldMapper = void 0; exports.createContentFieldMapper = createContentFieldMapper; var asset_reference_extractor_1 = require("../assets/asset-reference-extractor"); function createContentFieldMapper() { return new ContentFieldMapper(); } var ContentFieldMapper = /** @class */ (function () { function ContentFieldMapper() { this.assetExtractor = new asset_reference_extractor_1.AssetReferenceExtractor(); } ContentFieldMapper.prototype.mapContentFields = function (fields, context) { if (!fields || typeof fields !== 'object') { return { mappedFields: fields, validationWarnings: 0, validationErrors: 0 }; } var validationWarnings = 0; var validationErrors = 0; var mappedFields = __assign({}, fields); // Process each field for asset URL mapping and other transformations for (var _i = 0, _a = Object.entries(mappedFields); _i < _a.length; _i++) { var _b = _a[_i], fieldName = _b[0], fieldValue = _b[1]; try { var mappingResult = this.mapSingleField(fieldName, fieldValue, context); mappedFields[fieldName] = mappingResult.mappedValue; validationWarnings += mappingResult.warnings; validationErrors += mappingResult.errors; } catch (error) { console.warn("\u26A0\uFE0F Error mapping field ".concat(fieldName, ": ").concat(error.message)); validationErrors++; // Keep original value on error } } return { mappedFields: mappedFields, validationWarnings: validationWarnings, validationErrors: validationErrors }; }; ContentFieldMapper.prototype.mapSingleField = function (fieldName, fieldValue, context) { var _this = this; var warnings = 0; var errors = 0; var mappedValue = fieldValue; // Handle null/undefined values if (fieldValue === null || fieldValue === undefined) { return { mappedValue: mappedValue, warnings: warnings, errors: errors }; } // Handle asset attachment fields (ImageAttachment, FileAttachment, AttachmentList) if (this.isAssetAttachmentField(fieldValue)) { var assetMappingResult = this.mapAssetAttachmentField(fieldValue, context); mappedValue = assetMappingResult.mappedValue; warnings += assetMappingResult.warnings; errors += assetMappingResult.errors; } // Handle content reference fields (contentID, sortids, etc.) else if (this.isContentReferenceField(fieldValue)) { var contentMappingResult = this.mapContentReferenceField(fieldValue, context); mappedValue = contentMappingResult.mappedValue; warnings += contentMappingResult.warnings; errors += contentMappingResult.errors; } // Handle URL fields with potential asset references else if (typeof fieldValue === 'string' && fieldValue.includes('cdn.aglty.io')) { var urlMappingResult = this.mapAssetUrlString(fieldValue, context); mappedValue = urlMappingResult.mappedValue; warnings += urlMappingResult.warnings; errors += urlMappingResult.errors; } // Handle nested objects recursively else if (typeof fieldValue === 'object' && fieldValue !== null) { if (Array.isArray(fieldValue)) { mappedValue = fieldValue.map(function (item, index) { var itemResult = _this.mapSingleField("".concat(fieldName, "[").concat(index, "]"), item, context); warnings += itemResult.warnings; errors += itemResult.errors; return itemResult.mappedValue; }); } else { mappedValue = {}; for (var _i = 0, _a = Object.entries(fieldValue); _i < _a.length; _i++) { var _b = _a[_i], key = _b[0], value = _b[1]; var nestedResult = this.mapSingleField("".concat(fieldName, ".").concat(key), value, context); mappedValue[key] = nestedResult.mappedValue; warnings += nestedResult.warnings; errors += nestedResult.errors; } } } return { mappedValue: mappedValue, warnings: warnings, errors: errors }; }; ContentFieldMapper.prototype.isAssetAttachmentField = function (fieldValue) { if (!fieldValue || typeof fieldValue !== 'object') return false; // Check for asset attachment patterns if (Array.isArray(fieldValue)) { return fieldValue.some(function (item) { return item && typeof item === 'object' && 'url' in item; }); } else { return 'url' in fieldValue && typeof fieldValue.url === 'string'; } }; ContentFieldMapper.prototype.isContentReferenceField = function (fieldValue) { if (!fieldValue || typeof fieldValue !== 'object') return false; // Check for content reference patterns return 'contentid' in fieldValue || 'contentID' in fieldValue || 'sortids' in fieldValue; }; ContentFieldMapper.prototype.mapAssetAttachmentField = function (fieldValue, context) { var _this = this; var warnings = 0; var errors = 0; if (!(context === null || context === void 0 ? void 0 : context.referenceMapper)) { return { mappedValue: fieldValue, warnings: 1, errors: 0 }; } if (Array.isArray(fieldValue)) { // AttachmentList - array of asset objects var mappedArray = fieldValue.map(function (assetObj) { if (assetObj && typeof assetObj === 'object' && assetObj.url) { var mappedUrl = _this.mapAssetUrl(assetObj.url, context); if (mappedUrl !== assetObj.url) { return __assign(__assign({}, assetObj), { url: mappedUrl }); } } return assetObj; }); return { mappedValue: mappedArray, warnings: warnings, errors: errors }; } else { // Single asset object (ImageAttachment/FileAttachment) if (fieldValue.url) { var mappedUrl = this.mapAssetUrl(fieldValue.url, context); if (mappedUrl !== fieldValue.url) { return { mappedValue: __assign(__assign({}, fieldValue), { url: mappedUrl }), warnings: warnings, errors: errors }; } } return { mappedValue: fieldValue, warnings: warnings, errors: errors }; } }; ContentFieldMapper.prototype.mapContentReferenceField = function (fieldValue, context) { var warnings = 0; var errors = 0; var mappedValue = __assign({}, fieldValue); if (!(context === null || context === void 0 ? void 0 : context.referenceMapper)) { return { mappedValue: fieldValue, warnings: 1, errors: 0 }; } // Map contentid/contentID references if (fieldValue.contentid || fieldValue.contentID) { var sourceContentId = fieldValue.contentid || fieldValue.contentID; var contentMapping = context.referenceMapper.getContentItemMappingByContentID(sourceContentId, 'source'); if (contentMapping && contentMapping.contentID) { if (fieldValue.contentid !== undefined) { mappedValue.contentid = contentMapping.contentID; } if (fieldValue.contentID !== undefined) { mappedValue.contentID = contentMapping.contentID; } } else { warnings++; } } // Map sortids (comma-separated content IDs) if (fieldValue.sortids) { var sourceIds = fieldValue.sortids.toString().split(',').map(function (id) { return parseInt(id.trim()); }); var mappedIds = sourceIds.map(function (sourceId) { var mapping = context.referenceMapper.getContentItemMappingByContentID(sourceId, 'source'); return mapping ? mapping.contentID : sourceId; }); mappedValue.sortids = mappedIds.join(','); } return { mappedValue: mappedValue, warnings: warnings, errors: errors }; }; ContentFieldMapper.prototype.mapAssetUrlString = function (url, context) { var mappedUrl = this.mapAssetUrl(url, context); return { mappedValue: mappedUrl, warnings: mappedUrl === url ? 1 : 0, // Warning if no mapping found errors: 0 }; }; ContentFieldMapper.prototype.mapAssetUrl = function (sourceUrl, context) { // Try to find the asset by URL in the asset mapper var assetMapping = context.assetMapper.getAssetMappingByMediaUrl(sourceUrl, "source"); if (assetMapping) { var asset = assetMapping; return asset.originUrl || asset.url || asset.edgeUrl || sourceUrl; } // Return original URL if no mapping found return sourceUrl; }; return ContentFieldMapper; }()); exports.ContentFieldMapper = ContentFieldMapper; //# sourceMappingURL=content-field-mapper.js.map