Skip to content

Commit

Permalink
refactor: clean up after self-review
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Dec 17, 2024
1 parent df81d1d commit 0405e7c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
46 changes: 31 additions & 15 deletions src/lib/isValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { FilledContentRelationshipField } from "../types/value/contentRelat
import type { PrismicDocument } from "../types/value/document"
import type { GroupField } from "../types/value/group"
import type { ImageField } from "../types/value/image"
import type { LinkField } from "../types/value/link"
import { LinkType } from "../types/value/link"
import type { FilledLinkToMediaField } from "../types/value/linkToMedia"
import { type RTImageNode, RichTextNodeType } from "../types/value/richText"
Expand Down Expand Up @@ -34,7 +33,23 @@ type UnknownValue =
export const filledLinkToMedia = (
value: UnknownValue,
): value is FilledLinkToMediaField => {
return filledLink(value) && value.link_type === LinkType.Media
if (value && typeof value === "object" && !("version" in value)) {
if (
"link_type" in value &&
value.link_type === LinkType.Media &&
"id" in value &&
"name" in value &&
"kind" in value &&
"url" in value &&
"size" in value
) {
value

return true
}
}

return false
}

/**
Expand Down Expand Up @@ -120,18 +135,6 @@ export const rtImageNode = (value: UnknownValue): value is RTImageNode => {
return false
}

export const filledLink = (
value: UnknownValue,
): value is LinkField<string, string, unknown, "filled"> => {
return (
typeof value === "object" &&
value !== null &&
"link_type" in value &&
typeof value.link_type === "string" &&
value.link_type !== LinkType.Any
)
}

/**
* Checks if a value is a content relationship field.
*
Expand All @@ -145,7 +148,20 @@ export const filledLink = (
export const filledContentRelationship = (
value: UnknownValue,
): value is FilledContentRelationshipField => {
return filledLink(value) && value.link_type === LinkType.Document
if (value && typeof value === "object" && !("version" in value)) {
if (
"link_type" in value &&
value.link_type === LinkType.Document &&
"id" in value &&
"type" in value &&
"tags" in value &&
"lang" in value
) {
return true
}
}

return false
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/types/value/contentRelationship.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AnyRegularField, FieldState } from "./types"

import type { GroupField } from "./group"
import type { LinkType } from "./link"
import type { SliceZone } from "./sliceZone"

/**
Expand All @@ -25,7 +24,7 @@ export type ContentRelationshipField<
: FilledContentRelationshipField<TypeEnum, LangEnum, DataInterface>

type EmptyContentRelationshipField = {
link_type: typeof LinkType.Any
link_type: "Any"
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/types/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ export type LinkField<
/**
* A link field that is not filled.
*
* @typeParam Type - Unused. An empty link field will always have a `link_type`
* of "Any".
* @typeParam _Unused - THIS PARAMETER IS NOT USED. If you are passing a type,
* **please remove it**.
*/
// This type needs `OptionalLinkProperties` because this type may be used on its own.
export type EmptyLinkField<
_Unused extends
(typeof LinkType)[keyof typeof LinkType] = typeof LinkType.Any,
> = {
link_type: typeof LinkType.Any
link_type: "Any"
} & OptionalLinkProperties

/**
* A link field pointing to a relative or absolute URL.
*/
// This type needs `OptionalLinkProperties` because this type may be used on its own.
export type FilledLinkToWebField = {
link_type: typeof LinkType.Web
link_type: "Web"
url: string
target?: string
} & OptionalLinkProperties
Expand Down
4 changes: 1 addition & 3 deletions src/types/value/linkToMedia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { FieldState } from "./types"

import type { LinkType } from "./link"

/**
* A link field that points to media.
*
Expand All @@ -11,7 +9,7 @@ export type LinkToMediaField<State extends FieldState = FieldState> =
State extends "empty" ? EmptyLinkToMediaField : FilledLinkToMediaField

type EmptyLinkToMediaField = {
link_type: typeof LinkType.Any
link_type: "Any"
text?: string
}

Expand Down

0 comments on commit 0405e7c

Please sign in to comment.