@prismicio/types-internal 2.2.0-alpha.2 → 2.2.0-alpha.21

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 (244) hide show
  1. package/lib/common/Asset.d.ts +16 -0
  2. package/lib/common/Asset.js +11 -0
  3. package/lib/common/Embed.d.ts +15 -0
  4. package/lib/common/Embed.js +10 -0
  5. package/lib/common/UUID.d.ts +7 -0
  6. package/lib/common/UUID.js +8 -0
  7. package/lib/common/index.d.ts +2 -0
  8. package/lib/common/index.js +2 -0
  9. package/lib/content/Document.d.ts +40 -40
  10. package/lib/content/fields/GroupContent.d.ts +12 -12
  11. package/lib/content/fields/WidgetContent.d.ts +56 -56
  12. package/lib/content/fields/nestable/NestableContent.d.ts +7 -7
  13. package/lib/content/fields/nestable/RichTextContent/Blocks.d.ts +35 -12
  14. package/lib/content/fields/nestable/RichTextContent/Blocks.js +5 -5
  15. package/lib/content/fields/nestable/RichTextContent/index.d.ts +9 -9
  16. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +14 -14
  17. package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +5 -5
  18. package/lib/content/fields/slices/Slice/RepeatableContent.js +0 -1
  19. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +14 -14
  20. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +14 -14
  21. package/lib/content/fields/slices/Slice/index.d.ts +30 -30
  22. package/lib/content/fields/slices/SliceItem.d.ts +30 -30
  23. package/lib/content/fields/slices/SlicesContent.d.ts +42 -42
  24. package/lib/customtypes/CustomType.d.ts +2 -0
  25. package/lib/customtypes/CustomType.js +8 -1
  26. package/lib/import/converters/Document.d.ts +2 -2
  27. package/lib/import/converters/Document.js +6 -6
  28. package/lib/import/converters/fields/Slices/SharedSlice.d.ts +4 -0
  29. package/lib/import/converters/fields/Slices/SharedSlice.js +19 -0
  30. package/lib/import/converters/fields/Slices/SharedSliceContent.d.ts +10 -0
  31. package/lib/import/converters/fields/Slices/SharedSliceContent.js +58 -0
  32. package/lib/import/converters/fields/Slices/Slices.d.ts +4 -0
  33. package/lib/import/converters/fields/Slices/Slices.js +16 -0
  34. package/lib/import/converters/fields/Slices/index.d.ts +1 -0
  35. package/lib/import/converters/fields/Slices/index.js +4 -0
  36. package/lib/import/converters/fields/UID.d.ts +1 -1
  37. package/lib/import/converters/fields/UID.js +0 -2
  38. package/lib/import/converters/fields/index.d.ts +1 -0
  39. package/lib/import/converters/fields/index.js +1 -0
  40. package/lib/import/converters/fields/nestable/Embed.d.ts +2 -1
  41. package/lib/import/converters/fields/nestable/Embed.js +24 -5
  42. package/lib/import/converters/fields/nestable/GeoPoint.d.ts +3 -0
  43. package/lib/import/converters/fields/nestable/GeoPoint.js +15 -0
  44. package/lib/import/converters/fields/nestable/GeooPoint.d.ts +3 -0
  45. package/lib/import/converters/fields/nestable/GeooPoint.js +15 -0
  46. package/lib/import/converters/fields/nestable/Image.d.ts +6 -18
  47. package/lib/import/converters/fields/nestable/Image.js +47 -24
  48. package/lib/import/converters/fields/nestable/Link.d.ts +3 -2
  49. package/lib/import/converters/fields/nestable/Link.js +62 -11
  50. package/lib/import/converters/fields/nestable/Nestable.d.ts +4 -3
  51. package/lib/import/converters/fields/nestable/Nestable.js +9 -3
  52. package/lib/import/converters/fields/nestable/RichText/embedBlock.d.ts +4 -0
  53. package/lib/import/converters/fields/nestable/RichText/embedBlock.js +14 -0
  54. package/lib/import/converters/fields/nestable/RichText/imageBlock.d.ts +4 -0
  55. package/lib/import/converters/fields/nestable/RichText/imageBlock.js +12 -0
  56. package/lib/import/converters/fields/nestable/RichText/index.d.ts +4 -0
  57. package/lib/import/converters/fields/nestable/RichText/index.js +22 -0
  58. package/lib/import/converters/fields/nestable/RichText/textBlock.d.ts +4 -0
  59. package/lib/import/converters/fields/nestable/RichText/textBlock.js +34 -0
  60. package/lib/import/converters/fields/nestable/RichText.d.ts +4 -0
  61. package/lib/import/converters/fields/nestable/RichText.js +55 -0
  62. package/lib/import/converters/fields/nestable/index.d.ts +3 -0
  63. package/lib/import/converters/fields/nestable/index.js +3 -0
  64. package/lib/import/validators/Document.js +4 -9
  65. package/lib/import/validators/fields/ImportField.d.ts +68 -68
  66. package/lib/import/validators/fields/ImportField.js +8 -4
  67. package/lib/import/validators/fields/ImportSlices/ImportSlices.d.ts +8 -0
  68. package/lib/import/validators/fields/ImportSlices/ImportSlices.js +29 -0
  69. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.d.ts +14 -0
  70. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +61 -0
  71. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.d.ts +4 -0
  72. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +7 -0
  73. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.d.ts +2 -0
  74. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.js +15 -0
  75. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +17 -0
  76. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +30 -0
  77. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +43 -0
  78. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +69 -0
  79. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.d.ts +4 -0
  80. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.js +6 -0
  81. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.d.ts +2 -0
  82. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.js +5 -0
  83. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.d.ts +1 -0
  84. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.js +2 -0
  85. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.d.ts +9 -0
  86. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.js +39 -0
  87. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.d.ts +11 -0
  88. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +24 -0
  89. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.d.ts +11 -0
  90. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +24 -0
  91. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.d.ts +5 -0
  92. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.js +8 -0
  93. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.d.ts +3 -0
  94. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.js +6 -0
  95. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.d.ts +339 -0
  96. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.js +11 -0
  97. package/lib/import/validators/fields/ImportSlices/index.d.ts +1 -0
  98. package/lib/import/validators/fields/ImportSlices/index.js +4 -0
  99. package/lib/import/validators/fields/ImportSlices/utils.d.ts +4 -0
  100. package/lib/import/validators/fields/ImportSlices/utils.js +35 -0
  101. package/lib/import/validators/fields/UID.d.ts +2 -2
  102. package/lib/import/validators/fields/UID.js +1 -1
  103. package/lib/import/validators/fields/nestable/Embed.d.ts +8 -22
  104. package/lib/import/validators/fields/nestable/Embed.js +5 -28
  105. package/lib/import/validators/fields/nestable/GeoPoint.d.ts +13 -0
  106. package/lib/import/validators/fields/nestable/GeoPoint.js +13 -0
  107. package/lib/import/validators/fields/nestable/Image/default.d.ts +22 -0
  108. package/lib/import/validators/fields/nestable/Image/default.js +19 -0
  109. package/lib/import/validators/fields/nestable/Image/index.d.ts +45 -0
  110. package/lib/import/validators/fields/nestable/Image/index.js +41 -0
  111. package/lib/import/validators/fields/nestable/Image/merge.d.ts +23 -0
  112. package/lib/import/validators/fields/nestable/Image/merge.js +44 -0
  113. package/lib/import/validators/fields/nestable/Image/model.d.ts +18 -0
  114. package/lib/import/validators/fields/nestable/Image/model.js +2 -0
  115. package/lib/import/validators/fields/nestable/Image/validators.d.ts +35 -0
  116. package/lib/import/validators/fields/nestable/Image/validators.js +31 -0
  117. package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.d.ts +6 -0
  118. package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.js +25 -0
  119. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.d.ts +24 -0
  120. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.js +26 -0
  121. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.d.ts +6 -0
  122. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.js +18 -0
  123. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.d.ts +10 -0
  124. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.js +11 -0
  125. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.d.ts +8 -0
  126. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.js +11 -0
  127. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.d.ts +70 -0
  128. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.js +43 -0
  129. package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.d.ts +65 -0
  130. package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.js +20 -0
  131. package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.d.ts +4 -0
  132. package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.js +7 -0
  133. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.d.ts +24 -0
  134. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.js +18 -0
  135. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.d.ts +5 -0
  136. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.js +10 -0
  137. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.d.ts +17 -0
  138. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.js +22 -0
  139. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.d.ts +2 -0
  140. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.js +5 -0
  141. package/lib/import/validators/fields/nestable/ImportRichText/index.d.ts +2 -0
  142. package/lib/import/validators/fields/nestable/ImportRichText/index.js +7 -0
  143. package/lib/import/validators/fields/nestable/Link.d.ts +33 -0
  144. package/lib/import/validators/fields/nestable/Link.js +16 -11
  145. package/lib/import/validators/fields/nestable/Nestable.d.ts +65 -62
  146. package/lib/import/validators/fields/nestable/Nestable.js +18 -6
  147. package/lib/import/validators/fields/nestable/index.d.ts +5 -1
  148. package/lib/import/validators/fields/nestable/index.js +7 -1
  149. package/lib/utils/DocumentId.d.ts +2 -0
  150. package/lib/utils/DocumentId.js +7 -0
  151. package/lib/utils/Objects.d.ts +1 -1
  152. package/lib/utils/Objects.js +1 -1
  153. package/lib/utils/io-ts.d.ts +2 -0
  154. package/lib/utils/io-ts.js +22 -0
  155. package/lib/validators/BasicTypes.d.ts +0 -1
  156. package/lib/validators/BasicTypes.js +7 -8
  157. package/lib/validators/DefaultOrElse.js +1 -1
  158. package/lib/validators/NonEmptyString.js +1 -1
  159. package/lib/validators/NumberRange.d.ts +32 -0
  160. package/lib/validators/NumberRange.js +40 -0
  161. package/lib/validators/function.d.ts +20 -0
  162. package/lib/validators/function.js +41 -1
  163. package/lib/validators/index.d.ts +1 -0
  164. package/lib/validators/index.js +3 -1
  165. package/package.json +4 -2
  166. package/src/common/Asset.ts +25 -0
  167. package/src/common/Embed.ts +22 -0
  168. package/src/common/UUID.ts +19 -0
  169. package/src/common/index.ts +2 -0
  170. package/src/content/fields/nestable/RichTextContent/Blocks.ts +1 -1
  171. package/src/content/fields/slices/Slice/RepeatableContent.ts +0 -1
  172. package/src/customtypes/CustomType.ts +13 -0
  173. package/src/import/converters/Document.ts +10 -7
  174. package/src/import/converters/fields/Slices/SharedSlice.ts +24 -0
  175. package/src/import/converters/fields/Slices/SharedSliceContent.ts +94 -0
  176. package/src/import/converters/fields/Slices/Slices.ts +20 -0
  177. package/src/import/converters/fields/Slices/index.ts +1 -0
  178. package/src/import/converters/fields/UID.ts +1 -5
  179. package/src/import/converters/fields/index.ts +1 -0
  180. package/src/import/converters/fields/nestable/Embed.ts +29 -7
  181. package/src/import/converters/fields/nestable/GeoPoint.ts +16 -0
  182. package/src/import/converters/fields/nestable/Image.ts +69 -44
  183. package/src/import/converters/fields/nestable/Link.ts +77 -0
  184. package/src/import/converters/fields/nestable/Nestable.ts +17 -6
  185. package/src/import/converters/fields/nestable/RichText/embedBlock.ts +14 -0
  186. package/src/import/converters/fields/nestable/RichText/imageBlock.ts +14 -0
  187. package/src/import/converters/fields/nestable/RichText/index.ts +23 -0
  188. package/src/import/converters/fields/nestable/RichText/textBlock.ts +35 -0
  189. package/src/import/converters/fields/nestable/index.ts +3 -0
  190. package/src/import/validators/Document.ts +4 -12
  191. package/src/import/validators/fields/ImportField.ts +12 -6
  192. package/src/import/validators/fields/ImportSlices/ImportSlices.ts +54 -0
  193. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +116 -0
  194. package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +6 -0
  195. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.ts +20 -0
  196. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +64 -0
  197. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.ts +100 -0
  198. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.ts +10 -0
  199. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.ts +2 -0
  200. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.ts +1 -0
  201. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.ts +65 -0
  202. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +45 -0
  203. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +45 -0
  204. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/index.ts +5 -0
  205. package/src/import/validators/fields/ImportSlices/SharedSlice/index.ts +3 -0
  206. package/src/import/validators/fields/ImportSlices/SharedSlice/utils.ts +21 -0
  207. package/src/import/validators/fields/ImportSlices/index.ts +1 -0
  208. package/src/import/validators/fields/ImportSlices/utils.ts +43 -0
  209. package/src/import/validators/fields/UID.ts +2 -2
  210. package/src/import/validators/fields/nestable/Embed.ts +6 -51
  211. package/src/import/validators/fields/nestable/GeoPoint.ts +21 -0
  212. package/src/import/validators/fields/nestable/Image/default.ts +25 -0
  213. package/src/import/validators/fields/nestable/Image/index.ts +73 -0
  214. package/src/import/validators/fields/nestable/Image/merge.ts +64 -0
  215. package/src/import/validators/fields/nestable/Image/model.ts +18 -0
  216. package/src/import/validators/fields/nestable/Image/validators.ts +45 -0
  217. package/src/import/validators/fields/nestable/ImportRichText/ImportBlock.ts +39 -0
  218. package/src/import/validators/fields/nestable/ImportRichText/ImportRichText.ts +41 -0
  219. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.ts +44 -0
  220. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.ts +13 -0
  221. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.ts +13 -0
  222. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.ts +56 -0
  223. package/src/import/validators/fields/nestable/ImportRichText/blocks/Span.ts +44 -0
  224. package/src/import/validators/fields/nestable/ImportRichText/blocks/index.ts +4 -0
  225. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.ts +24 -0
  226. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.ts +8 -0
  227. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.ts +26 -0
  228. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/index.ts +2 -0
  229. package/src/import/validators/fields/nestable/ImportRichText/index.ts +2 -0
  230. package/src/import/validators/fields/nestable/Link.ts +59 -0
  231. package/src/import/validators/fields/nestable/Nestable.ts +19 -4
  232. package/src/import/validators/fields/nestable/index.ts +5 -1
  233. package/src/utils/DocumentId.ts +8 -0
  234. package/src/utils/Objects.ts +2 -2
  235. package/src/utils/io-ts.ts +29 -0
  236. package/src/validators/BasicTypes.ts +13 -11
  237. package/src/validators/DefaultOrElse.ts +1 -1
  238. package/src/validators/NonEmptyString.ts +4 -5
  239. package/src/validators/NumberRange.ts +51 -0
  240. package/src/validators/function.ts +44 -0
  241. package/src/validators/index.ts +1 -0
  242. package/lib/import/validators/fields/nestable/Image.d.ts +0 -24
  243. package/lib/import/validators/fields/nestable/Image.js +0 -45
  244. package/src/import/validators/fields/nestable/Image.ts +0 -90
