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

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 (57) hide show
  1. package/lib/import/converters/fields/utils.d.ts +1 -0
  2. package/lib/import/converters/fields/utils.js +10 -0
  3. package/lib/import/validators/fields/nestable/Image/index.js +12 -1
  4. package/lib/validators/DefaultOrElse.js +1 -1
  5. package/lib/validators/NullOrT.d.ts +2 -0
  6. package/lib/validators/NullOrT.js +13 -0
  7. package/package.json +1 -1
  8. package/src/import/validators/fields/nestable/Image/index.ts +33 -2
  9. package/src/validators/DefaultOrElse.ts +1 -1
  10. package/lib/import/converters/fields/Slices/SliceItem.d.ts +0 -4
  11. package/lib/import/converters/fields/Slices/SliceItem.js +0 -24
  12. package/lib/import/validators/fields/ImportSlices/ImportSliceId.d.ts +0 -9
  13. package/lib/import/validators/fields/ImportSlices/ImportSliceId.js +0 -43
  14. package/lib/import/validators/fields/ImportSlices/ImportSliceItem.d.ts +0 -24
  15. package/lib/import/validators/fields/ImportSlices/ImportSliceItem.js +0 -87
  16. package/lib/import/validators/fields/ImportSlices/ImportSliceItemContent.d.ts +0 -12
  17. package/lib/import/validators/fields/ImportSlices/ImportSliceItemContent.js +0 -41
  18. package/lib/import/validators/fields/Slices/SharedSlice/SharedSlice.d.ts +0 -14
  19. package/lib/import/validators/fields/Slices/SharedSlice/SharedSlice.js +0 -60
  20. package/lib/import/validators/fields/Slices/SharedSlice/errors.d.ts +0 -5
  21. package/lib/import/validators/fields/Slices/SharedSlice/errors.js +0 -8
  22. package/lib/import/validators/fields/Slices/SharedSlice/fields/OptionalSharedSliceId.d.ts +0 -2
  23. package/lib/import/validators/fields/Slices/SharedSlice/fields/OptionalSharedSliceId.js +0 -15
  24. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +0 -17
  25. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +0 -38
  26. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +0 -43
  27. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +0 -69
  28. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/errors.d.ts +0 -5
  29. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/errors.js +0 -9
  30. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/index.d.ts +0 -2
  31. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/index.js +0 -5
  32. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/types.d.ts +0 -1
  33. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceContent/types.js +0 -2
  34. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceId.d.ts +0 -9
  35. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceId.js +0 -39
  36. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceItems.d.ts +0 -11
  37. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceItems.js +0 -25
  38. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceType.d.ts +0 -11
  39. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceType.js +0 -24
  40. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceVariation.d.ts +0 -11
  41. package/lib/import/validators/fields/Slices/SharedSlice/fields/SharedSliceVariation.js +0 -24
  42. package/lib/import/validators/fields/Slices/SharedSlice/fields/index.d.ts +0 -6
  43. package/lib/import/validators/fields/Slices/SharedSlice/fields/index.js +0 -9
  44. package/lib/import/validators/fields/Slices/SharedSlice/index.d.ts +0 -3
  45. package/lib/import/validators/fields/Slices/SharedSlice/index.js +0 -6
  46. package/lib/import/validators/fields/Slices/SharedSlice/utils.d.ts +0 -339
  47. package/lib/import/validators/fields/Slices/SharedSlice/utils.js +0 -11
  48. package/lib/import/validators/fields/Slices/Slices.d.ts +0 -8
  49. package/lib/import/validators/fields/Slices/Slices.js +0 -29
  50. package/lib/import/validators/fields/Slices/index.d.ts +0 -1
  51. package/lib/import/validators/fields/Slices/index.js +0 -4
  52. package/lib/import/validators/fields/Slices/utils.d.ts +0 -4
  53. package/lib/import/validators/fields/Slices/utils.js +0 -35
  54. package/lib/import/validators/fields/Slices.d.ts +0 -4
  55. package/lib/import/validators/fields/Slices.js +0 -12
  56. package/lib/import/validators/fields/nestable/Image.d.ts +0 -45
  57. package/lib/import/validators/fields/nestable/Image.js +0 -45
