@prismicio/types-internal 2.2.0-alpha.9 → 2.2.0-traverse.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 (183) hide show
  1. package/lib/_internal/utils.d.ts +17 -0
  2. package/lib/_internal/utils.js +9 -0
  3. package/lib/common/Asset.d.ts +3 -4
  4. package/lib/common/Embed.d.ts +1 -2
  5. package/lib/common/UUID.d.ts +7 -0
  6. package/lib/common/UUID.js +8 -0
  7. package/lib/common/index.d.ts +1 -0
  8. package/lib/common/index.js +1 -0
  9. package/lib/content/Document.d.ts +22 -3
  10. package/lib/content/Document.js +71 -1
  11. package/lib/content/fields/GroupContent.d.ts +7 -0
  12. package/lib/content/fields/GroupContent.js +34 -1
  13. package/lib/content/fields/WidgetContent.d.ts +6 -6
  14. package/lib/content/fields/nestable/NestableContent.d.ts +2 -0
  15. package/lib/content/fields/nestable/NestableContent.js +29 -1
  16. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +8 -0
  17. package/lib/content/fields/slices/Slice/CompositeSliceContent.js +56 -1
  18. package/lib/content/fields/slices/Slice/RepeatableContent.js +0 -1
  19. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +8 -0
  20. package/lib/content/fields/slices/Slice/SharedSliceContent.js +58 -1
  21. package/lib/content/fields/slices/SlicesContent.d.ts +7 -0
  22. package/lib/content/fields/slices/SlicesContent.js +70 -1
  23. package/lib/customtypes/CustomType.d.ts +1 -0
  24. package/lib/customtypes/CustomType.js +8 -1
  25. package/lib/customtypes/widgets/slices/CompositeSlice.d.ts +2 -0
  26. package/lib/customtypes/widgets/slices/CompositeSlice.js +5 -1
  27. package/lib/customtypes/widgets/slices/LegacySlice.d.ts +2 -0
  28. package/lib/customtypes/widgets/slices/LegacySlice.js +5 -1
  29. package/lib/customtypes/widgets/slices/SharedSlice.d.ts +4 -0
  30. package/lib/customtypes/widgets/slices/SharedSlice.js +9 -1
  31. package/lib/import/converters/Document.d.ts +2 -2
  32. package/lib/import/converters/Document.js +6 -6
  33. package/lib/import/converters/fields/Slices/SharedSlice.d.ts +4 -0
  34. package/lib/import/converters/fields/Slices/SharedSlice.js +19 -0
  35. package/lib/import/converters/fields/Slices/SharedSliceContent.d.ts +10 -0
  36. package/lib/import/converters/fields/Slices/SharedSliceContent.js +58 -0
  37. package/lib/import/converters/fields/Slices/Slices.d.ts +4 -0
  38. package/lib/import/converters/fields/Slices/Slices.js +16 -0
  39. package/lib/import/converters/fields/Slices/index.d.ts +1 -0
  40. package/lib/import/converters/fields/Slices/index.js +4 -0
  41. package/lib/import/converters/fields/UID.d.ts +1 -1
  42. package/lib/import/converters/fields/UID.js +0 -2
  43. package/lib/import/converters/fields/index.d.ts +1 -0
  44. package/lib/import/converters/fields/index.js +1 -0
  45. package/lib/import/converters/fields/nestable/Embed.d.ts +2 -1
  46. package/lib/import/converters/fields/nestable/Embed.js +24 -5
  47. package/lib/import/converters/fields/nestable/Image.d.ts +2 -2
  48. package/lib/import/converters/fields/nestable/Image.js +1 -1
  49. package/lib/import/converters/fields/nestable/Link.d.ts +1 -1
  50. package/lib/import/converters/fields/nestable/Link.js +6 -6
  51. package/lib/import/converters/fields/nestable/Nestable.d.ts +4 -3
  52. package/lib/import/converters/fields/nestable/Nestable.js +2 -2
  53. package/lib/import/validators/Document.js +2 -2
  54. package/lib/import/validators/fields/ImportField.d.ts +17 -68
  55. package/lib/import/validators/fields/ImportField.js +8 -4
  56. package/lib/import/validators/fields/ImportSlices/ImportSlices.d.ts +8 -0
  57. package/lib/import/validators/fields/ImportSlices/ImportSlices.js +29 -0
  58. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.d.ts +14 -0
  59. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +61 -0
  60. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.d.ts +4 -0
  61. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +7 -0
  62. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.d.ts +2 -0
  63. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.js +15 -0
  64. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +17 -0
  65. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +30 -0
  66. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +43 -0
  67. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +69 -0
  68. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.d.ts +4 -0
  69. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.js +6 -0
  70. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.d.ts +2 -0
  71. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.js +5 -0
  72. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.d.ts +1 -0
  73. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.js +2 -0
  74. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.d.ts +9 -0
  75. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.js +39 -0
  76. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.d.ts +11 -0
  77. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +24 -0
  78. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.d.ts +11 -0
  79. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +24 -0
  80. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.d.ts +5 -0
  81. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.js +8 -0
  82. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.d.ts +3 -0
  83. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.js +6 -0
  84. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.d.ts +339 -0
  85. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.js +11 -0
  86. package/lib/import/validators/fields/ImportSlices/index.d.ts +1 -0
  87. package/lib/import/validators/fields/ImportSlices/index.js +4 -0
  88. package/lib/import/validators/fields/ImportSlices/utils.d.ts +4 -0
  89. package/lib/import/validators/fields/ImportSlices/utils.js +35 -0
  90. package/lib/import/validators/fields/UID.d.ts +2 -2
  91. package/lib/import/validators/fields/UID.js +1 -1
  92. package/lib/import/validators/fields/nestable/Embed.d.ts +6 -23
  93. package/lib/import/validators/fields/nestable/Embed.js +7 -25
  94. package/lib/import/validators/fields/nestable/GeoPoint.js +1 -1
  95. package/lib/import/validators/fields/nestable/Image/Decoder.d.ts +26 -0
  96. package/lib/import/validators/fields/nestable/Image/Decoder.js +40 -0
  97. package/lib/import/validators/fields/nestable/Image/Validator.d.ts +33 -0
  98. package/lib/import/validators/fields/nestable/Image/Validator.js +27 -0
  99. package/lib/import/validators/fields/nestable/Image/index.d.ts +26 -0
  100. package/lib/import/validators/fields/nestable/Image/index.js +41 -0
  101. package/lib/import/validators/fields/nestable/Image/model.d.ts +18 -0
  102. package/lib/import/validators/fields/nestable/Image/model.js +2 -0
  103. package/lib/import/validators/fields/nestable/Link.js +2 -2
  104. package/lib/import/validators/fields/nestable/Nestable.d.ts +9 -60
  105. package/lib/import/validators/fields/nestable/index.d.ts +1 -0
  106. package/lib/import/validators/fields/nestable/index.js +1 -0
  107. package/lib/utils/DocumentId.d.ts +2 -1
  108. package/lib/utils/DocumentId.js +3 -3
  109. package/lib/utils/io-ts.d.ts +2 -0
  110. package/lib/utils/io-ts.js +22 -0
  111. package/lib/validators/BasicTypes.d.ts +0 -1
  112. package/lib/validators/BasicTypes.js +7 -8
  113. package/lib/validators/DefaultOrElse.js +1 -1
  114. package/lib/validators/NonEmptyString.js +1 -1
  115. package/lib/validators/NullOrT.d.ts +2 -0
  116. package/lib/validators/NullOrT.js +12 -0
  117. package/lib/validators/function.d.ts +4 -4
  118. package/lib/validators/function.js +6 -6
  119. package/package.json +4 -2
  120. package/src/_internal/utils.ts +39 -0
  121. package/src/common/Asset.ts +4 -5
  122. package/src/common/Embed.ts +22 -0
  123. package/src/common/UUID.ts +19 -0
  124. package/src/common/index.ts +1 -0
  125. package/src/content/Document.ts +96 -1
  126. package/src/content/fields/GroupContent.ts +47 -0
  127. package/src/content/fields/nestable/NestableContent.ts +32 -0
  128. package/src/content/fields/slices/Slice/CompositeSliceContent.ts +77 -0
  129. package/src/content/fields/slices/Slice/RepeatableContent.ts +0 -1
  130. package/src/content/fields/slices/Slice/SharedSliceContent.ts +82 -0
  131. package/src/content/fields/slices/SlicesContent.ts +96 -0
  132. package/src/customtypes/CustomType.ts +17 -0
  133. package/src/customtypes/widgets/slices/CompositeSlice.ts +7 -0
  134. package/src/customtypes/widgets/slices/LegacySlice.ts +7 -0
  135. package/src/customtypes/widgets/slices/SharedSlice.ts +12 -0
  136. package/src/import/converters/Document.ts +10 -8
  137. package/src/import/converters/fields/Slices/SharedSlice.ts +24 -0
  138. package/src/import/converters/fields/Slices/SharedSliceContent.ts +94 -0
  139. package/src/import/converters/fields/Slices/Slices.ts +20 -0
  140. package/src/import/converters/fields/Slices/index.ts +1 -0
  141. package/src/import/converters/fields/UID.ts +1 -5
  142. package/src/import/converters/fields/index.ts +1 -0
  143. package/src/import/converters/fields/nestable/Embed.ts +29 -7
  144. package/src/import/converters/fields/nestable/Image.ts +4 -4
  145. package/src/import/converters/fields/nestable/Link.ts +5 -5
  146. package/src/import/converters/fields/nestable/Nestable.ts +7 -5
  147. package/src/import/validators/Document.ts +3 -3
  148. package/src/import/validators/fields/ImportField.ts +12 -6
  149. package/src/import/validators/fields/ImportSlices/ImportSlices.ts +54 -0
  150. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +116 -0
  151. package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +6 -0
  152. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.ts +20 -0
  153. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +64 -0
  154. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.ts +100 -0
  155. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.ts +10 -0
  156. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.ts +2 -0
  157. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.ts +1 -0
  158. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.ts +65 -0
  159. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +45 -0
  160. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +45 -0
  161. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/index.ts +5 -0
  162. package/src/import/validators/fields/ImportSlices/SharedSlice/index.ts +3 -0
  163. package/src/import/validators/fields/ImportSlices/SharedSlice/utils.ts +21 -0
  164. package/src/import/validators/fields/ImportSlices/index.ts +1 -0
  165. package/src/import/validators/fields/ImportSlices/utils.ts +43 -0
  166. package/src/import/validators/fields/UID.ts +2 -2
  167. package/src/import/validators/fields/nestable/Embed.ts +9 -41
  168. package/src/import/validators/fields/nestable/GeoPoint.ts +2 -2
  169. package/src/import/validators/fields/nestable/Image/Decoder.ts +61 -0
  170. package/src/import/validators/fields/nestable/Image/Validator.ts +38 -0
  171. package/src/import/validators/fields/nestable/Image/index.ts +75 -0
  172. package/src/import/validators/fields/nestable/Image/model.ts +18 -0
  173. package/src/import/validators/fields/nestable/Link.ts +2 -2
  174. package/src/import/validators/fields/nestable/index.ts +1 -0
  175. package/src/utils/DocumentId.ts +4 -5
  176. package/src/utils/io-ts.ts +29 -0
  177. package/src/validators/BasicTypes.ts +13 -11
  178. package/src/validators/DefaultOrElse.ts +1 -1
  179. package/src/validators/NonEmptyString.ts +4 -5
  180. package/src/validators/function.ts +4 -4
  181. package/lib/import/validators/fields/nestable/Image.d.ts +0 -62
  182. package/lib/import/validators/fields/nestable/Image.js +0 -76
  183. package/src/import/validators/fields/nestable/Image.ts +0 -147
