@prismicio/types-internal 2.5.0-alpha.3 → 2.5.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 (39) hide show
  1. package/README.md +13 -41
  2. package/lib/_internal/utils.d.ts +2 -2
  3. package/lib/content/Document.d.ts +2094 -138
  4. package/lib/content/fields/GroupContent.d.ts +1 -1
  5. package/lib/content/fields/GroupContent.js +1 -4
  6. package/lib/content/fields/WidgetContent.d.ts +2094 -138
  7. package/lib/content/fields/nestable/NestableContent.d.ts +155 -3
  8. package/lib/content/fields/nestable/RichTextContent/Blocks.d.ts +232 -4
  9. package/lib/content/fields/nestable/RichTextContent/Blocks.js +5 -2
  10. package/lib/content/fields/nestable/RichTextContent/TextBlock.d.ts +727 -0
  11. package/lib/content/fields/nestable/RichTextContent/TextBlock.js +80 -0
  12. package/lib/content/fields/nestable/RichTextContent/index.d.ts +194 -4
  13. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +314 -10
  14. package/lib/content/fields/slices/Slice/CompositeSliceContent.js +1 -2
  15. package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +2235 -6
  16. package/lib/content/fields/slices/Slice/RepeatableContent.js +148 -6
  17. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +314 -10
  18. package/lib/content/fields/slices/Slice/SharedSliceContent.js +1 -1
  19. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +1204 -9
  20. package/lib/content/fields/slices/Slice/SimpleSliceContent.js +10 -7
  21. package/lib/content/fields/slices/Slice/SlicePrimaryContent.d.ts +155 -3
  22. package/lib/content/fields/slices/Slice/index.d.ts +1367 -70
  23. package/lib/content/fields/slices/Slice/index.js +1 -0
  24. package/lib/content/fields/slices/SliceItem.d.ts +1445 -149
  25. package/lib/content/fields/slices/SlicesContent.d.ts +2849 -1045
  26. package/lib/customtypes/widgets/slices/SliceWidget.d.ts +327 -0
  27. package/lib/customtypes/widgets/slices/SliceWidget.js +8 -0
  28. package/lib/validators/function.js +1 -8
  29. package/package.json +1 -1
  30. package/src/_internal/utils.ts +2 -1
  31. package/src/content/fields/GroupContent.ts +1 -4
  32. package/src/content/fields/nestable/RichTextContent/Blocks.ts +6 -3
  33. package/src/content/fields/slices/Slice/CompositeSliceContent.ts +8 -4
  34. package/src/content/fields/slices/Slice/RepeatableContent.ts +242 -11
  35. package/src/content/fields/slices/Slice/SharedSliceContent.ts +7 -7
  36. package/src/content/fields/slices/Slice/SimpleSliceContent.ts +21 -17
  37. package/src/content/fields/slices/Slice/index.ts +1 -0
  38. package/src/content/fields/slices/SlicesContent.ts +2 -2
  39. package/src/validators/function.ts +1 -11
@@ -1,21 +1,252 @@
1
+ import { either } from "fp-ts"
2
+ import { isLeft } from "fp-ts/lib/Either"
3
+ import { pipe } from "fp-ts/lib/function"
1
4
  import * as t from "io-ts"
2
5
 
