@prismicio/types-internal 0.1.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 (68) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +110 -0
  3. package/dist/customtypes/index.cjs +864 -0
  4. package/dist/customtypes/index.cjs.map +1 -0
  5. package/dist/customtypes/index.d.ts +18433 -0
  6. package/dist/customtypes/index.d.ts.map +1 -0
  7. package/dist/customtypes/index.js +864 -0
  8. package/dist/customtypes/index.js.map +1 -0
  9. package/dist/customtypes/widgets/index.cjs +735 -0
  10. package/dist/customtypes/widgets/index.cjs.map +1 -0
  11. package/dist/customtypes/widgets/index.d.ts +11697 -0
  12. package/dist/customtypes/widgets/index.d.ts.map +1 -0
  13. package/dist/customtypes/widgets/index.js +735 -0
  14. package/dist/customtypes/widgets/index.js.map +1 -0
  15. package/dist/customtypes/widgets/slices/index.cjs +671 -0
  16. package/dist/customtypes/widgets/slices/index.cjs.map +1 -0
  17. package/dist/customtypes/widgets/slices/index.d.ts +7079 -0
  18. package/dist/customtypes/widgets/slices/index.d.ts.map +1 -0
  19. package/dist/customtypes/widgets/slices/index.js +671 -0
  20. package/dist/customtypes/widgets/slices/index.js.map +1 -0
  21. package/dist/index.cjs +870 -0
  22. package/dist/index.cjs.map +1 -0
  23. package/dist/index.d.ts +18455 -0
  24. package/dist/index.js +844 -0
  25. package/dist/index.js.map +1 -0
  26. package/package.json +84 -0
  27. package/src/customtypes/CustomType.ts +142 -0
  28. package/src/customtypes/Format.ts +4 -0
  29. package/src/customtypes/Section.ts +25 -0
  30. package/src/customtypes/index.ts +4 -0
  31. package/src/customtypes/widgets/Group.ts +30 -0
  32. package/src/customtypes/widgets/UID.ts +27 -0
  33. package/src/customtypes/widgets/Widget.ts +33 -0
  34. package/src/customtypes/widgets/WidgetTypes.ts +24 -0
  35. package/src/customtypes/widgets/index.ts +7 -0
  36. package/src/customtypes/widgets/nestable/BooleanField.ts +30 -0
  37. package/src/customtypes/widgets/nestable/Color.ts +26 -0
  38. package/src/customtypes/widgets/nestable/Date.ts +27 -0
  39. package/src/customtypes/widgets/nestable/Embed.ts +27 -0
  40. package/src/customtypes/widgets/nestable/GeoPoint.ts +25 -0
  41. package/src/customtypes/widgets/nestable/Image.ts +40 -0
  42. package/src/customtypes/widgets/nestable/IntegrationField.ts +27 -0
  43. package/src/customtypes/widgets/nestable/Link.ts +85 -0
  44. package/src/customtypes/widgets/nestable/NestableWidget.ts +39 -0
  45. package/src/customtypes/widgets/nestable/Number.ts +30 -0
  46. package/src/customtypes/widgets/nestable/Range.ts +30 -0
  47. package/src/customtypes/widgets/nestable/RichText.ts +160 -0
  48. package/src/customtypes/widgets/nestable/Select.ts +32 -0
  49. package/src/customtypes/widgets/nestable/Separator.ts +24 -0
  50. package/src/customtypes/widgets/nestable/Text.ts +27 -0
  51. package/src/customtypes/widgets/nestable/Timestamp.ts +27 -0
  52. package/src/customtypes/widgets/nestable/index.ts +15 -0
  53. package/src/customtypes/widgets/shared/ImageConstraint.ts +39 -0
  54. package/src/customtypes/widgets/shared/index.ts +1 -0
  55. package/src/customtypes/widgets/slices/CompositeSlice.ts +32 -0
  56. package/src/customtypes/widgets/slices/LegacySlice.ts +15 -0
  57. package/src/customtypes/widgets/slices/SharedSlice.ts +44 -0
  58. package/src/customtypes/widgets/slices/SharedSliceRef.ts +12 -0
  59. package/src/customtypes/widgets/slices/Slice.ts +7 -0
  60. package/src/customtypes/widgets/slices/Slices.ts +100 -0
  61. package/src/customtypes/widgets/slices/SlicesTypes.ts +6 -0
  62. package/src/customtypes/widgets/slices/index.ts +7 -0
  63. package/src/index.ts +1 -0
  64. package/src/validators/IntFromNumber.ts +24 -0
  65. package/src/validators/IntFromPixels.ts +31 -0
  66. package/src/validators/StringFromBoolean.ts +21 -0
  67. package/src/validators/StringFromNumber.ts +21 -0
  68. package/src/validators/StringOrNull.ts +3 -0