@@ -0,0 +1,17 @@
1
+ import type { SliceContent, WidgetContent } from "../content";
2
+ import type { StaticSlice, StaticWidget } from "../customtypes";
3
+ export declare type TraverseContentFn = <C extends WidgetContent | SliceContent, D extends StaticWidget | StaticSlice>({ path, key, apiId, model, content, }: {
4
+ path: ContentPath;
5
+ key: string;
6
+ apiId: string;
7
+ model?: D | undefined;
8
+ content: C;
9
+ }) => C | undefined;
10
+ export declare type ContentPathEntry = {
11
+ type: "CustomType" | WidgetContent["__TYPE__"] | SliceContent["__TYPE__"] | "GroupItem" | "primary" | "items";
12
+ key: string;
13
+ };
14
+ export declare type ContentPath = ReadonlyArray<ContentPathEntry>;
15
+ export declare const ContentPath: {
16
+ serialize(path: ContentPath): string;
17
+ };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContentPath = void 0;
4
+ const PATH_SEPARATOR = "::";
5
+ exports.ContentPath = {
6
+ serialize(path) {
7
+ return path.map((entry) => entry.key).join(PATH_SEPARATOR);
8
+ },
9
+ };
@@ -1,11 +1,10 @@
1
- export declare type AssetId = string;
2
1
  export declare type Asset = {
3
- id: AssetId;
2
+ id: string;
4
3
  last_modified: string;
5
4
  kind: "image" | "all";
6
5
  filename?: string;
7
6
  extension?: string;
8
- size?: string;
7
+ size?: number;
9
8
  origin_url: string;
10
9
  url: string;
11
10
  width?: number;
@@ -14,4 +13,4 @@ export declare type Asset = {
14
13
  credits?: string;
15
14
  alt?: string;
16
15
  };
17
- export declare const getAssetOrThrow: (assets: Record<AssetId, Asset>) => (assetId: AssetId) => Asset;
16
+ export declare const getAssetOrThrow: (assets: Record<Asset["id"], Asset | undefined>) => (assetId: Asset["id"]) => Asset;
@@ -12,5 +12,4 @@ export declare type Embed = {
12
12
  html?: string | null;
13
13
  title?: string | null;
14
14
  };
15
- export declare type EmbedUrl = string;
16
- export declare const getEmbedOrThrow: (embeds: Record<string, Embed>) => (embedUrl: string) => Embed;
15
+ export declare const getEmbedOrThrow: (embeds: Record<string, Embed | undefined>) => (embedUrl: string) => Embed;
@@ -0,0 +1,7 @@
1
+ import * as t from "io-ts";
2
+ interface UUIDBrand {
3
+ readonly UUID: unique symbol;
4
+ }
5
+ export declare const UUID: t.BrandC<t.StringC, UUIDBrand>;
6
+ export declare type UUID = t.TypeOf<typeof UUID>;
7
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UUID = 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 function_1 = require("../validators/function");
8
+ exports.UUID = (0, function_1.withCustomError)(t.brand(t.string, (s) => (0, uuid_1.validate)(s), "UUID"), () => "The value must be a valid UUID");
@@ -1,2 +1,3 @@
1
1
  export * from "./Asset";
2
+ export * from "./Embed";
2
3
  export * from "./WidgetKey";
@@ -2,4 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  (0, tslib_1.__exportStar)(require("./Asset"), exports);
5
+ (0, tslib_1.__exportStar)(require("./Embed"), exports);
5
6
  (0, tslib_1.__exportStar)(require("./WidgetKey"), exports);
@@ -1,5 +1,8 @@
1
1
  import * as t from "io-ts";
2
+ import { type TraverseContentFn, ContentPath } from "../_internal/utils";
2
3
  import { WidgetKey } from "../common";
4
+ import { type StaticCustomType } from "../customtypes";
5
+ import { SliceContent, WidgetContent } from "./fields";
3
6
  import { FieldOrSliceType, WithTypes } from "./LegacyContentCtx";
4
7
  export declare const Document: t.RecordC<t.Type<string, string, unknown>, t.UnionC<[t.ExactC<t.TypeC<{
5
8
  __TYPE__: t.LiteralC<"GroupContentType">;
@@ -2993,6 +2996,9 @@ export declare const DocumentLegacy: {
2993
2996
  __TYPE__: "SeparatorContent";
2994
2997
  }][];
2995
2998
  }[];
2999
+ } | {
3000
+ __TYPE__: "UIDContent";
3001
+ value: string;
2996
3002
  } | {
2997
3003
  __TYPE__: "SliceContentType";
2998
3004
  value: {
@@ -4622,9 +4628,6 @@ export declare const DocumentLegacy: {
4622
4628
  }[];
4623
4629
  };
4624
4630
  }[];
