Not include string/richtext keys if empty
This commit is contained in:
parent
d0896d854a
commit
6d155e73af
|
@ -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":
|
||||
|
|
|
@ -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 } : {}),
|
||||
})) ?? [],
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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 } : {}),
|
||||
})) ?? [],
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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) : [],
|
||||
});
|
||||
|
|
30
src/sdk.ts
30
src/sdk.ts
|
@ -96,36 +96,6 @@ const request = async (url: string, init?: RequestInit): Promise<Response> => {
|
|||
|
||||
// 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;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { RichTextContent, isNodeParagraphNode } from "../constants";
|
||||
import { PayloadImage } from "../sdk";
|
||||
|
||||
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 =>
|
||||
!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 = <T>(list: T[]): boolean => list.length !== new Set(list).size;
|
||||
|
||||
|
|
Loading…
Reference in New Issue