3
- import type { LegacyContentCtx } from "../../../LegacyContentCtx"
6
+ import type {
7
+ ContentPath,
8
+ TraverseWidgetContentFn,
9
+ } from "../../../../_internal/utils"
10
+ import type { NestableWidget, NestedGroup } from "../../../../customtypes"
4
11
  import {
5
- GroupContent,
6
- GroupItemContentType,
7
- GroupItemLegacy,
8
- } from "../../GroupContent"
9
- import { NestableContent } from "../../nestable"
12
+ FieldOrSliceType,
13
+ getFieldCtx,
14
+ LegacyContentCtx,
15
+ WithTypes,
16
+ } from "../../../LegacyContentCtx"
17
+ import { hasContentType } from "../../../utils"
18
+ import { GroupContentType, GroupItemContentType } from "../../GroupContent"
19
+ import {
20
+ isNestableContent,
21
+ NestableContent,
22
+ NestableLegacy,
23
+ } from "../../nestable"
24
+
25
+ /**
26
+ * This is essentially a copy of `GroupItemContent` that doesn't support
27
+ * nested groups to keep accurate content types for legacy slice.
28
+ */
29
+ const RepeatableWidgetItemContent = t.strict({
30
+ __TYPE__: t.literal(GroupItemContentType),
31
+ value: t.array(t.tuple([t.string, NestableContent])),
32
+ })
33
+ export type RepeatableWidgetItemContent = t.TypeOf<
34
+ typeof RepeatableWidgetItemContent
35
+ >
36
+
37
+ const itemLegacyReader = t.record(t.string, t.unknown)
38
+ type RepeatableWidgetItemLegacy = t.TypeOf<typeof itemLegacyReader>
39
+
40
+ /**
41
+ * This is essentially a copy of `GroupItemLegacy` that doesn't support
42
+ * nested groups to keep accurate content types for legacy slice.
43
+ */
44
+ const RepeatableWidgetItemLegacy = (ctx: LegacyContentCtx) => {
45
+ return new t.Type<
46
+ RepeatableWidgetItemContent,
47
+ WithTypes<RepeatableWidgetItemLegacy>
48
+ >(
49
+ "RepeatableWidgetItemLegacy",
50
+ (u): u is RepeatableWidgetItemContent =>
51
+ hasContentType(u) && u.__TYPE__ === GroupItemContentType,
52
+ (u) => {
53
+ const parsed = pipe(
54
+ itemLegacyReader.decode(u),
55
+ either.map((items) => {
56
+ const parsedItems = Object.entries(items).reduce<
57
+ Array<[string, NestableContent]>
58
+ >((acc, [itemKey, itemValue]) => {
59
+ const itemCtx = getFieldCtx(itemKey, ctx)
60
+ const result = NestableLegacy(itemCtx).decode(itemValue)
61
+ if (!result) return acc
62
+
63
+ if (isLeft(result)) return acc
64
+ return [...acc, [itemKey, result.right]]
65
+ }, [])
66
+
67
+ return {
68
+ value: parsedItems,
69
+ __TYPE__: GroupItemContentType,
70
+ }
71
+ }),
72
+ )
73
+ return parsed
74
+ },
75
+ (item) => {
76
+ return item.value.reduce<WithTypes<RepeatableWidgetItemLegacy>>(
77
+ (acc, [key, value]) => {
78
+ const itemCtx = getFieldCtx(key, ctx)
79
+ const encoded = NestableLegacy(itemCtx).encode(value)
80
+ if (!encoded) return acc
81
+
82
+ return {
83
+ content: { ...acc.content, [key]: encoded.content },
84
+ types: { ...acc.types, ...encoded.types },
85
+ }
86
+ },
87
+ { content: {}, types: {} },
88
+ )
89
+ },
90
+ )
91
+ }
92
+
93
+ /**
94
+ * This is essentially a copy of `GroupLegacy` that doesn't support
95
+ * nested groups to keep accurate content types for legacy slice.
96
+ */
97
+ type RepeatableWidgetLegacy = Array<RepeatableWidgetItemLegacy>
98
+ export const RepeatableWidgetLegacy = (ctx: LegacyContentCtx) => {
99
+ const codecDecode = t.array(
100
+ t.union([t.null, RepeatableWidgetItemLegacy(ctx)]),
101
+ )
102
+ const codecEncode = t.array(RepeatableWidgetItemLegacy(ctx))
103
+
104
+ return new t.Type<
105
+ RepeatableWidgetContent,
106
+ WithTypes<RepeatableWidgetLegacy>,
107
+ unknown
108
+ >(
109
+ "RepeatableWidgetLegacy",
110
+ isRepeatableWidgetContent,
111
+ (items) => {
112
+ return pipe(
113
+ codecDecode.decode(items),
114
+ either.map((parsedItems) => {
115
+ return {
116
+ value: parsedItems.map((i) => {
117
+ if (i === null) {
118
+ return { __TYPE__: GroupItemContentType, value: [] }
119
+ } else return i
120
+ }),
121
+ __TYPE__: GroupContentType,
122
+ }
123
+ }),
124
+ )
125
+ },
126
+ (g: RepeatableWidgetContent) => {
127
+ const res = codecEncode.encode(g.value)
128
+ return {
129
+ content: res.map((block) => block.content),
130
+ types: res.reduce<Record<string, FieldOrSliceType>>(
131
+ (acc, block) => {
132
+ return { ...acc, ...block.types }
133
+ },
134
+ { [ctx.keyOfType]: "Group" },
135
+ ),
136
+ }
137
+ },
138
+ )
139
+ }
140
+
141
+ /**
142
+ * This is essentially a copy of `isGroupContent` that doesn't support
143
+ * nested groups to keep accurate content types for legacy slice.
144
+ */
145
+ export const isRepeatableWidgetContent = (
146
+ u: unknown,
147
+ ): u is RepeatableWidgetContent =>
148
+ hasContentType(u) && u.__TYPE__ === GroupContentType
149
+
150
+ export const RepeatableWidgetContent = t.strict({
151
+ __TYPE__: t.literal(GroupContentType),
152
+ value: t.array(RepeatableWidgetItemContent),
153
+ })
154
+ export type RepeatableWidgetContent = t.TypeOf<typeof RepeatableWidgetContent>
155
+
156
+ /**
157
+ * This is essentially a copy of `traverseGroupContent` that doesn't support
158
+ * nested groups to keep accurate content types for legacy slice.
159
+ */
160
+ export function traverseRepeatableWidgetContent({
161
+ path,
162
+ key,
163
+ apiId,
164
+ model,
165
+ content,
166
+ }: {
167
+ path: ContentPath
168
+ key: string
169
+ apiId: string
170
+ content: RepeatableWidgetContent
171
+ model?: NestedGroup | undefined
172
+ }) {
173
+ return (
174
+ transform: TraverseWidgetContentFn,
175
+ ): RepeatableWidgetContent | undefined => {
176
+ const repeatableWidgetItems = traverseRepeatableWidgetItemsContent({
177
+ path,
178
+ model: model?.config?.fields,
179
+ content: content.value,
180
+ })(transform)
181
+
182
+ return transform({
183
+ path,
184
+ key,
185
+ apiId,
186
+ model,
187
+ content: {
188
+ __TYPE__: content.__TYPE__,
189
+ value: repeatableWidgetItems,
190
+ },
191
+ })
192
+ }
193
+ }
194
+
195
+ /**
196
+ * This is essentially a copy of `traverseGroupItemContent` that doesn't support
197
+ * nested groups to keep accurate content types for legacy slice.
198
+ */
199
+ export function traverseRepeatableWidgetItemsContent({
200
+ path,
201
+ model,
202
+ content,
203
+ }: {
204
+ path: ContentPath
205
+ content: Array<RepeatableWidgetItemContent>
206
+ model?: Record<string, NestableWidget> | undefined
207
+ }) {
208
+ return (
209
+ transform: TraverseWidgetContentFn,
210
+ ): Array<RepeatableWidgetItemContent> => {
211
+ return content.map((repeatableWidgetItem, index) => {
212
+ const repeatableWidgetItemPath = path.concat([
213
+ { key: index.toString(), type: "GroupItem" },
214
+ ])
215
+
216
+ const repeatableWidgetItemFields = repeatableWidgetItem.value.reduce<
217
+ RepeatableWidgetItemContent["value"]
218
+ >((acc, [fieldKey, fieldContent]) => {
219
+ const fieldDef = model?.[fieldKey]
220
+
221
+ const transformedField = transform({
222
+ path: repeatableWidgetItemPath.concat([
223
+ { key: fieldKey, type: "Widget" },
224
+ ]),
225
+ key: fieldKey,
226
+ apiId: fieldKey,
227
+ model: fieldDef,
228
+ content: fieldContent,
229
+ })
230
+ // Can happen if the transform function returns undefined to filter out a field
231
+ if (!transformedField || !isNestableContent(transformedField))
232
+ return acc
233
+
234
+ return acc.concat([[fieldKey, transformedField]])
235
+ }, [])
236
+
237
+ return {
238
+ __TYPE__: repeatableWidgetItem.__TYPE__,
239
+ value: repeatableWidgetItemFields,
240
+ }
241
+ })
242
+ }
243
+ }
10
244
 
