@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
81 lines • 3.11 kB
JavaScript
import { isValidId } from '../utilities/id';
import { isLocalizedFieldValue, } from '../utilities/normalizedFieldValues';
/**
* =============================================================================
* 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
*/
export function isItemId(input) {
return typeof input === 'string';
}
/**
* Validates if the input is a RawApiTypes.Item object (with optional `id` and `meta`)
*/
export function isItemWithOptionalIdAndMeta(block) {
return (typeof block === 'object' &&
block !== null &&
'type' in block &&
block.type === 'item' &&
'attributes' in block &&
'relationships' in block);
}
/**
* Validates if the input is a a complete RawApiTypes.Item object with optional `meta`
*/
export function isItemWithOptionalMeta(block) {
return (isItemWithOptionalIdAndMeta(block) &&
'id' in block &&
typeof block.id === 'string');
}
/**
* =============================================================================
* 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.
*/
export function isSingleBlockFieldValue(value) {
return (typeof value === 'string' && isValidId(value)) || value === null;
}
export function isLocalizedSingleBlockFieldValue(value) {
return (isLocalizedFieldValue(value) &&
Object.values(value).every(isSingleBlockFieldValue));
}
/**
* Type guard for Single Block field values in API request format.
* Allows block as string ID, full object with ID, or object without ID.
*/
export 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)
return isItemWithOptionalIdAndMeta(value);
}
export function isLocalizedSingleBlockFieldValueInRequest(value) {
return (isLocalizedFieldValue(value) &&
Object.values(value).every(isSingleBlockFieldValueInRequest));
}
/**
* Type guard for Single Block field values with nested blocks (?nested=true format).
* Ensures block is a full RawApiTypes.Item object with complete data.
*/
export 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);
}
export function isLocalizedSingleBlockFieldValueInNestedResponse(value) {
return (isLocalizedFieldValue(value) &&
Object.values(value).every(isSingleBlockFieldValueInNestedResponse));
}
//# sourceMappingURL=single_block.js.map