@prismicio/types-internal 3.16.0 → 3.16.1-alpha.0

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 (235) hide show
  1. package/lib/content/fields/RepeatableContent.d.ts +51 -123
  2. package/lib/content/fields/RepeatableContent.js +10 -14
  3. package/lib/content/fields/nestable/RichTextContent/Block.d.ts +1036 -0
  4. package/lib/content/fields/nestable/RichTextContent/Block.js +31 -0
  5. package/lib/content/fields/nestable/RichTextContent/EmbedBlock.d.ts +60 -0
  6. package/lib/content/fields/nestable/RichTextContent/EmbedBlock.js +53 -0
  7. package/lib/content/fields/nestable/RichTextContent/ImageBlock.d.ts +203 -0
  8. package/lib/content/fields/nestable/RichTextContent/ImageBlock.js +36 -0
  9. package/lib/content/fields/nestable/RichTextContent/TableBlock.d.ts +500 -0
  10. package/lib/content/fields/nestable/RichTextContent/TableBlock.js +21 -0
  11. package/lib/content/fields/nestable/RichTextContent/TextBlock.d.ts +590 -0
  12. package/lib/content/fields/nestable/RichTextContent/TextBlock.js +80 -0
  13. package/lib/scripts/release.d.ts +1 -0
  14. package/lib/scripts/release.js +34 -0
  15. package/lib/src/_internal/utils.d.ts +36 -0
  16. package/lib/src/_internal/utils.js +20 -0
  17. package/lib/src/common/Asset.d.ts +16 -0
  18. package/lib/src/common/Asset.js +11 -0
  19. package/lib/src/common/Embed.d.ts +15 -0
  20. package/lib/src/common/Embed.js +10 -0
  21. package/lib/src/common/HexaColorCode.d.ts +4 -0
  22. package/lib/src/common/HexaColorCode.js +7 -0
  23. package/lib/src/common/UUID.d.ts +6 -0
  24. package/lib/src/common/UUID.js +8 -0
  25. package/lib/src/common/WidgetKey.d.ts +3 -0
  26. package/lib/src/common/WidgetKey.js +8 -0
  27. package/lib/src/common/index.d.ts +3 -0
  28. package/lib/src/common/index.js +6 -0
  29. package/lib/src/content/Document.d.ts +11803 -0
  30. package/lib/src/content/Document.js +266 -0
  31. package/lib/src/content/LegacyContentCtx.d.ts +64 -0
  32. package/lib/src/content/LegacyContentCtx.js +130 -0
  33. package/lib/src/content/fields/EmptyContent.d.ts +16 -0
  34. package/lib/src/content/fields/EmptyContent.js +30 -0
  35. package/lib/src/content/fields/GroupContent.d.ts +42 -0
  36. package/lib/src/content/fields/GroupContent.js +208 -0
  37. package/lib/src/content/fields/UIDContent.d.ts +16 -0
  38. package/lib/src/content/fields/UIDContent.js +25 -0
  39. package/lib/src/content/fields/WidgetContent.d.ts +11749 -0
  40. package/lib/src/content/fields/WidgetContent.js +57 -0
  41. package/lib/src/content/fields/index.d.ts +6 -0
  42. package/lib/src/content/fields/index.js +9 -0
  43. package/lib/src/content/fields/nestable/BooleanContent.d.ts +18 -0
  44. package/lib/src/content/fields/nestable/BooleanContent.js +35 -0
  45. package/lib/src/content/fields/nestable/EmbedContent.d.ts +78 -0
  46. package/lib/src/content/fields/nestable/EmbedContent.js +56 -0
  47. package/lib/src/content/fields/nestable/FieldContent/ColorContent.d.ts +18 -0
  48. package/lib/src/content/fields/nestable/FieldContent/ColorContent.js +35 -0
  49. package/lib/src/content/fields/nestable/FieldContent/DateContent.d.ts +18 -0
  50. package/lib/src/content/fields/nestable/FieldContent/DateContent.js +35 -0
  51. package/lib/src/content/fields/nestable/FieldContent/NumberContent.d.ts +18 -0
  52. package/lib/src/content/fields/nestable/FieldContent/NumberContent.js +35 -0
  53. package/lib/src/content/fields/nestable/FieldContent/RangeContent.d.ts +18 -0
  54. package/lib/src/content/fields/nestable/FieldContent/RangeContent.js +35 -0
  55. package/lib/src/content/fields/nestable/FieldContent/SelectContent.d.ts +20 -0
  56. package/lib/src/content/fields/nestable/FieldContent/SelectContent.js +46 -0
  57. package/lib/src/content/fields/nestable/FieldContent/TextContent.d.ts +18 -0
  58. package/lib/src/content/fields/nestable/FieldContent/TextContent.js +35 -0
  59. package/lib/src/content/fields/nestable/FieldContent/TimestampContent.d.ts +18 -0
  60. package/lib/src/content/fields/nestable/FieldContent/TimestampContent.js +35 -0
  61. package/lib/src/content/fields/nestable/FieldContent/common.d.ts +1 -0
  62. package/lib/src/content/fields/nestable/FieldContent/common.js +4 -0
  63. package/lib/src/content/fields/nestable/FieldContent/index.d.ts +49 -0
  64. package/lib/src/content/fields/nestable/FieldContent/index.js +38 -0
  65. package/lib/src/content/fields/nestable/GeoPointContent.d.ts +33 -0
  66. package/lib/src/content/fields/nestable/GeoPointContent.js +31 -0
  67. package/lib/src/content/fields/nestable/ImageContent.d.ts +227 -0
  68. package/lib/src/content/fields/nestable/ImageContent.js +73 -0
  69. package/lib/src/content/fields/nestable/IntegrationFieldContent.d.ts +16 -0
  70. package/lib/src/content/fields/nestable/IntegrationFieldContent.js +28 -0
  71. package/lib/src/content/fields/nestable/LinkContent.d.ts +919 -0
  72. package/lib/src/content/fields/nestable/LinkContent.js +399 -0
  73. package/lib/src/content/fields/nestable/NestableContent.d.ts +1953 -0
  74. package/lib/src/content/fields/nestable/NestableContent.js +167 -0
  75. package/lib/src/content/fields/nestable/RepeatableContent.d.ts +176 -0
  76. package/lib/src/content/fields/nestable/RepeatableContent.js +104 -0
  77. package/lib/src/content/fields/nestable/RichTextContent/Blocks.d.ts +1261 -0
  78. package/lib/src/content/fields/nestable/RichTextContent/Blocks.js +170 -0
  79. package/lib/src/content/fields/nestable/RichTextContent/index.d.ts +1004 -0
  80. package/lib/src/content/fields/nestable/RichTextContent/index.js +28 -0
  81. package/lib/src/content/fields/nestable/SeparatorContent.d.ts +13 -0
  82. package/lib/src/content/fields/nestable/SeparatorContent.js +22 -0
  83. package/lib/src/content/fields/nestable/TableContent.d.ts +770 -0
  84. package/lib/src/content/fields/nestable/TableContent.js +121 -0
  85. package/lib/src/content/fields/nestable/index.d.ts +12 -0
  86. package/lib/src/content/fields/nestable/index.js +15 -0
  87. package/lib/src/content/fields/slices/Slice/CompositeSliceContent.d.ts +3934 -0
  88. package/lib/src/content/fields/slices/Slice/CompositeSliceContent.js +195 -0
  89. package/lib/src/content/fields/slices/Slice/RepeatableContent.d.ts +774 -0
  90. package/lib/src/content/fields/slices/Slice/RepeatableContent.js +21 -0
  91. package/lib/src/content/fields/slices/Slice/SharedSliceContent.d.ts +3937 -0
  92. package/lib/src/content/fields/slices/Slice/SharedSliceContent.js +215 -0
  93. package/lib/src/content/fields/slices/Slice/SimpleSliceContent.d.ts +1962 -0
  94. package/lib/src/content/fields/slices/Slice/SimpleSliceContent.js +130 -0
  95. package/lib/src/content/fields/slices/Slice/SlicePrimaryContent.d.ts +1952 -0
  96. package/lib/src/content/fields/slices/Slice/SlicePrimaryContent.js +30 -0
  97. package/lib/src/content/fields/slices/Slice/index.d.ts +6810 -0
  98. package/lib/src/content/fields/slices/Slice/index.js +45 -0
  99. package/lib/src/content/fields/slices/SliceItem.d.ts +6846 -0
  100. package/lib/src/content/fields/slices/SliceItem.js +116 -0
  101. package/lib/src/content/fields/slices/SlicesContent.d.ts +9809 -0
  102. package/lib/src/content/fields/slices/SlicesContent.js +187 -0
  103. package/lib/src/content/fields/slices/index.d.ts +3 -0
  104. package/lib/src/content/fields/slices/index.js +6 -0
  105. package/lib/src/content/fields/withDefaultValues.d.ts +9 -0
  106. package/lib/src/content/fields/withDefaultValues.js +79 -0
  107. package/lib/src/content/index.d.ts +3 -0
  108. package/lib/src/content/index.js +6 -0
  109. package/lib/src/content/utils.d.ts +14 -0
  110. package/lib/src/content/utils.js +33 -0
  111. package/lib/src/customtypes/CustomType.d.ts +3680 -0
  112. package/lib/src/customtypes/CustomType.js +184 -0
  113. package/lib/src/customtypes/Section.d.ts +3635 -0
  114. package/lib/src/customtypes/Section.js +66 -0
  115. package/lib/src/customtypes/diff/Changes.d.ts +17 -0
  116. package/lib/src/customtypes/diff/Changes.js +9 -0
  117. package/lib/src/customtypes/diff/SharedSlice.d.ts +1624 -0
  118. package/lib/src/customtypes/diff/SharedSlice.js +55 -0
  119. package/lib/src/customtypes/diff/Variation.d.ts +1605 -0
  120. package/lib/src/customtypes/diff/Variation.js +113 -0
  121. package/lib/src/customtypes/diff/Widgets.d.ts +11 -0
  122. package/lib/src/customtypes/diff/Widgets.js +2 -0
  123. package/lib/src/customtypes/diff/index.d.ts +4 -0
  124. package/lib/src/customtypes/diff/index.js +7 -0
  125. package/lib/src/customtypes/index.d.ts +4 -0
  126. package/lib/src/customtypes/index.js +7 -0
  127. package/lib/src/customtypes/widgets/Group.d.ts +1352 -0
  128. package/lib/src/customtypes/widgets/Group.js +92 -0
  129. package/lib/src/customtypes/widgets/UID.d.ts +19 -0
  130. package/lib/src/customtypes/widgets/UID.js +21 -0
  131. package/lib/src/customtypes/widgets/Widget.d.ts +4457 -0
  132. package/lib/src/customtypes/widgets/Widget.js +49 -0
  133. package/lib/src/customtypes/widgets/index.d.ts +6 -0
  134. package/lib/src/customtypes/widgets/index.js +9 -0
  135. package/lib/src/customtypes/widgets/nestable/BooleanField.d.ts +20 -0
  136. package/lib/src/customtypes/widgets/nestable/BooleanField.js +21 -0
  137. package/lib/src/customtypes/widgets/nestable/Color.d.ts +17 -0
  138. package/lib/src/customtypes/widgets/nestable/Color.js +20 -0
  139. package/lib/src/customtypes/widgets/nestable/Date.d.ts +19 -0
  140. package/lib/src/customtypes/widgets/nestable/Date.js +21 -0
  141. package/lib/src/customtypes/widgets/nestable/Embed.d.ts +19 -0
  142. package/lib/src/customtypes/widgets/nestable/Embed.js +21 -0
  143. package/lib/src/customtypes/widgets/nestable/GeoPoint.d.ts +15 -0
  144. package/lib/src/customtypes/widgets/nestable/GeoPoint.js +19 -0
  145. package/lib/src/customtypes/widgets/nestable/Image.d.ts +44 -0
  146. package/lib/src/customtypes/widgets/nestable/Image.js +29 -0
  147. package/lib/src/customtypes/widgets/nestable/IntegrationField.d.ts +19 -0
  148. package/lib/src/customtypes/widgets/nestable/IntegrationField.js +21 -0
  149. package/lib/src/customtypes/widgets/nestable/Link.d.ts +196 -0
  150. package/lib/src/customtypes/widgets/nestable/Link.js +167 -0
  151. package/lib/src/customtypes/widgets/nestable/NestableWidget.d.ts +217 -0
  152. package/lib/src/customtypes/widgets/nestable/NestableWidget.js +39 -0
  153. package/lib/src/customtypes/widgets/nestable/Number.d.ts +23 -0
  154. package/lib/src/customtypes/widgets/nestable/Number.js +24 -0
  155. package/lib/src/customtypes/widgets/nestable/Range.d.ts +23 -0
  156. package/lib/src/customtypes/widgets/nestable/Range.js +24 -0
  157. package/lib/src/customtypes/widgets/nestable/RichText.d.ts +73 -0
  158. package/lib/src/customtypes/widgets/nestable/RichText.js +131 -0
  159. package/lib/src/customtypes/widgets/nestable/Select.d.ts +21 -0
  160. package/lib/src/customtypes/widgets/nestable/Select.js +22 -0
  161. package/lib/src/customtypes/widgets/nestable/Separator.d.ts +14 -0
  162. package/lib/src/customtypes/widgets/nestable/Separator.js +18 -0
  163. package/lib/src/customtypes/widgets/nestable/Table.d.ts +21 -0
  164. package/lib/src/customtypes/widgets/nestable/Table.js +30 -0
  165. package/lib/src/customtypes/widgets/nestable/Text.d.ts +19 -0
  166. package/lib/src/customtypes/widgets/nestable/Text.js +21 -0
  167. package/lib/src/customtypes/widgets/nestable/Timestamp.d.ts +19 -0
  168. package/lib/src/customtypes/widgets/nestable/Timestamp.js +21 -0
  169. package/lib/src/customtypes/widgets/nestable/index.d.ts +17 -0
  170. package/lib/src/customtypes/widgets/nestable/index.js +20 -0
  171. package/lib/src/customtypes/widgets/shared/ImageConstraint.d.ts +7 -0
  172. package/lib/src/customtypes/widgets/shared/ImageConstraint.js +31 -0
  173. package/lib/src/customtypes/widgets/shared/index.d.ts +1 -0
  174. package/lib/src/customtypes/widgets/shared/index.js +8 -0
  175. package/lib/src/customtypes/widgets/slices/CompositeSlice.d.ts +455 -0
  176. package/lib/src/customtypes/widgets/slices/CompositeSlice.js +63 -0
  177. package/lib/src/customtypes/widgets/slices/LegacySlice.d.ts +442 -0
  178. package/lib/src/customtypes/widgets/slices/LegacySlice.js +12 -0
  179. package/lib/src/customtypes/widgets/slices/SharedSlice.d.ts +1807 -0
  180. package/lib/src/customtypes/widgets/slices/SharedSlice.js +118 -0
  181. package/lib/src/customtypes/widgets/slices/SharedSliceRef.d.ts +5 -0
  182. package/lib/src/customtypes/widgets/slices/SharedSliceRef.js +9 -0
  183. package/lib/src/customtypes/widgets/slices/Slice.d.ts +6 -0
  184. package/lib/src/customtypes/widgets/slices/Slice.js +2 -0
  185. package/lib/src/customtypes/widgets/slices/SlicePrimaryWidget.d.ts +1259 -0
  186. package/lib/src/customtypes/widgets/slices/SlicePrimaryWidget.js +10 -0
  187. package/lib/src/customtypes/widgets/slices/Slices.d.ts +5917 -0
  188. package/lib/src/customtypes/widgets/slices/Slices.js +146 -0
  189. package/lib/src/customtypes/widgets/slices/SlicesTypes.d.ts +6 -0
  190. package/lib/src/customtypes/widgets/slices/SlicesTypes.js +11 -0
  191. package/lib/src/customtypes/widgets/slices/index.d.ts +8 -0
  192. package/lib/src/customtypes/widgets/slices/index.js +11 -0
  193. package/lib/src/utils/Arrays.d.ts +1 -0
  194. package/lib/src/utils/Arrays.js +13 -0
  195. package/lib/src/utils/DocumentId.d.ts +2 -0
  196. package/lib/src/utils/DocumentId.js +7 -0
  197. package/lib/src/utils/Objects.d.ts +8 -0
  198. package/lib/src/utils/Objects.js +42 -0
  199. package/lib/src/utils/index.d.ts +3 -0
  200. package/lib/src/utils/index.js +7 -0
  201. package/lib/src/validators/BasicTypes.d.ts +10 -0
  202. package/lib/src/validators/BasicTypes.js +25 -0
  203. package/lib/src/validators/DateFromString.d.ts +3 -0
  204. package/lib/src/validators/DateFromString.js +11 -0
  205. package/lib/src/validators/DateFromStringOrNumber.d.ts +3 -0
  206. package/lib/src/validators/DateFromStringOrNumber.js +10 -0
  207. package/lib/src/validators/DateFromTsMs.d.ts +3 -0
  208. package/lib/src/validators/DateFromTsMs.js +10 -0
  209. package/lib/src/validators/DefaultOrElse.d.ts +5 -0
  210. package/lib/src/validators/DefaultOrElse.js +21 -0
  211. package/lib/src/validators/IntFromNumber.d.ts +5 -0
  212. package/lib/src/validators/IntFromNumber.js +14 -0
  213. package/lib/src/validators/IntFromPixels.d.ts +8 -0
  214. package/lib/src/validators/IntFromPixels.js +24 -0
  215. package/lib/src/validators/NonEmptyString.d.ts +3 -0
  216. package/lib/src/validators/NonEmptyString.js +6 -0
  217. package/lib/src/validators/NonEmptyStringOrNull.d.ts +3 -0
  218. package/lib/src/validators/NonEmptyStringOrNull.js +15 -0
  219. package/lib/src/validators/NumberOrNull.d.ts +3 -0
  220. package/lib/src/validators/NumberOrNull.js +6 -0
  221. package/lib/src/validators/NumberRange.d.ts +32 -0
  222. package/lib/src/validators/NumberRange.js +40 -0
  223. package/lib/src/validators/StringFromBoolean.d.ts +5 -0
  224. package/lib/src/validators/StringFromBoolean.js +10 -0
  225. package/lib/src/validators/StringFromNumber.d.ts +5 -0
  226. package/lib/src/validators/StringFromNumber.js +10 -0
  227. package/lib/src/validators/StringOrNull.d.ts +3 -0
  228. package/lib/src/validators/StringOrNull.js +6 -0
  229. package/lib/src/validators/TrimmedString.d.ts +5 -0
  230. package/lib/src/validators/TrimmedString.js +23 -0
  231. package/lib/src/validators/function.d.ts +34 -0
  232. package/lib/src/validators/function.js +100 -0
  233. package/lib/src/validators/index.d.ts +15 -0
  234. package/lib/src/validators/index.js +31 -0
  235. package/package.json +1 -1
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.migrateDocument = exports.collectWidgets = exports.traverseDocument = exports.fillDocumentWithDefaultValues = exports.DocumentLegacy = exports.Document = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fp_ts_1 = require("fp-ts");
6
+ const Either_1 = require("fp-ts/lib/Either");
7
+ const function_1 = require("fp-ts/lib/function");
8
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
9
+ const utils_1 = require("../_internal/utils");
10
+ const common_1 = require("../common");
11
+ const UUID_1 = require("../common/UUID");
12
+ const customtypes_1 = require("../customtypes");
13
+ const fields_1 = require("./fields");
14
+ const LegacyContentCtx_1 = require("./LegacyContentCtx");
15
+ exports.Document = t.record(common_1.WidgetKey, fields_1.WidgetContent);
16
+ const legacyDocReader = t.record(common_1.WidgetKey, t.unknown);
17
+ /**
18
+ * `DocumentLegacyCodec` handles decoding and encoding documents to the legacy
19
+ * format used by Prismic DB, therefore, this function itself is not "legacy".
20
+ */
21
+ const DocumentLegacyCodec = (allTypes, allKeys) => {
22
+ return new t.Type("Document", (u) => !!u && typeof u === "object", (doc) => {
23
+ return (0, function_1.pipe)(legacyDocReader.decode(doc), fp_ts_1.either.map((parsedDoc) => {
24
+ return Object.entries(parsedDoc).reduce((acc, [widgetKey, widgetValue]) => {
25
+ const widgetCtx = (0, LegacyContentCtx_1.defaultCtx)(widgetKey, allTypes, allKeys);
26
+ const parsedW = (0, fields_1.WidgetLegacy)(widgetCtx).decode(widgetValue);
27
+ if (!parsedW || (0, Either_1.isLeft)(parsedW))
28
+ return acc;
29
+ return { ...acc, [widgetKey]: parsedW.right };
30
+ }, {});
31
+ }));
32
+ }, (g) => {
33
+ return Object.entries(g).reduce((acc, [key, value]) => {
34
+ const widgetCtx = (0, LegacyContentCtx_1.defaultCtx)(key, allTypes);
35
+ const result = (0, fields_1.WidgetLegacy)(widgetCtx).encode(value);
36
+ if (!result)
37
+ return acc;
38
+ return {
39
+ content: { ...acc.content, [key]: result.content },
40
+ types: { ...acc.types, ...result.types },
41
+ keys: { ...acc.keys, ...result.keys },
42
+ };
43
+ }, { content: {}, types: {}, keys: {} });
44
+ });
45
+ };
46
+ function extractMetadata(data) {
47
+ const fields = Object.entries(data);
48
+ const { types, widgets, keys } = fields.reduce((acc, [k, v]) => {
49
+ if (k.endsWith("_TYPE")) {
50
+ const decodedValue = LegacyContentCtx_1.FieldOrSliceType.decode(v);
51
+ if ((0, Either_1.isRight)(decodedValue)) {
52
+ return {
53
+ ...acc,
54
+ types: acc.types.set(k.substring(0, k.length - 5), decodedValue.right),
55
+ };
56
+ }
57
+ }
58
+ if (k.endsWith("_KEY")) {
59
+ const decodedValue = UUID_1.UUID.decode(v);
60
+ if ((0, Either_1.isRight)(decodedValue)) {
61
+ return {
62
+ ...acc,
63
+ keys: acc.keys.set(k.substring(0, k.length - 4), decodedValue.right),
64
+ };
65
+ }
66
+ }
67
+ if (!k.endsWith("_POSITION") &&
68
+ !k.endsWith("_TYPE") &&
69
+ !k.endsWith("_KEY")) {
70
+ return {
71
+ ...acc,
72
+ widgets: {
73
+ ...acc.widgets,
74
+ [k]: v,
75
+ },
76
+ };
77
+ }
78
+ return acc;
79
+ }, {
80
+ types: new Map(),
81
+ widgets: {},
82
+ keys: new Map(),
83
+ });
84
+ const slugs = data["slugs_INTERNAL"] || [];
85
+ const uid = data["uid"];
86
+ return {
87
+ widgets,
88
+ types,
89
+ keys,
90
+ uid,
91
+ slugs,
92
+ };
93
+ }
94
+ function parseLegacyDocument(legacyDoc, customType) {
95
+ const result = (0, function_1.pipe)(
96
+ // ensure it's the right document format first
97
+ t.record(common_1.WidgetKey, t.unknown).decode(legacyDoc), fp_ts_1.either.chain((doc) => {
98
+ // extract all metadata, meaning all _TYPES keys from legacy format + the widgets as unknown
99
+ const { types, widgets, keys } = extractMetadata(doc);
100
+ // parse the actual widgets
101
+ return DocumentLegacyCodec(types, keys).decode(widgets);
102
+ }));
103
+ return (0, Either_1.isLeft)(result) ? undefined : migrateDocument(result.right, customType);
104
+ }
105
+ function encodeToLegacyDocument(document) {
106
+ const encoded = DocumentLegacyCodec().encode(document);
107
+ return { ...encoded.content, ...encoded.types, ...encoded.keys };
108
+ }
109
+ exports.DocumentLegacy = {
110
+ _codec: DocumentLegacyCodec,
111
+ extractMetadata,
112
+ parse: parseLegacyDocument,
113
+ encode: encodeToLegacyDocument,
114
+ };
115
+ function simplifyCustomType(customType) {
116
+ return {
117
+ customTypeId: customType === null || customType === void 0 ? void 0 : customType.id,
118
+ fields: Object.fromEntries((0, customtypes_1.flattenSections)(customType)),
119
+ };
120
+ }
121
+ function fillDocumentWithDefaultValues(customType, document) {
122
+ const { fields } = customType && customtypes_1.StaticCustomType.is(customType)
123
+ ? simplifyCustomType(customType)
124
+ : customType;
125
+ return Object.entries(fields).reduce((updatedDocument, [fieldKey, fieldDef]) => {
126
+ const fieldContent = document[fieldKey];
127
+ const updatedField = (() => {
128
+ switch (fieldDef.type) {
129
+ case "Group":
130
+ return (0, fields_1.isGroupContent)(fieldContent)
131
+ ? (0, fields_1.groupContentWithDefaultValues)(fieldDef, fieldContent)
132
+ : fieldContent;
133
+ case "Choice":
134
+ case "Slices":
135
+ return (0, fields_1.isSlicesContent)(fieldContent)
136
+ ? (0, fields_1.slicesContentWithDefaultValues)(fieldDef, fieldContent)
137
+ : fieldContent;
138
+ default:
139
+ return fieldContent === undefined && customtypes_1.NestableWidget.is(fieldDef)
140
+ ? (0, fields_1.NestableContentDefaultValue)(fieldDef)
141
+ : fieldContent;
142
+ }
143
+ })();
144
+ return updatedField
145
+ ? {
146
+ ...updatedDocument,
147
+ [fieldKey]: updatedField,
148
+ }
149
+ : updatedDocument;
150
+ }, document);
151
+ }
152
+ exports.fillDocumentWithDefaultValues = fillDocumentWithDefaultValues;
153
+ /**
154
+ * @param model: Can be optional if we simply want to loop through the content
155
+ * without any consideration for the attached model
156
+ * @param document: The content we actually want to iterate on in an immutable fashion
157
+ * @param transform: A user function that provides a way to transform any kind
158
+ * of content wherever it is in a structured Prismic object content.
159
+ * @returns A transformed document with the user's transformation applied with
160
+ * the transform function
161
+ */
162
+ function traverseDocument({ document, customType, }) {
163
+ const model = customType && customtypes_1.StaticCustomType.is(customType)
164
+ ? simplifyCustomType(customType)
165
+ : customType;
166
+ return ({ transformWidget = ({ content }) => content, transformSlice = ({ content }) => content, }) => {
167
+ const fieldModels = model &&
168
+ Object.entries(model.fields).reduce((acc, [key, def]) => ({ ...acc, [key]: def }), {});
169
+ return Object.entries(document).reduce((acc, [key, content]) => {
170
+ const fieldModel = fieldModels && fieldModels[key];
171
+ const path = utils_1.ContentPath.make([
172
+ { key: model === null || model === void 0 ? void 0 : model.customTypeId, type: "CustomType" },
173
+ { key, type: "Widget" },
174
+ ]);
175
+ const transformedWidget = (() => {
176
+ switch (content.__TYPE__) {
177
+ case "SliceContentType":
178
+ return (0, fields_1.traverseSlices)({
179
+ path,
180
+ key,
181
+ model: (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) === "Slices" || (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) === "Choice"
182
+ ? fieldModel
183
+ : undefined,
184
+ content,
185
+ })({ transformWidget, transformSlice });
186
+ case "GroupContentType":
187
+ return (0, fields_1.traverseGroupContent)({
188
+ path,
189
+ key,
190
+ apiId: key,
191
+ model: (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) === "Group" ? fieldModel : undefined,
192
+ content,
193
+ })(transformWidget);
194
+ case "RepeatableContent":
195
+ return (0, fields_1.traverseRepeatableContent)({
196
+ path,
197
+ key,
198
+ apiId: key,
199
+ model: (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) === "Link" ? fieldModel : undefined,
200
+ content,
201
+ })(transformWidget);
202
+ case "TableContent":
203
+ return (0, fields_1.traverseTableContent)({
204
+ path,
205
+ key,
206
+ apiId: key,
207
+ model: (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) === "Table" ? fieldModel : undefined,
208
+ content,
209
+ })(transformWidget);
210
+ default:
211
+ return transformWidget({
212
+ path,
213
+ key,
214
+ apiId: key,
215
+ model: (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) !== "Group" &&
216
+ (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) !== "Slices" &&
217
+ (fieldModel === null || fieldModel === void 0 ? void 0 : fieldModel.type) !== "Choice"
218
+ ? fieldModel
219
+ : undefined,
220
+ content,
221
+ });
222
+ }
223
+ })();
224
+ return {
225
+ ...acc,
226
+ ...(transformedWidget ? { [key]: transformedWidget } : {}),
227
+ };
228
+ }, {});
229
+ };
230
+ }
231
+ exports.traverseDocument = traverseDocument;
232
+ // /**
233
+ // * The goal is to be able to collect all widgets or slices of a given type at any level of nesting inside a prismic content
234
+ // *
235
+ // * @param document parsed prismic content
236
+ // * @param is typeguard to match specifically the type of widget we want to collect
237
+ // * @returns a record containing the path of the widget as key and the typed collected content as value
238
+ // */
239
+ function collectWidgets(document, is) {
240
+ const collected = {};
241
+ traverseDocument({ document })({
242
+ transformWidget: ({ content, path }) => {
243
+ const key = utils_1.ContentPath.serialize(path);
244
+ if (is(content, path))
245
+ collected[key] = content;
246
+ return content;
247
+ },
248
+ });
249
+ return collected;
250
+ }
251
+ exports.collectWidgets = collectWidgets;
252
+ function migrateDocument(document, customType) {
253
+ const model = customtypes_1.StaticCustomType.is(customType)
254
+ ? simplifyCustomType(customType)
255
+ : customType;
256
+ const needsMigration = Object.values((0, customtypes_1.collectSharedSlices)(model)).some((slice) => Boolean(slice.legacyPaths));
257
+ if (!needsMigration)
258
+ return document;
259
+ return traverseDocument({
260
+ document,
261
+ customType,
262
+ })({
263
+ transformSlice: fields_1.migrateSliceItem,
264
+ });
265
+ }
266
+ exports.migrateDocument = migrateDocument;
@@ -0,0 +1,64 @@
1
+ import * as t from "io-ts";
2
+ export declare const FieldOrSliceType: t.UnionC<[t.UnionC<[t.KeyofC<{
3
+ Color: null;
4
+ Boolean: null;
5
+ Number: null;
6
+ Embed: null;
7
+ GeoPoint: null;
8
+ Date: null;
9
+ Range: null;
10
+ StructuredText: null;
11
+ Select: null;
12
+ Separator: null;
13
+ Table: null;
14
+ Text: null;
15
+ Timestamp: null;
16
+ Link: null;
17
+ Image: null;
18
+ IntegrationFields: null;
19
+ UID: null;
20
+ Group: null;
21
+ Slices: null;
22
+ Choice: null;
23
+ }>, t.KeyofC<{
24
+ Slice: null;
25
+ SharedSlice: null;
26
+ }>]>, t.LiteralC<"Repeatable.Link">]>;
27
+ export declare type FieldOrSliceType = t.TypeOf<typeof FieldOrSliceType>;
28
+ interface LegacyContentCtxParams {
29
+ fieldKey: string;
30
+ contentKey?: string;
31
+ fieldPath?: Array<string>;
32
+ contentPath?: Array<string>;
33
+ allTypes?: Map<string, FieldOrSliceType>;
34
+ allKeys?: Map<string, string>;
35
+ }
36
+ export declare class LegacyContentCtx {
37
+ fieldKey: string;
38
+ prefixedKey: string;
39
+ keyOfType: string;
40
+ keyOfKey: string;
41
+ fieldPath: Array<string>;
42
+ fieldType?: FieldOrSliceType | undefined;
43
+ allTypes: Map<string, FieldOrSliceType>;
44
+ allKeys: Map<string, string>;
45
+ contentKey: string;
46
+ contentPath: Array<string>;
47
+ fieldContentKey: string;
48
+ constructor({ fieldKey, contentKey, contentPath, fieldPath, allTypes, allKeys, }: LegacyContentCtxParams);
49
+ withContentKey(contentKey: string): LegacyContentCtx;
50
+ }
51
+ interface GetFieldCtxParams {
52
+ fieldKey: string;
53
+ contentKey?: string;
54
+ ctx: LegacyContentCtx;
55
+ prefixes?: Array<string>;
56
+ }
57
+ export declare function getFieldCtx({ fieldKey, contentKey: contentKeyParam, ctx, prefixes, }: GetFieldCtxParams): LegacyContentCtx;
58
+ export declare function defaultCtx(key: string, allTypes?: Map<string, FieldOrSliceType>, allKeys?: Map<string, string>): LegacyContentCtx;
59
+ export declare type WithTypes<T> = {
60
+ keys: Record<string, string>;
61
+ types: Record<string, FieldOrSliceType>;
62
+ content: T;
63
+ };
64
+ export {};
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultCtx = exports.getFieldCtx = exports.LegacyContentCtx = exports.FieldOrSliceType = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const uuid_1 = require("uuid");
7
+ const widgets_1 = require("../customtypes/widgets");
8
+ exports.FieldOrSliceType = t.union([
9
+ t.union([widgets_1.FieldType, widgets_1.SlicesTypes]),
10
+ t.literal("Repeatable.Link"),
11
+ ]);
12
+ class LegacyContentCtx {
13
+ constructor({ fieldKey, contentKey, contentPath, fieldPath, allTypes, allKeys, }) {
14
+ var _a;
15
+ Object.defineProperty(this, "fieldKey", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "prefixedKey", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "keyOfType", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "keyOfKey", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "fieldPath", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "fieldType", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "allTypes", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "allKeys", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ Object.defineProperty(this, "contentKey", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
69
+ Object.defineProperty(this, "contentPath", {
70
+ enumerable: true,
71
+ configurable: true,
72
+ writable: true,
73
+ value: void 0
74
+ });
75
+ Object.defineProperty(this, "fieldContentKey", {
76
+ enumerable: true,
77
+ configurable: true,
78
+ writable: true,
79
+ value: void 0
80
+ });
81
+ this.fieldKey = fieldKey;
82
+ this.contentKey = contentKey || fieldKey;
83
+ this.fieldPath = fieldPath || [];
84
+ this.contentPath = contentPath || [];
85
+ this.allTypes = allTypes || new Map();
86
+ this.allKeys = allKeys || new Map();
87
+ const prefixedKey = Array.of(this.fieldPath, [this.fieldKey])
88
+ .flat()
89
+ .join(".");
90
+ const prefixedContentKey = Array.of(this.contentPath, [this.contentKey])
91
+ .flat()
92
+ .join(".");
93
+ this.prefixedKey = prefixedKey;
94
+ this.keyOfType = `${prefixedKey}_TYPE`;
95
+ this.keyOfKey = `${prefixedContentKey}_KEY`;
96
+ this.fieldType = this.allTypes.get(this.prefixedKey);
97
+ this.fieldContentKey = (_a = this.allKeys.get(prefixedContentKey)) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)();
98
+ }
99
+ withContentKey(contentKey) {
100
+ return new LegacyContentCtx({
101
+ ...this,
102
+ contentPath: [...this.contentPath, this.contentKey],
103
+ contentKey,
104
+ });
105
+ }
106
+ }
107
+ exports.LegacyContentCtx = LegacyContentCtx;
108
+ function getFieldCtx({ fieldKey, contentKey: contentKeyParam, ctx, prefixes, }) {
109
+ const contentKey = contentKeyParam || fieldKey;
110
+ return new LegacyContentCtx({
111
+ fieldKey,
112
+ contentKey,
113
+ contentPath: [...ctx.contentPath, ctx.contentKey, ...(prefixes || [])],
114
+ fieldPath: [...ctx.fieldPath, ctx.fieldKey, ...(prefixes || [])],
115
+ allTypes: ctx.allTypes,
116
+ allKeys: ctx.allKeys,
117
+ });
118
+ }
119
+ exports.getFieldCtx = getFieldCtx;
120
+ function defaultCtx(key, allTypes = new Map(), allKeys = new Map()) {
121
+ return new LegacyContentCtx({
122
+ fieldKey: key,
123
+ contentKey: key,
124
+ contentPath: [],
125
+ fieldPath: [],
126
+ allTypes,
127
+ allKeys,
128
+ });
129
+ }
130
+ exports.defaultCtx = defaultCtx;
@@ -0,0 +1,16 @@
1
+ import * as t from "io-ts";
2
+ import type { FieldOrSliceType } from "../LegacyContentCtx";
3
+ export declare const EmptyContentType = "EmptyContent";
4
+ export declare const isEmptyContent: (u: unknown) => u is {
5
+ type: string;
6
+ __TYPE__: "EmptyContent";
7
+ };
8
+ export declare const EmptyLegacy: (type: FieldOrSliceType) => t.Type<{
9
+ type: string;
10
+ __TYPE__: "EmptyContent";
11
+ }, unknown, unknown>;
12
+ export declare const EmptyContent: t.ExactC<t.TypeC<{
13
+ type: t.StringC;
14
+ __TYPE__: t.LiteralC<"EmptyContent">;
15
+ }>>;
16
+ export declare type EmptyContent = t.TypeOf<typeof EmptyContent>;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmptyContent = exports.EmptyLegacy = exports.isEmptyContent = exports.EmptyContentType = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fp_ts_1 = require("fp-ts");
6
+ const function_1 = require("fp-ts/lib/function");
7
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const utils_1 = require("../utils");
9
+ exports.EmptyContentType = "EmptyContent";
10
+ const isEmptyContent = (u) => (0, utils_1.hasContentType)(u) && u.__TYPE__ === exports.EmptyContentType;
11
+ exports.isEmptyContent = isEmptyContent;
12
+ const legacyReader = t.union([
13
+ t.null,
14
+ t.type({
15
+ value: t.null,
16
+ }),
17
+ ]);
18
+ const EmptyLegacy = (type) => new t.Type("EmptyLegacy", exports.isEmptyContent, (content) => {
19
+ return (0, function_1.pipe)(legacyReader.decode(content), fp_ts_1.either.map(() => {
20
+ return {
21
+ type,
22
+ __TYPE__: exports.EmptyContentType,
23
+ };
24
+ }));
25
+ }, () => null);
26
+ exports.EmptyLegacy = EmptyLegacy;
27
+ exports.EmptyContent = t.strict({
28
+ type: t.string,
29
+ __TYPE__: t.literal(exports.EmptyContentType),
30
+ });
@@ -0,0 +1,42 @@
1
+ import * as t from "io-ts";
2
+ import type { ContentPath, TraverseWidgetContentFn } from "../../_internal/utils";
3
+ import { type Group, type NestableWidget } from "../../customtypes";
4
+ import { LegacyContentCtx, WithTypes } from "../LegacyContentCtx";
5
+ import { NestableContent } from "./nestable";
6
+ export declare const GroupItemContentType: "GroupItemContent";
7
+ export declare const GroupContentType: "GroupContentType";
8
+ export declare const GroupItemContent: t.Type<GroupItemContent>;
9
+ export declare type GroupItemContent = {
10
+ __TYPE__: typeof GroupItemContentType;
11
+ key: string;
12
+ value: [string, NestableContent | GroupContent][];
13
+ };
14
+ export declare const GroupContent: t.Type<GroupContent>;
15
+ export declare type GroupContent = {
16
+ __TYPE__: typeof GroupContentType;
17
+ value: GroupItemContent[];
18
+ };
19
+ export declare const isGroupContent: (u: unknown) => u is GroupContent;
20
+ export declare const GroupContentDefaultValue: GroupContent;
21
+ declare const itemLegacyReader: t.RecordC<t.StringC, t.UnknownC>;
22
+ export declare type GroupItemLegacy = t.TypeOf<typeof itemLegacyReader>;
23
+ export declare const GroupItemLegacy: (ctx: LegacyContentCtx, index: number) => t.Type<GroupItemContent, WithTypes<{
24
+ [x: string]: unknown;
25
+ }>, unknown>;
26
+ export declare function arrayWithIndexCodec<T, O>(f: (index: number) => t.Type<T, O, unknown>): t.Type<Array<T>, O[], unknown>;
27
+ declare type GroupLegacy = Array<GroupItemLegacy>;
28
+ export declare const GroupLegacy: (ctx: LegacyContentCtx) => t.Type<GroupContent, WithTypes<GroupLegacy>, unknown>;
29
+ export declare function groupContentWithDefaultValues(customType: Group, content: GroupContent): GroupContent;
30
+ export declare function traverseGroupContent({ path, key, apiId, model, content, }: {
31
+ path: ContentPath;
32
+ key: string;
33
+ apiId: string;
34
+ content: GroupContent;
35
+ model?: Group | undefined;
36
+ }): (transform: TraverseWidgetContentFn) => GroupContent | undefined;
37
+ export declare function traverseGroupItemsContent({ path, model, content, }: {
38
+ path: ContentPath;
39
+ content: Array<GroupItemContent>;
40
+ model?: Record<string, Group | NestableWidget> | undefined;
41
+ }): (transform: TraverseWidgetContentFn) => Array<GroupItemContent>;
42
+ export {};