@prismicio/types-internal 2.2.0-alpha.12 → 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.
@@ -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.12",
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)