@prismicio/types-internal 2.2.0-alpha.10 → 2.2.0-alpha.12

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 (90) hide show
  1. package/lib/common/UUID.d.ts +7 -0
  2. package/lib/common/UUID.js +8 -0
  3. package/lib/content/fields/slices/Slice/RepeatableContent.js +0 -1
  4. package/lib/import/converters/Document.d.ts +1 -2
  5. package/lib/import/converters/Document.js +2 -0
  6. package/lib/import/converters/fields/Slices/SharedSlice.d.ts +4 -0
  7. package/lib/import/converters/fields/Slices/SharedSlice.js +19 -0
  8. package/lib/import/converters/fields/Slices/SharedSliceContent.d.ts +10 -0
  9. package/lib/import/converters/fields/Slices/SharedSliceContent.js +58 -0
  10. package/lib/import/converters/fields/Slices/Slices.d.ts +4 -0
  11. package/lib/import/converters/fields/Slices/Slices.js +16 -0
  12. package/lib/import/converters/fields/Slices/index.d.ts +1 -0
  13. package/lib/import/converters/fields/Slices/index.js +4 -0
  14. package/lib/import/converters/fields/index.d.ts +1 -0
  15. package/lib/import/converters/fields/index.js +1 -0
  16. package/lib/import/converters/fields/nestable/Nestable.d.ts +2 -2
  17. package/lib/import/validators/fields/ImportField.d.ts +11 -1
  18. package/lib/import/validators/fields/ImportField.js +6 -1
  19. package/lib/import/validators/fields/ImportSlices/ImportSlices.d.ts +8 -0
  20. package/lib/import/validators/fields/ImportSlices/ImportSlices.js +29 -0
  21. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.d.ts +14 -0
  22. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +60 -0
  23. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.d.ts +4 -0
  24. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +7 -0
  25. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.d.ts +2 -0
  26. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.js +15 -0
  27. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +17 -0
  28. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +30 -0
  29. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +43 -0
  30. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +69 -0
  31. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.d.ts +4 -0
  32. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.js +6 -0
  33. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.d.ts +2 -0
  34. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.js +5 -0
  35. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.d.ts +1 -0
  36. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.js +2 -0
  37. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.d.ts +9 -0
  38. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.js +39 -0
  39. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.d.ts +11 -0
  40. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +24 -0
  41. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.d.ts +11 -0
  42. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +24 -0
  43. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.d.ts +5 -0
  44. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.js +8 -0
  45. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.d.ts +3 -0
  46. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.js +6 -0
  47. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.d.ts +339 -0
  48. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.js +11 -0
  49. package/lib/import/validators/fields/ImportSlices/index.d.ts +1 -0
  50. package/lib/import/validators/fields/ImportSlices/index.js +4 -0
  51. package/lib/import/validators/fields/ImportSlices/utils.d.ts +4 -0
  52. package/lib/import/validators/fields/ImportSlices/utils.js +35 -0
  53. package/lib/utils/io-ts.d.ts +2 -0
  54. package/lib/utils/io-ts.js +22 -0
  55. package/lib/validators/BasicTypes.d.ts +1 -0
  56. package/lib/validators/BasicTypes.js +2 -1
  57. package/lib/validators/NonEmptyString.js +2 -1
  58. package/package.json +4 -2
  59. package/src/common/UUID.ts +18 -0
  60. package/src/content/fields/slices/Slice/RepeatableContent.ts +0 -1
  61. package/src/import/converters/Document.ts +8 -3
  62. package/src/import/converters/fields/Slices/SharedSlice.ts +24 -0
  63. package/src/import/converters/fields/Slices/SharedSliceContent.ts +94 -0
  64. package/src/import/converters/fields/Slices/Slices.ts +20 -0
  65. package/src/import/converters/fields/Slices/index.ts +1 -0
  66. package/src/import/converters/fields/index.ts +1 -0
  67. package/src/import/converters/fields/nestable/Nestable.ts +2 -2
  68. package/src/import/validators/fields/ImportField.ts +7 -2
  69. package/src/import/validators/fields/ImportSlices/ImportSlices.ts +54 -0
  70. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +114 -0
  71. package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +6 -0
  72. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.ts +20 -0
  73. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +61 -0
  74. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.ts +100 -0
  75. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.ts +10 -0
  76. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.ts +2 -0
  77. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.ts +1 -0
  78. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.ts +65 -0
  79. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +45 -0
  80. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +45 -0
  81. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/index.ts +5 -0
  82. package/src/import/validators/fields/ImportSlices/SharedSlice/index.ts +3 -0
  83. package/src/import/validators/fields/ImportSlices/SharedSlice/utils.ts +21 -0
  84. package/src/import/validators/fields/ImportSlices/index.ts +1 -0
  85. package/src/import/validators/fields/ImportSlices/utils.ts +43 -0
  86. package/src/utils/io-ts.ts +29 -0
  87. package/src/validators/BasicTypes.ts +5 -0
  88. package/src/validators/NonEmptyString.ts +4 -4
  89. package/lib/import/validators/fields/nestable/Image.d.ts +0 -62
  90. package/lib/import/validators/fields/nestable/Image.js +0 -76
