Reduced image data sent by endpoints

This commit is contained in:
DrMint 2024-06-15 14:09:31 +02:00
parent bf5b98b690
commit 6e0e8a1831
12 changed files with 85 additions and 57 deletions

View File

@ -7,7 +7,7 @@ import { isAudio, isMediaThumbnail, isNotEmpty } from "../../../utils/asserts";
import {
convertAttributesToEndpointAttributes,
convertCreditsToEndpointCredits,
convertMediaThumbnailToEndpointMediaThumbnail,
convertMediaThumbnailToEndpointPayloadImage,
convertRTCToEndpointRTC,
getLanguageId,
} from "../../../utils/endpoints";
@ -79,7 +79,7 @@ export const convertAudioToEndpointAudio = ({
})) ?? [],
duration,
...(isMediaThumbnail(thumbnail)
? { thumbnail: convertMediaThumbnailToEndpointMediaThumbnail(thumbnail) }
? { thumbnail: convertMediaThumbnailToEndpointPayloadImage(thumbnail) }
: {}),
credits: convertCreditsToEndpointCredits(credits),
});

View File

@ -15,12 +15,12 @@ import {
} from "../../../utils/asserts";
import {
convertAttributesToEndpointAttributes,
convertImageToEndpointPayloadImage,
convertScanToEndpointScanImage,
convertSourceToEndpointSource,
getDomainFromUrl,
} from "../../../utils/endpoints";
import { convertAudioToEndpointAudio } from "../../Audios/endpoints/getByID";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
import { convertPageToEndpointPagePreview } from "../../Pages/endpoints/getBySlugEndpoint";
import { convertRecorderToEndpointRecorderPreview } from "../../Recorders/endpoints/getByID";
import { convertVideoToEndpointVideo } from "../../Videos/endpoints/getByID";
@ -45,7 +45,7 @@ export const convertCollectibleToEndpointCollectiblePreview = ({
}: Collectible): EndpointCollectiblePreview => ({
id,
slug,
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointImage(thumbnail) } : {}),
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointPayloadImage(thumbnail) } : {}),
translations:
translations?.map(({ language, title, pretitle, subtitle }) => ({
language: isPayloadType(language) ? language.id : language,
@ -98,7 +98,7 @@ const convertCollectibleToEndpointCollectible = (collectible: Collectible): Endp
...(isNotEmpty(description) ? { description } : {}),
})) ?? [],
...(isImage(backgroundImage)
? { backgroundImage: convertImageToEndpointImage(backgroundImage) }
? { backgroundImage: convertImageToEndpointPayloadImage(backgroundImage) }
: {}),
nature: nature === "Physical" ? CollectibleNature.Physical : CollectibleNature.Digital,
...(gallery ? { gallery } : {}),
@ -162,7 +162,7 @@ const handlePageInfo = (
const handleGallery = (gallery: Collectible["gallery"]): EndpointCollectible["gallery"] => {
const thumbnail = gallery?.[0]?.image;
if (!thumbnail || !isImage(thumbnail)) return;
return { count: gallery.length, thumbnail: convertImageToEndpointImage(thumbnail) };
return { count: gallery.length, thumbnail: convertImageToEndpointPayloadImage(thumbnail) };
};
const handleScans = (scans: Collectible["scans"]): EndpointCollectible["scans"] => {

View File

@ -2,8 +2,10 @@ import { Collections } from "../../../constants";
import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint";
import { EndpointCollectibleGallery } from "../../../sdk";
import { isImage, isNotEmpty, isPayloadType } from "../../../utils/asserts";
import { convertSourceToEndpointSource } from "../../../utils/endpoints";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
import {
convertImageToEndpointPayloadImage,
convertSourceToEndpointSource,
} from "../../../utils/endpoints";
export const getBySlugEndpointGallery = createGetByEndpoint({
collection: Collections.Collectibles,
@ -22,10 +24,10 @@ export const getBySlugEndpointGallery = createGetByEndpoint({
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description } : {}),
})) ?? [],
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointImage(thumbnail) } : {}),
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointPayloadImage(thumbnail) } : {}),
images:
gallery?.flatMap(({ image }) =>
isImage(image) ? convertImageToEndpointImage(image) : []
isImage(image) ? convertImageToEndpointPayloadImage(image) : []
) ?? [],
parentPages: convertSourceToEndpointSource({ collectibles: [collectible] }),
};

View File

