@prismicio/types-internal 3.16.1 → 3.16.2-alpha.1

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,
@@ -288,3 +313,361 @@ export function traverseSlices({
288
313
  })
289
314
  }
290
315
  }
316
+
317
+ function buildVariationFields(
318
+ sharedSlice: SharedSlice,
319
+ variation: Variation,
320
+ ): VariationFields {
321
+ return {
322
+ type: "SharedSlice",
323
+ sliceName: sharedSlice.id,
324
+ variationId: variation.id,
325
+ fields: {
326
+ primary: variation.primary ?? {},
327
+ items: variation.items ?? {},
328
+ },
329
+ }
330
+ }
331
+
332
+ function traverseSharedSliceContentWithModel({
333
+ path,
334
+ sliceKey,
335
+ sliceName,
336
+ model,
337
+ content,
338
+ }: {
339
+ path: ContentPath
340
+ sliceKey: string
341
+ sliceName: string
342
+ model: VariationFields
343
+ content: SharedSliceItemContent | undefined
344
+ }): (
345
+ transformWidget: TraverseWidgetContentWithModelFn,
346
+ transformSlice: TraverseSliceContentWithModelFn,
347
+ ) => SharedSliceItemContent | undefined {
348
+ return (transformWidget, transformSlice) => {
349
+ if (!content) {
350
+ return undefined
351
+ }
352
+
353
+ const primary = Object.entries(model.fields.primary ?? {}).reduce<
354
+ SharedSliceContent["primary"]
355
+ >((acc, [fieldKey, fieldModel]) => {
356
+ const fieldContent = content.widget.primary[fieldKey]
357
+
358
+ const fieldPath = path.concat([
359
+ { key: "primary", type: "primary" },
360
+ { key: fieldKey, type: "Widget" },
361
+ ])
362
+
363
+ let transformedField
364
+ if (fieldModel.type === "Group") {
365
+ const groupContent = isGroupContent(fieldContent)
366
+ ? fieldContent
367
+ : undefined
368
+ transformedField = traverseGroupContentWithModel({
369
+ path: fieldPath,
370
+ key: fieldKey,
371
+ model: fieldModel,
372
+ content: groupContent,
373
+ })(transformWidget)
374
+ } else if (
375
+ fieldModel.type === "Link" &&
376
+ fieldModel.config?.repeat === true
377
+ ) {
378
+ const repeatableContent = isRepeatableContent(fieldContent)
379
+ ? fieldContent
380
+ : undefined
381
+ transformedField = traverseRepeatableContentWithModel({
382
+ path: fieldPath,
383
+ key: fieldKey,
384
+ model: fieldModel,
385
+ content: repeatableContent,
386
+ })(transformWidget)
387
+ } else if (fieldModel.type === "Table") {
388
+ const tableContent = isTableContent(fieldContent)
389
+ ? fieldContent
390
+ : undefined
391
+ transformedField = traverseTableContentWithModel({
392
+ path: fieldPath,
393
+ key: fieldKey,
394
+ model: fieldModel,
395
+ content: tableContent,
396
+ })(transformWidget)
397
+ } else {
398
+ const nestableContent = isNestableContent(fieldContent)
399
+ ? fieldContent
400
+ : undefined
401
+ transformedField = transformWidget({
402
+ path: fieldPath,
403
+ key: fieldKey,
404
+ apiId: fieldKey,
405
+ model: fieldModel,
406
+ content: nestableContent,
407
+ })
408
+ }
409
+
410
+ if (!transformedField) return acc
411
+
412
+ return {
413
+ ...acc,
414
+ [fieldKey]: transformedField,
415
+ }
416
+ }, {})
417
+
418
+ const items = traverseGroupItemsContentWithModel({
419
+ path: path.concat([{ key: "items", type: "items" }]),
420
+ model: model.fields.items ?? {},
421
+ content: content.widget.items,
422
+ })(transformWidget)
423
+
424
+ return transformSlice({
425
+ key: sliceKey,
426
+ apiId: sliceName,
427
+ path,
428
+ model,
429
+ content: {
430
+ ...content,
431
+ widget: {
432
+ __TYPE__: "SharedSliceContent",
433
+ variation: content.widget.variation,
434
+ primary,
435
+ items,
436
+ },
437
+ },
438
+ })
439
+ }
440
+ }
441
+
442
+ function traverseCompositeSliceContentWithModel({
443
+ path,
444
+ sliceKey,
445
+ sliceName,
446
+ model,
447
+ content,
448
+ }: {
449
+ path: ContentPath
450
+ sliceKey: string
451
+ sliceName: string
452
+ model: CompositeSlice | VariationFields
453
+ content: SliceItemContent | undefined
454
+ }): (
455
+ transformWidget: TraverseWidgetContentWithModelFn,
456
+ transformSlice: TraverseSliceContentWithModelFn,
457
+ ) => SliceItemContent | undefined {
458
+ return (transformWidget, transformSlice) => {
459
+ if (!content || !isCompositeSliceItemContent(content)) {
460
+ return undefined
461
+ }
462
+
463
+ const nonRepeatModel =
464
+ model.type === "SharedSlice"
465
+ ? model.fields.primary ?? {}
466
+ : model["non-repeat"] ?? {}
467
+ const repeatModel =
468
+ model.type === "SharedSlice"
469
+ ? model.fields.items ?? {}
470
+ : model.repeat ?? {}
471
+
472
+ const nonRepeat = Object.entries(nonRepeatModel).reduce<
473
+ CompositeSliceContent["nonRepeat"]
474
+ >((acc, [fieldKey, fieldModel]) => {
475
+ const fieldContent = content.widget.nonRepeat[fieldKey]
476
+ const fieldPath = path.concat([
477
+ { key: "non-repeat", type: "primary" },
478
+ { key: fieldKey, type: "Widget" },
479
+ ])
480
+
481
+ const nestableContent = isNestableContent(fieldContent)
482
+ ? fieldContent
483
+ : undefined
484
+
485
+ const transformedField = transformWidget({
486
+ path: fieldPath,
487
+ key: fieldKey,
488
+ apiId: sliceName,
489
+ model: fieldModel,
490
+ content: nestableContent,
491
+ })
492
+
493
+ if (!transformedField || !isNestableContent(transformedField)) return acc
494
+
495
+ return {
496
+ ...acc,
497
+ [fieldKey]: transformedField,
498
+ }
499
+ }, {})
500
+
501
+ const repeat = traverseGroupItemsContentWithModel({
502
+ path: path.concat([{ key: "repeat", type: "items" }]),
503
+ model: repeatModel,
504
+ content: content.widget.repeat,
505
+ })(transformWidget)
506
+
507
+ return transformSlice({
508
+ key: sliceKey,
509
+ apiId: sliceName,
510
+ path,
511
+ model: model.type === "SharedSlice" ? model : model,
512
+ content: {
513
+ ...content,
514
+ widget: {
515
+ __TYPE__: content.widget.__TYPE__,
516
+ nonRepeat,
517
+ repeat,
518
+ },
519
+ },
520
+ })
521
+ }
522
+ }
523
+
524
+ export function traverseSlicesWithModel({
525
+ path,
526
+ key,
527
+ model,
528
+ content,
529
+ }: {
530
+ path: ContentPath
531
+ key: string
532
+ model: StaticSlices
533
+ content: SlicesContent | undefined
534
+ }): (args: {
535
+ transformWidget: TraverseWidgetContentWithModelFn
536
+ transformSlice: TraverseSliceContentWithModelFn
537
+ }) => SlicesContent | undefined {
538
+ return ({ transformWidget, transformSlice }) => {
539
+ const choices = model.config?.choices ?? {}
540
+
541
+ const value = (content?.value ?? []).reduce<SlicesContent["value"]>(
542
+ (acc, sliceContent) => {
543
+ const sliceName = sliceContent.name
544
+ const sliceModel = choices[sliceName]
545
+
546
+ if (!sliceModel) {
547
+ return acc
548
+ }
549
+
550
+ const slicePath = path.concat({
551
+ key: sliceContent.key,
552
+ type: isStaticSharedSlice(sliceModel)
553
+ ? "SharedSlice"
554
+ : isCompositeSlice(sliceModel)
555
+ ? "Slice"
556
+ : "LegacySlice",
557
+ })
558
+
559
+ let convertedSlice: SliceItemContent | undefined
560
+
561
+ if (isStaticSharedSlice(sliceModel)) {
562
+ if (isSharedSliceItemContent(sliceContent)) {
563
+ const variation = sliceModel.variations.find(
564
+ (v) => v.id === sliceContent.widget.variation,
565
+ )
566
+ if (variation) {
567
+ const variationFields = buildVariationFields(
568
+ sliceModel,
569
+ variation,
570
+ )
571
+ convertedSlice = traverseSharedSliceContentWithModel({
572
+ path: slicePath,
573
+ sliceKey: sliceContent.key,
574
+ sliceName,
575
+ model: variationFields,
576
+ content: sliceContent,
577
+ })(transformWidget, transformSlice)
578
+ }
579
+ } else if (isCompositeSliceItemContent(sliceContent)) {
580
+ const defaultVariation = sliceModel.variations[0]
581
+ if (defaultVariation) {
582
+ const variationFields = buildVariationFields(
583
+ sliceModel,
584
+ defaultVariation,
585
+ )
586
+ convertedSlice = traverseCompositeSliceContentWithModel({
587
+ path: slicePath,
588
+ sliceKey: sliceContent.key,
589
+ sliceName,
590
+ model: variationFields,
591
+ content: sliceContent,
592
+ })(transformWidget, transformSlice)
593
+ }
594
+ }
595
+ } else if (isCompositeSlice(sliceModel)) {
596
+ if (isCompositeSliceItemContent(sliceContent)) {
597
+ convertedSlice = traverseCompositeSliceContentWithModel({
598
+ path: slicePath,
599
+ sliceKey: sliceContent.key,
600
+ sliceName,
601
+ model: sliceModel,
602
+ content: sliceContent,
603
+ })(transformWidget, transformSlice)
604
+ }
605
+ } else if (isLegacySlice(sliceModel)) {
606
+ if (isSimpleSliceItemContent(sliceContent)) {
607
+ let convertedWidget: SimpleSliceContent | undefined
608
+
609
+ if (isGroupContent(sliceContent.widget)) {
610
+ const groupModel =
611
+ sliceModel?.type === "Group" ? sliceModel : undefined
612
+
613
+ if (groupModel) {
614
+ convertedWidget = traverseGroupContentWithModel({
615
+ path: slicePath,
616
+ key: sliceContent.key,
617
+ model: groupModel,
618
+ content: sliceContent.widget,
619
+ })(transformWidget)
620
+ }
621
+ } else if (isNestableContent(sliceContent.widget)) {
622
+ const nestableModel =
623
+ sliceModel?.type !== "Group" ? sliceModel : undefined
624
+
625
+ if (nestableModel) {
626
+ convertedWidget = transformWidget({
627
+ key: sliceContent.key,
628
+ apiId: sliceName,
629
+ path: slicePath,
630
+ model: nestableModel,
631
+ content: sliceContent.widget,
632
+ })
633
+ }
634
+ }
635
+
636
+ convertedSlice =
637
+ convertedWidget &&
638
+ transformSlice({
639
+ key: sliceContent.key,
640
+ apiId: sliceName,
641
+ path: slicePath,
642
+ model: sliceModel,
643
+ content: {
644
+ ...sliceContent,
645
+ widget: convertedWidget,
646
+ },
647
+ })
648
+ }
649
+ }
650
+
651
+ if (convertedSlice) {
652
+ acc.push(convertedSlice)
653
+ }
654
+
655
+ return acc
656
+ },
657
+ [],
658
+ )
659
+
660
+ return transformWidget({
661
+ path,
662
+ key,
663
+ apiId: key,
664
+ model,
665
+ content: content
666
+ ? {
667
+ __TYPE__: content.__TYPE__,
668
+ value,
669
+ }
670
+ : undefined,
671
+ })
672
+ }
673
+ }
@@ -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"