@@ -0,0 +1 @@
1
+ export declare const nullableConverter: <I, O>(value: I | null, converter: (value: I) => O) => O | undefined;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nullableConverter = void 0;
4
+ const nullableConverter = (value, converter) => {
5
+ if (value === null)
6
+ return undefined;
7
+ else
8
+ return converter(value);
9
+ };
10
+ exports.nullableConverter = nullableConverter;
@@ -5,10 +5,20 @@ const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/Either");
6
6
  const function_1 = require("fp-ts/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const io_ts_types_1 = require("io-ts-types");
8
9
  const validators_1 = require("../../../../../validators");
9
10
  const ImportContent_1 = require("../../ImportContent");
10
11
  const Decoder_1 = require("./Decoder");
11
12
  const Validator_1 = require("./Validator");
13
+ const defaultImage = (field) => {
14
+ var _a, _b;
15
+ const thumbnailsNames = ((_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.map((t) => t.name)) || [];
16
+ const thumbnailsCodec = (0, io_ts_types_1.withMessage)(t.type(thumbnailsNames.reduce((acc, name) => ({
17
+ ...acc,
18
+ [name]: validators_1.EmptyObject,
19
+ }), {})), () => "The value must be an object with thumbnails as empty objects");
20
+ return validators_1.AnyObject.pipe(thumbnailsCodec);
21
+ };
12
22
  const ImageField = (field) => new t.Type("ImageField", (u) => Validator_1.ImageFieldValidator.is(u) &&
13
23
  "thumbnails" in u &&
14
24
  Validator_1.ThumbnailsValidator.is(u["thumbnails"]), (u, ctx) => {
@@ -23,6 +33,7 @@ const ImageField = (field) => new t.Type("ImageField", (u) => Validator_1.ImageF
23
33
  }));
24
34
  }));
25
35
  }, t.identity);
26
- const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", (0, validators_1.NullOrElse)(ImageField(field)));
36
+ const ImageCodec = (field) => (0, validators_1.DefaultOrElse)(defaultImage(field))(ImageField(field));
37
+ const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", ImageCodec(field));
27
38
  exports.ImportImage = ImportImage;
28
39
  (0, tslib_1.__exportStar)(require("./model"), exports);
@@ -6,7 +6,7 @@ const Either_1 = require("fp-ts/lib/Either");
6
6
  const t = (0, tslib_1.__importStar)(require("io-ts"));
7
7
  const BasicTypes_1 = require("./BasicTypes");
