import type { StaticWidget } from "@prismicio/types-internal/lib/customtypes" import { either } from "fp-ts" import { pipe } from "fp-ts/lib/function" import * as t from "io-ts" import { WidgetDef } from "./fields/WidgetDef" const codec = t.strict({ id: t.string, definitions: t.array(WidgetDef), }) export type CustomTypeDef = { customTypeId: string fields: Record } export const CustomTypeDef = new t.Type( "CustomTypeDef", (u: unknown): u is CustomTypeDef => typeof u === "object" && u !== null && "customTypeId" in u, (u: unknown) => { return pipe( codec.decode(u), either.map(({ id, definitions }): CustomTypeDef => { return { customTypeId: id, fields: definitions.reduce((acc, { key, def }) => { return { ...acc, [key]: def } }, {}), } }), ) }, (c) => c, )