@datocms/cma-client 5.1.9 → 5.1.11

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 (226) hide show
  1. package/README.md +575 -0
  2. package/dist/cjs/fieldTypes/boolean.js +2 -2
  3. package/dist/cjs/fieldTypes/boolean.js.map +1 -1
  4. package/dist/cjs/fieldTypes/color.js +2 -2
  5. package/dist/cjs/fieldTypes/color.js.map +1 -1
  6. package/dist/cjs/fieldTypes/date.js +2 -3
  7. package/dist/cjs/fieldTypes/date.js.map +1 -1
  8. package/dist/cjs/fieldTypes/date_time.js +2 -2
  9. package/dist/cjs/fieldTypes/date_time.js.map +1 -1
  10. package/dist/cjs/fieldTypes/file.js +33 -5
  11. package/dist/cjs/fieldTypes/file.js.map +1 -1
  12. package/dist/cjs/fieldTypes/float.js +2 -2
  13. package/dist/cjs/fieldTypes/float.js.map +1 -1
  14. package/dist/cjs/fieldTypes/gallery.js +20 -5
  15. package/dist/cjs/fieldTypes/gallery.js.map +1 -1
  16. package/dist/cjs/fieldTypes/index.js +2 -2
  17. package/dist/cjs/fieldTypes/index.js.map +1 -1
  18. package/dist/cjs/fieldTypes/integer.js +2 -2
  19. package/dist/cjs/fieldTypes/integer.js.map +1 -1
  20. package/dist/cjs/fieldTypes/json.js +2 -3
  21. package/dist/cjs/fieldTypes/json.js.map +1 -1
  22. package/dist/cjs/fieldTypes/lat_lon.js +2 -2
  23. package/dist/cjs/fieldTypes/lat_lon.js.map +1 -1
  24. package/dist/cjs/fieldTypes/link.js +4 -4
  25. package/dist/cjs/fieldTypes/link.js.map +1 -1
  26. package/dist/cjs/fieldTypes/links.js +5 -3
  27. package/dist/cjs/fieldTypes/links.js.map +1 -1
  28. package/dist/cjs/fieldTypes/rich_text.js +4 -6
  29. package/dist/cjs/fieldTypes/rich_text.js.map +1 -1
  30. package/dist/cjs/fieldTypes/seo.js +2 -3
  31. package/dist/cjs/fieldTypes/seo.js.map +1 -1
  32. package/dist/cjs/fieldTypes/single_block.js +7 -7
  33. package/dist/cjs/fieldTypes/single_block.js.map +1 -1
  34. package/dist/cjs/fieldTypes/slug.js +2 -3
  35. package/dist/cjs/fieldTypes/slug.js.map +1 -1
  36. package/dist/cjs/fieldTypes/string.js +2 -2
  37. package/dist/cjs/fieldTypes/string.js.map +1 -1
  38. package/dist/cjs/fieldTypes/structured_text.js +4 -6
  39. package/dist/cjs/fieldTypes/structured_text.js.map +1 -1
  40. package/dist/cjs/fieldTypes/text.js +2 -3
  41. package/dist/cjs/fieldTypes/text.js.map +1 -1
  42. package/dist/cjs/fieldTypes/video.js +2 -2
  43. package/dist/cjs/fieldTypes/video.js.map +1 -1
  44. package/dist/cjs/generated/Client.js +1 -1
  45. package/dist/cjs/generated/Client.js.map +1 -1
  46. package/dist/cjs/generated/resources/Item.js.map +1 -1
  47. package/dist/cjs/generated/resources/Upload.js.map +1 -1
  48. package/dist/cjs/index.js +4 -4
  49. package/dist/cjs/index.js.map +1 -1
  50. package/dist/cjs/utilities/buildBlockRecord.js +8 -7
  51. package/dist/cjs/utilities/buildBlockRecord.js.map +1 -1
  52. package/dist/cjs/utilities/inspectItem.js +397 -0
  53. package/dist/cjs/utilities/inspectItem.js.map +1 -0
  54. package/dist/cjs/utilities/{blocks.js → nonRecursiveBlocks.js} +106 -111
  55. package/dist/cjs/utilities/nonRecursiveBlocks.js.map +1 -0
  56. package/dist/cjs/utilities/{fieldValue.js → normalizedFieldValues.js} +53 -41
  57. package/dist/cjs/utilities/normalizedFieldValues.js.map +1 -0
  58. package/dist/cjs/utilities/recursiveBlocks.js +121 -80
  59. package/dist/cjs/utilities/recursiveBlocks.js.map +1 -1
  60. package/dist/cjs/utilities/schemaRepository.js +2 -2
  61. package/dist/esm/fieldTypes/boolean.d.ts +1 -1
  62. package/dist/esm/fieldTypes/boolean.js +2 -2
  63. package/dist/esm/fieldTypes/boolean.js.map +1 -1
  64. package/dist/esm/fieldTypes/color.d.ts +1 -1
  65. package/dist/esm/fieldTypes/color.js +2 -2
  66. package/dist/esm/fieldTypes/color.js.map +1 -1
  67. package/dist/esm/fieldTypes/date.d.ts +1 -1
  68. package/dist/esm/fieldTypes/date.js +2 -3
  69. package/dist/esm/fieldTypes/date.js.map +1 -1
  70. package/dist/esm/fieldTypes/date_time.d.ts +1 -1
  71. package/dist/esm/fieldTypes/date_time.js +2 -2
  72. package/dist/esm/fieldTypes/date_time.js.map +1 -1
  73. package/dist/esm/fieldTypes/file.d.ts +68 -1
  74. package/dist/esm/fieldTypes/file.js +29 -3
  75. package/dist/esm/fieldTypes/file.js.map +1 -1
  76. package/dist/esm/fieldTypes/float.d.ts +1 -1
  77. package/dist/esm/fieldTypes/float.js +2 -2
  78. package/dist/esm/fieldTypes/float.js.map +1 -1
  79. package/dist/esm/fieldTypes/gallery.d.ts +27 -11
  80. package/dist/esm/fieldTypes/gallery.js +17 -4
  81. package/dist/esm/fieldTypes/gallery.js.map +1 -1
  82. package/dist/esm/fieldTypes/index.d.ts +2 -2
  83. package/dist/esm/fieldTypes/index.js +2 -2
  84. package/dist/esm/fieldTypes/index.js.map +1 -1
  85. package/dist/esm/fieldTypes/integer.d.ts +1 -1
  86. package/dist/esm/fieldTypes/integer.js +2 -2
  87. package/dist/esm/fieldTypes/integer.js.map +1 -1
  88. package/dist/esm/fieldTypes/json.d.ts +1 -1
  89. package/dist/esm/fieldTypes/json.js +2 -3
  90. package/dist/esm/fieldTypes/json.js.map +1 -1
  91. package/dist/esm/fieldTypes/lat_lon.d.ts +1 -1
  92. package/dist/esm/fieldTypes/lat_lon.js +2 -2
  93. package/dist/esm/fieldTypes/lat_lon.js.map +1 -1
  94. package/dist/esm/fieldTypes/link.d.ts +1 -1
  95. package/dist/esm/fieldTypes/link.js +4 -4
  96. package/dist/esm/fieldTypes/link.js.map +1 -1
  97. package/dist/esm/fieldTypes/links.d.ts +1 -1
  98. package/dist/esm/fieldTypes/links.js +5 -3
  99. package/dist/esm/fieldTypes/links.js.map +1 -1
  100. package/dist/esm/fieldTypes/rich_text.d.ts +1 -1
  101. package/dist/esm/fieldTypes/rich_text.js +4 -6
  102. package/dist/esm/fieldTypes/rich_text.js.map +1 -1
  103. package/dist/esm/fieldTypes/seo.d.ts +1 -1
  104. package/dist/esm/fieldTypes/seo.js +2 -3
  105. package/dist/esm/fieldTypes/seo.js.map +1 -1
  106. package/dist/esm/fieldTypes/single_block.d.ts +3 -3
  107. package/dist/esm/fieldTypes/single_block.js +7 -7
  108. package/dist/esm/fieldTypes/single_block.js.map +1 -1
  109. package/dist/esm/fieldTypes/slug.d.ts +1 -1
  110. package/dist/esm/fieldTypes/slug.js +2 -3
  111. package/dist/esm/fieldTypes/slug.js.map +1 -1
  112. package/dist/esm/fieldTypes/string.d.ts +1 -1
  113. package/dist/esm/fieldTypes/string.js +2 -2
  114. package/dist/esm/fieldTypes/string.js.map +1 -1
  115. package/dist/esm/fieldTypes/structured_text.d.ts +1 -1
  116. package/dist/esm/fieldTypes/structured_text.js +4 -6
  117. package/dist/esm/fieldTypes/structured_text.js.map +1 -1
  118. package/dist/esm/fieldTypes/text.d.ts +1 -1
  119. package/dist/esm/fieldTypes/text.js +2 -3
  120. package/dist/esm/fieldTypes/text.js.map +1 -1
  121. package/dist/esm/fieldTypes/video.d.ts +1 -1
  122. package/dist/esm/fieldTypes/video.js +2 -2
  123. package/dist/esm/fieldTypes/video.js.map +1 -1
  124. package/dist/esm/generated/Client.js +1 -1
  125. package/dist/esm/generated/Client.js.map +1 -1
  126. package/dist/esm/generated/resources/Item.d.ts +46 -28
  127. package/dist/esm/generated/resources/Item.js.map +1 -1
  128. package/dist/esm/generated/resources/ScheduledPublication.d.ts +3 -3
  129. package/dist/esm/generated/resources/ScheduledUnpublishing.d.ts +3 -3
  130. package/dist/esm/generated/resources/Upload.d.ts +10 -4
  131. package/dist/esm/generated/resources/Upload.js.map +1 -1
  132. package/dist/esm/index.d.ts +4 -4
  133. package/dist/esm/index.js +4 -4
  134. package/dist/esm/index.js.map +1 -1
  135. package/dist/esm/utilities/buildBlockRecord.d.ts +3 -3
  136. package/dist/esm/utilities/buildBlockRecord.js +9 -8
  137. package/dist/esm/utilities/buildBlockRecord.js.map +1 -1
  138. package/dist/esm/utilities/inspectItem.d.ts +8 -0
  139. package/dist/esm/utilities/inspectItem.js +393 -0
  140. package/dist/esm/utilities/inspectItem.js.map +1 -0
  141. package/dist/esm/utilities/itemDefinition.d.ts +36 -12
  142. package/dist/esm/utilities/nonRecursiveBlocks.d.ts +179 -0
  143. package/dist/esm/utilities/{blocks.js → nonRecursiveBlocks.js} +91 -96
  144. package/dist/esm/utilities/nonRecursiveBlocks.js.map +1 -0
  145. package/dist/{types/utilities/fieldValue.d.ts → esm/utilities/normalizedFieldValues.d.ts} +15 -14
  146. package/dist/esm/utilities/{fieldValue.js → normalizedFieldValues.js} +39 -28
  147. package/dist/esm/utilities/normalizedFieldValues.js.map +1 -0
  148. package/dist/esm/utilities/recursiveBlocks.d.ts +92 -9
  149. package/dist/esm/utilities/recursiveBlocks.js +113 -72
  150. package/dist/esm/utilities/recursiveBlocks.js.map +1 -1
  151. package/dist/esm/utilities/schemaRepository.d.ts +2 -2
  152. package/dist/esm/utilities/schemaRepository.js +2 -2
  153. package/dist/types/fieldTypes/boolean.d.ts +1 -1
  154. package/dist/types/fieldTypes/color.d.ts +1 -1
  155. package/dist/types/fieldTypes/date.d.ts +1 -1
  156. package/dist/types/fieldTypes/date_time.d.ts +1 -1
  157. package/dist/types/fieldTypes/file.d.ts +68 -1
  158. package/dist/types/fieldTypes/float.d.ts +1 -1
  159. package/dist/types/fieldTypes/gallery.d.ts +27 -11
  160. package/dist/types/fieldTypes/index.d.ts +2 -2
  161. package/dist/types/fieldTypes/integer.d.ts +1 -1
  162. package/dist/types/fieldTypes/json.d.ts +1 -1
  163. package/dist/types/fieldTypes/lat_lon.d.ts +1 -1
  164. package/dist/types/fieldTypes/link.d.ts +1 -1
  165. package/dist/types/fieldTypes/links.d.ts +1 -1
  166. package/dist/types/fieldTypes/rich_text.d.ts +1 -1
  167. package/dist/types/fieldTypes/seo.d.ts +1 -1
  168. package/dist/types/fieldTypes/single_block.d.ts +3 -3
  169. package/dist/types/fieldTypes/slug.d.ts +1 -1
  170. package/dist/types/fieldTypes/string.d.ts +1 -1
  171. package/dist/types/fieldTypes/structured_text.d.ts +1 -1
  172. package/dist/types/fieldTypes/text.d.ts +1 -1
  173. package/dist/types/fieldTypes/video.d.ts +1 -1
  174. package/dist/types/generated/resources/Item.d.ts +46 -28
  175. package/dist/types/generated/resources/ScheduledPublication.d.ts +3 -3
  176. package/dist/types/generated/resources/ScheduledUnpublishing.d.ts +3 -3
  177. package/dist/types/generated/resources/Upload.d.ts +10 -4
  178. package/dist/types/index.d.ts +4 -4
  179. package/dist/types/utilities/buildBlockRecord.d.ts +3 -3
  180. package/dist/types/utilities/inspectItem.d.ts +8 -0
  181. package/dist/types/utilities/itemDefinition.d.ts +36 -12
  182. package/dist/types/utilities/nonRecursiveBlocks.d.ts +179 -0
  183. package/dist/{esm/utilities/fieldValue.d.ts → types/utilities/normalizedFieldValues.d.ts} +15 -14
  184. package/dist/types/utilities/recursiveBlocks.d.ts +92 -9
  185. package/dist/types/utilities/schemaRepository.d.ts +2 -2
  186. package/package.json +4 -4
  187. package/src/fieldTypes/boolean.ts +5 -3
  188. package/src/fieldTypes/color.ts +5 -3
  189. package/src/fieldTypes/date.ts +5 -4
  190. package/src/fieldTypes/date_time.ts +5 -3
  191. package/src/fieldTypes/file.ts +98 -4
  192. package/src/fieldTypes/float.ts +5 -3
  193. package/src/fieldTypes/gallery.ts +53 -20
  194. package/src/fieldTypes/index.ts +2 -3
  195. package/src/fieldTypes/integer.ts +5 -3
  196. package/src/fieldTypes/json.ts +5 -4
  197. package/src/fieldTypes/lat_lon.ts +5 -3
  198. package/src/fieldTypes/link.ts +7 -5
  199. package/src/fieldTypes/links.ts +8 -4
  200. package/src/fieldTypes/rich_text.ts +7 -7
  201. package/src/fieldTypes/seo.ts +5 -4
  202. package/src/fieldTypes/single_block.ts +12 -10
  203. package/src/fieldTypes/slug.ts +5 -4
  204. package/src/fieldTypes/string.ts +5 -3
  205. package/src/fieldTypes/structured_text.ts +7 -7
  206. package/src/fieldTypes/text.ts +5 -4
  207. package/src/fieldTypes/video.ts +5 -3
  208. package/src/generated/Client.ts +1 -1
  209. package/src/generated/resources/Item.ts +129 -92
  210. package/src/generated/resources/ScheduledPublication.ts +6 -6
  211. package/src/generated/resources/ScheduledUnpublishing.ts +6 -6
  212. package/src/generated/resources/Upload.ts +36 -10
  213. package/src/index.ts +4 -4
  214. package/src/utilities/buildBlockRecord.ts +24 -31
  215. package/src/utilities/inspectItem.ts +590 -0
  216. package/src/utilities/itemDefinition.ts +53 -35
  217. package/src/utilities/{blocks.ts → nonRecursiveBlocks.ts} +170 -136
  218. package/src/utilities/{fieldValue.ts → normalizedFieldValues.ts} +70 -30
  219. package/src/utilities/recursiveBlocks.ts +186 -171
  220. package/src/utilities/schemaRepository.ts +2 -2
  221. package/dist/cjs/utilities/blocks.js.map +0 -1
  222. package/dist/cjs/utilities/fieldValue.js.map +0 -1
  223. package/dist/esm/utilities/blocks.d.ts +0 -180
  224. package/dist/esm/utilities/blocks.js.map +0 -1
  225. package/dist/esm/utilities/fieldValue.js.map +0 -1
  226. package/dist/types/utilities/blocks.d.ts +0 -180
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  export { ApiError, LogLevel, TimeoutError } from '@datocms/rest-client-utils';
2
2
  export * from './buildClient';
