@prismicio/types-internal 2.2.0-traverse.alpha-6 → 2.2.0-traverse.alpha-8

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 (75) hide show
  1. package/lib/_internal/utils.d.ts +3 -6
  2. package/lib/content/Document.d.ts +1248 -32
  3. package/lib/content/Document.js +1 -1
  4. package/lib/content/fields/GroupContent.d.ts +273 -7
  5. package/lib/content/fields/WidgetContent.d.ts +1248 -32
  6. package/lib/content/fields/nestable/NestableContent.d.ts +156 -6
  7. package/lib/content/fields/nestable/NestableContent.js +1 -29
  8. package/lib/content/fields/nestable/RichTextContent/Blocks.d.ts +582 -28
  9. package/lib/content/fields/nestable/RichTextContent/Blocks.js +29 -46
  10. package/lib/content/fields/nestable/RichTextContent/index.d.ts +195 -5
  11. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +314 -10
  12. package/lib/content/fields/slices/Slice/CompositeSliceContent.js +18 -12
  13. package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +117 -3
  14. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +312 -8
  15. package/lib/content/fields/slices/Slice/SharedSliceContent.js +5 -1
  16. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +312 -8
  17. package/lib/content/fields/slices/Slice/SimpleSliceContent.js +1 -1
  18. package/lib/content/fields/slices/Slice/index.d.ts +702 -18
  19. package/lib/content/fields/slices/SliceItem.d.ts +702 -18
  20. package/lib/content/fields/slices/SlicesContent.d.ts +936 -24
  21. package/lib/content/fields/slices/SlicesContent.js +2 -4
  22. package/lib/customtypes/CustomType.d.ts +1 -1
  23. package/lib/customtypes/CustomType.js +3 -3
  24. package/lib/customtypes/widgets/slices/CompositeSlice.d.ts +0 -5
  25. package/lib/customtypes/widgets/slices/SharedSlice.d.ts +1 -2
  26. package/lib/import/converters/fields/nestable/GeoPoint.d.ts +3 -0
  27. package/lib/import/converters/fields/nestable/GeoPoint.js +15 -0
  28. package/lib/import/converters/fields/nestable/Image.d.ts +25 -3
  29. package/lib/import/converters/fields/nestable/Image.js +21 -25
  30. package/lib/import/converters/fields/nestable/RichText/embedBlock.d.ts +4 -0
  31. package/lib/import/converters/fields/nestable/RichText/embedBlock.js +14 -0
  32. package/lib/import/converters/fields/nestable/RichText/imageBlock.d.ts +4 -0
  33. package/lib/import/converters/fields/nestable/RichText/imageBlock.js +12 -0
  34. package/lib/import/converters/fields/nestable/RichText/index.d.ts +4 -0
  35. package/lib/import/converters/fields/nestable/RichText/index.js +22 -0
  36. package/lib/import/converters/fields/nestable/RichText/textBlock.d.ts +4 -0
  37. package/lib/import/converters/fields/nestable/RichText/textBlock.js +41 -0
  38. package/lib/import/converters/fields/nestable/index.d.ts +1 -1
  39. package/lib/import/converters/fields/nestable/index.js +1 -1
  40. package/lib/import/validators/fields/ImportContent.js +3 -3
  41. package/lib/import/validators/fields/ImportField.d.ts +1 -1
  42. package/lib/import/validators/fields/ImportSlices/ImportSlices.js +4 -3
  43. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +1 -1
  44. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.d.ts +1 -1
  45. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.js +20 -14
  46. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.d.ts +16 -2
  47. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.js +7 -5
  48. package/lib/import/validators/fields/nestable/Nestable.d.ts +1 -1
  49. package/lib/validators/DefaultOrElse.js +3 -3
  50. package/package.json +1 -1
  51. package/src/_internal/utils.ts +3 -7
  52. package/src/content/Document.ts +2 -2
  53. package/src/content/fields/nestable/NestableContent.ts +0 -32
  54. package/src/content/fields/nestable/RichTextContent/Blocks.ts +48 -58
  55. package/src/content/fields/slices/Slice/CompositeSliceContent.ts +26 -16
  56. package/src/content/fields/slices/Slice/SharedSliceContent.ts +7 -1
  57. package/src/content/fields/slices/Slice/SimpleSliceContent.ts +1 -1
  58. package/src/content/fields/slices/SlicesContent.ts +4 -6
  59. package/src/customtypes/CustomType.ts +1 -1
  60. package/src/customtypes/widgets/slices/CompositeSlice.ts +0 -6
  61. package/src/customtypes/widgets/slices/SharedSlice.ts +2 -2
  62. package/src/import/converters/fields/nestable/Image.ts +10 -22
  63. package/src/import/converters/fields/nestable/RichText/embedBlock.ts +17 -0
  64. package/src/import/converters/fields/nestable/RichText/imageBlock.ts +17 -0
  65. package/src/import/converters/fields/nestable/RichText/index.ts +23 -0
  66. package/src/import/converters/fields/nestable/RichText/textBlock.ts +48 -0
  67. package/src/import/converters/fields/nestable/index.ts +1 -1
  68. package/src/import/validators/fields/ImportContent.ts +3 -3
  69. package/src/import/validators/fields/ImportSlices/ImportSlices.ts +7 -5
  70. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.ts +1 -1
  71. package/src/import/validators/fields/nestable/ImportRichText/ImportRichText.ts +38 -25
  72. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.ts +9 -5
  73. package/src/validators/DefaultOrElse.ts +4 -4
  74. package/src/import/converters/fields/nestable/RichText.ts +0 -62
  75. /package/src/import/converters/fields/nestable/{GeooPoint.ts → GeoPoint.ts} +0 -0
