@prismicio/types-internal 3.16.2-alpha.0 → 3.16.2-alpha.2

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.
@@ -9,6 +9,7 @@ import {
9
9
  ContentPath,
10
10
  hasContentType,
11
11
  TraverseWidgetContentFn,
12
+ TraverseWidgetContentWithModelFn,
12
13
  } from "../../utils"
13
14
  import {
14
15
  isLinkContent,
@@ -162,3 +163,62 @@ export function traverseRepeatableContent({
162
163
  })
163
164
  }
164
165
  }
166
+
167
+ export function traverseRepeatableContentWithModel({
168
+ path,
169
+ key,
170
+ model,
171
+ content,
172
+ }: {
173
+ path: ContentPath
174
+ key: string
175
+ model: NestableWidget
176
+ content: RepeatableContent | undefined
177
+ }): (
178
+ transform: TraverseWidgetContentWithModelFn,
179
+ ) => RepeatableContent | undefined {
180
+ return (transform) => {
181
+ const items =
182
+ content?.value.reduce<Array<LinkContent>>((acc, fieldContent, index) => {
183
+ const itemPath = path.concat([
184
+ { key: index.toString(), type: "Widget" },
185
+ ])
186
+
187
+ const newModel =
188
+ model?.type === "Link" && model.config
189
+ ? {
190
+ ...model,
191
+ config: { ...model.config, repeat: false },
192
+ }
193
+ : model
194
+
195
+ const transformedField = transform({
196
+ path: itemPath,
197
+ key: key,
198
+ apiId: key,
199
+ model: newModel,
200
+ content: fieldContent,
201
+ })
202
+
203
+ if (!transformedField) return acc
204
+
205
+ if (!isLinkContent(transformedField)) return acc
206
+
207
+ return acc.concat(transformedField)
208
+ }, []) ?? []
209
+
210
+ return transform({
211
+ path,
212
+ key,
213
+ apiId: key,
214
+ model,
215
+ content: content
216
+ ? {
217
+ __TYPE__: content.__TYPE__,
218
+ type: content.type,
219
+ value: items,
220
+ }
221
+ : undefined,
222
+ })
223
+ }
224
+ }
@@ -11,6 +11,7 @@ import {
11
11
  ContentPath,
12
12
  hasContentType,
13
13
  TraverseWidgetContentFn,
14
+ TraverseWidgetContentWithModelFn,
14
15
  withKey,
15
16
  } from "../../utils"
16
17
  import { RichTextContent, RichTextLegacyContent } from "./RichTextContent"
@@ -173,3 +174,62 @@ export function traverseTableContent({
173
174
  })
174
175
  }
175
176
  }
177
+
178
+ export function traverseTableContentWithModel({
179
+ path,
180
+ key,
181
+ model,
182
+ content,
183
+ }: {
184
+ path: ContentPath
185
+ key: string
186
+ model: NestableWidget
187
+ content: TableContent | undefined
188
+ }): (transform: TraverseWidgetContentWithModelFn) => TableContent | undefined {
189
+ return (transform) => {
190
+ const tableContent =
191
+ content?.content.map((row, rowIndex) => ({
192
+ ...row,
193
+ content: row.content.map((cell, cellIndex) => {
194
+ const itemPath = path.concat([
195
+ {
196
+ key: [rowIndex.toString(), cellIndex.toString()].join(","),
197
+ type: "Widget",
198
+ },
199
+ ])
200
+
201
+ const cellContent = transform({
202
+ path: itemPath,
203
+ key,
204
+ apiId: key,
205
+ model: TableCellModel,
206
+ content: {
207
+ __TYPE__: cell.content.__TYPE__,
208
+ value: cell.content.value,
209
+ },
210
+ }) || {
211
+ __TYPE__: "StructuredTextContent",
212
+ value: [],
213
+ }
214
+
215
+ return {
216
+ ...cell,
217
+ content: cellContent,
218
+ }
219
+ }),
220
+ })) ?? []
221
+
222
+ return transform({
223
+ path,
224
+ key,
225
+ apiId: key,
226
+ model,
227
+ content: content
228
+ ? {
229
+ __TYPE__: content.__TYPE__,
230
+ content: tableContent,
231
+ }
232
+ : undefined,
233
+ })
234
+ }
235
+ }
@@ -11,6 +11,7 @@ import {
11
11
  isStaticSharedSlice,
12
12
  NestableWidget,
13
13
  SharedSlice,
14
+ Variation,
14
15
  VariationFields,
15
16
  } from "../../../customtypes"