@@ -0,0 +1,77 @@
1
+ import type { Asset } from "../../../../common"
2
+ import { getAssetOrThrow } from "../../../../common"
3
+ import type { LinkContent } from "../../../../content"
4
+ import type {
5
+ DocumentLink,
6
+ ImportLink,
7
+ MediaLink,
8
+ WebLink,
9
+ } from "../../../validators"
10
+
11
+ const webLinkConverter = (field: WebLink): LinkContent => ({
12
+ value: {
13
+ url: field.url,
14
+ target: field.target,
15
+ __TYPE__: "ExternalLink",
16
+ },
17
+ __TYPE__: "LinkContent",
18
+ })
19
+
20
+ const documentLinkConverter = (field: DocumentLink): LinkContent => ({
21
+ value: {
22
+ id: field.id,
23
+ __TYPE__: "DocumentLink",
24
+ },
25
+ __TYPE__: "LinkContent",
26
+ })
27
+
28
+ const fileLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
29
+ value: {
30
+ id: field.id,
31
+ url: asset.origin_url,
32
+ name: asset.filename ?? "unknown.jpg",
33
+ kind: asset.kind,
34
+ size: asset.size ? String(asset.size) : "0",
35
+ __TYPE__: "FileLink",
36
+ },
37
+ __TYPE__: "LinkContent",
38
+ })
39
+
40
+ const imageLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
41
+ value: {
42
+ id: field.id,
43
+ url: asset.origin_url,
44
+ height: String(asset.height ?? 1),
45
+ width: String(asset.width ?? 1),
46
+ name: asset.filename ?? "unknown.jpg",
47
+ kind: asset.kind,
48
+ size: asset.size ? String(asset.size) : "0",
49
+ __TYPE__: "ImageLink",
50
+ },
51
+ __TYPE__: "LinkContent",
52
+ })
53
+
54
+ export const linkConverter = (
55
+ field: ImportLink["value"],
56
+ assets: Record<string, Asset | undefined>,
57
+ ): LinkContent | undefined => {
58
+ if (field === null) {
59
+ return
60
+ }
61
+
62
+ switch (field.link_type) {
63
+ case "Web":
64
+ return webLinkConverter(field)
65
+ case "Document":
66
+ return documentLinkConverter(field)
67
+ case "Media": {
68
+ const asset = getAssetOrThrow(assets)(field.id)
69
+ switch (asset.kind) {
70
+ case "all":
71
+ return fileLinkConverter(field, asset)
72
+ case "image":
73
+ return imageLinkConverter(field, asset)
74
+ }
75
+ }
76
+ }
77
+ }
@@ -1,13 +1,17 @@
1
- import type { WidgetContent } from "../../../../content"
1
+ import type { Asset } from "../../../../common"
2
+ import type { Embed } from "../../../../common/Embed"
3
+ import type { NestableContent } from "../../../../content"
2
4
  import type { ImportNestable } from "../../../validators"