3
+ export * from './fieldTypes';
3
4
  export { Client } from './generated/Client';
4
5
  export type { ClientConfigOptions } from './generated/Client';
5
6
  export * as Resources from './generated/resources';
6
- export * from './utilities/blocks';
7
7
  export * from './utilities/buildBlockRecord';
8
8
  export * from './utilities/fieldsContainingReferences';
9
- export * from './utilities/fieldValue';
10
9
  export * from './utilities/id';
10
+ export * from './utilities/inspectItem';
11
+ export * from './utilities/itemDefinition';
12
+ export * from './utilities/normalizedFieldValues';
11
13
  export * from './utilities/recursiveBlocks';
12
14
  export * from './utilities/schemaRepository';
13
- export * from './utilities/itemDefinition';
14
- export * from './fieldTypes';
15
15
  export type { ApiTypes, RawApiTypes };
16
16
  // Legacy names
17
17
  export type { ApiTypes as SchemaTypes, RawApiTypes as SimpleSchemaTypes };
@@ -7,10 +7,10 @@ import type * as ApiTypes from '../generated/ApiTypes';
7
7
  import { Item } from '../generated/resources';
8
8
  import type {
9
9
  ItemTypeDefinition,
10
- ItemTypeDefinitionToItemDefinitionAsRequest,
11
- ItemTypeDefinitionToItemDefinitionWithNestedBlocks,
10
+ ToItemDefinitionAsRequest,
11
+ ToItemDefinitionWithNestedBlocks,
12
12
  } from './itemDefinition';