@ -54,9 +54,6 @@ export const getBySlugEndpointGalleryImage: CollectionEndpoint = {
image: convertImageToEndpointImage(image),
parentPages: convertSourceToEndpointSource({ gallery: [collectible] }),
slug,
...(isImage(collectible.thumbnail)
? { thumbnail: convertImageToEndpointImage(collectible.thumbnail) }
: {}),
translations:
collectible.translations?.map(({ language, title, description, pretitle, subtitle }) => ({
language: isPayloadType(language) ? language.id : language,

View File

@ -3,12 +3,11 @@ import { Collections } from "../../../constants";
import { EndpointCollectibleScanPage } from "../../../sdk";
import { Collectible, Scan } from "../../../types/collections";
import { CollectionEndpoint } from "../../../types/payload";
import { isDefined, isImage, isNotEmpty, isPayloadType, isScan } from "../../../utils/asserts";
import { isDefined, isNotEmpty, isPayloadType, isScan } from "../../../utils/asserts";
import {
convertScanToEndpointScanImage,
convertSourceToEndpointSource,
} from "../../../utils/endpoints";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getBySlugEndpointScanPage: CollectionEndpoint = {
path: "/slug/:slug/scans/:index",
@ -57,9 +56,6 @@ export const getBySlugEndpointScanPage: CollectionEndpoint = {
image: convertScanToEndpointScanImage(scan, index),
parentPages: convertSourceToEndpointSource({ scans: [collectible] }),
slug,
...(isImage(collectible.thumbnail)
? { thumbnail: convertImageToEndpointImage(collectible.thumbnail) }
: {}),
translations:
collectible.translations?.map(({ language, title, description, pretitle, subtitle }) => ({
language: isPayloadType(language) ? language.id : language,

View File

@ -5,10 +5,10 @@ import { Collectible } from "../../../types/collections";
import { isImage, isNotEmpty, isPayloadType, isScan } from "../../../utils/asserts";
import {
convertCreditsToEndpointCredits,
convertImageToEndpointPayloadImage,
convertScanToEndpointScanImage,
convertSourceToEndpointSource,
} from "../../../utils/endpoints";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getBySlugEndpointScans = createGetByEndpoint({
collection: Collections.Collectibles,
@ -27,7 +27,7 @@ export const getBySlugEndpointScans = createGetByEndpoint({
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description } : {}),
})) ?? [],
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointImage(thumbnail) } : {}),
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointPayloadImage(thumbnail) } : {}),
...(scansEnabled && scans ? handleScans(scans) : { credits: [], pages: [] }),
parentPages: convertSourceToEndpointSource({ collectibles: [collectible] }),
};

View File

@ -13,10 +13,10 @@ import { isImage, isNotEmpty, isPayloadType } from "../../../utils/asserts";
import {
convertAttributesToEndpointAttributes,
convertCreditsToEndpointCredits,
convertImageToEndpointPayloadImage,
convertRTCToEndpointRTC,
convertSourceToEndpointSource,
} from "../../../utils/endpoints";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
import { convertRecorderToEndpointRecorderPreview } from "../../Recorders/endpoints/getByID";
export const getBySlugEndpoint = createGetByEndpoint({
@ -35,7 +35,7 @@ export const convertPageToEndpointPagePreview = ({
}: Page): EndpointPagePreview => ({
id,
slug,
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointImage(thumbnail) } : {}),
...(isImage(thumbnail) ? { thumbnail: convertImageToEndpointPayloadImage(thumbnail) } : {}),
attributes: convertAttributesToEndpointAttributes(attributes),
translations: translations.map(({ language, title, pretitle, subtitle }) => ({
language: isPayloadType(language) ? language.id : language,
@ -52,7 +52,7 @@ const convertPageToEndpointPage = (page: Page): EndpointPage => {
return {
...convertPageToEndpointPagePreview(page),
...(isImage(backgroundImage)
? { backgroundImage: convertImageToEndpointImage(backgroundImage) }
? { backgroundImage: convertImageToEndpointPayloadImage(backgroundImage) }
: {}),
translations: translations.map(
({ content, language, sourceLanguage, title, pretitle, subtitle, summary, credits }) => ({

View File

@ -4,8 +4,10 @@ import { EndpointRecorder, EndpointRecorderPreview } from "../../../sdk";
import { Recorder } from "../../../types/collections";
import { CollectionEndpoint } from "../../../types/payload";
import { isImage, isPayloadType } from "../../../utils/asserts";
import { convertRTCToEndpointRTC } from "../../../utils/endpoints";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
import {
convertImageToEndpointPayloadImage,
convertRTCToEndpointRTC,
} from "../../../utils/endpoints";
export const getByID: CollectionEndpoint = {
method: "get",
@ -53,7 +55,7 @@ const convertRecorderToEndpointRecorder = (recorder: Recorder): EndpointRecorder
...convertRecorderToEndpointRecorderPreview(recorder),
languages:
languages?.map((language) => (isPayloadType(language) ? language.id : language)) ?? [],
...(isImage(avatar) ? { avatar: convertImageToEndpointImage(avatar) } : {}),
...(isImage(avatar) ? { avatar: convertImageToEndpointPayloadImage(avatar) } : {}),
translations:
translations?.map(({ language, biography }) => ({
language: isPayloadType(language) ? language.id : language,

View File

@ -15,7 +15,7 @@ import {
import {
convertAttributesToEndpointAttributes,
convertCreditsToEndpointCredits,
convertMediaThumbnailToEndpointMediaThumbnail,
convertMediaThumbnailToEndpointPayloadImage,
convertRTCToEndpointRTC,
getLanguageId,
} from "../../../utils/endpoints";
@ -90,7 +90,7 @@ export const convertVideoToEndpointVideo = ({
duration,
...(isMediaThumbnail(thumbnail)
? { thumbnail: convertMediaThumbnailToEndpointMediaThumbnail(thumbnail) }
? { thumbnail: convertMediaThumbnailToEndpointPayloadImage(thumbnail) }
: {}),
...(platformEnabled && isDefined(platform) && isPayloadType(platform.channel)
? {

View File

@ -3,8 +3,8 @@ import { Collections } from "../../../constants";
import { EndpointWebsiteConfig } from "../../../sdk";
import { CollectionEndpoint } from "../../../types/payload";
import { isImage, isPayloadType } from "../../../utils/asserts";
import { convertImageToEndpointPayloadImage } from "../../../utils/endpoints";
import { convertFolderToEndpointFolderPreview } from "../../Folders/endpoints/getBySlugEndpoint";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
export const getConfigEndpoint: CollectionEndpoint = {
method: "get",
@ -51,7 +51,7 @@ export const getConfigEndpoint: CollectionEndpoint = {
const endpointWebsiteConfig: EndpointWebsiteConfig = {
home: {
...(isImage(homeBackgroundImage)
? { backgroundImage: convertImageToEndpointImage(homeBackgroundImage) }
? { backgroundImage: convertImageToEndpointPayloadImage(homeBackgroundImage) }
: {}),
folders:
homeFolders?.flatMap(({ folder, darkThumbnail, lightThumbnail }) => {
@ -59,17 +59,17 @@ export const getConfigEndpoint: CollectionEndpoint = {
return {
...convertFolderToEndpointFolderPreview(folder),
...(isImage(darkThumbnail)
? { darkThumbnail: convertImageToEndpointImage(darkThumbnail) }
? { darkThumbnail: convertImageToEndpointPayloadImage(darkThumbnail) }
: {}),
...(isImage(lightThumbnail)
? { lightThumbnail: convertImageToEndpointImage(lightThumbnail) }
? { lightThumbnail: convertImageToEndpointPayloadImage(lightThumbnail) }
: {}),
};
}) ?? [],
},
timeline: {
...(isImage(timelineBackgroundImage)
? { backgroundImage: convertImageToEndpointImage(timelineBackgroundImage) }
? { backgroundImage: convertImageToEndpointPayloadImage(timelineBackgroundImage) }
: {}),
breaks: timeline?.breaks ?? [],
eventCount,
@ -84,7 +84,7 @@ export const getConfigEndpoint: CollectionEndpoint = {
}) ?? [],
},
...(isImage(defaultOpenGraphImage)
? { defaultOpenGraphImage: convertImageToEndpointImage(defaultOpenGraphImage) }
? { defaultOpenGraphImage: convertImageToEndpointPayloadImage(defaultOpenGraphImage) }
: {}),
};
res.status(200).json(endpointWebsiteConfig);

View File

@ -60,14 +60,14 @@ export type EndpointFolder = EndpointFolderPreview & {
export type EndpointWebsiteConfig = {
home: {
backgroundImage?: EndpointImage;
backgroundImage?: EndpointPayloadImage;
folders: (EndpointFolderPreview & {
lightThumbnail?: EndpointImage;
darkThumbnail?: EndpointImage;
lightThumbnail?: EndpointPayloadImage;
darkThumbnail?: EndpointPayloadImage;
})[];
};
timeline: {
backgroundImage?: EndpointImage;
backgroundImage?: EndpointPayloadImage;
breaks: number[];
eventCount: number;
eras: {
@ -76,7 +76,7 @@ export type EndpointWebsiteConfig = {
name: string;
}[];
};
defaultOpenGraphImage?: EndpointImage;
defaultOpenGraphImage?: EndpointPayloadImage;
};
export type EndpointRecorderPreview = {
@ -85,7 +85,7 @@ export type EndpointRecorderPreview = {
};
export type EndpointRecorder = EndpointRecorderPreview & {
avatar?: EndpointImage;
avatar?: EndpointPayloadImage;
translations: {
language: string;
biography: RichTextContent;
@ -158,7 +158,7 @@ export type EndpointCredit = {
export type EndpointPagePreview = {
id: string;
slug: string;
thumbnail?: EndpointImage;
thumbnail?: EndpointPayloadImage;
attributes: EndpointAttribute[];
translations: {
language: string;
@ -170,7 +170,7 @@ export type EndpointPagePreview = {
};
export type EndpointPage = EndpointPagePreview & {
backgroundImage?: EndpointImage;
backgroundImage?: EndpointPayloadImage;
translations: (EndpointPagePreview["translations"][number] & {
sourceLanguage: string;
summary?: RichTextContent;
@ -186,7 +186,7 @@ export type EndpointPage = EndpointPagePreview & {
export type EndpointCollectiblePreview = {
id: string;
slug: string;
thumbnail?: EndpointImage;
thumbnail?: EndpointPayloadImage;
translations: {
language: string;
pretitle?: string;
@ -206,10 +206,10 @@ export type EndpointCollectible = EndpointCollectiblePreview & {
translations: (EndpointCollectiblePreview["translations"][number] & {
description?: RichTextContent;
})[];
backgroundImage?: EndpointImage;
backgroundImage?: EndpointPayloadImage;
nature: CollectibleNature;
gallery?: { count: number; thumbnail: EndpointImage };
scans?: { count: number; thumbnail: EndpointScanImage };
gallery?: { count: number; thumbnail: EndpointPayloadImage };
scans?: { count: number; thumbnail: EndpointPayloadImage };
urls: { url: string; label: string }[];
size?: {
width: number;
@ -274,7 +274,7 @@ export type EndpointCollectible = EndpointCollectiblePreview & {
export type EndpointCollectibleScans = {
slug: string;
thumbnail?: EndpointImage;
thumbnail?: EndpointPayloadImage;
translations: {
language: string;
pretitle?: string;
@ -324,7 +324,7 @@ export type EndpointCollectibleScans = {
export type EndpointCollectibleGallery = {
slug: string;
thumbnail?: EndpointImage;
thumbnail?: EndpointPayloadImage;
translations: {
language: string;
pretitle?: string;
@ -332,13 +332,12 @@ export type EndpointCollectibleGallery = {
subtitle?: string;
description?: RichTextContent;
}[];
images: EndpointImage[];
images: EndpointPayloadImage[];
parentPages: EndpointSource[];
};
export type EndpointCollectibleGalleryImage = {
slug: string;
thumbnail?: EndpointImage;
translations: {
language: string;
pretitle?: string;
@ -354,7 +353,6 @@ export type EndpointCollectibleGalleryImage = {
export type EndpointCollectibleScanPage = {
slug: string;
thumbnail?: EndpointImage;
translations: {
language: string;
pretitle?: string;
@ -449,12 +447,12 @@ export type EndpointImage = EndpointMedia & {
};
export type EndpointAudio = EndpointMedia & {
thumbnail?: EndpointMediaThumbnail;
thumbnail?: EndpointPayloadImage;
duration: number;
};
export type EndpointVideo = EndpointMedia & {
thumbnail?: EndpointMediaThumbnail;
thumbnail?: EndpointPayloadImage;
subtitles: {
language: string;
url: string;
@ -474,7 +472,7 @@ export type EndpointVideo = EndpointMedia & {
duration: number;
};
export type EndpointMediaThumbnail = PayloadImage & {
export type EndpointPayloadImage = PayloadImage & {
sizes: PayloadImage[];
openGraph?: PayloadImage;
};

View File

@ -19,7 +19,7 @@ import {
import {
EndpointAttribute,
EndpointCredit,
EndpointMediaThumbnail,
EndpointPayloadImage,
EndpointRole,
EndpointScanImage,
EndpointSource,
@ -367,7 +367,40 @@ export const convertScanToEndpointScanImage = (
),
});
export const convertMediaThumbnailToEndpointMediaThumbnail = ({
export const convertImageToEndpointPayloadImage = ({
url,
width,
height,
mimeType,
filename,
filesize,
id,
sizes,
}: Image & PayloadImage): EndpointPayloadImage => ({
filename,
filesize,
height,
id,
mimeType,
sizes: convertSizesToPayloadImages(
[
sizes?.["200w"],
sizes?.["320w"],
sizes?.["480w"],
sizes?.["800w"],
sizes?.["1280w"],
sizes?.["1920w"],
sizes?.["2560w"],
{ url, width, height, filename, filesize, mimeType },
],
[200, 320, 480, 800, 1280, 1920, 2560]
),
url,
width,
...(isPayloadImage(sizes?.og) ? { openGraph: sizes.og } : {}),
});
export const convertMediaThumbnailToEndpointPayloadImage = ({
id,
url,
width,
@ -376,7 +409,7 @@ export const convertMediaThumbnailToEndpointMediaThumbnail = ({
filename,
filesize,
sizes,
}: MediaThumbnail & PayloadImage): EndpointMediaThumbnail => ({
}: MediaThumbnail & PayloadImage): EndpointPayloadImage => ({
id,
url,
width,