Not include string/richtext keys if empty

This commit is contained in:
DrMint 2024-04-04 15:07:36 +02:00
parent d0896d854a
commit 6d155e73af
6 changed files with 29 additions and 50 deletions

View File

@ -80,8 +80,8 @@ export const eventToEndpointEvent = ({
language: isPayloadType(language) ? language.id : language, language: isPayloadType(language) ? language.id : language,
sourceLanguage: isPayloadType(sourceLanguage) ? sourceLanguage.id : sourceLanguage, sourceLanguage: isPayloadType(sourceLanguage) ? sourceLanguage.id : sourceLanguage,
...(isNotEmpty(title) ? { title } : {}), ...(isNotEmpty(title) ? { title } : {}),
...(isDefined(description) ? { description } : {}), ...(isNotEmpty(description) ? { description } : {}),
...(isDefined(notes) ? { notes } : {}), ...(isNotEmpty(notes) ? { notes } : {}),
proofreaders: isPayloadArrayType(proofreaders) ? proofreaders.map(handleRecorder) : [], proofreaders: isPayloadArrayType(proofreaders) ? proofreaders.map(handleRecorder) : [],
transcribers: isPayloadArrayType(transcribers) ? transcribers.map(handleRecorder) : [], transcribers: isPayloadArrayType(transcribers) ? transcribers.map(handleRecorder) : [],
translators: isPayloadArrayType(translators) ? translators.map(handleRecorder) : [], translators: isPayloadArrayType(translators) ? translators.map(handleRecorder) : [],
@ -101,7 +101,7 @@ const handleSources = (sources: ChronologyEvent["events"][number]["sources"]): E
return { return {
type: "collectible", type: "collectible",
collectible: convertCollectibleToPreview(source.collectible), collectible: convertCollectibleToPreview(source.collectible),
...(range ? { range } : {}), ...(isDefined(range) ? { range } : {}),
}; };
case "pageBlock": case "pageBlock":

View File

@ -4,6 +4,7 @@ import { EndpointCollectible, EndpointCollectiblePreview, PayloadImage } from ".
import { Collectible } from "../../../types/collections"; import { Collectible } from "../../../types/collections";
import { import {
isDefined, isDefined,
isNotEmpty,
isPayloadArrayType, isPayloadArrayType,
isPayloadType, isPayloadType,
isPublished, isPublished,
@ -48,7 +49,7 @@ export const getBySlugEndpoint = createGetByEndpoint({
? subitems.filter(isPublished).map(convertCollectibleToPreview) ? subitems.filter(isPublished).map(convertCollectibleToPreview)
: [], : [],
urls: urls?.map(({ url }) => ({ url, label: getDomainFromUrl(url) })) ?? [], urls: urls?.map(({ url }) => ({ url, label: getDomainFromUrl(url) })) ?? [],
...(weightEnabled && weight ? { weight: weight.amount } : {}), ...(weightEnabled && isDefined(weight) ? { weight: weight.amount } : {}),
...handleSize(size, sizeEnabled), ...handleSize(size, sizeEnabled),
...handlePageInfo(pageInfo, pageInfoEnabled), ...handlePageInfo(pageInfo, pageInfoEnabled),
...handlePrice(price, priceEnabled), ...handlePrice(price, priceEnabled),
@ -215,16 +216,16 @@ export const convertCollectibleToPreview = ({
slug, slug,
languages: languages:
languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [],
...(releaseDate ? { releaseDate } : {}), ...(isDefined(releaseDate) ? { releaseDate } : {}),
...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}), ...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}),
tagGroups: convertTagsToGroups(tags), tagGroups: convertTagsToGroups(tags),
translations: translations:
translations?.map(({ language, title, description, pretitle, subtitle }) => ({ translations?.map(({ language, title, description, pretitle, subtitle }) => ({
language: isPayloadType(language) ? language.id : language, language: isPayloadType(language) ? language.id : language,
title, title,
...(pretitle ? { pretitle } : {}), ...(isNotEmpty(pretitle) ? { pretitle } : {}),
...(subtitle ? { subtitle } : {}), ...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(description ? { description } : {}), ...(isNotEmpty(description) ? { description } : {}),
})) ?? [], })) ?? [],
}; };
}; };

View File

