@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 @@
1
+ export type SharedSliceContentField = "primary" | "items"
@@ -0,0 +1,65 @@
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 { UUID } from "../../../../../../common/UUID"
6
+ import { NonEmptyString } from "../../../../../../validators"
7
+
8
+ export type SharedSliceId = `${string}$${UUID}`
9
+
10
+ const baseErrorText =
11
+ "Slice id must be a non empty string matching the following pattern 'slice_type$uuid'"
12
+
13
+ export const SharedSliceIdValidationError = {
14
+ Base: baseErrorText,
15
+ IncorrectUUID: (uuid: string | undefined) =>
16
+ `Incorrect UUID: '${uuid}' - ${baseErrorText}`,
17
+ IncorrectSliceType: (sliceType: string | undefined) =>
18
+ `Incorrect slice_type: '${sliceType}' - ${baseErrorText}`,
19
+ } as const
20
+
21
+ export const SharedSliceId = new t.Type<SharedSliceId, string, unknown>(
22
+ "SharedSliceId",
23
+ (u): u is SharedSliceId => {
24
+ if (typeof u !== "string") {
25
+ return false
26
+ }
27
+ const [sliceName, uuid] = u.split("$")
28
+ return NonEmptyString.is(sliceName) && UUID.is(uuid)
29
+ },
30
+ (u, c) =>
31
+ pipe(
32
+ NonEmptyString.validate(u, c),
33
+ E.mapLeft((errors) =>
34
+ errors.map(
35
+ (error): t.ValidationError => ({
36
+ ...error,
37
+ message: SharedSliceIdValidationError.Base,
38
+ }),
39
+ ),
40
+ ),
41
+ E.chain((sliceId) => {
42
+ const [sliceName, uuid] = sliceId.split("$")
43
+ return pipe(
44
+ t.tuple([NonEmptyString, UUID]).validate([sliceName, uuid], c),
45
+ E.mapLeft((errors) =>
46
+ errors.map((error): t.ValidationError => {
47
+ const tupleErrorContext = error.context[error.context.length - 1]
48
+ return {
49
+ ...error,
50
+ context: [
51
+ ...error.context.slice(0, -1), // We don't want the tuple decoder error context
52
+ ],
53
+ message:
54
+ tupleErrorContext?.key === "0"
55
+ ? SharedSliceIdValidationError.IncorrectSliceType(sliceName)
56
+ : SharedSliceIdValidationError.IncorrectUUID(uuid),
57
+ }
58
+ }),
59
+ ),
60
+ )
61
+ }),
62
+ E.map(([sliceName, uuid]) => `${sliceName}$${uuid}`),
63
+ ),
64
+ (sliceId) => sliceId,
65
+ )
@@ -0,0 +1,45 @@
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 { SharedSlice as SharedSliceCustomType } from "../../../../../../customtypes"
6
+ import { NonEmptyString } from "../../../../../../validators"
7
+ import { findSlice } from "../utils"
8
+
9
+ export type SharedSliceType = {
10
+ slice_type: string
11
+ data: SharedSliceCustomType
12
+ }
13
+
14
+ /**
15
+ * slice_type - the validated slice_type
16
+ * slice - SharedSlice custom type data matching the slice_type. We return it alongside the validated slice_type, because it is needed to decode the 'variation' field in the SharedSlice (see SharedSliceVariation.ts)
17
+ */
18
+ const SharedSliceTypeShape = t.strict({
19
+ slice_type: NonEmptyString,
20
+ slice: SharedSliceCustomType,
21
+ })
22
+
23
+ /**
24
+ * Builds the decoder for the 'slice_type' field in the SharedSlice. Alongside the decoded 'slice_type' it also returns the SharedSlice data matching the slice_type.
25
+ * @param slices all the SharedSlices from the custom type
26
+ */
27
+ export const SharedSliceType = (slices: SharedSliceCustomType[]) =>
28
+ new t.Type<SharedSliceType, string, unknown>(
29
+ "slice_type",
30
+ (u): u is SharedSliceType => SharedSliceTypeShape.is(u),
31
+ (u, c) =>
32
+ pipe(
33
+ NonEmptyString.validate(u, c),
34
+ E.chain((slice_type) =>
35
+ pipe(
36
+ findSlice(slices)(slice_type),
37
+ E.fold(
38
+ (err) => t.failure(u, c, err),
39
+ (data) => t.success({ slice_type, data }),
40
+ ),
41
+ ),
42
+ ),
43
+ ),
44
+ ({ slice_type }) => slice_type,
45
+ )
@@ -0,0 +1,45 @@
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 { SharedSlice, Variation } from "../../../../../../customtypes"
6
+ import { NonEmptyString } from "../../../../../../validators"
7
+ import { findSliceVariation } from "../utils"
8
+
9
+ export type SharedSliceVariation = {
10
+ variation: string
11
+ data: Variation
12
+ }
13
+
14
+ /**
15
+ * variation - the validated variation of the slice
16
+ * data - Variation data matching the variation. We return it alongside the validated variation, because it is needed to decode the 'primary' and 'items' fields in the SharedSlice.
17
+ */
18
+ const SharedSliceVariationShape = t.strict({
19
+ variation: NonEmptyString,
20
+ data: Variation,
21
+ })
22
+
23
+ /**
24
+ * Builds the decoder for the 'variation' field in the SharedSlice. Alongside the decoded 'variation' it also returns the Variation data matching the variation custom type.
25
+ * @param slice SharedSlice from the custom type
26
+ */
27
+ export const SharedSliceVariation = (slice: SharedSlice) =>
28
+ new t.Type<SharedSliceVariation, string, unknown>(
29
+ "variation",
30
+ (u): u is SharedSliceVariation => SharedSliceVariationShape.is(u),
31
+ (u, c) =>
32
+ pipe(
33
+ NonEmptyString.validate(u, c),
34
+ E.chain((variation) =>
35
+ pipe(
36
+ findSliceVariation(slice)(variation),
37
+ E.fold(
38
+ (err) => t.failure(u, c, err),
39
+ (data) => t.success({ variation, data }),
40
+ ),
41
+ ),
42
+ ),
43
+ ),
44
+ ({ variation }) => variation,
45
+ )
@@ -0,0 +1,5 @@
1
+ export * from "./OptionalSharedSliceId"
2
+ export * from "./SharedSliceContent"
3
+ export * from "./SharedSliceId"
4
+ export * from "./SharedSliceType"
5
+ export * from "./SharedSliceVariation"
@@ -0,0 +1,3 @@
1
+ export * from "./errors"
2
+ export * from "./fields"
3
+ export * from "./SharedSlice"
@@ -0,0 +1,21 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
3
+
4
+ import type { SharedSlice as SharedSliceCustomType } from "../../../../../customtypes"
5
+ import type { Variation } from "../../../../../customtypes"
6
+ import { SharedSliceErrors } from "./errors"
7
+
8
+ export const findSlice =
9
+ (slices: SharedSliceCustomType[]) => (sliceId: string) =>
10
+ pipe(
11
+ slices.find((slice) => slice.id === sliceId),
12
+ E.fromNullable(SharedSliceErrors.SliceTypeNotFound(sliceId)),
13
+ )
14
+
15
+ export const findSliceVariation =
16
+ (slice: SharedSliceCustomType) =>
17
+ (variation: string): E.Either<string, Variation> =>
18
+ pipe(
19
+ slice.variations.find((v) => v.id === variation),
20
+ E.fromNullable(SharedSliceErrors.VariationNotFound(variation, slice.id)),
21
+ )
@@ -0,0 +1 @@
1
+ export * from "./ImportSlices"
@@ -0,0 +1,43 @@
1
+ import * as A from "fp-ts/Array"
2
+ import { pipe } from "fp-ts/function"
3
+ import * as O from "fp-ts/Option"
4
+
5
+ import {
6
+ SharedSlice as SharedSliceCustomType,
7
+ StaticSlices,
8
+ } from "../../../../customtypes"
9
+ import type { SharedSlice, SharedSliceId } from "./SharedSlice"
10
+
11
+ // Extracts all supported slices from the static slices object
12
+ // For now we only support the SharedSlice, if we want to support other slices in the future, we have to add them here
13
+ export const extractSupportedSlices = (
14
+ staticSlices: StaticSlices,
15
+ ): SharedSliceCustomType[] =>
16
+ pipe(
17
+ Object.values(staticSlices.config?.choices ?? {}).map((slice) =>
18
+ pipe(SharedSliceCustomType.decode(slice), O.fromEither),
19
+ ),
20
+ A.compact,
21
+ )
22
+
23
+ export const findImportSharedSliceDuplicateIds = (
24
+ slices: SharedSlice[],
25
+ ): SharedSliceId[] => {
26
+ const duplicatesMap = slices.reduce((acc, { id }) => {
27
+ if (id) {
28
+ const currentNumOfDuplicates = acc[id] ?? 0
29
+ return {
30
+ ...acc,
31
+ [id]: acc[id] === undefined ? 0 : currentNumOfDuplicates + 1,
32
+ }
33
+ }
34
+ return acc
35
+ }, {} as Record<SharedSliceId, number>)
36
+
37
+ return Object.entries(duplicatesMap).flatMap(([id, numOfDuplicates]) => {
38
+ if (numOfDuplicates > 0) {
39
+ return [id]
40
+ }
41
+ return []
42
+ })
43
+ }
@@ -1,6 +1,6 @@
1
1
  import * as t from "io-ts"
