Skip to content

Commit

Permalink
Add variant to link to media value
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-mp committed Dec 24, 2024
1 parent 18e0fcb commit ee58647
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
7 changes: 1 addition & 6 deletions src/lib/isValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ type UnknownValue =
/**
* Checks if a value is a link to media field.
*
* @remarks
* The return value includes `OptionalLinkProperties` because
* `FilledLinkToMediaField` may be a link field, not strictly a content
* relationship field.
*
* @param value - Value to check.
*
* @returns `true` if `value` is a link to media field, `false` otherwise.
Expand All @@ -38,7 +33,7 @@ type UnknownValue =
*/
export const filledLinkToMedia = (
value: UnknownValue,
): value is FilledLinkToMediaField & OptionalLinkProperties => {
): value is FilledLinkToMediaField => {
if (value && typeof value === "object" && !("version" in value)) {
if (
"link_type" in value &&
Expand Down
10 changes: 5 additions & 5 deletions src/types/value/linkToMedia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FieldState } from "./types"

import type { OptionalLinkProperties } from "./link"

/**
* A link field that points to media.
*
Expand All @@ -10,13 +12,12 @@ export type LinkToMediaField<State extends FieldState = FieldState> =

type EmptyLinkToMediaField = {
link_type: "Any"
text?: string
}
} & OptionalLinkProperties

/**
* A link that points to media.
*/
export interface FilledLinkToMediaField {
export type FilledLinkToMediaField = {
id: string
link_type: "Media"
name: string
Expand All @@ -25,5 +26,4 @@ export interface FilledLinkToMediaField {
size: string
height?: string | null
width?: string | null
text?: string
}
} & OptionalLinkProperties
6 changes: 6 additions & 0 deletions test/types/fields-linkToMedia.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ expectType<prismic.LinkToMediaField>({
height: "string",
width: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkToMediaField<"filled">>({
id: "string",
Expand All @@ -42,6 +43,7 @@ expectType<prismic.LinkToMediaField<"filled">>({
height: "string",
width: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkToMediaField<"empty">>({
link_type: prismic.LinkType.Any,
Expand All @@ -54,6 +56,7 @@ expectType<prismic.LinkToMediaField<"empty">>({
height: "string",
width: "string",
text: "string",
variant: "string",
})

/**
Expand All @@ -76,13 +79,16 @@ expectType<prismic.LinkToMediaField<"filled">>({
expectType<prismic.LinkToMediaField>({
link_type: prismic.LinkType.Any,
text: "string",
variant: "string",
})
expectType<prismic.LinkToMediaField<"empty">>({
link_type: prismic.LinkType.Any,
text: "string",
variant: "string",
})
expectType<prismic.LinkToMediaField<"filled">>({
// @ts-expect-error - Filled fields cannot contain an empty value.
link_type: prismic.LinkType.Any,
text: "string",
variant: "string",
})

0 comments on commit ee58647

Please sign in to comment.