11
245
  export const RepeatableWidgetsLegacy = (ctx: LegacyContentCtx) => {
12
- return t.array(GroupItemLegacy(ctx))
246
+ return t.array(RepeatableWidgetItemLegacy(ctx))
13
247
  }
14
248
 
15
- const RepeatableWidget = t.tuple([
16
- t.string,
17
- t.union([NestableContent, GroupContent]),
18
- ])
249
+ const RepeatableWidget = t.tuple([t.string, NestableContent])
19
250
  const RepeatableWidgetsBlock = t.strict({
20
251
  __TYPE__: t.literal(GroupItemContentType),
21
252
  value: t.array(RepeatableWidget),
@@ -20,18 +20,18 @@ import {
20
20
  WithTypes,
21
21
  } from "../../../LegacyContentCtx"
22
22
  import { hasContentType } from "../../../utils"
23
- import {
24
- isGroupContent,
25
- traverseGroupContent,
26
- traverseGroupItemsContent,
27
- } from "../../GroupContent"
23
+ import { isGroupContent, traverseGroupContent } from "../../GroupContent"
28
24
  import { isNestableContent } from "../../nestable"
29
25
  import {
30
26
  repeatableContentWithDefaultNestableContentValues,
31
27
  withDefaultSlicePrimaryContentValues,
32
28
  } from "../../withDefaultValues"
33
29
  import type { SharedSliceItemContent } from "../SliceItem"
34
- import { RepeatableWidgets, RepeatableWidgetsLegacy } from "./RepeatableContent"
30
+ import {
31
+ RepeatableWidgets,
32
+ RepeatableWidgetsLegacy,
33
+ traverseRepeatableWidgetItemsContent,
34
+ } from "./RepeatableContent"
35
35
  import {
36
36
  isSlicePrimaryContent,
37
37
  SlicePrimaryContent,
@@ -245,7 +245,7 @@ export function traverseSharedSliceContent({
245
245
  }
246
246
  }, {})
247
247
 
248
- const items = traverseGroupItemsContent({
248
+ const items = traverseRepeatableWidgetItemsContent({
249
249
  path: path.concat([{ key: "items", type: "items" }]),
250
250
  model: model?.fields.items,
251
251
  content: content.widget.items,
@@ -6,18 +6,11 @@ import type {
6
6
  TraverseWidgetContentFn,
7
7
  } from "../../../../_internal/utils"
8
8
  import type {
9
- Group,
10
9
  NestableWidget,
10
+ NestedGroup,
11
11
  VariationFields,
12
12
  } from "../../../../customtypes"
13
13
  import type { LegacyContentCtx } from "../../../LegacyContentCtx"
14
- import {
15
- GroupContent,
16
- GroupItemContent,
17
- GroupLegacy,
18
- isGroupContent,
19
- traverseGroupContent,
20
- } from "../../GroupContent"
21
14
  import {
22
15
  isNestableContent,
23
16
  NestableContent,
@@ -27,24 +20,35 @@ import type {
27
20
  SharedSliceItemContent,
28
21
  SimpleSliceItemContent,
29
22
  } from "../SliceItem"
23
+ import {
24
+ isRepeatableWidgetContent,
25
+ RepeatableWidgetContent,
26
+ RepeatableWidgetItemContent,
27
+ RepeatableWidgetLegacy,
28
+ traverseRepeatableWidgetContent,
29
+ } from "./RepeatableContent"
30
30
 
31
- export const SimpleSliceContent = t.union([NestableContent, GroupContent])
31
+ export const SimpleSliceContent = t.union([
32
+ NestableContent,
33
+ RepeatableWidgetContent,
34
+ ])
32
35
  export type SimpleSliceContent = t.TypeOf<typeof SimpleSliceContent>
33
36
 
34
37
  export const isSimpleSliceContent = (u: unknown): u is SimpleSliceContent =>
35
- isNestableContent(u) || isGroupContent(u)
38
+ isNestableContent(u) || isRepeatableWidgetContent(u)
36
39
 
37
40
  export const SimpleSliceLegacy = (ctx: LegacyContentCtx) => {
38
41
  return {
39
42
  decode: (() => {
40
- if (ctx.fieldType === "Group") return GroupLegacy(ctx).decode.bind(null)
43
+ if (ctx.fieldType === "Group")
44
+ return RepeatableWidgetLegacy(ctx).decode.bind(null)
41
45
  return NestableLegacy(ctx).decode.bind(null)
42
46
  })(),
43
47
 
44
48
  encode: (value: SimpleSliceContent) => {
45
49
  switch (value.__TYPE__) {
46
50
  case "GroupContentType":
47
- return GroupLegacy(ctx).encode(value)
51
+ return RepeatableWidgetLegacy(ctx).encode(value)
48
52
  default:
49
53
  return NestableLegacy(ctx).encode(value)
50
54
  }
@@ -63,15 +67,15 @@ export function traverseSimpleSliceContent({
63
67
  sliceKey: string
64
68
  sliceName: string
65
69
  content: SimpleSliceItemContent
66
- model?: VariationFields | Group | NestableWidget | undefined
70
+ model?: VariationFields | NestedGroup | NestableWidget | undefined
67
71
  }) {
68
72
  return (
69
73
  transformWidget: TraverseWidgetContentFn,
70
74
  transformSlice: TraverseSliceContentFn,
71
75
  ): SharedSliceItemContent | SimpleSliceItemContent | undefined => {
72
- if (isGroupContent(content.widget)) {
73
- const convertedGroupWidget: GroupContent | undefined =
74
- traverseGroupContent({
76
+ if (isRepeatableWidgetContent(content.widget)) {
77
+ const convertedGroupWidget: RepeatableWidgetContent | undefined =
78
+ traverseRepeatableWidgetContent({
75
79
  path,
76
80
  key: content.key,
77
81
  apiId: content.name,
@@ -150,7 +154,7 @@ export function migrateSimpleSlice(
150
154
  items: content.widget.value.map((groupItem) => {
151
155
  return {
152
156
  __TYPE__: "GroupItemContent",
153
- value: groupItem.value.reduce<GroupItemContent["value"]>(
157
+ value: groupItem.value.reduce<RepeatableWidgetItemContent["value"]>(
154
158
  (acc, [fieldKey, fieldContent]) => {
155
159
  return model.fields.items?.[fieldKey]
156
160
  ? acc.concat([[fieldKey, fieldContent]])
@@ -47,6 +47,7 @@ export const SliceContent = t.union([
47
47
  export type SliceContent = t.TypeOf<typeof SliceContent>
48
48
 
49
49
  export * from "./CompositeSliceContent"
50
+ export * from "./RepeatableContent"
50
51
  export * from "./SharedSliceContent"
51
52
  export * from "./SimpleSliceContent"
52
53
  export * from "./SlicePrimaryContent"
@@ -11,11 +11,11 @@ import {
11
11
  import {
12
12
  type StaticSlices,
13
13
  CompositeSlice,
14
- Group,
15
14
  isCompositeSlice,
16
15
  isLegacySlice,
17
16
  isStaticSharedSlice,
18
17
  NestableWidget,
18
+ NestedGroup,
19
19
  SharedSlice,
20
20
  VariationFields,
21
21
  } from "../../../customtypes"
@@ -177,7 +177,7 @@ function findSliceModel(
177
177
  const legacySliceModel = ():
178
178
  | NestableWidget
179
179
  | CompositeSlice
180
- | Group
180
+ | NestedGroup
181
181
  | undefined => {
182
182
  if (!defaultModel) return
183
183
 
@@ -19,17 +19,7 @@ export function refineType<A, O, I>(
19
19
  (u, c) =>
20
20
  pipe(
21
21
  type.validate(u, c),
22
- either.chain((v) => {
23
- try {
24
- return pred(v) ? t.success(v) : t.failure(u, c)
25
- } catch (error) {
26
- return t.failure(
27
- u,
28
- c,
29
- error instanceof Error ? error.message : String(error),
30
- )
31
- }
32
- }),
22
+ either.chain((v) => (pred(v) ? t.success(v) : t.failure(u, c))),
33
23
  ),
34
24
  type.encode,
35
25
  )