@prismicio/types-internal 2.5.0 → 2.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -7,7 +7,6 @@ import type {
7
7
  CompositeSlice,
8
8
  Group,
9
9
  NestableWidget,
10
- NestedGroup,
11
10
  StaticSlices,
12
11
  UID,
13
12
  VariationFields,
@@ -89,4 +88,4 @@ export type SliceModel =
89
88
  | VariationFields
90
89
  | NestableWidget
91
90
  | CompositeSlice
92
- | NestedGroup
91
+ | Group
@@ -15,6 +15,7 @@ import {
15
15
  WithTypes,
16
16
  } from "../../../LegacyContentCtx"
17
17
  import { hasContentType } from "../../../utils"
18
+ import { GroupItemContent, traverseGroupItemsContent } from "../../GroupContent"
18
19
  import {
19
20
  isNestableContent,
20
21
  NestableContent,
@@ -28,12 +29,7 @@ import type {
28
29
  CompositeSliceItemContent,
29
30
  SharedSliceItemContent,
30
31
  } from "../SliceItem"
31
- import {
32
- RepeatableWidgetItemContent,
33
- RepeatableWidgets,
34
- RepeatableWidgetsLegacy,
35
- traverseRepeatableWidgetItemsContent,
36
- } from "./RepeatableContent"
32
+ import { RepeatableWidgets, RepeatableWidgetsLegacy } from "./RepeatableContent"
37
33
 
38
34
  export const CompositeSliceContentType = "CompositeSliceContent"
39
35
 
@@ -212,7 +208,7 @@ export function traverseCompositeSliceContent({
212
208
  }
213
209
  }, {})
214
210
 
215
- const items = traverseRepeatableWidgetItemsContent({
211
+ const items = traverseGroupItemsContent({
216
212
  path: path.concat([{ key: "repeat", type: "items" }]),
217
213
  model:
218
214
  model?.type === "SharedSlice" ? model?.fields.items : model?.repeat,
@@ -264,7 +260,7 @@ export function migrateCompositeSlice(
264
260
  items: content.widget.repeat.map((groupItem) => {
265
261
  return {
266
262
  __TYPE__: "GroupItemContent",
267
- value: groupItem.value.reduce<RepeatableWidgetItemContent["value"]>(
263
+ value: groupItem.value.reduce<GroupItemContent["value"]>(
268
264
  (acc, [fieldKey, fieldContent]) => {
269
265
  if (!model.fields.items?.[fieldKey]) {
270
266
  console.warn(
@@ -1,252 +1,21 @@
1
- import { either } from "fp-ts"
2
- import { isLeft } from "fp-ts/lib/Either"
3
- import { pipe } from "fp-ts/lib/function"
4
1
  import * as t from "io-ts"
5
2
 
6
- import type {
7
- ContentPath,
8
- TraverseWidgetContentFn,
9
- } from "../../../../_internal/utils"
10
- import type { NestableWidget, NestedGroup } from "../../../../customtypes"
3
+ import type { LegacyContentCtx } from "../../../LegacyContentCtx"
11
4
  import {
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
- }
5
+ GroupContent,
6
+ GroupItemContentType,
7
+ GroupItemLegacy,
8
+ } from "../../GroupContent"
9
+ import { NestableContent } from "../../nestable"
244
10
 
245
11
  export const RepeatableWidgetsLegacy = (ctx: LegacyContentCtx) => {
246
- return t.array(RepeatableWidgetItemLegacy(ctx))
12
+ return t.array(GroupItemLegacy(ctx))
247
13
  }
248
14
 
249
- const RepeatableWidget = t.tuple([t.string, NestableContent])
15
+ const RepeatableWidget = t.tuple([
16
+ t.string,
17
+ t.union([NestableContent, GroupContent]),
18
+ ])
250
19
  const RepeatableWidgetsBlock = t.strict({
251
20
  __TYPE__: t.literal(GroupItemContentType),
252
21
  value: t.array(RepeatableWidget),
@@ -20,18 +20,18 @@ import {
20
20
  WithTypes,
21
21
  } from "../../../LegacyContentCtx"
22
22
  import { hasContentType } from "../../../utils"
23
- import { isGroupContent, traverseGroupContent } from "../../GroupContent"
23
+ import {
24
+ isGroupContent,
25
+ traverseGroupContent,
26
+ traverseGroupItemsContent,
27
+ } from "../../GroupContent"
24
28
  import { isNestableContent } from "../../nestable"
25
29
  import {
26
30
  repeatableContentWithDefaultNestableContentValues,
27
31
  withDefaultSlicePrimaryContentValues,
28
32
  } from "../../withDefaultValues"
29
33
  import type { SharedSliceItemContent } from "../SliceItem"
30
- import {
31
- RepeatableWidgets,
32
- RepeatableWidgetsLegacy,
33
- traverseRepeatableWidgetItemsContent,
34
- } from "./RepeatableContent"
34
+ import { RepeatableWidgets, RepeatableWidgetsLegacy } 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 = traverseRepeatableWidgetItemsContent({
248
+ const items = traverseGroupItemsContent({
249
249
  path: path.concat([{ key: "items", type: "items" }]),
250
250
  model: model?.fields.items,
251
251
  content: content.widget.items,
@@ -6,11 +6,18 @@ import type {
6
6
  TraverseWidgetContentFn,
7
7
  } from "../../../../_internal/utils"
8
8
  import type {
9
+ Group,
9
10
  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"
14
21
  import {
15
22
  isNestableContent,
16
23
  NestableContent,
@@ -20,35 +27,24 @@ import type {
20
27
  SharedSliceItemContent,
21
28
  SimpleSliceItemContent,
22
29
  } 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([
32
- NestableContent,
33
- RepeatableWidgetContent,
34
- ])
31
+ export const SimpleSliceContent = t.union([NestableContent, GroupContent])
35
32
  export type SimpleSliceContent = t.TypeOf<typeof SimpleSliceContent>
36
33
 
37
34
  export const isSimpleSliceContent = (u: unknown): u is SimpleSliceContent =>
38
- isNestableContent(u) || isRepeatableWidgetContent(u)
35
+ isNestableContent(u) || isGroupContent(u)
39
36
 
40
37
  export const SimpleSliceLegacy = (ctx: LegacyContentCtx) => {
41
38
  return {
42
39
  decode: (() => {
43
- if (ctx.fieldType === "Group")
44
- return RepeatableWidgetLegacy(ctx).decode.bind(null)
40
+ if (ctx.fieldType === "Group") return GroupLegacy(ctx).decode.bind(null)
45
41
  return NestableLegacy(ctx).decode.bind(null)
46
42
  })(),
47
43
 
48
44
  encode: (value: SimpleSliceContent) => {
49
45
  switch (value.__TYPE__) {
50
46
  case "GroupContentType":
51
- return RepeatableWidgetLegacy(ctx).encode(value)
47
+ return GroupLegacy(ctx).encode(value)
52
48
  default:
53
49
  return NestableLegacy(ctx).encode(value)
54
50
  }
@@ -67,15 +63,15 @@ export function traverseSimpleSliceContent({
67
63
  sliceKey: string
68
64
  sliceName: string
69
65
  content: SimpleSliceItemContent
70
- model?: VariationFields | NestedGroup | NestableWidget | undefined
66
+ model?: VariationFields | Group | NestableWidget | undefined
71
67
  }) {
72
68
  return (
73
69
  transformWidget: TraverseWidgetContentFn,
74
70
  transformSlice: TraverseSliceContentFn,
75
71
  ): SharedSliceItemContent | SimpleSliceItemContent | undefined => {
76
- if (isRepeatableWidgetContent(content.widget)) {
77
- const convertedGroupWidget: RepeatableWidgetContent | undefined =
78
- traverseRepeatableWidgetContent({
72
+ if (isGroupContent(content.widget)) {
73
+ const convertedGroupWidget: GroupContent | undefined =
74
+ traverseGroupContent({
79
75
  path,
80
76
  key: content.key,
81
77
  apiId: content.name,
@@ -154,7 +150,7 @@ export function migrateSimpleSlice(
154
150
  items: content.widget.value.map((groupItem) => {
155
151
  return {
156
152
  __TYPE__: "GroupItemContent",
157
- value: groupItem.value.reduce<RepeatableWidgetItemContent["value"]>(
153
+ value: groupItem.value.reduce<GroupItemContent["value"]>(
158
154
  (acc, [fieldKey, fieldContent]) => {
159
155
  return model.fields.items?.[fieldKey]
160
156
  ? acc.concat([[fieldKey, fieldContent]])
@@ -47,7 +47,6 @@ 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"
51
50
  export * from "./SharedSliceContent"
52
51
  export * from "./SimpleSliceContent"
53
52
  export * from "./SlicePrimaryContent"
@@ -11,11 +11,11 @@ import {
11
11
  import {
12
12
  type StaticSlices,
13
13
  CompositeSlice,
14
+ Group,
14
15
  isCompositeSlice,
15
16
  isLegacySlice,
16
17
  isStaticSharedSlice,
17
18
  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
- | NestedGroup
180
+ | Group
181
181
  | undefined => {
182
182
  if (!defaultModel) return
183
183