Turn PayloadImage into EndpointImage

This commit is contained in:
DrMint 2024-04-13 21:23:01 +02:00
parent 47f8a12042
commit 89f79cb7d5
10 changed files with 58 additions and 71 deletions

8
package-lock.json generated
View File

@ -35,7 +35,7 @@
"prettier": "3.2.5", "prettier": "3.2.5",
"ts-node": "10.9.2", "ts-node": "10.9.2",
"ts-unused-exports": "10.0.1", "ts-unused-exports": "10.0.1",
"typescript": "5.4.4" "typescript": "5.4.5"
} }
}, },
"node_modules/@aws-crypto/crc32": { "node_modules/@aws-crypto/crc32": {
@ -15922,9 +15922,9 @@
} }
}, },
"node_modules/typescript": { "node_modules/typescript": {
"version": "5.4.4", "version": "5.4.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.4.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz",
"integrity": "sha512-dGE2Vv8cpVvw28v8HCPqyb08EzbBURxDpuhJvTrusShUfGnhHBafDsLdS1EhhxyL6BJQE+2cT3dDPAv+MQ6oLw==", "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==",
"bin": { "bin": {
"tsc": "bin/tsc", "tsc": "bin/tsc",
"tsserver": "bin/tsserver" "tsserver": "bin/tsserver"

View File

@ -48,6 +48,6 @@
"prettier": "3.2.5", "prettier": "3.2.5",
"ts-node": "10.9.2", "ts-node": "10.9.2",
"ts-unused-exports": "10.0.1", "ts-unused-exports": "10.0.1",
"typescript": "5.4.4" "typescript": "5.4.5"
} }
} }

View File

