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

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 (136) 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/SliceItem.d.ts +4 -0
  11. package/lib/import/converters/fields/Slices/SliceItem.js +24 -0
  12. package/lib/import/converters/fields/Slices/Slices.d.ts +4 -0
  13. package/lib/import/converters/fields/Slices/Slices.js +16 -0
  14. package/lib/import/converters/fields/Slices/index.d.ts +1 -0
  15. package/lib/import/converters/fields/Slices/index.js +4 -0
  16. package/lib/import/converters/fields/index.d.ts +1 -0
  17. package/lib/import/converters/fields/index.js +1 -0
  18. package/lib/import/converters/fields/nestable/Nestable.d.ts +2 -2
  19. package/lib/import/validators/fields/ImportField.d.ts +11 -1
  20. package/lib/import/validators/fields/ImportField.js +6 -1
  21. package/lib/import/validators/fields/ImportSlices/ImportSliceId.d.ts +9 -0
  22. package/lib/import/validators/fields/ImportSlices/ImportSliceId.js +43 -0
  23. package/lib/import/validators/fields/ImportSlices/ImportSliceItem.d.ts +24 -0
  24. package/lib/import/validators/fields/ImportSlices/ImportSliceItem.js +87 -0
  25. package/lib/import/validators/fields/ImportSlices/ImportSliceItemContent.d.ts +12 -0
  26. package/lib/import/validators/fields/ImportSlices/ImportSliceItemContent.js +41 -0
  27. package/lib/import/validators/fields/ImportSlices/ImportSlices.d.ts +8 -0
  28. package/lib/import/validators/fields/ImportSlices/ImportSlices.js +29 -0
  29. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.d.ts +14 -0
  30. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +60 -0
  31. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.d.ts +4 -0
  32. package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +7 -0
  33. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.d.ts +2 -0
  34. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.js +15 -0
  35. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +17 -0
  36. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +30 -0
  37. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +43 -0
  38. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +69 -0
  39. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.d.ts +4 -0
  40. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.js +6 -0
  41. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.d.ts +2 -0
  42. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.js +5 -0
  43. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.d.ts +1 -0
  44. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.js +2 -0
  45. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.d.ts +9 -0
  46. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.js +39 -0
  47. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.d.ts +11 -0
  48. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +24 -0
  49. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.d.ts +11 -0
  50. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +24 -0
  51. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.d.ts +5 -0
  52. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.js +8 -0
  53. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.d.ts +3 -0
  54. package/lib/import/validators/fields/ImportSlices/SharedSlice/index.js +6 -0
  55. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.d.ts +339 -0
  56. package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.js +11 -0
  57. package/lib/import/validators/fields/ImportSlices/index.d.ts +1 -0
  58. package/lib/import/validators/fields/ImportSlices/index.js +4 -0
  59. package/lib/import/validators/fields/ImportSlices/utils.d.ts +4 -0
  60. package/lib/import/validators/fields/ImportSlices/utils.js +35 -0
  61. package/lib/import/validators/fields/Slices/SharedSlice/SharedSlice.d.ts +14 -0
  62. package/lib/import/validators/fields/Slices/SharedSlice/SharedSlice.js +60 -0
  63. package/lib/import/validators/fields/Slices/SharedSlice/errors.d.ts +5 -0
  64. package/lib/import/validators/fields/Slices/SharedSlice/errors.js +8 -0
  65. package/lib/import/validators/fields/Slices/SharedSlice/fields/OptionalSharedSliceId.d.ts +2 -0
  66. package/lib/import/validators/fields/Slices/SharedSlice/fields/OptionalSharedSliceId.js +15 -0
  67. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +17 -0
  68. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +38 -0
  69. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +43 -0
  70. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +69 -0
  71. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/errors.d.ts +5 -0
  72. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/errors.js +9 -0
  73. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/index.d.ts +2 -0
  74. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/index.js +5 -0
  75. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/types.d.ts +1 -0
  76. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/types.js +2 -0
  77. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceId.d.ts +9 -0
  78. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceId.js +39 -0
  79. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceItems.d.ts +11 -0
  80. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceItems.js +25 -0
  81. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceType.d.ts +11 -0
  82. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceType.js +24 -0
  83. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceVariation.d.ts +11 -0
  84. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceVariation.js +24 -0
  85. package/lib/import/validators/fields/Slices/SharedSlice/fields/index.d.ts +6 -0
  86. package/lib/import/validators/fields/Slices/SharedSlice/fields/index.js +9 -0
  87. package/lib/import/validators/fields/Slices/SharedSlice/index.d.ts +3 -0
  88. package/lib/import/validators/fields/Slices/SharedSlice/index.js +6 -0
  89. package/lib/import/validators/fields/Slices/SharedSlice/utils.d.ts +339 -0
  90. package/lib/import/validators/fields/Slices/SharedSlice/utils.js +11 -0
  91. package/lib/import/validators/fields/Slices/Slices.d.ts +8 -0
  92. package/lib/import/validators/fields/Slices/Slices.js +29 -0
  93. package/lib/import/validators/fields/Slices/index.d.ts +1 -0
  94. package/lib/import/validators/fields/Slices/index.js +4 -0
  95. package/lib/import/validators/fields/Slices/utils.d.ts +4 -0
  96. package/lib/import/validators/fields/Slices/utils.js +35 -0
  97. package/lib/import/validators/fields/Slices.d.ts +4 -0
  98. package/lib/import/validators/fields/Slices.js +12 -0
  99. package/lib/import/validators/fields/nestable/Image.d.ts +30 -47
  100. package/lib/import/validators/fields/nestable/Image.js +22 -53
  101. package/lib/utils/io-ts.d.ts +2 -0
  102. package/lib/utils/io-ts.js +22 -0
  103. package/lib/validators/BasicTypes.d.ts +1 -0
  104. package/lib/validators/BasicTypes.js +2 -1
  105. package/lib/validators/NonEmptyString.js +2 -1
  106. package/package.json +4 -2
  107. package/src/common/UUID.ts +18 -0
  108. package/src/content/fields/slices/Slice/RepeatableContent.ts +0 -1
  109. package/src/import/converters/Document.ts +8 -3
  110. package/src/import/converters/fields/Slices/SharedSlice.ts +24 -0
  111. package/src/import/converters/fields/Slices/SharedSliceContent.ts +94 -0
  112. package/src/import/converters/fields/Slices/Slices.ts +20 -0
  113. package/src/import/converters/fields/Slices/index.ts +1 -0
  114. package/src/import/converters/fields/index.ts +1 -0
  115. package/src/import/converters/fields/nestable/Nestable.ts +2 -2
  116. package/src/import/validators/fields/ImportField.ts +7 -2
  117. package/src/import/validators/fields/ImportSlices/ImportSlices.ts +54 -0
  118. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +114 -0
  119. package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +6 -0
  120. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.ts +20 -0
  121. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +61 -0
  122. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.ts +100 -0
  123. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.ts +10 -0
  124. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.ts +2 -0
  125. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.ts +1 -0
  126. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.ts +65 -0
  127. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +45 -0
  128. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +45 -0
  129. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/index.ts +5 -0
  130. package/src/import/validators/fields/ImportSlices/SharedSlice/index.ts +3 -0
  131. package/src/import/validators/fields/ImportSlices/SharedSlice/utils.ts +21 -0
  132. package/src/import/validators/fields/ImportSlices/index.ts +1 -0
  133. package/src/import/validators/fields/ImportSlices/utils.ts +43 -0
  134. package/src/utils/io-ts.ts +29 -0
  135. package/src/validators/BasicTypes.ts +5 -0
  136. package/src/validators/NonEmptyString.ts +4 -4
