@datocms/cma-client 5.1.0 → 5.1.1

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.
Files changed (46) hide show
  1. package/dist/cjs/fieldTypes/rich_text.js +3 -3
  2. package/dist/cjs/fieldTypes/rich_text.js.map +1 -1
  3. package/dist/cjs/fieldTypes/single_block.js +3 -3
  4. package/dist/cjs/fieldTypes/single_block.js.map +1 -1
  5. package/dist/cjs/fieldTypes/structured_text.js +3 -3
  6. package/dist/cjs/fieldTypes/structured_text.js.map +1 -1
  7. package/dist/cjs/generated/Client.js +1 -1
  8. package/dist/cjs/utilities/fieldValueLocalization.js +90 -55
  9. package/dist/cjs/utilities/fieldValueLocalization.js.map +1 -1
  10. package/dist/cjs/utilities/structuredText.js +11 -11
  11. package/dist/cjs/utilities/structuredText.js.map +1 -1
  12. package/dist/esm/fieldTypes/rich_text.d.ts +2 -2
  13. package/dist/esm/fieldTypes/rich_text.js +1 -1
  14. package/dist/esm/fieldTypes/rich_text.js.map +1 -1
  15. package/dist/esm/fieldTypes/single_block.d.ts +2 -2
  16. package/dist/esm/fieldTypes/single_block.js +1 -1
  17. package/dist/esm/fieldTypes/single_block.js.map +1 -1
  18. package/dist/esm/fieldTypes/structured_text.d.ts +8 -8
  19. package/dist/esm/fieldTypes/structured_text.js +1 -1
  20. package/dist/esm/fieldTypes/structured_text.js.map +1 -1
  21. package/dist/esm/generated/Client.js +1 -1
  22. package/dist/esm/generated/SchemaTypes.d.ts +11 -6
  23. package/dist/esm/generated/SimpleSchemaTypes.d.ts +11 -6
  24. package/dist/esm/utilities/fieldValueLocalization.d.ts +51 -9
  25. package/dist/esm/utilities/fieldValueLocalization.js +87 -54
  26. package/dist/esm/utilities/fieldValueLocalization.js.map +1 -1
  27. package/dist/esm/utilities/structuredText.d.ts +2 -2
  28. package/dist/esm/utilities/structuredText.js +8 -8
  29. package/dist/esm/utilities/structuredText.js.map +1 -1
  30. package/dist/types/fieldTypes/rich_text.d.ts +2 -2
  31. package/dist/types/fieldTypes/single_block.d.ts +2 -2
  32. package/dist/types/fieldTypes/structured_text.d.ts +8 -8
  33. package/dist/types/generated/SchemaTypes.d.ts +11 -6
  34. package/dist/types/generated/SimpleSchemaTypes.d.ts +11 -6
  35. package/dist/types/utilities/fieldValueLocalization.d.ts +51 -9
  36. package/dist/types/utilities/structuredText.d.ts +2 -2
  37. package/package.json +4 -4
  38. package/src/fieldTypes/rich_text.ts +3 -3
  39. package/src/fieldTypes/single_block.ts +3 -3
  40. package/src/fieldTypes/structured_text.ts +13 -13
  41. package/src/generated/Client.ts +1 -1
  42. package/src/generated/SchemaTypes.ts +42 -7
  43. package/src/generated/SimpleSchemaTypes.ts +42 -7
  44. package/src/utilities/blocks.ts +6 -6
  45. package/src/utilities/fieldValueLocalization.ts +122 -93
  46. package/src/utilities/structuredText.ts +8 -8
