@prismicio/types-internal 2.2.0-traverse.alpha-1 → 2.2.0-traverse.alpha-3

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 (122) hide show
  1. package/lib/_internal/utils.d.ts +21 -4
  2. package/lib/_internal/utils.js +11 -0
  3. package/lib/content/Document.d.ts +2194 -13
  4. package/lib/content/Document.js +64 -36
  5. package/lib/content/fields/GroupContent.d.ts +6 -4
  6. package/lib/content/fields/GroupContent.js +13 -9
  7. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +9 -6
  8. package/lib/content/fields/slices/Slice/CompositeSliceContent.js +61 -17
  9. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +8 -6
  10. package/lib/content/fields/slices/Slice/SharedSliceContent.js +26 -18
  11. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +11 -0
  12. package/lib/content/fields/slices/Slice/SimpleSliceContent.js +100 -1
  13. package/lib/content/fields/slices/SliceItem.d.ts +15 -0
  14. package/lib/content/fields/slices/SliceItem.js +15 -1
  15. package/lib/content/fields/slices/SlicesContent.d.ts +4 -3
  16. package/lib/content/fields/slices/SlicesContent.js +125 -60
  17. package/lib/customtypes/CustomType.d.ts +8 -1
  18. package/lib/customtypes/CustomType.js +18 -1
  19. package/lib/customtypes/Section.d.ts +3 -0
  20. package/lib/customtypes/diff/SharedSlice.d.ts +6 -0
  21. package/lib/customtypes/widgets/Widget.d.ts +3 -0
  22. package/lib/customtypes/widgets/slices/CompositeSlice.d.ts +5 -0
  23. package/lib/customtypes/widgets/slices/SharedSlice.d.ts +8 -0
  24. package/lib/customtypes/widgets/slices/SharedSlice.js +3 -0
  25. package/lib/customtypes/widgets/slices/Slices.d.ts +6 -0
  26. package/lib/import/converters/fields/nestable/Image.d.ts +3 -1
  27. package/lib/import/converters/fields/nestable/Image.js +18 -2
  28. package/lib/import/converters/fields/nestable/Nestable.js +2 -0
  29. package/lib/import/converters/fields/nestable/RichText.d.ts +4 -0
  30. package/lib/import/converters/fields/nestable/RichText.js +55 -0
  31. package/lib/import/converters/fields/nestable/index.d.ts +1 -0
  32. package/lib/import/converters/fields/nestable/index.js +1 -0
  33. package/lib/import/validators/fields/ImportField.d.ts +6 -0
  34. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +1 -1
  35. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +1 -1
  36. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +1 -1
  37. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.d.ts +3 -0
  38. package/lib/import/validators/fields/nestable/Embed.d.ts +3 -0
  39. package/lib/import/validators/fields/nestable/Embed.js +3 -8
  40. package/lib/import/validators/fields/nestable/GeoPoint.js +2 -2
  41. package/lib/import/validators/fields/nestable/Image/default.d.ts +22 -0
  42. package/lib/import/validators/fields/nestable/Image/default.js +19 -0
  43. package/lib/import/validators/fields/nestable/Image/index.d.ts +19 -0
  44. package/lib/import/validators/fields/nestable/Image/index.js +22 -22
  45. package/lib/import/validators/fields/nestable/Image/merge.d.ts +23 -0
  46. package/lib/import/validators/fields/nestable/Image/{Decoder.js → merge.js} +18 -14
  47. package/lib/import/validators/fields/nestable/Image/{Validator.d.ts → validators.d.ts} +4 -2
  48. package/lib/import/validators/fields/nestable/Image/{Validator.js → validators.js} +7 -3
  49. package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.d.ts +6 -0
  50. package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.js +25 -0
  51. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.d.ts +24 -0
  52. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.js +26 -0
  53. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.d.ts +6 -0
  54. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.js +18 -0
  55. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.d.ts +10 -0
  56. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.js +11 -0
  57. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.d.ts +8 -0
  58. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.js +11 -0
  59. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.d.ts +70 -0
  60. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.js +43 -0
  61. package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.d.ts +65 -0
  62. package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.js +20 -0
  63. package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.d.ts +4 -0
  64. package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.js +7 -0
  65. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.d.ts +24 -0
  66. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.js +18 -0
  67. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.d.ts +5 -0
  68. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.js +10 -0
  69. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.d.ts +17 -0
  70. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.js +22 -0
  71. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.d.ts +2 -0
  72. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.js +5 -0
  73. package/lib/import/validators/fields/nestable/ImportRichText/index.d.ts +2 -0
  74. package/lib/import/validators/fields/nestable/ImportRichText/index.js +7 -0
  75. package/lib/import/validators/fields/nestable/Link.d.ts +21 -6
  76. package/lib/import/validators/fields/nestable/Link.js +8 -8
  77. package/lib/import/validators/fields/nestable/Nestable.d.ts +8 -1
  78. package/lib/import/validators/fields/nestable/Nestable.js +5 -1
  79. package/lib/import/validators/fields/nestable/index.d.ts +2 -1
  80. package/lib/import/validators/fields/nestable/index.js +4 -1
  81. package/package.json +1 -1
  82. package/src/_internal/utils.ts +63 -7
  83. package/src/content/Document.ts +106 -47
  84. package/src/content/fields/GroupContent.ts +25 -12
  85. package/src/content/fields/slices/Slice/CompositeSliceContent.ts +91 -22
  86. package/src/content/fields/slices/Slice/SharedSliceContent.ts +42 -26
  87. package/src/content/fields/slices/Slice/SimpleSliceContent.ts +144 -1
  88. package/src/content/fields/slices/SliceItem.ts +39 -3
  89. package/src/content/fields/slices/SlicesContent.ts +172 -67
  90. package/src/customtypes/CustomType.ts +23 -1
  91. package/src/customtypes/widgets/slices/CompositeSlice.ts +6 -0
  92. package/src/customtypes/widgets/slices/SharedSlice.ts +12 -0
  93. package/src/import/converters/fields/nestable/Image.ts +34 -4
  94. package/src/import/converters/fields/nestable/Nestable.ts +3 -0
  95. package/src/import/converters/fields/nestable/RichText.ts +62 -0
  96. package/src/import/converters/fields/nestable/index.ts +1 -0
  97. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +1 -1
  98. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +1 -1
  99. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +1 -1
  100. package/src/import/validators/fields/nestable/Embed.ts +4 -17
  101. package/src/import/validators/fields/nestable/GeoPoint.ts +2 -2
  102. package/src/import/validators/fields/nestable/Image/default.ts +25 -0
  103. package/src/import/validators/fields/nestable/Image/index.ts +34 -36
  104. package/src/import/validators/fields/nestable/Image/{Decoder.ts → merge.ts} +23 -20
  105. package/src/import/validators/fields/nestable/Image/{Validator.ts → validators.ts} +9 -2
  106. package/src/import/validators/fields/nestable/ImportRichText/ImportBlock.ts +39 -0
  107. package/src/import/validators/fields/nestable/ImportRichText/ImportRichText.ts +41 -0
  108. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.ts +44 -0
  109. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.ts +13 -0
  110. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.ts +13 -0
  111. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.ts +56 -0
  112. package/src/import/validators/fields/nestable/ImportRichText/blocks/Span.ts +44 -0
  113. package/src/import/validators/fields/nestable/ImportRichText/blocks/index.ts +4 -0
  114. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.ts +24 -0
  115. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.ts +8 -0
  116. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.ts +26 -0
  117. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/index.ts +2 -0
  118. package/src/import/validators/fields/nestable/ImportRichText/index.ts +2 -0
  119. package/src/import/validators/fields/nestable/Link.ts +18 -13
  120. package/src/import/validators/fields/nestable/Nestable.ts +6 -1
  121. package/src/import/validators/fields/nestable/index.ts +2 -1
  122. package/lib/import/validators/fields/nestable/Image/Decoder.d.ts +0 -26
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Span = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const function_1 = require("../../../../../../validators/function");
7
+ const spans_1 = require("./spans");
8
+ const SpanType = (0, function_1.withCustomError)(t.union([spans_1.HyperlinkSpanType, spans_1.TextSpanType]), () => `Span 'type' field must be specified and have one of the following values: ${Object.keys(spans_1.TextSpanType.keys).join(", ")} or ${spans_1.HyperlinkSpanType.value}`);
9
+ const SpanTypeValidator = (0, function_1.withCustomError)(t.type({
10
+ type: SpanType,
11
+ }), () => "Span must be an object");
12
+ const SpanShape = t.union([spans_1.HyperlinkSpan, spans_1.TextSpan]);
13
+ exports.Span = SpanTypeValidator.pipe(new t.Type("Span", (u) => SpanShape.is(u), (u, c) => {
14
+ if (spans_1.HyperlinkSpanType.is(u.type)) {
15
+ return spans_1.HyperlinkSpan.validate(u, c);
16
+ }
17
+ else {
18
+ return spans_1.TextSpan.validate(u, c);
19
+ }
20
+ }, t.identity));
@@ -0,0 +1,4 @@
1
+ export * from "./ImportBlockType";
2
+ export * from "./ImportEmbedBlock";
3
+ export * from "./ImportImageBlock";
4
+ export * from "./ImportTextBlock";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./ImportBlockType"), exports);
5
+ (0, tslib_1.__exportStar)(require("./ImportEmbedBlock"), exports);
6
+ (0, tslib_1.__exportStar)(require("./ImportImageBlock"), exports);
7
+ (0, tslib_1.__exportStar)(require("./ImportTextBlock"), exports);
@@ -0,0 +1,24 @@
1
+ import * as t from "io-ts";
2
+ export declare const HyperlinkSpanType: t.LiteralC<"hyperlink">;
3
+ export declare type HyperlinkSpanType = t.TypeOf<typeof HyperlinkSpanType>;
4
+ export declare const HyperlinkSpan: t.ExactC<t.IntersectionC<[t.TypeC<{
5
+ type: t.LiteralC<"hyperlink">;
6
+ }>, t.TypeC<{
7
+ start: t.NumberC;
8
+ end: t.NumberC;
9
+ }>, t.TypeC<{
10
+ data: t.Type<({
11
+ link_type: "Web";
12
+ url: string;
13
+ } & {
14
+ target?: string;
15
+ }) | {
16
+ link_type: "Document";
17
+ id: string;
18
+ } | {
19
+ link_type: "Media";
20
+ id: string;
21
+ }, {
22
+ link_type: "Document" | "Web" | "Media";
23
+ }, unknown>;
24
+ }>]>>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HyperlinkSpan = exports.HyperlinkSpanType = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const function_1 = require("../../../../../../../validators/function");
7
+ const Link_1 = require("../../../Link");
8
+ const SpanLocation_1 = require("./SpanLocation");
9
+ exports.HyperlinkSpanType = t.literal("hyperlink");
10
+ exports.HyperlinkSpan = t.exact(t.intersection([
11
+ t.type({
12
+ type: exports.HyperlinkSpanType,
13
+ }),
14
+ SpanLocation_1.SpanLocation,
15
+ (0, function_1.withCustomError)(t.type({
16
+ data: Link_1.Link,
17
+ }), () => "Hyperlink span must contain a 'data' field with link data"),
18
+ ]));
@@ -0,0 +1,5 @@
1
+ import * as t from "io-ts";
2
+ export declare const SpanLocation: t.TypeC<{
3
+ start: t.NumberC;
4
+ end: t.NumberC;
5
+ }>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SpanLocation = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const validators_1 = require("../../../../../../../validators");
7
+ exports.SpanLocation = t.type({
8
+ start: validators_1.Number,
9
+ end: validators_1.Number,
10
+ });
@@ -0,0 +1,17 @@
1
+ import * as t from "io-ts";
2
+ export declare const TextSpanType: t.KeyofC<{
3
+ strong: null;
4
+ em: null;
5
+ label: null;
6
+ }>;
7
+ export declare const TextSpan: t.ExactC<t.IntersectionC<[t.TypeC<{
8
+ type: t.KeyofC<{
9
+ strong: null;
10
+ em: null;
11
+ label: null;
12
+ }>;
13
+ }>, t.TypeC<{
14
+ start: t.NumberC;
15
+ end: t.NumberC;
16
+ }>]>>;
17
+ export declare type TextSpan = t.TypeOf<typeof TextSpan>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TextSpan = exports.TextSpanType = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const SpanLocation_1 = require("./SpanLocation");
7
+ const TextSpanTypes = {
8
+ Strong: "strong",
9
+ Em: "em",
10
+ Label: "label",
11
+ };
12
+ exports.TextSpanType = t.keyof({
13
+ [TextSpanTypes.Strong]: null,
14
+ [TextSpanTypes.Em]: null,
15
+ [TextSpanTypes.Label]: null,
16
+ });
17
+ exports.TextSpan = t.exact(t.intersection([
18
+ t.type({
19
+ type: exports.TextSpanType,
20
+ }),
21
+ SpanLocation_1.SpanLocation,
22
+ ]));
@@ -0,0 +1,2 @@
1
+ export * from "./HyperlinkSpan";
2
+ export * from "./TextSpan";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./HyperlinkSpan"), exports);
5
+ (0, tslib_1.__exportStar)(require("./TextSpan"), exports);
@@ -0,0 +1,2 @@
1
+ export { ImportBlock } from "./ImportBlock";
2
+ export { ImportRichText } from "./ImportRichText";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImportRichText = exports.ImportBlock = void 0;
4
+ var ImportBlock_1 = require("./ImportBlock");
5
+ Object.defineProperty(exports, "ImportBlock", { enumerable: true, get: function () { return ImportBlock_1.ImportBlock; } });
6
+ var ImportRichText_1 = require("./ImportRichText");
7
+ Object.defineProperty(exports, "ImportRichText", { enumerable: true, get: function () { return ImportRichText_1.ImportRichText; } });
@@ -1,22 +1,37 @@
1
1
  import type { TypeOf } from "io-ts";