@@ -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 Slices: (staticSlices: StaticSlices) => t.Type<{
5
+ type: "Slices";
6
+ value: SharedSlice[] | null;
7
+ }, SharedSlice[] | undefined, unknown>;
8
+ export declare type Slices = t.TypeOf<ReturnType<typeof Slices>>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Slices = 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 Slices = (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.Slices = Slices;
@@ -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);
@@ -0,0 +1,4 @@
1
+ import { SharedSlice as SharedSliceCustomType, StaticSlices } from "../../../../customtypes";
2
+ import type { SharedSlice, SharedSliceId } from "./SharedSlice";
3
+ export declare const extractSupportedSlices: (staticSlices: StaticSlices) => SharedSliceCustomType[];
4
+ export declare const findImportSharedSliceDuplicateIds: (slices: SharedSlice[]) => SharedSliceId[];
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findImportSharedSliceDuplicateIds = exports.extractSupportedSlices = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const A = (0, tslib_1.__importStar)(require("fp-ts/Array"));
6
+ const function_1 = require("fp-ts/function");
7
+ const O = (0, tslib_1.__importStar)(require("fp-ts/Option"));
8
+ const customtypes_1 = require("../../../../customtypes");
9
+ // Extracts all supported slices from the static slices object
10
+ // For now we only support the SharedSlice, if we want to support other slices in the future, we have to add them here
11
+ const extractSupportedSlices = (staticSlices) => {
12
+ var _a, _b;
13
+ return (0, function_1.pipe)(Object.values((_b = (_a = staticSlices.config) === null || _a === void 0 ? void 0 : _a.choices) !== null && _b !== void 0 ? _b : {}).map((slice) => (0, function_1.pipe)(customtypes_1.SharedSlice.decode(slice), O.fromEither)), A.compact);
14
+ };
15
+ exports.extractSupportedSlices = extractSupportedSlices;
16
+ const findImportSharedSliceDuplicateIds = (slices) => {
17
+ const duplicatesMap = slices.reduce((acc, { id }) => {
18
+ var _a;
19
+ if (id) {
20
+ const currentNumOfDuplicates = (_a = acc[id]) !== null && _a !== void 0 ? _a : 0;
21
+ return {
22
+ ...acc,
23
+ [id]: acc[id] === undefined ? 0 : currentNumOfDuplicates + 1,
24
+ };
25
+ }
26
+ return acc;
27
+ }, {});
28
+ return Object.entries(duplicatesMap).flatMap(([id, numOfDuplicates]) => {
29
+ if (numOfDuplicates > 0) {
30
+ return [id];
31
+ }
32
+ return [];
33
+ });
34
+ };
35
+ exports.findImportSharedSliceDuplicateIds = findImportSharedSliceDuplicateIds;
@@ -0,0 +1,4 @@
1
+ import * as t from "io-ts";
2
+ import { UUID } from "io-ts-types";
3
+ export declare type SliceId = `${string}$${UUID}`;
4
+ export declare const SliceId: t.Type<string, string, unknown>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SliceId = 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 io_ts_types_1 = require("io-ts-types");
9
+ exports.SliceId = t.string.pipe(new t.Type("SliceId", (u) => typeof u === "string", (u, c) => {
10
+ const [sliceType, uuid] = u.split("$");
11
+ return (0, function_1.pipe)(t.string.validate(sliceType, c), E.chain((sliceType) => (0, function_1.pipe)(io_ts_types_1.UUID.validate(uuid, c), E.map((decodedUUID) => `${sliceType}$${decodedUUID}`))));
12
+ }, t.identity));
@@ -1,62 +1,45 @@
1
1
  import * as t from "io-ts";
