Skip to content

Commit

Permalink
fix: fix link types value + model
Browse files Browse the repository at this point in the history
  • Loading branch information
xrutayisire committed Dec 13, 2024
1 parent 8f1581d commit 69bcaba
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 68 deletions.
1 change: 0 additions & 1 deletion src/types/model/contentRelationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ export interface CustomTypeModelContentRelationshipField<
select: typeof CustomTypeModelLinkSelectType.Document
customtypes?: readonly CustomTypeIDs[]
tags?: readonly Tags[]
allowText?: boolean
}
}
1 change: 1 addition & 0 deletions src/types/model/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export interface CustomTypeModelLinkToMediaField {
placeholder?: string
select: typeof CustomTypeModelLinkSelectType.Media
allowText?: boolean
variants?: string[]
}
}
15 changes: 8 additions & 7 deletions src/types/value/contentRelationship.ts
Original file line number Diff line number Diff line change
@@ -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"

/**
Expand All @@ -22,18 +22,21 @@ export type ContentRelationshipField<
State extends FieldState = FieldState,
> = State extends "empty"
? EmptyLinkField<typeof LinkType.Document>
: FilledContentRelationshipField<TypeEnum, LangEnum, DataInterface>
: Omit<
FilledContentRelationshipField<TypeEnum, LangEnum, DataInterface>,
"text" | "variant"
>

/**
* Links that refer to documents
*/
export interface FilledContentRelationshipField<
export type FilledContentRelationshipField<
TypeEnum = string,
LangEnum = string,
DataInterface extends
| Record<string, AnyRegularField | GroupField | SliceZone>
| unknown = unknown,
> {
> = WithLinkAdditionalData<{
link_type: typeof LinkType.Document
id: string
uid?: string
Expand All @@ -44,6 +47,4 @@ export interface FilledContentRelationshipField<
slug?: string
isBroken?: boolean
data?: DataInterface
text?: string
variant?: string
}
}>
24 changes: 16 additions & 8 deletions src/types/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = Link & {
text?: string
variant?: string
}

/**
* For link fields that haven't been filled
*
Expand All @@ -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.
Expand All @@ -56,8 +62,10 @@ export type LinkField<
| unknown = unknown,
State extends FieldState = FieldState,
> = State extends "empty"
? EmptyLinkField<typeof LinkType.Any>
? WithLinkAdditionalData<EmptyLinkField<typeof LinkType.Any>>
:
| ContentRelationshipField<TypeEnum, LangEnum, DataInterface, State>
| WithLinkAdditionalData<
ContentRelationshipField<TypeEnum, LangEnum, DataInterface, State>
>
| FilledLinkToWebField
| LinkToMediaField<State>
10 changes: 4 additions & 6 deletions src/types/value/linkToMedia.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,13 +9,13 @@ import type { EmptyLinkField, LinkType } from "./link"
*/
export type LinkToMediaField<State extends FieldState = FieldState> =
State extends "empty"
? EmptyLinkField<typeof LinkType.Media>
? WithLinkAdditionalData<EmptyLinkField<typeof LinkType.Media>>
: FilledLinkToMediaField

/**
* A link that points to media.
*/
export interface FilledLinkToMediaField {
export type FilledLinkToMediaField = WithLinkAdditionalData<{
id: string
link_type: typeof LinkType.Media
name: string
Expand All @@ -24,6 +24,4 @@ export interface FilledLinkToMediaField {
size: string
height?: string | null
width?: string | null
text?: string
variant?: string
}
}>
12 changes: 6 additions & 6 deletions src/types/value/richText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ export type RTImageNode = {
background: string
}
linkTo?:
| FilledContentRelationshipField
| FilledLinkToWebField
| FilledLinkToMediaField
| Omit<FilledContentRelationshipField, "text" | "variant">
| Omit<FilledLinkToWebField, "text" | "variant">
| Omit<FilledLinkToMediaField, "text" | "variant">
}

/**
Expand All @@ -200,9 +200,9 @@ export type RTEmbedNode = {
export interface RTLinkNode extends RTSpanNodeBase {
type: typeof RichTextNodeType.hyperlink
data:
| FilledContentRelationshipField
| FilledLinkToWebField
| FilledLinkToMediaField
| Omit<FilledContentRelationshipField, "text" | "variant">
| Omit<FilledLinkToWebField, "text" | "variant">
| Omit<FilledLinkToMediaField, "text" | "variant">
}

// Serialization related nodes
Expand Down
12 changes: 0 additions & 12 deletions test/types/customType-contentRelationship.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ expectType<prismic.CustomTypeModelContentRelationshipField<string, "foo">>({
},
})

/**
* Supports optional `allowText` property.
*/
expectType<prismic.CustomTypeModelContentRelationshipField<string, "foo">>({
type: prismic.CustomTypeModelFieldType.Link,
config: {
label: "string",
select: prismic.CustomTypeModelLinkSelectType.Document,
allowText: true,
},
})

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
12 changes: 12 additions & 0 deletions test/types/customType-linkToMedia.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ expectType<prismic.CustomTypeModelLinkToMediaField>({
},
})

/**
* Supports optional `variants` property.
*/
expectType<prismic.CustomTypeModelLinkToMediaField>({
type: prismic.CustomTypeModelFieldType.Link,
config: {
label: "string",
select: prismic.CustomTypeModelLinkSelectType.Media,
variants: ["string"],
},
})

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
28 changes: 0 additions & 28 deletions test/types/fields-contentRelationship.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ expectType<prismic.ContentRelationshipField>({
slug: "string",
isBroken: true,
data: undefined,
text: "string",
variant: "string",
})
expectType<prismic.ContentRelationshipField<string, string, never, "filled">>({
link_type: prismic.LinkType.Document,
Expand All @@ -46,8 +44,6 @@ expectType<prismic.ContentRelationshipField<string, string, never, "filled">>({
slug: "string",
isBroken: true,
data: undefined,
text: "string",
variant: "string",
})
expectType<prismic.ContentRelationshipField<string, string, never, "empty">>({
link_type: prismic.LinkType.Document,
Expand All @@ -61,8 +57,6 @@ expectType<prismic.ContentRelationshipField<string, string, never, "empty">>({
slug: "string",
isBroken: true,
data: undefined,
text: "string",
variant: "string",
})

/**
Expand All @@ -81,28 +75,6 @@ expectType<prismic.ContentRelationshipField<string, string, never, "filled">>(
},
)

/**
* Empty state with text and variant.
*/
expectType<prismic.ContentRelationshipField>({
link_type: prismic.LinkType.Document,
text: "string",
variant: "string",
})
expectType<prismic.ContentRelationshipField<string, string, never, "empty">>({
link_type: prismic.LinkType.Document,
text: "string",
variant: "string",
})
expectType<prismic.ContentRelationshipField<string, string, never, "filled">>(
// @ts-expect-error - Filled fields cannot contain an empty value.
{
link_type: prismic.LinkType.Document,
text: "string",
variant: "string",
},
)

/**
* Supports custom document type.
*/
Expand Down

0 comments on commit 69bcaba

Please sign in to comment.