@@ -0,0 +1,7 @@
1
+ import * as t from "io-ts";
2
+ interface UUIDBrand {
3
+ readonly UUID: unique symbol;
4
+ }
5
+ export declare const UUID: t.BrandC<t.StringC, UUIDBrand>;
6
+ export declare type UUID = t.TypeOf<typeof UUID>;
7
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UUID = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const io_ts_types_1 = require("io-ts-types");
7
+ const uuid_1 = require("uuid");
8
+ exports.UUID = (0, io_ts_types_1.withMessage)(t.brand(t.string, (s) => (0, uuid_1.validate)(s), "UUID"), () => "The value must be a valid UUID");
@@ -14,5 +14,4 @@ const RepeatableWidgetsBlock = t.strict({
14
14
  __TYPE__: t.literal(GroupContent_1.GroupItemContentType),
15
15
  value: t.array(RepeatableWidget),
16
16
  });
17
- t.array(RepeatableWidget);
18
17
  exports.RepeatableWidgets = t.array(RepeatableWidgetsBlock);
@@ -1,5 +1,4 @@
1
- import type { Asset } from "../../common";
2
- import type { Embed } from "../../common/Embed";
1
+ import type { Asset, Embed } from "../../common";
3
2
  import type { Document } from "../../content";
4
3
  import type { ImportDocument } from "../validators";
5
4
  export declare function convertImportToContent(document: ImportDocument, assets: Record<Asset["id"], Asset | undefined>, embeds: Record<string, Embed | undefined>): Document;