2
- export declare type ImageField = {
2
+ declare const ImageFieldCodec: t.Type<{
3
3
  id: string;
4
- edit: {
4
+ } & {
5
+ edit?: {
5
6
  x: number;
6
7
  y: number;
8
+ width: number;
9
+ height: number;
7
10
  zoom: number;
8
- background?: string;
11
+ background: string;
9
12
  };
10
- dimensions: {
11
- width?: number;
12
- height?: number;
13
+ credit?: string | null;
14
+ alt?: string | null;
15
+ }, {
16
+ [x: string]: unknown;
17
+ }, unknown>;
18
+ declare const ThumbnailsCodec: t.RecordC<t.StringC, t.Type<{
19
+ id: string;
20
+ } & {
21
+ edit?: {
22
+ x: number;
23
+ y: number;
24
+ width: number;
25
+ height: number;
26
+ zoom: number;
27
+ background: string;
13
28
  };
14
29
  credit?: string | null;
15
30
  alt?: string | null;
16
- };
31
+ }, {
32
+ [x: string]: unknown;
33
+ }, unknown>>;
34
+ declare type Thumbnails = t.TypeOf<typeof ThumbnailsCodec>;
35
+ export declare type ImageField = t.TypeOf<typeof ImageFieldCodec>;
17
36
  declare type ImageFieldWithThumbnails = ImageField & {
18
- thumbnails: Record<string, ImageField>;
37
+ thumbnails: Thumbnails;
19
38
  };
20
- declare const ImageFieldWithThumbnails: (field?: ({
21
- type: "Image";
22
- } & {
23
- fieldset?: string | null | undefined;
24
- config?: {
25
- label?: string | null | undefined;
26
- placeholder?: string;
27
- constraint?: {
28
- width?: number | null;
29
- height?: number | null;
30
- };
31
- thumbnails?: readonly ({
32
- name: string;
33
- } & {
34
- width?: number | null;
35
- height?: number | null;
36
- })[];
37
- };
38
- }) | undefined) => t.Type<ImageFieldWithThumbnails, ImageFieldWithThumbnails, unknown>;
39
- export declare const ImportImage: (field?: ({
40
- type: "Image";
41
- } & {
42
- fieldset?: string | null | undefined;
43
- config?: {
44
- label?: string | null | undefined;
45
- placeholder?: string;
46
- constraint?: {
47
- width?: number | null;
48
- height?: number | null;
49
- };
50
- thumbnails?: readonly ({
51
- name: string;
52
- } & {
53
- width?: number | null;
54
- height?: number | null;
55
- })[];
56
- };
57
- }) | undefined) => t.Type<{
39
+ declare const ImageFieldWithThumbnails: t.Type<ImageFieldWithThumbnails, ImageFieldWithThumbnails, unknown>;
40
+ export declare const ImportImage: t.Type<{
58
41
  type: "Image";
59
42
  value: ImageFieldWithThumbnails | null;
60
43
  }, ImageFieldWithThumbnails | undefined, unknown>;
61
- export declare type ImportImage = t.TypeOf<ReturnType<typeof ImportImage>>;
44
+ export declare type ImportImage = t.TypeOf<typeof ImportImage>;
62
45
  export {};
@@ -5,72 +5,41 @@ const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/Either");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const Objects_1 = require("../../../../utils/Objects");
8
9
  const validators_1 = require("../../../../validators");
9
10
  const BasicTypes_1 = require("../../../../validators/BasicTypes");
10
11
  const ImportContent_1 = require("../ImportContent");
11
- const ImageFieldValidator = BasicTypes_1.AnyObject.pipe(t.intersection([
12
+ const ImageFieldCodec = validators_1.AnyObject.pipe(t.intersection([
12
13
  t.type({
13
14
  id: BasicTypes_1.String,
14
15
  }),
15
16
  t.partial({
16
- dimensions: BasicTypes_1.AnyObject.pipe(t.partial({
17
- width: BasicTypes_1.Number,
18
- height: BasicTypes_1.Number,
19
- })),
20
- edit: BasicTypes_1.AnyObject.pipe(t.partial({
21
- x: BasicTypes_1.Number,
22
- y: BasicTypes_1.Number,
23
- zoom: BasicTypes_1.Number,
17
+ edit: validators_1.AnyObject.pipe(t.strict({
18
+ x: validators_1.Number,
19
+ y: validators_1.Number,
20
+ width: validators_1.Number,
21
+ height: validators_1.Number,
22
+ zoom: validators_1.Number,
24
23
  background: BasicTypes_1.String,
25
24
  })),
26
25
  credit: BasicTypes_1.StringOrNull,
27
26
  alt: BasicTypes_1.StringOrNull,
28
27
  }),
29
28
  ]));
30
- const ThumbnailsValidator = t.record(t.string, ImageFieldValidator);
31
- const encodeImageField = (image, mainImage, constraints) => {
32
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
33
- const background = (_a = image === null || image === void 0 ? void 0 : image.edit) === null || _a === void 0 ? void 0 : _a.background;
34
- const width = (_d = (_b = constraints === null || constraints === void 0 ? void 0 : constraints.width) !== null && _b !== void 0 ? _b : (_c = image === null || image === void 0 ? void 0 : image.dimensions) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : (_e = mainImage.dimensions) === null || _e === void 0 ? void 0 : _e.width;
35
- const height = (_h = (_f = constraints === null || constraints === void 0 ? void 0 : constraints.height) !== null && _f !== void 0 ? _f : (_g = image === null || image === void 0 ? void 0 : image.dimensions) === null || _g === void 0 ? void 0 : _g.height) !== null && _h !== void 0 ? _h : (_j = mainImage.dimensions) === null || _j === void 0 ? void 0 : _j.height;
36
- const alt = image === null || image === void 0 ? void 0 : image.alt;
37
- const credit = image === null || image === void 0 ? void 0 : image.credit;
38
- return {
39
- id: (_k = image === null || image === void 0 ? void 0 : image.id) !== null && _k !== void 0 ? _k : mainImage === null || mainImage === void 0 ? void 0 : mainImage.id,
40
- edit: {
41
- x: (_m = (_l = image === null || image === void 0 ? void 0 : image.edit) === null || _l === void 0 ? void 0 : _l.x) !== null && _m !== void 0 ? _m : 0,
42
- y: (_p = (_o = image === null || image === void 0 ? void 0 : image.edit) === null || _o === void 0 ? void 0 : _o.y) !== null && _p !== void 0 ? _p : 0,
43
- zoom: (_r = (_q = image === null || image === void 0 ? void 0 : image.edit) === null || _q === void 0 ? void 0 : _q.zoom) !== null && _r !== void 0 ? _r : 1,
44
- ...(background !== undefined ? { background } : {}),
45
- },
46
- dimensions: {
47
- ...(width !== undefined ? { width } : {}),
48
- ...(height !== undefined ? { height } : {}),
49
- },
50
- ...(alt !== undefined ? { alt } : {}),
51
- ...(credit !== undefined ? { credit } : {}),
52
- };
53
- };
54
- const encodeThumbnails = (mainImage, thumbnails, field) => {
55
- var _a, _b, _c;
56
- return (_c = (_b = (_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.thumbnails) === null || _b === void 0 ? void 0 : _b.reduce((acc, thumbnail) => ({
57
- ...acc,
58
- [thumbnail.name]: encodeImageField(thumbnails[thumbnail.name], mainImage, thumbnail),
59
- }), {})) !== null && _c !== void 0 ? _c : {};
60
- };
61
- const ImageFieldWithThumbnails = (field) => new t.Type("ImageFieldWithThumbnails", (u) => ImageFieldValidator.is(u) &&
29
+ const ThumbnailsCodec = t.record(t.string, ImageFieldCodec);
30
+ const ImageFieldWithThumbnails = new t.Type("ImageFieldWithThumbnails", (u) => ImageFieldCodec.is(u) &&
62
31
  "thumbnails" in u &&
63
- ThumbnailsValidator.is(u["thumbnails"]), (u, ctx) => {
64
- return (0, function_1.pipe)(ImageFieldValidator.validate(u, ctx), (0, Either_1.chain)((mainImage) => {
65
- const { id, dimensions, edit, credit, alt, ...maybeThumbnails } = mainImage;
66
- return (0, function_1.pipe)(ThumbnailsValidator.validate(maybeThumbnails, ctx), (0, Either_1.map)((thumbnails) => {
67
- var _a;
68
- return ({
69
- ...encodeImageField(mainImage, mainImage, (_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.constraint),
70
- thumbnails: encodeThumbnails(mainImage, thumbnails, field),
71
- });
72
- }));
32
+ ThumbnailsCodec.is(u["thumbnails"]), (u, ctx) => {
33
+ return (0, function_1.pipe)(ImageFieldCodec.validate(u, ctx), (0, Either_1.chain)((proto) => {
34
+ const { id, edit, credit, alt, ...maybeThumbnails } = proto;
35
+ return (0, function_1.pipe)(ThumbnailsCodec.validate(maybeThumbnails, ctx), (0, Either_1.map)((thumbnails) => (0, Objects_1.withOptionals)({
36
+ id,
37
+ thumbnails,
38
+ }, [
39
+ ["edit", edit],
40
+ ["credit", credit],
41
+ ["alt", alt],
42
+ ])));
73
43
  }));
74
44
  }, t.identity);
75
- const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", (0, validators_1.NullOrElse)(ImageFieldWithThumbnails(field)));
76
- exports.ImportImage = ImportImage;
45
+ exports.ImportImage = (0, ImportContent_1.ImportContent)("Image", (0, validators_1.NullOrElse)(ImageFieldWithThumbnails));
@@ -0,0 +1,2 @@
1
+ import type { Validation } from "io-ts";
2
+ export declare const combineValidationResults: <T>(validationResults: readonly Validation<T>[]) => Validation<readonly T[]>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.combineValidationResults = 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 RA = (0, tslib_1.__importStar)(require("fp-ts/ReadonlyArray"));
8
+ /*
9
+ * Semigroup simply defines how to concatenate two values of the same type.
10
+ * In this instance, Errors from io-ts are arrays already, so we just need to concatenate them.
11
+ */
12
+ const validationErrorsSemigroup = {
13
+ concat: (a, b) => [...a, ...b],
14
+ };
15
+ /*
16
+ * This is a helper function to combine multiple io-ts validations into one.
17
+ * t.array(...) works in the same way - it collects all the errors when decoding an array of values.
18
+ * However, it requires you to pass in a single io-ts validator as a parameter, which is not always convenient.
19
+ * Sometimes we have arrays of validation results received from function calls that don't have a validator instance.
20
+ */
21
+ const combineValidationResults = (validationResults) => (0, function_1.pipe)(validationResults, RA.traverse(E.getApplicativeValidation(validationErrorsSemigroup))((validation) => validation));
22
+ exports.combineValidationResults = combineValidationResults;
@@ -9,3 +9,4 @@ export declare const EmptyObject: t.Type<Record<never, never>, {
9
9
  }, unknown>;
10
10
  export declare const EmptyArray: t.Type<never[], never[], unknown>;
11
11
  export declare const AnyObject: t.RecordC<t.StringC, t.UnknownC>;
12
+ export declare const AnyArray: t.ArrayC<t.UnknownC>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AnyObject = exports.EmptyArray = exports.EmptyObject = exports.Boolean = exports.NumberOrNull = exports.Number = exports.StringOrNull = exports.String = void 0;
3
+ exports.AnyArray = exports.AnyObject = exports.EmptyArray = exports.EmptyObject = exports.Boolean = exports.NumberOrNull = exports.Number = exports.StringOrNull = exports.String = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/lib/function");
@@ -24,3 +24,4 @@ exports.EmptyArray = new t.Type("emptyArray", (u) => t.UnknownArray.is(u) && u.l
24
24
  }));
25
25
  }, () => []);
26
26
  exports.AnyObject = (0, io_ts_types_1.withMessage)(t.record(t.string, t.unknown), () => "The value must be an object");
27
+ exports.AnyArray = (0, io_ts_types_1.withMessage)(t.array(t.unknown), () => "The value must be an array");
@@ -2,5 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const t = (0, tslib_1.__importStar)(require("io-ts"));
5
+ const io_ts_types_1 = require("io-ts-types");
5
6
  const function_1 = require("./function");
6
- exports.default = (0, function_1.refineType)(t.string, "nonEmptyString", (s) => s.trim().length > 0);
7
+ exports.default = (0, io_ts_types_1.withMessage)((0, function_1.refineType)(t.string, "nonEmptyString", (s) => s.trim().length > 0), () => "The value must be a non-empty string");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-alpha.10",
3
+ "version": "2.2.0-alpha.11",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -67,7 +67,9 @@
67
67
  "peerDependencies": {
68
68
  "fp-ts": "^2.11.8",
69
69
  "io-ts": "^2.2.16",
70
- "io-ts-types": "^0.5.16"
70
+ "io-ts-types": "^0.5.16",
71
+ "uuid": "^9.0.0",
72
+ "@types/uuid": "^9.0.2"
71
73
  },
72
74
  "engines": {
73
75
  "node": ">=12.7.0"
@@ -0,0 +1,18 @@
1
+ import * as t from "io-ts"
2
+ import { withMessage } from "io-ts-types"
3
+ import { validate as validateUUID } from "uuid"
4
+
5
+ interface UUIDBrand {
6
+ readonly UUID: unique symbol
7
+ }
8
+
9
+ export const UUID = withMessage(
10
+ t.brand(
11
+ t.string,
12
+ (s): s is t.Branded<string, UUIDBrand> => validateUUID(s),
13
+ "UUID",
14
+ ),
15
+ () => "The value must be a valid UUID",
16
+ )
17
+
18
+ export type UUID = t.TypeOf<typeof UUID>
@@ -13,6 +13,5 @@ const RepeatableWidgetsBlock = t.strict({
13
13
  __TYPE__: t.literal(GroupItemContentType),
14
14
  value: t.array(RepeatableWidget),
15
15
  })
16
- t.array(RepeatableWidget)
17
16
 
18
17
  export const RepeatableWidgets = t.array(RepeatableWidgetsBlock)
@@ -1,9 +1,12 @@
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, WidgetContent } from "../../content"
4
3
  import type { ImportDocument } from "../validators"
5
4
  import type { ImportField } from "../validators/fields/ImportField"
6
- import { convertNestableWidget, uidConverter } from "./fields"
5
+ import {
6
+ convertNestableWidget,
7
+ importSlicesConverter,
8
+ uidConverter,
9
+ } from "./fields"
7
10
 
8
11
  export function convertImportToContent(
9
12
  document: ImportDocument,
@@ -27,6 +30,8 @@ function convertWidget(
27
30
  switch (field.type) {
28
31
  case "UID":
29
32
  return uidConverter(field.value)
33
+ case "Slices":
34
+ return importSlicesConverter(field.value, assets, embeds)
30
35
  default:
31
36
  return convertNestableWidget(field, assets, embeds)
32
37
  }
@@ -0,0 +1,24 @@
1
+ import { v4 as uuid } from "uuid"
2
+
3
+ import type { Asset, Embed } from "../../../../common"
4
+ import type { SliceItemContent } from "../../../../content"
5
+ import type { SharedSlice as ImportSharedSlice } from "../../../validators/fields/ImportSlices/SharedSlice"
6
+ import { importSharedSliceContentConverter } from "./SharedSliceContent"
7
+
8
+ // TODO should we put it together with SharedSliceId validator?
9
+ const buildSharedSliceId = (slice_type: string) => `${slice_type}$${uuid()}`
10
+
11
+ export const sharedSliceConverter = (
12
+ slice: ImportSharedSlice,
13
+ assets: Record<Asset["id"], Asset | undefined>,
14
+ embeds: Record<string, Embed | undefined>,
15
+ ): SliceItemContent => {
16
+ // 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
17
+ const widget = importSharedSliceContentConverter(slice, assets, embeds)
18
+ return {
19
+ key: slice.id ?? buildSharedSliceId(slice.slice_type),
20
+ name: slice.slice_type,
21
+ maybeLabel: slice.slice_label ?? undefined,
22
+ widget,
23
+ }
24
+ }
@@ -0,0 +1,94 @@
1
+ import { pipe } from "fp-ts/function"
2
+ import * as O from "fp-ts/Option"
3
+ import * as R from "fp-ts/Record"
4
+
5
+ import type { Asset, Embed } from "../../../../common"
6
+ import type { NestableContent, SharedSliceContent } from "../../../../content"
7
+ import {
8
+ GroupItemContentType,
9
+ SharedSliceContentType,
10
+ } from "../../../../content"
11
+ import type {
12
+ SharedSlice as ImportSharedSlice,
13
+ SharedSliceContent as ImportSharedSliceContent,
14
+ } from "../../../validators/fields/ImportSlices/SharedSlice"
15
+ import { convertNestableWidget } from "../nestable"
16
+
17
+ /**
18
+ * Converts the ImportSharedSliceContent which is a record of widget keys and ImportNestable values to a record of widget keys and NestableContent values.
19
+ *
20
+ * @param content a single ImportSharedSliceContent
21
+ * @param assets assets that are required for a conversion of a nestable widget
22
+ * @param embeds embeds that are required for a conversion of a nestable widget
23
+ */
24
+ const sharedSliceContentConverter = (
25
+ content: ImportSharedSliceContent,
26
+ assets: Record<Asset["id"], Asset | undefined>,
27
+ embeds: Record<string, Embed | undefined>,
28
+ ): Record<string, NestableContent> =>
29
+ pipe(
30
+ content,
31
+ // convertNestableWidget can theoretically return undefined, so we need to filter out those values
32
+ R.filterMap((contentValue) =>
33
+ pipe(
34
+ contentValue,
35
+ (content) => convertNestableWidget(content, assets, embeds),
36
+ O.fromNullable,
37
+ ),
38
+ ),
39
+ )
40
+
41
+ /**
42
+ * Converts a list of items to a list of repeatable widgets.
43
+ * Each ImportSharedSliceContent element is a record of fields, but in the content model it is represented as a list of tuples (key, value)
44
+ *
45
+ * For example, given the following slice content:
46
+ * {
47
+ * "slice_text": (ImportNestable),
48
+ * "slice_number": (ImportNestable),
49
+ * }
50
+ *
51
+ * has to be converted to:
52
+ *
53
+ * {
54
+ * __TYPE__: "GroupItemContentType",
55
+ * value: [
56
+ * ["slice_text", (NestableContent)],
57
+ * ["slice_number", (NestableContent)],
58
+ * ]
59
+ * }
60
+ *
61
+ * @param items list of items to be converted
62
+ * @param assets assets that are required for a conversion of a nestable widget
63
+ */
64
+ const itemsConverter = (
65
+ items: ImportSharedSliceContent[],
66
+ assets: Record<Asset["id"], Asset | undefined>,
67
+ embeds: Record<string, Embed | undefined>,
68
+ ): SharedSliceContent["items"] =>
69
+ items.map((item) =>
70
+ pipe(
71
+ sharedSliceContentConverter(item, assets, embeds),
72
+ (record) => Object.entries(record),
73
+ (entries) => ({ __TYPE__: GroupItemContentType, value: entries }),
74
+ ),
75
+ )
76
+
77
+ /**
78
+ * Builds SharedSliceContent model from ImportSharedSlice
79
+ * @param field ImportSharedSlice to be converted - a single slice from the slices array in the import document
80
+ * @param assets assets that are required for a conversion of a nestable widget
81
+ * @param embeds embeds that are required for a conversion of a nestable widget
82
+ */
83
+ export const importSharedSliceContentConverter = (
84
+ field: ImportSharedSlice,
85
+ assets: Record<Asset["id"], Asset | undefined>,
86
+ embeds: Record<string, Embed | undefined>,
87
+ ): SharedSliceContent => ({
88
+ __TYPE__: SharedSliceContentType,
89
+ primary: field.primary
90
+ ? sharedSliceContentConverter(field.primary, assets, embeds)
91
+ : {},
92
+ items: field.items ? itemsConverter(field.items, assets, embeds) : [],
93
+ variation: field.variation,
94
+ })
@@ -0,0 +1,20 @@
1
+ import type { Asset, Embed } from "../../../../common"
2
+ import type { SliceItemContent, SlicesContent } from "../../../../content"
3
+ import type { ImportSlices } from "../../../validators/fields/ImportSlices"
4
+ import { sharedSliceConverter } from "./SharedSlice"
5
+
6
+ export const importSlicesConverter = (
7
+ field: ImportSlices["value"],
8
+ assets: Record<Asset["id"], Asset | undefined>,
9
+ embeds: Record<string, Embed | undefined>,
10
+ ): SlicesContent | undefined => {
11
+ if (field === null) return
12
+
13
+ return {
14
+ value: field.map((slice): SliceItemContent => {
15
+ // 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
16
+ return sharedSliceConverter(slice, assets, embeds)
17
+ }),
18
+ __TYPE__: "SliceContentType",
19
+ }
20
+ }
@@ -0,0 +1 @@
1
+ export * from "./Slices"
@@ -1,2 +1,3 @@
1
1
  export * from "./nestable"
2
+ export * from "./Slices"
2
3
  export * from "./UID"
@@ -1,6 +1,6 @@
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
5
  import {
6
6
  booleanConverter,
@@ -20,7 +20,7 @@ export function convertNestableWidget(
20
20
  field: ImportNestable,
21
21
  assets: Record<Asset["id"], Asset | undefined>,
22
22
  embeds: Record<string, Embed | undefined>,
23
- ): WidgetContent | undefined {
23
+ ): NestableContent | undefined {
24
24
  switch (field.type) {
25
25
  case "Boolean":
26
26
  return booleanConverter(field.value)
@@ -1,8 +1,9 @@
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
5
 
5
- export type ImportField = ImportUID | ImportNestable
6
+ export type ImportField = ImportUID | ImportSlices | ImportNestable
6
7
 
7
8
  export const ImportField = {
8
9
  is(u: unknown): u is ImportNestable {
@@ -13,8 +14,12 @@ export const ImportField = {
13
14
  switch (field.type) {
14
15
  case "UID":
15
16
  return { codec: ImportUID, result: ImportUID.decode(content) }
16
- case "Choice":
17
17
  case "Slices":
18
+ return {
19
+ codec: ImportSlices(field),
20
+ result: ImportSlices(field).decode(content),
21
+ }
22
+ case "Choice":
18
23
  case "Group":
19
24
  throw new Error(`Unsupported type of field ${field.type}`)
20
25
  default: