@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
49 lines (40 loc) • 1.21 kB
text/typescript
import { either } from "fp-ts"
import { pipe } from "fp-ts/function"
import * as t from "io-ts"
import type { LegacyContentCtx, WithTypes } from "../../LegacyContentCtx"
import { hasContentType } from "../../utils"
export const GeoPointContentType = "GeoPointContent"
export const isGeoPointContent = (u: unknown): u is GeoPointContent =>
hasContentType(u) && u.__TYPE__ === GeoPointContentType
const legacyReader = t.strict({
position: t.strict({
lat: t.number,
lng: t.number,
}),
})
type GeoPointLegacy = t.TypeOf<typeof legacyReader>
export const GeoPointLegacy = (ctx: LegacyContentCtx) =>
new t.Type<GeoPointContent, WithTypes<GeoPointLegacy>, unknown>(
"GeoPointLegacy",
isGeoPointContent,
(u) => {
return pipe(
legacyReader.decode(u),
either.map((geo) =>
GeoPointContent.encode({ ...geo, __TYPE__: GeoPointContentType }),
),
)
},
(geo: GeoPointContent) => ({
content: { position: geo.position },
types: { [ctx.keyOfType]: "GeoPoint" },
keys: {},
}),
)
export const GeoPointContent = t.intersection([
legacyReader,
t.strict({
__TYPE__: t.literal(GeoPointContentType),
}),
])
export type GeoPointContent = t.TypeOf<typeof GeoPointContent>