@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
43 lines (34 loc) • 972 B
text/typescript
import { either } from "fp-ts"
import { pipe } from "fp-ts/lib/function"
import * as t from "io-ts"
import type { LegacyContentCtx, WithTypes } from "../LegacyContentCtx"
import { hasContentType } from "../utils"
export const UIDContentType = "UIDContent"
export const isUIDContent = (u: unknown): u is UIDContent =>
hasContentType(u) && u.__TYPE__ === UIDContentType
type UIDLegacy = string
export const UIDLegacy = (ctx: LegacyContentCtx) =>
new t.Type<UIDContent, WithTypes<UIDLegacy>, unknown>(
"UIDLegacy",
isUIDContent,
(u) => {
return pipe(
t.string.decode(u),
either.map((uid) =>
UIDContent.encode({ value: uid, __TYPE__: UIDContentType }),
),
)
},
(uid: UIDContent) => {
return {
content: uid.value,
types: { [ctx.keyOfType]: "UID" },
keys: {},
}
},
)
export const UIDContent = t.type({
__TYPE__: t.literal(UIDContentType),
value: t.string,
})
export type UIDContent = t.TypeOf<typeof UIDContent>