3
5
  import {
4
- Asset,
5
6
  booleanConverter,
6
7
  colorConverter,
7
8
  dateConverter,
8
9
  embedConverter,
10
+ geopointConverter,
9
11
  imageConverter,
12
+ linkConverter,
10
13
  numberConverter,
14
+ richTextConverter,
11
15
  selectConverter,
12
16
  textConverter,
13
17
  timestampConverter,
@@ -15,8 +19,9 @@ import {
15
19
 
16
20
  export function convertNestableWidget(
17
21
  field: ImportNestable,
18
- images: Map<string, Asset>,
19
- ): WidgetContent | undefined {
22
+ assets: Record<Asset["id"], Asset | undefined>,
23
+ embeds: Record<string, Embed | undefined>,
24
+ ): NestableContent | undefined {
20
25
  switch (field.type) {
21
26
  case "Boolean":
22
27
  return booleanConverter(field.value)
@@ -33,9 +38,15 @@ export function convertNestableWidget(
33
38
  case "Timestamp":
34
39
  return timestampConverter(field.value)
35
40
  case "Embed":
36
- return embedConverter(field.value)
41
+ return embedConverter(field.value, embeds)
42
+ case "GeoPoint":
43
+ return geopointConverter(field.value)
44
+ case "Link":
45
+ return linkConverter(field.value, assets)
37
46
  case "Image":
38
- return imageConverter(field.value, images)
47
+ return imageConverter(field.value, assets)
48
+ case "StructuredText":
49
+ return richTextConverter(field.value, assets, embeds)
39
50
  default:
40
51
  throw new Error(
41
52
  `Unsupported type of nestable converter ${JSON.stringify(field)}`,
@@ -0,0 +1,14 @@
1
+ import type { Embed } from "../../../../../common"
2
+ import type { EmbedBlock } from "../../../../../content";
3
+ import type { ImportEmbedBlock } from "../../../../validators/fields/nestable/ImportRichText/blocks";
4
+ import { embedConverter } from "../Embed";
5
+
6
+ export function embedBlockConverter(block: ImportEmbedBlock, embeds: Record<string, Embed | undefined>): EmbedBlock {
7
+ const embedData = embedConverter(block.oembed, embeds)
8
+
9
+ if (!embedData) throw new Error("Failed to convert embed data")
10
+ return {
11
+ type: block.type,
12
+ data: embedData,
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import { Asset, getAssetOrThrow } from "../../../../../common";
2
+ import type { ImageBlock } from "../../../../../content";
3
+ import type { ImportImageBlock } from "../../../../validators/fields/nestable/ImportRichText/blocks";
4
+ import { convertImageView } from "../Image";
5
+
6
+ export function imageBlockConverter(block: ImportImageBlock, assets: Record<Asset["id"], Asset | undefined>): ImageBlock {
7
+ return {
8
+ type: block.type,
9
+ data: convertImageView<ImportImageBlock>(
10
+ block,
11
+ getAssetOrThrow(assets)(block.id)
12
+ ),
13
+ }
14
+ }
@@ -0,0 +1,23 @@
1
+ import type { Asset, Embed } from "../../../../../common"
2
+ import type { RichTextContent } from "../../../../../content"
3
+ import { RichTextContentType } from "../../../../../content"
4
+ import type { ImportRichText } from "../../../../validators"
5
+ import { embedBlockConverter } from "./embedBlock"
6
+ import { imageBlockConverter } from "./imageBlock"
7
+ import { textBlockConverter } from "./textBlock"
8
+
9
+ export const richTextConverter = (
10
+ richTextField: ImportRichText["value"],
11
+ assets: Record<Asset["id"], Asset | undefined>,
12
+ embeds: Record<string, Embed | undefined>,
13
+ ): RichTextContent | undefined => {
14
+ if (richTextField === null) return
15
+ return {
16
+ __TYPE__: RichTextContentType,
17
+ value: richTextField.map((block) => {
18
+ if (block.type === "image") return imageBlockConverter(block, assets)
19
+ else if (block.type === "embed") return embedBlockConverter(block, embeds)
20
+ return textBlockConverter(block, assets)
21
+ }),
22
+ }
23
+ }
@@ -0,0 +1,35 @@
1
+ import type { Asset } from "../../../../../common"
2
+ import type { TextBlock } from "../../../../../content"
3
+ import type { ImportTextBlock } from "../../../../validators/fields/nestable/ImportRichText/blocks"
4
+ import { linkConverter } from "../Link"
5
+
6
+ export function textBlockConverter(block: ImportTextBlock, assets: Record<Asset["id"], Asset | undefined>): TextBlock {
7
+ return {
8
+ type: block.type,
9
+ content: {
10
+ text: block.text,
11
+ ...block.spans ? { spans: spansConverter(block.spans, assets) }: {}
12
+ },
13
+ ...block.direction ? { direction: block.direction } : {},
14
+ }
15
+ }
16
+
17
+ function spansConverter(spans: NonNullable<ImportTextBlock['spans']>, assets: Record<Asset["id"], Asset | undefined>): TextBlock['content']['spans'] {
18
+ return spans.map(span => {
19
+ if (span.type === 'hyperlink') {
20
+ const { __TYPE__, ...data } = linkConverter(span.data, assets)?.value || {}
21
+ return {
22
+ type: span.type,
23
+ start: span.start,
24
+ end: span.end,
25
+ data: data
26
+ }
27
+ }
28
+
29
+ return {
30
+ type: span.type,
31
+ start: span.start,
32
+ end: span.end
33
+ }
34
+ })
35
+ }
@@ -2,9 +2,12 @@ export * from "./Boolean"
2
2
  export * from "./Color"
3
3
  export * from "./Date"
4
4
  export * from "./Embed"
5
+ export * from "./GeoPoint"
5
6
  export * from "./Image"
7
+ export * from "./Link"
6
8
  export * from "./Nestable"
7
9
  export * from "./Number"
10
+ export * from "./RichText"
8
11
  export * from "./Select"
9
12
  export * from "./Text"
10
13
  export * from "./Timestamp"
@@ -2,27 +2,19 @@ import { either } from "fp-ts"
2
2
  import { Either, isLeft } from "fp-ts/Either"
3
3
  import { pipe } from "fp-ts/lib/function"
4
4
  import * as t from "io-ts"
5
- import { withMessage } from "io-ts-types"
6
5
 
7
6
  import type { WidgetKey } from "../../common"
8
7
  import type { StaticCustomType, StaticWidget } from "../../customtypes"
8
+ import { flattenCustomTypeFields } from "../../customtypes"
9
9
  import { isObject } from "../../utils/Objects"
10
+ import { withCustomError } from "../../validators/function"
10
11
  import { ImportField } from "./fields/ImportField"
11
12
 
12
- const rawImportDocument = withMessage(
13
- t.record(t.string, t.unknown),
13
+ const rawImportDocument = withCustomError(
14
+ t.UnknownRecord,
14
15
  () => "document is not an object",
15
16
  )
16
17
 
17
- function flattenCustomTypeFields(
18
- customType: StaticCustomType,
19
- ): Partial<Record<string, StaticWidget>> {
20
- return Object.values(customType.json).reduce((acc, tab) => ({
21
- ...acc,
22
- ...tab,
23
- }))
24
- }
25
-
26
18
  function validateField(
27
19
  document: Record<string, unknown>,
28
20
  customType: StaticCustomType,
@@ -1,20 +1,26 @@
1
1
  import type { StaticWidget } from "../../../customtypes"
2
+ import { ImportSlices } from "./ImportSlices"
2
3
  import { ImportNestable } from "./nestable"
3
- import { ImportUID } from "./UID"
4
4
 
5
- export type ImportField = ImportUID | ImportNestable
5
+ export type ImportField = ImportSlices | ImportNestable
6
6
 
7
7
  export const ImportField = {
8
- is(u: unknown): u is ImportNestable {
9
- return ImportUID.is(u) || ImportNestable.is(u)
8
+ is(u: unknown): u is ImportField {
9
+ return ImportField.is(u)
10
10
  },
11
11
  decode: (field: StaticWidget) => {
12
12
  return (content: unknown) => {
13
13
  switch (field.type) {
14
14
  case "UID":
15
- return { codec: ImportUID, result: ImportUID.decode(content) }
16
- case "Choice":
15
+ throw new Error(
16
+ `The field '${field.type}' should be put at the root level of the document`,
17
+ )
17
18
  case "Slices":
19
+ return {
20
+ codec: ImportSlices(field),
21
+ result: ImportSlices(field).decode(content),
22
+ }
23
+ case "Choice":
18
24
  case "Group":
19
25
  throw new Error(`Unsupported type of field ${field.type}`)
20
26
  default:
@@ -0,0 +1,54 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
3
+ import * as t from "io-ts"
4
+
5
+ import type { StaticSlices } from "../../../../customtypes"
6
+ import { EmptyObjectOrElse } from "../../../../validators"
7
+ import { ImportContent } from "../ImportContent"
8
+ import { SharedSlice } from "./SharedSlice"
9
+ import {
10
+ extractSupportedSlices,
11
+ findImportSharedSliceDuplicateIds,
12
+ } from "./utils"
13
+
14
+ export const ImportSlices = (staticSlices: StaticSlices) => {
15
+ const supportedSlices = extractSupportedSlices(staticSlices)
16
+
17
+ // For now we only support the SharedSlice, however if we want to support more in the future
18
+ // we would have to change the codec here to something like this: t.array(t.union([ImportSharedSlice(sharedSlices), NewSliceCodec(newSliceCustomTypes)])).
19
+ const SlicesArrayCodec = t.array(SharedSlice(supportedSlices))
20
+
21
+ return ImportContent(
22
+ "Slices",
23
+ EmptyObjectOrElse(
24
+ new t.Type<SharedSlice[]>(
25
+ "ImportSlices",
26
+ (u): u is SharedSlice[] => SlicesArrayCodec.is(u),
27
+ (u, c) => {
28
+ return pipe(
29
+ SlicesArrayCodec.validate(u, c),
30
+ E.chain((slices) => {
31
+ // This part might not make sense for all Slice types in the future, but for now we only support the SharedSlice
32
+ // In case we support more in the future, we would have to filter only the relevant type for this check
33
+ const sharedSliceDuplicates =
34
+ findImportSharedSliceDuplicateIds(slices)
35
+ if (sharedSliceDuplicates.length > 0) {
36
+ return t.failure(
37
+ slices,
38
+ [],
39
+ `Duplicate slice IDs detected: ${sharedSliceDuplicates.join(
40
+ ", ",
41
+ )}`,
42
+ )
43
+ }
44
+ return t.success(slices)
45
+ }),
46
+ )
47
+ },
48
+ t.identity,
49
+ ),
50
+ ),
51
+ )
52
+ }
53
+
54
+ export type ImportSlices = t.TypeOf<ReturnType<typeof ImportSlices>>
@@ -0,0 +1,116 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
3
+ import * as t from "io-ts"
4
+
5
+ import type { SharedSlice as SharedSliceCustomType } from "../../../../../customtypes"
6
+ import { NonEmptyString, String } from "../../../../../validators"
7
+ import { withCustomError } from "../../../../../validators/function"
8
+ import {
9
+ OptionalSharedSliceId,
10
+ SharedSliceContent,
11
+ SharedSliceId,
12
+ SharedSliceType,
13
+ SharedSliceVariation,
14
+ } from "./fields"
15
+
16
+ export type SharedSlice = {
17
+ id: SharedSliceId | null | undefined
18
+ slice_type: string
19
+ name: string
20
+ variation: string
21
+ primary: SharedSliceContent | null | undefined
22
+ items: SharedSliceContent[] | null | undefined
23
+ slice_label: string | null | undefined
24
+ version: string | null | undefined
25
+ }
26
+
27
+ const SharedSliceShape = t.strict({
28
+ id: OptionalSharedSliceId,
29
+ slice_type: NonEmptyString,
30
+ name: String,
31
+ variation: NonEmptyString,
32
+ primary: t.union([t.undefined, t.null, t.record(NonEmptyString, t.unknown)]),
33
+ items: t.union([
34
+ t.undefined,
35
+ t.null,
36
+ t.array(t.record(NonEmptyString, t.unknown)),
37
+ ]),
38
+ slice_label: t.union([t.undefined, t.null, String]),
39
+ version: t.union([t.undefined, t.null, String]),
40
+ })
41
+
42
+ export const SharedSlice = (sharedSlices: SharedSliceCustomType[]) =>
43
+ new t.Type<SharedSlice>(
44
+ "SharedSlice",
45
+ (u): u is SharedSlice => SharedSliceShape.is(u),
46
+ (u, c) =>
47
+ pipe(
48
+ // We validate the base fields that we can validate directly based on the provided 'sharedSlices'
49
+ t
50
+ .type({
51
+ id: OptionalSharedSliceId,
52
+ slice_type: SharedSliceType(sharedSlices),
53
+ slice_label: t.union([t.undefined, t.null, String]),
54
+ version: t.union([t.undefined, t.null, String]),
55
+ })
56
+ .validate(u, c),
57
+ E.chain((decoded) =>
58
+ pipe(
59
+ // We validate the 'variation' field, for which we need the SharedSlice custom type retrieved in the first step
60
+ t
61
+ .type({
62
+ variation: SharedSliceVariation(decoded.slice_type.data),
63
+ })
64
+ .validate(u, c),
65
+ E.map(({ variation }) => ({ ...decoded, variation })),
66
+ ),
67
+ ),
68
+ E.chain((decoded) =>
69
+ pipe(
70
+ // We validate the 'primary' and 'items' content fields, for which we need the Variation custom type retrieved in the previous step
71
+ t
72
+ .partial({
73
+ primary: SharedSliceContent(
74
+ decoded.slice_type.slice_type,
75
+ "primary",
76
+ decoded.variation.data.primary ?? {},
77
+ ),
78
+ // We need to use this wrapper codec instead of using t.array(SharedSliceContent) directly in order to get proper custom error messages
79
+ items: withCustomError(
80
+ t.array(
81
+ SharedSliceContent(
82
+ decoded.slice_type.slice_type,
83
+ "items",
84
+ decoded.variation.data.items ?? {},
85
+ ),
86
+ ),
87
+ () => "The value must be an array",
88
+ ),
89
+ })
90
+ .validate(u, c),
91
+ E.map(({ primary, items }) => ({ ...decoded, primary, items })),
92
+ ),
93
+ ),
94
+ E.map(
95
+ ({
96
+ id,
97
+ slice_type,
98
+ variation,
99
+ primary,
100
+ items,
101
+ slice_label,
102
+ version,
103
+ }) => ({
104
+ id,
105
+ slice_type: slice_type.slice_type,
106
+ name: slice_type.data.name,
107
+ variation: variation.variation,
108
+ primary,
109
+ items,
110
+ slice_label,
111
+ version: version ?? variation.data.version,
112
+ }),
113
+ ),
114
+ ),
115
+ t.identity,
116
+ )
@@ -0,0 +1,6 @@
1
+ export const SharedSliceErrors = {
2
+ SliceTypeNotFound: (slice_type: string) =>
3
+ `Shared slice '${slice_type}' not found in this slice zone in the document's custom type`,
4
+ VariationNotFound: (variation: string, slice_type: string) =>
5
+ `Variation '${variation}' not found for Slice '${slice_type}'`,
6
+ } as const
@@ -0,0 +1,20 @@
1
+ import * as t from "io-ts"
2
+
3
+ import { SharedSliceId } from "./SharedSliceId"
4
+
5
+ // Wrapping the 'SharedSliceId' codec in an optional codec here for the sake of error reporting.
6
+ // When using t.union([t.undefined, t.null, SharedSliceId]) we get a nested error context
7
+ // with the actual decoding error at position 2 in the context array.
8
+ // To avoid mutating the error context we create this new wrapper codec so that the error context will always have desired depth.
9
+ export const OptionalSharedSliceId = new t.Type<
10
+ SharedSliceId | null | undefined
11
+ >(
12
+ "OptionalSharedSliceId",
13
+ (u): u is SharedSliceId | null | undefined =>
14
+ u === null || u === undefined || SharedSliceId.is(u),
15
+ (u, c) => {
16
+ if (u === null || u === undefined) return t.success(u)
17
+ return SharedSliceId.validate(u, c)
18
+ },
19
+ t.identity,
20
+ )
@@ -0,0 +1,64 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/lib/function"
3
+ import * as t from "io-ts"
4
+
5
+ import { WidgetKey } from "../../../../../../../common"
6
+ import type { NestableWidget } from "../../../../../../../customtypes"
7
+ import { combineValidationResults } from "../../../../../../../utils/io-ts"
8
+ import { withCustomError } from "../../../../../../../validators/function"
9
+ import type { ImportNestable } from "../../../../nestable"
10
+ import { SharedSliceContentEntry } from "./SharedSliceContentEntry"
11
+
12
+ /**
13
+ * Represents the object in the 'primary' field or a single item in the 'items' array in the SharedSlice.
14
+ */
15
+ export type SharedSliceContent = Record<WidgetKey, ImportNestable>
16
+
17
+ /**
18
+ * Builds the decoder for a single entry in the 'primary' field or a single item in the 'items' array in the SharedSlice.
19
+ *
20
+ * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
21
+ * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
22
+ * @param sliceFieldModels models of the fields of the slice from custom type - used to know which codec to use for decoding each value of the object
23
+ * @constructor
24
+ */
25
+ export const SharedSliceContent = (
26
+ sliceName: string,
27
+ sliceContentField: "primary" | "items",
28
+ sliceFieldModels: Record<WidgetKey, NestableWidget>,
29
+ ) =>
30
+ new t.Type<SharedSliceContent, SharedSliceContent, unknown>(
31
+ "SharedSliceContent",
32
+ (u): u is SharedSliceContent => t.record(WidgetKey, t.unknown).is(u),
33
+ (u, c) =>
34
+ pipe(
35
+ // We first decode the raw content object in the shape of Record<WidgetKey, unknown>
36
+ withCustomError(
37
+ t.UnknownRecord,
38
+ () => "The value must be an object",
39
+ ).validate(u, c),
40
+ E.chain((rawContent) =>
41
+ pipe(
42
+ // We split the object into entries because there is no easy other way to decode each value with a different codec
43
+ Object.entries(rawContent).map((entry) =>
44
+ SharedSliceContentEntry(
45
+ sliceName,
46
+ sliceContentField,
47
+ sliceFieldModels,
48
+ ).validate(entry, c),
49
+ ),
50
+ // We get a validation result for each entry in the content object (t.Validation<SharedSliceContentEntry>[]),
51
+ // however we want to get a single validation result for the whole array of entries (t.Validation<SharedSliceContentEntry[]>).
52
+ combineValidationResults,
53
+ // We build the object back from entries if all decoded successfully
54
+ E.map((entries) =>
55
+ entries.reduce(
56
+ (acc, [key, value]) => ({ ...acc, [key]: value }),
57
+ {},
58
+ ),
59
+ ),
60
+ ),
61
+ ),
62
+ ),
63
+ t.identity,
64
+ )
@@ -0,0 +1,100 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
3
+ import * as t from "io-ts"
4
+
5
+ import { WidgetKey } from "../../../../../../../common"
6
+ import type { NestableWidget } from "../../../../../../../customtypes"
7
+ import { ImportNestable } from "../../../../nestable"
8
+ import { SharedSliceContentErrors } from "./errors"
9
+
10
+ export type SharedSliceContentEntry = [WidgetKey, ImportNestable]
11
+ /**
12
+ * Represents a single entry in the 'primary' field or a single entry in an item in the 'items' array in the SharedSlice. It is used to build the SharedSliceContent type from entries of the import object.
13
+ *
14
+ * For example, given the following slice:
15
+ * {
16
+ * "slice_type": "my_slice",
17
+ * "variation": "default",
18
+ * "primary": {
19
+ * "slice_text": "abc", // This is one entry
20
+ * "slice_text2": "def: // This is another entry
21
+ * },
22
+ * "items": [
23
+ * {
24
+ * "slice_number1": 0, // This is one entry
25
+ * "slice_number2": 1 // And this is yet another entry
26
+ * }
27
+ * ]
28
+ * }
29
+ *
30
+ * The 'primary' object is converted to following entries to be decoded:
31
+ * [
32
+ * ["slice_text", "abc"],
33
+ * ["slice_text2", "def"]
34
+ * ]
35
+ * and the first item in the 'items' array is converted to following entries to be decoded:
36
+ * [
37
+ * ["slice_number1", 0],
38
+ * ["slice_number2", 1]
39
+ * ]
40
+ *
41
+ * We decode them as separate entries because each of the values might require a different codec as per what is specified in the SharedSlice custom type.
42
+ * In this way we save on the decoding performance in comparison to a scenario where we would decode the whole content object as a record with a union type decoder for each value.
43
+ *
44
+ * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
45
+ * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
46
+ * @param sliceFieldModels models of the fields of the slice from custom type - used to know which codec to use for decoding each value of the object
47
+ */
48
+ export const SharedSliceContentEntry = (
49
+ sliceName: string,
50
+ sliceContentField: "primary" | "items",
51
+ sliceFieldModels: Record<WidgetKey, NestableWidget>,
52
+ ) =>
53
+ new t.Type<SharedSliceContentEntry, SharedSliceContentEntry, unknown>(
54
+ "SharedSliceContentEntry",
55
+ (u): u is SharedSliceContentEntry =>
56
+ t.tuple([WidgetKey, t.unknown]).is(u) && ImportNestable.is(u[1]),
57
+ (u, c) => {
58
+ return pipe(
59
+ t.tuple([WidgetKey, t.unknown]).validate(u, c),
60
+ E.chain(([key, content]) => {
61
+ const model: NestableWidget | undefined = sliceFieldModels[key]
62
+ if (!model) {
63
+ return t.failure(
64
+ [key, content],
65
+ [...c, { key, actual: content, type: t.unknown }],
66
+ SharedSliceContentErrors.UnknownField(
67
+ sliceName,
68
+ sliceContentField,
69
+ key,
70
+ ),
71
+ )
72
+ }
73
+ const { codec, result } = ImportNestable.decode(model)(content)
74
+
75
+ return pipe(
76
+ result,
77
+ E.map<ImportNestable, SharedSliceContentEntry>((decodedContent) => [
78
+ key,
79
+ decodedContent,
80
+ ]),
81
+ E.mapLeft(
82
+ (errors): t.Errors =>
83
+ errors.map((error) => {
84
+ const context = [
85
+ ...c,
86
+ ...error.context.filter(({ key }) => !!key), // We replace the "" key coming from ImportNestable decoder
87
+ { key, actual: content, type: codec },
88
+ ]
89
+
90
+ const updatedError: t.ValidationError = { ...error, context }
91
+
92
+ return updatedError
93
+ }),
94
+ ),
95
+ )
96
+ }),
97
+ )
98
+ },
99
+ t.identity,
100
+ )
@@ -0,0 +1,10 @@
1
+ import type { SharedSliceContentField } from "./types"
2
+
3
+ export const SharedSliceContentErrors = {
4
+ UnknownField: (
5
+ sliceName: string,
6
+ contentField: SharedSliceContentField,
7
+ fieldName: string,
8
+ ) =>
9
+ `Unsupported field '${fieldName}'for '${contentField}' content in Shared Slice '${sliceName}'`,
10
+ } as const
@@ -0,0 +1,2 @@
1
+ export * from "./errors"
2
+ export * from "./SharedSliceContent"