@@ -1,3 +1,5 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
1
3
  import * as t from "io-ts"
2
4
 
3
5
  import type { RichText as RichTextCustomType } from "../../../../../customtypes"
@@ -6,36 +8,47 @@ import { withCustomError } from "../../../../../validators/function"
6
8
  import { ImportContent } from "../../ImportContent"
7
9
  import { ImportBlock } from "./ImportBlock"
8
10
 
9
- export const ImportRichText = (customType?: RichTextCustomType) =>
10
- ImportContent(
11
+ export const ImportRichText = (customType?: RichTextCustomType) => {
12
+ const single = customType?.config?.single?.split(",")
13
+ const multi = customType?.config?.multi?.split(",")
14
+ const allowedBlockTypes = multi || single
15
+
16
+ const isSingleBlockAllowed = multi === undefined && single !== undefined
17
+
18
+ const BlockCodec = ImportBlock(allowedBlockTypes ?? [])
19
+ const RichTextArrayCodec = withCustomError(
20
+ t.array(BlockCodec),
21
+ () => "Rich text field must be an array",
22
+ )
23
+
24
+ return ImportContent(
11
25
  "StructuredText",
12
26
  EmptyArrayOrElse(
13
- withCustomError(
14
- t.array(t.unknown).pipe(
15
- new t.Type<ImportBlock[], ImportBlock[], unknown[]>(
16
- "RichTextField",
17
- (u): u is ImportBlock[] => t.array(ImportBlock([])).is(u),
18
- (u, c) => {
19
- const single = customType?.config?.single?.split(",")
20
- const multi = customType?.config?.multi?.split(",")
27
+ new t.ArrayType<typeof BlockCodec, ImportBlock[], ImportBlock[]>(
28
+ RichTextArrayCodec.name,
29
+ (u): u is ImportBlock[] => RichTextArrayCodec.is(u),
30
+ (u, c) => {
31
+ if (allowedBlockTypes === undefined) {
32
+ throw new Error(
33
+ "Invalid custom type for the rich text field: the model is missing a 'single' or 'multi' field defining a list of allowed block types",
34
+ )
35
+ }
21
36
 
22
- if (multi) {
23
- return t.array(ImportBlock(multi)).validate(u, c)
24
- } else if (single) {
25
- return u.length > 1
26
- ? t.failure(u, c, "This field only allows one block")
27
- : t.array(ImportBlock(single)).validate(u, c)
28
- } else {
29
- throw new Error(
30
- "Rich text config must have either a 'single' or 'multi' field defined",
31
- )
37
+ return pipe(
38
+ RichTextArrayCodec.validate(u, c),
39
+ E.chain((blocks) => {
40
+ if (isSingleBlockAllowed && blocks.length > 1) {
41
+ return t.failure(blocks, c, "This field only allows one block")
32
42
  }
33
- },
34
- t.identity,
35
- ),
36
- ),
37
- () => "Rich text field must be an array",
43
+ return t.success(blocks)
44
+ }),
45
+ )
46
+ },
47
+ t.identity,
48
+ BlockCodec,
38
49
  ),
39
50
  ),
40
51
  )
52
+ }
53
+
41
54
  export type ImportRichText = t.TypeOf<ReturnType<typeof ImportRichText>>
@@ -1,13 +1,17 @@
1
1
  import * as t from "io-ts"
2
2
 
3
- import { String } from "../../../../../../validators"
3
+ import { ImportImageView } from "../../Image/validators"
4
4
 
5
5
  export const ImportImageBlockType = t.literal("image")
6
6
  export type ImportImageBlockType = t.TypeOf<typeof ImportImageBlockType>
7
7
 
8
- export const ImportImageBlock = t.strict({
9
- type: ImportImageBlockType,
10
- id: String,
11
- })
8
+ export const ImportImageBlock = t.exact(
9
+ t.intersection([
10
+ t.type({
11
+ type: ImportImageBlockType,
12
+ }),
13
+ ImportImageView,
14
+ ]),
15
+ )
12
16
 
13
17
  export type ImportImageBlock = t.TypeOf<typeof ImportImageBlock>
@@ -6,11 +6,11 @@ import { EmptyArray, EmptyObject } from "./BasicTypes"
6
6
  export const DefaultOrElse = <I, A, O = A>(inputValidator: t.Type<I>) => {
7
7
  return (codec: t.Type<A, O>) =>
8
8
  new t.Type<A | null, O | undefined>(
9
- "DefaultOrElse",
9
+ `DefaultOrElse<${inputValidator.name}, ${codec.name}>`,
10
10
  (u: unknown): u is A | null => null === u || codec.is(u),
11
- (u: unknown) => {
12
- if (isRight(inputValidator.decode(u))) return t.success(null)
13
- return codec.decode(u)
11
+ (u: unknown, c) => {
12
+ if (isRight(inputValidator.validate(u, c))) return t.success(null)
13
+ return codec.validate(u, c)
14
14
  },
15
15
  (chunk) => (chunk ? codec.encode(chunk) : undefined),
16
16
  )
@@ -1,62 +0,0 @@
1
- import type { Asset, Embed } from "../../../../common"
2
- import type { Block, RichTextContent } from "../../../../content"
3
- import { RichTextContentType } from "../../../../content"
4
- import type { ImportBlock, ImportRichText } from "../../../validators"
5
- import { embedConverter } from "./Embed"
6
- import { imageBlockConverter } from "./Image"
7
- import { linkConverter } from "./Link"
8
-
9
- const richTextBlockConverter = (
10
- importBlock: ImportBlock,
11
- assets: Record<Asset["id"], Asset | undefined>,
12
- embeds: Record<string, Embed | undefined>,
13
- ): Block => {
14
- if (importBlock.type === "image") {
15
- return {
16
- type: importBlock.type,
17
- data: imageBlockConverter(importBlock, assets),
18
- }
19
- } else if (importBlock.type === "embed") {
20
- const embedData = embedConverter(importBlock.oembed, embeds)
21
- if (!embedData) throw new Error("Failed to convert embed data")
22
- return {
23
- type: importBlock.type,
24
- data: embedData,
25
- }
26
- } else {
27
- // Text block
28
- return {
29
- type: importBlock.type,
30
- direction: importBlock.direction ?? "ltr",
31
- content: {
32
- text: importBlock.text,
33
- spans:
34
- importBlock.spans?.map((span) => {
35
- const linkData =
36
- span.type === "hyperlink"
37
- ? linkConverter(span.data, assets)
38
- : undefined
39
- return {
40
- type: span.type,
41
- start: span.start,
42
- end: span.end,
43
- data: linkData,
44
- }
45
- }) ?? [],
46
- },
47
- }
48
- }
49
- }
50
- export const richTextConverter = (
51
- richTextField: ImportRichText["value"],
52
- assets: Record<Asset["id"], Asset | undefined>,
53
- embeds: Record<string, Embed | undefined>,
54
- ): RichTextContent | undefined => {
55
- if (richTextField === null) return
56
- return {
57
- __TYPE__: RichTextContentType,
58
- value: richTextField.map((block) =>
59
- richTextBlockConverter(block, assets, embeds),
60
- ),
61
- }
62
- }