UNPKG

@datocms/cma-client

Version:
117 lines 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isLocalizedSingleBlockFieldValueInNestedResponse = exports.isSingleBlockFieldValueInNestedResponse = exports.isLocalizedSingleBlockFieldValueInRequest = exports.isSingleBlockFieldValueInRequest = exports.isBlockObjectInRequest = exports.isLocalizedSingleBlockFieldValue = exports.isSingleBlockFieldValue = exports.isItemWithOptionalMeta = exports.isItemWithOptionalIdAndMeta = exports.isItemId = void 0; const id_js_1 = require("../utilities/id.js"); const normalizedFieldValues_js_1 = require("../utilities/normalizedFieldValues.js"); /** * ============================================================================= * SHARED UTILITY FUNCTIONS * ============================================================================= * These functions are used internally and can be imported by other modules */ /** * Validates if the input is a valid item (either block or record) ID */ function isItemId(input) { return typeof input === 'string'; } exports.isItemId = isItemId; /** * Validates if the input is a RawApiTypes.Item object (with optional `id` and `meta`) */ function isItemWithOptionalIdAndMeta(block) { return (typeof block === 'object' && block !== null && 'type' in block && block.type === 'item' && 'attributes' in block && 'relationships' in block); } exports.isItemWithOptionalIdAndMeta = isItemWithOptionalIdAndMeta; /** * Validates if the input is a a complete RawApiTypes.Item object with optional `meta` */ function isItemWithOptionalMeta(block) { return (isItemWithOptionalIdAndMeta(block) && 'id' in block && typeof block.id === 'string'); } exports.isItemWithOptionalMeta = isItemWithOptionalMeta; /** * ============================================================================= * TYPE GUARDS - Runtime validation functions * ============================================================================= */ /** * Type guard for basic Single Block field values (block as string ID only). * Checks for string structure and ensures block is a string reference. */ function isSingleBlockFieldValue(value) { return (typeof value === 'string' && (0, id_js_1.isValidId)(value)) || value === null; } exports.isSingleBlockFieldValue = isSingleBlockFieldValue; function isLocalizedSingleBlockFieldValue(value) { return ((0, normalizedFieldValues_js_1.isLocalizedFieldValue)(value) && Object.values(value).every(isSingleBlockFieldValue)); } exports.isLocalizedSingleBlockFieldValue = isLocalizedSingleBlockFieldValue; /** * Shape check for a block object on the *request* side. Accepts every object * form the CMA allows inside a request payload: * * 1. New block (no id, full body): * { type: 'item', attributes, relationships: { item_type } } * 2. Updated block, full body: * { type: 'item', id, attributes, relationships: { item_type } } * 3. Updated block, id-only (patch some attributes of an existing block): * { type: 'item', id, attributes } ← no relationships * * Case 3 is what `buildBlockRecord` produces when the caller omits * `item_type` — the server derives the model from the existing block's id, * so re-specifying it in `relationships` is redundant. */ function isBlockObjectInRequest(block) { return (typeof block === 'object' && block !== null && 'type' in block && block.type === 'item' && 'attributes' in block); } exports.isBlockObjectInRequest = isBlockObjectInRequest; /** * Type guard for Single Block field values in API request format. * Allows block as string ID, full object with ID, or object without ID. */ function isSingleBlockFieldValueInRequest(value) { if (value === null) return true; // String ID - referencing existing block if (isItemId(value)) return true; // Object (either with or without ID for updates/creation, including // id-only updates without `relationships`) return isBlockObjectInRequest(value); } exports.isSingleBlockFieldValueInRequest = isSingleBlockFieldValueInRequest; function isLocalizedSingleBlockFieldValueInRequest(value) { return ((0, normalizedFieldValues_js_1.isLocalizedFieldValue)(value) && Object.values(value).every(isSingleBlockFieldValueInRequest)); } exports.isLocalizedSingleBlockFieldValueInRequest = isLocalizedSingleBlockFieldValueInRequest; /** * Type guard for Single Block field values with nested blocks (?nested=true format). * Ensures block is a full RawApiTypes.Item object with complete data. */ function isSingleBlockFieldValueInNestedResponse(value) { if (value === null) return true; // Must be a full object with ID (nested format always includes complete block objects) return isItemWithOptionalMeta(value); } exports.isSingleBlockFieldValueInNestedResponse = isSingleBlockFieldValueInNestedResponse; function isLocalizedSingleBlockFieldValueInNestedResponse(value) { return ((0, normalizedFieldValues_js_1.isLocalizedFieldValue)(value) && Object.values(value).every(isSingleBlockFieldValueInNestedResponse)); } exports.isLocalizedSingleBlockFieldValueInNestedResponse = isLocalizedSingleBlockFieldValueInNestedResponse; //# sourceMappingURL=single_block.js.map