From 9e7d020ad759a4593c8c2112464fbfbe99dc330b Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Fri, 26 Jul 2024 08:13:43 +0200 Subject: [PATCH] Removed unused endpoints and use EndpointChanges for getAll --- ...getAllIdsEndpoint.ts => getAllEndpoint.ts} | 50 +++-- src/endpoints/getAllSDKUrlsEndpoint.ts | 208 ------------------ src/hooks/afterOperationSendChangesWebhook.ts | 88 ++++---- src/payload.config.ts | 5 +- src/shared | 2 +- 5 files changed, 84 insertions(+), 269 deletions(-) rename src/endpoints/{getAllIdsEndpoint.ts => getAllEndpoint.ts} (57%) delete mode 100644 src/endpoints/getAllSDKUrlsEndpoint.ts diff --git a/src/endpoints/getAllIdsEndpoint.ts b/src/endpoints/getAllEndpoint.ts similarity index 57% rename from src/endpoints/getAllIdsEndpoint.ts rename to src/endpoints/getAllEndpoint.ts index 6d2c5bc..e175d59 100644 --- a/src/endpoints/getAllIdsEndpoint.ts +++ b/src/endpoints/getAllEndpoint.ts @@ -1,11 +1,27 @@ import payload from "payload"; import { Endpoint } from "payload/config"; import { Collections } from "../shared/payload/constants"; -import { EndpointAllIds } from "../shared/payload/endpoint-types"; +import { EndpointChange } from "../shared/payload/webhooks"; +import { + getEndpointChangesForAudio, + getEndpointChangesForChronologyEvent, + getEndpointChangesForCollectible, + getEndpointChangesForCurrency, + getEndpointChangesForFile, + getEndpointChangesForFolder, + getEndpointChangesForImage, + getEndpointChangesForLanguage, + getEndpointChangesForPage, + getEndpointChangesForRecorder, + getEndpointChangesForVideo, + getEndpointChangesForWebsiteConfig, + getEndpointChangesForWording, +} from "../hooks/afterOperationSendChangesWebhook"; +import { uniqueBy } from "../utils/array"; -export const getAllIds: Endpoint = { +export const getAllEndpoint: Endpoint = { method: "get", - path: "/all-ids", + path: "/all", handler: async (req, res) => { if (!req.user) { return res.status(403).send({ @@ -95,18 +111,22 @@ export const getAllIds: Endpoint = { }, }); - const result: EndpointAllIds = { - collectibles: { slugs: collectibles.docs.map(({ slug }) => slug) }, - pages: { slugs: pages.docs.map(({ slug }) => slug) }, - folders: { slugs: folders.docs.map(({ slug }) => slug) }, - videos: { ids: videos.docs.map(({ id }) => id) }, - audios: { ids: audios.docs.map(({ id }) => id) }, - images: { ids: images.docs.map(({ id }) => id) }, - files: { ids: files.docs.map(({ id }) => id) }, - recorders: { ids: recorders.docs.map(({ id }) => id) }, - chronologyEvents: { ids: chronologyEvents.docs.map(({ id }) => id) }, - }; + const result: EndpointChange[] = [ + ...getEndpointChangesForWebsiteConfig(), + ...getEndpointChangesForLanguage(), + ...getEndpointChangesForCurrency(), + ...getEndpointChangesForWording(), + ...folders.docs.flatMap(getEndpointChangesForFolder), + ...pages.docs.flatMap(getEndpointChangesForPage), + ...collectibles.docs.flatMap(getEndpointChangesForCollectible), + ...audios.docs.flatMap(getEndpointChangesForAudio), + ...images.docs.flatMap(getEndpointChangesForImage), + ...videos.docs.flatMap(getEndpointChangesForVideo), + ...files.docs.flatMap(getEndpointChangesForFile), + ...recorders.docs.flatMap(getEndpointChangesForRecorder), + ...chronologyEvents.docs.flatMap(getEndpointChangesForChronologyEvent), + ]; - return res.status(200).send(result); + return res.status(200).send(uniqueBy(result, ({ url }) => url)); }, }; diff --git a/src/endpoints/getAllSDKUrlsEndpoint.ts b/src/endpoints/getAllSDKUrlsEndpoint.ts deleted file mode 100644 index 5f193e6..0000000 --- a/src/endpoints/getAllSDKUrlsEndpoint.ts +++ /dev/null @@ -1,208 +0,0 @@ -import payload from "payload"; -import { Endpoint } from "payload/config"; -import { Collectible } from "../types/collections"; -import { Collections } from "../shared/payload/constants"; -import { EndpointAllSDKUrls } from "../shared/payload/endpoint-types"; -import { getSDKEndpoint } from "../shared/payload/sdk"; - -export const getAllSDKUrlsEndpoint: Endpoint = { - method: "get", - path: "/all-sdk-urls", - handler: async (req, res) => { - if (!req.user) { - return res.status(403).send({ - errors: [ - { - message: "You are not allowed to perform this action.", - }, - ], - }); - } - - const collectibles = await payload.find({ - collection: Collections.Collectibles, - depth: 0, - pagination: false, - user: req.user, - where: { - _status: { - equals: "published", - }, - }, - }); - - const pages = await payload.find({ - collection: Collections.Pages, - depth: 0, - pagination: false, - user: req.user, - where: { - _status: { - equals: "published", - }, - }, - }); - - const folders = await payload.find({ - collection: Collections.Folders, - depth: 0, - pagination: false, - user: req.user, - }); - - const videos = await payload.find({ - collection: Collections.Videos, - depth: 0, - pagination: false, - user: req.user, - }); - - const audios = await payload.find({ - collection: Collections.Audios, - depth: 0, - pagination: false, - user: req.user, - }); - - const images = await payload.find({ - collection: Collections.Images, - depth: 0, - pagination: false, - user: req.user, - }); - - const files = await payload.find({ - collection: Collections.Files, - depth: 0, - pagination: false, - user: req.user, - }); - - const recorders = await payload.find({ - collection: Collections.Recorders, - depth: 0, - pagination: false, - user: req.user, - }); - - const chronologyEvents = await payload.find({ - collection: Collections.ChronologyEvents, - depth: 0, - pagination: false, - user: req.user, - where: { - _status: { - equals: "published", - }, - }, - }); - - const urls = new Set([ - getSDKEndpoint.getConfigEndpoint(), - getSDKEndpoint.getLanguagesEndpoint(), - getSDKEndpoint.getCurrenciesEndpoint(), - getSDKEndpoint.getWordingsEndpoint(), - - ...folders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Folders, doc)), - ...pages.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Pages, doc)), - ...chronologyEvents.docs.flatMap((doc) => - getSDKUrlsForDocument(Collections.ChronologyEvents, doc) - ), - ...videos.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Videos, doc)), - ...audios.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Audios, doc)), - ...images.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Images, doc)), - ...files.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Files, doc)), - ...collectibles.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Collectibles, doc)), - ...recorders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Recorders, doc)), - ]); - - const result: EndpointAllSDKUrls = { - urls: [...urls], - }; - - return res.status(200).send(result); - }, -}; - -const getSDKUrlsForDocument = (collection: Collections, doc: any): string[] => { - switch (collection) { - case Collections.WebsiteConfig: - return [getSDKEndpoint.getConfigEndpoint()]; - - case Collections.Folders: - return [getSDKEndpoint.getFolderEndpoint(doc.slug)]; - - case Collections.Languages: - return [getSDKEndpoint.getLanguagesEndpoint()]; - - case Collections.Currencies: - return [getSDKEndpoint.getCurrenciesEndpoint()]; - - case Collections.Wordings: - return [getSDKEndpoint.getWordingsEndpoint()]; - - case Collections.Pages: - return [getSDKEndpoint.getPageEndpoint(doc.slug)]; - - case Collections.Collectibles: { - const { slug, gallery, scans, scansEnabled } = doc as Collectible; - const urls: string[] = [getSDKEndpoint.getCollectibleEndpoint(slug)]; - if (gallery && gallery.length > 0) { - urls.push(getSDKEndpoint.getCollectibleGalleryEndpoint(slug)); - urls.push( - ...gallery.map((_, index) => - getSDKEndpoint.getCollectibleGalleryImageEndpoint(slug, index.toString()) - ) - ); - } - if (scans && scansEnabled) { - urls.push(getSDKEndpoint.getCollectibleScansEndpoint(slug)); - // TODO: Add other pages for cover, obi, dustjacket... - if (scans.pages) { - urls.push( - ...scans.pages.map(({ page }) => - getSDKEndpoint.getCollectibleScanPageEndpoint(slug, page.toString()) - ) - ); - } - } - return urls; - } - - case Collections.ChronologyEvents: - return [ - getSDKEndpoint.getChronologyEventsEndpoint(), - getSDKEndpoint.getChronologyEventByIDEndpoint(doc.id), - ]; - - case Collections.Images: - return [getSDKEndpoint.getImageByIDEndpoint(doc.id)]; - - case Collections.Audios: - return [getSDKEndpoint.getAudioByIDEndpoint(doc.id)]; - - case Collections.Videos: - return [getSDKEndpoint.getVideoByIDEndpoint(doc.id)]; - - case Collections.Recorders: - return [getSDKEndpoint.getRecorderByIDEndpoint(doc.id)]; - - case Collections.Files: - return [getSDKEndpoint.getFileByIDEndpoint(doc.id)]; - - case Collections.Attributes: - case Collections.CreditsRole: - case Collections.GenericContents: - case Collections.MediaThumbnails: - case Collections.Scans: - case Collections.Tags: - case Collections.VideosChannels: - case Collections.VideosSubtitles: - return []; - - default: { - console.warn("Unrecognized collection", collection); - return []; - } - } -}; diff --git a/src/hooks/afterOperationSendChangesWebhook.ts b/src/hooks/afterOperationSendChangesWebhook.ts index c5f4c8c..942ed35 100644 --- a/src/hooks/afterOperationSendChangesWebhook.ts +++ b/src/hooks/afterOperationSendChangesWebhook.ts @@ -1,20 +1,17 @@ import { Collections } from "../shared/payload/constants"; -import { getSDKEndpoint } from "../shared/payload/sdk"; +import { SDKEndpointNames, getSDKEndpoint } from "../shared/payload/sdk"; import { EndpointChange } from "../shared/payload/webhooks"; import { Audio, ChronologyEvent, Collectible, - Currency, File, Folder, Image, - Language, Page, Recorder, Relationship, Video, - Wording, } from "../types/collections"; import { isPayloadType } from "../utils/asserts"; import { AfterChangeHook, AfterDeleteHook } from "payload/dist/collections/config/types"; @@ -55,7 +52,7 @@ export const globalAfterChangeSendChangesWebhook: GlobalAfterChangeHook = async switch (global.slug as keyof GeneratedTypes["globals"]) { case Collections.WebsiteConfig: - changes.push({ type: "getConfig", url: getSDKEndpoint.getConfigEndpoint() }); + changes.push(...getEndpointChangesForWebsiteConfig()); break; default: @@ -133,13 +130,13 @@ const getEndpointChangesFromDocument = ({ return getEndpointChangesForChronologyEvent(value); case Collections.Languages: - return getEndpointChangesForLanguage(value); + return getEndpointChangesForLanguage(); case Collections.Currencies: - return getEndpointChangesForCurrency(value); + return getEndpointChangesForCurrency(); case Collections.Wordings: - return getEndpointChangesForWording(value); + return getEndpointChangesForWording(); case Collections.Attributes: case Collections.CreditsRole: @@ -248,27 +245,34 @@ const getEndpointChangesFromOutgoingRelation = ({ } }; -const getEndpointChangesForFolder = ({ slug }: Folder): EndpointChange[] => [ - { type: "getFolder", slug, url: getSDKEndpoint.getFolderEndpoint(slug) }, +export const getEndpointChangesForWebsiteConfig = (): EndpointChange[] => [ + { + type: SDKEndpointNames.getWebsiteConfig, + url: getSDKEndpoint.getWebsiteConfig(), + }, ]; -const getEndpointChangesForLanguage = (_: Language): EndpointChange[] => [ - { type: "getLanguages", url: getSDKEndpoint.getLanguagesEndpoint() }, +export const getEndpointChangesForFolder = ({ slug }: Folder): EndpointChange[] => [ + { type: SDKEndpointNames.getFolder, slug, url: getSDKEndpoint.getFolder(slug) }, ]; -const getEndpointChangesForCurrency = (_: Currency): EndpointChange[] => [ - { type: "getCurrencies", url: getSDKEndpoint.getCurrenciesEndpoint() }, +export const getEndpointChangesForLanguage = (): EndpointChange[] => [ + { type: SDKEndpointNames.getLanguages, url: getSDKEndpoint.getLanguages() }, ]; -const getEndpointChangesForWording = (_: Wording): EndpointChange[] => [ - { type: "getWordings", url: getSDKEndpoint.getWordingsEndpoint() }, +export const getEndpointChangesForCurrency = (): EndpointChange[] => [ + { type: SDKEndpointNames.getCurrencies, url: getSDKEndpoint.getCurrencies() }, ]; -const getEndpointChangesForPage = ({ slug }: Page): EndpointChange[] => [ - { type: "getPage", slug, url: getSDKEndpoint.getPageEndpoint(slug) }, +export const getEndpointChangesForWording = (): EndpointChange[] => [ + { type: SDKEndpointNames.getWordings, url: getSDKEndpoint.getWordings() }, ]; -const getEndpointChangesForCollectible = ({ +export const getEndpointChangesForPage = ({ slug }: Page): EndpointChange[] => [ + { type: SDKEndpointNames.getPage, slug, url: getSDKEndpoint.getPage(slug) }, +]; + +export const getEndpointChangesForCollectible = ({ slug, gallery, scans, @@ -278,26 +282,26 @@ const getEndpointChangesForCollectible = ({ if (gallery && gallery.length > 0) { changes.push({ - type: "getCollectibleGallery", + type: SDKEndpointNames.getCollectibleGallery, slug, - url: getSDKEndpoint.getCollectibleGalleryEndpoint(slug), + url: getSDKEndpoint.getCollectibleGallery(slug), }); gallery.forEach((_, indexNumber) => { const index = indexNumber.toString(); changes.push({ - type: "getCollectibleGalleryImage", + type: SDKEndpointNames.getCollectibleGalleryImage, slug, index: index, - url: getSDKEndpoint.getCollectibleGalleryImageEndpoint(slug, index), + url: getSDKEndpoint.getCollectibleGalleryImage(slug, index), }); }); } if (scans && scansEnabled) { changes.push({ - type: "getCollectibleScans", + type: SDKEndpointNames.getCollectibleScans, slug, - url: getSDKEndpoint.getCollectibleScansEndpoint(slug), + url: getSDKEndpoint.getCollectibleScans(slug), }); // TODO: Add other changes for cover, obi, dustjacket... @@ -305,10 +309,10 @@ const getEndpointChangesForCollectible = ({ scans.pages?.forEach((_, indexNumber) => { const index = indexNumber.toString(); changes.push({ - type: "getCollectibleScanPage", + type: SDKEndpointNames.getCollectibleScanPage, slug, index: index, - url: getSDKEndpoint.getCollectibleScanPageEndpoint(slug, index), + url: getSDKEndpoint.getCollectibleScanPage(slug, index), }); }); } @@ -316,35 +320,35 @@ const getEndpointChangesForCollectible = ({ return changes; }; -const getEndpointChangesForAudio = ({ id }: Audio): EndpointChange[] => [ - { type: "getAudioByID", id, url: getSDKEndpoint.getAudioByIDEndpoint(id) }, +export const getEndpointChangesForAudio = ({ id }: Audio): EndpointChange[] => [ + { type: SDKEndpointNames.getAudioByID, id, url: getSDKEndpoint.getAudioByID(id) }, ]; -const getEndpointChangesForImage = ({ id }: Image): EndpointChange[] => [ - { type: "getImageByID", id, url: getSDKEndpoint.getImageByIDEndpoint(id) }, +export const getEndpointChangesForImage = ({ id }: Image): EndpointChange[] => [ + { type: SDKEndpointNames.getImageByID, id, url: getSDKEndpoint.getImageByID(id) }, ]; -const getEndpointChangesForVideo = ({ id }: Video): EndpointChange[] => [ - { type: "getVideoByID", id, url: getSDKEndpoint.getVideoByIDEndpoint(id) }, +export const getEndpointChangesForVideo = ({ id }: Video): EndpointChange[] => [ + { type: SDKEndpointNames.getVideoByID, id, url: getSDKEndpoint.getVideoByID(id) }, ]; -const getEndpointChangesForFile = ({ id }: File): EndpointChange[] => [ - { type: "getFileByID", id, url: getSDKEndpoint.getFileByIDEndpoint(id) }, +export const getEndpointChangesForFile = ({ id }: File): EndpointChange[] => [ + { type: SDKEndpointNames.getFileByID, id, url: getSDKEndpoint.getFileByID(id) }, ]; -const getEndpointChangesForRecorder = ({ id }: Recorder): EndpointChange[] => [ - { type: "getRecorderByID", id, url: getSDKEndpoint.getRecorderByIDEndpoint(id) }, +export const getEndpointChangesForRecorder = ({ id }: Recorder): EndpointChange[] => [ + { type: SDKEndpointNames.getRecorderByID, id, url: getSDKEndpoint.getRecorderByID(id) }, ]; -const getEndpointChangesForChronologyEvent = ({ id }: ChronologyEvent): EndpointChange[] => [ +export const getEndpointChangesForChronologyEvent = ({ id }: ChronologyEvent): EndpointChange[] => [ { - type: "getChronologyEventByID", + type: SDKEndpointNames.getChronologyEventByID, id, - url: getSDKEndpoint.getChronologyEventByIDEndpoint(id), + url: getSDKEndpoint.getChronologyEventByID(id), }, { - type: "getChronologyEvents", - url: getSDKEndpoint.getChronologyEventsEndpoint(), + type: SDKEndpointNames.getChronologyEvents, + url: getSDKEndpoint.getChronologyEvents(), }, ]; diff --git a/src/payload.config.ts b/src/payload.config.ts index 1fccb35..a96ac2a 100644 --- a/src/payload.config.ts +++ b/src/payload.config.ts @@ -27,8 +27,7 @@ import { WebsiteConfig } from "./collections/WebsiteConfig/WebsiteConfig"; import { Wordings } from "./collections/Wordings/Wordings"; import { Icon } from "./components/Icon"; import { Logo } from "./components/Logo"; -import { getAllIds } from "./endpoints/getAllIdsEndpoint"; -import { getAllSDKUrlsEndpoint } from "./endpoints/getAllSDKUrlsEndpoint"; +import { getAllEndpoint } from "./endpoints/getAllEndpoint"; import { createEditor } from "./utils/editor"; import { Collections } from "./shared/payload/constants"; import { relationshipsPlugin } from "payloadcms-relationships"; @@ -92,7 +91,7 @@ export default buildConfig({ typescript: { outputFile: path.resolve(__dirname, "types/collections.ts"), }, - endpoints: [getAllSDKUrlsEndpoint, getAllIds], + endpoints: [getAllEndpoint], graphQL: { disable: true, }, diff --git a/src/shared b/src/shared index 3582a48..caa79de 160000 --- a/src/shared +++ b/src/shared @@ -1 +1 @@ -Subproject commit 3582a48bd12a66e99121d9fbc01681971a7d943a +Subproject commit caa79dee9eca5b9b6959e6f5a721245202423612