@ -128,7 +128,8 @@ export const Collectibles = buildVersionedCollectionConfig({
"A physical or digital item. This can be a book, a CD/DVD, a video game copy...\ "A physical or digital item. This can be a book, a CD/DVD, a video game copy...\
any product related to our Scope.\ any product related to our Scope.\
This can also include merchandises such as figurines, music boxes, posters, key chains...", This can also include merchandises such as figurines, music boxes, posters, key chains...",
preview: ({ slug }) => `${process.env.PAYLOAD_PUBLIC_FRONTEND_BASE_URL}/en/collectibles/${slug}`, preview: ({ slug }) =>
`${process.env.PAYLOAD_PUBLIC_FRONTEND_BASE_URL}/en/collectibles/${slug}`,
hooks: { hooks: {
beforeDuplicate: beforeDuplicatePiping([ beforeDuplicate: beforeDuplicatePiping([
beforeDuplicateUnpublish, beforeDuplicateUnpublish,

View File

@ -1,6 +1,6 @@
import { CollectibleNature, Collections } from "../../../constants"; import { CollectibleNature, Collections } from "../../../constants";
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
import { EndpointCollectible, PayloadImage } from "../../../sdk"; import { EndpointCollectible } from "../../../sdk";
import { Collectible } from "../../../types/collections"; import { Collectible } from "../../../types/collections";
import { import {
isDefined, isDefined,
@ -17,6 +17,7 @@ import {
getDomainFromUrl, getDomainFromUrl,
} from "../../../utils/endpoints"; } from "../../../utils/endpoints";
import { convertAudioToEndpointAudio } from "../../Audios/endpoints/getByID"; import { convertAudioToEndpointAudio } from "../../Audios/endpoints/getByID";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
import { convertPageToEndpointPage } from "../../Pages/endpoints/getBySlugEndpoint"; import { convertPageToEndpointPage } from "../../Pages/endpoints/getBySlugEndpoint";
import { convertVideoToEndpointVideo } from "../../Videos/endpoints/getByID"; import { convertVideoToEndpointVideo } from "../../Videos/endpoints/getByID";
@ -55,7 +56,10 @@ export const convertCollectibleToEndpointCollectible = ({
slug, slug,
languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [],
...(isDefined(releaseDate) ? { releaseDate } : {}), ...(isDefined(releaseDate) ? { releaseDate } : {}),
...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}), ...(isValidPayloadImage(thumbnail) ? { thumbnail: convertImageToEndpointImage(thumbnail) } : {}),
...(isValidPayloadImage(backgroundImage)
? { backgroundImage: convertImageToEndpointImage(backgroundImage) }
: {}),
tagGroups: convertTagsEndpointTagsGroups(tags), tagGroups: convertTagsEndpointTagsGroups(tags),
translations: translations:
translations?.map(({ language, title, description, pretitle, subtitle }) => ({ translations?.map(({ language, title, description, pretitle, subtitle }) => ({
@ -65,7 +69,6 @@ export const convertCollectibleToEndpointCollectible = ({
...(isNotEmpty(subtitle) ? { subtitle } : {}), ...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description } : {}), ...(isNotEmpty(description) ? { description } : {}),
})) ?? [], })) ?? [],
...(isValidPayloadImage(backgroundImage) ? { backgroundImage } : {}),
contents: handleContents(contents), contents: handleContents(contents),
gallery: handleGallery(gallery), gallery: handleGallery(gallery),
scans: handleScans(scans), scans: handleScans(scans),
@ -119,42 +122,34 @@ const handlePageInfo = (
}; };
}; };
const handleGallery = (gallery: Collectible["gallery"]): EndpointCollectible["gallery"] => { const handleGallery = (gallery: Collectible["gallery"]): EndpointCollectible["gallery"] =>
const result: PayloadImage[] = []; gallery
if (!gallery) return result; ?.flatMap(({ image }) => {
if (!isValidPayloadImage(image)) return [];
gallery?.forEach(({ image }) => { return convertImageToEndpointImage(image);
if (isValidPayloadImage(image)) { })
result.push(image); .slice(0, 10) ?? [];
}
});
return result.slice(0, 10);
};
const handleScans = (scans: Collectible["scans"]): EndpointCollectible["scans"] => { const handleScans = (scans: Collectible["scans"]): EndpointCollectible["scans"] => {
const result: PayloadImage[] = []; const result =
if (!scans) return result; scans?.pages?.flatMap(({ image }) => {
if (!isValidPayloadImage(image)) return [];
return image;
}) ?? [];
scans.pages?.forEach(({ image }) => { if (isValidPayloadImage(scans?.cover?.front)) {
if (isValidPayloadImage(image)) {
result.push(image);
}
});
if (isValidPayloadImage(scans.cover?.front)) {
result.push(scans.cover.front); result.push(scans.cover.front);
} }
if (isValidPayloadImage(scans.cover?.back)) { if (isValidPayloadImage(scans?.cover?.back)) {
result.push(scans.cover.back); result.push(scans.cover.back);
} }
if (isValidPayloadImage(scans.dustjacket?.front)) { if (isValidPayloadImage(scans?.dustjacket?.front)) {
result.push(scans.dustjacket.front); result.push(scans.dustjacket.front);
} }
if (isValidPayloadImage(scans.dustjacket?.back)) { if (isValidPayloadImage(scans?.dustjacket?.back)) {
result.push(scans.dustjacket.back); result.push(scans.dustjacket.back);
} }

View File

@ -16,6 +16,7 @@ import {
convertSourceToEndpointSource, convertSourceToEndpointSource,
convertTagsEndpointTagsGroups, convertTagsEndpointTagsGroups,
} from "../../../utils/endpoints"; } from "../../../utils/endpoints";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getBySlugEndpoint = createGetByEndpoint({ export const getBySlugEndpoint = createGetByEndpoint({
collection: Collections.Pages, collection: Collections.Pages,
@ -33,9 +34,11 @@ export const convertPageToEndpointPage = ({
thumbnail, thumbnail,
}: Page): EndpointPage => ({ }: Page): EndpointPage => ({
slug, slug,
...(isValidPayloadImage(thumbnail) ? { thumbnail } : {}), ...(isValidPayloadImage(thumbnail) ? { thumbnail: convertImageToEndpointImage(thumbnail) } : {}),
...(isValidPayloadImage(backgroundImage)
? { backgroundImage: convertImageToEndpointImage(backgroundImage) }
: {}),
tagGroups: convertTagsEndpointTagsGroups(tags), tagGroups: convertTagsEndpointTagsGroups(tags),
...(isValidPayloadImage(backgroundImage) ? { backgroundImage } : {}),
translations: translations.map( translations: translations.map(
({ content, language, sourceLanguage, title, pretitle, subtitle, summary, credits }) => ({ ({ content, language, sourceLanguage, title, pretitle, subtitle, summary, credits }) => ({
language: isPayloadType(language) ? language.id : language, language: isPayloadType(language) ? language.id : language,

View File

@ -3,6 +3,7 @@ import { Collections } from "../../../constants";
import { EndpointRecorder } from "../../../sdk"; import { EndpointRecorder } from "../../../sdk";
import { CollectionEndpoint } from "../../../types/payload"; import { CollectionEndpoint } from "../../../types/payload";
import { isPayloadArrayType, isValidPayloadImage } from "../../../utils/asserts"; import { isPayloadArrayType, isValidPayloadImage } from "../../../utils/asserts";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getAllEndpoint: CollectionEndpoint = { export const getAllEndpoint: CollectionEndpoint = {
method: "get", method: "get",
@ -30,7 +31,7 @@ export const getAllEndpoint: CollectionEndpoint = {
({ anonymize, id, username, avatar, languages }) => ({ ({ anonymize, id, username, avatar, languages }) => ({
id, id,
username: anonymize ? `Recorder#${id.substring(0, 5)}` : username, username: anonymize ? `Recorder#${id.substring(0, 5)}` : username,
...(isValidPayloadImage(avatar) ? { avatar } : {}), ...(isValidPayloadImage(avatar) ? { avatar: convertImageToEndpointImage(avatar) } : {}),
languages: isPayloadArrayType(languages) ? languages.map(({ id }) => id) : [], languages: isPayloadArrayType(languages) ? languages.map(({ id }) => id) : [],
}) })
); );

View File

@ -3,6 +3,7 @@ import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
import { EndpointRecorder } from "../../../sdk"; import { EndpointRecorder } from "../../../sdk";
import { Recorder } from "../../../types/collections"; import { Recorder } from "../../../types/collections";
import { isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; import { isPayloadType, isValidPayloadImage } from "../../../utils/asserts";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getByUsernameEndpoint = createGetByEndpoint({ export const getByUsernameEndpoint = createGetByEndpoint({
collection: Collections.Recorders, collection: Collections.Recorders,
@ -20,5 +21,5 @@ export const convertRecorderToEndpointRecorder = ({
id, id,
languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [], languages: languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [],
username: anonymize ? `Recorder#${id.substring(0, 5)}` : username, username: anonymize ? `Recorder#${id.substring(0, 5)}` : username,
...(isValidPayloadImage(avatar) ? { avatar } : {}), ...(isValidPayloadImage(avatar) ? { avatar: convertImageToEndpointImage(avatar) } : {}),
}); });

View File

@ -4,6 +4,7 @@ import { EndpointWebsiteConfig } from "../../../sdk";
import { CollectionEndpoint } from "../../../types/payload"; import { CollectionEndpoint } from "../../../types/payload";
import { isPayloadType, isValidPayloadImage } from "../../../utils/asserts"; import { isPayloadType, isValidPayloadImage } from "../../../utils/asserts";
import { convertFolderToEndpointFolder } from "../../Folders/endpoints/getBySlugEndpoint"; import { convertFolderToEndpointFolder } from "../../Folders/endpoints/getBySlugEndpoint";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getConfigEndpoint: CollectionEndpoint = { export const getConfigEndpoint: CollectionEndpoint = {
method: "get", method: "get",
@ -47,8 +48,12 @@ export const getConfigEndpoint: CollectionEndpoint = {
if (!isPayloadType(folder)) return []; if (!isPayloadType(folder)) return [];
return { return {
...convertFolderToEndpointFolder(folder), ...convertFolderToEndpointFolder(folder),
...(isValidPayloadImage(darkThumbnail) ? { darkThumbnail } : {}), ...(isValidPayloadImage(darkThumbnail)
...(isValidPayloadImage(lightThumbnail) ? { lightThumbnail } : {}), ? { darkThumbnail: convertImageToEndpointImage(darkThumbnail) }
: {}),
...(isValidPayloadImage(lightThumbnail)
? { lightThumbnail: convertImageToEndpointImage(lightThumbnail) }
: {}),
}; };
}) ?? [], }) ?? [],
timeline: { timeline: {

View File

@ -139,8 +139,8 @@ export type EndpointFolder = {
export type EndpointWebsiteConfig = { export type EndpointWebsiteConfig = {
homeFolders: (EndpointFolder & { homeFolders: (EndpointFolder & {
lightThumbnail?: PayloadImage; lightThumbnail?: EndpointImage;
darkThumbnail?: PayloadImage; darkThumbnail?: EndpointImage;
})[]; })[];
timeline: { timeline: {
breaks: number[]; breaks: number[];
@ -156,7 +156,7 @@ export type EndpointWebsiteConfig = {
export type EndpointRecorder = { export type EndpointRecorder = {
id: string; id: string;
username: string; username: string;
avatar?: PayloadImage; avatar?: EndpointImage;
languages: string[]; languages: string[];
}; };
@ -201,9 +201,9 @@ export type EndpointCredit = {
export type EndpointPage = { export type EndpointPage = {
slug: string; slug: string;
thumbnail?: PayloadImage; thumbnail?: EndpointImage;
tagGroups: EndpointTagsGroup[]; tagGroups: EndpointTagsGroup[];
backgroundImage?: PayloadImage; backgroundImage?: EndpointImage;
translations: { translations: {
language: string; language: string;
pretitle?: string; pretitle?: string;
@ -220,7 +220,7 @@ export type EndpointPage = {
export type EndpointCollectible = { export type EndpointCollectible = {
slug: string; slug: string;
thumbnail?: PayloadImage; thumbnail?: EndpointImage;
translations: { translations: {
language: string; language: string;
pretitle?: string; pretitle?: string;
@ -231,9 +231,9 @@ export type EndpointCollectible = {
tagGroups: EndpointTagsGroup[]; tagGroups: EndpointTagsGroup[];
releaseDate?: string; releaseDate?: string;
languages: string[]; languages: string[];
backgroundImage?: PayloadImage; backgroundImage?: EndpointImage;
nature: CollectibleNature; nature: CollectibleNature;
gallery: PayloadImage[]; gallery: EndpointImage[];
scans: PayloadImage[]; scans: PayloadImage[];
urls: { url: string; label: string }[]; urls: { url: string; label: string }[];
price?: { price?: {

View File

@ -1,5 +1,6 @@
import { RichTextContent, isNodeParagraphNode } from "../constants"; import { RichTextContent, isNodeParagraphNode } from "../constants";
import { PayloadImage, PayloadMedia } from "../sdk"; import { PayloadImage, PayloadMedia } from "../sdk";
import { Image } from "../types/collections";
export const isDefined = <T>(value: T | null | undefined): value is T => export const isDefined = <T>(value: T | null | undefined): value is T =>
value !== null && value !== undefined; value !== null && value !== undefined;
@ -24,38 +25,18 @@ const isEmptyRichText = (value: RichTextContent) =>
export const hasDuplicates = <T>(list: T[]): boolean => list.length !== new Set(list).size; export const hasDuplicates = <T>(list: T[]): boolean => list.length !== new Set(list).size;
export const isValidPayloadImage = ( export const isValidPayloadImage = (
image: image: string | Image | null | undefined
| { ): image is Image & PayloadImage => {
filename?: string | null;
filesize?: number | null;
mimeType?: string | null;
width?: number | null;
height?: number | null;
url?: string | null;
}
| undefined
| null
| string
): image is PayloadImage => {
if (isUndefined(image)) return false;
if (typeof image === "string") return false; if (typeof image === "string") return false;
if (isEmpty(image.filename)) return false; if (!isValidPayloadMedia(image)) return false;
if (isEmpty(image.url)) return false;
if (isEmpty(image.mimeType)) return false;
if (isUndefined(image.width)) return false; if (isUndefined(image.width)) return false;
if (isUndefined(image.height)) return false; if (isUndefined(image.height)) return false;
if (isUndefined(image.filesize)) return false;
return true; return true;
}; };
export const isValidPayloadMedia = ( export const isValidPayloadMedia = (
media: media:
| { | Partial<{ [K in keyof PayloadMedia]: null | undefined | PayloadMedia[K] }>
filename?: string | null;
filesize?: number | null;
mimeType?: string | null;
url?: string | null;
}
| undefined | undefined
| null | null
| string | string