@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
115 lines (114 loc) • 8.13 kB
TypeScript
/**
* DatoCMS Block Field Value Processing Utilities
*
* This utility provides a unified interface for working with blocks embedded within DatoCMS field values.
* DatoCMS supports three field types that can contain blocks:
* - Modular Content fields: arrays of blocks
* - Single Block fields: a single block
* - Structured Text fields: complex document structures with embedded blocks
*
* The challenge this solves: Each field type stores blocks differently and requires different
* traversal logic, making it complex to perform operations like transformations, filtering,
* or searching across blocks regardless of their containing field type.
*
* This utility abstracts away these differences, providing a consistent API to:
* - Visit/iterate through all blocks in any field type
* - Transform blocks while preserving field structure
* - Filter blocks based on conditions
* - Search for specific blocks
* - Perform functional operations (map, reduce, some, every)
*
* All functions come in both sync and async variants to support different use cases,
* particularly useful when block transformations require async operations like API calls.
*/
import { type TreePath } from 'datocms-structured-text-utils';
import type { BlockInRequest } from '../fieldTypes';
import type * as ApiTypes from '../generated/ApiTypes';
/**
* Visit every block in a field value, calling the visitor function for each block found.
* Supports rich text, single block, and structured text field types.
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to visit
* @param visitor - Asynchronous function called for each block. Receives the block item and its path
* @returns Promise that resolves when all blocks have been visited
*/
export declare function nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, visitor: (item: BlockInRequest, path: TreePath) => Promise<void>): Promise<void>;
/**
* Transform blocks in a field value by applying a mapping function to each block.
* Creates a new field value structure with transformed blocks while preserving the original structure.
* Supports rich text, single block, and structured text field types.
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to transform
* @param mapper - Synchronous function that transforms each block. Receives block item and path, returns new block
* @returns The new field value with transformed blocks
*/
export declare function nonRecursiveMapBlocksInNonLocalizedFieldValue(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, mapper: (item: BlockInRequest, path: TreePath) => BlockInRequest): unknown;
/**
* Transform blocks in a field value by applying a mapping function to each block.
* Creates a new field value structure with transformed blocks while preserving the original structure.
* Supports rich text, single block, and structured text field types.
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to transform
* @param mapper - Asynchronous function that transforms each block. Receives block item and path, returns new block
* @returns Promise that resolves to the new field value with transformed blocks
*/
export declare function nonRecursiveMapBlocksInNonLocalizedFieldValueAsync(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, mapper: (item: BlockInRequest, path: TreePath) => Promise<BlockInRequest>): Promise<unknown>;
/**
* Find all blocks that match the predicate function.
* Searches through all blocks in the non-localized field value and returns all matches.
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to search
* @param predicate - Asynchronous function that tests each block. Should return true for matching blocks
* @returns Promise that resolves to an array of objects, each containing a matching block and its path
*/
export declare function nonRecursiveFindAllBlocksInNonLocalizedFieldValueAsync(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>): Promise<Array<{
item: BlockInRequest;
path: TreePath;
}>>;
/**
* Filter blocks in a field value, removing those that don't match the predicate.
* Creates a new field value containing only blocks that pass the predicate test.
* Preserves the original field value structure and hierarchy.
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to filter
* @param predicate - Asynchronous function that tests each block. Blocks returning false are removed
* @returns Promise that resolves to the new field value with filtered blocks
*/
export declare function nonRecursiveFilterBlocksInNonLocalizedFieldValueAsync(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>): Promise<unknown>;
/**
* Reduce all blocks in a field value to a single value by applying a reducer function.
* Processes each block in the non-localized field value and accumulates the results into a single value.
*
* @template R - The type of the accumulated result
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to reduce
* @param reducer - Asynchronous function that processes each block and updates the accumulator
* @param initialValue - The initial value for the accumulator
* @returns Promise that resolves to the final accumulated value
*/
export declare function nonRecursiveReduceBlocksInNonLocalizedFieldValueAsync<R>(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, reducer: (accumulator: R, item: BlockInRequest, path: TreePath) => Promise<R>, initialValue: R): Promise<R>;
/**
* Check if any block in the non-localized field value matches the predicate function.
* Returns true as soon as the first matching block is found (short-circuit evaluation).
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to test
* @param predicate - Asynchronous function that tests each block. Should return true for matching blocks
* @returns Promise that resolves to true if any block matches, false otherwise
*/
export declare function nonRecursiveSomeBlocksInNonLocalizedFieldValueAsync(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>): Promise<boolean>;
/**
* Check if every block in the non-localized field value matches the predicate function.
* Returns false as soon as the first non-matching block is found (short-circuit evaluation).
*
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to test
* @param predicate - Asynchronous function that tests each block. Should return true for valid blocks
* @returns Promise that resolves to true if all blocks match, false otherwise
*/
export declare function nonRecursiveEveryBlockInNonLocalizedFieldValueAsync(fieldType: ApiTypes.Field['field_type'], nonLocalizedFieldValue: unknown, predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>): Promise<boolean>;