2
2
 
3
- import { NullOrElse, String } from "../../../validators"
3
+ import { String } from "../../../validators"
4
4
  import { ImportContent } from "./ImportContent"
5
5
 
6
6
  const regex = /^[a-z0-9-_.]+$/
@@ -23,5 +23,5 @@ const uidValue = String.pipe(
23
23
  ),
24
24
  )
25
25
 
26
- export const ImportUID = ImportContent("UID", NullOrElse(uidValue))
26
+ export const ImportUID = ImportContent("UID", uidValue)
27
27
  export type ImportUID = t.TypeOf<typeof ImportUID>
@@ -1,14 +1,6 @@
1
- import * as Either from "fp-ts/lib/Either"
2
- import { pipe } from "fp-ts/lib/function"
3
1
  import * as t from "io-ts"
4
- import { withMessage } from "io-ts-types"
5
2
 
6
3
  import { EmptyObjectOrElse } from "../../../../validators"
7
- import {
8
- NumberOrNull,
9
- String,
10
- StringOrNull,
11
- } from "../../../../validators/BasicTypes"
12
4
  import { ImportContent } from "../ImportContent"
13
5
 
14
6
  type URL = string
@@ -38,49 +30,12 @@ const EmbedUrl = new t.Type<URL, URL, unknown>(
38
30
  t.identity,
39
31
  )
