UNPKG

@prismicio/types-internal

Version:
98 lines (90 loc) 1.99 kB
import type { RepeatableContent, SharedSliceItemContent, SliceItemContent, WidgetContent, } from "../content" import type { CompositeSlice, Group, NestableWidget, NestedGroup, StaticSlices, UID, VariationFields, } from "../customtypes" export type TraverseSliceContentFn = < S extends SliceItemContent | SharedSliceItemContent, D extends VariationFields | CompositeSlice | Group | NestableWidget, >({ path, key, apiId, model, content, }: { path: ContentPath key: string apiId: string model?: D | undefined content: S }) => S | SharedSliceItemContent | undefined export type TraverseWidgetContentFn = < C extends WidgetContent | RepeatableContent, D extends NestableWidget | StaticSlices | Group | UID, >({ path, key, apiId, model, content, }: { path: ContentPath key: string apiId: string model?: D | undefined content: C }) => C | undefined export type ContentPathEntry = { type: | "CustomType" | "Widget" | "SharedSlice" | "Slice" | "LegacySlice" | "GroupItem" | "primary" | "items" | "RepeatableItem" key: string } export type ContentPath = ReadonlyArray<ContentPathEntry> const PATH_SEPARATOR = "::" export const ContentPath = { serialize(path: ContentPath): string { return path.map((entry) => entry.key).join(PATH_SEPARATOR) }, current(path: ContentPath): ContentPathEntry | undefined { return path[path.length - 1] }, append(path: string, strElement: string): string { return path + PATH_SEPARATOR + strElement }, make( entries: Array<{ key?: ContentPathEntry["key"] | undefined type: ContentPathEntry["type"] }>, ): ContentPath { return entries.reduce<ContentPath>((acc, { key, type }) => { return key ? acc.concat({ key, type }) : acc }, []) }, } export type SliceModel = | VariationFields | NestableWidget | CompositeSlice | Group export type OnFieldFn<T extends UID | NestableWidget | Group | NestedGroup> = (args: { path: ReadonlyArray<string>; key: string; field: T }) => T