16
17
  import type {
@@ -21,16 +22,40 @@ import type {
21
22
  import {
22
23
  ContentPath,
23
24
  TraverseSliceContentFn,
25
+ TraverseSliceContentWithModelFn,
24
26
  TraverseWidgetContentFn,
27
+ TraverseWidgetContentWithModelFn,
25
28
  } from "../../utils"
26
29
  import { hasContentType } from "../../utils"
27
- import { traverseCompositeSliceContent } from "./Slice/CompositeSliceContent"
28
- import { traverseSharedSliceContent } from "./Slice/SharedSliceContent"
29
- import { traverseSimpleSliceContent } from "./Slice/SimpleSliceContent"
30
+ import {
31
+ isGroupContent,
32
+ traverseGroupContentWithModel,
33
+ traverseGroupItemsContentWithModel,
34
+ } from "../GroupContent"
35
+ import {
36
+ isNestableContent,
37
+ isRepeatableContent,
38
+ isTableContent,
39
+ traverseRepeatableContentWithModel,
40
+ traverseTableContentWithModel,
41
+ } from "../nestable"
42
+ import {
43
+ CompositeSliceContent,
44
+ traverseCompositeSliceContent,
45
+ } from "./Slice/CompositeSliceContent"
46
+ import {
47
+ SharedSliceContent,
48
+ traverseSharedSliceContent,
49
+ } from "./Slice/SharedSliceContent"
50
+ import {
51
+ SimpleSliceContent,
52
+ traverseSimpleSliceContent,
53
+ } from "./Slice/SimpleSliceContent"
30
54
  import {
31
55
  isCompositeSliceItemContent,
32
56
  isSharedSliceItemContent,
33
57
  isSimpleSliceItemContent,
58
+ SharedSliceItemContent,
34
59
  SliceItemContent,
35
60
  sliceItemContentWithDefaultValues,
36
61
  SlicesItemLegacy,
@@ -43,7 +68,11 @@ export const isSlicesContent = (u: unknown): u is SlicesContent =>
43
68
 
44
69
  type SlicesLegacy = Array<unknown>
45
70
 
46
- type SliceModel = VariationFields | NestableWidget | CompositeSlice | Group
71
+ export type SliceModel =
72
+ | VariationFields
73
+ | NestableWidget
74
+ | CompositeSlice
75
+ | Group
47
76
 
48
77
  export const SlicesLegacy = (ctx: LegacyContentCtx) => {
49
78
  const codec = t.array(SlicesItemLegacy(ctx))
@@ -288,3 +317,361 @@ export function traverseSlices({
288
317
  })
289
318
  }
290
319
  }
320
+
321
+ function buildVariationFields(
322
+ sharedSlice: SharedSlice,
323
+ variation: Variation,
324
+ ): VariationFields {
325
+ return {
326
+ type: "SharedSlice",
327
+ sliceName: sharedSlice.id,
328
+ variationId: variation.id,
329
+ fields: {
330
+ primary: variation.primary ?? {},
331
+ items: variation.items ?? {},
332
+ },
333
+ }
334
+ }
335
+
336
+ function traverseSharedSliceContentWithModel({
337
+ path,
338
+ sliceKey,
339
+ sliceName,
340
+ model,
341
+ content,
342
+ }: {
343
+ path: ContentPath
344
+ sliceKey: string
345
+ sliceName: string
346
+ model: VariationFields
347
+ content: SharedSliceItemContent | undefined
348
+ }): (
349
+ transformWidget: TraverseWidgetContentWithModelFn,
350
+ transformSlice: TraverseSliceContentWithModelFn,
351
+ ) => SharedSliceItemContent | undefined {
352
+ return (transformWidget, transformSlice) => {
353
+ if (!content) {
354
+ return undefined
355
+ }
356
+
357
+ const primary = Object.entries(model.fields.primary ?? {}).reduce<
358
+ SharedSliceContent["primary"]
359
+ >((acc, [fieldKey, fieldModel]) => {
360
+ const fieldContent = content.widget.primary[fieldKey]
361
+
362
+ const fieldPath = path.concat([
363
+ { key: "primary", type: "primary" },
364
+ { key: fieldKey, type: "Widget" },
365
+ ])
366
+
367
+ let transformedField
368
+ if (fieldModel.type === "Group") {
369
+ const groupContent = isGroupContent(fieldContent)
370
+ ? fieldContent
371
+ : undefined
372
+ transformedField = traverseGroupContentWithModel({
373
+ path: fieldPath,
374
+ key: fieldKey,
375
+ model: fieldModel,
376
+ content: groupContent,
377
+ })(transformWidget)
378
+ } else if (
379
+ fieldModel.type === "Link" &&
380
+ fieldModel.config?.repeat === true
381
+ ) {
382
+ const repeatableContent = isRepeatableContent(fieldContent)
383
+ ? fieldContent
384
+ : undefined
385
+ transformedField = traverseRepeatableContentWithModel({
386
+ path: fieldPath,
387
+ key: fieldKey,
388
+ model: fieldModel,
389
+ content: repeatableContent,
390
+ })(transformWidget)
391
+ } else if (fieldModel.type === "Table") {
392
+ const tableContent = isTableContent(fieldContent)
393
+ ? fieldContent
394
+ : undefined
395
+ transformedField = traverseTableContentWithModel({
396
+ path: fieldPath,
397
+ key: fieldKey,
398
+ model: fieldModel,
399
+ content: tableContent,
400
+ })(transformWidget)
401
+ } else {
402
+ const nestableContent = isNestableContent(fieldContent)
403
+ ? fieldContent
404
+ : undefined
405
+ transformedField = transformWidget({
406
+ path: fieldPath,
407
+ key: fieldKey,
408
+ apiId: fieldKey,
409
+ model: fieldModel,
410
+ content: nestableContent,
411
+ })
412
+ }
413
+
414
+ if (!transformedField) return acc
415
+
416
+ return {
417
+ ...acc,
418
+ [fieldKey]: transformedField,
419
+ }
420
+ }, {})
421
+
422
+ const items = traverseGroupItemsContentWithModel({
423
+ path: path.concat([{ key: "items", type: "items" }]),
424
+ model: model.fields.items ?? {},
425
+ content: content.widget.items,
426
+ })(transformWidget)
427
+
428
+ return transformSlice({
429
+ key: sliceKey,
430
+ apiId: sliceName,
431
+ path,
432
+ model,
433
+ content: {
434
+ ...content,
435
+ widget: {
436
+ __TYPE__: "SharedSliceContent",
437
+ variation: content.widget.variation,
438
+ primary,
439
+ items,
440
+ },
441
+ },
442
+ })
443
+ }
444
+ }
445
+
446
+ function traverseCompositeSliceContentWithModel({
447
+ path,
448
+ sliceKey,
449
+ sliceName,
450
+ model,
451
+ content,
452
+ }: {
453
+ path: ContentPath
454
+ sliceKey: string
455
+ sliceName: string
456
+ model: CompositeSlice | VariationFields
457
+ content: SliceItemContent | undefined
458
+ }): (
459
+ transformWidget: TraverseWidgetContentWithModelFn,
460
+ transformSlice: TraverseSliceContentWithModelFn,
461
+ ) => SliceItemContent | undefined {
462
+ return (transformWidget, transformSlice) => {
463
+ if (!content || !isCompositeSliceItemContent(content)) {
464
+ return undefined
465
+ }
466
+
467
+ const nonRepeatModel =
468
+ model.type === "SharedSlice"
469
+ ? model.fields.primary ?? {}
470
+ : model["non-repeat"] ?? {}
471
+ const repeatModel =
472
+ model.type === "SharedSlice"
473
+ ? model.fields.items ?? {}
474
+ : model.repeat ?? {}
475
+
476
+ const nonRepeat = Object.entries(nonRepeatModel).reduce<
477
+ CompositeSliceContent["nonRepeat"]
478
+ >((acc, [fieldKey, fieldModel]) => {
479
+ const fieldContent = content.widget.nonRepeat[fieldKey]
480
+ const fieldPath = path.concat([
481
+ { key: "non-repeat", type: "primary" },
482
+ { key: fieldKey, type: "Widget" },
483
+ ])
484
+
485
+ const nestableContent = isNestableContent(fieldContent)
486
+ ? fieldContent
487
+ : undefined
488
+
489
+ const transformedField = transformWidget({
490
+ path: fieldPath,
491
+ key: fieldKey,
492
+ apiId: sliceName,
493
+ model: fieldModel,
494
+ content: nestableContent,
495
+ })
496
+
497
+ if (!transformedField || !isNestableContent(transformedField)) return acc
498
+
499
+ return {
500
+ ...acc,
501
+ [fieldKey]: transformedField,
502
+ }
503
+ }, {})
504
+
505
+ const repeat = traverseGroupItemsContentWithModel({
506
+ path: path.concat([{ key: "repeat", type: "items" }]),
507
+ model: repeatModel,
508
+ content: content.widget.repeat,
509
+ })(transformWidget)
510
+
511
+ return transformSlice({
512
+ key: sliceKey,
513
+ apiId: sliceName,
514
+ path,
515
+ model: model.type === "SharedSlice" ? model : model,
516
+ content: {
517
+ ...content,
518
+ widget: {
519
+ __TYPE__: content.widget.__TYPE__,
520
+ nonRepeat,
521
+ repeat,
522
+ },
523
+ },
524
+ })
525
+ }
526
+ }
527
+
528
+ export function traverseSlicesWithModel({
529
+ path,
530
+ key,
531
+ model,
532
+ content,
533
+ }: {
534
+ path: ContentPath
535
+ key: string
536
+ model: StaticSlices
537
+ content: SlicesContent | undefined
538
+ }): (args: {
539
+ transformWidget: TraverseWidgetContentWithModelFn
540
+ transformSlice: TraverseSliceContentWithModelFn
541
+ }) => SlicesContent | undefined {
542
+ return ({ transformWidget, transformSlice }) => {
543
+ const choices = model.config?.choices ?? {}
544
+
545
+ const value = (content?.value ?? []).reduce<SlicesContent["value"]>(
546
+ (acc, sliceContent) => {
547
+ const sliceName = sliceContent.name
548
+ const sliceModel = choices[sliceName]
549
+
550
+ if (!sliceModel) {
551
+ return acc
552
+ }
553
+
554
+ const slicePath = path.concat({
555
+ key: sliceContent.key,
556
+ type: isStaticSharedSlice(sliceModel)
557
+ ? "SharedSlice"
558
+ : isCompositeSlice(sliceModel)
559
+ ? "Slice"
560
+ : "LegacySlice",
561
+ })
562
+
563
+ let convertedSlice: SliceItemContent | undefined
564
+
565
+ if (isStaticSharedSlice(sliceModel)) {
566
+ if (isSharedSliceItemContent(sliceContent)) {
567
+ const variation = sliceModel.variations.find(
568
+ (v) => v.id === sliceContent.widget.variation,
569
+ )
570
+ if (variation) {
571
+ const variationFields = buildVariationFields(
572
+ sliceModel,
573
+ variation,
574
+ )
575
+ convertedSlice = traverseSharedSliceContentWithModel({
576
+ path: slicePath,
577
+ sliceKey: sliceContent.key,
578
+ sliceName,
579
+ model: variationFields,
580
+ content: sliceContent,
581
+ })(transformWidget, transformSlice)
582
+ }
583
+ } else if (isCompositeSliceItemContent(sliceContent)) {
584
+ const defaultVariation = sliceModel.variations[0]
585
+ if (defaultVariation) {
586
+ const variationFields = buildVariationFields(
587
+ sliceModel,
588
+ defaultVariation,
589
+ )
590
+ convertedSlice = traverseCompositeSliceContentWithModel({
591
+ path: slicePath,
592
+ sliceKey: sliceContent.key,
593
+ sliceName,
594
+ model: variationFields,
595
+ content: sliceContent,
596
+ })(transformWidget, transformSlice)
597
+ }
598
+ }
599
+ } else if (isCompositeSlice(sliceModel)) {
600
+ if (isCompositeSliceItemContent(sliceContent)) {
601
+ convertedSlice = traverseCompositeSliceContentWithModel({
602
+ path: slicePath,
603
+ sliceKey: sliceContent.key,
604
+ sliceName,
605
+ model: sliceModel,
606
+ content: sliceContent,
607
+ })(transformWidget, transformSlice)
608
+ }
609
+ } else if (isLegacySlice(sliceModel)) {
610
+ if (isSimpleSliceItemContent(sliceContent)) {
611
+ let convertedWidget: SimpleSliceContent | undefined
612
+
613
+ if (isGroupContent(sliceContent.widget)) {
614
+ const groupModel =
615
+ sliceModel?.type === "Group" ? sliceModel : undefined
616
+
617
+ if (groupModel) {
618
+ convertedWidget = traverseGroupContentWithModel({
619
+ path: slicePath,
620
+ key: sliceContent.key,
621
+ model: groupModel,
622
+ content: sliceContent.widget,
623
+ })(transformWidget)
624
+ }
625
+ } else if (isNestableContent(sliceContent.widget)) {
626
+ const nestableModel =
627
+ sliceModel?.type !== "Group" ? sliceModel : undefined
628
+
629
+ if (nestableModel) {
630
+ convertedWidget = transformWidget({
631
+ key: sliceContent.key,
632
+ apiId: sliceName,
633
+ path: slicePath,
634
+ model: nestableModel,
635
+ content: sliceContent.widget,
636
+ })
637
+ }
638
+ }
639
+
640
+ convertedSlice =
641
+ convertedWidget &&
642
+ transformSlice({
643
+ key: sliceContent.key,
644
+ apiId: sliceName,
645
+ path: slicePath,
646
+ model: sliceModel,
647
+ content: {
648
+ ...sliceContent,
649
+ widget: convertedWidget,
650
+ },
651
+ })
652
+ }
653
+ }
654
+
655
+ if (convertedSlice) {
656
+ acc.push(convertedSlice)
657
+ }
658
+
659
+ return acc
660
+ },
661
+ [],
662
+ )
663
+
664
+ return transformWidget({
665
+ path,
666
+ key,
667
+ apiId: key,
668
+ model,
669
+ content: content
670
+ ? {
671
+ __TYPE__: content.__TYPE__,
672
+ value,
673
+ }
674
+ : undefined,
675
+ })
676
+ }
677
+ }
@@ -90,6 +90,44 @@ export type TraverseWidgetContentFn<
90
90
  | ([ContentTransformMode] extends ["preserve"] ? C : WidgetContent)
91
91
  | undefined
92
92
 
93
+ export type TraverseWidgetContentWithModelFn<
94
+ ContentTransformMode extends "preserve" | "widen" = "preserve",
95
+ > = <
96
+ C extends WidgetContent | undefined,
97
+ D extends NestableWidget | StaticSlices | Group | UID,
98
+ >({
99
+ path,
100
+ key,
101
+ apiId,
102
+ model,
103
+ content,
104
+ }: {
105
+ path: ContentPath
106
+ key: string
107
+ apiId: string
108
+ model: D
109
+ content: C
110
+ }) =>
111
+ | ([ContentTransformMode] extends ["preserve"] ? C : WidgetContent)
112
+ | undefined
113
+
114
+ export type TraverseSliceContentWithModelFn = <
115
+ S extends SliceItemContent | SharedSliceItemContent | undefined,
116
+ D extends VariationFields | CompositeSlice | Group | NestableWidget,
117
+ >({
118
+ path,
119
+ key,
120
+ apiId,
121
+ model,
122
+ content,
123
+ }: {
124
+ path: ContentPath
125
+ key: string
126
+ apiId: string
127
+ model: D
128
+ content: S
129
+ }) => S | SharedSliceItemContent | undefined
130
+
93
131
  export type ContentPathEntry = {
94
132
  type:
95
133
  | "CustomType"