@@ -13,6 +13,8 @@ function convertWidget(field, assets, embeds) {
13
13
  switch (field.type) {
14
14
  case "UID":
15
15
  return (0, fields_1.uidConverter)(field.value);
16
+ case "Slices":
17
+ return (0, fields_1.importSlicesConverter)(field.value, assets, embeds);
16
18
  default:
17
19
  return (0, fields_1.convertNestableWidget)(field, assets, embeds);
18
20
  }
@@ -0,0 +1,4 @@
1
+ import type { Asset, Embed } from "../../../../common";
2
+ import type { SliceItemContent } from "../../../../content";
3
+ import type { SharedSlice as ImportSharedSlice } from "../../../validators/fields/ImportSlices/SharedSlice";
4
+ export declare const sharedSliceConverter: (slice: ImportSharedSlice, assets: Record<Asset["id"], Asset | undefined>, embeds: Record<string, Embed | undefined>) => SliceItemContent;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sharedSliceConverter = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const SharedSliceContent_1 = require("./SharedSliceContent");
6
+ // TODO should we put it together with SharedSliceId validator?
7
+ const buildSharedSliceId = (slice_type) => `${slice_type}$${(0, uuid_1.v4)()}`;
8
+ const sharedSliceConverter = (slice, assets, embeds) => {
9
+ var _a, _b;
10
+ // Right now we only support SharedSlices, if we support more types of slices in the future we'll need to select a correct converter here
11
+ const widget = (0, SharedSliceContent_1.importSharedSliceContentConverter)(slice, assets, embeds);
12
+ return {
13
+ key: (_a = slice.id) !== null && _a !== void 0 ? _a : buildSharedSliceId(slice.slice_type),
14
+ name: slice.slice_type,
15
+ maybeLabel: (_b = slice.slice_label) !== null && _b !== void 0 ? _b : undefined,
16
+ widget,
17
+ };
18
+ };
19
+ exports.sharedSliceConverter = sharedSliceConverter;
@@ -0,0 +1,10 @@
1
+ import type { Asset, Embed } from "../../../../common";
2
+ import type { SharedSliceContent } from "../../../../content";
3
+ import type { SharedSlice as ImportSharedSlice } from "../../../validators/fields/ImportSlices/SharedSlice";
4
+ /**
5
+ * Builds SharedSliceContent model from ImportSharedSlice
6
+ * @param field ImportSharedSlice to be converted - a single slice from the slices array in the import document
7
+ * @param assets assets that are required for a conversion of a nestable widget
8
+ * @param embeds embeds that are required for a conversion of a nestable widget
9
+ */
10
+ export declare const importSharedSliceContentConverter: (field: ImportSharedSlice, assets: Record<Asset["id"], Asset | undefined>, embeds: Record<string, Embed | undefined>) => SharedSliceContent;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.importSharedSliceContentConverter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const function_1 = require("fp-ts/function");
6
+ const O = (0, tslib_1.__importStar)(require("fp-ts/Option"));
7
+ const R = (0, tslib_1.__importStar)(require("fp-ts/Record"));
8
+ const content_1 = require("../../../../content");
9
+ const nestable_1 = require("../nestable");
10
+ /**
11
+ * Converts the ImportSharedSliceContent which is a record of widget keys and ImportNestable values to a record of widget keys and NestableContent values.
12
+ *
13
+ * @param content a single ImportSharedSliceContent
14
+ * @param assets assets that are required for a conversion of a nestable widget
15
+ * @param embeds embeds that are required for a conversion of a nestable widget
16
+ */
17
+ const sharedSliceContentConverter = (content, assets, embeds) => (0, function_1.pipe)(content,
18
+ // convertNestableWidget can theoretically return undefined, so we need to filter out those values
19
+ R.filterMap((contentValue) => (0, function_1.pipe)(contentValue, (content) => (0, nestable_1.convertNestableWidget)(content, assets, embeds), O.fromNullable)));
20
+ /**
21
+ * Converts a list of items to a list of repeatable widgets.
22
+ * Each ImportSharedSliceContent element is a record of fields, but in the content model it is represented as a list of tuples (key, value)
23
+ *
24
+ * For example, given the following slice content:
25
+ * {
26
+ * "slice_text": (ImportNestable),
27
+ * "slice_number": (ImportNestable),
28
+ * }
29
+ *
30
+ * has to be converted to:
31
+ *
32
+ * {
33
+ * __TYPE__: "GroupItemContentType",
34
+ * value: [
35
+ * ["slice_text", (NestableContent)],
36
+ * ["slice_number", (NestableContent)],
37
+ * ]
38
+ * }
39
+ *
40
+ * @param items list of items to be converted
41
+ * @param assets assets that are required for a conversion of a nestable widget
42
+ */
43
+ const itemsConverter = (items, assets, embeds) => items.map((item) => (0, function_1.pipe)(sharedSliceContentConverter(item, assets, embeds), (record) => Object.entries(record), (entries) => ({ __TYPE__: content_1.GroupItemContentType, value: entries })));
44
+ /**
45
+ * Builds SharedSliceContent model from ImportSharedSlice
46
+ * @param field ImportSharedSlice to be converted - a single slice from the slices array in the import document
47
+ * @param assets assets that are required for a conversion of a nestable widget
48
+ * @param embeds embeds that are required for a conversion of a nestable widget
49
+ */
50
+ const importSharedSliceContentConverter = (field, assets, embeds) => ({
51
+ __TYPE__: content_1.SharedSliceContentType,
52
+ primary: field.primary
53
+ ? sharedSliceContentConverter(field.primary, assets, embeds)
54
+ : {},
55
+ items: field.items ? itemsConverter(field.items, assets, embeds) : [],
56
+ variation: field.variation,
57
+ });
58
+ exports.importSharedSliceContentConverter = importSharedSliceContentConverter;
@@ -0,0 +1,4 @@
1
+ import type { Asset, Embed } from "../../../../common";
2
+ import type { SlicesContent } from "../../../../content";
3
+ import type { ImportSlices } from "../../../validators/fields/ImportSlices";
4
+ export declare const importSlicesConverter: (field: ImportSlices["value"], assets: Record<Asset["id"], Asset | undefined>, embeds: Record<string, Embed | undefined>) => SlicesContent | undefined;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.importSlicesConverter = void 0;
4
+ const SharedSlice_1 = require("./SharedSlice");
5
+ const importSlicesConverter = (field, assets, embeds) => {
6
+ if (field === null)
7
+ return;
8
+ return {
9
+ value: field.map((slice) => {
10
+ // Right now we only support SharedSlices, if we support more types of slices in the future we'll need to select a correct converter here
11
+ return (0, SharedSlice_1.sharedSliceConverter)(slice, assets, embeds);
12
+ }),
13
+ __TYPE__: "SliceContentType",
14
+ };
15
+ };
16
+ exports.importSlicesConverter = importSlicesConverter;
@@ -0,0 +1 @@
1
+ export * from "./Slices";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./Slices"), exports);
@@ -1,2 +1,3 @@
1
1
  export * from "./nestable";
2
+ export * from "./Slices";
2
3
  export * from "./UID";
@@ -2,4 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  (0, tslib_1.__exportStar)(require("./nestable"), exports);
5
+ (0, tslib_1.__exportStar)(require("./Slices"), exports);
5
6
  (0, tslib_1.__exportStar)(require("./UID"), exports);
@@ -1,5 +1,5 @@
1
1
  import type { Asset } from "../../../../common";
2
2
  import type { Embed } from "../../../../common/Embed";
3
- import type { WidgetContent } from "../../../../content";
3
+ import type { NestableContent } from "../../../../content";
4
4
  import type { ImportNestable } from "../../../validators";
5
- export declare function convertNestableWidget(field: ImportNestable, assets: Record<Asset["id"], Asset | undefined>, embeds: Record<string, Embed | undefined>): WidgetContent | undefined;
5
+ export declare function convertNestableWidget(field: ImportNestable, assets: Record<Asset["id"], Asset | undefined>, embeds: Record<string, Embed | undefined>): NestableContent | undefined;
@@ -1,7 +1,8 @@
1
1
  import type { StaticWidget } from "../../../customtypes";
2
+ import { ImportSlices } from "./ImportSlices";
2
3
  import { ImportNestable } from "./nestable";
3
4
  import { ImportUID } from "./UID";
4
- export declare type ImportField = ImportUID | ImportNestable;
5
+ export declare type ImportField = ImportUID | ImportSlices | ImportNestable;
5
6
  export declare const ImportField: {
6
7
  is(u: unknown): u is ImportNestable;
7
8
  decode: (field: StaticWidget) => (content: unknown) => {
@@ -121,5 +122,14 @@ export declare const ImportField: {
121
122
  type: "UID";
122
123
  value: string | null;
123
124
  }>;
125
+ } | {
126
+ codec: import("io-ts").Type<{
127
+ type: "Slices";
128
+ value: import("./ImportSlices/SharedSlice").SharedSlice[] | null;
129
+ }, import("./ImportSlices/SharedSlice").SharedSlice[] | undefined, unknown>;
130
+ result: import("io-ts").Validation<{
131
+ type: "Slices";
132
+ value: import("./ImportSlices/SharedSlice").SharedSlice[] | null;
133
+ }>;
124
134
  };
125
135
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImportField = void 0;
4
+ const ImportSlices_1 = require("./ImportSlices");
4
5
  const nestable_1 = require("./nestable");
5
6
  const UID_1 = require("./UID");
6
7
  exports.ImportField = {
@@ -12,8 +13,12 @@ exports.ImportField = {
12
13
  switch (field.type) {
13
14
  case "UID":
14
15
  return { codec: UID_1.ImportUID, result: UID_1.ImportUID.decode(content) };
15
- case "Choice":
16
16
  case "Slices":
17
+ return {
18
+ codec: (0, ImportSlices_1.ImportSlices)(field),
19
+ result: (0, ImportSlices_1.ImportSlices)(field).decode(content),
20
+ };
21
+ case "Choice":
17
22
  case "Group":
18
23
  throw new Error(`Unsupported type of field ${field.type}`);
19
24
  default:
@@ -0,0 +1,8 @@
1
+ import * as t from "io-ts";
2
+ import type { StaticSlices } from "../../../../customtypes";
3
+ import { SharedSlice } from "./SharedSlice";
4
+ export declare const ImportSlices: (staticSlices: StaticSlices) => t.Type<{
5
+ type: "Slices";
6
+ value: SharedSlice[] | null;
7
+ }, SharedSlice[] | undefined, unknown>;
8
+ export declare type ImportSlices = t.TypeOf<ReturnType<typeof ImportSlices>>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImportSlices = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const E = (0, tslib_1.__importStar)(require("fp-ts/Either"));
6
+ const function_1 = require("fp-ts/function");
7
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const validators_1 = require("../../../../validators");
9
+ const ImportContent_1 = require("../ImportContent");
10
+ const SharedSlice_1 = require("./SharedSlice");
11
+ const utils_1 = require("./utils");
12
+ const ImportSlices = (staticSlices) => {
13
+ const supportedSlices = (0, utils_1.extractSupportedSlices)(staticSlices);
14
+ // For now we only support the SharedSlice, however if we want to support more in the future
15
+ // we would have to change the codec here to something like this: t.array(t.union([ImportSharedSlice(sharedSlices), NewSliceCodec(newSliceCustomTypes)])).
16
+ const SlicesArrayCodec = t.array((0, SharedSlice_1.SharedSlice)(supportedSlices));
17
+ return (0, ImportContent_1.ImportContent)("Slices", (0, validators_1.EmptyObjectOrElse)(new t.Type("ImportSlices", (u) => SlicesArrayCodec.is(u), (u, c) => {
18
+ return (0, function_1.pipe)(SlicesArrayCodec.validate(u, c), E.chain((slices) => {
19
+ // This part might not make sense for all Slice types in the future, but for now we only support the SharedSlice
20
+ // In case we support more in the future, we would have to filter only the relevant type for this check
21
+ const sharedSliceDuplicates = (0, utils_1.findImportSharedSliceDuplicateIds)(slices);
22
+ if (sharedSliceDuplicates.length > 0) {
23
+ return t.failure(slices, [], `Duplicate slice IDs detected: ${sharedSliceDuplicates.join(", ")}`);
24
+ }
25
+ return t.success(slices);
26
+ }));
27
+ }, t.identity)));
28
+ };
29
+ exports.ImportSlices = ImportSlices;
@@ -0,0 +1,14 @@
1
+ import * as t from "io-ts";
2
+ import type { SharedSlice as SharedSliceCustomType } from "../../../../../customtypes";
3
+ import { SharedSliceContent, SharedSliceId } from "./fields";
4
+ export declare type SharedSlice = {
5
+ id: SharedSliceId | null | undefined;
6
+ slice_type: string;
7
+ name: string;
8
+ variation: string;
9
+ primary: SharedSliceContent | null | undefined;
10
+ items: SharedSliceContent[] | null | undefined;
11
+ slice_label: string | null | undefined;
12
+ version: string | null | undefined;
13
+ };
14
+ export declare const SharedSlice: (sharedSlices: SharedSliceCustomType[]) => t.Type<SharedSlice, SharedSlice, unknown>;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedSlice = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const E = (0, tslib_1.__importStar)(require("fp-ts/Either"));
6
+ const function_1 = require("fp-ts/function");
7
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const validators_1 = require("../../../../../validators");
9
+ const fields_1 = require("./fields");
10
+ const SharedSliceShape = t.type({
11
+ id: fields_1.OptionalSharedSliceId,
12
+ slice_type: validators_1.NonEmptyString,
13
+ name: validators_1.String,
14
+ variation: validators_1.NonEmptyString,
15
+ primary: t.union([t.undefined, t.null, t.record(validators_1.NonEmptyString, t.unknown)]),
16
+ items: t.union([
17
+ t.undefined,
18
+ t.null,
19
+ t.array(t.record(validators_1.NonEmptyString, t.unknown)),
20
+ ]),
21
+ slice_label: t.union([t.undefined, t.null, validators_1.String]),
22
+ version: t.union([t.undefined, t.null, validators_1.String]),
23
+ });
24
+ const SharedSlice = (sharedSlices) => new t.Type("SharedSlice", (u) => SharedSliceShape.is(u), (u, c) => (0, function_1.pipe)(
25
+ // We validate the base fields that we can validate directly based on the provided 'sharedSlices'
26
+ t
27
+ .type({
28
+ id: fields_1.OptionalSharedSliceId,
29
+ slice_type: (0, fields_1.SharedSliceType)(sharedSlices),
30
+ slice_label: t.union([t.undefined, t.null, validators_1.String]),
31
+ version: t.union([t.undefined, t.null, validators_1.String]),
32
+ })
33
+ .validate(u, c), E.chain((decoded) => (0, function_1.pipe)(
34
+ // We validate the 'variation' field, for which we need the SharedSlice custom type retrieved in the first step
35
+ t
36
+ .type({
37
+ variation: (0, fields_1.SharedSliceVariation)(decoded.slice_type.data),
38
+ })
39
+ .validate(u, c), E.map(({ variation }) => ({ ...decoded, variation })))), E.chain((decoded) => {
40
+ var _a, _b;
41
+ return (0, function_1.pipe)(
42
+ // We validate the 'primary' and 'items' content fields, for which we need the Variation custom type retrieved in the previous step
43
+ t
44
+ .partial({
45
+ primary: (0, fields_1.SharedSliceContent)(decoded.slice_type.slice_type, "primary", (_a = decoded.variation.data.primary) !== null && _a !== void 0 ? _a : {}),
46
+ // We need to use this wrapper codec instead of using t.array(SharedSliceContent) directly in order to get proper custom error messages
47
+ items: validators_1.AnyArray.pipe(t.array((0, fields_1.SharedSliceContent)(decoded.slice_type.slice_type, "items", (_b = decoded.variation.data.items) !== null && _b !== void 0 ? _b : {}))),
48
+ })
49
+ .validate(u, c), E.map(({ primary, items }) => ({ ...decoded, primary, items })));
50
+ }), E.map(({ id, slice_type, variation, primary, items, slice_label, version, }) => ({
51
+ id,
52
+ slice_type: slice_type.slice_type,
53
+ name: slice_type.data.name,
54
+ variation: variation.variation,
55
+ primary,
56
+ items,
57
+ slice_label,
58
+ version: version !== null && version !== void 0 ? version : variation.data.version,
59
+ }))), t.identity);
60
+ exports.SharedSlice = SharedSlice;
@@ -0,0 +1,4 @@
1
+ export declare const SharedSliceErrors: {
2
+ readonly SliceTypeNotFound: (slice_type: string) => string;
3
+ readonly VariationNotFound: (variation: string, slice_type: string) => string;
4
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedSliceErrors = void 0;
4
+ exports.SharedSliceErrors = {
5
+ SliceTypeNotFound: (slice_type) => `Shared slice '${slice_type}' not found in document's custom type`,
6
+ VariationNotFound: (variation, slice_type) => `Variation '${variation}' not found for Slice '${slice_type}'`,
7
+ };
@@ -0,0 +1,2 @@
1
+ import * as t from "io-ts";
2
+ export declare const OptionalSharedSliceId: t.Type<string | null | undefined, string | null | undefined, unknown>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OptionalSharedSliceId = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const SharedSliceId_1 = require("./SharedSliceId");
7
+ // Wrapping the 'SharedSliceId' codec in an optional codec here for the sake of error reporting.
8
+ // When using t.union([t.undefined, t.null, SharedSliceId]) we get a nested error context
9
+ // with the actual decoding error at position 2 in the context array.
10
+ // To avoid mutating the error context we create this new wrapper codec so that the error context will always have desired depth.
11
+ exports.OptionalSharedSliceId = new t.Type("OptionalSharedSliceId", (u) => u === null || u === undefined || SharedSliceId_1.SharedSliceId.is(u), (u, c) => {
12
+ if (u === null || u === undefined)
13
+ return t.success(u);
14
+ return SharedSliceId_1.SharedSliceId.validate(u, c);
15
+ }, t.identity);
@@ -0,0 +1,17 @@
1
+ import * as t from "io-ts";
2
+ import { WidgetKey } from "../../../../../../../common";
3
+ import type { NestableWidget } from "../../../../../../../customtypes";
4
+ import type { ImportNestable } from "../../../../nestable";
5
+ /**
6
+ * Represents the object in the 'primary' field or a single item in the 'items' array in the SharedSlice.
7
+ */
8
+ export declare type SharedSliceContent = Record<WidgetKey, ImportNestable>;
9
+ /**
10
+ * Builds the decoder for a single entry in the 'primary' field or a single item in the 'items' array in the SharedSlice.
11
+ *
12
+ * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
13
+ * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
14
+ * @param sliceFieldModels models of the fields of the slice from custom type - used to know which codec to use for decoding each value of the object
15
+ * @constructor
16
+ */
17
+ export declare const SharedSliceContent: (sliceName: string, sliceContentField: "primary" | "items", sliceFieldModels: Record<WidgetKey, NestableWidget>) => t.Type<SharedSliceContent, SharedSliceContent, unknown>;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedSliceContent = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const E = (0, tslib_1.__importStar)(require("fp-ts/Either"));
6
+ const function_1 = require("fp-ts/lib/function");
7
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const common_1 = require("../../../../../../../common");
9
+ const io_ts_1 = require("../../../../../../../utils/io-ts");
10
+ const validators_1 = require("../../../../../../../validators");
11
+ const SharedSliceContentEntry_1 = require("./SharedSliceContentEntry");
12
+ /**
13
+ * Builds the decoder for a single entry in the 'primary' field or a single item in the 'items' array in the SharedSlice.
14
+ *
15
+ * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
16
+ * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
17
+ * @param sliceFieldModels models of the fields of the slice from custom type - used to know which codec to use for decoding each value of the object
18
+ * @constructor
19
+ */
20
+ const SharedSliceContent = (sliceName, sliceContentField, sliceFieldModels) => new t.Type("SharedSliceContent", (u) => t.record(common_1.WidgetKey, t.unknown).is(u), (u, c) => (0, function_1.pipe)(
21
+ // We first decode the raw content object in the shape of Record<WidgetKey, unknown>
22
+ validators_1.AnyObject.validate(u, c), E.chain((rawContent) => (0, function_1.pipe)(
23
+ // We split the object into entries because there is no easy other way to decode each value with a different codec
24
+ Object.entries(rawContent).map((entry) => (0, SharedSliceContentEntry_1.SharedSliceContentEntry)(sliceName, sliceContentField, sliceFieldModels).validate(entry, c)),
25
+ // We get a validation result for each entry in the content object (t.Validation<SharedSliceContentEntry>[]),
26
+ // however we want to get a single validation result for the whole array of entries (t.Validation<SharedSliceContentEntry[]>).
27
+ io_ts_1.combineValidationResults,
28
+ // We build the object back from entries if all decoded successfully
29
+ E.map((entries) => entries.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}))))), t.identity);
30
+ exports.SharedSliceContent = SharedSliceContent;
@@ -0,0 +1,43 @@
1
+ import * as t from "io-ts";
2
+ import { WidgetKey } from "../../../../../../../common";
3
+ import type { NestableWidget } from "../../../../../../../customtypes";
4
+ import { ImportNestable } from "../../../../nestable";
5
+ export declare type SharedSliceContentEntry = [WidgetKey, ImportNestable];
6
+ /**
7
+ * Represents a single entry in the 'primary' field or a single entry in an item in the 'items' array in the SharedSlice. It is used to build the SharedSliceContent type from entries of the import object.
8
+ *
9
+ * For example, given the following slice:
10
+ * {
11
+ * "slice_type": "my_slice",
12
+ * "variation": "default",
13
+ * "primary": {
14
+ * "slice_text": "abc", // This is one entry
15
+ * "slice_text2": "def: // This is another entry
16
+ * },
17
+ * "items": [
18
+ * {
19
+ * "slice_number1": 0, // This is one entry
20
+ * "slice_number2": 1 // And this is yet another entry
21
+ * }
22
+ * ]
23
+ * }
24
+ *
25
+ * The 'primary' object is converted to following entries to be decoded:
26
+ * [
27
+ * ["slice_text", "abc"],
28
+ * ["slice_text2", "def"]
29
+ * ]
30
+ * and the first item in the 'items' array is converted to following entries to be decoded:
31
+ * [
32
+ * ["slice_number1", 0],
33
+ * ["slice_number2", 1]
34
+ * ]
35
+ *
36
+ * We decode them as separate entries because each of the values might require a different codec as per what is specified in the SharedSlice custom type.
37
+ * In this way we save on the decoding performance in comparison to a scenario where we would decode the whole content object as a record with a union type decoder for each value.
38
+ *
39
+ * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
40
+ * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
41
+ * @param sliceFieldModels models of the fields of the slice from custom type - used to know which codec to use for decoding each value of the object
42
+ */
43
+ export declare const SharedSliceContentEntry: (sliceName: string, sliceContentField: "primary" | "items", sliceFieldModels: Record<WidgetKey, NestableWidget>) => t.Type<SharedSliceContentEntry, SharedSliceContentEntry, unknown>;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedSliceContentEntry = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const E = (0, tslib_1.__importStar)(require("fp-ts/Either"));
6
+ const function_1 = require("fp-ts/function");
7
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const common_1 = require("../../../../../../../common");
9
+ const nestable_1 = require("../../../../nestable");
10
+ const errors_1 = require("./errors");
11
+ /**
12
+ * Represents a single entry in the 'primary' field or a single entry in an item in the 'items' array in the SharedSlice. It is used to build the SharedSliceContent type from entries of the import object.
13
+ *
14
+ * For example, given the following slice:
15
+ * {
16
+ * "slice_type": "my_slice",
17
+ * "variation": "default",
18
+ * "primary": {
19
+ * "slice_text": "abc", // This is one entry
20
+ * "slice_text2": "def: // This is another entry
21
+ * },
22
+ * "items": [
23
+ * {
24
+ * "slice_number1": 0, // This is one entry
25
+ * "slice_number2": 1 // And this is yet another entry
26
+ * }
27
+ * ]
28
+ * }
29
+ *
30
+ * The 'primary' object is converted to following entries to be decoded:
31
+ * [
32
+ * ["slice_text", "abc"],
33
+ * ["slice_text2", "def"]
34
+ * ]
35
+ * and the first item in the 'items' array is converted to following entries to be decoded:
36
+ * [
37
+ * ["slice_number1", 0],
38
+ * ["slice_number2", 1]
39
+ * ]
40
+ *
41
+ * We decode them as separate entries because each of the values might require a different codec as per what is specified in the SharedSlice custom type.
42
+ * In this way we save on the decoding performance in comparison to a scenario where we would decode the whole content object as a record with a union type decoder for each value.
43
+ *
44
+ * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
45
+ * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
46
+ * @param sliceFieldModels models of the fields of the slice from custom type - used to know which codec to use for decoding each value of the object
47
+ */
48
+ const SharedSliceContentEntry = (sliceName, sliceContentField, sliceFieldModels) => new t.Type("SharedSliceContentEntry", (u) => t.tuple([common_1.WidgetKey, t.unknown]).is(u) && nestable_1.ImportNestable.is(u[1]), (u, c) => {
49
+ return (0, function_1.pipe)(t.tuple([common_1.WidgetKey, t.unknown]).validate(u, c), E.chain(([key, content]) => {
50
+ const model = sliceFieldModels[key];
51
+ if (!model) {
52
+ return t.failure([key, content], [...c, { key, actual: content, type: t.unknown }], errors_1.SharedSliceContentErrors.UnknownField(sliceName, sliceContentField, key));
53
+ }
54
+ const { codec, result } = nestable_1.ImportNestable.decode(model)(content);
55
+ return (0, function_1.pipe)(result, E.map((decodedContent) => [
56
+ key,
57
+ decodedContent,
58
+ ]), E.mapLeft((errors) => errors.map((error) => {
59
+ const context = [
60
+ ...c,
61
+ ...error.context.filter(({ key }) => !!key),
62
+ { key, actual: content, type: codec },
63
+ ];
64
+ const updatedError = { ...error, context };
65
+ return updatedError;
66
+ })));
67
+ }));
68
+ }, t.identity);
69
+ exports.SharedSliceContentEntry = SharedSliceContentEntry;
@@ -0,0 +1,4 @@
1
+ import type { SharedSliceContentField } from "./types";
2
+ export declare const SharedSliceContentErrors: {
3
+ readonly UnknownField: (sliceName: string, contentField: SharedSliceContentField, fieldName: string) => string;
4
+ };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedSliceContentErrors = void 0;
4
+ exports.SharedSliceContentErrors = {
5
+ UnknownField: (sliceName, contentField, fieldName) => `Unsupported field '${fieldName}'for '${contentField}' content in Shared Slice '${sliceName}'`,
6
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./errors";
2
+ export * from "./SharedSliceContent";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./errors"), exports);
5
+ (0, tslib_1.__exportStar)(require("./SharedSliceContent"), exports);
@@ -0,0 +1 @@
1
+ export declare type SharedSliceContentField = "primary" | "items";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ import * as t from "io-ts";
2
+ import { UUID } from "../../../../../../common/UUID";
3
+ export declare type SharedSliceId = `${string}$${UUID}`;
4
+ export declare const SharedSliceIdValidationError: {
5
+ readonly Base: "Slice id must be a non empty string matching the following pattern 'slice_type$uuid'";
6
+ readonly IncorrectUUID: (uuid: string | undefined) => string;
7
+ readonly IncorrectSliceType: (sliceType: string | undefined) => string;
8
+ };
9
+ export declare const SharedSliceId: t.Type<string, string, unknown>;