4625
- } | {
4626
- __TYPE__: "UIDContent";
4627
- value: string;
4628
4631
  };
4629
4632
  }, WithTypes<{
4630
4633
  [x: string]: unknown;
@@ -4633,4 +4636,20 @@ export declare const DocumentLegacy: {
4633
4636
  parse: typeof parseLegacyDocument;
4634
4637
  encode: typeof encodeToLegacyDocument;
4635
4638
  };
4639
+ /**
4640
+ *
4641
+ * @param model: can be optional if we simply want to loop through the content without any consideration for the attached model
4642
+ * @param document: The content we actually want to iterate on in an immutable fashion
4643
+ * @param transform: A user function that provides a way to transform any kind of content wherever it is in a structured Prismic object content.
4644
+ * @returns a transformed document with the user's transformation applied with the transform function
4645
+ */
4646
+ export declare function traverseDocument(document: Document, model?: StaticCustomType): (transform: TraverseContentFn) => Document;
4647
+ /**
4648
+ * 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
4649
+ *
4650
+ * @param document parsed prismic content
4651
+ * @param is typeguard to match specifically the type of widget we want to collect
4652
+ * @returns a record containing the path of the widget as key and the typed collected content as value
4653
+ */
4654
+ export declare function collectWidgets<W extends WidgetContent | SliceContent>(document: Document, is: (content: WidgetContent | SliceContent, path: ContentPath) => content is W): Record<string, W>;
4636
4655
  export {};
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DocumentLegacy = exports.Document = void 0;
3
+ exports.collectWidgets = exports.traverseDocument = exports.DocumentLegacy = exports.Document = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const Either_1 = require("fp-ts/lib/Either");
7
7
  const function_1 = require("fp-ts/lib/function");
8
8
  const t = (0, tslib_1.__importStar)(require("io-ts"));
9
+ const utils_1 = require("../_internal/utils");
9
10
  const common_1 = require("../common");
11
+ const customtypes_1 = require("../customtypes");
10
12
  const fields_1 = require("./fields");
11
13
  const LegacyContentCtx_1 = require("./LegacyContentCtx");
12
14
  exports.Document = t.record(common_1.WidgetKey, fields_1.WidgetContent);
@@ -91,3 +93,71 @@ exports.DocumentLegacy = {
91
93
  parse: parseLegacyDocument,
92
94
  encode: encodeToLegacyDocument,
93
95
  };
96
+ /**
97
+ *
98
+ * @param model: can be optional if we simply want to loop through the content without any consideration for the attached model
99
+ * @param document: The content we actually want to iterate on in an immutable fashion
100
+ * @param transform: A user function that provides a way to transform any kind of content wherever it is in a structured Prismic object content.
101
+ * @returns a transformed document with the user's transformation applied with the transform function
102
+ */
103
+ function traverseDocument(document, model) {
104
+ return (transform) => {
105
+ const fieldDefs = model &&
106
+ (0, customtypes_1.flattenStaticWidgets)(model).reduce((acc, [key, def]) => ({ ...acc, [key]: def }), {});
107
+ return Object.entries(document).reduce((acc, [key, content]) => {
108
+ const fieldDef = fieldDefs && fieldDefs[key];
109
+ const path = [{ key, type: content.__TYPE__ }];
110
+ const transformedWidgetContent = (() => {
111
+ switch (content.__TYPE__) {
112
+ case "SliceContentType":
113
+ return (0, fields_1.traverseSlices)({
114
+ path,
115
+ model: (fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.type) === "Slices" || (fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.type) === "Choice"
116
+ ? fieldDef
117
+ : undefined,
118
+ content,
119
+ })(transform);
120
+ case "GroupContentType":
121
+ return (0, fields_1.traverseGroupContent)({
122
+ path,
123
+ model: (fieldDef === null || fieldDef === void 0 ? void 0 : fieldDef.type) === "Group" ? fieldDef : undefined,
124
+ content,
125
+ })(transform);
126
+ default:
127
+ return content;
128
+ }
129
+ })();
130
+ const transformedWidget = transformedWidgetContent &&
131
+ transform({
132
+ path,
133
+ key,
134
+ apiId: key,
135
+ model: fieldDef,
136
+ content: transformedWidgetContent,
137
+ });
138
+ return {
139
+ ...acc,
140
+ ...(transformedWidget ? { [key]: transformedWidget } : {}),
141
+ };
142
+ }, {});
143
+ };
144
+ }
145
+ exports.traverseDocument = traverseDocument;
146
+ /**
147
+ * 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
148
+ *
149
+ * @param document parsed prismic content
150
+ * @param is typeguard to match specifically the type of widget we want to collect
151
+ * @returns a record containing the path of the widget as key and the typed collected content as value
152
+ */
153
+ function collectWidgets(document, is) {
154
+ const collected = {};
155
+ traverseDocument(document)(({ content, path }) => {
156
+ const key = utils_1.ContentPath.serialize(path);
157
+ if (is(content, path))
158
+ collected[key] = content;
159
+ return content;
160
+ });
161
+ return collected;
162
+ }
163
+ exports.collectWidgets = collectWidgets;
@@ -1,4 +1,6 @@
1
1
  import * as t from "io-ts";
2
+ import type { ContentPath, TraverseContentFn } from "../../_internal/utils";
3
+ import type { Group } from "../../customtypes";
2
4
  import { LegacyContentCtx, WithTypes } from "../LegacyContentCtx";
3
5
  export declare const GroupItemContentType: "GroupItemContent";
4
6
  export declare const GroupItemContent: t.ExactC<t.TypeC<{
@@ -1436,4 +1438,9 @@ export declare const GroupContent: t.ExactC<t.TypeC<{
1436
1438
  }>>>;
1437
1439
  }>>;
1438
1440
  export declare type GroupContent = t.TypeOf<typeof GroupContent>;
1441
+ export declare function traverseGroupContent({ path, model, content, }: {
1442
+ path: ContentPath;
1443
+ content: GroupContent;
1444
+ model?: Group | undefined;
1445
+ }): (transform: TraverseContentFn) => GroupContent | undefined;
1439
1446
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GroupContent = exports.isGroupContent = exports.GroupContentType = exports.GroupLegacy = exports.GroupItemLegacy = exports.GroupItemContent = exports.GroupItemContentType = void 0;
3
+ exports.traverseGroupContent = exports.GroupContent = exports.isGroupContent = exports.GroupContentType = exports.GroupLegacy = exports.GroupItemLegacy = exports.GroupItemContent = exports.GroupItemContentType = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const Either_1 = require("fp-ts/lib/Either");
@@ -81,3 +81,36 @@ exports.GroupContent = t.strict({
81
81
  __TYPE__: t.literal(exports.GroupContentType),
82
82
  value: t.array(exports.GroupItemContent),
83
83
  });
84
+ function traverseGroupContent({ path, model, content, }) {
85
+ return (transform) => {
86
+ const groupItems = content.value.map((groupItem, index) => {
87
+ const groupItemFields = groupItem.value.reduce((acc, [fieldKey, fieldContent]) => {
88
+ var _a, _b;
89
+ const fieldDef = (_b = (_a = model === null || model === void 0 ? void 0 : model.config) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b[fieldKey];
90
+ const transformedField = transform({
91
+ path: path.concat([
92
+ { key: index.toString(), type: "GroupItem" },
93
+ { key: fieldKey, type: fieldContent.__TYPE__ },
94
+ ]),
95
+ key: fieldKey,
96
+ apiId: fieldKey,
97
+ model: fieldDef,
98
+ content: fieldContent,
99
+ });
100
+ // Can happen if the transform function returns undefined to filter out a field
101
+ if (!transformedField)
102
+ return acc;
103
+ return acc.concat([[fieldKey, transformedField]]);
104
+ }, []);
105
+ return {
106
+ __TYPE__: groupItem.__TYPE__,
107
+ value: groupItemFields,
108
+ };
109
+ });
110
+ return {
111
+ __TYPE__: content.__TYPE__,
112
+ value: groupItems,
113
+ };
114
+ };
115
+ }
116
+ exports.traverseGroupContent = traverseGroupContent;
@@ -2977,6 +2977,9 @@ export declare const isWidgetContent: (u: unknown) => u is {
2977
2977
  __TYPE__: "SeparatorContent";
2978
2978
  }][];
2979
2979
  }[];