package/dist/index.js ADDED
@@ -0,0 +1,844 @@
1
+ import * as t from 'io-ts';
2
+ import { NumberFromString } from 'io-ts-types/lib/NumberFromString';
3
+ import { chain, either, left, right } from 'fp-ts/lib/Either';
4
+ import { IntFromString } from 'io-ts-types/lib/IntFromString';
5
+ import { pipe } from 'fp-ts/lib/pipeable';
6
+ import { withFallback } from 'io-ts-types/lib/withFallback';
7
+
8
+ var Format;
9
+ (function(Format2) {
10
+ Format2["Static"] = "static";
11
+ Format2["Dynamic"] = "dynamic";
12
+ })(Format || (Format = {}));
13
+
14
+ const StringOrNull = t.union([t.string, t.null, t.undefined]);
15
+
16
+ var WidgetTypes;
17
+ (function(WidgetTypes2) {
18
+ WidgetTypes2["Text"] = "Text";
19
+ WidgetTypes2["RichText"] = "StructuredText";
20
+ WidgetTypes2["Color"] = "Color";
21
+ WidgetTypes2["Image"] = "Image";
22
+ WidgetTypes2["Date"] = "Date";
23
+ WidgetTypes2["Timestamp"] = "Timestamp";
24
+ WidgetTypes2["Number"] = "Number";
25
+ WidgetTypes2["Range"] = "Range";
26
+ WidgetTypes2["Select"] = "Select";
27
+ WidgetTypes2["Link"] = "Link";
28
+ WidgetTypes2["Embed"] = "Embed";
29
+ WidgetTypes2["GeoPoint"] = "GeoPoint";
30
+ WidgetTypes2["Separator"] = "Separator";
31
+ WidgetTypes2["UID"] = "UID";
32
+ WidgetTypes2["BooleanField"] = "Boolean";
33
+ WidgetTypes2["IntegrationField"] = "IntegrationFields";
34
+ WidgetTypes2["Group"] = "Group";
35
+ WidgetTypes2["Slices"] = "Slices";
36
+ WidgetTypes2["LegacySlices"] = "Choice";
37
+ })(WidgetTypes || (WidgetTypes = {}));
38
+ var WidgetTypes$1 = WidgetTypes;
39
+
40
+ const UIDConfig = t.exact(t.partial({
41
+ label: StringOrNull,
42
+ useAsTitle: t.boolean,
43
+ placeholder: t.string
44
+ }));
45
+ const UID = t.exact(t.intersection([
46
+ t.type({
47
+ type: t.literal(WidgetTypes$1.UID)
48
+ }),
49
+ t.partial({
50
+ fieldset: StringOrNull,
51
+ config: UIDConfig
52
+ })
53
+ ]));
54
+
55
+ const ColorConfig = t.exact(t.partial({
56
+ label: StringOrNull,
57
+ placeholder: t.string
58
+ }));
59
+ const Color = t.exact(t.intersection([
60
+ t.type({
61
+ type: t.literal(WidgetTypes$1.Color)
62
+ }),
63
+ t.partial({
64
+ fieldset: StringOrNull,
65
+ config: ColorConfig
66
+ })
67
+ ]));
68
+
69
+ const BooleanConfig = t.exact(t.partial({
70
+ label: StringOrNull,
71
+ default_value: t.boolean,
72
+ placeholder_true: t.string,
73
+ placeholder_false: t.string
74
+ }));
75
+ const BooleanField = t.exact(t.intersection([
76
+ t.type({
77
+ type: t.literal(WidgetTypes$1.BooleanField)
78
+ }),
79
+ t.partial({
80
+ config: BooleanConfig
81
+ })
82
+ ]));
83
+
84
+ const DateConfig = t.exact(t.partial({
85
+ label: StringOrNull,
86
+ placeholder: t.string,
87
+ default: t.string
88
+ }));
89
+ const Date = t.exact(t.intersection([
90
+ t.type({
91
+ type: t.literal(WidgetTypes$1.Date)
92
+ }),
93
+ t.partial({
94
+ fieldset: StringOrNull,
95
+ config: DateConfig
96
+ })
97
+ ]));
98
+
99
+ const EmbedConfig = t.exact(t.partial({
100
+ label: StringOrNull,
101
+ placeholder: t.string,
102
+ useAsTitle: t.boolean
103
+ }));
104
+ const Embed = t.exact(t.intersection([
105
+ t.type({
106
+ type: t.literal(WidgetTypes$1.Embed)
107
+ }),
108
+ t.partial({
109
+ fieldset: StringOrNull,
110
+ config: EmbedConfig
111
+ })
112
+ ]));
113
+
114
+ const GeoPointConfig = t.exact(t.partial({
115
+ label: StringOrNull
116
+ }));
117
+ const GeoPoint = t.exact(t.intersection([
118
+ t.type({
119
+ type: t.literal(WidgetTypes$1.GeoPoint)
120
+ }),
121
+ t.partial({
122
+ fieldset: StringOrNull,
123
+ config: GeoPointConfig
124
+ })
125
+ ]));
126
+
127
+ const NumberConfig = t.exact(t.partial({
128
+ label: StringOrNull,
129
+ placeholder: t.string,
130
+ min: t.union([t.number, NumberFromString]),
131
+ max: t.union([t.number, NumberFromString]),
132
+ step: t.union([t.number, NumberFromString])
133
+ }));
134
+ const Number = t.exact(t.intersection([
135
+ t.type({
136
+ type: t.literal(WidgetTypes$1.Number)
137
+ }),
138
+ t.partial({
139
+ fieldset: StringOrNull,
140
+ config: NumberConfig
141
+ })
142
+ ]));
143
+
144
+ const RangeConfig = t.exact(t.partial({
145
+ label: StringOrNull,
146
+ placeholder: t.string,
147
+ min: t.union([t.number, NumberFromString]),
148
+ max: t.union([t.number, NumberFromString]),
149
+ step: t.union([t.number, NumberFromString])
150
+ }));
151
+ const Range = t.exact(t.intersection([
152
+ t.type({
153
+ type: t.literal(WidgetTypes$1.Range)
154
+ }),
155
+ t.partial({
156
+ fieldset: StringOrNull,
157
+ config: RangeConfig
158
+ })
159
+ ]));
160
+
161
+ const IntFromNumber = new t.Type("IntFromNumber", t.Int.is, (u, c) => pipe(t.number.validate(u, c), chain((n) => {
162
+ if (t.Int.is(n))
163
+ return t.success(n);
164
+ else {
165
+ return t.success(Math.round(n));
166
+ }
167
+ })), t.Int.encode);
168
+
169
+ const PixelsRegex = /^([0-9]+)px$/;
170
+ const IntFromPixels = new t.Type("IntFromPixels", t.Int.is, (u, c) => pipe(t.string.validate(u, c), chain((strPixels) => {
171
+ try {
172
+ const matched = strPixels.match(PixelsRegex);
173
+ if (!matched)
174
+ return t.failure(u, c);
175
+ else {
176
+ const parsed = parseInt(matched[1]);
177
+ return t.success(parsed);
178
+ }
179
+ } catch (e) {
180
+ return t.failure(u, c);
181
+ }
182
+ })), String);
183
+
184
+ const SideConstraint = new t.Type("SideConstraints", (u) => {
185
+ return !u || typeof u === "number";
186
+ }, (u, context) => {
187
+ return either.chain(t.union([
188
+ t.literal("auto"),
189
+ t.literal(""),
190
+ t.Int,
191
+ IntFromString,
192
+ IntFromNumber,
193
+ IntFromPixels,
194
+ t.null
195
+ ]).validate(u, context), (constraint) => {
196
+ if (constraint === "auto" || constraint === "")
197
+ return t.success(null);
198
+ return t.success(constraint);
199
+ });
200
+ }, (res) => res);
201
+ const ImageConstraint = t.partial({
202
+ width: SideConstraint,
203
+ height: SideConstraint
204
+ });
205
+
206
+ const DEFAULT_OPTION = "paragraph";
207
+ const options = [
208
+ "heading1",
209
+ "heading2",
210
+ "heading3",
211
+ "heading4",
212
+ "heading5",
213
+ "heading6",
214
+ "paragraph",
215
+ "strong",
216
+ "em",
217
+ "preformatted",
218
+ "hyperlink",
219
+ "image",
220
+ "embed",
221
+ "list-item",
222
+ "o-list-item",
223
+ "rtl"
224
+ ];
225
+ const RichTextOptions = new t.Type("RichTextOptions", (u) => typeof u === "string", (u, context) => {
226
+ return either.chain(t.union([
227
+ t.string,
228
+ t.null
229
+ ]).validate(u, context), (s) => {
230
+ if (!s)
231
+ return t.success(DEFAULT_OPTION);
232
+ const entries = s.split(",").map((e) => e.trim());
233
+ const filtered = entries.filter((entry) => options.includes(entry));
234
+ if (!filtered.length)
235
+ return t.success(DEFAULT_OPTION);
236
+ return t.success(filtered.join(","));
237
+ });
238
+ }, (a) => a);
239
+ const NoLabels = (labels) => {
240
+ if (!labels)
241
+ return t.success([]);
242
+ };
243
+ const LabelsAsObject = (labels) => {
244
+ if (labels instanceof Object) {
245
+ const labelsObj = labels;
246
+ if (!Object.entries(labelsObj).length)
247
+ return t.success([]);
248
+ if (labelsObj[""]) {
249
+ return t.success(labelsObj[""].map((l) => l.name));
250
+ }
251
+ const convertedObjectToArray = Object.entries(labelsObj).reduce((acc, [, labelsEntries]) => {
252
+ return acc.concat(labelsEntries.map((l) => l.name));
253
+ }, []).filter(Boolean);
254
+ return t.success(convertedObjectToArray);
255
+ }
256
+ };
257
+ const LabelsAsArray = (labels) => {
258
+ if (labels instanceof Array) {
259
+ const isValidLabels = labels.reduce((acc, l) => acc && typeof l === "string", true);
260
+ if (isValidLabels)
261
+ return t.success(labels);
262
+ }
263
+ };
264
+ const LabelsAsString = (labels) => {
265
+ if (typeof labels === "string") {
266
+ return t.success([labels]);
267
+ }
268
+ };
269
+ const RichTextLabels = new t.Type("RichTextLabels", (u) => {
270
+ return u instanceof Array;
271
+ }, (u, context) => {
272
+ const legacyValidator = t.record(t.string, t.array(t.record(t.literal("name"), t.string)));
273
+ const validator = t.array(t.string);
274
+ return either.chain(t.union([
275
+ legacyValidator,
276
+ validator,
277
+ t.string,
278
+ t.null
279
+ ]).validate(u, context), (labels) => {
280
+ return NoLabels(labels) || LabelsAsArray(labels) || LabelsAsObject(labels) || LabelsAsString(labels) || t.failure(u, context);
281
+ });
282
+ }, (res) => res);
283
+ const RichTextConfig = t.exact(t.partial({
284
+ label: StringOrNull,
285
+ placeholder: t.string,
286
+ useAsTitle: t.boolean,
287
+ single: RichTextOptions,
288
+ multi: RichTextOptions,
289
+ imageConstraint: ImageConstraint,
290
+ labels: RichTextLabels,
291
+ allowTargetBlank: t.boolean
292
+ }));
293
+ const RichText = t.exact(t.intersection([
294
+ t.type({
295
+ type: t.literal(WidgetTypes$1.RichText)
296
+ }),
297
+ t.partial({
298
+ fieldset: StringOrNull,
299
+ config: RichTextConfig
300
+ })
301
+ ]));
302
+
303
+ const StringFromBoolean = new t.Type("StringFromInt", t.string.is, (u, c) => pipe(t.boolean.validate(u, c), chain((i) => {
304
+ return t.success(i.toString());
305
+ })), (i) => i);
306
+
307
+ const StringFromNumber = new t.Type("StringFromInt", t.string.is, (u, c) => pipe(t.number.validate(u, c), chain((i) => {
308
+ return t.success(i.toString());
309
+ })), (i) => i);
310
+
311
+ const SelectConfig = t.exact(t.partial({
312
+ label: StringOrNull,
313
+ placeholder: t.string,
314
+ default_value: t.string,
315
+ options: t.array(t.union([t.string, StringFromNumber, StringFromBoolean]))
316
+ }));
317
+ const Select = t.exact(t.intersection([
318
+ t.type({
319
+ type: t.literal(WidgetTypes$1.Select)
320
+ }),
321
+ t.partial({
322
+ fieldset: StringOrNull,
323
+ config: SelectConfig
324
+ })
325
+ ]));
326
+
327
+ const SeparatorConfig = t.exact(t.partial({
328
+ label: StringOrNull
329
+ }));
330
+ const Separator = t.exact(t.intersection([
331
+ t.type({
332
+ type: t.literal(WidgetTypes$1.Separator)
333
+ }),
334
+ t.partial({
335
+ config: SeparatorConfig
336
+ })
337
+ ]));
338
+
339
+ const TextConfig = t.exact(t.partial({
340
+ label: StringOrNull,
341
+ useAsTitle: t.boolean,
342
+ placeholder: t.string
343
+ }));
344
+ const Text = t.exact(t.intersection([
345
+ t.type({
346
+ type: t.literal(WidgetTypes$1.Text)
347
+ }),
348
+ t.partial({
349
+ fieldset: StringOrNull,
350
+ config: TextConfig
351
+ })
352
+ ]));
353
+
354
+ const TimestampConfig = t.exact(t.partial({
355
+ label: StringOrNull,
356
+ placeholder: t.string,
357
+ default: t.string
358
+ }));
359
+ const Timestamp = t.exact(t.intersection([
360
+ t.type({
361
+ type: t.literal(WidgetTypes$1.Timestamp)
362
+ }),
363
+ t.partial({
364
+ fieldset: StringOrNull,
365
+ config: TimestampConfig
366
+ })
367
+ ]));
368
+
369
+ const arrayString = (entries) => {
370
+ if (entries instanceof Array) {
371
+ const isValidEntries = entries.reduce((acc, l) => acc && typeof l === "string", true);
372
+ if (isValidEntries)
373
+ return t.success(entries);
374
+ }
375
+ };
376
+ const plainString = (entries) => {
377
+ if (typeof entries === "string") {
378
+ return t.success([entries]);
379
+ }
380
+ };
381
+ const MasksArrayString = new t.Type("MasksArrayString", (u) => {
382
+ return u instanceof Array;
383
+ }, (u, context) => {
384
+ return either.chain(t.union([
385
+ t.array(t.string),
386
+ t.string
387
+ ]).validate(u, context), (masks) => {
388
+ return arrayString(masks) || plainString(masks) || t.failure(u, context);
389
+ });
390
+ }, (res) => res);
391
+ const LinkConfig = t.exact(t.partial({
392
+ label: StringOrNull,
393
+ useAsTitle: t.boolean,
394
+ placeholder: t.string,
395
+ select: withFallback(t.union([
396
+ t.literal("media"),
397
+ t.literal("document"),
398
+ t.literal("web"),
399
+ t.null
400
+ ]), null),
401
+ customtypes: t.array(t.string),
402
+ masks: MasksArrayString,
403
+ tags: MasksArrayString,
404
+ allowTargetBlank: t.boolean
405
+ }));
406
+ const Link = t.exact(t.intersection([
407
+ t.type({
408
+ type: t.literal(WidgetTypes$1.Link)
409
+ }),
410
+ t.partial({
411
+ fieldset: StringOrNull,
412
+ config: LinkConfig
413
+ })
414
+ ]));
415
+
416
+ const Thumbnail = t.exact(t.intersection([
417
+ t.type({
418
+ name: t.string
419
+ }),
420
+ ImageConstraint
421
+ ]));
422
+ const ImageConfig = t.exact(t.partial({
423
+ label: StringOrNull,
424
+ placeholder: t.string,
425
+ constraint: ImageConstraint,
426
+ thumbnails: t.array(Thumbnail)
427
+ }));
428
+ const Image = t.exact(t.intersection([
429
+ t.type({
430
+ type: t.literal(WidgetTypes$1.Image)
431
+ }),
432
+ t.partial({
433
+ fieldset: StringOrNull,
434
+ config: ImageConfig
435
+ })
436
+ ]));
437
+
438
+ const IntegrationFieldConfig = t.exact(t.partial({
439
+ label: StringOrNull,
440
+ placeholder: t.string,
441
+ catalog: t.string
442
+ }));
443
+ const IntegrationField = t.exact(t.intersection([
444
+ t.type({
445
+ type: t.literal(WidgetTypes$1.IntegrationField)
446
+ }),
447
+ t.partial({
448
+ fieldset: StringOrNull,
449
+ config: IntegrationFieldConfig
450
+ })
451
+ ]));
452
+
453
+ const NestableWidget = t.union([
454
+ Color,
455
+ BooleanField,
456
+ Embed,
457
+ GeoPoint,
458
+ Date,
459
+ Number,
460
+ Range,
461
+ RichText,
462
+ Select,
463
+ Separator,
464
+ Text,
465
+ Timestamp,
466
+ Link,
467
+ Image,
468
+ IntegrationField
469
+ ]);
470
+
471
+ const GroupConfig = t.exact(t.partial({
472
+ label: StringOrNull,
473
+ repeat: t.boolean,
474
+ fields: t.record(t.string, NestableWidget)
475
+ }));
476
+ const Group = t.exact(t.intersection([
477
+ t.type({
478
+ type: t.literal(WidgetTypes$1.Group)
479
+ }),
480
+ t.partial({
481
+ fieldset: StringOrNull,
482
+ icon: t.string,
483
+ description: t.string,
484
+ config: GroupConfig
485
+ })
486
+ ]));
487
+
488
+ const LegacySlice = t.union([
489
+ UID,
490
+ NestableWidget,
491
+ Group
492
+ ]);
493
+
494
+ var SlicesTypes;
495
+ (function(SlicesTypes2) {
496
+ SlicesTypes2["Slice"] = "Slice";
497
+ SlicesTypes2["SharedSlice"] = "SharedSlice";
498
+ })(SlicesTypes || (SlicesTypes = {}));
499
+ var SlicesTypes$1 = SlicesTypes;
500
+
501
+ const CompositeSliceConfig = t.exact(t.partial({
502
+ label: StringOrNull
503
+ }));
504
+ const CompositeSlice = t.exact(t.intersection([
505
+ t.type({
506
+ type: t.literal(SlicesTypes$1.Slice)
507
+ }),
508
+ t.partial({
509
+ fieldset: StringOrNull,
510
+ description: t.string,
511
+ icon: t.string,
512
+ display: t.string,
513
+ "non-repeat": t.record(t.string, NestableWidget),
514
+ repeat: t.record(t.string, NestableWidget),
515
+ config: CompositeSliceConfig
516
+ })
517
+ ]));
518
+
519
+ const SharedSliceRef = t.exact(t.type({
520
+ type: t.literal(SlicesTypes$1.SharedSlice)
521
+ }));
522
+
523
+ const IMAGE_PLACEHOLDER_URL = "https://images.prismic.io/slice-machine/621a5ec4-0387-4bc5-9860-2dd46cbc07cd_default_ss.png?auto=compress,format";
524
+ const Variation = t.exact(t.intersection([
525
+ t.type({
526
+ id: t.string,
527
+ name: t.string,
528
+ description: t.string,
529
+ imageUrl: withFallback(t.string, IMAGE_PLACEHOLDER_URL),
530
+ docURL: t.string,
531
+ version: t.string
532
+ }),
533
+ t.partial({
534
+ display: t.string,
535
+ primary: t.record(t.string, NestableWidget),
536
+ items: t.record(t.string, NestableWidget)
537
+ })
538
+ ]));
539
+ const SharedSlice = t.exact(t.intersection([
540
+ t.type({
541
+ id: t.string,
542
+ type: t.literal(SlicesTypes$1.SharedSlice),
543
+ name: t.string,
544
+ variations: t.array(Variation)
545
+ }),
546
+ t.partial({
547
+ description: t.string
548
+ })
549
+ ]));
550
+
551
+ const SlicesLabels = t.union([
552
+ t.record(t.string, t.array(t.exact(t.intersection([
553
+ t.type({
554
+ name: t.string
555
+ }),
556
+ t.partial({
557
+ display: t.string
558
+ })
559
+ ])))),
560
+ t.null
561
+ ]);
562
+ function slicesConfigReader(format) {
563
+ return t.exact(t.partial({
564
+ label: StringOrNull,
565
+ labels: SlicesLabels,
566
+ choices: t.record(t.string, t.union([
567
+ LegacySlice,
568
+ CompositeSlice,
569
+ (() => {
570
+ switch (format) {
571
+ case Format.Static:
572
+ return SharedSlice;
573
+ case Format.Dynamic:
574
+ return SharedSliceRef;
575
+ default:
576
+ throw new Error(`Invalid Format Exception: ${format} doesn't exist`);
577
+ }
578
+ })()
579
+ ]))
580
+ }));
581
+ }
582
+ const StaticSlicesConfig = slicesConfigReader(Format.Static);
583
+ const DynamicSlicesConfig = slicesConfigReader(Format.Dynamic);
584
+ const SlicesConfig = {
585
+ toStatic(config, sharedSlices) {
586
+ const choices = Object.entries(config.choices || {}).reduce((acc, [ref, slice]) => {
587
+ if (slice.type === SlicesTypes$1.SharedSlice) {
588
+ const sharedSlice = sharedSlices.get(ref);
589
+ if (sharedSlice)
590
+ return { ...acc, [ref]: sharedSlice };
591
+ else
592
+ return acc;
593
+ } else {
594
+ return { ...acc, [ref]: slice };
595
+ }
596
+ }, {});
597
+ return { ...config, choices };
598
+ }
599
+ };
600
+ function slicesReader(format) {
601
+ return t.exact(t.intersection([
602
+ t.type({
603
+ type: t.union([t.literal(WidgetTypes$1.Slices), t.literal(WidgetTypes$1.LegacySlices)])
604
+ }),
605
+ t.partial({
606
+ fieldset: StringOrNull,
607
+ config: slicesConfigReader(format)
608
+ })
609
+ ]));
610
+ }
611
+ const StaticSlices = slicesReader(Format.Static);
612
+ const DynamicSlices = slicesReader(Format.Dynamic);
613
+ const Slices = {
614
+ toStatic(slices, sharedSlices) {
615
+ if (!slices.config)
616
+ return slices;
617
+ else {
618
+ return { ...slices, config: SlicesConfig.toStatic(slices.config, sharedSlices) };
619
+ }
620
+ }
621
+ };
622
+
623
+ var Slices$1 = /*#__PURE__*/Object.freeze({
624
+ __proto__: null,
625
+ slicesConfigReader: slicesConfigReader,
626
+ StaticSlicesConfig: StaticSlicesConfig,
627
+ DynamicSlicesConfig: DynamicSlicesConfig,
628
+ slicesReader: slicesReader,
629
+ StaticSlices: StaticSlices,
630
+ DynamicSlices: DynamicSlices,
631
+ Slices: Slices
632
+ });
633
+
634
+ function widgetReader(format) {
635
+ return t.union([
636
+ UID,
637
+ NestableWidget,
638
+ Group,
639
+ slicesReader(format)
640
+ ]);
641
+ }
642
+ const StaticWidget = widgetReader(Format.Static);
643
+ const DynamicWidget = widgetReader(Format.Dynamic);
644
+ const Widgets = {
645
+ toStatic(widget, sharedSlices) {
646
+ switch (widget.type) {
647
+ case WidgetTypes$1.Slices:
648
+ return Slices.toStatic(widget, sharedSlices);
649
+ default:
650
+ return widget;
651
+ }
652
+ }
653
+ };
654
+
655
+ var Widget = /*#__PURE__*/Object.freeze({
656
+ __proto__: null,
657
+ widgetReader: widgetReader,
658
+ StaticWidget: StaticWidget,
659
+ DynamicWidget: DynamicWidget,
660
+ Widgets: Widgets
661
+ });
662
+
663
+ function sectionReader(format) {
664
+ return t.record(t.string, widgetReader(format));
665
+ }
666
+ const StaticSection = sectionReader(Format.Static);
667
+ const DynamicSection = sectionReader(Format.Dynamic);
668
+ const Sections = {
669
+ toStatic(dynamic, sharedSlices) {
670
+ const section = Object.entries(dynamic).reduce((acc, [widgetKey, widget]) => {
671
+ return { ...acc, [widgetKey]: Widgets.toStatic(widget, sharedSlices) };
672
+ }, {});
673
+ return section;
674
+ }
675
+ };
676
+
677
+ var Section = /*#__PURE__*/Object.freeze({
678
+ __proto__: null,
679
+ sectionReader: sectionReader,
680
+ StaticSection: StaticSection,
681
+ DynamicSection: DynamicSection,
682
+ Sections: Sections
683
+ });
684
+
685
+ var index$4 = /*#__PURE__*/Object.freeze({
686
+ __proto__: null,
687
+ BooleanField: BooleanField,
688
+ Color: Color,
689
+ Date: Date,
690
+ GeoPoint: GeoPoint,
691
+ Image: Image,
692
+ IntegrationField: IntegrationField,
693
+ Link: Link,
694
+ NestableWidget: NestableWidget,
695
+ Number: Number,
696
+ Range: Range,
697
+ RichText: RichText,
698
+ Select: Select,
699
+ Separator: Separator,
700
+ Text: Text,
701
+ Timestamp: Timestamp
702
+ });
703
+
704
+ var index$3 = /*#__PURE__*/Object.freeze({
705
+ __proto__: null,
706
+ ImageConstraint: ImageConstraint
707
+ });
708
+
709
+ var Slice = /*#__PURE__*/Object.freeze({
710
+ __proto__: null
711
+ });
712
+
713
+ var index$2 = /*#__PURE__*/Object.freeze({
714
+ __proto__: null,
715
+ Slice: Slice,
716
+ SliceZone: Slices$1,
717
+ CompositeSlice: CompositeSlice,
718
+ LegacySlice: LegacySlice,
719
+ SharedSlice: SharedSlice,
720
+ SharedSliceRef: SharedSliceRef,
721
+ SlicesTypes: SlicesTypes$1
722
+ });
723
+
724
+ var index$1 = /*#__PURE__*/Object.freeze({
725
+ __proto__: null,
726
+ Nestable: index$4,
727
+ Shared: index$3,
728
+ Slices: index$2,
729
+ Widget: Widget,
730
+ Group: Group,
731
+ UID: UID,
732
+ WidgetTypes: WidgetTypes$1
733
+ });
734
+
735
+ class CustomTypeSlicesError extends Error {
736
+ constructor(slices) {
737
+ super();
738
+ this.slices = slices;
739
+ this.message = this._formatError(slices);
740
+ }
741
+ _formatError(slicesRefs) {
742
+ const slicesMsg = slicesRefs.map((ref) => ` - ${ref}`);
743
+ return `The following slices doesn't exists in your Prismic repository:
744
+ ${slicesMsg.join("\n")}
745
+ `;
746
+ }
747
+ }
748
+ function customTypeReader(format) {
749
+ return t.exact(t.intersection([
750
+ t.type({
751
+ id: t.string,
752
+ label: StringOrNull,
753
+ repeatable: withFallback(t.boolean, true),
754
+ json: t.record(t.string, sectionReader(format)),
755
+ status: withFallback(t.boolean, true)
756
+ }),
757
+ t.partial({
758
+ hash: t.string
759
+ })
760
+ ]));
761
+ }
762
+ const StaticCustomType = customTypeReader(Format.Static);
763
+ const CustomType = customTypeReader(Format.Dynamic);
764
+ function _retrieveSharedSlicesRef(customType) {
765
+ const flattenWidgets = Object.entries(customType.json).reduce((acc, [, section]) => {
766
+ const sectionWidgets = Object.entries(section);
767
+ return acc.concat(sectionWidgets);
768
+ }, []);
769
+ const slicezones = flattenWidgets.filter(([, widget]) => widget.type === WidgetTypes$1.Slices);
770
+ const allSharedRefs = slicezones.reduce((acc, [, slicezone]) => {
771
+ const sharedRefs = Object.entries(slicezone.config && slicezone.config.choices ? slicezone.config.choices : {}).filter(([, slice]) => slice.type === SlicesTypes$1.SharedSlice).map(([key]) => key);
772
+ return acc.concat(sharedRefs);
773
+ }, []);
774
+ return allSharedRefs;
775
+ }
776
+ function _mapSharedSlicesRefs(customType) {
777
+ const refs = _retrieveSharedSlicesRef(customType);
778
+ return function(mapFn) {
779
+ return refs.map(mapFn);
780
+ };
781
+ }
782
+ function toStatic(customType, sharedSlices) {
783
+ const json = Object.entries(customType.json).reduce((acc, [sectionKey, dynSection]) => {
784
+ return { ...acc, [sectionKey]: Sections.toStatic(dynSection, sharedSlices) };
785
+ }, {});
786
+ return { ...customType, json };
787
+ }
788
+ function validateSlices(customType, sharedSlices) {
789
+ const missingSlices = _mapSharedSlicesRefs(customType)((ref) => {
790
+ const slice = sharedSlices.get(ref);
791
+ const isMissing = !Boolean(slice);
792
+ if (isMissing)
793
+ return ref;
794
+ }).filter(Boolean);
795
+ if (missingSlices.length > 0)
796
+ return left(new CustomTypeSlicesError(missingSlices));
797
+ else
798
+ return right(customType);
799
+ }
800
+ function collectWidgets(customType, f) {
801
+ const json = Object.entries(customType.json).reduce((acc, [sectionId, section]) => {
802
+ const updatedSection = Object.entries(section).reduce((acc2, [ref, widget]) => {
803
+ const updatedWidget = f(ref, widget);
804
+ if (!!updatedWidget) {
805
+ return { ...acc2, [ref]: updatedWidget };
806
+ }
807
+ return acc2;
808
+ }, {});
809
+ return { ...acc, [sectionId]: updatedSection };
810
+ }, {});
811
+ return { ...customType, json };
812
+ }
813
+ function filterMissingSharedSlices(customType, sharedSlices) {
814
+ return collectWidgets(customType, (_widgetId, widget) => {
815
+ if (widget.type === WidgetTypes$1.Slices) {
816
+ if (!widget.config || !widget.config.choices)
817
+ return widget;
818
+ const choices = Object.entries(widget.config.choices).reduce((acc, [sliceId, sliceValue]) => {
819
+ if (sliceValue.type === SlicesTypes$1.SharedSlice && !sharedSlices.get(sliceId))
820
+ return acc;
821
+ return { ...acc, [sliceId]: sliceValue };
822
+ }, {});
823
+ const config = { ...widget.config, choices };
824
+ return { ...widget, config };
825
+ }
826
+ return widget;
827
+ });
828
+ }
829
+
830
+ var index = /*#__PURE__*/Object.freeze({
831
+ __proto__: null,
832
+ Section: Section,
833
+ Widgets: index$1,
834
+ get Format () { return Format; },
835
+ StaticCustomType: StaticCustomType,
836
+ CustomType: CustomType,
837
+ toStatic: toStatic,
838
+ validateSlices: validateSlices,
839
+ collectWidgets: collectWidgets,
840
+ filterMissingSharedSlices: filterMissingSharedSlices
841
+ });
842
+
843
+ export { index as CustomTypes };
844
+ //# sourceMappingURL=index.js.map