13
- import { mapBlocksInFieldValues } from './recursiveBlocks';
13
+ import { mapBlocksInNonLocalizedFieldValue } from './recursiveBlocks';
14
14
  import type { SchemaRepository } from './schemaRepository';
15
15
 
16
16
  type NoInfer<T> = [T][T extends any ? 0 : never];
@@ -18,14 +18,10 @@ type NoInfer<T> = [T][T extends any ? 0 : never];
18
18
  export function buildBlockRecord<
19
19
  D extends ItemTypeDefinition = ItemTypeDefinition,
20
20
  >(
21
- body: ApiTypes.ItemUpdateSchema<
22
- ItemTypeDefinitionToItemDefinitionAsRequest<NoInfer<D>>
23
- >,
24
- ): NewBlockInARequest<ItemTypeDefinitionToItemDefinitionAsRequest<NoInfer<D>>> {
21
+ body: ApiTypes.ItemUpdateSchema<ToItemDefinitionAsRequest<NoInfer<D>>>,
22
+ ): NewBlockInARequest<ToItemDefinitionAsRequest<NoInfer<D>>> {
25
23
  return Utils.serializeRequestBody<{
26
- data: NewBlockInARequest<
27
- ItemTypeDefinitionToItemDefinitionAsRequest<NoInfer<D>>
28
- >;
24
+ data: NewBlockInARequest<ToItemDefinitionAsRequest<NoInfer<D>>>;
29
25
  }>(body, {
30
26
  type: Item.TYPE,
31
27
  attributes: '*',
@@ -37,12 +33,10 @@ export async function duplicateBlockRecord<
37
33
  D extends ItemTypeDefinition = ItemTypeDefinition,
38
34
  >(
39
35
  existingBlock: ItemWithOptionalIdAndMeta<
40
- ItemTypeDefinitionToItemDefinitionWithNestedBlocks<NoInfer<D>>
36
+ ToItemDefinitionWithNestedBlocks<NoInfer<D>>
41
37
  >,
42
38
  schemaRepository: SchemaRepository,
43
- ): Promise<
44
- NewBlockInARequest<ItemTypeDefinitionToItemDefinitionAsRequest<NoInfer<D>>>
45
- > {
39
+ ): Promise<NewBlockInARequest<ToItemDefinitionAsRequest<NoInfer<D>>>> {
46
40
  const { type, attributes, relationships } = existingBlock;
47
41
 
48
42
  const itemType = await schemaRepository.getRawItemTypeById(
@@ -58,25 +52,24 @@ export async function duplicateBlockRecord<
58
52
  const fields = await schemaRepository.getRawItemTypeFields(itemType);
59
53
 
60
54
  for (const field of fields) {
61
- newBlock.attributes[field.attributes.api_key] = mapBlocksInFieldValues(
62
- schemaRepository,
63
- field,
64
- newBlock.attributes[field.attributes.api_key],
65
- (block, path) => {
66
- if (typeof block === 'string') {
67
- throw new Error(
68
- `Block cannot be duplicated as it contains nested block at ${path.join('.')} that is expressed as ID (${block}) instead of full object!`,
69
- );
70
- }
55
+ newBlock.attributes[field.attributes.api_key] =
56
+ mapBlocksInNonLocalizedFieldValue(
57
+ schemaRepository,
58
+ field.attributes.field_type,
59
+ newBlock.attributes[field.attributes.api_key],
60
+ (block, path) => {
61
+ if (typeof block === 'string') {
62
+ throw new Error(
63
+ `Block cannot be duplicated as it contains nested block at ${path.join('.')} that is expressed as ID (${block}) instead of full object!`,
64
+ );
65
+ }
71
66
 
72
- const { id, meta, ...blockWithoutIdAndMeta } = block;
67
+ const { id, meta, ...blockWithoutIdAndMeta } = block;
73
68
 
74
- return blockWithoutIdAndMeta;
75
- },
76
- );
69
+ return blockWithoutIdAndMeta;
70
+ },
71
+ );
77
72
  }
78
73
 
79
- return newBlock as NewBlockInARequest<
80
- ItemTypeDefinitionToItemDefinitionAsRequest<NoInfer<D>>
81
- >;
74
+ return newBlock as NewBlockInARequest<ToItemDefinitionAsRequest<NoInfer<D>>>;
82
75
  }
@@ -0,0 +1,590 @@
1
+ import {
2
+ type TreeNode,
3
+ formatAsTree,
4
+ inspectionTreeNodes,
5
+ } from 'datocms-structured-text-utils';
6
+ import {
7
+ type BlockItemInARequest,
8
+ type BooleanFieldValue,
9
+ type ColorFieldValue,
10
+ type DateFieldValue,
11
+ type DateTimeFieldValue,
12
+ type FileFieldValue,
13
+ type FileFieldValueAsRequest,
14
+ type FloatFieldValue,
15
+ type GalleryFieldValue,
16
+ type GalleryFieldValueAsRequest,
17
+ type IntegerFieldValue,
18
+ type JsonFieldValue,
19
+ type LatLonFieldValue,
20
+ type LinkFieldValue,
21
+ type LinksFieldValue,
22
+ type NewBlockInARequest,
23
+ type RichTextFieldValue,
24
+ type RichTextFieldValueAsRequest,
25
+ type RichTextFieldValueWithNestedBlocks,
26
+ type SeoFieldValue,
27
+ type SingleBlockFieldValue,
28
+ type SingleBlockFieldValueAsRequest,
29
+ type SingleBlockFieldValueWithNestedBlocks,
30
+ type SlugFieldValue,
31
+ type StringFieldValue,
32
+ type StructuredTextFieldValue,
33
+ type StructuredTextFieldValueAsRequest,
34
+ type StructuredTextFieldValueWithNestedBlocks,
35
+ type TextFieldValue,
36
+ type UpdatedBlockInARequest,
37
+ type VideoFieldValue,
38
+ isBooleanFieldValue,
39
+ isColorFieldValue,
40
+ isDateFieldValue,
41
+ isDateTimeFieldValue,
42
+ isFileFieldValue,
43
+ isFileFieldValueAsRequest,
44
+ isFloatFieldValue,
45
+ isGalleryFieldValue,
46
+ isGalleryFieldValueAsRequest,
47
+ isIntegerFieldValue,
48
+ isJsonFieldValue,
49
+ isLatLonFieldValue,
50
+ isLinkFieldValue,
51
+ isLinksFieldValue,
52
+ isRichTextFieldValue,
53
+ isRichTextFieldValueAsRequest,
54
+ isRichTextFieldValueWithNestedBlocks,
55
+ isSeoFieldValue,
56
+ isSingleBlockFieldValue,
57
+ isSingleBlockFieldValueAsRequest,
58
+ isSingleBlockFieldValueWithNestedBlocks,
59
+ isSlugFieldValue,
60
+ isStringFieldValue,
61
+ isStructuredTextFieldValue,
62
+ isStructuredTextFieldValueAsRequest,
63
+ isStructuredTextFieldValueWithNestedBlocks,
64
+ isTextFieldValue,
65
+ isVideoFieldValue,
66
+ } from '../fieldTypes';
67
+
68
+ import { type ApiTypes, type RawApiTypes, buildBlockRecord } from '../index';
69
+ import type { LocalizedFieldValue } from './normalizedFieldValues';
70
+ import { isLocalizedFieldValue } from './normalizedFieldValues';
71
+
72
+ type Item =
73
+ | ApiTypes.Item
74
+ | RawApiTypes.Item
75
+ | NewBlockInARequest
76
+ | UpdatedBlockInARequest
77
+ | RawApiTypes.ItemCreateSchema
78
+ | RawApiTypes.ItemUpdateSchema
79
+ | ApiTypes.ItemCreateSchema
80
+ | ApiTypes.ItemUpdateSchema;
81
+
82
+ interface FieldTypeHandler {
83
+ guard: (value: unknown) => boolean;
84
+ inspect: (
85
+ value: any,
86
+ options?: InspectItemOptions,
87
+ ) => string | TreeNode | TreeNode[];
88
+ }
89
+
90
+ const fieldTypeHandlers: FieldTypeHandler[] = [
91
+ { guard: isBooleanFieldValue, inspect: booleanInspectionTreeNodes },
92
+ { guard: isFloatFieldValue, inspect: floatInspectionTreeNodes },
93
+ { guard: isIntegerFieldValue, inspect: integerInspectionTreeNodes },
94
+ { guard: isColorFieldValue, inspect: colorInspectionTreeNodes },
95
+ { guard: isDateFieldValue, inspect: dateInspectionTreeNodes },
96
+ { guard: isDateTimeFieldValue, inspect: dateTimeInspectionTreeNodes },
97
+ { guard: isGalleryFieldValue, inspect: galleryInspectionTreeNodes },
98
+ { guard: isGalleryFieldValueAsRequest, inspect: galleryInspectionTreeNodes },
99
+ { guard: isFileFieldValue, inspect: fileInspectionTreeNodes },
100
+ { guard: isFileFieldValueAsRequest, inspect: fileInspectionTreeNodes },
101
+ { guard: isJsonFieldValue, inspect: jsonInspectionTreeNodes },
102
+ { guard: isSlugFieldValue, inspect: slugInspectionTreeNodes },
103
+ { guard: isStringFieldValue, inspect: stringInspectionTreeNodes },
104
+ { guard: isTextFieldValue, inspect: textInspectionTreeNodes },
105
+ { guard: isLatLonFieldValue, inspect: latLonInspectionTreeNodes },
106
+ { guard: isLinkFieldValue, inspect: linkInspectionTreeNodes },
107
+ { guard: isLinksFieldValue, inspect: linksInspectionTreeNodes },
108
+ { guard: isSeoFieldValue, inspect: seoFieldInspectionTreeNodes },
109
+ { guard: isVideoFieldValue, inspect: videoFieldInspectionTreeNodes },
110
+ {
111
+ guard: isStructuredTextFieldValue,
112
+ inspect: structuredTextInspectionTreeNodes,
113
+ },
114
+ {
115
+ guard: isStructuredTextFieldValueAsRequest,
116
+ inspect: structuredTextInspectionTreeNodes,
117
+ },
118
+ {
119
+ guard: isStructuredTextFieldValueWithNestedBlocks,
120
+ inspect: structuredTextInspectionTreeNodes,
121
+ },
122
+ { guard: isSingleBlockFieldValue, inspect: singleBlockInspectionTreeNodes },
123
+ {
124
+ guard: isSingleBlockFieldValueAsRequest,
125
+ inspect: singleBlockInspectionTreeNodes,
126
+ },
127
+ {
128
+ guard: isSingleBlockFieldValueWithNestedBlocks,
129
+ inspect: singleBlockInspectionTreeNodes,
130
+ },
131
+ { guard: isRichTextFieldValue, inspect: richTextInspectionTreeNodes },
132
+ {
133
+ guard: isRichTextFieldValueAsRequest,
134
+ inspect: richTextInspectionTreeNodes,
135
+ },
136
+ {
137
+ guard: isRichTextFieldValueWithNestedBlocks,
138
+ inspect: richTextInspectionTreeNodes,
139
+ },
140
+ ];
141
+
142
+ function createChildNode(
143
+ attributeName: string,
144
+ inspectResult: string | TreeNode | TreeNode[],
145
+ ): TreeNode {
146
+ if (typeof inspectResult === 'string') {
147
+ return { label: `${attributeName}: ${inspectResult}` };
148
+ }
149
+
150
+ if (Array.isArray(inspectResult)) {
151
+ return {
152
+ label: attributeName,
153
+ nodes: inspectResult,
154
+ };
155
+ }
156
+
157
+ // Single TreeNode
158
+ return {
159
+ label: attributeName,
160
+ nodes: [inspectResult],
161
+ };
162
+ }
163
+
164
+ function inspectFieldValue(
165
+ value: unknown,
166
+ options?: InspectItemOptions,
167
+ ): string | TreeNode | TreeNode[] {
168
+ if (isLocalizedFieldValue(value)) {
169
+ for (const handler of fieldTypeHandlers) {
170
+ if (
171
+ Object.values(value).every((localeValue) => handler.guard(localeValue))
172
+ ) {
173
+ const localeEntries = Object.entries(value).sort(([a], [b]) =>
174
+ a.localeCompare(b),
175
+ );
176
+
177
+ return localeEntries.map(([locale, localeValue]) => {
178
+ const inspectResult = handler.inspect(localeValue, options);
179
+
180
+ if (typeof inspectResult === 'string') {
181
+ return { label: `${locale}: ${inspectResult}` };
182
+ }
183
+
184
+ if (Array.isArray(inspectResult)) {
185
+ return {
186
+ label: locale,
187
+ nodes: inspectResult,
188
+ };
189
+ }
190
+
191
+ // Single TreeNode
192
+ return {
193
+ label: locale,
194
+ nodes: [inspectResult],
195
+ };
196
+ });
197
+ }
198
+ }
199
+ }
200
+
201
+ for (const handler of fieldTypeHandlers) {
202
+ if (handler.guard(value)) {
203
+ return handler.inspect(value, options);
204
+ }
205
+ }
206
+
207
+ // Fallback for unknown field types
208
+ return `UNKNOWN: ${JSON.stringify(value)}`;
209
+ }
210
+
211
+ function extractAttributes(item: Item): Record<string, unknown> {
212
+ if ('attributes' in item) {
213
+ return item.attributes as Record<string, unknown>;
214
+ }
215
+
216
+ const serializedItem = buildBlockRecord(item);
217
+ const { __itemTypeId, ...rest } = serializedItem.attributes;
218
+ return rest;
219
+ }
220
+
221
+ export type InspectItemOptions = {
222
+ maxWidth?: number;
223
+ };
224
+
225
+ export function inspectItem(item: Item, options?: InspectItemOptions): string {
226
+ return formatAsTree(itemInspectionTreeNodes(item, options));
227
+ }
228
+
229
+ function itemInspectionTreeNodes(
230
+ item: Item,
231
+ options?: InspectItemOptions,
232
+ ): TreeNode {
233
+ let itemTypeId: string | undefined;
234
+
235
+ if ('relationships' in item && item.relationships) {
236
+ const relationships = item.relationships as any;
237
+ if (relationships.item_type?.data?.id) {
238
+ itemTypeId = relationships.item_type.data.id;
239
+ }
240
+ }
241
+
242
+ const rootLabel = [
243
+ 'Item',
244
+ 'id' in item ? JSON.stringify(item.id) : null,
245
+ itemTypeId ? `(item_type: ${JSON.stringify(itemTypeId)})` : null,
246
+ ]
247
+ .filter(Boolean)
248
+ .join(' ');
249
+
250
+ const nodes: TreeNode[] = [];
251
+
252
+ for (const [attributeName, attributeValue] of Object.entries(
253
+ extractAttributes(item),
254
+ )) {
255
+ const inspectResult = inspectFieldValue(attributeValue, options);
256
+ nodes.push(createChildNode(attributeName, inspectResult));
257
+ }
258
+
259
+ return {
260
+ label: rootLabel,
261
+ nodes: nodes.length > 0 ? nodes : undefined,
262
+ };
263
+ }
264
+
265
+ function structuredTextInspectionTreeNodes(
266
+ value:
267
+ | StructuredTextFieldValue
268
+ | StructuredTextFieldValueAsRequest
269
+ | StructuredTextFieldValueWithNestedBlocks,
270
+ options?: InspectItemOptions,
271
+ ) {
272
+ if (value === null) return 'null';
273
+
274
+ return inspectionTreeNodes<BlockItemInARequest, BlockItemInARequest>(value, {
275
+ maxWidth: options?.maxWidth || 80,
276
+ blockFormatter: (block, maxWidth) => {
277
+ if (typeof block === 'string') {
278
+ return JSON.stringify(block);
279
+ }
280
+
281
+ return itemInspectionTreeNodes(block, options);
282
+ },
283
+ });
284
+ }
285
+
286
+ function singleBlockInspectionTreeNodes(
287
+ value:
288
+ | SingleBlockFieldValue
289
+ | SingleBlockFieldValueAsRequest
290
+ | SingleBlockFieldValueWithNestedBlocks,
291
+ options?: InspectItemOptions,
292
+ ) {
293
+ if (value === null) return 'null';
294
+ if (typeof value === 'string') return JSON.stringify(value);
295
+ return itemInspectionTreeNodes(value, options);
296
+ }
297
+
298
+ function richTextInspectionTreeNodes(
299
+ value:
300
+ | RichTextFieldValue
301
+ | RichTextFieldValueAsRequest
302
+ | RichTextFieldValueWithNestedBlocks,
303
+ options?: InspectItemOptions,
304
+ ): string | TreeNode[] {
305
+ if (value === null) return 'null';
306
+
307
+ return value.map<TreeNode>((item, index) => {
308
+ const result = singleBlockInspectionTreeNodes(item, options);
309
+ if (typeof result === 'string') {
310
+ return { label: `[${index}] ${result}` };
311
+ }
312
+ result.label = `[${index}] ${result.label}`;
313
+ return result;
314
+ });
315
+ }
316
+
317
+ function booleanInspectionTreeNodes(value: BooleanFieldValue): string {
318
+ return JSON.stringify(value);
319
+ }
320
+
321
+ function colorInspectionTreeNodes(value: ColorFieldValue): string {
322
+ if (value === null) return 'null';
323
+
324
+ const { red, green, blue, alpha } = value;
325
+ const hex = `#${red.toString(16).padStart(2, '0').toUpperCase()}${green.toString(16).padStart(2, '0').toUpperCase()}${blue.toString(16).padStart(2, '0').toUpperCase()}`;
326
+
327
+ if (alpha === 255) {
328
+ return hex;
329
+ }
330
+ const alphaPercent = Math.round((alpha / 255) * 100);
331
+ return `${hex} ${alphaPercent}%`;
332
+ }
333
+
334
+ function dateInspectionTreeNodes(value: DateFieldValue): string {
335
+ if (value === null) return 'null';
336
+ return value;
337
+ }
338
+
339
+ function dateTimeInspectionTreeNodes(value: DateTimeFieldValue): string {
340
+ if (value === null) return 'null';
341
+ return value;
342
+ }
343
+
344
+ function fileInspectionTreeNodes(
345
+ value: FileFieldValue | FileFieldValueAsRequest,
346
+ options?: InspectItemOptions,
347
+ ): string | TreeNode[] {
348
+ if (value === null) return 'null';
349
+
350
+ const nodes: TreeNode[] = [];
351
+
352
+ nodes.push({ label: `upload_id: ${JSON.stringify(value.upload_id)}` });
353
+
354
+ if (value.alt) {
355
+ nodes.push({
356
+ label: `alt: ${JSON.stringify(truncate(value.alt, options?.maxWidth || 80))}`,
357
+ });
358
+ }
359
+
360
+ if (value.title) {
361
+ nodes.push({
362
+ label: `title: ${JSON.stringify(truncate(value.title, options?.maxWidth || 80))}`,
363
+ });
364
+ }
365
+
366
+ if (value.custom_data && Object.keys(value.custom_data).length > 0) {
367
+ nodes.push({
368
+ label: `custom_data: ${truncate(JSON.stringify(value.custom_data), options?.maxWidth || 80)}`,
369
+ });
370
+ }
371
+
372
+ if (value.focal_point) {
373
+ const x = value.focal_point.x * 100;
374
+ const y = value.focal_point.y * 100;
375
+ nodes.push({ label: `focal_point: x=${x}% y=${y}%` });
376
+ }
377
+
378
+ return nodes;
379
+ }
380
+
381
+ function floatInspectionTreeNodes(value: FloatFieldValue): string {
382
+ if (value === null) return 'null';
383
+ return value.toString();
384
+ }
385
+
386
+ function galleryInspectionTreeNodes(
387
+ value: GalleryFieldValue | GalleryFieldValueAsRequest,
388
+ options?: InspectItemOptions,
389
+ ): string | TreeNode[] {
390
+ if (!value || value.length === 0) return '[]';
391
+
392
+ const itemNodes: TreeNode[] = value.map((item, index) => {
393
+ const nodes: TreeNode[] = [];
394
+
395
+ nodes.push({
396
+ label: `upload_id: ${JSON.stringify(item.upload_id)}`,
397
+ });
398
+
399
+ if (item.alt) {
400
+ nodes.push({
401
+ label: `alt: ${JSON.stringify(truncate(item.alt, options?.maxWidth || 80))}`,
402
+ });
403
+ }
404
+
405
+ if (item.title) {
406
+ nodes.push({
407
+ label: `title: ${JSON.stringify(truncate(item.title, options?.maxWidth || 80))}`,
408
+ });
409
+ }
410
+
411
+ if (item.custom_data && Object.keys(item.custom_data).length > 0) {
412
+ nodes.push({
413
+ label: `custom_data: ${truncate(JSON.stringify(item.custom_data), options?.maxWidth || 80)}`,
414
+ });
415
+ }
416
+
417
+ if (item.focal_point) {
418
+ const x = item.focal_point.x * 100;
419
+ const y = item.focal_point.y * 100;
420
+ nodes.push({ label: `focal_point: x=${x}% y=${y}%` });
421
+ }
422
+
423
+ return {
424
+ label: `[${index}]`,
425
+ nodes: nodes,
426
+ };
427
+ });
428
+
429
+ return itemNodes;
430
+ }
431
+
432
+ function integerInspectionTreeNodes(value: IntegerFieldValue): string {
433
+ if (value === null) return 'null';
434
+ return value.toString();
435
+ }
436
+
437
+ function jsonInspectionTreeNodes(
438
+ value: JsonFieldValue,
439
+ options?: InspectItemOptions,
440
+ ): string {
441
+ if (value === null) return 'null';
442
+ return truncate(JSON.stringify(JSON.parse(value)), options?.maxWidth || 80);
443
+ }
444
+
445
+ function latLonInspectionTreeNodes(
446
+ value: LatLonFieldValue,
447
+ ): string | TreeNode[] {
448
+ if (value === null) return 'null';
449
+
450
+ return [
451
+ { label: `latitude: ${value.latitude}` },
452
+ { label: `longitude: ${value.longitude}` },
453
+ ];
454
+ }
455
+
456
+ function linkInspectionTreeNodes(value: LinkFieldValue): string {
457
+ if (value === null) return 'null';
458
+ return JSON.stringify(value);
459
+ }
460
+
461
+ function linksInspectionTreeNodes(value: LinksFieldValue): string | TreeNode[] {
462
+ if (value === null) return 'null';
463
+
464
+ return value.map<TreeNode>((itemId, index) => ({
465
+ label: `[${index}]: ${JSON.stringify(itemId)}`,
466
+ }));
467
+ }
468
+
469
+ function seoFieldInspectionTreeNodes(
470
+ value: SeoFieldValue,
471
+ options?: InspectItemOptions,
472
+ ): string | TreeNode[] {
473
+ if (value === null) return 'null';
474
+
475
+ const nodes: TreeNode[] = [];
476
+
477
+ if (value.title) {
478
+ nodes.push({
479
+ label: `title: ${JSON.stringify(truncate(value.title, options?.maxWidth || 80))}`,
480
+ });
481
+ }
482
+
483
+ if (value.description) {
484
+ nodes.push({
485
+ label: `description: ${JSON.stringify(truncate(value.description, options?.maxWidth || 80))}`,
486
+ });
487
+ }
488
+
489
+ if (value.image) {
490
+ nodes.push({
491
+ label: `image: ${value.image}`,
492
+ });
493
+ }
494
+
495
+ if (value.twitter_card) {
496
+ nodes.push({
497
+ label: `twitter_card: ${value.twitter_card}`,
498
+ });
499
+ }
500
+
501
+ if (value.no_index !== undefined) {
502
+ nodes.push({
503
+ label: `no_index: ${value.no_index}`,
504
+ });
505
+ }
506
+
507
+ return nodes;
508
+ }
509
+
510
+ function slugInspectionTreeNodes(
511
+ value: SlugFieldValue,
512
+ options?: InspectItemOptions,
513
+ ): string {
514
+ if (value === null) return 'null';
515
+ return JSON.stringify(truncate(value, options?.maxWidth || 80));
516
+ }
517
+
518
+ function stringInspectionTreeNodes(
519
+ value: StringFieldValue,
520
+ options?: InspectItemOptions,
521
+ ): string {
522
+ if (value === null) return 'null';
523
+ return JSON.stringify(truncate(value, options?.maxWidth || 80));
524
+ }
525
+
526
+ function textInspectionTreeNodes(
527
+ value: TextFieldValue,
528
+ options?: InspectItemOptions,
529
+ ): string {
530
+ if (value === null) return 'null';
531
+ return JSON.stringify(truncate(value, options?.maxWidth || 80));
532
+ }
533
+
534
+ function videoFieldInspectionTreeNodes(
535
+ value: VideoFieldValue,
536
+ options?: InspectItemOptions,
537
+ ): string | TreeNode[] {
538
+ if (value === null) return 'null';
539
+
540
+ const nodes: TreeNode[] = [];
541
+
542
+ nodes.push({ label: `provider: ${value.provider}` });
543
+ nodes.push({ label: `provider_uid: ${value.provider_uid}` });
544
+ nodes.push({ label: `url: ${value.url}` });
545
+ nodes.push({ label: `size: ${value.width}⨯${value.height}px` });
546
+ nodes.push({ label: `thumbnail_url: ${value.thumbnail_url}` });
547
+
548
+ if (value.title) {
549
+ nodes.push({
550
+ label: `title: ${JSON.stringify(truncate(value.title, options?.maxWidth || 80))}`,
551
+ });
552
+ }
553
+
554
+ return nodes;
555
+ }
556
+
557
+ function localizedFieldValueInspectionTreeNodes(
558
+ value: LocalizedFieldValue<unknown>,
559
+ options?: InspectItemOptions,
560
+ ): TreeNode[] {
561
+ const localeEntries = Object.entries(value).sort(([a], [b]) =>
562
+ a.localeCompare(b),
563
+ );
564
+
565
+ return localeEntries.map(([locale, localeValue]) => {
566
+ const inspectResult = inspectFieldValue(localeValue, options);
567
+
568
+ if (typeof inspectResult === 'string') {
569
+ return { label: `${locale}: ${inspectResult}` };
570
+ }
571
+
572
+ if (Array.isArray(inspectResult)) {
573
+ return {
574
+ label: locale,
575
+ nodes: inspectResult,
576
+ };
577
+ }
578
+
579
+ // Single TreeNode
580
+ return {
581
+ label: locale,
582
+ nodes: [inspectResult],
583
+ };
584
+ });
585
+ }
586
+
587
+ function truncate(text: string, maxWidth: number): string {
588
+ if (text.length <= maxWidth) return text;
589
+ return `${text.slice(0, maxWidth - 3)}...`;
590
+ }