2980
+ } | {
2981
+ __TYPE__: "UIDContent";
2982
+ value: string;
2980
2983
  } | {
2981
2984
  __TYPE__: "SliceContentType";
2982
2985
  value: {
@@ -4606,9 +4609,6 @@ export declare const isWidgetContent: (u: unknown) => u is {
4606
4609
  }[];
4607
4610
  };
4608
4611
  }[];
4609
- } | {
4610
- __TYPE__: "UIDContent";
4611
- value: string;
4612
4612
  };
4613
4613
  export declare type WidgetContent = t.TypeOf<typeof WidgetContent>;
4614
4614
  export declare type ContentType = WidgetContent["__TYPE__"];
@@ -5151,6 +5151,9 @@ export declare const WidgetLegacy: (ctx: LegacyContentCtx) => {
5151
5151
  __TYPE__: "SeparatorContent";
5152
5152
  }][];
5153
5153
  }[];
5154
+ }> | import("fp-ts/lib/Either").Right<{
5155
+ __TYPE__: "UIDContent";
5156
+ value: string;
5154
5157
  }> | import("fp-ts/lib/Either").Right<{
5155
5158
  __TYPE__: "SliceContentType";
5156
5159
  value: {
@@ -6780,9 +6783,6 @@ export declare const WidgetLegacy: (ctx: LegacyContentCtx) => {
6780
6783
  }[];
6781
6784
  };
6782
6785
  }[];