@@ -67,27 +67,27 @@ export type DocumentAsRequest = {
67
67
  /**
68
68
  * Variant of 'block' structured text node for ?nested=true API responses
69
69
  */
70
- export type BlockWithResolvedBlocks = Omit<Block, 'item'> & {
70
+ export type BlockWithNestedBlocks = Omit<Block, 'item'> & {
71
71
  item: SchemaTypes.Item;
72
72
  };
73
73
  /**
74
74
  * Variant of 'inlineBlock' structured text node for ?nested=true API responses
75
75
  */
76
- export type InlineBlockWithResolvedBlocks = Omit<InlineBlock, 'item'> & {
76
+ export type InlineBlockWithNestedBlocks = Omit<InlineBlock, 'item'> & {
77
77
  item: SchemaTypes.Item;
78
78
  };
79
79
  /**
80
80
  * Generic type to transform a node that might be containing a 'block' or 'inlineBlock' as a (deeply nested) children to it's variant for ?nested=true API responses
81
81
  */
82
- export type NodeWithResolvedBlocks<T> = WithMappedChildren<T, DeepMapVariants<T extends {
82
+ export type NodeWithNestedBlocks<T> = WithMappedChildren<T, DeepMapVariants<T extends {
83
83
  children: infer C;
84
- } ? C : never, BlockWithResolvedBlocks, InlineBlockWithResolvedBlocks>>;
84
+ } ? C : never, BlockWithNestedBlocks, InlineBlockWithNestedBlocks>>;
85
85
  /**
86
86
  * Variant of Structured Text document for ?nested=true API responses
87
87
  */
88
- export type DocumentWithResolvedBlocks = {
88
+ export type DocumentWithNestedBlocks = {
89
89
  schema: 'dast';
90
- document: NodeWithResolvedBlocks<Root>;
90
+ document: NodeWithNestedBlocks<Root>;
91
91
  };
92
92
  /**
93
93
  * =============================================================================
@@ -100,7 +100,7 @@ export type DocumentWithResolvedBlocks = {
100
100
  */
101
101
  export type StructuredTextFieldValue = Document | null;
102
102
  export type StructuredTextFieldValueAsRequest = DocumentAsRequest | null;
103
- export type StructuredTextFieldValueWithResolvedBlocks = DocumentWithResolvedBlocks | null;
103
+ export type StructuredTextFieldValueWithNestedBlocks = DocumentWithNestedBlocks | null;
104
104
  /**
105
105
  * Type guard for basic structured text field values (blocks as string IDs only).
106
106
  * Checks for the expected structure and ensures all block/inlineBlock nodes have string IDs.
@@ -115,7 +115,7 @@ export declare function isStructuredTextFieldValueAsRequest(value: unknown): val
115
115
  * Type guard for structured text field values with nested blocks (?nested=true format).
116
116
  * Ensures all block/inlineBlock nodes have full SchemaTypes.Item objects.
117
117
  */
118
- export declare function isStructuredTextFieldValueWithResolvedBlocks(value: unknown): value is StructuredTextFieldValueWithResolvedBlocks;
118
+ export declare function isStructuredTextFieldValueWithNestedBlocks(value: unknown): value is StructuredTextFieldValueWithNestedBlocks;
119
119
  /**
120
120
  * =============================================================================
121
121
  * SHARED TRANSFORMATION UTILITIES
@@ -30,16 +30,21 @@ type FieldAttributesForFieldType<SourceType, FieldType extends RawFieldAttribute
30
30
  type GenericFieldAttributes<SourceType> = FieldAttributesForFieldType<SourceType, 'boolean', BooleanFieldValue, BooleanFieldValidators, BooleanFieldAppearance> | FieldAttributesForFieldType<SourceType, 'color', ColorFieldValue, ColorFieldValidators, ColorFieldAppearance> | FieldAttributesForFieldType<SourceType, 'date', DateFieldValue, DateFieldValidators, DateFieldAppearance> | FieldAttributesForFieldType<SourceType, 'date_time', DateTimeFieldValue, DateTimeFieldValidators, DateTimeFieldAppearance> | FieldAttributesForFieldType<SourceType, 'file', null, FileFieldValidators, FileFieldAppearance> | FieldAttributesForFieldType<SourceType, 'float', FloatFieldValue, FloatFieldValidators, FloatFieldAppearance> | FieldAttributesForFieldType<SourceType, 'gallery', null, GalleryFieldValidators, GalleryFieldAppearance> | FieldAttributesForFieldType<SourceType, 'integer', IntegerFieldValue, IntegerFieldValidators, IntegerFieldAppearance> | FieldAttributesForFieldType<SourceType, 'json', JsonFieldValue, JsonFieldValidators, JsonFieldAppearance> | FieldAttributesForFieldType<SourceType, 'lat_lon', LocationFieldValue, LocationFieldValidators, LocationFieldAppearance> | FieldAttributesForFieldType<SourceType, 'link', null, LinkFieldValidators, LinkFieldAppearance> | FieldAttributesForFieldType<SourceType, 'links', null, LinksFieldValidators, LinksFieldAppearance> | FieldAttributesForFieldType<SourceType, 'rich_text', null, RichTextFieldValidators, RichTextFieldAppearance> | FieldAttributesForFieldType<SourceType, 'seo', null, SeoFieldValidators, SeoFieldAppearance> | FieldAttributesForFieldType<SourceType, 'single_block', null, SingleBlockFieldValidators, SingleBlockFieldAppearance> | FieldAttributesForFieldType<SourceType, 'slug', null, SlugFieldValidators, SlugFieldAppearance> | FieldAttributesForFieldType<SourceType, 'string', StringFieldValue, StringFieldValidators, StringFieldAppearance> | FieldAttributesForFieldType<SourceType, 'structured_text', null, StructuredTextFieldValidators, StructuredTextFieldAppearance> | FieldAttributesForFieldType<SourceType, 'text', TextFieldValue, TextFieldValidators, TextFieldAppearance> | FieldAttributesForFieldType<SourceType, 'video', null, VideoFieldValidators, VideoFieldAppearance>;
31
31
  export type FieldAttributes = GenericFieldAttributes<RawFieldAttributes>;
32
32
  export type Field = RawField;
33
- /**
34
- * Helper type to conditionally handle default values based on localization for field creation
35
- */
36
- type FieldCreateDefaultValue<T> = T | Record<string, T> | undefined;
37
- type FieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = Omit<SourceType, 'field_type' | 'default_value' | 'validators' | 'appearance'> & {
33
+ type LocalizedFieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = Omit<SourceType, 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'> & {
34
+ field_type: FieldType;
35
+ localized: true;
36
+ default_value?: Record<string, FieldValue>;
37
+ validators?: FieldValidators;
38
+ appearance?: FieldAppearanceConfig<FieldAppearance>;
39
+ };
40
+ type NonLocalizedFieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = Omit<SourceType, 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'> & {
38
41
  field_type: FieldType;
39
- default_value?: FieldCreateDefaultValue<FieldValue>;
42
+ localized?: false;
43
+ default_value?: FieldValue;
40
44
  validators?: FieldValidators;
41
45
  appearance?: FieldAppearanceConfig<FieldAppearance>;
42
46
  };
47
+ type FieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = LocalizedFieldCreateConfigForFieldType<SourceType, FieldType, FieldValue, FieldValidators, FieldAppearance> | NonLocalizedFieldCreateConfigForFieldType<SourceType, FieldType, FieldValue, FieldValidators, FieldAppearance>;
43
48
  type FieldCreateConfig<SourceType> = FieldCreateConfigForFieldType<SourceType, 'boolean', BooleanFieldValue, BooleanFieldValidators, BooleanFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'color', ColorFieldValue, ColorFieldValidators, ColorFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'date', DateFieldValue, DateFieldValidators, DateFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'date_time', DateTimeFieldValue, DateTimeFieldValidators, DateTimeFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'file', null, FileFieldValidators, FileFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'float', FloatFieldValue, FloatFieldValidators, FloatFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'gallery', null, GalleryFieldValidators, GalleryFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'integer', IntegerFieldValue, IntegerFieldValidators, IntegerFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'json', JsonFieldValue, JsonFieldValidators, JsonFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'lat_lon', LocationFieldValue, LocationFieldValidators, LocationFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'link', null, LinkFieldValidators, LinkFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'links', null, LinksFieldValidators, LinksFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'rich_text', null, RichTextFieldValidators, RichTextFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'seo', null, SeoFieldValidators, SeoFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'single_block', null, SingleBlockFieldValidators, SingleBlockFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'slug', null, SlugFieldValidators, SlugFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'string', StringFieldValue, StringFieldValidators, StringFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'structured_text', null, StructuredTextFieldValidators, StructuredTextFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'text', TextFieldValue, TextFieldValidators, TextFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'video', null, VideoFieldValidators, VideoFieldAppearance>;
44
49
  export type FieldCreateSchema = Omit<RawFieldCreateSchema, 'data'> & {
45
50
  data: Omit<RawFieldCreateSchema['data'], 'attributes'> & {
@@ -30,16 +30,21 @@ type FieldAttributesForFieldType<SourceType, FieldType extends RawFieldAttribute
30
30
  type GenericFieldAttributes<SourceType> = FieldAttributesForFieldType<SourceType, 'boolean', BooleanFieldValue, BooleanFieldValidators, BooleanFieldAppearance> | FieldAttributesForFieldType<SourceType, 'color', ColorFieldValue, ColorFieldValidators, ColorFieldAppearance> | FieldAttributesForFieldType<SourceType, 'date', DateFieldValue, DateFieldValidators, DateFieldAppearance> | FieldAttributesForFieldType<SourceType, 'date_time', DateTimeFieldValue, DateTimeFieldValidators, DateTimeFieldAppearance> | FieldAttributesForFieldType<SourceType, 'file', null, FileFieldValidators, FileFieldAppearance> | FieldAttributesForFieldType<SourceType, 'float', FloatFieldValue, FloatFieldValidators, FloatFieldAppearance> | FieldAttributesForFieldType<SourceType, 'gallery', null, GalleryFieldValidators, GalleryFieldAppearance> | FieldAttributesForFieldType<SourceType, 'integer', IntegerFieldValue, IntegerFieldValidators, IntegerFieldAppearance> | FieldAttributesForFieldType<SourceType, 'json', JsonFieldValue, JsonFieldValidators, JsonFieldAppearance> | FieldAttributesForFieldType<SourceType, 'lat_lon', LocationFieldValue, LocationFieldValidators, LocationFieldAppearance> | FieldAttributesForFieldType<SourceType, 'link', null, LinkFieldValidators, LinkFieldAppearance> | FieldAttributesForFieldType<SourceType, 'links', null, LinksFieldValidators, LinksFieldAppearance> | FieldAttributesForFieldType<SourceType, 'rich_text', null, RichTextFieldValidators, RichTextFieldAppearance> | FieldAttributesForFieldType<SourceType, 'seo', null, SeoFieldValidators, SeoFieldAppearance> | FieldAttributesForFieldType<SourceType, 'single_block', null, SingleBlockFieldValidators, SingleBlockFieldAppearance> | FieldAttributesForFieldType<SourceType, 'slug', null, SlugFieldValidators, SlugFieldAppearance> | FieldAttributesForFieldType<SourceType, 'string', StringFieldValue, StringFieldValidators, StringFieldAppearance> | FieldAttributesForFieldType<SourceType, 'structured_text', null, StructuredTextFieldValidators, StructuredTextFieldAppearance> | FieldAttributesForFieldType<SourceType, 'text', TextFieldValue, TextFieldValidators, TextFieldAppearance> | FieldAttributesForFieldType<SourceType, 'video', null, VideoFieldValidators, VideoFieldAppearance>;
31
31
  export type FieldAttributes = GenericFieldAttributes<RawFieldAttributes>;
32
32
  export type Field = GenericFieldAttributes<RawField>;
33
- /**
34
- * Helper type to conditionally handle default values based on localization for field creation
35
- */
36
- type FieldCreateDefaultValue<T> = T | Record<string, T> | undefined;
37
- type FieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = Omit<SourceType, 'field_type' | 'default_value' | 'validators' | 'appearance'> & {
33
+ type LocalizedFieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = Omit<SourceType, 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'> & {
34
+ field_type: FieldType;
35
+ localized: true;
36
+ default_value?: Record<string, FieldValue>;
37
+ validators?: FieldValidators;
38
+ appearance?: FieldAppearanceConfig<FieldAppearance>;
39
+ };
40
+ type NonLocalizedFieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = Omit<SourceType, 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'> & {
38
41
  field_type: FieldType;
39
- default_value?: FieldCreateDefaultValue<FieldValue>;
42
+ localized?: false;
43
+ default_value?: FieldValue;
40
44
  validators?: FieldValidators;
41
45
  appearance?: FieldAppearanceConfig<FieldAppearance>;
42
46
  };
47
+ type FieldCreateConfigForFieldType<SourceType, FieldType extends RawFieldAttributes['field_type'], FieldValue, FieldValidators, FieldAppearance> = LocalizedFieldCreateConfigForFieldType<SourceType, FieldType, FieldValue, FieldValidators, FieldAppearance> | NonLocalizedFieldCreateConfigForFieldType<SourceType, FieldType, FieldValue, FieldValidators, FieldAppearance>;
43
48
  type FieldCreateConfig<SourceType> = FieldCreateConfigForFieldType<SourceType, 'boolean', BooleanFieldValue, BooleanFieldValidators, BooleanFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'color', ColorFieldValue, ColorFieldValidators, ColorFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'date', DateFieldValue, DateFieldValidators, DateFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'date_time', DateTimeFieldValue, DateTimeFieldValidators, DateTimeFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'file', null, FileFieldValidators, FileFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'float', FloatFieldValue, FloatFieldValidators, FloatFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'gallery', null, GalleryFieldValidators, GalleryFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'integer', IntegerFieldValue, IntegerFieldValidators, IntegerFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'json', JsonFieldValue, JsonFieldValidators, JsonFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'lat_lon', LocationFieldValue, LocationFieldValidators, LocationFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'link', null, LinkFieldValidators, LinkFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'links', null, LinksFieldValidators, LinksFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'rich_text', null, RichTextFieldValidators, RichTextFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'seo', null, SeoFieldValidators, SeoFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'single_block', null, SingleBlockFieldValidators, SingleBlockFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'slug', null, SlugFieldValidators, SlugFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'string', StringFieldValue, StringFieldValidators, StringFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'structured_text', null, StructuredTextFieldValidators, StructuredTextFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'text', TextFieldValue, TextFieldValidators, TextFieldAppearance> | FieldCreateConfigForFieldType<SourceType, 'video', null, VideoFieldValidators, VideoFieldAppearance>;
44
49
  export type FieldCreateSchema = FieldCreateConfig<RawFieldCreateSchema>;
45
50
  /**
@@ -1,12 +1,58 @@
1
1
  import type * as SchemaTypes from '../generated/SchemaTypes';
2
2
  import type * as SimpleSchemaTypes from '../generated/SimpleSchemaTypes';
3
3
  /**
4
+ * Represents a localized field value in DatoCMS.
5
+ *
4
6
  * In DatoCMS, fields can be localized. In this scenario, their value contains values
5
7
  * for various locales structured as an object, such as
6
- `{ "en": "Hello", "it": "Ciao" }`)
8
+ * `{ "en": "Hello", "it": "Ciao" }`
7
9
  */
8
10
  export type LocalizedFieldValue = Record<string, unknown>;
11
+ /**
12
+ * Determines whether a DatoCMS field is localized or not.
13
+ *
14
+ * This function handles both full Schema field objects and simplified Schema field objects
15
+ * by checking the appropriate property based on the object structure.
16
+ *
17
+ * @param field - The DatoCMS field definition (either full or simple schema)
18
+ * @returns true if the field is localized, false otherwise
19
+ */
9
20
  export declare function isLocalized(field: SchemaTypes.Field | SimpleSchemaTypes.Field): boolean;
21
+ /**
22
+ * A normalized entry that represents a single value from either a localized or non-localized field.
23
+ *
24
+ * For localized fields, each locale produces one entry with `locale` set to the locale code (e.g., "en", "it").
25
+ * For non-localized fields, there's one entry with `locale` set to `undefined`.
26
+ *
27
+ * This uniform structure allows the same processing logic to work with both field types.
28
+ */
29
+ export type PossiblyLocalizedEntry = {
30
+ locale: string | undefined;
31
+ value: unknown;
32
+ };
33
+ /**
34
+ * Converts a field value (localized or non-localized) into a uniform array of entries.
35
+ *
36
+ * This function normalizes the handling of field values by converting them into a consistent
37
+ * array format, regardless of whether the field is localized or not.
38
+ *
39
+ * @param field - The DatoCMS field definition that determines localization behavior
40
+ * @param value - The field value to convert (either a localized object or direct value)
41
+ * @returns Array of entries where each entry contains a locale (string for localized, undefined for non-localized) and the corresponding value
42
+ */
43
+ export declare function fieldValueToPossiblyLocalizedEntries(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown): PossiblyLocalizedEntry[];
44
+ /**
45
+ * Converts an array of possibly localized entries back into the appropriate field value format.
46
+ *
47
+ * This function is the inverse of `fieldValueToPossiblyLocalizedEntries`. It takes a uniform
48
+ * array of entries and converts them back to either a localized object or a direct value,
49
+ * depending on the field's localization setting.
50
+ *
51
+ * @param field - The DatoCMS field definition that determines the output format
52
+ * @param possiblyLocalizedEntries - Array of entries to convert back to field value format
53
+ * @returns Either a localized object (for localized fields) or the direct value (for non-localized fields)
54
+ */
55
+ export declare function possiblyLocalizedEntriesToFieldValue(field: SchemaTypes.Field | SimpleSchemaTypes.Field, possiblyLocalizedEntries: PossiblyLocalizedEntry[]): any;
10
56
  /**
11
57
  * Maps localized field values using a provided mapping function.
12
58
  * For localized fields, applies the mapping function to each locale value.
@@ -18,9 +64,7 @@ export declare function isLocalized(field: SchemaTypes.Field | SimpleSchemaTypes
18
64
  * @param mapFn - The function to apply to each locale value or the direct value
19
65
  * @returns The mapped value with the same structure as the input
20
66
  */
21
- export declare function mapLocalizedFieldValues<T>(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, mapFn: (locale: string | undefined, localeValue: unknown) => T): T | {
22
- [k: string]: T;
23
- };
67
+ export declare function mapLocalizedFieldValues<T>(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, mapFn: (locale: string | undefined, localeValue: unknown) => T): any;
24
68
  /**
25
69
  * Maps localized field values using a provided mapping function (async version).
26
70
  * For localized fields, applies the mapping function to each locale value.
@@ -32,9 +76,7 @@ export declare function mapLocalizedFieldValues<T>(field: SchemaTypes.Field | Si
32
76
  * @param mapFn - The function to apply to each locale value or the direct value
33
77
  * @returns The mapped value with the same structure as the input
34
78
  */
35
- export declare function mapLocalizedFieldValuesAsync<T>(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, mapFn: (locale: string | undefined, localeValue: unknown) => Promise<T>): Promise<T | {
36
- [k: string]: unknown;
37
- }>;
79
+ export declare function mapLocalizedFieldValuesAsync<T>(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, mapFn: (locale: string | undefined, localeValue: unknown) => Promise<T>): Promise<any>;
38
80
  /**
39
81
  * Filters localized field values using a provided filter function.
40
82
  * For localized fields, filters each locale value.
@@ -45,7 +87,7 @@ export declare function mapLocalizedFieldValuesAsync<T>(field: SchemaTypes.Field
45
87
  * @param filterFn - The function to test each locale value or the direct value
46
88
  * @returns The filtered value with the same structure as the input
47
89
  */
48
- export declare function filterLocalizedFieldValues(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, filterFn: (locale: string | undefined, localeValue: unknown) => boolean): unknown;
90
+ export declare function filterLocalizedFieldValues(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, filterFn: (locale: string | undefined, localeValue: unknown) => boolean): any;
49
91
  /**
50
92
  * Filters localized field values using a provided filter function (async version).
51
93
  * For localized fields, filters each locale value.
@@ -56,7 +98,7 @@ export declare function filterLocalizedFieldValues(field: SchemaTypes.Field | Si
56
98
  * @param filterFn - The function to test each locale value or the direct value
57
99
  * @returns The filtered value with the same structure as the input
58
100
  */
59
- export declare function filterLocalizedFieldValuesAsync(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, filterFn: (locale: string | undefined, localeValue: unknown) => Promise<boolean>): Promise<unknown>;
101
+ export declare function filterLocalizedFieldValuesAsync(field: SchemaTypes.Field | SimpleSchemaTypes.Field, value: unknown, filterFn: (locale: string | undefined, localeValue: unknown) => Promise<boolean>): Promise<any>;
60
102
  /**
61
103
  * Tests whether at least one localized field value passes the test implemented by the provided function.
62
104
  * For localized fields, tests each locale value.
@@ -27,7 +27,7 @@ export type NodePredicate<T, R> = (node: AllNodesInTree<T>, parent: AllNodesInTr
27
27
  * @param node - The root node to start visiting from
28
28
  * @param visitor - Synchronous function called for each node. Receives the node, its parent, and path through the Structured Text tree
29
29
  */
30
- export declare function visitNodes<T>(node: T, visitor: NodePredicate<T, void>, parent?: AllNodesInTree<T> | null, path?: TreePath): void;
30
+ export declare function forEachNode<T>(node: T, visitor: NodePredicate<T, void>, parent?: AllNodesInTree<T> | null, path?: TreePath): void;
31
31
  /**
32
32
  * Visit every node in the Structured Text tree, calling the visitor function for each.
33
33
  * Uses pre-order traversal (parent is visited before its children).
@@ -37,7 +37,7 @@ export declare function visitNodes<T>(node: T, visitor: NodePredicate<T, void>,
37
37
  * @param visitor - Asynchronous function called for each node. Receives the node, its parent, and path through the Structured Text tree
38
38
  * @returns Promise that resolves when all nodes have been visited
39
39
  */
40
- export declare function visitNodesAsync<T>(node: T, visitor: NodePredicate<T, Promise<void>>, parent?: AllNodesInTree<T> | null, path?: TreePath): Promise<void>;
40
+ export declare function forEachNodeAsync<T>(node: T, visitor: NodePredicate<T, Promise<void>>, parent?: AllNodesInTree<T> | null, path?: TreePath): Promise<void>;
41
41
  /**
42
42
  * Transform nodes in the Structured Text tree by applying a mapping function.
43
43
  * Creates a new tree structure with transformed nodes while preserving the Structured Text tree hierarchy.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datocms/cma-client",
3
- "version": "5.1.0",
3
+ "version": "5.1.1",
4
4
  "description": "JS client for DatoCMS REST Content Management API",
5
5
  "keywords": [
6
6
  "datocms",
@@ -37,13 +37,13 @@
37
37
  "url": "https://github.com/datocms/js-rest-api-clients/issues"
38
38
  },
39
39
  "dependencies": {
40
- "@datocms/rest-client-utils": "^5.1.0",
40
+ "@datocms/rest-client-utils": "^5.1.1",
41
41
  "datocms-structured-text-utils": "^5.0.0",
42
42
  "uuid": "^9.0.1"
43
43
  },
44
44
  "devDependencies": {
45
- "@datocms/dashboard-client": "^5.1.0",
45
+ "@datocms/dashboard-client": "^5.1.1",
46
46
  "@types/uuid": "^9.0.7"
47
47
  },
48
- "gitHead": "ccc646f22202e951675e695fd4e5be2cfde4e9a5"
48
+ "gitHead": "9683432160a36b785e866b447e2baaecf11f00ab"
49
49
  }
@@ -71,7 +71,7 @@ export type RichTextFieldValueAsRequest = BlockItemInARequest[] | null;
71
71
  /**
72
72
  * Modular Content field value with nested blocks - array of fully populated block objects
73
73
  */
74
- export type RichTextFieldValueWithResolvedBlocks = SchemaTypes.Item[] | null;
74
+ export type RichTextFieldValueWithNestedBlocks = SchemaTypes.Item[] | null;
75
75
 
76
76
  /**
77
77
  * =============================================================================
@@ -117,9 +117,9 @@ export function isRichTextFieldValueAsRequest(
117
117
  * Type guard for Modular Content field values with nested blocks (?nested=true format).
118
118
  * Ensures all blocks are full SchemaTypes.Item objects with complete data.
119
119
  */
120
- export function isRichTextFieldValueWithResolvedBlocks(
120
+ export function isRichTextFieldValueWithNestedBlocks(
121
121
  value: unknown,
122
- ): value is RichTextFieldValueWithResolvedBlocks {
122
+ ): value is RichTextFieldValueWithNestedBlocks {
123
123
  if (value === null) return true;
124
124
 
125
125
  if (!Array.isArray(value)) return false;
@@ -77,7 +77,7 @@ export type SingleBlockFieldValueAsRequest = BlockItemInARequest | null;
77
77
  /**
78
78
  * Single Block field value with nested block - fully populated block object
79
79
  */
80
- export type SingleBlockFieldValueWithResolvedBlocks = SchemaTypes.Item | null;
80
+ export type SingleBlockFieldValueWithNestedBlocks = SchemaTypes.Item | null;
81
81
 
82
82
  /**
83
83
  * =============================================================================
@@ -159,9 +159,9 @@ export function isSingleBlockFieldValueAsRequest(
159
159
  * Type guard for Single Block field values with nested blocks (?nested=true format).
160
160
  * Ensures block is a full SchemaTypes.Item object with complete data.
161
161
  */
162
- export function isSingleBlockFieldValueWithResolvedBlocks(
162
+ export function isSingleBlockFieldValueWithNestedBlocks(
163
163
  value: unknown,
164
- ): value is SingleBlockFieldValueWithResolvedBlocks {
164
+ ): value is SingleBlockFieldValueWithNestedBlocks {
165
165
  if (value === null) return true;
166
166
 
167
167
  // Must be a full object with ID (nested format always includes complete block objects)
@@ -94,35 +94,35 @@ export type DocumentAsRequest = {
94
94
  /**
95
95
  * Variant of 'block' structured text node for ?nested=true API responses
96
96
  */
97
- export type BlockWithResolvedBlocks = Omit<Block, 'item'> & {
97
+ export type BlockWithNestedBlocks = Omit<Block, 'item'> & {
98
98
  item: SchemaTypes.Item;
99
99
  };
100
100
 
101
101
  /**
102
102
  * Variant of 'inlineBlock' structured text node for ?nested=true API responses
103
103
  */
104
- export type InlineBlockWithResolvedBlocks = Omit<InlineBlock, 'item'> & {
104
+ export type InlineBlockWithNestedBlocks = Omit<InlineBlock, 'item'> & {
105
105
  item: SchemaTypes.Item;
106
106
  };
107
107
 
108
108
  /**
109
109
  * Generic type to transform a node that might be containing a 'block' or 'inlineBlock' as a (deeply nested) children to it's variant for ?nested=true API responses
110
110
  */
111
- export type NodeWithResolvedBlocks<T> = WithMappedChildren<
111
+ export type NodeWithNestedBlocks<T> = WithMappedChildren<
112
112
  T,
113
113
  DeepMapVariants<
114
114
  T extends { children: infer C } ? C : never,
115
- BlockWithResolvedBlocks,
116
- InlineBlockWithResolvedBlocks
115
+ BlockWithNestedBlocks,
116
+ InlineBlockWithNestedBlocks
117
117
  >
118
118
  >;
119
119
 
120
120
  /**
121
121
  * Variant of Structured Text document for ?nested=true API responses
122
122
  */
123
- export type DocumentWithResolvedBlocks = {
123
+ export type DocumentWithNestedBlocks = {
124
124
  schema: 'dast';
125
- document: NodeWithResolvedBlocks<Root>;
125
+ document: NodeWithNestedBlocks<Root>;
126
126
  };
127
127
 
128
128
  /**
@@ -137,8 +137,8 @@ export type DocumentWithResolvedBlocks = {
137
137
  */
138
138
  export type StructuredTextFieldValue = Document | null;
139
139
  export type StructuredTextFieldValueAsRequest = DocumentAsRequest | null;
140
- export type StructuredTextFieldValueWithResolvedBlocks =
141
- DocumentWithResolvedBlocks | null;
140
+ export type StructuredTextFieldValueWithNestedBlocks =
141
+ DocumentWithNestedBlocks | null;
142
142
 
143
143
  /**
144
144
  * Helper function to validate if a value has the expected structured text document structure.
@@ -165,10 +165,10 @@ function validateAllBlockNodes(
165
165
  node:
166
166
  | Block
167
167
  | BlockAsRequest
168
- | BlockWithResolvedBlocks
168
+ | BlockWithNestedBlocks
169
169
  | InlineBlock
170
170
  | InlineBlockAsRequest
171
- | InlineBlockWithResolvedBlocks,
171
+ | InlineBlockWithNestedBlocks,
172
172
  ) => boolean,
173
173
  ): boolean {
174
174
  return everyNode(node, (currentNode) => {
@@ -232,9 +232,9 @@ export function isStructuredTextFieldValueAsRequest(
232
232
  * Type guard for structured text field values with nested blocks (?nested=true format).
233
233
  * Ensures all block/inlineBlock nodes have full SchemaTypes.Item objects.
234
234
  */
235
- export function isStructuredTextFieldValueWithResolvedBlocks(
235
+ export function isStructuredTextFieldValueWithNestedBlocks(
236
236
  value: unknown,
237
- ): value is StructuredTextFieldValueWithResolvedBlocks {
237
+ ): value is StructuredTextFieldValueWithNestedBlocks {
238
238
  if (value === null) return true;
239
239
 
240
240
  if (!isValidDocumentStructure(value)) {
@@ -147,7 +147,7 @@ export class Client {
147
147
  ...this.config,
148
148
  ...options,
149
149
  logFn: this.config.logFn || console.log,
150
- userAgent: '@datocms/cma-client v5.1.0',
150
+ userAgent: '@datocms/cma-client v5.1.1',
151
151
  baseUrl: this.baseUrl,
152
152
  preCallStack: new Error().stack,
153
153
  extraHeaders: {
@@ -278,12 +278,24 @@ export type FieldAttributes = GenericFieldAttributes<RawFieldAttributes>;
278
278
 
279
279
  export type Field = RawField;
280
280
 
281
- /**
282
- * Helper type to conditionally handle default values based on localization for field creation
283
- */
284
- type FieldCreateDefaultValue<T> = T | Record<string, T> | undefined;
281
+ type LocalizedFieldCreateConfigForFieldType<
282
+ SourceType,
283
+ FieldType extends RawFieldAttributes['field_type'],
284
+ FieldValue,
285
+ FieldValidators,
286
+ FieldAppearance,
287
+ > = Omit<
288
+ SourceType,
289
+ 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'
290
+ > & {
291
+ field_type: FieldType;
292
+ localized: true;
293
+ default_value?: Record<string, FieldValue>;
294
+ validators?: FieldValidators;
295
+ appearance?: FieldAppearanceConfig<FieldAppearance>;
296
+ };
285
297
 
286
- type FieldCreateConfigForFieldType<
298
+ type NonLocalizedFieldCreateConfigForFieldType<
287
299
  SourceType,
288
300
  FieldType extends RawFieldAttributes['field_type'],
289
301
  FieldValue,
@@ -291,14 +303,37 @@ type FieldCreateConfigForFieldType<
291
303
  FieldAppearance,
292
304
  > = Omit<
293
305
  SourceType,
294
- 'field_type' | 'default_value' | 'validators' | 'appearance'
306
+ 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'
295
307
  > & {
296
308
  field_type: FieldType;
297
- default_value?: FieldCreateDefaultValue<FieldValue>;
309
+ localized?: false;
310
+ default_value?: FieldValue;
298
311
  validators?: FieldValidators;
299
312
  appearance?: FieldAppearanceConfig<FieldAppearance>;
300
313
  };
301
314
 
315
+ type FieldCreateConfigForFieldType<
316
+ SourceType,
317
+ FieldType extends RawFieldAttributes['field_type'],
318
+ FieldValue,
319
+ FieldValidators,
320
+ FieldAppearance,
321
+ > =
322
+ | LocalizedFieldCreateConfigForFieldType<
323
+ SourceType,
324
+ FieldType,
325
+ FieldValue,
326
+ FieldValidators,
327
+ FieldAppearance
328
+ >
329
+ | NonLocalizedFieldCreateConfigForFieldType<
330
+ SourceType,
331
+ FieldType,
332
+ FieldValue,
333
+ FieldValidators,
334
+ FieldAppearance
335
+ >;
336
+
302
337
  type FieldCreateConfig<SourceType> =
303
338
  | FieldCreateConfigForFieldType<
304
339
  SourceType,
@@ -278,12 +278,24 @@ export type FieldAttributes = GenericFieldAttributes<RawFieldAttributes>;
278
278
 
279
279
  export type Field = GenericFieldAttributes<RawField>;
280
280
 
281
- /**
282
- * Helper type to conditionally handle default values based on localization for field creation
283
- */
284
- type FieldCreateDefaultValue<T> = T | Record<string, T> | undefined;
281
+ type LocalizedFieldCreateConfigForFieldType<
282
+ SourceType,
283
+ FieldType extends RawFieldAttributes['field_type'],
284
+ FieldValue,
285
+ FieldValidators,
286
+ FieldAppearance,
287
+ > = Omit<
288
+ SourceType,
289
+ 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'
290
+ > & {
291
+ field_type: FieldType;
292
+ localized: true;
293
+ default_value?: Record<string, FieldValue>;
294
+ validators?: FieldValidators;
295
+ appearance?: FieldAppearanceConfig<FieldAppearance>;
296
+ };
285
297
 
286
- type FieldCreateConfigForFieldType<
298
+ type NonLocalizedFieldCreateConfigForFieldType<
287
299
  SourceType,
288
300
  FieldType extends RawFieldAttributes['field_type'],
289
301
  FieldValue,
@@ -291,14 +303,37 @@ type FieldCreateConfigForFieldType<
291
303
  FieldAppearance,
292
304
  > = Omit<
293
305
  SourceType,
294
- 'field_type' | 'default_value' | 'validators' | 'appearance'
306
+ 'field_type' | 'default_value' | 'validators' | 'appearance' | 'localized'
295
307
  > & {
296
308
  field_type: FieldType;
297
- default_value?: FieldCreateDefaultValue<FieldValue>;
309
+ localized?: false;
310
+ default_value?: FieldValue;
298
311
  validators?: FieldValidators;
299
312
  appearance?: FieldAppearanceConfig<FieldAppearance>;
300
313
  };
301
314
 
315
+ type FieldCreateConfigForFieldType<
316
+ SourceType,
317
+ FieldType extends RawFieldAttributes['field_type'],
318
+ FieldValue,
319
+ FieldValidators,
320
+ FieldAppearance,
321
+ > =
322
+ | LocalizedFieldCreateConfigForFieldType<
323
+ SourceType,
324
+ FieldType,
325
+ FieldValue,
326
+ FieldValidators,
327
+ FieldAppearance
328
+ >
329
+ | NonLocalizedFieldCreateConfigForFieldType<
330
+ SourceType,
331
+ FieldType,
332
+ FieldValue,
333
+ FieldValidators,
334
+ FieldAppearance
335
+ >;
336
+
302
337
  type FieldCreateConfig<SourceType> =
303
338
  | FieldCreateConfigForFieldType<
304
339
  SourceType,
@@ -26,13 +26,13 @@ import type {
26
26
  BlockItemInARequest,
27
27
  RichTextFieldValue,
28
28
  RichTextFieldValueAsRequest,
29
- RichTextFieldValueWithResolvedBlocks,
29
+ RichTextFieldValueWithNestedBlocks,
30
30
  SingleBlockFieldValue,
31
31
  SingleBlockFieldValueAsRequest,
32
- SingleBlockFieldValueWithResolvedBlocks,
32
+ SingleBlockFieldValueWithNestedBlocks,
33
33
  StructuredTextFieldValue,
34
34
  StructuredTextFieldValueAsRequest,
35
- StructuredTextFieldValueWithResolvedBlocks,
35
+ StructuredTextFieldValueWithNestedBlocks,
36
36
  } from '../fieldTypes';
37
37
  import type * as SchemaTypes from '../generated/SchemaTypes';
38
38
  import type * as SimpleSchemaTypes from '../generated/SimpleSchemaTypes';
@@ -49,15 +49,15 @@ import {
49
49
  type PossibleRichTextValue =
50
50
  | RichTextFieldValue
51
51
  | RichTextFieldValueAsRequest
52
- | RichTextFieldValueWithResolvedBlocks;
52
+ | RichTextFieldValueWithNestedBlocks;
53
53
  type PossibleSingleBlockValue =
54
54
  | SingleBlockFieldValue
55
55
  | SingleBlockFieldValueAsRequest
56
- | SingleBlockFieldValueWithResolvedBlocks;
56
+ | SingleBlockFieldValueWithNestedBlocks;
57
57
  type PossibleStructuredTextValue =
58
58
  | StructuredTextFieldValue
59
59
  | StructuredTextFieldValueAsRequest
60
- | StructuredTextFieldValueWithResolvedBlocks;
60
+ | StructuredTextFieldValueWithNestedBlocks;
61
61
 
62
62
  function getFieldType(field: SchemaTypes.Field | SimpleSchemaTypes.Field) {
63
63
  return 'attributes' in field ? field.attributes.field_type : field.field_type;