@ -2,7 +2,7 @@ import { Collections } from "../../../constants";
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
import { EndpointFolder, EndpointFolderPreview } from "../../../sdk"; import { EndpointFolder, EndpointFolderPreview } from "../../../sdk";
import { Folder, Language } from "../../../types/collections"; 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 { handleParentPages } from "../../../utils/endpoints";
import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint"; import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint";
import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint"; import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint";
@ -63,7 +63,7 @@ export const convertFolderToPreview = ({
translations?.map(({ language, name, description }) => ({ translations?.map(({ language, name, description }) => ({
language: getLanguageId(language), language: getLanguageId(language),
name, name,
...(description ? { description } : {}), ...(isNotEmpty(description) ? { description } : {}),
})) ?? [], })) ?? [],
}; };
}; };

View File

@ -12,7 +12,7 @@ import {
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
import { EndpointPage, EndpointPagePreview, TableOfContentEntry } from "../../../sdk"; import { EndpointPage, EndpointPagePreview, TableOfContentEntry } from "../../../sdk";
import { Page } from "../../../types/collections"; 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"; import { convertTagsToGroups, handleParentPages, handleRecorder } from "../../../utils/endpoints";
export const getBySlugEndpoint = createGetByEndpoint({ export const getBySlugEndpoint = createGetByEndpoint({
@ -39,10 +39,10 @@ export const getBySlugEndpoint = createGetByEndpoint({
}) => ({ }) => ({
language: isPayloadType(language) ? language.id : language, language: isPayloadType(language) ? language.id : language,
sourceLanguage: isPayloadType(sourceLanguage) ? sourceLanguage.id : sourceLanguage, sourceLanguage: isPayloadType(sourceLanguage) ? sourceLanguage.id : sourceLanguage,
...(pretitle ? { pretitle } : {}), ...(isNotEmpty(pretitle) ? { pretitle } : {}),
title, title,
...(subtitle ? { subtitle } : {}), ...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(summary ? { summary } : {}), ...(isNotEmpty(summary) ? { summary } : {}),
content: handleContent(content), content: handleContent(content),
toc: handleToc(content), toc: handleToc(content),
translators: isPayloadArrayType(translators) ? translators.map(handleRecorder) : [], translators: isPayloadArrayType(translators) ? translators.map(handleRecorder) : [],
@ -160,9 +160,9 @@ export const convertPageToPreview = ({
tagGroups: convertTagsToGroups(tags), tagGroups: convertTagsToGroups(tags),
translations: translations.map(({ language, title, pretitle, subtitle }) => ({ translations: translations.map(({ language, title, pretitle, subtitle }) => ({
language: isPayloadType(language) ? language.id : language, language: isPayloadType(language) ? language.id : language,
...(pretitle ? { pretitle } : {}), ...(isNotEmpty(pretitle) ? { pretitle } : {}),
title, title,
...(subtitle ? { subtitle } : {}), ...(isNotEmpty(subtitle) ? { subtitle } : {}),
})), })),
authors: isPayloadArrayType(authors) ? authors.map(handleRecorder) : [], authors: isPayloadArrayType(authors) ? authors.map(handleRecorder) : [],
}); });

View File

@ -96,36 +96,6 @@ const request = async (url: string, init?: RequestInit): Promise<Response> => {
// SDK and Types // 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 = { export type EndpointFolderPreview = {
slug: string; slug: string;
icon?: string; icon?: string;

View File

@ -1,3 +1,4 @@
import { RichTextContent, isNodeParagraphNode } from "../constants";
import { PayloadImage } from "../sdk"; import { PayloadImage } from "../sdk";
export const isDefined = <T>(value: T | null | undefined): value is T => export const isDefined = <T>(value: T | null | undefined): value is T =>
@ -6,11 +7,18 @@ export const isDefined = <T>(value: T | null | undefined): value is T =>
export const isUndefined = <T>(value: T | null | undefined): value is null | undefined => export const isUndefined = <T>(value: T | null | undefined): value is null | undefined =>
!isDefined(value); !isDefined(value);
export const isNotEmpty = (value: string | null | undefined): value is string => export const isNotEmpty = (value: string | null | undefined | RichTextContent): value is string =>
isDefined(value) && value.trim().length > 0; !isEmpty(value);
export const isEmpty = (value: string | null | undefined): value is string => export const isEmpty = (value: string | null | undefined | RichTextContent): value is string =>
isUndefined(value) || value.trim().length === 0; 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 = <T>(list: T[]): boolean => list.length !== new Set(list).size; export const hasDuplicates = <T>(list: T[]): boolean => list.length !== new Set(list).size;