6783
- }> | import("fp-ts/lib/Either").Right<{
6784
- __TYPE__: "UIDContent";
6785
- value: string;
6786
6786
  }> | undefined;
6787
6787
  encode(value: WidgetContent): import("../LegacyContentCtx").WithTypes<unknown> | undefined;
6788
6788
  };
@@ -1,4 +1,5 @@
1
1
  import * as t from "io-ts";
2
+ import type { NestableWidget } from "../../../customtypes";
2
3
  import type { LegacyContentCtx } from "../../LegacyContentCtx";
3
4
  export declare const NestableContent: t.UnionC<[t.ExactC<t.TypeC<{
4
5
  type: t.StringC;
@@ -840,3 +841,4 @@ export declare const NestableLegacy: (ctx: LegacyContentCtx) => {
840
841
  }> | undefined;
841
842
  encode(value: NestableContent): import("../../LegacyContentCtx").WithTypes<unknown> | undefined;
842
843
  };
844
+ export declare function isValidStaticNestableContent(content: NestableContent, model?: NestableWidget): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NestableLegacy = exports.isNestableContent = exports.NestableContent = void 0;
3
+ exports.isValidStaticNestableContent = exports.NestableLegacy = exports.isNestableContent = exports.NestableContent = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/lib/Either");
6
6
  const t = (0, tslib_1.__importStar)(require("io-ts"));
@@ -140,3 +140,31 @@ const NestableLegacy = (ctx) => {
140
140
  };
141
141
  };
