@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
53 lines (44 loc) • 1.36 kB
text/typescript
import { either } from "fp-ts"
import { pipe } from "fp-ts/lib/function"
import * as t from "io-ts"
import type { BooleanField } from "../../../customtypes"
import type { LegacyContentCtx, WithTypes } from "../../LegacyContentCtx"
import { hasContentType } from "../../utils"
export const BooleanContentType = "BooleanContent"
export const isBooleanContent = (u: unknown): u is BooleanContent =>
hasContentType(u) && u.__TYPE__ === BooleanContentType
type BooleanLegacy = boolean
export const BooleanLegacy = (ctx: LegacyContentCtx) =>
new t.Type<BooleanContent, WithTypes<BooleanLegacy>, unknown>(
"BooleanLegacy",
isBooleanContent,
(u) => {
return pipe(
t.boolean.decode(u),
either.map((b) =>
BooleanContent.encode({ value: b, __TYPE__: BooleanContentType }),
),
)
},
(b: BooleanContent) => {
return {
content: b.value,
types: { [ctx.keyOfType]: "Boolean" },
keys: {},
}
},
)
export const BooleanContent = t.strict({
__TYPE__: t.literal(BooleanContentType),
value: t.boolean,
})
export type BooleanContent = t.TypeOf<typeof BooleanContent>
export const BooleanContentDefaultValue = (
field: BooleanField,
): BooleanContent | undefined =>
field.config?.default_value !== undefined
? {
__TYPE__: BooleanContentType,
value: field.config.default_value || false,
}
: undefined