diff --git a/src/endpoints/getAllPathsEndpoint.ts b/src/endpoints/getAllPathsEndpoint.ts new file mode 100644 index 0000000..086c636 --- /dev/null +++ b/src/endpoints/getAllPathsEndpoint.ts @@ -0,0 +1,81 @@ +import payload from "payload"; +import { Endpoint } from "payload/config"; +import { Collections } from "../constants"; +import { EndpointAllPaths } from "../sdk"; + +export const getAllPathsEndpoint: Endpoint = { + method: "get", + path: "/all-paths", + 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, + }); + + const pages = await payload.find({ + collection: Collections.Pages, + depth: 0, + pagination: false, + user: req.user, + }); + + 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 recorders = await payload.find({ + collection: Collections.Recorders, + depth: 0, + pagination: false, + user: req.user, + }); + + const result: EndpointAllPaths = { + collectibles: collectibles.docs.map(({ slug }) => slug), + pages: pages.docs.map(({ slug }) => slug), + folders: folders.docs.map(({ slug }) => slug), + videos: videos.docs.map(({ id }) => id), + audios: audios.docs.map(({ id }) => id), + images: images.docs.map(({ id }) => id), + recorders: recorders.docs.map(({ id }) => id), + }; + + return res.status(200).send(result); + }, +}; diff --git a/src/payload.config.ts b/src/payload.config.ts index 5001687..b1ed049 100644 --- a/src/payload.config.ts +++ b/src/payload.config.ts @@ -27,6 +27,7 @@ import { Wordings } from "./collections/Wordings/Wordings"; import { Icon } from "./components/Icon"; import { Logo } from "./components/Logo"; import { Collections } from "./constants"; +import { getAllPathsEndpoint } from "./endpoints/getAllPathsEndpoint"; import { createEditor } from "./utils/editor"; const configuredFtpAdapter = sftpAdapter({ @@ -84,6 +85,7 @@ export default buildConfig({ typescript: { outputFile: path.resolve(__dirname, "types/collections.ts"), }, + endpoints: [getAllPathsEndpoint], graphQL: { disable: true, }, diff --git a/src/sdk.ts b/src/sdk.ts index 29787fa..bec2dff 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -516,6 +516,16 @@ export type PayloadImage = PayloadMedia & { height: number; }; +export type EndpointAllPaths = { + collectibles: string[]; + pages: string[]; + folders: string[]; + videos: string[]; + audios: string[]; + images: string[]; + recorders: string[]; +}; + // SDK type GetPayloadSDKParams = { @@ -542,7 +552,7 @@ export const getPayloadSDK = ({ responseCache, }: GetPayloadSDKParams) => { const refreshToken = async () => { - const loginUrl = payloadApiUrl(Collections.Recorders, "login"); + const loginUrl = `${apiURL}/${Collections.Recorders}/login`; const loginResult = await fetch(loginUrl, { method: "POST", headers: { "Content-Type": "application/json" }, @@ -562,9 +572,6 @@ export const getPayloadSDK = ({ return token; }; - const payloadApiUrl = (collection: Collections, endpoint?: string, isGlobal?: boolean): string => - `${apiURL}/${isGlobal === undefined ? "" : "globals/"}${collection}${endpoint === undefined ? "" : `/${endpoint}`}`; - const request = async (url: string): Promise => { const cachedResponse = responseCache?.get(url); if (cachedResponse) { @@ -589,45 +596,46 @@ export const getPayloadSDK = ({ return { getConfig: async (): Promise => - await request(payloadApiUrl(Collections.WebsiteConfig, `config`, true)), + await request(`${apiURL}/globals/${Collections.WebsiteConfig}/config`), getFolder: async (slug: string): Promise => - await request(payloadApiUrl(Collections.Folders, `slug/${slug}`)), + await request(`${apiURL}/${Collections.Folders}/slug/${slug}`), getLanguages: async (): Promise => - await request(payloadApiUrl(Collections.Languages, `all`)), + await request(`${apiURL}/${Collections.Languages}/all`), getCurrencies: async (): Promise => - await request(payloadApiUrl(Collections.Currencies, `all`)), + await request(`${apiURL}/${Collections.Currencies}/all`), getWordings: async (): Promise => - await request(payloadApiUrl(Collections.Wordings, `all`)), + await request(`${apiURL}/${Collections.Wordings}/all`), getPage: async (slug: string): Promise => - await request(payloadApiUrl(Collections.Pages, `slug/${slug}`)), + await request(`${apiURL}/${Collections.Pages}/slug/${slug}`), getCollectible: async (slug: string): Promise => - await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}`)), + await request(`${apiURL}/${Collections.Collectibles}/slug/${slug}`), getCollectibleScans: async (slug: string): Promise => - await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/scans`)), + await request(`${apiURL}/${Collections.Collectibles}/slug/${slug}/scans`), getCollectibleScanPage: async ( slug: string, index: string ): Promise => - await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/scans/${index}`)), + await request(`${apiURL}/${Collections.Collectibles}/slug/${slug}/scans/${index}`), getCollectibleGallery: async (slug: string): Promise => - await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/gallery`)), + await request(`${apiURL}/${Collections.Collectibles}/slug/${slug}/gallery`), getCollectibleGalleryImage: async ( slug: string, index: string ): Promise => - await request(payloadApiUrl(Collections.Collectibles, `slug/${slug}/gallery/${index}`)), + await request(`${apiURL}/${Collections.Collectibles}/slug/${slug}/gallery/${index}`), getChronologyEvents: async (): Promise => - await request(payloadApiUrl(Collections.ChronologyEvents, `all`)), + await request(`${apiURL}/${Collections.ChronologyEvents}/all`), getChronologyEventByID: async (id: string): Promise => - await request(payloadApiUrl(Collections.ChronologyEvents, `id/${id}`)), + await request(`${apiURL}/${Collections.ChronologyEvents}/id/${id}`), getImageByID: async (id: string): Promise => - await request(payloadApiUrl(Collections.Images, `id/${id}`)), + await request(`${apiURL}/${Collections.Images}/id/${id}`), getAudioByID: async (id: string): Promise => - await request(payloadApiUrl(Collections.Audios, `id/${id}`)), + await request(`${apiURL}/${Collections.Audios}/id/${id}`), getVideoByID: async (id: string): Promise => - await request(payloadApiUrl(Collections.Videos, `id/${id}`)), + await request(`${apiURL}/${Collections.Videos}/id/${id}`), getRecorderByID: async (id: string): Promise => - await request(payloadApiUrl(Collections.Recorders, `id/${id}`)), + await request(`${apiURL}/${Collections.Recorders}/id/${id}`), + getAllPaths: async (): Promise => await request(`${apiURL}/all-paths`), request: async (url: string): Promise => await request(url), }; };