142
142
  exports.NestableLegacy = NestableLegacy;
143
+ function isValidStaticNestableContent(content, model) {
144
+ if (!model)
145
+ return false;
146
+ switch (content.__TYPE__) {
147
+ // special case for no content so we can consider it valid no matter its model
148
+ case "EmptyContent":
149
+ return true;
150
+ case "BooleanContent":
151
+ return model.type === "Boolean";
152
+ case "EmbedContent":
153
+ return model.type === "Embed";
154
+ case "FieldContent":
155
+ return content.type === model.type;
156
+ case "GeoPointContent":
157
+ return model.type === "GeoPoint";
158
+ case "ImageContent":
159
+ return model.type === "Image";
160
+ case "IntegrationFieldsContent":
161
+ return model.type === "IntegrationFields";
162
+ case "LinkContent":
163
+ return model.type === "Link";
164
+ case "StructuredTextContent":
165
+ return model.type === "StructuredText";
166
+ case "SeparatorContent":
167
+ return model.type === "Separator";
168
+ }
169
+ }
170
+ exports.isValidStaticNestableContent = isValidStaticNestableContent;
@@ -1,4 +1,6 @@
1
1
  import * as t from "io-ts";
2
+ import type { ContentPath, TraverseContentFn } from "../../../../_internal/utils";
3
+ import type { CompositeSlice } from "../../../../customtypes";
2
4
  import { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx";
3
5
  export declare const CompositeSliceContentType = "CompositeSliceContent";
4
6
  export declare const isCompositeSliceContent: (u: unknown) => u is {
@@ -1699,3 +1701,9 @@ export declare const CompositeSliceContent: t.ExactC<t.TypeC<{
1699
1701
  }>>>;
1700
1702
  }>>;
1701
1703
  export declare type CompositeSliceContent = t.TypeOf<typeof CompositeSliceContent>;
1704
+ export declare function traverseCompositeSliceContent({ path, sliceName, model, content, }: {
1705
+ path: ContentPath;
1706
+ sliceName: string;
1707
+ content: CompositeSliceContent;
1708
+ model?: CompositeSlice | undefined;
1709
+ }): (transform: TraverseContentFn) => CompositeSliceContent | undefined;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CompositeSliceContent = exports.CompositeSliceLegacy = exports.isCompositeSliceContent = exports.CompositeSliceContentType = void 0;
3
+ exports.traverseCompositeSliceContent = exports.CompositeSliceContent = exports.CompositeSliceLegacy = exports.isCompositeSliceContent = exports.CompositeSliceContentType = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const Either_1 = require("fp-ts/lib/Either");
@@ -79,3 +79,58 @@ exports.CompositeSliceContent = t.strict({
79
79
  nonRepeat: t.record(t.string, nestable_1.NestableContent),
80
80
  repeat: RepeatableContent_1.RepeatableWidgets,
81
81
  });
82
+ function traverseCompositeSliceContent({ path, sliceName, model, content, }) {
83
+ return (transform) => {
84
+ const primary = Object.entries(content.nonRepeat).reduce((acc, [fieldKey, fieldContent]) => {
85
+ var _a;
86
+ const fieldDef = (_a = model === null || model === void 0 ? void 0 : model["non-repeat"]) === null || _a === void 0 ? void 0 : _a[fieldKey];
87
+ const transformedField = transform({
88
+ path: path.concat([
89
+ { key: "non-repeat", type: "primary" },
90
+ { key: fieldKey, type: fieldContent.__TYPE__ },
91
+ ]),
92
+ key: fieldKey,
93
+ apiId: sliceName,
94
+ model: fieldDef,
95
+ content: fieldContent,
96
+ });
97
+ // Can happen if the transform function returns undefined to filter out a field
98
+ if (!transformedField)
99
+ return acc;
100
+ return {
101
+ ...acc,
102
+ [fieldKey]: transformedField,
103
+ };
104
+ }, {});
105
+ const items = content.repeat.map((itemBlock) => {
106
+ const itemBlockFields = itemBlock.value.reduce((acc, [fieldKey, fieldContent]) => {
107
+ var _a;
108
+ const fieldDef = (_a = model === null || model === void 0 ? void 0 : model.repeat) === null || _a === void 0 ? void 0 : _a[fieldKey];
109
+ const transformedField = transform({
110
+ path: path.concat([
111
+ { key: "repeat", type: "items" },
112
+ { key: fieldKey, type: fieldContent.__TYPE__ },
113
+ ]),
114
+ key: fieldKey,
115
+ apiId: sliceName,
116
+ model: fieldDef,
117
+ content: fieldContent,
118
+ });
119
+ // Can happen if the transform function returns undefined to filter out a field
120
+ if (!transformedField)
121
+ return acc;
122
+ return acc.concat([[fieldKey, transformedField]]);
123
+ }, []);
124
+ return {
125
+ __TYPE__: itemBlock.__TYPE__,
126
+ value: itemBlockFields,
127
+ };
128
+ });
129
+ return {
130
+ __TYPE__: content.__TYPE__,
131
+ repeat: items,
132
+ nonRepeat: primary,
133
+ };
134
+ };
135
+ }
136
+ exports.traverseCompositeSliceContent = traverseCompositeSliceContent;
@@ -14,5 +14,4 @@ const RepeatableWidgetsBlock = t.strict({
14
14
  __TYPE__: t.literal(GroupContent_1.GroupItemContentType),
15
15
  value: t.array(RepeatableWidget),
16
16
  });
17
- t.array(RepeatableWidget);
18
17
  exports.RepeatableWidgets = t.array(RepeatableWidgetsBlock);
@@ -1,4 +1,6 @@
1
1
  import * as t from "io-ts";
2
+ import type { ContentPath, TraverseContentFn } from "../../../../_internal/utils";
3
+ import type { SharedSlice } from "../../../../customtypes";
2
4
  import { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx";
3
5
  export declare const SharedSliceContentType = "SharedSliceContent";
4
6
  export declare const isSharedSliceContent: (u: unknown) => u is {
@@ -1703,3 +1705,9 @@ export declare const SharedSliceContent: t.ExactC<t.TypeC<{
1703
1705
  }>>>;
1704
1706
  }>>;
1705
1707
  export declare type SharedSliceContent = t.TypeOf<typeof SharedSliceContent>;
1708
+ export declare function traverseSharedSliceContent({ path, sliceName, model, content, }: {
1709
+ path: ContentPath;
1710
+ sliceName: string;
1711
+ content: SharedSliceContent;
1712
+ model?: SharedSlice | undefined;
1713
+ }): (transform: TraverseContentFn) => SharedSliceContent | undefined;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SharedSliceContent = exports.SharedSliceLegacy = exports.isSharedSliceContent = exports.SharedSliceContentType = void 0;
3
+ exports.traverseSharedSliceContent = exports.SharedSliceContent = exports.SharedSliceLegacy = exports.isSharedSliceContent = exports.SharedSliceContentType = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const Either_1 = require("fp-ts/lib/Either");
@@ -96,3 +96,60 @@ exports.SharedSliceContent = t.strict({
96
96
  primary: t.record(t.string, nestable_1.NestableContent),
97
97
  items: RepeatableContent_1.RepeatableWidgets,
98
98
  });
99
+ function traverseSharedSliceContent({ path, sliceName, model, content, }) {
100
+ return (transform) => {
101
+ const variationDef = model === null || model === void 0 ? void 0 : model.variations.find((v) => v.id === content.variation);
102
+ const primary = Object.entries(content.primary).reduce((acc, [fieldKey, fieldContent]) => {
103
+ var _a;
104
+ const fieldDef = variationDef && ((_a = variationDef.primary) === null || _a === void 0 ? void 0 : _a[fieldKey]);
105
+ const transformedField = transform({
106
+ path: path.concat([
107
+ { key: "primary", type: "primary" },
108
+ { key: fieldKey, type: fieldContent.__TYPE__ },
109
+ ]),
110
+ key: fieldKey,
111
+ apiId: sliceName,
112
+ model: fieldDef,
113
+ content: fieldContent,
114
+ });
115
+ // Can happen if the transform function returns undefined to filter out a field
116
+ if (!transformedField)
117
+ return acc;
118
+ return {
119
+ ...acc,
120
+ [fieldKey]: transformedField,
121
+ };
122
+ }, {});
123
+ const items = content.items.map((itemBlock) => {
124
+ const itemBlockFields = itemBlock.value.reduce((acc, [fieldKey, fieldContent]) => {
125
+ var _a;
126
+ const fieldDef = variationDef && ((_a = variationDef.items) === null || _a === void 0 ? void 0 : _a[fieldKey]);
127
+ const transformedField = transform({
128
+ path: path.concat([
129
+ { key: "items", type: "items" },
130
+ { key: fieldKey, type: fieldContent.__TYPE__ },
131
+ ]),
132
+ key: fieldKey,
133
+ apiId: sliceName,
134
+ model: fieldDef,
135
+ content: fieldContent,
136
+ });
137
+ // Can happen if the transform function returns undefined to filter out a field
138
+ if (!transformedField)
139
+ return acc;
140
+ return acc.concat([[fieldKey, transformedField]]);
141
+ }, []);
142
+ return {
143
+ __TYPE__: itemBlock.__TYPE__,
144
+ value: itemBlockFields,
145
+ };
146
+ });
147
+ return {
148
+ __TYPE__: content.__TYPE__,
149
+ variation: content.variation,
150
+ primary,
151
+ items,
152
+ };
153
+ };
154
+ }
155
+ exports.traverseSharedSliceContent = traverseSharedSliceContent;
@@ -1,4 +1,6 @@
1
1
  import * as t from "io-ts";
2
+ import type { ContentPath, TraverseContentFn } from "../../../_internal/utils";
3
+ import { type StaticSlices } from "../../../customtypes";
2
4
  import type { LegacyContentCtx, WithTypes } from "../../LegacyContentCtx";
3
5
  export declare const SlicesContentType = "SliceContentType";
4
6
  export declare const isSlicesContent: (u: unknown) => u is {
@@ -5093,4 +5095,9 @@ export declare const SlicesContent: t.TypeC<{
5093
5095
  }>>;
5094
5096
  }>;
5095
5097
  export declare type SlicesContent = t.TypeOf<typeof SlicesContent>;
5098
+ export declare function traverseSlices({ path, model, content, }: {
5099
+ path: ContentPath;
5100
+ content: SlicesContent;
5101
+ model?: StaticSlices | undefined;
5102
+ }): (transform: TraverseContentFn) => SlicesContent | undefined;
5096
5103
  export {};
@@ -1,11 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SlicesContent = exports.SlicesLegacy = exports.isSlicesContent = exports.SlicesContentType = void 0;
3
+ exports.traverseSlices = exports.SlicesContent = exports.SlicesLegacy = exports.isSlicesContent = exports.SlicesContentType = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const customtypes_1 = require("../../../customtypes");
8
9
  const utils_1 = require("../../utils");
10
+ const GroupContent_1 = require("../GroupContent");
11
+ const CompositeSliceContent_1 = require("./Slice/CompositeSliceContent");
12
+ const SharedSliceContent_1 = require("./Slice/SharedSliceContent");
9
13
  const SliceItem_1 = require("./SliceItem");
10
14
  exports.SlicesContentType = "SliceContentType";
11
15
  const isSlicesContent = (u) => (0, utils_1.hasContentType)(u) && u.__TYPE__ === exports.SlicesContentType;
@@ -32,3 +36,68 @@ exports.SlicesContent = t.type({
32
36
  __TYPE__: t.literal(exports.SlicesContentType),
33
37
  value: t.array(SliceItem_1.SliceItemContent),
34
38
  });
39
+ function traverseSlices({ path, model, content, }) {
40
+ return (transform) => {
41
+ const value = content.value.reduce((acc, sliceContent) => {
42
+ var _a, _b;
43
+ const sliceModel = (_b = (_a = model === null || model === void 0 ? void 0 : model.config) === null || _a === void 0 ? void 0 : _a.choices) === null || _b === void 0 ? void 0 : _b[sliceContent.name];
44
+ const slicePath = path.concat({
45
+ key: sliceContent.key,
46
+ type: sliceContent.widget.__TYPE__,
47
+ });
48
+ const convertedSliceWidget = (() => {
49
+ switch (sliceContent.widget.__TYPE__) {
50
+ case "SharedSliceContent":
51
+ return (0, SharedSliceContent_1.traverseSharedSliceContent)({
52
+ path: slicePath,
53
+ sliceName: sliceContent.name,
54
+ model: sliceModel && (0, customtypes_1.isStaticSharedSlice)(sliceModel)
55
+ ? sliceModel
56
+ : undefined,
57
+ content: sliceContent.widget,
58
+ })(transform);
59
+ case "CompositeSliceContent":
60
+ return (0, CompositeSliceContent_1.traverseCompositeSliceContent)({
61
+ path: slicePath,
62
+ sliceName: sliceContent.name,
63
+ model: sliceModel && (0, customtypes_1.isCompositeSlice)(sliceModel)
64
+ ? sliceModel
65
+ : undefined,
66
+ content: sliceContent.widget,
67
+ })(transform);
68
+ case "GroupContentType":
69
+ return (0, GroupContent_1.traverseGroupContent)({
70
+ path: slicePath,
71
+ model: sliceModel &&
72
+ (0, customtypes_1.isLegacySlice)(sliceModel) &&
73
+ sliceModel.type === "Group"
74
+ ? sliceModel
75
+ : undefined,
76
+ content: sliceContent.widget,
77
+ })(transform);
78
+ default:
79
+ return sliceContent.widget;
80
+ }
81
+ })();
82
+ if (!convertedSliceWidget)
83
+ return acc;
84
+ const convertedSlice = transform({
85
+ key: sliceContent.key,
86
+ apiId: sliceContent.name,
87
+ path: slicePath,
88
+ model: sliceModel,
89
+ content: convertedSliceWidget,
90
+ });
91
+ const convertedSliceParent = convertedSlice && {
92
+ ...sliceContent,
93
+ widget: convertedSlice,
94
+ };
95
+ return convertedSliceParent ? acc.concat(convertedSliceParent) : acc;
96
+ }, []);
97
+ return {
98
+ __TYPE__: content.__TYPE__,
99
+ value,
100
+ };
101
+ };
102
+ }
103
+ exports.traverseSlices = traverseSlices;