UNPKG

@datocms/cma-client

Version:
400 lines 15.6 kB
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; }; import * as Utils from '@datocms/rest-client-utils'; import { formatAsTree, inspectionTreeNodes, } from 'datocms-structured-text-utils'; import { isBooleanFieldValue, isColorFieldValue, isDateFieldValue, isDateTimeFieldValue, isFileFieldValue, isFileFieldValueInRequest, isFloatFieldValue, isGalleryFieldValue, isGalleryFieldValueInRequest, isIntegerFieldValue, isJsonFieldValue, isLatLonFieldValue, isLinkFieldValue, isLinksFieldValue, isRichTextFieldValue, isRichTextFieldValueInNestedResponse, isRichTextFieldValueInRequest, isSeoFieldValue, isSingleBlockFieldValue, isSingleBlockFieldValueInNestedResponse, isSingleBlockFieldValueInRequest, isSlugFieldValue, isStringFieldValue, isStructuredTextFieldValue, isStructuredTextFieldValueInNestedResponse, isStructuredTextFieldValueInRequest, isTextFieldValue, isVideoFieldValue, } from '../fieldTypes'; import { Item as ItemResource } from '../generated/resources'; import { isLocalizedFieldValue } from './normalizedFieldValues'; const fieldTypeHandlers = [ { guard: isBooleanFieldValue, inspect: booleanInspectionTreeNodes }, { guard: isFloatFieldValue, inspect: floatInspectionTreeNodes }, { guard: isIntegerFieldValue, inspect: integerInspectionTreeNodes }, { guard: isColorFieldValue, inspect: colorInspectionTreeNodes }, { guard: isDateFieldValue, inspect: dateInspectionTreeNodes }, { guard: isDateTimeFieldValue, inspect: dateTimeInspectionTreeNodes }, { guard: isGalleryFieldValue, inspect: galleryInspectionTreeNodes }, { guard: isGalleryFieldValueInRequest, inspect: galleryInspectionTreeNodes }, { guard: isFileFieldValue, inspect: fileInspectionTreeNodes }, { guard: isFileFieldValueInRequest, inspect: fileInspectionTreeNodes }, { guard: isJsonFieldValue, inspect: jsonInspectionTreeNodes }, { guard: isSlugFieldValue, inspect: slugInspectionTreeNodes }, { guard: isStringFieldValue, inspect: stringInspectionTreeNodes }, { guard: isTextFieldValue, inspect: textInspectionTreeNodes }, { guard: isLatLonFieldValue, inspect: latLonInspectionTreeNodes }, { guard: isLinkFieldValue, inspect: linkInspectionTreeNodes }, { guard: isLinksFieldValue, inspect: linksInspectionTreeNodes }, { guard: isSeoFieldValue, inspect: seoFieldInspectionTreeNodes }, { guard: isVideoFieldValue, inspect: videoFieldInspectionTreeNodes }, { guard: isStructuredTextFieldValue, inspect: structuredTextInspectionTreeNodes, }, { guard: isStructuredTextFieldValueInRequest, inspect: structuredTextInspectionTreeNodes, }, { guard: isStructuredTextFieldValueInNestedResponse, inspect: structuredTextInspectionTreeNodes, }, { guard: isSingleBlockFieldValue, inspect: singleBlockInspectionTreeNodes }, { guard: isSingleBlockFieldValueInRequest, inspect: singleBlockInspectionTreeNodes, }, { guard: isSingleBlockFieldValueInNestedResponse, inspect: singleBlockInspectionTreeNodes, }, { guard: isRichTextFieldValue, inspect: richTextInspectionTreeNodes }, { guard: isRichTextFieldValueInRequest, inspect: richTextInspectionTreeNodes, }, { guard: 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 (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: ItemResource.TYPE, attributes: '*', relationships: ['item_type', 'creator'], }).data; } export function inspectItem(item, options) { return formatAsTree(itemInspectionTreeNodes(item, options)); } 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 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