From 6d155e73af539f800af0b61d8358f7f9b613dd39 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:07:36 +0200 Subject: [PATCH] Not include string/richtext keys if empty --- .../endpoints/getAllEndpoint.ts | 6 ++-- .../endpoints/getBySlugEndpoint.ts | 11 +++---- .../Folders/endpoints/getBySlugEndpoint.ts | 4 +-- .../Pages/endpoints/getBySlugEndpoint.ts | 12 ++++---- src/sdk.ts | 30 ------------------- src/utils/asserts.ts | 16 +++++++--- 6 files changed, 29 insertions(+), 50 deletions(-) diff --git a/src/collections/ChronologyEvents/endpoints/getAllEndpoint.ts b/src/collections/ChronologyEvents/endpoints/getAllEndpoint.ts index b4b0924..11ed636 100644 --- a/src/collections/ChronologyEvents/endpoints/getAllEndpoint.ts +++ b/src/collections/ChronologyEvents/endpoints/getAllEndpoint.ts @@ -80,8 +80,8 @@ export const eventToEndpointEvent = ({ language: isPayloadType(language) ? language.id : language, sourceLanguage: isPayloadType(sourceLanguage) ? sourceLanguage.id : sourceLanguage, ...(isNotEmpty(title) ? { title } : {}), - ...(isDefined(description) ? { description } : {}), - ...(isDefined(notes) ? { notes } : {}), + ...(isNotEmpty(description) ? { description } : {}), + ...(isNotEmpty(notes) ? { notes } : {}), proofreaders: isPayloadArrayType(proofreaders) ? proofreaders.map(handleRecorder) : [], transcribers: isPayloadArrayType(transcribers) ? transcribers.map(handleRecorder) : [], translators: isPayloadArrayType(translators) ? translators.map(handleRecorder) : [], @@ -101,7 +101,7 @@ const handleSources = (sources: ChronologyEvent["events"][number]["sources"]): E return { type: "collectible", collectible: convertCollectibleToPreview(source.collectible), - ...(range ? { range } : {}), + ...(isDefined(range) ? { range } : {}), }; case "pageBlock": diff --git a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts index 35dc3bc..9779aba 100644 --- a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts @@ -4,6 +4,7 @@ import { EndpointCollectible, EndpointCollectiblePreview, PayloadImage } from ". import { Collectible } from "../../../types/collections"; import { isDefined, + isNotEmpty, isPayloadArrayType, isPayloadType, isPublished, @@ -48,7 +49,7 @@ export const getBySlugEndpoint = createGetByEndpoint({ ? subitems.filter(isPublished).map(convertCollectibleToPreview) : [], urls: urls?.map(({ url }) => ({ url, label: getDomainFromUrl(url) })) ?? [], - ...(weightEnabled && weight ? { weight: weight.amount } : {}), + ...(weightEnabled && isDefined(weight) ? { weight: weight.amount } : {}), ...handleSize(size, sizeEnabled), ...handlePageInfo(pageInfo, pageInfoEnabled), ...handlePrice(price, priceEnabled), @@ -215,16 +216,16 @@ export const convertCollectibleToPreview = ({ slug, languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], - ...(releaseDate ? { releaseDate } : {}), + ...(isDefined(releaseDate) ? { releaseDate } : {}), ...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}), tagGroups: convertTagsToGroups(tags), translations: translations?.map(({ language, title, description, pretitle, subtitle }) => ({ language: isPayloadType(language) ? language.id : language, title, - ...(pretitle ? { pretitle } : {}), - ...(subtitle ? { subtitle } : {}), - ...(description ? { description } : {}), + ...(isNotEmpty(pretitle) ? { pretitle } : {}), + ...(isNotEmpty(subtitle) ? { subtitle } : {}), + ...(isNotEmpty(description) ? { description } : {}), })) ?? [], }; }; diff --git a/src/collections/Folders/endpoints/getBySlugEndpoint.ts b/src/collections/Folders/endpoints/getBySlugEndpoint.ts index 74e6301..4935170 100644 --- a/src/collections/Folders/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Folders/endpoints/getBySlugEndpoint.ts @@ -2,7 +2,7 @@ import { Collections } from "../../../constants"; import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { EndpointFolder, EndpointFolderPreview } from "../../../sdk"; import { Folder, Language } from "../../../types/collections"; -import { isDefined, isPayloadType, isPublished } from "../../../utils/asserts"; +import { isDefined, isNotEmpty, isPayloadType, isPublished } from "../../../utils/asserts"; import { handleParentPages } from "../../../utils/endpoints"; import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint"; import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint"; @@ -63,7 +63,7 @@ export const convertFolderToPreview = ({ translations?.map(({ language, name, description }) => ({ language: getLanguageId(language), name, - ...(description ? { description } : {}), + ...(isNotEmpty(description) ? { description } : {}), })) ?? [], }; }; diff --git a/src/collections/Pages/endpoints/getBySlugEndpoint.ts b/src/collections/Pages/endpoints/getBySlugEndpoint.ts index 420349f..1da7472 100644 --- a/src/collections/Pages/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Pages/endpoints/getBySlugEndpoint.ts @@ -12,7 +12,7 @@ import { import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { EndpointPage, EndpointPagePreview, TableOfContentEntry } from "../../../sdk"; import { Page } from "../../../types/collections"; -import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; +import { isNotEmpty, isPayloadArrayType, isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; import { convertTagsToGroups, handleParentPages, handleRecorder } from "../../../utils/endpoints"; export const getBySlugEndpoint = createGetByEndpoint({ @@ -39,10 +39,10 @@ export const getBySlugEndpoint = createGetByEndpoint({ }) => ({ language: isPayloadType(language) ? language.id : language, sourceLanguage: isPayloadType(sourceLanguage) ? sourceLanguage.id : sourceLanguage, - ...(pretitle ? { pretitle } : {}), + ...(isNotEmpty(pretitle) ? { pretitle } : {}), title, - ...(subtitle ? { subtitle } : {}), - ...(summary ? { summary } : {}), + ...(isNotEmpty(subtitle) ? { subtitle } : {}), + ...(isNotEmpty(summary) ? { summary } : {}), content: handleContent(content), toc: handleToc(content), translators: isPayloadArrayType(translators) ? translators.map(handleRecorder) : [], @@ -160,9 +160,9 @@ export const convertPageToPreview = ({ tagGroups: convertTagsToGroups(tags), translations: translations.map(({ language, title, pretitle, subtitle }) => ({ language: isPayloadType(language) ? language.id : language, - ...(pretitle ? { pretitle } : {}), + ...(isNotEmpty(pretitle) ? { pretitle } : {}), title, - ...(subtitle ? { subtitle } : {}), + ...(isNotEmpty(subtitle) ? { subtitle } : {}), })), authors: isPayloadArrayType(authors) ? authors.map(handleRecorder) : [], }); diff --git a/src/sdk.ts b/src/sdk.ts index 679ec5d..65ec94c 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -96,36 +96,6 @@ const request = async (url: string, init?: RequestInit): Promise => { // SDK and Types -export type EndpointEra = { - slug: string; - startingYear: number; - endingYear: number; - translations: { - language: string; - title: string; - description?: RichTextContent; - }[]; - items: { - date: { - year: number; - month?: number; - day?: number; - }; - events: { - translations: { - language: string; - sourceLanguage: string; - title?: string; - description?: RichTextContent; - notes?: RichTextContent; - transcribers: EndpointRecorder[]; - translators: EndpointRecorder[]; - proofreaders: EndpointRecorder[]; - }[]; - }[]; - }[]; -}; - export type EndpointFolderPreview = { slug: string; icon?: string; diff --git a/src/utils/asserts.ts b/src/utils/asserts.ts index 779b97d..57953fa 100644 --- a/src/utils/asserts.ts +++ b/src/utils/asserts.ts @@ -1,3 +1,4 @@ +import { RichTextContent, isNodeParagraphNode } from "../constants"; import { PayloadImage } from "../sdk"; export const isDefined = (value: T | null | undefined): value is T => @@ -6,11 +7,18 @@ export const isDefined = (value: T | null | undefined): value is T => export const isUndefined = (value: T | null | undefined): value is null | undefined => !isDefined(value); -export const isNotEmpty = (value: string | null | undefined): value is string => - isDefined(value) && value.trim().length > 0; +export const isNotEmpty = (value: string | null | undefined | RichTextContent): value is string => + !isEmpty(value); -export const isEmpty = (value: string | null | undefined): value is string => - isUndefined(value) || value.trim().length === 0; +export const isEmpty = (value: string | null | undefined | RichTextContent): value is string => + isUndefined(value) || + (typeof value === "string" && isEmptyString(value)) || + (typeof value === "object" && isEmptyRichText(value)); + +const isEmptyString = (value: string) => value.trim().length === 0; +const isEmptyRichText = (value: RichTextContent) => + value.root.children.length === 0 || + value.root.children.every((node) => isNodeParagraphNode(node) && node.children.length === 0); export const hasDuplicates = (list: T[]): boolean => list.length !== new Set(list).size;