Improved getAllSDKUrls

This commit is contained in:
DrMint 2024-06-26 06:38:49 +02:00
parent eeab8fcc4a
commit 0a4fe750f8
5 changed files with 220 additions and 202 deletions

View File

@ -1,112 +0,0 @@
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,
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 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),
files: files.docs.map(({ id }) => id),
recorders: recorders.docs.map(({ id }) => id),
chronologyEvents: chronologyEvents.docs.map(({ id }) => id),
};
return res.status(200).send(result);
},
};

View File

@ -0,0 +1,209 @@
import payload from "payload";
import { Endpoint } from "payload/config";
import { Collections } from "../constants";
import { EndpointAllSDKUrls, getSDKEndpoint } from "../sdk";
import { Collectible } from "../types/collections";
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(),
...collectibles.docs.flatMap((doc) =>
getSDKUrlsForDocument(Collections.Collectibles, doc)
),
...pages.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Pages, doc)),
...folders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Folders, 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)),
...recorders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Recorders, doc)),
...chronologyEvents.docs.flatMap((doc) =>
getSDKUrlsForDocument(Collections.ChronologyEvents, doc)
),
]);
const result: EndpointAllSDKUrls = {
urls: [...urls],
};
return res.status(200).send(result);
},
};
export 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 [];
}
}
};

View File

@ -4,79 +4,8 @@ import {
} from "payload/dist/collections/config/types";
import { AfterChangeHook as GlobalAfterChangeHook } from "payload/dist/globals/config/types";
import { AfterOperationWebHookMessage, Collections } from "../constants";
import { getSDKUrlsForDocument } from "../endpoints/getAllSDKUrlsEndpoint";
import { getAddedBackPropagationRelationships } from "../fields/backPropagationField/backPropagationUtils";
import { getSDKEndpoint } from "../sdk";
import { Collectible } from "../types/collections";
const getURLs = (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)];
default: {
console.warn("Unrecognized collection", collection, "when sending webhook. No URL.");
return [];
}
}
};
export const globalAfterChangeWebhook: GlobalAfterChangeHook = async ({
global,
@ -87,7 +16,7 @@ export const globalAfterChangeWebhook: GlobalAfterChangeHook = async ({
await sendWebhookMessage({
collection,
addedDependantIds: await getAddedBackPropagationRelationships(global, doc, previousDoc),
urls: getURLs(collection, doc),
urls: getSDKUrlsForDocument(collection, doc),
});
return doc;
};
@ -113,7 +42,7 @@ export const collectionAfterChangeWebhook: CollectionAfterChangeHook = async ({
collection: collectionSlug,
id: doc.id,
addedDependantIds: await getAddedBackPropagationRelationships(collection, doc, previousDoc),
urls: getURLs(collectionSlug, doc),
urls: getSDKUrlsForDocument(collectionSlug, doc),
});
return doc;
@ -131,7 +60,7 @@ export const afterDeleteWebhook: AfterDeleteHook = async ({ collection, doc }) =
collection: collectionSlug,
id: doc.id,
addedDependantIds: [],
urls: getURLs(collectionSlug, doc),
urls: getSDKUrlsForDocument(collectionSlug, doc),
});
return doc;

View File

@ -28,7 +28,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 { getAllSDKUrlsEndpoint } from "./endpoints/getAllSDKUrlsEndpoint";
import { createEditor } from "./utils/editor";
const configuredSftpAdapter = sftpAdapter({
@ -87,7 +87,7 @@ export default buildConfig({
typescript: {
outputFile: path.resolve(__dirname, "types/collections.ts"),
},
endpoints: [getAllPathsEndpoint],
endpoints: [getAllSDKUrlsEndpoint],
graphQL: {
disable: true,
},

View File

@ -531,16 +531,8 @@ export type PayloadImage = PayloadMedia & {
height: number;
};
export type EndpointAllPaths = {
collectibles: string[];
pages: string[];
folders: string[];
videos: string[];
audios: string[];
images: string[];
files: string[];
recorders: string[];
chronologyEvents: string[];
export type EndpointAllSDKUrls = {
urls: string[];
};
// SDK
@ -583,7 +575,7 @@ export const getSDKEndpoint = {
getVideoByIDEndpoint: (id: string) => `/${Collections.Videos}/id/${id}`,
getFileByIDEndpoint: (id: string) => `/${Collections.Files}/id/${id}`,
getRecorderByIDEndpoint: (id: string) => `/${Collections.Recorders}/id/${id}`,
getAllPathsEndpoint: () => `/all-paths`,
getAllSDKUrlsEndpoint: () => `/all-sdk-urls`,
getLoginEndpoint: () => `/${Collections.Recorders}/login`,
};
@ -680,8 +672,8 @@ export const getPayloadSDK = ({
await request(getSDKEndpoint.getFileByIDEndpoint(id)),
getRecorderByID: async (id: string): Promise<EndpointRecorder> =>
await request(getSDKEndpoint.getRecorderByIDEndpoint(id)),
getAllPaths: async (): Promise<EndpointAllPaths> =>
await request(getSDKEndpoint.getAllPathsEndpoint()),
getAllSdkUrls: async (): Promise<EndpointAllSDKUrls> =>
await request(getSDKEndpoint.getAllSDKUrlsEndpoint()),
request: async (pathname: string): Promise<any> => await request(pathname),
};
};