40
32
 
41
- const EmbedProto = t.exact(
42
- t.intersection([
43
- t.type({
44
- embed_url: EmbedUrl,
45
- type: String,
46
- }),
47
- t.partial({
48
- version: withMessage(
49
- t.union([t.string, t.number, t.null]),
50
- () => "The value must be either string, number or null",
51
- ),
52
- title: String,
53
- author_name: StringOrNull,
54
- author_url: StringOrNull,
55
- provider_name: StringOrNull,
56
- provider_url: StringOrNull,
57
- cache_age: withMessage(
58
- t.union([t.string, t.number, t.null]),
59
- () => "The value must be either string, number or null",
60
- ),
61
- thumbnail_url: StringOrNull,
62
- thumbnail_width: NumberOrNull,
63
- thumbnail_height: NumberOrNull,
64
- html: StringOrNull,
65
- }),
66
- ]),
67
- )
68
- type EmbedProto = t.TypeOf<typeof EmbedProto>
33
+ export const ImportEmbedValue = t.strict({
34
+ embed_url: EmbedUrl,
35
+ })
69
36
 
70
- type Embed = EmbedProto & {
71
- all: unknown
72
- }
73
- const embedValue = new t.Type<Embed>(
74
- "ImportEmbedValue",
75
- (u: unknown): u is Embed => EmbedProto.is(u) && "all" in u,
76
- (u: unknown) => {
77
- return pipe(
78
- EmbedProto.decode(u),
79
- Either.map((parsed) => ({ ...parsed, all: u })),
80
- )
81
- },
82
- t.identity,
37
+ export const ImportEmbed = ImportContent(
38
+ "Embed",
39
+ EmptyObjectOrElse(ImportEmbedValue),
83
40
  )
84
-
85
- export const ImportEmbed = ImportContent("Embed", EmptyObjectOrElse(embedValue))
86
41
  export type ImportEmbed = t.TypeOf<typeof ImportEmbed>
@@ -0,0 +1,21 @@
1
+ import type { TypeOf } from "io-ts"
2
+ import * as t from "io-ts"
3
+
4
+ import { EmptyObjectOrElse, NumberRange } from "../../../../validators"
5
+ import { withCustomError } from "../../../../validators/function"
6
+ import { ImportContent } from "../ImportContent"
7
+
8
+ const GeoPoint = withCustomError(
9
+ t.strict({
10
+ latitude: NumberRange(-90, 90, "latitude"),
11
+ longitude: NumberRange(-180, 180, "longitude"),
12
+ }),
13
+ () =>
14
+ "GeoPoint must be an object with the properties `latitude` and `longitude` between the given ranges",
15
+ )
16
+
17
+ export const ImportGeoPoint = ImportContent(
18
+ "GeoPoint",
19
+ EmptyObjectOrElse(GeoPoint),
20
+ )
21
+ export type ImportGeoPoint = TypeOf<typeof ImportGeoPoint>
@@ -0,0 +1,25 @@
1
+ import * as t from "io-ts"
2
+
3
+ import type { Image as ImageDefinition } from "../../../../../customtypes"
4
+ import { EmptyObject } from "../../../../../validators"
5
+ import { withCustomError } from "../../../../../validators/function"
6
+
7
+ export const defaultImportImage = (field?: ImageDefinition) => {
8
+ // If there is not thumbnails we expect an empty object
9
+ if (!field?.config?.thumbnails || !field.config.thumbnails.length)
10
+ return EmptyObject
11
+
12
+ // If there are thumbnails, we expect an object with thumbnails as empty objects
13
+ return withCustomError(
14
+ t.strict(
15
+ field.config.thumbnails.reduce(
16
+ (acc, thumbnail) => ({
17
+ ...acc,
18
+ [thumbnail.name]: EmptyObject,
19
+ }),
20
+ {},
21
+ ),
22
+ ),
23
+ () => "The value must be an object",
24
+ )
25
+ }
@@ -0,0 +1,73 @@
1
+ import { chain, map } from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
3
+ import type { Validation } from "io-ts"
4
+ import * as t from "io-ts"
5
+
6
+ import type { Image as ImageDefinition } from "../../../../../customtypes"
7
+ import { DefaultOrElse } from "../../../../../validators"
8
+ import { withCustomError } from "../../../../../validators/function"
9
+ import { ImportContent } from "../../ImportContent"
10
+ import { defaultImportImage } from "./default"
11
+ import {
12
+ mergeImageViewWithCtConstraints,
13
+ mergeThumbnailsWithCtConstraints,
14
+ } from "./merge"
15
+ import type { ImageFieldWithThumbnails } from "./model"
16
+ import { ImportImageView, ImportThumbnails } from "./validators"
17
+
18
+ /* For code clarity, the output of this validator will be simplified
19
+ *
20
+ * Input -> type ImageAPIV2 = ImportImageView & ImportThumbnails
21
+ * Output -> type ImageWithThumbnails 👇
22
+ */
23
+
24
+ export const ImageFieldCodec = (field?: ImageDefinition) =>
25
+ new t.Type<ImageFieldWithThumbnails>(
26
+ "ImageField",
27
+ (u: unknown): u is ImageFieldWithThumbnails =>
28
+ ImportImageView.is(u) &&
29
+ "thumbnails" in u &&
30
+ ImportThumbnails.is(u["thumbnails"]),
31
+ (u: unknown, ctx): Validation<ImageFieldWithThumbnails> => {
32
+ return pipe(
33
+ // Validating the higher level image
34
+ ImportImageView.validate(u, ctx),
35
+ chain((imageApiV2) => {
36
+ const { id, dimensions, edit, copyright, alt, ...thumbnails } =
37
+ imageApiV2
38
+
39
+ return pipe(
40
+ // Validating the thumbnails
41
+ ImportThumbnails.validate(thumbnails, ctx),
42
+ map((thumbnails) => ({
43
+ ...mergeImageViewWithCtConstraints(
44
+ imageApiV2,
45
+ imageApiV2,
46
+ field?.config?.constraint,
47
+ ),
48
+ thumbnails: mergeThumbnailsWithCtConstraints(
49
+ thumbnails,
50
+ imageApiV2,
51
+ field,
52
+ ),
53
+ })),
54
+ )
55
+ }),
56
+ )
57
+ },
58
+ t.identity,
59
+ )
60
+
61
+ const ImageCodec = (field?: ImageDefinition) =>
62
+ withCustomError(
63
+ DefaultOrElse<Record<never, never>, ImageFieldWithThumbnails>(
64
+ defaultImportImage(field),
65
+ )(ImageFieldCodec(field)),
66
+ () => "An image field must be an object",
67
+ )
68
+
69
+ export const ImportImage = (field?: ImageDefinition) =>
70
+ ImportContent("Image", ImageCodec(field))
71
+ export type ImportImage = t.TypeOf<ReturnType<typeof ImportImage>>
72
+
73
+ export * from "./model"
@@ -0,0 +1,64 @@
1
+ import type { Image } from "../../../../../customtypes"
2
+ import type ImageConstraint from "../../../../../customtypes/widgets/shared/ImageConstraint"
3
+ import type { ImageField } from "./model"
4
+ import type { ImportImageView, ImportThumbnails } from "./validators"
5
+
6
+ /* This function merges user input with custom type constraints.
7
+ * If a thumbnails' data are missing (`image`) then the default is used (`defaultImage`)
8
+ */
9
+ export const mergeImageViewWithCtConstraints = (
10
+ image: ImportImageView | undefined,
11
+ defaultImage: ImportImageView,
12
+ constraints: ImageConstraint | undefined,
13
+ ): ImageField => {
14
+ const background = image?.edit?.background
15
+ const width =
16
+ constraints?.width ??
17
+ image?.dimensions?.width ??
18
+ defaultImage?.dimensions?.width
19
+ const height =
20
+ constraints?.height ??
21
+ image?.dimensions?.height ??
22
+ defaultImage?.dimensions?.height
23
+ const alt = image?.alt
24
+ const copyright = image?.copyright
25
+
26
+ return {
27
+ id: image?.id ?? defaultImage?.id,
28
+ edit: {
29
+ x: image?.edit?.x ?? 0,
30
+ y: image?.edit?.y ?? 0,
31
+ zoom: image?.edit?.zoom ?? 1,
32
+ ...(background !== undefined ? { background } : {}),
33
+ },
34
+ dimensions: {
35
+ ...(width !== undefined ? { width } : {}),
36
+ ...(height !== undefined ? { height } : {}),
37
+ },
38
+ ...(alt !== undefined ? { alt } : {}),
39
+ ...(copyright !== undefined ? { copyright } : {}),
40
+ }
41
+ }
42
+
43
+ /* This function handles each thumbnail defined in the custom type.
44
+ *
45
+ * For each of them it merges the user input with custom type constraints.
46
+ *
47
+ * If a thumbnail is missing, the data from the default image is used.
48
+ */
49
+ export const mergeThumbnailsWithCtConstraints = (
50
+ thumbnails: ImportThumbnails,
51
+ defaultImage: ImportImageView,
52
+ field?: Image,
53
+ ) =>
54
+ field?.config?.thumbnails?.reduce(
55
+ (acc, thumbnail) => ({
56
+ ...acc,
57
+ [thumbnail.name]: mergeImageViewWithCtConstraints(
58
+ thumbnails[thumbnail.name],
59
+ defaultImage,
60
+ thumbnail,
61
+ ),
62
+ }),
63
+ {},
64
+ ) ?? {}
@@ -0,0 +1,18 @@
1
+ export type ImageField = {
2
+ id: string
3
+ edit: {
4
+ x: number
5
+ y: number
6
+ zoom: number
7
+ background?: string
8
+ }
9
+ dimensions: {
10
+ width?: number
11
+ height?: number
12
+ }
13
+ copyright?: string | null
14
+ alt?: string | null
15
+ }
16
+ export type ImageFieldWithThumbnails = ImageField & {
17
+ thumbnails: Record<string, ImageField>
18
+ }
@@ -0,0 +1,45 @@
1
+ import * as t from "io-ts"
2
+
3
+ import {
4
+ Number,
5
+ String,
6
+ StringOrNull,
7
+ } from "../../../../../validators/BasicTypes"
8
+ import { withCustomError } from "../../../../../validators/function"
9
+
10
+ export const ImportImageView = withCustomError(
11
+ t.intersection([
12
+ t.type({
13
+ // Image Id
14
+ id: String,
15
+ }),
16
+ t.partial({
17
+ // Dimensions here will be used if they are not defined in the custom type
18
+ dimensions: withCustomError(
19
+ t.partial({
20
+ width: Number,
21
+ height: Number,
22
+ }),
23
+ () => "The value must be an object",
24
+ ),
25
+ // Crop information
26
+ edit: withCustomError(
27
+ t.partial({
28
+ x: Number,
29
+ y: Number,
30
+ zoom: Number,
31
+ background: String,
32
+ }),
33
+ () => "The value must be an object",
34
+ ),
35
+ // Copyrights and alt are set in the Asset by default but they can be overrided
36
+ copyright: StringOrNull,
37
+ alt: StringOrNull,
38
+ }),
39
+ ]),
40
+ () => "The value must be an object",
41
+ )
42
+ export type ImportImageView = t.TypeOf<typeof ImportImageView>
43
+
44
+ export const ImportThumbnails = t.record(t.string, ImportImageView)
45
+ export type ImportThumbnails = t.TypeOf<typeof ImportThumbnails>
@@ -0,0 +1,39 @@
1
+ import * as t from "io-ts"
2
+
3
+ import { withCustomError } from "../../../../../validators/function"
4
+ import {
5
+ ImportBlockType,
6
+ ImportEmbedBlock,
7
+ ImportImageBlock,
8
+ ImportTextBlock,
9
+ } from "./blocks"
10
+
11
+ export type ImportBlock = ImportTextBlock | ImportImageBlock | ImportEmbedBlock
12
+
13
+ export const ImportBlock = (allowedBlockTypes: string[]) =>
14
+ withCustomError(
15
+ t
16
+ .type({
17
+ type: ImportBlockType(allowedBlockTypes),
18
+ })
19
+ .pipe(
20
+ new t.Type<ImportBlock, ImportBlock, { type: ImportBlockType }>(
21
+ "ImportBlock",
22
+ (u): u is ImportBlock =>
23
+ t
24
+ .union([ImportTextBlock, ImportImageBlock, ImportEmbedBlock])
25
+ .is(u),
26
+ (u, c) => {
27
+ if (u.type === "image") {
28
+ return ImportImageBlock.validate(u, c)
29
+ } else if (u.type === "embed") {
30
+ return ImportEmbedBlock.validate(u, c)
31
+ } else {
32
+ return ImportTextBlock.validate(u, c)
33
+ }
34
+ },
35
+ t.identity,
36
+ ),
37
+ ),
38
+ () => "Rich text block must be an object",
39
+ )
@@ -0,0 +1,41 @@
1
+ import * as t from "io-ts"
2
+
3
+ import type { RichText as RichTextCustomType } from "../../../../../customtypes"
4
+ import { EmptyArrayOrElse } from "../../../../../validators"
5
+ import { withCustomError } from "../../../../../validators/function"
6
+ import { ImportContent } from "../../ImportContent"
7
+ import { ImportBlock } from "./ImportBlock"
8
+
9
+ export const ImportRichText = (customType?: RichTextCustomType) =>
10
+ ImportContent(
11
+ "StructuredText",
12
+ EmptyArrayOrElse(
13
+ withCustomError(
14
+ t.array(t.unknown).pipe(
15
+ new t.Type<ImportBlock[], ImportBlock[], unknown[]>(
16
+ "RichTextField",
17
+ (u): u is ImportBlock[] => t.array(ImportBlock([])).is(u),
18
+ (u, c) => {
19
+ const single = customType?.config?.single?.split(",")
20
+ const multi = customType?.config?.multi?.split(",")
21
+
22
+ if (multi) {
23
+ return t.array(ImportBlock(multi)).validate(u, c)
24
+ } else if (single) {
25
+ return u.length > 1
26
+ ? t.failure(u, c, "This field only allows one block")
27
+ : t.array(ImportBlock(single)).validate(u, c)
28
+ } else {
29
+ throw new Error(
30
+ "Rich text config must have either a 'single' or 'multi' field defined",
31
+ )
32
+ }
33
+ },
34
+ t.identity,
35
+ ),
36
+ ),
37
+ () => "Rich text field must be an array",
38
+ ),
39
+ ),
40
+ )
41
+ export type ImportRichText = t.TypeOf<ReturnType<typeof ImportRichText>>