@prismicio/types-internal 2.2.0-alpha.23 → 2.2.0-alpha.25

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.
@@ -6,12 +6,12 @@ const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
8
  const Objects_1 = require("../../../utils/Objects");
9
- const ImportContent = (type, valueCodec) => new t.Type("ImportField", (u) => {
9
+ const ImportContent = (type, valueCodec) => new t.Type(`ImportField: ${valueCodec.name}`, (u) => {
10
10
  if (!(0, Objects_1.isObject)(u))
11
11
  return false;
12
12
  return type === u["type"] && valueCodec.is(u["value"]);
13
- }, (u) => {
14
- return (0, function_1.pipe)(valueCodec.decode(u), fp_ts_1.either.map((decodedValue) => {
13
+ }, (u, c) => {
14
+ return (0, function_1.pipe)(valueCodec.validate(u, c), fp_ts_1.either.map((decodedValue) => {
15
15
  return {
16
16
  type,
17
17
  value: decodedValue,
@@ -52,7 +52,7 @@ export declare const ImportField: {
52
52
  } | undefined, unknown> | import("io-ts").Type<{
53
53
  type: "StructuredText";
54
54
  value: import("./nestable").ImportBlock[] | null;
55
- }, unknown[] | undefined, unknown> | import("io-ts").Type<{
55
+ }, import("./nestable").ImportBlock[] | undefined, unknown> | import("io-ts").Type<{
56
56
  type: "Number";
57
57
  value: number | null;
58
58
  }, number | undefined, unknown> | import("io-ts").Type<{
@@ -13,8 +13,9 @@ const ImportSlices = (staticSlices) => {
13
13
  const supportedSlices = (0, utils_1.extractSupportedSlices)(staticSlices);
14
14
  // For now we only support the SharedSlice, however if we want to support more in the future
15
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) => {
16
+ const SharedSliceCodec = (0, SharedSlice_1.SharedSlice)(supportedSlices);
17
+ const SlicesArrayCodec = t.array(SharedSliceCodec);
18
+ return (0, ImportContent_1.ImportContent)("Slices", (0, validators_1.EmptyArrayOrElse)(new t.ArrayType(SlicesArrayCodec.name, (u) => SlicesArrayCodec.is(u), (u, c) => {
18
19
  return (0, function_1.pipe)(SlicesArrayCodec.validate(u, c), E.chain((slices) => {
19
20
  // This part might not make sense for all Slice types in the future, but for now we only support the SharedSlice
20
21
  // In case we support more in the future, we would have to filter only the relevant type for this check
@@ -24,6 +25,6 @@ const ImportSlices = (staticSlices) => {
24
25
  }
25
26
  return t.success(slices);
26
27
  }));
27
- }, t.identity)));
28
+ }, t.identity, SharedSliceCodec)));
28
29
  };
29
30
  exports.ImportSlices = ImportSlices;
@@ -58,8 +58,8 @@ const SharedSliceContentEntry = (sliceName, sliceContentField, sliceFieldModels)
58
58
  ]), E.mapLeft((errors) => errors.map((error) => {
59
59
  const context = [
60
60
  ...c,
61
- ...error.context.filter(({ key }) => !!key),
62
61
  { key, actual: content, type: codec },
62
+ ...error.context.slice(1), // We ignore the first context element with "" key coming from ImportNestable decoder
63
63
  ];
64
64
  const updatedError = { ...error, context };
65
65
  return updatedError;
@@ -20,5 +20,5 @@ export declare const ImportRichText: (customType?: ({
20
20
  }) | undefined) => t.Type<{
21
21
  type: "StructuredText";
22
22
  value: ImportBlock[] | null;
23
- }, unknown[] | undefined, unknown>;
23
+ }, ImportBlock[] | undefined, unknown>;
24
24
  export declare type ImportRichText = t.TypeOf<ReturnType<typeof ImportRichText>>;
@@ -2,25 +2,31 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImportRichText = void 0;
4
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");
5
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
8
  const validators_1 = require("../../../../../validators");
7
- const function_1 = require("../../../../../validators/function");
9
+ const function_2 = require("../../../../../validators/function");
8
10
  const ImportContent_1 = require("../../ImportContent");
9
11
  const ImportBlock_1 = require("./ImportBlock");
10
- const ImportRichText = (customType) => (0, ImportContent_1.ImportContent)("StructuredText", (0, validators_1.EmptyArrayOrElse)((0, function_1.withCustomError)(t.array(t.unknown).pipe(new t.Type("RichTextField", (u) => t.array((0, ImportBlock_1.ImportBlock)([])).is(u), (u, c) => {
12
+ const ImportRichText = (customType) => {
11
13
  var _a, _b, _c, _d;
12
14
  const single = (_b = (_a = customType === null || customType === void 0 ? void 0 : customType.config) === null || _a === void 0 ? void 0 : _a.single) === null || _b === void 0 ? void 0 : _b.split(",");
13
15
  const multi = (_d = (_c = customType === null || customType === void 0 ? void 0 : customType.config) === null || _c === void 0 ? void 0 : _c.multi) === null || _d === void 0 ? void 0 : _d.split(",");
14
- if (multi) {
15
- return t.array((0, ImportBlock_1.ImportBlock)(multi)).validate(u, c);
16
- }
17
- else if (single) {
18
- return u.length > 1
19
- ? t.failure(u, c, "This field only allows one block")
20
- : t.array((0, ImportBlock_1.ImportBlock)(single)).validate(u, c);
21
- }
22
- else {
23
- throw new Error("Rich text config must have either a 'single' or 'multi' field defined");
24
- }
25
- }, t.identity)), () => "Rich text field must be an array")));
16
+ const allowedBlockTypes = multi || single;
17
+ const isSingleBlockAllowed = multi === undefined && single !== undefined;
18
+ const BlockCodec = (0, ImportBlock_1.ImportBlock)(allowedBlockTypes !== null && allowedBlockTypes !== void 0 ? allowedBlockTypes : []);
19
+ const RichTextArrayCodec = (0, function_2.withCustomError)(t.array(BlockCodec), () => "Rich text field must be an array");
20
+ return (0, ImportContent_1.ImportContent)("StructuredText", (0, validators_1.EmptyArrayOrElse)(new t.ArrayType(RichTextArrayCodec.name, (u) => RichTextArrayCodec.is(u), (u, c) => {
21
+ if (allowedBlockTypes === undefined) {
22
+ throw new Error("Invalid custom type for the rich text field: the model is missing a 'single' or 'multi' field defining a list of allowed block types");
23
+ }
24
+ return (0, function_1.pipe)(RichTextArrayCodec.validate(u, c), E.chain((blocks) => {
25
+ if (isSingleBlockAllowed && blocks.length > 1) {
26
+ return t.failure(blocks, c, "This field only allows one block");
27
+ }
28
+ return t.success(blocks);
29
+ }));
30
+ }, t.identity, BlockCodec)));
31
+ };
26
32
  exports.ImportRichText = ImportRichText;
@@ -62,7 +62,7 @@ export declare const ImportNestable: {
62
62
  } | undefined, unknown> | import("io-ts").Type<{
63
63
  type: "StructuredText";
64
64
  value: import("./ImportRichText").ImportBlock[] | null;
65
- }, unknown[] | undefined, unknown> | import("io-ts").Type<{
65
+ }, import("./ImportRichText").ImportBlock[] | undefined, unknown> | import("io-ts").Type<{
66
66
  type: "Number";
67
67
  value: number | null;
68
68
  }, number | undefined, unknown> | import("io-ts").Type<{
@@ -6,10 +6,10 @@ 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("DefaultOrElse", (u) => null === u || codec.is(u), (u) => {
10
- if ((0, Either_1.isRight)(inputValidator.decode(u)))
9
+ return (codec) => new t.Type(`DefaultOrElse<${inputValidator.name}, ${codec.name}>`, (u) => null === u || codec.is(u), (u, c) => {
10
+ if ((0, Either_1.isRight)(inputValidator.validate(u, c)))
11
11
  return t.success(null);
12
- return codec.decode(u);
12
+ return codec.validate(u, c);
13
13
  }, (chunk) => (chunk ? codec.encode(chunk) : undefined));
14
14
  };
15
15
  exports.DefaultOrElse = DefaultOrElse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-alpha.23",
3
+ "version": "2.2.0-alpha.25",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -10,14 +10,14 @@ export const ImportContent = <T extends FieldType, A, O = A>(
10
10
  valueCodec: t.Type<A, O>,
11
11
  ) =>
12
12
  new t.Type<{ type: T; value: A }, O>(
13
- "ImportField",
13
+ `ImportField: ${valueCodec.name}`,
14
14
  (u: unknown): u is { type: T; value: A } => {
15
15
  if (!isObject(u)) return false
16
16
  return type === u["type"] && valueCodec.is(u["value"])
17
17
  },
18
- (u: unknown) => {
18
+ (u: unknown, c) => {
19
19
  return pipe(
20
- valueCodec.decode(u),
20
+ valueCodec.validate(u, c),
21
21
  either.map((decodedValue) => {
22
22
  return {
23
23
  type,
@@ -3,7 +3,7 @@ import { pipe } from "fp-ts/function"
3
3
  import * as t from "io-ts"
4
4
 
5
5
  import type { StaticSlices } from "../../../../customtypes"
6
- import { EmptyObjectOrElse } from "../../../../validators"
6
+ import { EmptyArrayOrElse } from "../../../../validators"
7
7
  import { ImportContent } from "../ImportContent"
8
8
  import { SharedSlice } from "./SharedSlice"
9
9
  import {
@@ -16,13 +16,14 @@ export const ImportSlices = (staticSlices: StaticSlices) => {
16
16
 
17
17
  // For now we only support the SharedSlice, however if we want to support more in the future
18
18
  // we would have to change the codec here to something like this: t.array(t.union([ImportSharedSlice(sharedSlices), NewSliceCodec(newSliceCustomTypes)])).
19
- const SlicesArrayCodec = t.array(SharedSlice(supportedSlices))
19
+ const SharedSliceCodec = SharedSlice(supportedSlices)
20
+ const SlicesArrayCodec = t.array(SharedSliceCodec)
20
21
 
21
22
  return ImportContent(
22
23
  "Slices",
23
- EmptyObjectOrElse(
24
- new t.Type<SharedSlice[]>(
25
- "ImportSlices",
24
+ EmptyArrayOrElse(
25
+ new t.ArrayType<typeof SharedSliceCodec, SharedSlice[]>(
26
+ SlicesArrayCodec.name,
26
27
  (u): u is SharedSlice[] => SlicesArrayCodec.is(u),
27
28
  (u, c) => {
28
29
  return pipe(
@@ -46,6 +47,7 @@ export const ImportSlices = (staticSlices: StaticSlices) => {
46
47
  )
47
48
  },
48
49
  t.identity,
50
+ SharedSliceCodec,
49
51
  ),
50
52
  ),
51
53
  )
@@ -83,8 +83,8 @@ export const SharedSliceContentEntry = (
83
83
  errors.map((error) => {
84
84
  const context = [
85
85
  ...c,
86
- ...error.context.filter(({ key }) => !!key), // We replace the "" key coming from ImportNestable decoder
87
86
  { key, actual: content, type: codec },
87
+ ...error.context.slice(1), // We ignore the first context element with "" key coming from ImportNestable decoder
88
88
  ]
89
89
 
90
90
  const updatedError: t.ValidationError = { ...error, context }
@@ -1,3 +1,5 @@
1
+ import * as E from "fp-ts/Either"
2
+ import { pipe } from "fp-ts/function"
1
3
  import * as t from "io-ts"
2
4
 
3
5
  import type { RichText as RichTextCustomType } from "../../../../../customtypes"
@@ -6,36 +8,47 @@ import { withCustomError } from "../../../../../validators/function"
6
8
  import { ImportContent } from "../../ImportContent"
7
9
  import { ImportBlock } from "./ImportBlock"
8
10
 
9
- export const ImportRichText = (customType?: RichTextCustomType) =>
10
- ImportContent(
11
+ export const ImportRichText = (customType?: RichTextCustomType) => {
12
+ const single = customType?.config?.single?.split(",")
13
+ const multi = customType?.config?.multi?.split(",")
14
+ const allowedBlockTypes = multi || single
15
+
16
+ const isSingleBlockAllowed = multi === undefined && single !== undefined
17
+
18
+ const BlockCodec = ImportBlock(allowedBlockTypes ?? [])
19
+ const RichTextArrayCodec = withCustomError(
20
+ t.array(BlockCodec),
21
+ () => "Rich text field must be an array",
22
+ )
23
+
24
+ return ImportContent(
11
25
  "StructuredText",
12
26
  EmptyArrayOrElse(
13
- withCustomError(
14
- t.array(t.unknown).pipe(
15
- new t.Type<ImportBlock[], ImportBlock[], unknown[]>(
16
- "RichTextField",
17
- (u): u is ImportBlock[] => t.array(ImportBlock([])).is(u),
18
- (u, c) => {
19
- const single = customType?.config?.single?.split(",")
20
- const multi = customType?.config?.multi?.split(",")
27
+ new t.ArrayType<typeof BlockCodec, ImportBlock[], ImportBlock[]>(
28
+ RichTextArrayCodec.name,
29
+ (u): u is ImportBlock[] => RichTextArrayCodec.is(u),
30
+ (u, c) => {
31
+ if (allowedBlockTypes === undefined) {
32
+ throw new Error(
33
+ "Invalid custom type for the rich text field: the model is missing a 'single' or 'multi' field defining a list of allowed block types",
34
+ )
35
+ }
21
36
 
22
- if (multi) {
23
- return t.array(ImportBlock(multi)).validate(u, c)
24
- } else if (single) {
25
- return u.length > 1
26
- ? t.failure(u, c, "This field only allows one block")
27
- : t.array(ImportBlock(single)).validate(u, c)
28
- } else {
29
- throw new Error(
30
- "Rich text config must have either a 'single' or 'multi' field defined",
31
- )
37
+ return pipe(
38
+ RichTextArrayCodec.validate(u, c),
39
+ E.chain((blocks) => {
40
+ if (isSingleBlockAllowed && blocks.length > 1) {
41
+ return t.failure(blocks, c, "This field only allows one block")
32
42
  }
33
- },
34
- t.identity,
35
- ),
36
- ),
37
- () => "Rich text field must be an array",
43
+ return t.success(blocks)
44
+ }),
45
+ )
46
+ },
47
+ t.identity,
48
+ BlockCodec,
38
49
  ),
39
50
  ),
40
51
  )
52
+ }
53
+
41
54
  export type ImportRichText = t.TypeOf<ReturnType<typeof ImportRichText>>
@@ -6,11 +6,11 @@ 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
- "DefaultOrElse",
9
+ `DefaultOrElse<${inputValidator.name}, ${codec.name}>`,
10
10
  (u: unknown): u is A | null => null === u || codec.is(u),
11
- (u: unknown) => {
12
- if (isRight(inputValidator.decode(u))) return t.success(null)
13
- return codec.decode(u)
11
+ (u: unknown, c) => {
12
+ if (isRight(inputValidator.validate(u, c))) return t.success(null)
13
+ return codec.validate(u, c)
14
14
  },
15
15
  (chunk) => (chunk ? codec.encode(chunk) : undefined),
16
16
  )