8
8
  const DefaultOrElse = (inputValidator) => {
9
- return (codec) => new t.Type("NullOrT", (u) => null === u || codec.is(u), (u) => {
9
+ return (codec) => new t.Type("DefaultOrT", (u) => null === u || codec.is(u), (u) => {
10
10
  if ((0, Either_1.isRight)(inputValidator.decode(u)))
11
11
  return t.success(null);
12
12
  return codec.decode(u);
@@ -0,0 +1,2 @@
1
+ import * as t from "io-ts";
2
+ export declare const NullOrT: <T, O = T>(codec: t.Type<T, T, unknown>) => (asO: (value: T | null) => O) => t.Type<O, O, unknown>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NullOrT = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fp_ts_1 = require("fp-ts");
6
+ const function_1 = require("fp-ts/function");
7
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
8
+ const NullOrT = (codec) => {
9
+ return (asO) => new t.Type(`NullOr${codec.name}`, (u) => t.null.is(u) || codec.is(u), (u) => {
10
+ return (0, function_1.pipe)(t.union([t.null, codec]).decode(u), fp_ts_1.either.map((decoded) => asO(decoded)));
11
+ }, (f) => f);
12
+ };
13
+ exports.NullOrT = NullOrT;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-alpha.11",
3
+ "version": "2.2.0-alpha.13",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -2,14 +2,37 @@ import { chain, map } from "fp-ts/Either"
2
2
  import { pipe } from "fp-ts/function"
3
3
  import type { Validation } from "io-ts"
4
4
  import * as t from "io-ts"
5
+ import { withMessage } from "io-ts-types"
5
6
 
6
7
  import type { Image as ImageDefinition } from "../../../../../customtypes"
7
- import { NullOrElse } from "../../../../../validators"
8
+ import {
9
+ AnyObject,
10
+ DefaultOrElse,
11
+ EmptyObject,
12
+ } from "../../../../../validators"
8
13
  import { ImportContent } from "../../ImportContent"
9
14
  import { decodeImageField, decodeThumbnails } from "./Decoder"
10
15
  import type { ImageFieldWithThumbnails } from "./model"
11
16
  import { ImageFieldValidator, ThumbnailsValidator } from "./Validator"
12
17
 
18
+ const defaultImage = (field?: ImageDefinition) => {
19
+ const thumbnailsNames = field?.config?.thumbnails?.map((t) => t.name) || []
20
+ const thumbnailsCodec = withMessage(
21
+ t.type(
22
+ thumbnailsNames.reduce(
23
+ (acc, name) => ({
24
+ ...acc,
25
+ [name]: EmptyObject,
26
+ }),
27
+ {},
28
+ ),
29
+ ),
30
+ () => "The value must be an object with thumbnails as empty objects",
31
+ )
32
+
33
+ return AnyObject.pipe(thumbnailsCodec)
34
+ }
35
+
13
36
  const ImageField = (field?: ImageDefinition) =>
14
37
  new t.Type<ImageFieldWithThumbnails>(
15
38
  "ImageField",
@@ -39,7 +62,15 @@ const ImageField = (field?: ImageDefinition) =>
39
62
  },
40
63
  t.identity,
41
64
  )
65
+
66
+ const ImageCodec = (field?: ImageDefinition) =>
67
+ DefaultOrElse<
68
+ Record<string, unknown>,
69
+ ImageFieldWithThumbnails,
70
+ ImageFieldWithThumbnails
71
+ >(defaultImage(field))(ImageField(field))
72
+
42
73
  export const ImportImage = (field?: ImageDefinition) =>
43
- ImportContent("Image", NullOrElse(ImageField(field)))
74
+ ImportContent("Image", ImageCodec(field))
44
75
  export type ImportImage = t.TypeOf<ReturnType<typeof ImportImage>>
45
76
  export * from "./model"
@@ -6,7 +6,7 @@ import { EmptyArray, EmptyObject } from "./BasicTypes"
6
6
  export const DefaultOrElse = <I, A, O = A>(inputValidator: t.Type<I>) => {
7
7
  return (codec: t.Type<A, O>) =>
8
8
  new t.Type<A | null, O | undefined>(
9
- "NullOrT",
9
+ "DefaultOrT",
10
10
  (u: unknown): u is A | null => null === u || codec.is(u),
11
11
  (u: unknown) => {
12
12
  if (isRight(inputValidator.decode(u))) return t.success(null)
@@ -1,4 +0,0 @@
1
- import type { Asset } from "../../../../common";
2
- import type { SharedSliceContent } from "../../../../content";
3
- import type { SharedSlice as ImportSharedSlice } from "../../../validators/fields/Slices/SharedSlice";
4
- export declare const importSliceItemConverter: (field: ImportSharedSlice, assets: Record<string, Asset>) => SharedSliceContent;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.importSliceItemConverter = 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
- const importSliceItemContentConverter = (content, assets) => (0, function_1.pipe)(content,
11
- // convertNestableWidget can theoretically return undefined, so we need to filter out those values
12
- R.filterMap((contentValue) => (0, function_1.pipe)(contentValue, (content) => (0, nestable_1.convertNestableWidget)(content, assets), O.fromNullable)));
13
- const itemsToRepeatableWidgetsConverter = (items, assets) => items.map((item) => (0, function_1.pipe)(importSliceItemContentConverter(item, assets), (record) => Object.entries(record), (entries) => ({ __TYPE__: content_1.GroupItemContentType, value: entries })));
14
- const importSliceItemConverter = (field, assets) => ({
15
- __TYPE__: content_1.SharedSliceContentType,
16
- primary: field.primary
17
- ? importSliceItemContentConverter(field.primary, assets)
18
- : {},
19
- items: field.items
20
- ? itemsToRepeatableWidgetsConverter(field.items, assets)
21
- : [],
22
- variation: field.variation,
23
- });
24
- exports.importSliceItemConverter = importSliceItemConverter;
@@ -1,9 +0,0 @@
1
- import * as t from "io-ts";
2
- import { UUID } from "../../../../common/UUID";
3
- export declare type ImportSliceId = `${string}$${UUID}`;
4
- export declare const ImportSliceIdValidationError: {
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 ImportSliceId: t.Type<string, string, unknown>;
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImportSliceId = exports.ImportSliceIdValidationError = 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 UUID_1 = require("../../../../common/UUID");
9
- const validators_1 = require("../../../../validators");
10
- const baseErrorText = "Slice id must be a non empty string matching the following pattern 'slice_type$uuid'";
11
- exports.ImportSliceIdValidationError = {
12
- Base: baseErrorText,
13
- IncorrectUUID: (uuid) => `Incorrect UUID: '${uuid}' - ${baseErrorText}`,
14
- IncorrectSliceType: (sliceType) => `Incorrect slice_type: '${sliceType}' - ${baseErrorText}`,
15
- };
16
- exports.ImportSliceId = new t.Type("ImportSliceId", (u) => {
17
- if (typeof u !== "string") {
18
- return false;
19
- }
20
- const [sliceName, uuid] = u.split("$");
21
- return validators_1.NonEmptyString.is(sliceName) && UUID_1.UUID.is(uuid);
22
- }, (u, c) => (0, function_1.pipe)(validators_1.NonEmptyString.validate(u, c), E.mapLeft((errors) => errors.map((error) => ({
23
- ...error,
24
- message: exports.ImportSliceIdValidationError.Base,
25
- }))), E.chain((sliceId) => {
26
- const [sliceName, uuid] = sliceId.split("$");
27
- return (0, function_1.pipe)(t.tuple([validators_1.NonEmptyString, UUID_1.UUID]).validate([sliceName, uuid], c), E.mapLeft((errors) => errors.map((error) => {
28
- const tupleErrorContext = error.context[error.context.length - 1];
29
- const correctErrorKey = (tupleErrorContext === null || tupleErrorContext === void 0 ? void 0 : tupleErrorContext.key) === "0" ? "slice_type" : "uuid";
30
- const codec = (tupleErrorContext === null || tupleErrorContext === void 0 ? void 0 : tupleErrorContext.key) === "0" ? validators_1.NonEmptyString : UUID_1.UUID;
31
- const actual = (tupleErrorContext === null || tupleErrorContext === void 0 ? void 0 : tupleErrorContext.key) === "0" ? sliceName : uuid;
32
- return {
33
- ...error,
34
- context: [
35
- ...error.context.slice(0, -1),
36
- { key: correctErrorKey, type: codec, actual },
37
- ],
38
- message: (tupleErrorContext === null || tupleErrorContext === void 0 ? void 0 : tupleErrorContext.key) === "0"
39
- ? exports.ImportSliceIdValidationError.IncorrectSliceType(sliceName)
40
- : exports.ImportSliceIdValidationError.IncorrectUUID(uuid),
41
- };
42
- })));
43
- }), E.map(([sliceName, uuid]) => `${sliceName}$${uuid}`)), (sliceId) => sliceId);
@@ -1,24 +0,0 @@
1
- import * as t from "io-ts";
2
- import { SharedSlice } from "../../../../customtypes";
3
- import { ImportSliceId } from "./ImportSliceId";
4
- import { ImportSliceItemContent } from "./ImportSliceItemContent";
5
- export declare const ImportSliceItemErrors: {
6
- readonly SliceTypeNotFound: (slice_type: string) => string;
7
- readonly VariationNotFound: (variation: string, slice_type: string) => string;
8
- readonly SliceContentItemErrors: {
9
- readonly UnknownField: (sliceName: string, sliceContentField: "primary" | "items", fieldName: string) => string;
10
- readonly InvalidSliceItemContent: (sliceName: string, sliceContentField: "primary" | "items") => string;
11
- };
12
- readonly InvalidItemsArray: (slice_type: string) => string;
13
- };
14
- export declare type ImportSliceItem = {
15
- id: ImportSliceId | null | undefined;
16
- slice_type: string;
17
- name: string;
18
- variation: string;
19
- primary: ImportSliceItemContent | null | undefined;
20
- items: ImportSliceItemContent[] | null | undefined;
21
- slice_label: string | null | undefined;
22
- version: string | null | undefined;
23
- };
24
- export declare const ImportSliceItem: (sharedSlices: SharedSlice[]) => t.Type<ImportSliceItem, ImportSliceItem, unknown>;
@@ -1,87 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImportSliceItem = exports.ImportSliceItemErrors = 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
- const customtypes_1 = require("../../../../customtypes");
10
- const validators_1 = require("../../../../validators");
11
- const ImportSliceId_1 = require("./ImportSliceId");
12
- const ImportSliceItemContent_1 = require("./ImportSliceItemContent");
13
- exports.ImportSliceItemErrors = {
14
- SliceTypeNotFound: (slice_type) => `Slice '${slice_type}' not found in document's custom type`,
15
- VariationNotFound: (variation, slice_type) => `Variation '${variation}' not found for Slice '${slice_type}'`,
16
- SliceContentItemErrors: ImportSliceItemContent_1.SliceItemContentErrors,
17
- InvalidItemsArray: (slice_type) => `Invalid value for 'items' array in Slice '${slice_type}'. Must be an array of key-value records.`,
18
- };
19
- const SliceTypeWithSliceDataShape = t.type({
20
- slice_type: validators_1.NonEmptyString,
21
- slice: customtypes_1.SharedSlice,
22
- });
23
- const findSlice = (slices) => (sliceId) => (0, function_1.pipe)(slices.find((slice) => slice.id === sliceId), E.fromNullable(exports.ImportSliceItemErrors.SliceTypeNotFound(sliceId)));
24
- const SliceTypeWithSliceData = (slices) => new t.Type("slice_type", (u) => SliceTypeWithSliceDataShape.is(u), (u, c) => (0, function_1.pipe)(validators_1.NonEmptyString.validate(u, c), E.chain((slice_type) => (0, function_1.pipe)(findSlice(slices)(slice_type), E.fold((err) => t.failure(u, c, err), (data) => t.success({ slice_type, data }))))), ({ slice_type }) => slice_type);
25
- const SliceVariationWithVariationDataShape = t.type({
26
- variation: validators_1.NonEmptyString,
27
- data: customtypes_1.Variation,
28
- });
29
- const findSliceVariation = (slice) => (variation) => (0, function_1.pipe)(slice.variations.find((v) => v.id === variation), E.fromNullable(exports.ImportSliceItemErrors.VariationNotFound(variation, slice.id)));
30
- const SliceVariationWithVariationData = (slice) => new t.Type("variation", (u) => SliceVariationWithVariationDataShape.is(u), (u, c) => (0, function_1.pipe)(validators_1.NonEmptyString.validate(u, c), E.chain((variation) => (0, function_1.pipe)(findSliceVariation(slice)(variation), E.fold((err) => t.failure(u, c, err), (data) => t.success({ variation, data }))))), ({ variation }) => variation);
31
- const SliceId = new t.Type("ImportSliceId", (u) => u === null || u === undefined || ImportSliceId_1.ImportSliceId.is(u), (u, c) => {
32
- if (u === null || u === undefined)
33
- return t.success(u);
34
- return ImportSliceId_1.ImportSliceId.validate(u, c);
35
- }, t.identity);
36
- const SliceLabel = t.union([t.undefined, t.null, validators_1.String]);
37
- const SliceVersion = t.union([t.undefined, t.null, validators_1.String]);
38
- const ImportSliceItemShape = t.type({
39
- id: SliceId,
40
- slice_type: validators_1.NonEmptyString,
41
- name: validators_1.String,
42
- variation: validators_1.NonEmptyString,
43
- primary: t.union([t.undefined, t.null, t.record(validators_1.NonEmptyString, t.unknown)]),
44
- items: t.union([
45
- t.undefined,
46
- t.null,
47
- t.array(t.record(validators_1.NonEmptyString, t.unknown)),
48
- ]),
49
- slice_label: SliceLabel,
50
- version: SliceVersion,
51
- });
52
- const baseFields = (sharedSlices) => t.type({
53
- id: SliceId,
54
- slice_type: SliceTypeWithSliceData(sharedSlices),
55
- slice_label: SliceLabel,
56
- version: SliceVersion,
57
- });
58
- const variationData = (slice) => t.type({ variation: SliceVariationWithVariationData(slice) });
59
- const Items = (sliceName, model) => {
60
- const ItemContentCodec = (0, ImportSliceItemContent_1.ImportSliceItemContent)(sliceName, "items", model);
61
- return new t.Type("Items", (u) => t.array(ItemContentCodec).is(u), (u, c) => (0, function_1.pipe)((0, io_ts_types_1.withMessage)(t.array(t.unknown), () => exports.ImportSliceItemErrors.InvalidItemsArray(sliceName)).validate(u, c), E.chain((arr) => t.array(ItemContentCodec).validate(arr, c))), t.identity);
62
- };
63
- const ImportSliceItem = (sharedSlices) => new t.Type("ImportSliceItem", (u) => ImportSliceItemShape.is(u), (u, c) => (0, function_1.pipe)(baseFields(sharedSlices).validate(u, c), E.chain((decoded) => (0, function_1.pipe)(variationData(decoded.slice_type.data).validate(u, c), E.map(({ variation }) => ({ ...decoded, variation })))), E.chain((decoded) => {
64
- var _a;
65
- return (0, function_1.pipe)(t
66
- .partial({
67
- primary: (0, ImportSliceItemContent_1.ImportSliceItemContent)(decoded.slice_type.slice_type, "primary", (_a = decoded.variation.data.primary) !== null && _a !== void 0 ? _a : {}),
68
- })
69
- .validate(u, c), E.map(({ primary }) => ({ ...decoded, primary })));
70
- }), E.chain((decoded) => {
71
- var _a;
72
- return (0, function_1.pipe)(t
73
- .partial({
74
- items: Items(decoded.slice_type.slice_type, (_a = decoded.variation.data.items) !== null && _a !== void 0 ? _a : {}),
75
- })
76
- .validate(u, c), E.map(({ items }) => ({ ...decoded, items })));
77
- }), E.map(({ id, slice_type, variation, primary, items, slice_label, version, }) => ({
78
- id,
79
- slice_type: slice_type.slice_type,
80
- name: slice_type.data.name,
81
- variation: variation.variation,
82
- primary,
83
- items,
84
- slice_label,
85
- version: version !== null && version !== void 0 ? version : variation.data.version,
86
- }))), t.identity);
87
- exports.ImportSliceItem = ImportSliceItem;
@@ -1,12 +0,0 @@
1
- import * as t from "io-ts";
2
- import { WidgetKey } from "../../../../common";
3
- import type { NestableWidget } from "../../../../customtypes";
4
- import { ImportNestable } from "../nestable";
5
- declare type SliceContentField = "primary" | "items";
6
- export declare const SliceItemContentErrors: {
7
- readonly UnknownField: (sliceName: string, sliceContentField: SliceContentField, fieldName: string) => string;
8
- readonly InvalidSliceItemContent: (sliceName: string, sliceContentField: SliceContentField) => string;
9
- };
10
- export declare type ImportSliceItemContent = Record<WidgetKey, ImportNestable>;
11
- export declare const ImportSliceItemContent: (sliceName: string, sliceContentField: "primary" | "items", sliceFieldModels: Record<WidgetKey, NestableWidget>) => t.Type<ImportSliceItemContent, ImportSliceItemContent, unknown>;
12
- export {};
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImportSliceItemContent = exports.SliceItemContentErrors = 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 io_ts_types_1 = require("io-ts-types");
9
- const common_1 = require("../../../../common");
10
- const io_ts_1 = require("../../../../utils/io-ts");
11
- const nestable_1 = require("../nestable");
12
- exports.SliceItemContentErrors = {
13
- UnknownField: (sliceName, sliceContentField, fieldName) => `Unsupported field '${fieldName}'for ${sliceContentField} content in Slice '${sliceName}'`,
14
- InvalidSliceItemContent: (sliceName, sliceContentField) => {
15
- return `Invalid value for '${sliceContentField}' content in Slice '${sliceName}'. Must be a key-value record.`;
16
- },
17
- };
18
- const SliceItemContentEntry = (sliceName, sliceContentField, sliceFieldModels) => new t.Type("SliceItemContentEntry", (u) => t.tuple([common_1.WidgetKey, t.unknown]).is(u) && nestable_1.ImportNestable.is(u[1]), (u, c) => {
19
- return (0, function_1.pipe)(t.tuple([common_1.WidgetKey, t.unknown]).validate(u, c), E.chain(([key, content]) => {
20
- const model = sliceFieldModels[key];
21
- if (!model) {
22
- return t.failure([key, content], [...c, { key, actual: content, type: t.unknown }], exports.SliceItemContentErrors.UnknownField(sliceName, sliceContentField, key));
23
- }
24
- const { codec, result } = nestable_1.ImportNestable.decode(model)(content);
25
- return (0, function_1.pipe)(result, E.map((decodedContent) => [
26
- key,
27
- decodedContent,
28
- ]), E.mapLeft((errors) => errors.map((error) => {
29
- const context = [
30
- ...c,
31
- ...error.context.filter(({ key }) => !!key),
32
- { key, actual: content, type: codec },
33
- ];
34
- const updatedError = { ...error, context };
35
- return updatedError;
36
- })));
37
- }));
38
- }, t.identity);
39
- const RawContent = (sliceName, sliceContentField) => (0, io_ts_types_1.withMessage)(t.record(common_1.WidgetKey, t.unknown), () => exports.SliceItemContentErrors.InvalidSliceItemContent(sliceName, sliceContentField));
40
- const ImportSliceItemContent = (sliceName, sliceContentField, sliceFieldModels) => new t.Type("ImportSliceItemContent", (u) => t.record(common_1.WidgetKey, t.unknown).is(u), (u, c) => (0, function_1.pipe)(RawContent(sliceName, sliceContentField).validate(u, c), E.chain((rawContent) => (0, function_1.pipe)(Object.entries(rawContent).map((entry) => SliceItemContentEntry(sliceName, sliceContentField, sliceFieldModels).validate(entry, c)), io_ts_1.combineValidationResults, E.map((entries) => entries.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}))))), t.identity);
41
- exports.ImportSliceItemContent = ImportSliceItemContent;
@@ -1,14 +0,0 @@
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>;
@@ -1,60 +0,0 @@
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: (0, fields_1.SharedSliceItems)(decoded.slice_type.slice_type, (_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;
@@ -1,5 +0,0 @@
1
- export declare const SharedSliceErrors: {
2
- readonly SliceTypeNotFound: (slice_type: string) => string;
3
- readonly VariationNotFound: (variation: string, slice_type: string) => string;
4
- readonly InvalidItemsArray: (slice_type: string) => string;
5
- };
@@ -1,8 +0,0 @@
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
- InvalidItemsArray: (slice_type) => `Invalid value for 'items' array in Shared Slice '${slice_type}'. Must be an array of key-value records.`,
8
- };
@@ -1,2 +0,0 @@
1
- import * as t from "io-ts";
2
- export declare const OptionalSharedSliceId: t.Type<string | null | undefined, string | null | undefined, unknown>;
@@ -1,15 +0,0 @@
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);
@@ -1,17 +0,0 @@
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>;
@@ -1,38 +0,0 @@
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 io_ts_types_1 = require("io-ts-types");
9
- const common_1 = require("../../../../../../../common");
10
- const io_ts_1 = require("../../../../../../../utils/io-ts");
11
- const errors_1 = require("./errors");
12
- const SharedSliceContentEntry_1 = require("./SharedSliceContentEntry");
13
- /**
14
- * Builds the decoder for the raw content object in the 'primary' field or a single item in the 'items' array in the SharedSlice.
15
- *
16
- * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
17
- * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
18
- */
19
- const RawContent = (sliceName, sliceContentField) => (0, io_ts_types_1.withMessage)(t.record(common_1.WidgetKey, t.unknown), () => errors_1.SharedSliceContentErrors.InvalidSliceItemContent(sliceName, sliceContentField));
20
- /**
21
- * Builds the decoder for a single entry in the 'primary' field or a single item in the 'items' array in the SharedSlice.
22
- *
23
- * @param sliceName name of the slice - used for error reporting in order to know which slice is being decoded
24
- * @param sliceContentField 'primary' or 'items' - used for error reporting in order to know which field of the slice is being decoded
25
- * @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
26
- * @constructor
27
- */
28
- 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)(
29
- // We first decode the raw content object in the shape of Record<WidgetKey, unknown>
30
- RawContent(sliceName, sliceContentField).validate(u, c), E.chain((rawContent) => (0, function_1.pipe)(
31
- // We split the object into entries because there is no easy other way to decode each value with a different codec
32
- Object.entries(rawContent).map((entry) => (0, SharedSliceContentEntry_1.SharedSliceContentEntry)(sliceName, sliceContentField, sliceFieldModels).validate(entry, c)),
33
- // We get a validation result for each entry in the content object (t.Validation<SharedSliceContentEntry>[]),
34
- // however we want to get a single validation result for the whole array of entries (t.Validation<SharedSliceContentEntry[]>).
35
- io_ts_1.combineValidationResults,
36
- // We build the object back from entries if all decoded successfully
37
- E.map((entries) => entries.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}))))), t.identity);
38
- exports.SharedSliceContent = SharedSliceContent;
@@ -1,43 +0,0 @@
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>;