/** * "Functions" (utility types) to infer the expected TypeScript types for content types and content type properties * * Inferred types are the types returned by the Graph */ import { AnyProperty, ArrayProperty, BinaryProperty, BooleanProperty, ComponentProperty, ContentProperty, ContentReferenceProperty, DateTimeProperty, FloatProperty, IntegerProperty, JsonProperty, JsonValue, LinkProperty, RichTextProperty, StringProperty, UrlProperty } from './model/properties.js'; import { AnyContentType, ExperienceContentType, MediaContentType, SectionContentType } from './model/contentTypes.js'; import { Node } from './components/richText/renderer.js'; import { PublicImageAsset, PublicRawFileAsset, PublicVideoAsset } from './model/assets.js'; import { DisplayTemplate } from './model/displayTemplates.js'; import type { TaxonomyTerm } from './graph/options.js'; /** Forces Intellisense to resolve types */ export type Prettify = { [K in keyof T]: T[K]; } & {}; export type InferredUrl = { type: string | null; default: string | null; hierarchical: string | null; internal: string | null; graph: string | null; base: string | null; }; type InferredItemMetadata = { changeset: string | null; displayOption: string | null; categories: string[] | undefined; resolvedCategories: TaxonomyTerm[] | undefined; }; type InferredInstanceMetadata = { changeset: string | null; locales: string[]; expired: string | null; container: string | null; owner: string | null; routeSegment: string | null; lastModifiedBy: string | null; path: string[]; createdBy: string | null; }; type InferredContentMetadata = { key: string; locale: string; fallbackForLocale: string; version: string; displayName: string; url: InferredUrl; types: string[]; published: string; status: string; created: string; lastModified: string; sortOrder: number; variation: string; } & Partial & Partial; /** Selected `richTextFormat` representation(s), defaulting to `'json'`. */ type InferredRichText = { html?: string; json?: { type: 'richText'; children: Node[]; }; }; /** Asset types that can be returned in ContentReference */ export type ContentReferenceItem = PublicImageAsset | PublicVideoAsset | PublicRawFileAsset; /** CMS media returned in `item`, with its own properties left untyped. */ export type InferredReferencedMedia = InferredBase & { assetMetadata?: InferredAssetMetadata; imageMetadata?: InferredImageMetadata; }; export type InferredContentReference = { url: InferredUrl; /** * The referenced asset: a DAM asset, or the CMS media itself. Absent for a * reference that allows no media, which the query does not resolve. */ item?: ((ContentReferenceItem | InferredReferencedMedia) & Record) | null; key: string; }; /** Infers the Typescript type for each content type property */ export type InferFromProperty = T extends BooleanProperty ? boolean : T extends BinaryProperty ? unknown : T extends StringProperty ? string : T extends DateTimeProperty ? string : T extends JsonProperty ? unknown extends TSchema ? JsonValue : TSchema : T extends RichTextProperty ? InferredRichText : T extends UrlProperty ? InferredUrl : T extends LinkProperty ? { text: string | null; title: string | null; target: string | null; url: InferredUrl; } : T extends IntegerProperty ? number : T extends FloatProperty ? number : T extends ContentReferenceProperty ? InferredContentReference : T extends ArrayProperty ? InferFromProperty[] : T extends ContentProperty ? { __typename: string; __viewname: string; } : T extends ComponentProperty ? ContentProps : unknown; export type InferredAssetMetadata = { fileSize: number | null; mimeType: string | null; url: string | null; }; export type InferredImageMetadata = { width: number | null; height: number | null; }; /** Added by `getPreviewContent` to every node of a preview response */ export type PreviewContext = { edit: boolean; preview_token: string; /** The token media URLs carry. Set only with `GraphGetPreviewOptions.forceMediaToken`. */ media_token?: string; }; /** Attributes included in the response from Graph in every content type */ export type InferredBase = { _id: string; _metadata: InferredContentMetadata; __typename: string; __context?: PreviewContext; __composition?: ExperienceComponentNode; }; /** Only include keys where indexingType is not 'disabled' */ type EnabledKeys> = { [K in keyof T]: T[K]['indexingType'] extends 'disabled' ? never : K; }[keyof T]; /** Infers an `object` with the TS type inferred for each type */ type InferProps = T extends ({ properties: Record; }) ? { [Key in EnabledKeys]: InferFromProperty | null; } : {}; export type ExperienceNode = ExperienceComponentNode | ExperienceStructureNode; export type DisplaySettingsType = { key: string; value: string; }; export type ExperienceCompositionNode = { /** Internal node type. Can be `CompositionStructureNode` or `CompositionComponentNode` */ __typename: string; __context?: PreviewContext; /** Name of the content type if provided, `null` otherwise */ type: string | null; key: string; layoutType: string | null; displayName: string; displayTemplateKey: string | null; displaySettings: DisplaySettingsType[] | null; }; export type ExperienceStructureNode = ExperienceCompositionNode & { /** "row", "column", etc. */ nodeType: string; nodes?: ExperienceNode[]; /** User-defined properties of a section. `null` for rows, columns, etc. */ component?: Record | null; }; export type ExperienceComponentNode = ExperienceCompositionNode & { nodeType: 'component'; component: { __typename: string; }; }; /** Adds TS fields specific to `Experience` */ type InferExperience = T extends ExperienceContentType ? { composition: ExperienceStructureNode; } : {}; /** Adds TS fields specific to `Section` */ type InferSection = T extends SectionContentType ? { key: string; nodes: ExperienceNode[]; __typename: string; __context?: PreviewContext; } : {}; /** Adds `_assetMetadata` for media base types (`_image`, `_media`, `_video`) */ type InferAssetMetadata = T extends MediaContentType ? { _assetMetadata: InferredAssetMetadata; } : {}; /** Adds `_imageMetadata` for image base types (`_image`) */ type InferImageMetadata = T extends { baseType: '_image'; } ? { _imageMetadata: InferredImageMetadata; } : {}; /** Infers the TypeScript type for a content type */ type InferFromContentType = Prettify & InferExperience & InferSection & InferAssetMetadata & InferImageMetadata>; /** Infers the TypeScript type for a display setting */ type InferFromDisplayTemplate = T extends ({ settings: infer S; }) ? { [K in keyof S]: S[K] extends ({ choices: Record; editor: infer E; }) ? E extends 'select' ? keyof S[K]['choices'] : E extends 'checkbox' ? boolean : never : never; } : {}; /** Infers the Graph response types of `T`. `T` can be a content type or a property */ export type ContentProps = T extends DisplayTemplate ? InferFromDisplayTemplate : T extends AnyContentType ? InferFromContentType : T extends AnyProperty ? InferFromProperty : unknown; export type { JsonValue, JsonObject, JsonArray, JsonPrimitive } from './model/properties.js';