@prismicio/types-internal
Advanced tools
Comparing version 2.2.0-alpha.12 to 2.2.0-alpha.13
@@ -8,2 +8,3 @@ "use strict"; | ||
const t = (0, tslib_1.__importStar)(require("io-ts")); | ||
const io_ts_types_1 = require("io-ts-types"); | ||
const validators_1 = require("../../../../../validators"); | ||
@@ -13,2 +14,11 @@ const ImportContent_1 = require("../../ImportContent"); | ||
const Validator_1 = require("./Validator"); | ||
const defaultImage = (field) => { | ||
var _a, _b; | ||
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)) || []; | ||
const thumbnailsCodec = (0, io_ts_types_1.withMessage)(t.type(thumbnailsNames.reduce((acc, name) => ({ | ||
...acc, | ||
[name]: validators_1.EmptyObject, | ||
}), {})), () => "The value must be an object with thumbnails as empty objects"); | ||
return validators_1.AnyObject.pipe(thumbnailsCodec); | ||
}; | ||
const ImageField = (field) => new t.Type("ImageField", (u) => Validator_1.ImageFieldValidator.is(u) && | ||
@@ -28,4 +38,5 @@ "thumbnails" in u && | ||
}, t.identity); | ||
const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", (0, validators_1.NullOrElse)(ImageField(field))); | ||
const ImageCodec = (field) => (0, validators_1.DefaultOrElse)(defaultImage(field))(ImageField(field)); | ||
const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", ImageCodec(field)); | ||
exports.ImportImage = ImportImage; | ||
(0, tslib_1.__exportStar)(require("./model"), exports); |
@@ -9,3 +9,3 @@ "use strict"; | ||
const DefaultOrElse = (inputValidator) => { | ||
return (codec) => new t.Type("NullOrT", (u) => null === u || codec.is(u), (u) => { | ||
return (codec) => new t.Type("DefaultOrT", (u) => null === u || codec.is(u), (u) => { | ||
if ((0, Either_1.isRight)(inputValidator.decode(u))) | ||
@@ -12,0 +12,0 @@ return t.success(null); |
{ | ||
"name": "@prismicio/types-internal", | ||
"version": "2.2.0-alpha.12", | ||
"version": "2.2.0-alpha.13", | ||
"description": "Prismic types for Custom Types and Prismic Data", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -5,5 +5,10 @@ import { chain, map } from "fp-ts/Either" | ||
import * as t from "io-ts" | ||
import { withMessage } from "io-ts-types" | ||
import type { Image as ImageDefinition } from "../../../../../customtypes" | ||
import { NullOrElse } from "../../../../../validators" | ||
import { | ||
AnyObject, | ||
DefaultOrElse, | ||
EmptyObject, | ||
} from "../../../../../validators" | ||
import { ImportContent } from "../../ImportContent" | ||
@@ -14,2 +19,20 @@ import { decodeImageField, decodeThumbnails } from "./Decoder" | ||
const defaultImage = (field?: ImageDefinition) => { | ||
const thumbnailsNames = field?.config?.thumbnails?.map((t) => t.name) || [] | ||
const thumbnailsCodec = withMessage( | ||
t.type( | ||
thumbnailsNames.reduce( | ||
(acc, name) => ({ | ||
...acc, | ||
[name]: EmptyObject, | ||
}), | ||
{}, | ||
), | ||
), | ||
() => "The value must be an object with thumbnails as empty objects", | ||
) | ||
return AnyObject.pipe(thumbnailsCodec) | ||
} | ||
const ImageField = (field?: ImageDefinition) => | ||
@@ -44,5 +67,13 @@ new t.Type<ImageFieldWithThumbnails>( | ||
) | ||
const ImageCodec = (field?: ImageDefinition) => | ||
DefaultOrElse< | ||
Record<string, unknown>, | ||
ImageFieldWithThumbnails, | ||
ImageFieldWithThumbnails | ||
>(defaultImage(field))(ImageField(field)) | ||
export const ImportImage = (field?: ImageDefinition) => | ||
ImportContent("Image", NullOrElse(ImageField(field))) | ||
ImportContent("Image", ImageCodec(field)) | ||
export type ImportImage = t.TypeOf<ReturnType<typeof ImportImage>> | ||
export * from "./model" |
@@ -9,3 +9,3 @@ import { isRight } from "fp-ts/lib/Either" | ||
new t.Type<A | null, O | undefined>( | ||
"NullOrT", | ||
"DefaultOrT", | ||
(u: unknown): u is A | null => null === u || codec.is(u), | ||
@@ -12,0 +12,0 @@ (u: unknown) => { |
2524689
499
60673