@prismicio/types-internal 3.17.0-alpha.3 → 3.17.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/lib/content/Document.d.ts +11 -1
- package/lib/content/Document.js +99 -1
- package/lib/content/fields/GroupContent.d.ts +13 -2
- package/lib/content/fields/GroupContent.js +100 -1
- package/lib/content/fields/nestable/LinkContent.js +17 -17
- package/lib/content/fields/nestable/RepeatableContent.d.ts +7 -1
- package/lib/content/fields/nestable/RepeatableContent.js +43 -1
- package/lib/content/fields/nestable/TableContent.d.ts +7 -1
- package/lib/content/fields/nestable/TableContent.js +53 -7
- package/lib/content/fields/slices/SlicesContent.d.ts +32 -14
- package/lib/content/fields/slices/SlicesContent.js +279 -1
- package/lib/content/helpers.d.ts +9 -0
- package/lib/content/helpers.js +22 -0
- package/lib/content/utils.d.ts +17 -10
- package/lib/content/utils.js +6 -21
- package/package.json +1 -1
- package/src/content/Document.ts +130 -0
- package/src/content/fields/GroupContent.ts +138 -1
- package/src/content/fields/nestable/LinkContent.ts +1 -1
- package/src/content/fields/nestable/RepeatableContent.ts +60 -0
- package/src/content/fields/nestable/TableContent.ts +62 -3
- package/src/content/fields/slices/SlicesContent.ts +391 -4
- package/src/content/helpers.ts +25 -0
- package/src/content/utils.ts +41 -26
|
@@ -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 {
|
|
28
|
-
|
|
29
|
-
|
|
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 =
|
|
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
|
+
export 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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
import { v4 as uuid, validate as validateUuid } from "uuid"
|
|
3
|
+
|
|
4
|
+
type WithContentType = { __TYPE__: string }
|
|
5
|
+
export function hasContentType(obj: unknown): obj is WithContentType {
|
|
6
|
+
return typeof obj === "object" && obj !== null && "__TYPE__" in obj
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const isUuid = (input: unknown): input is string =>
|
|
10
|
+
typeof input === "string" && validateUuid(input)
|
|
11
|
+
|
|
12
|
+
const uuidWithFallback = new t.Type<string, string, unknown>(
|
|
13
|
+
"UUID",
|
|
14
|
+
isUuid,
|
|
15
|
+
(u, c) => {
|
|
16
|
+
if (typeof u === "undefined") return t.success(uuid())
|
|
17
|
+
if (isUuid(u)) return t.success(u)
|
|
18
|
+
return t.failure(u, c)
|
|
19
|
+
},
|
|
20
|
+
t.identity,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
export function withKey<C extends t.Mixed>(codec: C) {
|
|
24
|
+
return t.intersection([t.strict({ key: uuidWithFallback }), codec])
|
|
25
|
+
}
|
package/src/content/utils.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { isRight } from "fp-ts/lib/Either"
|
|
2
|
-
import * as t from "io-ts"
|
|
3
|
-
import { v4 as uuid, validate as validateUuid } from "uuid"
|
|
4
2
|
|
|
5
3
|
import type {
|
|
6
4
|
CompositeSlice,
|
|
@@ -17,8 +15,10 @@ import {
|
|
|
17
15
|
SliceItemContent,
|
|
18
16
|
WidgetContent,
|
|
19
17
|
} from "./fields"
|
|
18
|
+
export { hasContentType, withKey } from "./helpers"
|
|
19
|
+
import { hasContentType } from "./helpers"
|
|
20
20
|
|
|
21
|
-
type WithFieldType =
|
|
21
|
+
type WithFieldType = { __TYPE__: string; type: FieldType }
|
|
22
22
|
export function hasFieldContentType(obj: unknown): obj is WithFieldType {
|
|
23
23
|
return (
|
|
24
24
|
hasContentType(obj) &&
|
|
@@ -29,29 +29,6 @@ export function hasFieldContentType(obj: unknown): obj is WithFieldType {
|
|
|
29
29
|
)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
type WithContentType = { __TYPE__: string }
|
|
33
|
-
export function hasContentType(obj: unknown): obj is WithContentType {
|
|
34
|
-
return typeof obj === "object" && obj !== null && "__TYPE__" in obj
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const isUuid = (input: unknown): input is string =>
|
|
38
|
-
typeof input === "string" && validateUuid(input)
|
|
39
|
-
|
|
40
|
-
const uuidWithFallback = new t.Type<string, string, unknown>(
|
|
41
|
-
"UUID",
|
|
42
|
-
isUuid,
|
|
43
|
-
(u, c) => {
|
|
44
|
-
if (typeof u === "undefined") return t.success(uuid())
|
|
45
|
-
if (isUuid(u)) return t.success(u)
|
|
46
|
-
return t.failure(u, c, "Expected a valid UUID v4 string")
|
|
47
|
-
},
|
|
48
|
-
t.identity,
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
export function withKey<C extends t.Mixed>(codec: C) {
|
|
52
|
-
return t.intersection([t.strict({ key: uuidWithFallback }), codec])
|
|
53
|
-
}
|
|
54
|
-
|
|
55
32
|
export type TraverseSliceContentFn = <
|
|
56
33
|
S extends SliceItemContent | SharedSliceItemContent,
|
|
57
34
|
D extends VariationFields | CompositeSlice | Group | NestableWidget,
|
|
@@ -90,6 +67,44 @@ export type TraverseWidgetContentFn<
|
|
|
90
67
|
| ([ContentTransformMode] extends ["preserve"] ? C : WidgetContent)
|
|
91
68
|
| undefined
|
|
92
69
|
|
|
70
|
+
export type TraverseWidgetContentWithModelFn<
|
|
71
|
+
ContentTransformMode extends "preserve" | "widen" = "preserve",
|
|
72
|
+
> = <
|
|
73
|
+
C extends WidgetContent | undefined,
|
|
74
|
+
D extends NestableWidget | StaticSlices | Group | UID,
|
|
75
|
+
>({
|
|
76
|
+
path,
|
|
77
|
+
key,
|
|
78
|
+
apiId,
|
|
79
|
+
model,
|
|
80
|
+
content,
|
|
81
|
+
}: {
|
|
82
|
+
path: ContentPath
|
|
83
|
+
key: string
|
|
84
|
+
apiId: string
|
|
85
|
+
model: D
|
|
86
|
+
content: C
|
|
87
|
+
}) =>
|
|
88
|
+
| ([ContentTransformMode] extends ["preserve"] ? C : WidgetContent)
|
|
89
|
+
| undefined
|
|
90
|
+
|
|
91
|
+
export type TraverseSliceContentWithModelFn = <
|
|
92
|
+
S extends SliceItemContent | SharedSliceItemContent | undefined,
|
|
93
|
+
D extends VariationFields | CompositeSlice | Group | NestableWidget,
|
|
94
|
+
>({
|
|
95
|
+
path,
|
|
96
|
+
key,
|
|
97
|
+
apiId,
|
|
98
|
+
model,
|
|
99
|
+
content,
|
|
100
|
+
}: {
|
|
101
|
+
path: ContentPath
|
|
102
|
+
key: string
|
|
103
|
+
apiId: string
|
|
104
|
+
model: D
|
|
105
|
+
content: S
|
|
106
|
+
}) => S | SharedSliceItemContent | undefined
|
|
107
|
+
|
|
93
108
|
export type ContentPathEntry = {
|
|
94
109
|
type:
|
|
95
110
|
| "CustomType"
|