UNPKG

@datocms/cma-client

Version:
427 lines 16.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.inspectItem = void 0; const Utils = __importStar(require("@datocms/rest-client-utils")); const datocms_structured_text_utils_1 = require("datocms-structured-text-utils"); const fieldTypes_1 = require("../fieldTypes"); const resources_1 = require("../generated/resources"); const normalizedFieldValues_1 = require("./normalizedFieldValues"); const fieldTypeHandlers = [ { guard: fieldTypes_1.isBooleanFieldValue, inspect: booleanInspectionTreeNodes }, { guard: fieldTypes_1.isFloatFieldValue, inspect: floatInspectionTreeNodes }, { guard: fieldTypes_1.isIntegerFieldValue, inspect: integerInspectionTreeNodes }, { guard: fieldTypes_1.isColorFieldValue, inspect: colorInspectionTreeNodes }, { guard: fieldTypes_1.isDateFieldValue, inspect: dateInspectionTreeNodes }, { guard: fieldTypes_1.isDateTimeFieldValue, inspect: dateTimeInspectionTreeNodes }, { guard: fieldTypes_1.isGalleryFieldValue, inspect: galleryInspectionTreeNodes }, { guard: fieldTypes_1.isGalleryFieldValueInRequest, inspect: galleryInspectionTreeNodes }, { guard: fieldTypes_1.isFileFieldValue, inspect: fileInspectionTreeNodes }, { guard: fieldTypes_1.isFileFieldValueInRequest, inspect: fileInspectionTreeNodes }, { guard: fieldTypes_1.isJsonFieldValue, inspect: jsonInspectionTreeNodes }, { guard: fieldTypes_1.isSlugFieldValue, inspect: slugInspectionTreeNodes }, { guard: fieldTypes_1.isStringFieldValue, inspect: stringInspectionTreeNodes }, { guard: fieldTypes_1.isTextFieldValue, inspect: textInspectionTreeNodes }, { guard: fieldTypes_1.isLatLonFieldValue, inspect: latLonInspectionTreeNodes }, { guard: fieldTypes_1.isLinkFieldValue, inspect: linkInspectionTreeNodes }, { guard: fieldTypes_1.isLinksFieldValue, inspect: linksInspectionTreeNodes }, { guard: fieldTypes_1.isSeoFieldValue, inspect: seoFieldInspectionTreeNodes }, { guard: fieldTypes_1.isVideoFieldValue, inspect: videoFieldInspectionTreeNodes }, { guard: fieldTypes_1.isStructuredTextFieldValue, inspect: structuredTextInspectionTreeNodes, }, { guard: fieldTypes_1.isStructuredTextFieldValueInRequest, inspect: structuredTextInspectionTreeNodes, }, { guard: fieldTypes_1.isStructuredTextFieldValueInNestedResponse, inspect: structuredTextInspectionTreeNodes, }, { guard: fieldTypes_1.isSingleBlockFieldValue, inspect: singleBlockInspectionTreeNodes }, { guard: fieldTypes_1.isSingleBlockFieldValueInRequest, inspect: singleBlockInspectionTreeNodes, }, { guard: fieldTypes_1.isSingleBlockFieldValueInNestedResponse, inspect: singleBlockInspectionTreeNodes, }, { guard: fieldTypes_1.isRichTextFieldValue, inspect: richTextInspectionTreeNodes }, { guard: fieldTypes_1.isRichTextFieldValueInRequest, inspect: richTextInspectionTreeNodes, }, { guard: fieldTypes_1.isRichTextFieldValueInNestedResponse, inspect: richTextInspectionTreeNodes, }, ]; function createChildNode(attributeName, inspectResult) { if (typeof inspectResult === 'string') { return { label: `${attributeName}: ${inspectResult}` }; } if (Array.isArray(inspectResult)) { return { label: attributeName, nodes: inspectResult, }; } // Single TreeNode return { label: attributeName, nodes: [inspectResult], }; } function inspectFieldValue(value, options) { if ((0, normalizedFieldValues_1.isLocalizedFieldValue)(value)) { for (const handler of fieldTypeHandlers) { if (Object.values(value).every((localeValue) => handler.guard(localeValue))) { const localeEntries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b)); return localeEntries.map(([locale, localeValue]) => { const inspectResult = handler.inspect(localeValue, options); if (typeof inspectResult === 'string') { return { label: `${locale}: ${inspectResult}` }; } if (Array.isArray(inspectResult)) { return { label: locale, nodes: inspectResult, }; } // Single TreeNode return { label: locale, nodes: [inspectResult], }; }); } } } for (const handler of fieldTypeHandlers) { if (handler.guard(value)) { return handler.inspect(value, options); } } // Fallback for unknown field types return `UNKNOWN: ${JSON.stringify(value)}`; } function normalizeItem(item) { if ('attributes' in item) { return item; } const { __itemTypeId } = item, itemWithoutItemTypeId = __rest(item, ["__itemTypeId"]); return Utils.serializeRequestBody(itemWithoutItemTypeId, { type: resources_1.Item.TYPE, attributes: '*', relationships: ['item_type', 'creator'], }).data; } function inspectItem(item, options) { return (0, datocms_structured_text_utils_1.formatAsTree)(itemInspectionTreeNodes(item, options)); } exports.inspectItem = inspectItem; function itemInspectionTreeNodes(item, options) { var _a, _b; let itemTypeId; const normalizedItem = normalizeItem(item); if ('relationships' in normalizedItem && normalizedItem.relationships) { const relationships = normalizedItem.relationships; if ((_b = (_a = relationships.item_type) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.id) { itemTypeId = relationships.item_type.data.id; } } const rootLabel = [ 'Item', 'id' in item ? JSON.stringify(normalizedItem.id) : null, itemTypeId ? `(item_type: ${JSON.stringify(itemTypeId)})` : null, ] .filter(Boolean) .join(' '); const nodes = []; if ('attributes' in normalizedItem) { for (const [attributeName, attributeValue] of Object.entries(normalizedItem.attributes)) { const inspectResult = inspectFieldValue(attributeValue, options); nodes.push(createChildNode(attributeName, inspectResult)); } } return { label: rootLabel, nodes: nodes.length > 0 ? nodes : undefined, }; } function structuredTextInspectionTreeNodes(value, options) { if (value === null) return 'null'; return (0, datocms_structured_text_utils_1.inspectionTreeNodes)(value, { maxWidth: (options === null || options === void 0 ? void 0 : options.maxWidth) || 80, blockFormatter: (block, maxWidth) => { if (typeof block === 'string') { return JSON.stringify(block); } return itemInspectionTreeNodes(block, options); }, }); } function singleBlockInspectionTreeNodes(value, options) { if (value === null) return 'null'; if (typeof value === 'string') return JSON.stringify(value); return itemInspectionTreeNodes(value, options); } function richTextInspectionTreeNodes(value, options) { if (value === null) return 'null'; return value.map((item, index) => { const result = singleBlockInspectionTreeNodes(item, options); if (typeof result === 'string') { return { label: `[${index}] ${result}` }; } result.label = `[${index}] ${result.label}`; return result; }); } function booleanInspectionTreeNodes(value) { return JSON.stringify(value); } function colorInspectionTreeNodes(value) { if (value === null) return 'null'; const { red, green, blue, alpha } = value; const hex = `#${red.toString(16).padStart(2, '0').toUpperCase()}${green.toString(16).padStart(2, '0').toUpperCase()}${blue.toString(16).padStart(2, '0').toUpperCase()}`; if (alpha === 255) { return hex; } const alphaPercent = Math.round((alpha / 255) * 100); return `${hex} ${alphaPercent}%`; } function dateInspectionTreeNodes(value) { if (value === null) return 'null'; return value; } function dateTimeInspectionTreeNodes(value) { if (value === null) return 'null'; return value; } function fileInspectionTreeNodes(value, options) { if (value === null) return 'null'; const nodes = []; nodes.push({ label: `upload_id: ${JSON.stringify(value.upload_id)}` }); if (value.alt) { nodes.push({ label: `alt: ${JSON.stringify(truncate(value.alt, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } if (value.title) { nodes.push({ label: `title: ${JSON.stringify(truncate(value.title, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } if (value.custom_data && Object.keys(value.custom_data).length > 0) { nodes.push({ label: `custom_data: ${truncate(JSON.stringify(value.custom_data), (options === null || options === void 0 ? void 0 : options.maxWidth) || 80)}`, }); } if (value.focal_point) { const x = value.focal_point.x * 100; const y = value.focal_point.y * 100; nodes.push({ label: `focal_point: x=${x}% y=${y}%` }); } return nodes; } function floatInspectionTreeNodes(value) { if (value === null) return 'null'; return value.toString(); } function galleryInspectionTreeNodes(value, options) { if (!value || value.length === 0) return '[]'; const itemNodes = value.map((item, index) => { const nodes = []; nodes.push({ label: `upload_id: ${JSON.stringify(item.upload_id)}`, }); if (item.alt) { nodes.push({ label: `alt: ${JSON.stringify(truncate(item.alt, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } if (item.title) { nodes.push({ label: `title: ${JSON.stringify(truncate(item.title, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } if (item.custom_data && Object.keys(item.custom_data).length > 0) { nodes.push({ label: `custom_data: ${truncate(JSON.stringify(item.custom_data), (options === null || options === void 0 ? void 0 : options.maxWidth) || 80)}`, }); } if (item.focal_point) { const x = item.focal_point.x * 100; const y = item.focal_point.y * 100; nodes.push({ label: `focal_point: x=${x}% y=${y}%` }); } return { label: `[${index}]`, nodes: nodes, }; }); return itemNodes; } function integerInspectionTreeNodes(value) { if (value === null) return 'null'; return value.toString(); } function jsonInspectionTreeNodes(value, options) { if (value === null) return 'null'; return truncate(JSON.stringify(JSON.parse(value)), (options === null || options === void 0 ? void 0 : options.maxWidth) || 80); } function latLonInspectionTreeNodes(value) { if (value === null) return 'null'; return [ { label: `latitude: ${value.latitude}` }, { label: `longitude: ${value.longitude}` }, ]; } function linkInspectionTreeNodes(value) { if (value === null) return 'null'; return JSON.stringify(value); } function linksInspectionTreeNodes(value) { if (value === null) return 'null'; return value.map((itemId, index) => ({ label: `[${index}]: ${JSON.stringify(itemId)}`, })); } function seoFieldInspectionTreeNodes(value, options) { if (value === null) return 'null'; const nodes = []; if (value.title) { nodes.push({ label: `title: ${JSON.stringify(truncate(value.title, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } if (value.description) { nodes.push({ label: `description: ${JSON.stringify(truncate(value.description, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } if (value.image) { nodes.push({ label: `image: ${value.image}`, }); } if (value.twitter_card) { nodes.push({ label: `twitter_card: ${value.twitter_card}`, }); } if (value.no_index !== undefined) { nodes.push({ label: `no_index: ${value.no_index}`, }); } return nodes; } function slugInspectionTreeNodes(value, options) { if (value === null) return 'null'; return JSON.stringify(truncate(value, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80)); } function stringInspectionTreeNodes(value, options) { if (value === null) return 'null'; return JSON.stringify(truncate(value, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80)); } function textInspectionTreeNodes(value, options) { if (value === null) return 'null'; return JSON.stringify(truncate(value, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80)); } function videoFieldInspectionTreeNodes(value, options) { if (value === null) return 'null'; const nodes = []; nodes.push({ label: `provider: ${value.provider}` }); nodes.push({ label: `provider_uid: ${value.provider_uid}` }); nodes.push({ label: `url: ${value.url}` }); nodes.push({ label: `size: ${value.width}${value.height}px` }); nodes.push({ label: `thumbnail_url: ${value.thumbnail_url}` }); if (value.title) { nodes.push({ label: `title: ${JSON.stringify(truncate(value.title, (options === null || options === void 0 ? void 0 : options.maxWidth) || 80))}`, }); } return nodes; } function localizedFieldValueInspectionTreeNodes(value, options) { const localeEntries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b)); return localeEntries.map(([locale, localeValue]) => { const inspectResult = inspectFieldValue(localeValue, options); if (typeof inspectResult === 'string') { return { label: `${locale}: ${inspectResult}` }; } if (Array.isArray(inspectResult)) { return { label: locale, nodes: inspectResult, }; } // Single TreeNode return { label: locale, nodes: [inspectResult], }; }); } function truncate(text, maxWidth) { if (text.length <= maxWidth) return text; return `${text.slice(0, maxWidth - 3)}...`; } //# sourceMappingURL=inspectItem.js.map