From 64bc76febb7b5509ed8cec427ed61bd242d39072 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:58:59 +0100 Subject: [PATCH] Handle draft/published status in endpoints --- .../endpoints/getBySlugEndpoint.ts | 23 +++++++------- .../Folders/endpoints/getBySlugEndpoint.ts | 17 ++++++----- .../Pages/endpoints/getBySlugEndpoint.ts | 14 ++++----- src/endpoints/createGetByEndpoint.ts | 30 ++++++++++++++----- src/sdk.ts | 2 -- src/utils/asserts.ts | 4 +++ src/utils/endpoints.ts | 4 +-- 7 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts index d149537..35dc3bc 100644 --- a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts @@ -1,4 +1,4 @@ -import { CollectibleNature, CollectionStatus, Collections } from "../../../constants"; +import { CollectibleNature, Collections } from "../../../constants"; import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { EndpointCollectible, EndpointCollectiblePreview, PayloadImage } from "../../../sdk"; import { Collectible } from "../../../types/collections"; @@ -6,15 +6,17 @@ import { isDefined, isPayloadArrayType, isPayloadType, + isPublished, isValidPayloadImage, } from "../../../utils/asserts"; import { convertTagsToGroups, getDomainFromUrl, handleParentPages } from "../../../utils/endpoints"; import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint"; -export const getBySlugEndpoint = createGetByEndpoint( - Collections.Collectibles, - "slug", - (collectible: Collectible): EndpointCollectible => { +export const getBySlugEndpoint = createGetByEndpoint({ + collection: Collections.Collectibles, + attribute: "slug", + depth: 3, + handler: (collectible: Collectible): EndpointCollectible => { const { nature, urls, @@ -42,7 +44,9 @@ export const getBySlugEndpoint = createGetByEndpoint( scans: handleScans(collectible.scans), nature: nature === "Physical" ? CollectibleNature.Physical : CollectibleNature.Digital, parentPages: handleParentPages({ collectibles: parentItems, folders }), - subitems: isPayloadArrayType(subitems) ? subitems.map(convertCollectibleToPreview) : [], + subitems: isPayloadArrayType(subitems) + ? subitems.filter(isPublished).map(convertCollectibleToPreview) + : [], urls: urls?.map(({ url }) => ({ url, label: getDomainFromUrl(url) })) ?? [], ...(weightEnabled && weight ? { weight: weight.amount } : {}), ...handleSize(size, sizeEnabled), @@ -50,8 +54,7 @@ export const getBySlugEndpoint = createGetByEndpoint( ...handlePrice(price, priceEnabled), }; }, - 3 -); +}); const handlePrice = ( price: Collectible["price"], @@ -183,7 +186,7 @@ const handleContents = (contents: Collectible["contents"]): EndpointCollectible[ : undefined; case "pages": - return isPayloadType(content.value) + return isPayloadType(content.value) && isPublished(content.value) ? { relationTo: "pages", value: convertPageToPreview(content.value) } : undefined; @@ -202,7 +205,6 @@ const handleContents = (contents: Collectible["contents"]): EndpointCollectible[ export const convertCollectibleToPreview = ({ slug, - _status, thumbnail, translations, releaseDate, @@ -213,7 +215,6 @@ export const convertCollectibleToPreview = ({ slug, languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], - status: _status === "draft" ? CollectionStatus.Draft : CollectionStatus.Published, ...(releaseDate ? { releaseDate } : {}), ...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}), tagGroups: convertTagsToGroups(tags), diff --git a/src/collections/Folders/endpoints/getBySlugEndpoint.ts b/src/collections/Folders/endpoints/getBySlugEndpoint.ts index 6b5ef1e..74e6301 100644 --- a/src/collections/Folders/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Folders/endpoints/getBySlugEndpoint.ts @@ -2,15 +2,16 @@ import { Collections } from "../../../constants"; import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { EndpointFolder, EndpointFolderPreview } from "../../../sdk"; import { Folder, Language } from "../../../types/collections"; -import { isDefined, isPayloadType } from "../../../utils/asserts"; +import { isDefined, isPayloadType, isPublished } from "../../../utils/asserts"; import { handleParentPages } from "../../../utils/endpoints"; import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint"; import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint"; -export const getBySlugEndpoint = createGetByEndpoint( - Collections.Folders, - "slug", - (folder: Folder): EndpointFolder => { +export const getBySlugEndpoint = createGetByEndpoint({ + collection: Collections.Folders, + attribute: "slug", + depth: 3, + handler: (folder: Folder): EndpointFolder => { const { sections, files, parentFolders } = folder; return { ...convertFolderToPreview(folder), @@ -34,9 +35,10 @@ export const getBySlugEndpoint = createGetByEndpoint( }, files: files?.flatMap(({ relationTo, value }) => { - if (!isPayloadType(value)) { + if (!isPayloadType(value) || !isPublished(value)) { return []; } + switch (relationTo) { case "collectibles": return [{ relationTo, value: convertCollectibleToPreview(value) }]; @@ -47,8 +49,7 @@ export const getBySlugEndpoint = createGetByEndpoint( parentPages: handleParentPages({ folders: parentFolders }), }; }, - 3 -); +}); export const convertFolderToPreview = ({ slug, diff --git a/src/collections/Pages/endpoints/getBySlugEndpoint.ts b/src/collections/Pages/endpoints/getBySlugEndpoint.ts index c5eed66..420349f 100644 --- a/src/collections/Pages/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Pages/endpoints/getBySlugEndpoint.ts @@ -15,10 +15,10 @@ import { Page } from "../../../types/collections"; import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; import { convertTagsToGroups, handleParentPages, handleRecorder } from "../../../utils/endpoints"; -export const getBySlugEndpoint = createGetByEndpoint( - Collections.Pages, - "slug", - (page: Page): EndpointPage => { +export const getBySlugEndpoint = createGetByEndpoint({ + collection: Collections.Pages, + attribute: "slug", + handler: (page: Page): EndpointPage => { const { translations, collectibles, folders, backgroundImage } = page; return { @@ -52,8 +52,8 @@ export const getBySlugEndpoint = createGetByEndpoint( ), parentPages: handleParentPages({ collectibles, folders }), }; - } -); + }, +}); const handleContent = ( { root: { children, ...others } }: RichTextContent, @@ -152,7 +152,6 @@ export const convertPageToPreview = ({ translations, tags, thumbnail, - _status, type, }: Page): EndpointPagePreview => ({ slug, @@ -166,5 +165,4 @@ export const convertPageToPreview = ({ ...(subtitle ? { subtitle } : {}), })), authors: isPayloadArrayType(authors) ? authors.map(handleRecorder) : [], - status: _status === "published" ? "published" : "draft", }); diff --git a/src/endpoints/createGetByEndpoint.ts b/src/endpoints/createGetByEndpoint.ts index f6bb698..a011b6b 100644 --- a/src/endpoints/createGetByEndpoint.ts +++ b/src/endpoints/createGetByEndpoint.ts @@ -1,12 +1,20 @@ import payload, { GeneratedTypes } from "payload"; import { CollectionEndpoint } from "../types/payload"; +import { isPublished } from "../utils/asserts"; -export const createGetByEndpoint = ( - collection: C, - attribute: string, - handler: (doc: GeneratedTypes["collections"][C]) => Promise | R, - depth?: number -): CollectionEndpoint => ({ +interface Params { + collection: C; + attribute: string; + handler: (doc: GeneratedTypes["collections"][C]) => Promise | R; + depth?: number; +} + +export const createGetByEndpoint = ({ + attribute, + collection, + handler, + depth, +}: Params): CollectionEndpoint => ({ path: `/${attribute}/:${attribute}`, method: "get", handler: async (req, res) => { @@ -26,10 +34,16 @@ export const createGetByEndpoint = (value: string | T): value is T = export const isPayloadArrayType = ( value: (string | T)[] | null | undefined ): value is T[] => isDefined(value) && value.every(isPayloadType); + +export const isPublished = ( + object: T +): boolean => object._status === "published"; diff --git a/src/utils/endpoints.ts b/src/utils/endpoints.ts index de7a6e3..dd7243d 100644 --- a/src/utils/endpoints.ts +++ b/src/utils/endpoints.ts @@ -1,7 +1,7 @@ import { Collections } from "../constants"; import { EndpointRecorder, EndpointTag, EndpointTagsGroup, ParentPage } from "../sdk"; import { Collectible, Folder, Recorder, Tag } from "../types/collections"; -import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "./asserts"; +import { isPayloadArrayType, isPayloadType, isPublished, isValidPayloadImage } from "./asserts"; export const convertTagsToGroups = ( tags: (string | Tag)[] | null | undefined @@ -53,7 +53,7 @@ export const handleParentPages = ({ const result: ParentPage[] = []; if (collectibles && isPayloadArrayType(collectibles)) { - collectibles.forEach(({ slug, translations }) => { + collectibles.filter(isPublished).forEach(({ slug, translations }) => { result.push({ collection: Collections.Collectibles, slug,