From 69bcabab9babb9e83a0200747a3f818132577af5 Mon Sep 17 00:00:00 2001 From: Xavier Rutayisire Date: Fri, 13 Dec 2024 18:44:09 +0100 Subject: [PATCH] fix: fix link types value + model --- src/types/model/contentRelationship.ts | 1 - src/types/model/linkToMedia.ts | 1 + src/types/value/contentRelationship.ts | 15 +++++----- src/types/value/link.ts | 24 ++++++++++------ src/types/value/linkToMedia.ts | 10 +++---- src/types/value/richText.ts | 12 ++++---- .../customType-contentRelationship.types.ts | 12 -------- test/types/customType-linkToMedia.types.ts | 12 ++++++++ .../types/fields-contentRelationship.types.ts | 28 ------------------- 9 files changed, 47 insertions(+), 68 deletions(-) diff --git a/src/types/model/contentRelationship.ts b/src/types/model/contentRelationship.ts index 4c542748..09f06294 100644 --- a/src/types/model/contentRelationship.ts +++ b/src/types/model/contentRelationship.ts @@ -18,6 +18,5 @@ export interface CustomTypeModelContentRelationshipField< select: typeof CustomTypeModelLinkSelectType.Document customtypes?: readonly CustomTypeIDs[] tags?: readonly Tags[] - allowText?: boolean } } diff --git a/src/types/model/linkToMedia.ts b/src/types/model/linkToMedia.ts index bf4dbddc..385db69c 100644 --- a/src/types/model/linkToMedia.ts +++ b/src/types/model/linkToMedia.ts @@ -14,5 +14,6 @@ export interface CustomTypeModelLinkToMediaField { placeholder?: string select: typeof CustomTypeModelLinkSelectType.Media allowText?: boolean + variants?: string[] } } diff --git a/src/types/value/contentRelationship.ts b/src/types/value/contentRelationship.ts index caa3cdc6..675e8e1d 100644 --- a/src/types/value/contentRelationship.ts +++ b/src/types/value/contentRelationship.ts @@ -1,7 +1,7 @@ import type { AnyRegularField, FieldState } from "./types" import type { GroupField } from "./group" -import type { EmptyLinkField, LinkType } from "./link" +import type { EmptyLinkField, LinkType, WithLinkAdditionalData } from "./link" import type { SliceZone } from "./sliceZone" /** @@ -22,18 +22,21 @@ export type ContentRelationshipField< State extends FieldState = FieldState, > = State extends "empty" ? EmptyLinkField - : FilledContentRelationshipField + : Omit< + FilledContentRelationshipField, + "text" | "variant" + > /** * Links that refer to documents */ -export interface FilledContentRelationshipField< +export type FilledContentRelationshipField< TypeEnum = string, LangEnum = string, DataInterface extends | Record | unknown = unknown, -> { +> = WithLinkAdditionalData<{ link_type: typeof LinkType.Document id: string uid?: string @@ -44,6 +47,4 @@ export interface FilledContentRelationshipField< slug?: string isBroken?: boolean data?: DataInterface - text?: string - variant?: string -} +}> diff --git a/src/types/value/link.ts b/src/types/value/link.ts index a7b6766d..e60fadf9 100644 --- a/src/types/value/link.ts +++ b/src/types/value/link.ts @@ -15,6 +15,16 @@ export const LinkType = { Web: "Web", } as const +/** + * Additional optional data for a link field. + * + * @typeParam Link - Link field. + */ +export type WithLinkAdditionalData = Link & { + text?: string + variant?: string +} + /** * For link fields that haven't been filled * @@ -24,20 +34,16 @@ export type EmptyLinkField< Type extends (typeof LinkType)[keyof typeof LinkType] = typeof LinkType.Any, > = { link_type: Type | string - text?: string - variant?: string } /** * Link that points to external website */ -export interface FilledLinkToWebField { +export type FilledLinkToWebField = WithLinkAdditionalData<{ link_type: typeof LinkType.Web url: string target?: string - text?: string - variant?: string -} +}> /** * A link field. @@ -56,8 +62,10 @@ export type LinkField< | unknown = unknown, State extends FieldState = FieldState, > = State extends "empty" - ? EmptyLinkField + ? WithLinkAdditionalData> : - | ContentRelationshipField + | WithLinkAdditionalData< + ContentRelationshipField + > | FilledLinkToWebField | LinkToMediaField diff --git a/src/types/value/linkToMedia.ts b/src/types/value/linkToMedia.ts index b0d95092..41cc97cf 100644 --- a/src/types/value/linkToMedia.ts +++ b/src/types/value/linkToMedia.ts @@ -1,6 +1,6 @@ import type { FieldState } from "./types" -import type { EmptyLinkField, LinkType } from "./link" +import type { EmptyLinkField, LinkType, WithLinkAdditionalData } from "./link" /** * A link field that points to media. @@ -9,13 +9,13 @@ import type { EmptyLinkField, LinkType } from "./link" */ export type LinkToMediaField = State extends "empty" - ? EmptyLinkField + ? WithLinkAdditionalData> : FilledLinkToMediaField /** * A link that points to media. */ -export interface FilledLinkToMediaField { +export type FilledLinkToMediaField = WithLinkAdditionalData<{ id: string link_type: typeof LinkType.Media name: string @@ -24,6 +24,4 @@ export interface FilledLinkToMediaField { size: string height?: string | null width?: string | null - text?: string - variant?: string -} +}> diff --git a/src/types/value/richText.ts b/src/types/value/richText.ts index 3156c4aa..638c853e 100644 --- a/src/types/value/richText.ts +++ b/src/types/value/richText.ts @@ -177,9 +177,9 @@ export type RTImageNode = { background: string } linkTo?: - | FilledContentRelationshipField - | FilledLinkToWebField - | FilledLinkToMediaField + | Omit + | Omit + | Omit } /** @@ -200,9 +200,9 @@ export type RTEmbedNode = { export interface RTLinkNode extends RTSpanNodeBase { type: typeof RichTextNodeType.hyperlink data: - | FilledContentRelationshipField - | FilledLinkToWebField - | FilledLinkToMediaField + | Omit + | Omit + | Omit } // Serialization related nodes diff --git a/test/types/customType-contentRelationship.types.ts b/test/types/customType-contentRelationship.types.ts index 0648933e..2789c2be 100644 --- a/test/types/customType-contentRelationship.types.ts +++ b/test/types/customType-contentRelationship.types.ts @@ -96,18 +96,6 @@ expectType>({ }, }) -/** - * Supports optional `allowText` property. - */ -expectType>({ - type: prismic.CustomTypeModelFieldType.Link, - config: { - label: "string", - select: prismic.CustomTypeModelLinkSelectType.Document, - allowText: true, - }, -}) - /** * `@prismicio/types` extends `@prismicio/types-internal` */ diff --git a/test/types/customType-linkToMedia.types.ts b/test/types/customType-linkToMedia.types.ts index 88538976..4ac904df 100644 --- a/test/types/customType-linkToMedia.types.ts +++ b/test/types/customType-linkToMedia.types.ts @@ -52,6 +52,18 @@ expectType({ }, }) +/** + * Supports optional `variants` property. + */ +expectType({ + type: prismic.CustomTypeModelFieldType.Link, + config: { + label: "string", + select: prismic.CustomTypeModelLinkSelectType.Media, + variants: ["string"], + }, +}) + /** * `@prismicio/types` extends `@prismicio/types-internal` */ diff --git a/test/types/fields-contentRelationship.types.ts b/test/types/fields-contentRelationship.types.ts index fe532f73..352cf78f 100644 --- a/test/types/fields-contentRelationship.types.ts +++ b/test/types/fields-contentRelationship.types.ts @@ -32,8 +32,6 @@ expectType({ slug: "string", isBroken: true, data: undefined, - text: "string", - variant: "string", }) expectType>({ link_type: prismic.LinkType.Document, @@ -46,8 +44,6 @@ expectType>({ slug: "string", isBroken: true, data: undefined, - text: "string", - variant: "string", }) expectType>({ link_type: prismic.LinkType.Document, @@ -61,8 +57,6 @@ expectType>({ slug: "string", isBroken: true, data: undefined, - text: "string", - variant: "string", }) /** @@ -81,28 +75,6 @@ expectType>( }, ) -/** - * Empty state with text and variant. - */ -expectType({ - link_type: prismic.LinkType.Document, - text: "string", - variant: "string", -}) -expectType>({ - link_type: prismic.LinkType.Document, - text: "string", - variant: "string", -}) -expectType>( - // @ts-expect-error - Filled fields cannot contain an empty value. - { - link_type: prismic.LinkType.Document, - text: "string", - variant: "string", - }, -) - /** * Supports custom document type. */