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 f6f8566
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 54 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[]
}
}
5 changes: 4 additions & 1 deletion src/types/value/contentRelationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ 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
Expand Down
18 changes: 14 additions & 4 deletions src/types/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export type EmptyLinkField<
Type extends (typeof LinkType)[keyof typeof LinkType] = typeof LinkType.Any,
> = {
link_type: Type | string
text?: string
variant?: string
}

/**
Expand All @@ -39,6 +37,16 @@ export interface FilledLinkToWebField {
variant?: string
}

/**
* Additional optional data for a link field.
*
* @typeParam Link - Link field.
*/
export type WithLinkAdditionalData<Link> = Link & {
text?: string
variant?: string
}

/**
* A link field.
*
Expand All @@ -56,8 +64,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>
4 changes: 2 additions & 2 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,7 +9,7 @@ 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

/**
Expand Down
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 f6f8566

Please sign in to comment.