Skip to content

Commit

Permalink
feat: support repeatable links in helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
levimykel committed Oct 29, 2024
1 parent c1e25ec commit a683942
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/helpers/asLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,20 @@ export const asLink: {
return null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>
}

// Converts document to link field if needed
const linkField =
// prettier-ignore
(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501
// TODO: Remove the `prettier-ignore` comment when this bug is fixed.
// If the link field is repeatable, then it returns the first link in
// the array. If not, then it converts document to link field if needed
let linkField: LinkField
if (Array.isArray(linkFieldOrDocument)) {
if (!linkFieldOrDocument[0]) {
return null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>
}
linkField = linkFieldOrDocument[0]
} else {
linkField =
"link_type" in linkFieldOrDocument
? linkFieldOrDocument
: documentToLinkField(linkFieldOrDocument)
) as LinkField
}

// TODO: Remove when we remove support for deprecated tuple-style configuration.
const [configObjectOrLinkResolver] = configObjectOrTuple
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/isFilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export const image = imageThumbnail as <
) => field is ImageField<ThumbnailNames, "filled">

/**
* Determines if a link field is filled.
* Determines if a link field is filled. If the field is repeatable, it checks
* if the link array contains at least one item
*
* @param field - Link field to check.
*
Expand All @@ -115,6 +116,10 @@ export const link = <
>(
field: LinkField<TypeEnum, LangEnum, DataInterface> | null | undefined,
): field is LinkField<TypeEnum, LangEnum, DataInterface, "filled"> => {
if (Array.isArray(field)) {
return isNonNullish(field) && isNonEmptyArray(field)
}

return isNonNullish(field) && ("id" in field || "url" in field)
}

Expand Down

0 comments on commit a683942

Please sign in to comment.