@datocms/cma-client 5.1.11 → 5.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +313 -178
- package/dist/cjs/fieldTypes/file.js +6 -6
- package/dist/cjs/fieldTypes/gallery.js +7 -7
- package/dist/cjs/fieldTypes/rich_text.js +14 -12
- package/dist/cjs/fieldTypes/rich_text.js.map +1 -1
- package/dist/cjs/fieldTypes/schema.js +3 -0
- package/dist/cjs/fieldTypes/schema.js.map +1 -0
- package/dist/cjs/fieldTypes/single_block.js +13 -12
- package/dist/cjs/fieldTypes/single_block.js.map +1 -1
- package/dist/cjs/fieldTypes/structured_text.js +13 -12
- package/dist/cjs/fieldTypes/structured_text.js.map +1 -1
- package/dist/cjs/generated/Client.js +1 -1
- package/dist/cjs/generated/resources/Item.js.map +1 -1
- package/dist/cjs/generated/resources/ScheduledPublication.js.map +1 -1
- package/dist/cjs/generated/resources/ScheduledUnpublishing.js.map +1 -1
- package/dist/cjs/generated/resources/Upload.js.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utilities/buildBlockRecord.js +1 -46
- package/dist/cjs/utilities/buildBlockRecord.js.map +1 -1
- package/dist/cjs/utilities/duplicateBlockRecord.js +50 -0
- package/dist/cjs/utilities/duplicateBlockRecord.js.map +1 -0
- package/dist/cjs/utilities/inspectItem.js +50 -20
- package/dist/cjs/utilities/inspectItem.js.map +1 -1
- package/dist/cjs/utilities/nonRecursiveBlocks.js +1 -170
- package/dist/cjs/utilities/nonRecursiveBlocks.js.map +1 -1
- package/dist/cjs/utilities/recursiveBlocks.js +52 -112
- package/dist/cjs/utilities/recursiveBlocks.js.map +1 -1
- package/dist/esm/fieldTypes/file.d.ts +3 -3
- package/dist/esm/fieldTypes/file.js +3 -3
- package/dist/esm/fieldTypes/gallery.d.ts +5 -5
- package/dist/esm/fieldTypes/gallery.js +5 -5
- package/dist/esm/fieldTypes/rich_text.d.ts +8 -9
- package/dist/esm/fieldTypes/rich_text.js +9 -7
- package/dist/esm/fieldTypes/rich_text.js.map +1 -1
- package/dist/esm/fieldTypes/schema.d.ts +59 -0
- package/dist/esm/fieldTypes/schema.js +2 -0
- package/dist/esm/fieldTypes/schema.js.map +1 -0
- package/dist/esm/fieldTypes/single_block.d.ts +30 -15
- package/dist/esm/fieldTypes/single_block.js +8 -7
- package/dist/esm/fieldTypes/single_block.js.map +1 -1
- package/dist/esm/fieldTypes/structured_text.d.ts +14 -15
- package/dist/esm/fieldTypes/structured_text.js +8 -7
- package/dist/esm/fieldTypes/structured_text.js.map +1 -1
- package/dist/esm/generated/ApiTypes.d.ts +94 -980
- package/dist/esm/generated/Client.js +1 -1
- package/dist/esm/generated/RawApiTypes.d.ts +160 -999
- package/dist/esm/generated/resources/Field.d.ts +300 -300
- package/dist/esm/generated/resources/Item.d.ts +34 -34
- package/dist/esm/generated/resources/Item.js.map +1 -1
- package/dist/esm/generated/resources/ItemVersion.d.ts +3 -1
- package/dist/esm/generated/resources/ScheduledPublication.d.ts +3 -3
- package/dist/esm/generated/resources/ScheduledPublication.js.map +1 -1
- package/dist/esm/generated/resources/ScheduledUnpublishing.d.ts +3 -3
- package/dist/esm/generated/resources/ScheduledUnpublishing.js.map +1 -1
- package/dist/esm/generated/resources/Upload.d.ts +6 -6
- package/dist/esm/generated/resources/Upload.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utilities/buildBlockRecord.d.ts +11 -5
- package/dist/esm/utilities/buildBlockRecord.js +0 -44
- package/dist/esm/utilities/buildBlockRecord.js.map +1 -1
- package/dist/esm/utilities/duplicateBlockRecord.d.ts +6 -0
- package/dist/esm/utilities/duplicateBlockRecord.js +46 -0
- package/dist/esm/utilities/duplicateBlockRecord.js.map +1 -0
- package/dist/esm/utilities/inspectItem.d.ts +3 -3
- package/dist/esm/utilities/inspectItem.js +28 -21
- package/dist/esm/utilities/inspectItem.js.map +1 -1
- package/dist/esm/utilities/itemDefinition.d.ts +25 -27
- package/dist/esm/utilities/nonRecursiveBlocks.d.ts +10 -75
- package/dist/esm/utilities/nonRecursiveBlocks.js +1 -164
- package/dist/esm/utilities/nonRecursiveBlocks.js.map +1 -1
- package/dist/esm/utilities/recursiveBlocks.d.ts +53 -23
- package/dist/esm/utilities/recursiveBlocks.js +52 -112
- package/dist/esm/utilities/recursiveBlocks.js.map +1 -1
- package/dist/types/fieldTypes/file.d.ts +3 -3
- package/dist/types/fieldTypes/gallery.d.ts +5 -5
- package/dist/types/fieldTypes/rich_text.d.ts +8 -9
- package/dist/types/fieldTypes/schema.d.ts +59 -0
- package/dist/types/fieldTypes/single_block.d.ts +30 -15
- package/dist/types/fieldTypes/structured_text.d.ts +14 -15
- package/dist/types/generated/ApiTypes.d.ts +94 -980
- package/dist/types/generated/RawApiTypes.d.ts +160 -999
- package/dist/types/generated/resources/Field.d.ts +300 -300
- package/dist/types/generated/resources/Item.d.ts +34 -34
- package/dist/types/generated/resources/ItemVersion.d.ts +3 -1
- package/dist/types/generated/resources/ScheduledPublication.d.ts +3 -3
- package/dist/types/generated/resources/ScheduledUnpublishing.d.ts +3 -3
- package/dist/types/generated/resources/Upload.d.ts +6 -6
- package/dist/types/index.d.ts +1 -0
- package/dist/types/utilities/buildBlockRecord.d.ts +11 -5
- package/dist/types/utilities/duplicateBlockRecord.d.ts +6 -0
- package/dist/types/utilities/inspectItem.d.ts +3 -3
- package/dist/types/utilities/itemDefinition.d.ts +25 -27
- package/dist/types/utilities/nonRecursiveBlocks.d.ts +10 -75
- package/dist/types/utilities/recursiveBlocks.d.ts +53 -23
- package/package.json +4 -4
- package/src/fieldTypes/file.ts +6 -6
- package/src/fieldTypes/gallery.ts +10 -10
- package/src/fieldTypes/rich_text.ts +26 -24
- package/src/fieldTypes/schema.ts +657 -0
- package/src/fieldTypes/single_block.ts +61 -38
- package/src/fieldTypes/structured_text.ts +57 -51
- package/src/generated/ApiTypes.ts +221 -1880
- package/src/generated/Client.ts +1 -1
- package/src/generated/RawApiTypes.ts +276 -2113
- package/src/generated/resources/Item.ts +93 -187
- package/src/generated/resources/ScheduledPublication.ts +4 -15
- package/src/generated/resources/ScheduledUnpublishing.ts +4 -15
- package/src/generated/resources/Upload.ts +9 -32
- package/src/index.ts +1 -0
- package/src/utilities/buildBlockRecord.ts +16 -56
- package/src/utilities/duplicateBlockRecord.ts +53 -0
- package/src/utilities/inspectItem.ts +64 -52
- package/src/utilities/itemDefinition.ts +109 -98
- package/src/utilities/nonRecursiveBlocks.ts +25 -279
- package/src/utilities/recursiveBlocks.ts +337 -72
|
@@ -24,91 +24,42 @@
|
|
|
24
24
|
|
|
25
25
|
import {
|
|
26
26
|
type TreePath,
|
|
27
|
-
collectNodes,
|
|
28
27
|
collectNodesAsync,
|
|
29
|
-
filterNodes,
|
|
30
28
|
filterNodesAsync,
|
|
31
29
|
mapNodes,
|
|
32
30
|
mapNodesAsync,
|
|
33
31
|
} from 'datocms-structured-text-utils';
|
|
34
32
|
import type {
|
|
35
|
-
|
|
33
|
+
BlockInRequest,
|
|
36
34
|
RichTextFieldValue,
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
RichTextFieldValueInNestedResponse,
|
|
36
|
+
RichTextFieldValueInRequest,
|
|
39
37
|
SingleBlockFieldValue,
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
SingleBlockFieldValueInNestedResponse,
|
|
39
|
+
SingleBlockFieldValueInRequest,
|
|
42
40
|
StructuredTextFieldValue,
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
StructuredTextFieldValueInNestedResponse,
|
|
42
|
+
StructuredTextFieldValueInRequest,
|
|
45
43
|
} from '../fieldTypes';
|
|
46
44
|
import type * as ApiTypes from '../generated/ApiTypes';
|
|
47
|
-
import { isLocalizedFieldValue } from './normalizedFieldValues';
|
|
48
45
|
|
|
49
46
|
type PossibleRichTextValue =
|
|
50
47
|
| RichTextFieldValue
|
|
51
|
-
|
|
|
52
|
-
|
|
|
48
|
+
| RichTextFieldValueInRequest
|
|
49
|
+
| RichTextFieldValueInNestedResponse;
|
|
53
50
|
type PossibleSingleBlockValue =
|
|
54
51
|
| SingleBlockFieldValue
|
|
55
|
-
|
|
|
56
|
-
|
|
|
52
|
+
| SingleBlockFieldValueInRequest
|
|
53
|
+
| SingleBlockFieldValueInNestedResponse;
|
|
57
54
|
type PossibleStructuredTextValue =
|
|
58
55
|
| StructuredTextFieldValue
|
|
59
|
-
|
|
|
60
|
-
|
|
|
61
|
-
|
|
62
|
-
function* iterateBlocks(
|
|
63
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
64
|
-
nonLocalizedFieldValue: unknown,
|
|
65
|
-
): Generator<{ item: BlockItemInARequest; path: TreePath }> {
|
|
66
|
-
if (isLocalizedFieldValue(nonLocalizedFieldValue)) {
|
|
67
|
-
throw new Error('A non-localized field value is required!');
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (fieldType === 'rich_text') {
|
|
71
|
-
const richTextValue = nonLocalizedFieldValue as PossibleRichTextValue;
|
|
72
|
-
if (richTextValue) {
|
|
73
|
-
for (let index = 0; index < richTextValue.length; index++) {
|
|
74
|
-
const item = richTextValue[index]!;
|
|
75
|
-
yield { item, path: [index] };
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (fieldType === 'single_block') {
|
|
82
|
-
const singleBlockValue = nonLocalizedFieldValue as PossibleSingleBlockValue;
|
|
83
|
-
if (singleBlockValue) {
|
|
84
|
-
yield { item: singleBlockValue, path: [] };
|
|
85
|
-
}
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (fieldType === 'structured_text') {
|
|
90
|
-
const structuredTextValue =
|
|
91
|
-
nonLocalizedFieldValue as PossibleStructuredTextValue;
|
|
92
|
-
if (structuredTextValue) {
|
|
93
|
-
const foundNodes = collectNodes(
|
|
94
|
-
structuredTextValue.document,
|
|
95
|
-
(node) => node.type === 'block' || node.type === 'inlineBlock',
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
for (const { node, path } of foundNodes) {
|
|
99
|
-
if (node.type === 'block' || node.type === 'inlineBlock') {
|
|
100
|
-
yield { item: node.item, path };
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
56
|
+
| StructuredTextFieldValueInRequest
|
|
57
|
+
| StructuredTextFieldValueInNestedResponse;
|
|
107
58
|
|
|
108
59
|
async function* iterateBlocksAsync(
|
|
109
60
|
fieldType: ApiTypes.Field['field_type'],
|
|
110
61
|
nonLocalizedFieldValue: unknown,
|
|
111
|
-
): AsyncGenerator<{ item:
|
|
62
|
+
): AsyncGenerator<{ item: BlockInRequest; path: TreePath }> {
|
|
112
63
|
if (fieldType === 'rich_text') {
|
|
113
64
|
const richTextValue = nonLocalizedFieldValue as PossibleRichTextValue;
|
|
114
65
|
if (richTextValue) {
|
|
@@ -147,27 +98,6 @@ async function* iterateBlocksAsync(
|
|
|
147
98
|
}
|
|
148
99
|
}
|
|
149
100
|
|
|
150
|
-
/**
|
|
151
|
-
* Visit every block in a field value, calling the visitor function for each block found.
|
|
152
|
-
* Supports rich text, single block, and structured text field types.
|
|
153
|
-
*
|
|
154
|
-
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
|
|
155
|
-
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to visit
|
|
156
|
-
* @param visitor - Synchronous function called for each block. Receives the block item and its path
|
|
157
|
-
*/
|
|
158
|
-
export function nonRecursiveVisitBlocksInNonLocalizedFieldValue(
|
|
159
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
160
|
-
nonLocalizedFieldValue: unknown,
|
|
161
|
-
visitor: (item: BlockItemInARequest, path: TreePath) => void,
|
|
162
|
-
): void {
|
|
163
|
-
for (const { item, path } of iterateBlocks(
|
|
164
|
-
fieldType,
|
|
165
|
-
nonLocalizedFieldValue,
|
|
166
|
-
)) {
|
|
167
|
-
visitor(item, path);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
101
|
/**
|
|
172
102
|
* Visit every block in a field value, calling the visitor function for each block found.
|
|
173
103
|
* Supports rich text, single block, and structured text field types.
|
|
@@ -180,7 +110,7 @@ export function nonRecursiveVisitBlocksInNonLocalizedFieldValue(
|
|
|
180
110
|
export async function nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(
|
|
181
111
|
fieldType: ApiTypes.Field['field_type'],
|
|
182
112
|
nonLocalizedFieldValue: unknown,
|
|
183
|
-
visitor: (item:
|
|
113
|
+
visitor: (item: BlockInRequest, path: TreePath) => Promise<void>,
|
|
184
114
|
): Promise<void> {
|
|
185
115
|
for await (const { item, path } of iterateBlocksAsync(
|
|
186
116
|
fieldType,
|
|
@@ -203,7 +133,7 @@ export async function nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(
|
|
|
203
133
|
export function nonRecursiveMapBlocksInNonLocalizedFieldValue(
|
|
204
134
|
fieldType: ApiTypes.Field['field_type'],
|
|
205
135
|
nonLocalizedFieldValue: unknown,
|
|
206
|
-
mapper: (item:
|
|
136
|
+
mapper: (item: BlockInRequest, path: TreePath) => BlockInRequest,
|
|
207
137
|
): unknown {
|
|
208
138
|
if (fieldType === 'rich_text') {
|
|
209
139
|
const richTextValue = nonLocalizedFieldValue as PossibleRichTextValue;
|
|
@@ -256,10 +186,7 @@ export function nonRecursiveMapBlocksInNonLocalizedFieldValue(
|
|
|
256
186
|
export async function nonRecursiveMapBlocksInNonLocalizedFieldValueAsync(
|
|
257
187
|
fieldType: ApiTypes.Field['field_type'],
|
|
258
188
|
nonLocalizedFieldValue: unknown,
|
|
259
|
-
mapper: (
|
|
260
|
-
item: BlockItemInARequest,
|
|
261
|
-
path: TreePath,
|
|
262
|
-
) => Promise<BlockItemInARequest>,
|
|
189
|
+
mapper: (item: BlockInRequest, path: TreePath) => Promise<BlockInRequest>,
|
|
263
190
|
): Promise<unknown> {
|
|
264
191
|
if (fieldType === 'rich_text') {
|
|
265
192
|
const richTextValue = nonLocalizedFieldValue as PossibleRichTextValue;
|
|
@@ -301,34 +228,6 @@ export async function nonRecursiveMapBlocksInNonLocalizedFieldValueAsync(
|
|
|
301
228
|
return nonLocalizedFieldValue;
|
|
302
229
|
}
|
|
303
230
|
|
|
304
|
-
/**
|
|
305
|
-
* Find all blocks that match the predicate function.
|
|
306
|
-
* Searches through all blocks in the non-localized field value and returns all matches.
|
|
307
|
-
*
|
|
308
|
-
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
|
|
309
|
-
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to search
|
|
310
|
-
* @param predicate - Synchronous function that tests each block. Should return true for matching blocks
|
|
311
|
-
* @returns Array of objects, each containing a matching block and its path
|
|
312
|
-
*/
|
|
313
|
-
export function nonRecursiveFindAllBlocksInNonLocalizedFieldValue(
|
|
314
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
315
|
-
nonLocalizedFieldValue: unknown,
|
|
316
|
-
predicate: (item: BlockItemInARequest, path: TreePath) => boolean,
|
|
317
|
-
): Array<{ item: BlockItemInARequest; path: TreePath }> {
|
|
318
|
-
const results: Array<{ item: BlockItemInARequest; path: TreePath }> = [];
|
|
319
|
-
|
|
320
|
-
for (const { item, path } of iterateBlocks(
|
|
321
|
-
fieldType,
|
|
322
|
-
nonLocalizedFieldValue,
|
|
323
|
-
)) {
|
|
324
|
-
if (predicate(item, path)) {
|
|
325
|
-
results.push({ item, path });
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return results;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
231
|
/**
|
|
333
232
|
* Find all blocks that match the predicate function.
|
|
334
233
|
* Searches through all blocks in the non-localized field value and returns all matches.
|
|
@@ -341,9 +240,9 @@ export function nonRecursiveFindAllBlocksInNonLocalizedFieldValue(
|
|
|
341
240
|
export async function nonRecursiveFindAllBlocksInNonLocalizedFieldValueAsync(
|
|
342
241
|
fieldType: ApiTypes.Field['field_type'],
|
|
343
242
|
nonLocalizedFieldValue: unknown,
|
|
344
|
-
predicate: (item:
|
|
345
|
-
): Promise<Array<{ item:
|
|
346
|
-
const results: Array<{ item:
|
|
243
|
+
predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>,
|
|
244
|
+
): Promise<Array<{ item: BlockInRequest; path: TreePath }>> {
|
|
245
|
+
const results: Array<{ item: BlockInRequest; path: TreePath }> = [];
|
|
347
246
|
|
|
348
247
|
for await (const { item, path } of iterateBlocksAsync(
|
|
349
248
|
fieldType,
|
|
@@ -357,77 +256,6 @@ export async function nonRecursiveFindAllBlocksInNonLocalizedFieldValueAsync(
|
|
|
357
256
|
return results;
|
|
358
257
|
}
|
|
359
258
|
|
|
360
|
-
/**
|
|
361
|
-
* Filter blocks in a field value, removing those that don't match the predicate.
|
|
362
|
-
* Creates a new field value containing only blocks that pass the predicate test.
|
|
363
|
-
* Preserves the original field value structure and hierarchy.
|
|
364
|
-
*
|
|
365
|
-
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
|
|
366
|
-
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to filter
|
|
367
|
-
* @param predicate - Synchronous function that tests each block. Blocks returning false are removed
|
|
368
|
-
* @returns The new field value with filtered blocks
|
|
369
|
-
*/
|
|
370
|
-
export function nonRecursiveFilterBlocksInNonLocalizedFieldValue(
|
|
371
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
372
|
-
nonLocalizedFieldValue: unknown,
|
|
373
|
-
predicate: (item: BlockItemInARequest, path: TreePath) => boolean,
|
|
374
|
-
): unknown {
|
|
375
|
-
if (fieldType === 'rich_text') {
|
|
376
|
-
const filteredItems: BlockItemInARequest[] = [];
|
|
377
|
-
|
|
378
|
-
for (const { item, path } of iterateBlocks(
|
|
379
|
-
fieldType,
|
|
380
|
-
nonLocalizedFieldValue,
|
|
381
|
-
)) {
|
|
382
|
-
if (predicate(item, path)) {
|
|
383
|
-
filteredItems.push(item);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
return filteredItems;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
if (fieldType === 'single_block') {
|
|
391
|
-
for (const { item, path } of iterateBlocks(
|
|
392
|
-
fieldType,
|
|
393
|
-
nonLocalizedFieldValue,
|
|
394
|
-
)) {
|
|
395
|
-
if (predicate(item, path)) {
|
|
396
|
-
return item;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return null;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
if (fieldType === 'structured_text') {
|
|
403
|
-
const structuredTextValue =
|
|
404
|
-
nonLocalizedFieldValue as PossibleStructuredTextValue;
|
|
405
|
-
|
|
406
|
-
if (!structuredTextValue) {
|
|
407
|
-
return null;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
const filteredDocument = filterNodes(
|
|
411
|
-
structuredTextValue.document,
|
|
412
|
-
(node, _parent, path) => {
|
|
413
|
-
if (node.type === 'block' || node.type === 'inlineBlock') {
|
|
414
|
-
return predicate(node.item, path);
|
|
415
|
-
}
|
|
416
|
-
return true;
|
|
417
|
-
},
|
|
418
|
-
);
|
|
419
|
-
|
|
420
|
-
return filteredDocument
|
|
421
|
-
? {
|
|
422
|
-
schema: 'dast',
|
|
423
|
-
document: filteredDocument,
|
|
424
|
-
}
|
|
425
|
-
: null;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
return nonLocalizedFieldValue;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
259
|
/**
|
|
432
260
|
* Filter blocks in a field value, removing those that don't match the predicate.
|
|
433
261
|
* Creates a new field value containing only blocks that pass the predicate test.
|
|
@@ -441,10 +269,10 @@ export function nonRecursiveFilterBlocksInNonLocalizedFieldValue(
|
|
|
441
269
|
export async function nonRecursiveFilterBlocksInNonLocalizedFieldValueAsync(
|
|
442
270
|
fieldType: ApiTypes.Field['field_type'],
|
|
443
271
|
nonLocalizedFieldValue: unknown,
|
|
444
|
-
predicate: (item:
|
|
272
|
+
predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>,
|
|
445
273
|
): Promise<unknown> {
|
|
446
274
|
if (fieldType === 'rich_text') {
|
|
447
|
-
const filteredItems:
|
|
275
|
+
const filteredItems: BlockInRequest[] = [];
|
|
448
276
|
|
|
449
277
|
for await (const { item, path } of iterateBlocksAsync(
|
|
450
278
|
fieldType,
|
|
@@ -499,35 +327,6 @@ export async function nonRecursiveFilterBlocksInNonLocalizedFieldValueAsync(
|
|
|
499
327
|
return nonLocalizedFieldValue;
|
|
500
328
|
}
|
|
501
329
|
|
|
502
|
-
/**
|
|
503
|
-
* Reduce all blocks in a field value to a single value by applying a reducer function.
|
|
504
|
-
* Processes each block in the non-localized field value and accumulates the results into a single value.
|
|
505
|
-
*
|
|
506
|
-
* @template R - The type of the accumulated result
|
|
507
|
-
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
|
|
508
|
-
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to reduce
|
|
509
|
-
* @param reducer - Synchronous function that processes each block and updates the accumulator
|
|
510
|
-
* @param initialValue - The initial value for the accumulator
|
|
511
|
-
* @returns The final accumulated value
|
|
512
|
-
*/
|
|
513
|
-
export function nonRecursiveReduceBlocksInNonLocalizedFieldValue<R>(
|
|
514
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
515
|
-
nonLocalizedFieldValue: unknown,
|
|
516
|
-
reducer: (accumulator: R, item: BlockItemInARequest, path: TreePath) => R,
|
|
517
|
-
initialValue: R,
|
|
518
|
-
): R {
|
|
519
|
-
let accumulator = initialValue;
|
|
520
|
-
|
|
521
|
-
for (const { item, path } of iterateBlocks(
|
|
522
|
-
fieldType,
|
|
523
|
-
nonLocalizedFieldValue,
|
|
524
|
-
)) {
|
|
525
|
-
accumulator = reducer(accumulator, item, path);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return accumulator;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
330
|
/**
|
|
532
331
|
* Reduce all blocks in a field value to a single value by applying a reducer function.
|
|
533
332
|
* Processes each block in the non-localized field value and accumulates the results into a single value.
|
|
@@ -542,11 +341,7 @@ export function nonRecursiveReduceBlocksInNonLocalizedFieldValue<R>(
|
|
|
542
341
|
export async function nonRecursiveReduceBlocksInNonLocalizedFieldValueAsync<R>(
|
|
543
342
|
fieldType: ApiTypes.Field['field_type'],
|
|
544
343
|
nonLocalizedFieldValue: unknown,
|
|
545
|
-
reducer: (
|
|
546
|
-
accumulator: R,
|
|
547
|
-
item: BlockItemInARequest,
|
|
548
|
-
path: TreePath,
|
|
549
|
-
) => Promise<R>,
|
|
344
|
+
reducer: (accumulator: R, item: BlockInRequest, path: TreePath) => Promise<R>,
|
|
550
345
|
initialValue: R,
|
|
551
346
|
): Promise<R> {
|
|
552
347
|
let accumulator = initialValue;
|
|
@@ -561,32 +356,6 @@ export async function nonRecursiveReduceBlocksInNonLocalizedFieldValueAsync<R>(
|
|
|
561
356
|
return accumulator;
|
|
562
357
|
}
|
|
563
358
|
|
|
564
|
-
/**
|
|
565
|
-
* Check if any block in the non-localized field value matches the predicate function.
|
|
566
|
-
* Returns true as soon as the first matching block is found (short-circuit evaluation).
|
|
567
|
-
*
|
|
568
|
-
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
|
|
569
|
-
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to test
|
|
570
|
-
* @param predicate - Synchronous function that tests each block. Should return true for matching blocks
|
|
571
|
-
* @returns True if any block matches, false otherwise
|
|
572
|
-
*/
|
|
573
|
-
export function nonRecursiveSomeBlocksInNonLocalizedFieldValue(
|
|
574
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
575
|
-
nonLocalizedFieldValue: unknown,
|
|
576
|
-
predicate: (item: BlockItemInARequest, path: TreePath) => boolean,
|
|
577
|
-
): boolean {
|
|
578
|
-
for (const { item, path } of iterateBlocks(
|
|
579
|
-
fieldType,
|
|
580
|
-
nonLocalizedFieldValue,
|
|
581
|
-
)) {
|
|
582
|
-
if (predicate(item, path)) {
|
|
583
|
-
return true;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
return false;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
359
|
/**
|
|
591
360
|
* Check if any block in the non-localized field value matches the predicate function.
|
|
592
361
|
* Returns true as soon as the first matching block is found (short-circuit evaluation).
|
|
@@ -599,7 +368,7 @@ export function nonRecursiveSomeBlocksInNonLocalizedFieldValue(
|
|
|
599
368
|
export async function nonRecursiveSomeBlocksInNonLocalizedFieldValueAsync(
|
|
600
369
|
fieldType: ApiTypes.Field['field_type'],
|
|
601
370
|
nonLocalizedFieldValue: unknown,
|
|
602
|
-
predicate: (item:
|
|
371
|
+
predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>,
|
|
603
372
|
): Promise<boolean> {
|
|
604
373
|
for await (const { item, path } of iterateBlocksAsync(
|
|
605
374
|
fieldType,
|
|
@@ -613,29 +382,6 @@ export async function nonRecursiveSomeBlocksInNonLocalizedFieldValueAsync(
|
|
|
613
382
|
return false;
|
|
614
383
|
}
|
|
615
384
|
|
|
616
|
-
/**
|
|
617
|
-
* Check if every block in the non-localized field value matches the predicate function.
|
|
618
|
-
* Returns false as soon as the first non-matching block is found (short-circuit evaluation).
|
|
619
|
-
*
|
|
620
|
-
* @param fieldType - The type of DatoCMS field definition that determines how the value is processed
|
|
621
|
-
* @param nonLocalizedFieldValue - The non-localized field value containing blocks to test
|
|
622
|
-
* @param predicate - Synchronous function that tests each block. Should return true for valid blocks
|
|
623
|
-
* @returns True if all blocks match, false otherwise
|
|
624
|
-
*/
|
|
625
|
-
export function nonRecursiveEveryBlockInNonLocalizedFieldValue(
|
|
626
|
-
fieldType: ApiTypes.Field['field_type'],
|
|
627
|
-
nonLocalizedFieldValue: unknown,
|
|
628
|
-
predicate: (item: BlockItemInARequest, path: TreePath) => boolean,
|
|
629
|
-
): boolean {
|
|
630
|
-
return !nonRecursiveSomeBlocksInNonLocalizedFieldValue(
|
|
631
|
-
fieldType,
|
|
632
|
-
nonLocalizedFieldValue,
|
|
633
|
-
(item, path) => {
|
|
634
|
-
return !predicate(item, path);
|
|
635
|
-
},
|
|
636
|
-
);
|
|
637
|
-
}
|
|
638
|
-
|
|
639
385
|
/**
|
|
640
386
|
* Check if every block in the non-localized field value matches the predicate function.
|
|
641
387
|
* Returns false as soon as the first non-matching block is found (short-circuit evaluation).
|
|
@@ -648,7 +394,7 @@ export function nonRecursiveEveryBlockInNonLocalizedFieldValue(
|
|
|
648
394
|
export async function nonRecursiveEveryBlockInNonLocalizedFieldValueAsync(
|
|
649
395
|
fieldType: ApiTypes.Field['field_type'],
|
|
650
396
|
nonLocalizedFieldValue: unknown,
|
|
651
|
-
predicate: (item:
|
|
397
|
+
predicate: (item: BlockInRequest, path: TreePath) => Promise<boolean>,
|
|
652
398
|
): Promise<boolean> {
|
|
653
399
|
return !(await nonRecursiveSomeBlocksInNonLocalizedFieldValueAsync(
|
|
654
400
|
fieldType,
|