2
2
  import * as t from "io-ts";
3
- declare const WebLink: t.IntersectionC<[t.TypeC<{
3
+ declare const WebLink: t.ExactC<t.IntersectionC<[t.TypeC<{
4
4
  link_type: t.LiteralC<"Web">;
5
5
  url: t.StringC;
6
6
  }>, t.PartialC<{
7
7
  target: t.StringC;
8
- }>]>;
8
+ }>]>>;
9
9
  export declare type WebLink = t.TypeOf<typeof WebLink>;
10
- declare const DocumentLink: t.TypeC<{
10
+ declare const DocumentLink: t.ExactC<t.TypeC<{
11
11
  link_type: t.LiteralC<"Document">;
12
12
  id: t.Type<string, string, unknown>;
13
- }>;
13
+ }>>;
14
14
  export declare type DocumentLink = t.TypeOf<typeof DocumentLink>;
15
- declare const MediaLink: t.TypeC<{
15
+ declare const MediaLink: t.ExactC<t.TypeC<{
16
16
  link_type: t.LiteralC<"Media">;
17
17
  id: t.StringC;
18
- }>;
18
+ }>>;
19
19
  export declare type MediaLink = t.TypeOf<typeof MediaLink>;
20
+ export declare const Link: t.Type<({
21
+ link_type: "Web";
22
+ url: string;
23
+ } & {
24
+ target?: string;
25
+ }) | {
26
+ link_type: "Document";
27
+ id: string;
28
+ } | {
29
+ link_type: "Media";
30
+ id: string;
31
+ }, {
32
+ link_type: "Document" | "Web" | "Media";
33
+ }, unknown>;
34
+ export declare type Link = TypeOf<typeof Link>;
20
35
  export declare const ImportLink: t.Type<{
21
36
  type: "Link";
22
37
  value: ({
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImportLink = void 0;
3
+ exports.ImportLink = exports.Link = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
6
  const DocumentId_1 = require("../../../../utils/DocumentId");
@@ -10,7 +10,7 @@ const ImportContent_1 = require("../ImportContent");
10
10
  const LinkTypeValidator = t.type({
11
11
  link_type: (0, function_1.withCustomError)(t.union([t.literal("Web"), t.literal("Document"), t.literal("Media")]), () => "The value must be `Web`, `Document` or `Media`"),
12
12
  });
13
- const WebLink = t.intersection([
13
+ const WebLink = t.exact(t.intersection([
14
14
  t.type({
15
15
  link_type: t.literal("Web"),
16
16
  url: validators_1.String,
@@ -18,18 +18,18 @@ const WebLink = t.intersection([
18
18
  t.partial({
19
19
  target: validators_1.String,
20
20
  }),
21
- ]);
22
- const DocumentLink = t.type({
21
+ ]));
22
+ const DocumentLink = t.strict({
23
23
  link_type: t.literal("Document"),
24
24
  id: DocumentId_1.DocumentId,
25
25
  });
26
- const MediaLink = t.type({
26
+ const MediaLink = t.strict({
27
27
  link_type: t.literal("Media"),
28
28
  id: validators_1.String,
29
29
  });
30
- const Link = LinkTypeValidator.pipe(t.union([WebLink, DocumentLink, MediaLink]));
30
+ exports.Link = LinkTypeValidator.pipe(t.union([WebLink, DocumentLink, MediaLink]));
31
31
  // This is the default value for the link
32
- const AnyLink = t.type({
32
+ const AnyLink = t.strict({
33
33
  link_type: t.literal("Any"),
34
34
  });
35
- exports.ImportLink = (0, ImportContent_1.ImportContent)("Link", (0, validators_1.DefaultOrElse)(AnyLink)(Link));
35
+ exports.ImportLink = (0, ImportContent_1.ImportContent)("Link", (0, validators_1.DefaultOrElse)(AnyLink)(exports.Link));
@@ -5,12 +5,13 @@ import { ImportDate } from "./Date";
5
5
  import { ImportEmbed } from "./Embed";
6
6
  import { ImportGeoPoint } from "./GeoPoint";
7
7
  import { ImportImage } from "./Image";
8
+ import { ImportRichText } from "./ImportRichText";
8
9
  import { ImportLink } from "./Link";
9
10
  import { ImportNumber } from "./Number";
10
11
  import { ImportSelect } from "./Select";
11
12
  import { ImportText } from "./Text";
12
13
  import { ImportTimestamp } from "./Timestamp";
13
- export declare type ImportNestable = ImportBoolean | ImportColor | ImportNumber | ImportSelect | ImportText | ImportDate | ImportTimestamp | ImportEmbed | ImportLink | ImportGeoPoint | ImportImage;
14
+ export declare type ImportNestable = ImportBoolean | ImportColor | ImportNumber | ImportSelect | ImportText | ImportDate | ImportTimestamp | ImportEmbed | ImportLink | ImportGeoPoint | ImportImage | ImportRichText;
14
15
  export declare const ImportNestable: {
15
16
  is(u: unknown): u is ImportNestable;
16
17
  decode: (field: NestableWidget) => (content: unknown) => {
@@ -59,6 +60,9 @@ export declare const ImportNestable: {
59
60
  }, {
60
61
  link_type: "Document" | "Web" | "Media";
61
62
  } | undefined, unknown> | import("io-ts").Type<{
63
+ type: "StructuredText";
64
+ value: import("./ImportRichText").ImportBlock[] | null;
65
+ }, unknown[] | undefined, unknown> | import("io-ts").Type<{
62
66
  type: "Number";
63
67
  value: number | null;
64
68
  }, number | undefined, unknown> | import("io-ts").Type<{
@@ -83,6 +87,9 @@ export declare const ImportNestable: {
83
87
  }> | import("fp-ts/lib/Either").Right<{
84
88
  type: "Number";
85
89
  value: number | null;
90
+ }> | import("fp-ts/lib/Either").Right<{
91
+ type: "StructuredText";
92
+ value: import("./ImportRichText").ImportBlock[] | null;
86
93
  }> | import("fp-ts/lib/Either").Right<{
87
94
  type: "Link";
88
95
  value: ({
@@ -7,6 +7,7 @@ const Date_1 = require("./Date");
7
7
  const Embed_1 = require("./Embed");
8
8
  const GeoPoint_1 = require("./GeoPoint");
9
9
  const Image_1 = require("./Image");
10
+ const ImportRichText_1 = require("./ImportRichText");
10
11
  const Link_1 = require("./Link");
11
12
  const Number_1 = require("./Number");
12
13
  const Select_1 = require("./Select");
@@ -24,7 +25,8 @@ exports.ImportNestable = {
24
25
  Embed_1.ImportEmbed.is(u) ||
25
26
  Link_1.ImportLink.is(u) ||
26
27
  GeoPoint_1.ImportGeoPoint.is(u) ||
27
- (0, Image_1.ImportImage)().is(u));
28
+ (0, Image_1.ImportImage)().is(u) ||
29
+ (0, ImportRichText_1.ImportRichText)().is(u));
28
30
  },
29
31
  decode: (field) => {
30
32
  return (content) => {
@@ -52,6 +54,8 @@ exports.ImportNestable = {
52
54
  return (0, Image_1.ImportImage)(field);
53
55
  case "GeoPoint":
54
56
  return GeoPoint_1.ImportGeoPoint;
57
+ case "StructuredText":
58
+ return (0, ImportRichText_1.ImportRichText)(field);
55
59
  default:
56
60
  throw new Error(`Unsupported type of nestable field ${field.type}`);
57
61
  }
@@ -1,10 +1,11 @@
1
1
  export * from "./Boolean";
2
2
  export * from "./Color";
3
3
  export * from "./Date";
4
- export * from "./Embed";
4
+ export { ImportEmbed } from "./Embed";
5
5
  export * from "./GeoPoint";
6
6
  export * from "./Image";
7
7
  export * from "./Image/index";
8
+ export * from "./ImportRichText";
8
9
  export * from "./Link";
9
10
  export * from "./Nestable";
10
11
  export * from "./Number";
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImportEmbed = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  (0, tslib_1.__exportStar)(require("./Boolean"), exports);
5
6
  (0, tslib_1.__exportStar)(require("./Color"), exports);
6
7
  (0, tslib_1.__exportStar)(require("./Date"), exports);
7
- (0, tslib_1.__exportStar)(require("./Embed"), exports);
8
+ var Embed_1 = require("./Embed");
9
+ Object.defineProperty(exports, "ImportEmbed", { enumerable: true, get: function () { return Embed_1.ImportEmbed; } });
8
10
  (0, tslib_1.__exportStar)(require("./GeoPoint"), exports);
9
11
  (0, tslib_1.__exportStar)(require("./Image"), exports);
10
12
  (0, tslib_1.__exportStar)(require("./Image/index"), exports);
13
+ (0, tslib_1.__exportStar)(require("./ImportRichText"), exports);
11
14
  (0, tslib_1.__exportStar)(require("./Link"), exports);
12
15
  (0, tslib_1.__exportStar)(require("./Nestable"), exports);
13
16
  (0, tslib_1.__exportStar)(require("./Number"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-traverse.alpha-1",
3
+ "version": "2.2.0-traverse.alpha-3",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -1,9 +1,41 @@
1
- import type { SliceContent, WidgetContent } from "../content"
2
- import type { StaticSlice, StaticWidget } from "../customtypes"
1
+ import type {
2
+ SharedSliceItemContent,
3
+ SliceItemContent,
4
+ WidgetContent,
5
+ } from "../content"
6
+ import type {
7
+ CompositeSliceFields,
8
+ Group,
9
+ NestableWidget,
10
+ StaticSlices,
11
+ UID,
12
+ VariationFields,
13
+ } from "../customtypes"
3
14
 
4
- export type TraverseContentFn = <
5
- C extends WidgetContent | SliceContent,
6
- D extends StaticWidget | StaticSlice,
15
+ export type PickOnly<T, K extends keyof T> = Pick<T, K> & {
16
+ [P in Exclude<keyof T, K>]?: never
17
+ }
18
+
19
+ export type TraverseSliceContentFn = <
20
+ S extends SliceItemContent | SharedSliceItemContent,
21
+ D extends VariationFields | CompositeSliceFields | Group | NestableWidget,
22
+ >({
23
+ path,
24
+ key,
25
+ apiId,
26
+ model,
27
+ content,
28
+ }: {
29
+ path: ContentPath
30
+ key: string
31
+ apiId: string
32
+ model?: D | undefined
33
+ content: S
34
+ }) => S | SharedSliceItemContent | undefined
35
+
36
+ export type TraverseWidgetContentFn = <
37
+ C extends WidgetContent,
38
+ D extends NestableWidget | StaticSlices | Group | UID,
7
39
  >({
8
40
  path,
9
41
  key,
@@ -21,8 +53,10 @@ export type TraverseContentFn = <
21
53
  export type ContentPathEntry = {
22
54
  type:
23
55
  | "CustomType"
24
- | WidgetContent["__TYPE__"]
25
- | SliceContent["__TYPE__"]
56
+ | "Widget"
57
+ | "SharedSlice"
58
+ | "Slice"
59
+ | "LegacySlice"
26
60
  | "GroupItem"
27
61
  | "primary"
28
62
  | "items"
@@ -36,4 +70,26 @@ export const ContentPath = {
36
70
  serialize(path: ContentPath): string {
37
71
  return path.map((entry) => entry.key).join(PATH_SEPARATOR)
38
72
  },
73
+ current(path: ContentPath): ContentPathEntry | undefined {
74
+ return path[path.length - 1]
75
+ },
76
+ append(path: string, strElement: string): string {
77
+ return path + PATH_SEPARATOR + strElement
78
+ },
79
+ make(
80
+ entries: Array<{
81
+ key?: ContentPathEntry["key"] | undefined
82
+ type: ContentPathEntry["type"]
83
+ }>,
84
+ ): ContentPath {
85
+ return entries.reduce<ContentPath>((acc, { key, type }) => {
86
+ return key ? acc.concat({ key, type }) : acc
87
+ }, [])
88
+ },
39
89
  }
90
+
91
+ export type SliceModel =
92
+ | VariationFields
93
+ | NestableWidget
94
+ | CompositeSliceFields
95
+ | Group
@@ -3,15 +3,18 @@ import { isLeft, isRight } from "fp-ts/lib/Either"
3
3
  import { pipe } from "fp-ts/lib/function"
4
4
  import * as t from "io-ts"
5
5
 
6
- import { type TraverseContentFn, ContentPath } from "../_internal/utils"
7
- import { WidgetKey } from "../common"
8
6
  import {
9
- type StaticCustomType,
10
- type StaticWidget,
11
- flattenStaticWidgets,
12
- } from "../customtypes"
7
+ ContentPath,
8
+ TraverseSliceContentFn,
9
+ TraverseWidgetContentFn,
10
+ } from "../_internal/utils"
11
+ import { WidgetKey } from "../common"
12
+ import { type StaticWidget, collectSharedSlices } from "../customtypes"
13
13
  import {
14
- SliceContent,
14
+ isCompositeSliceItemContent,
15
+ isSimpleSliceItemContent,
16
+ migrateCompositeSlice,
17
+ migrateSimpleSlice,
15
18
  traverseGroupContent,
16
19
  traverseSlices,
17
20
  WidgetContent,
@@ -119,7 +122,13 @@ function extractMetadata(data: { [p: string]: unknown }): {
119
122
  }
120
123
  }
121
124
 
122
- function parseLegacyDocument(legacyDoc: unknown): Document | undefined {
125
+ function parseLegacyDocument(
126
+ legacyDoc: unknown,
127
+ customType: {
128
+ customTypeId: string
129
+ fields: Record<string, StaticWidget>
130
+ },
131
+ ): Document | undefined {
123
132
  const result = pipe(
124
133
  // ensure it's the right document format first
125
134
  t.record(WidgetKey, t.unknown).decode(legacyDoc),
@@ -131,7 +140,7 @@ function parseLegacyDocument(legacyDoc: unknown): Document | undefined {
131
140
  }),
132
141
  )
133
142
 
134
- return isLeft(result) ? undefined : result.right
143
+ return isLeft(result) ? undefined : migrateDocument(result.right, customType)
135
144
  }
136
145
 
137
146
  function encodeToLegacyDocument(document: Document): DocumentLegacy {
@@ -153,50 +162,74 @@ export const DocumentLegacy = {
153
162
  * @param transform: A user function that provides a way to transform any kind of content wherever it is in a structured Prismic object content.
154
163
  * @returns a transformed document with the user's transformation applied with the transform function
155
164
  */
156
- export function traverseDocument(document: Document, model?: StaticCustomType) {
157
- return (transform: TraverseContentFn): Document => {
158
- const fieldDefs =
165
+ export function traverseDocument({
166
+ document,
167
+ model,
168
+ }: {
169
+ document: Document
170
+ model?: {
171
+ customTypeId: string
172
+ fields: Record<string, StaticWidget>
173
+ }
174
+ }) {
175
+ return ({
176
+ transformWidget = ({ content }) => content,
177
+ transformSlice = ({ content }) => content,
178
+ }: {
179
+ transformWidget?: TraverseWidgetContentFn
180
+ transformSlice?: TraverseSliceContentFn
181
+ }): Document => {
182
+ const fieldModels =
159
183
  model &&
160
- flattenStaticWidgets(model).reduce<Record<string, StaticWidget>>(
184
+ Object.entries(model.fields).reduce<Record<string, StaticWidget>>(
161
185
  (acc, [key, def]) => ({ ...acc, [key]: def }),
162
186
  {},
163
187
  )
164
188
 
165
189
  return Object.entries(document).reduce((acc, [key, content]) => {
166
- const fieldDef = fieldDefs && fieldDefs[key]
190
+ const fieldModel = fieldModels && fieldModels[key]
167
191
 
168
- const path = [{ key, type: content.__TYPE__ }]
169
- const transformedWidgetContent = (() => {
192
+ const path = ContentPath.make([
193
+ { key: model?.customTypeId, type: "CustomType" },
194
+ { key, type: "Widget" },
195
+ ])
196
+
197
+ const transformedWidget = (() => {
170
198
  switch (content.__TYPE__) {
171
199
  case "SliceContentType":
172
200
  return traverseSlices({
173
201
  path,
202
+ key,
174
203
  model:
175
- fieldDef?.type === "Slices" || fieldDef?.type === "Choice"
176
- ? fieldDef
204
+ fieldModel?.type === "Slices" || fieldModel?.type === "Choice"
205
+ ? fieldModel
177
206
  : undefined,
178
207
  content,
179
- })(transform)
208
+ })(transformWidget, transformSlice)
180
209
  case "GroupContentType":
181
210
  return traverseGroupContent({
182
211
  path,
183
- model: fieldDef?.type === "Group" ? fieldDef : undefined,
212
+ key,
213
+ apiId: key,
214
+ model: fieldModel?.type === "Group" ? fieldModel : undefined,
184
215
  content,
185
- })(transform)
216
+ })(transformWidget)
186
217
  default:
187
- return content
218
+ return transformWidget({
219
+ path,
220
+ key,
221
+ apiId: key,
222
+ model:
223
+ fieldModel?.type !== "Group" &&
224
+ fieldModel?.type !== "Slices" &&
225
+ fieldModel?.type !== "Choice"
226
+ ? fieldModel
227
+ : undefined,
228
+ content,
229
+ })
188
230
  }
189
231
  })()
190
232
 
191
- const transformedWidget =
192
- transformedWidgetContent &&
193
- transform({
194
- path,
195
- key,
196
- apiId: key,
197
- model: fieldDef,
198
- content: transformedWidgetContent,
199
- })
200
233
  return {
201
234
  ...acc,
202
235
  ...(transformedWidget ? { [key]: transformedWidget } : {}),
@@ -205,26 +238,52 @@ export function traverseDocument(document: Document, model?: StaticCustomType) {
205
238
  }
206
239
  }
207
240
 
208
- /**
209
- * 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
210
- *
211
- * @param document parsed prismic content
212
- * @param is typeguard to match specifically the type of widget we want to collect
213
- * @returns a record containing the path of the widget as key and the typed collected content as value
214
- */
215
- export function collectWidgets<W extends WidgetContent | SliceContent>(
241
+ // /**
242
+ // * 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
243
+ // *
244
+ // * @param document parsed prismic content
245
+ // * @param is typeguard to match specifically the type of widget we want to collect
246
+ // * @returns a record containing the path of the widget as key and the typed collected content as value
247
+ // */
248
+ export function collectWidgets<W extends WidgetContent>(
216
249
  document: Document,
217
- is: (
218
- content: WidgetContent | SliceContent,
219
- path: ContentPath,
220
- ) => content is W,
250
+ is: (content: WidgetContent, path: ContentPath) => content is W,
221
251
  ): Record<string, W> {
222
252
  const collected: Record<string, W> = {}
223
253
 
224
- traverseDocument(document)(({ content, path }) => {
225
- const key = ContentPath.serialize(path)
226
- if (is(content, path)) collected[key] = content
227
- return content
254
+ traverseDocument({ document })({
255
+ transformWidget: ({ content, path }) => {
256
+ const key = ContentPath.serialize(path)
257
+ if (is(content, path)) collected[key] = content
258
+ return content
259
+ },
228
260
  })
229
261
  return collected
230
262
  }
263
+
264
+ export function migrateDocument(
265
+ document: Document,
266
+ customType: {
267
+ customTypeId: string
268
+ fields: Record<string, StaticWidget>
269
+ },
270
+ ) {
271
+ const needsMigration = Object.values(collectSharedSlices(customType)).some(
272
+ (slice) => Boolean(slice.legacyPaths),
273
+ )
274
+
275
+ if (!needsMigration) return document
276
+
277
+ return traverseDocument({
278
+ document,
279
+ model: customType,
280
+ })({
281
+ transformSlice: ({ content, model }) => {
282
+ if (isCompositeSliceItemContent(content) && model?.type === "SharedSlice")
283
+ return migrateCompositeSlice(model, content)
284
+ if (isSimpleSliceItemContent(content) && model?.type === "SharedSlice")
285
+ return migrateSimpleSlice(model, content)
286
+ return content
287
+ },
288
+ })
289
+ }