From a683942963a66f4dfcf4a0ba3e94b34f05062a04 Mon Sep 17 00:00:00 2001 From: Levi Mykel Gable <9385378+levimykel@users.noreply.github.com> Date: Tue, 29 Oct 2024 19:36:49 +0100 Subject: [PATCH] feat: support repeatable links in helpers --- src/helpers/asLink.ts | 19 +++++++++++-------- src/helpers/isFilled.ts | 7 ++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/helpers/asLink.ts b/src/helpers/asLink.ts index c16975a9..fe9acbed 100644 --- a/src/helpers/asLink.ts +++ b/src/helpers/asLink.ts @@ -140,17 +140,20 @@ export const asLink: { return null as AsLinkReturnType } - // 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 + } + 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 diff --git a/src/helpers/isFilled.ts b/src/helpers/isFilled.ts index e6c454ac..c818c87f 100644 --- a/src/helpers/isFilled.ts +++ b/src/helpers/isFilled.ts @@ -100,7 +100,8 @@ export const image = imageThumbnail as < ) => field is ImageField /** - * 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. * @@ -115,6 +116,10 @@ export const link = < >( field: LinkField | null | undefined, ): field is LinkField => { + if (Array.isArray(field)) { + return isNonNullish(field) && isNonEmptyArray(field) + } + return isNonNullish(field) && ("id" in field || "url" in field) }