Reduced media data sent by endpoints

This commit is contained in:
DrMint 2024-06-15 14:33:56 +02:00
parent 6e0e8a1831
commit 48cc52ac24
8 changed files with 159 additions and 91 deletions

View File

@ -1,6 +1,6 @@
import payload from "payload";
import { Collections } from "../../../constants";
import { EndpointAudio, PayloadMedia } from "../../../sdk";
import { EndpointAudio, EndpointAudioPreview, PayloadMedia } from "../../../sdk";
import { Audio } from "../../../types/collections";
import { CollectionEndpoint } from "../../../types/payload";
import { isAudio, isMediaThumbnail, isNotEmpty } from "../../../utils/asserts";
@ -47,39 +47,49 @@ export const getByID: CollectionEndpoint = {
},
};
export const convertAudioToEndpointAudio = ({
export const convertAudioToEndpointAudioPreview = ({
url,
attributes,
translations,
mimeType,
createdAt,
updatedAt,
filename,
filesize,
duration,
id,
thumbnail,
credits,
}: Audio & PayloadMedia): EndpointAudio => ({
url,
attributes: convertAttributesToEndpointAttributes(attributes),
createdAt,
filename,
filesize,
}: Audio & PayloadMedia): EndpointAudioPreview => ({
id,
url,
filename,
mimeType,
updatedAt,
attributes: convertAttributesToEndpointAttributes(attributes),
translations:
translations?.map(({ language, title, pretitle, subtitle, description }) => ({
translations?.map(({ language, title, pretitle, subtitle }) => ({
language: getLanguageId(language),
...(isNotEmpty(pretitle) ? { pretitle } : {}),
title,
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description: convertRTCToEndpointRTC(description) } : {}),
})) ?? [],
duration,
...(isMediaThumbnail(thumbnail)
? { thumbnail: convertMediaThumbnailToEndpointPayloadImage(thumbnail) }
: {}),
credits: convertCreditsToEndpointCredits(credits),
});
const convertAudioToEndpointAudio = (audio: Audio & PayloadMedia): EndpointAudio => {
const { translations, createdAt, updatedAt, filesize, credits } = audio;
return {
...convertAudioToEndpointAudioPreview(audio),
createdAt,
filesize,
updatedAt,
translations:
translations?.map(({ language, title, pretitle, subtitle, description }) => ({
language: getLanguageId(language),
...(isNotEmpty(pretitle) ? { pretitle } : {}),
title,
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description: convertRTCToEndpointRTC(description) } : {}),
})) ?? [],
credits: convertCreditsToEndpointCredits(credits),
};
};

View File

@ -20,10 +20,10 @@ import {
convertSourceToEndpointSource,
getDomainFromUrl,
} from "../../../utils/endpoints";
import { convertAudioToEndpointAudio } from "../../Audios/endpoints/getByID";
import { convertAudioToEndpointAudioPreview } from "../../Audios/endpoints/getByID";
import { convertPageToEndpointPagePreview } from "../../Pages/endpoints/getBySlugEndpoint";
import { convertRecorderToEndpointRecorderPreview } from "../../Recorders/endpoints/getByID";
import { convertVideoToEndpointVideo } from "../../Videos/endpoints/getByID";
import { convertVideoToEndpointVideoPreview } from "../../Videos/endpoints/getByID";
export const getBySlugEndpoint = createGetByEndpoint({
collection: Collections.Collectibles,
@ -256,12 +256,18 @@ const handleContents = (contents: Collectible["contents"]): EndpointCollectible[
case Collections.Audios:
return isAudio(content.value)
? { relationTo: Collections.Audios, value: convertAudioToEndpointAudio(content.value) }
? {
relationTo: Collections.Audios,
value: convertAudioToEndpointAudioPreview(content.value),
}
: undefined;
case Collections.Videos:
return isVideo(content.value)
? { relationTo: Collections.Videos, value: convertVideoToEndpointVideo(content.value) }
? {
relationTo: Collections.Videos,
value: convertVideoToEndpointVideoPreview(content.value),
}
: undefined;
default:

View File

@ -12,11 +12,11 @@ import {
isVideo,
} from "../../../utils/asserts";
import { convertSourceToEndpointSource, getLanguageId } from "../../../utils/endpoints";
import { convertAudioToEndpointAudio } from "../../Audios/endpoints/getByID";
import { convertAudioToEndpointAudioPreview } from "../../Audios/endpoints/getByID";
import { convertCollectibleToEndpointCollectiblePreview } from "../../Collectibles/endpoints/getBySlugEndpoint";
import { convertImageToEndpointImage } from "../../Images/endpoints/getByID";
import { convertImageToEndpointImagePreview } from "../../Images/endpoints/getByID";
import { convertPageToEndpointPagePreview } from "../../Pages/endpoints/getBySlugEndpoint";
import { convertVideoToEndpointVideo } from "../../Videos/endpoints/getByID";
import { convertVideoToEndpointVideoPreview } from "../../Videos/endpoints/getByID";
export const getBySlugEndpoint = createGetByEndpoint({
collection: Collections.Folders,
@ -92,13 +92,19 @@ const convertFolderToEndpointFolder = (folder: Folder): EndpointFolder => {
];
case Collections.Images:
if (!isImage(value)) return [];
return [{ relationTo: Collections.Images, value: convertImageToEndpointImage(value) }];
return [
{ relationTo: Collections.Images, value: convertImageToEndpointImagePreview(value) },
];
case Collections.Audios:
if (!isAudio(value)) return [];
return [{ relationTo: Collections.Audios, value: convertAudioToEndpointAudio(value) }];
return [
{ relationTo: Collections.Audios, value: convertAudioToEndpointAudioPreview(value) },
];
case Collections.Videos:
if (!isVideo(value)) return [];
return [{ relationTo: Collections.Videos, value: convertVideoToEndpointVideo(value) }];
return [
{ relationTo: Collections.Videos, value: convertVideoToEndpointVideoPreview(value) },
];
default:
return [];
}

View File

@ -1,6 +1,6 @@
import payload from "payload";
import { Collections } from "../../../constants";
import { EndpointImage, PayloadImage } from "../../../sdk";
import { EndpointImage, EndpointImagePreview, PayloadImage } from "../../../sdk";
import { Image } from "../../../types/collections";
import { CollectionEndpoint } from "../../../types/payload";
import { isImage, isNotEmpty, isPayloadImage } from "../../../utils/asserts";
@ -47,40 +47,32 @@ export const getByID: CollectionEndpoint = {
},
};
export const convertImageToEndpointImage = ({
export const convertImageToEndpointImagePreview = ({
url,
width,
height,
attributes,
translations,
mimeType,
createdAt,
updatedAt,
filename,
filesize,
id,
credits,
sizes,
}: Image & PayloadImage): EndpointImage => ({
url,
width,
height,
attributes: convertAttributesToEndpointAttributes(attributes),
createdAt,
filename,
filesize,
}: Image & PayloadImage): EndpointImagePreview => ({
id,
url,
filename,
mimeType,
updatedAt,
attributes: convertAttributesToEndpointAttributes(attributes),
translations:
translations?.map(({ language, title, pretitle, subtitle, description }) => ({
translations?.map(({ language, title, pretitle, subtitle }) => ({
language: getLanguageId(language),
...(isNotEmpty(pretitle) ? { pretitle } : {}),
title,
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description: convertRTCToEndpointRTC(description) } : {}),
})) ?? [],
credits: convertCreditsToEndpointCredits(credits),
width,
height,
sizes: convertSizesToPayloadImages(
[
sizes?.["200w"],
@ -96,3 +88,22 @@ export const convertImageToEndpointImage = ({
),
...(isPayloadImage(sizes?.og) ? { openGraph: sizes.og } : {}),
});
export const convertImageToEndpointImage = (image: Image & PayloadImage): EndpointImage => {
const { translations, createdAt, updatedAt, filesize, credits } = image;
return {
...convertImageToEndpointImagePreview(image),
createdAt,
filesize,
updatedAt,
translations:
translations?.map(({ language, title, pretitle, subtitle, description }) => ({
language: getLanguageId(language),
...(isNotEmpty(pretitle) ? { pretitle } : {}),
title,
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description: convertRTCToEndpointRTC(description) } : {}),
})) ?? [],
credits: convertCreditsToEndpointCredits(credits),
};
};

View File

@ -1,6 +1,6 @@
import payload from "payload";
import { Collections } from "../../../constants";
import { EndpointVideo, PayloadMedia } from "../../../sdk";
import { EndpointVideo, EndpointVideoPreview, PayloadMedia } from "../../../sdk";
import { Video } from "../../../types/collections";
import { CollectionEndpoint } from "../../../types/payload";
import {
@ -55,52 +55,32 @@ export const getByID: CollectionEndpoint = {
},
};
export const convertVideoToEndpointVideo = ({
export const convertVideoToEndpointVideoPreview = ({
url,
attributes,
translations,
mimeType,
createdAt,
updatedAt,
filename,
filesize,
duration,
id,
thumbnail,
platform,
platformEnabled,
credits,
}: Video & PayloadMedia): EndpointVideo => ({
url,
attributes: convertAttributesToEndpointAttributes(attributes),
createdAt,
filename,
filesize,
}: Video & PayloadMedia): EndpointVideoPreview => ({
id,
url,
filename,
mimeType,
updatedAt,
attributes: convertAttributesToEndpointAttributes(attributes),
translations:
translations?.map(({ language, title, pretitle, subtitle, description }) => ({
translations?.map(({ language, title, pretitle, subtitle }) => ({
language: getLanguageId(language),
...(isNotEmpty(pretitle) ? { pretitle } : {}),
title,
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description: convertRTCToEndpointRTC(description) } : {}),
})) ?? [],
duration,
...(isMediaThumbnail(thumbnail)
? { thumbnail: convertMediaThumbnailToEndpointPayloadImage(thumbnail) }
: {}),
...(platformEnabled && isDefined(platform) && isPayloadType(platform.channel)
? {
platform: {
channel: platform.channel,
publishedDate: platform.publishedDate,
url: platform.url,
},
}
: {}),
subtitles:
translations.flatMap(({ language, subfile }) => {
if (
@ -112,5 +92,34 @@ export const convertVideoToEndpointVideo = ({
return [];
return { language: getLanguageId(language), url: subfile.url };
}) ?? [],
credits: convertCreditsToEndpointCredits(credits),
});
const convertVideoToEndpointVideo = (video: Video & PayloadMedia): EndpointVideo => {
const { translations, createdAt, updatedAt, filesize, platform, platformEnabled, credits } =
video;
return {
...convertVideoToEndpointVideoPreview(video),
createdAt,
filesize,
updatedAt,
translations:
translations?.map(({ language, title, pretitle, subtitle, description }) => ({
language: getLanguageId(language),
...(isNotEmpty(pretitle) ? { pretitle } : {}),
title,
...(isNotEmpty(subtitle) ? { subtitle } : {}),
...(isNotEmpty(description) ? { description: convertRTCToEndpointRTC(description) } : {}),
})) ?? [],
...(platformEnabled && isDefined(platform) && isPayloadType(platform.channel)
? {
platform: {
channel: platform.channel,
publishedDate: platform.publishedDate,
url: platform.url,
},
}
: {}),
credits: convertCreditsToEndpointCredits(credits),
};
};

View File

@ -1,4 +1,4 @@
import { EndpointAudio, EndpointImage, EndpointVideo } from "./sdk";
import { EndpointAudioPreview, EndpointImagePreview, EndpointVideoPreview } from "./sdk";
import type { BreakBlock, SectionBlock, TranscriptBlock } from "./types/collections";
// END MOCKING SECTION
@ -144,17 +144,17 @@ export interface RichTextUploadNode extends RichTextNode {
export interface RichTextUploadImageNode extends RichTextUploadNode {
relationTo: Collections.Images;
value: EndpointImage;
value: EndpointImagePreview;
}
export interface RichTextUploadVideoNode extends RichTextUploadNode {
relationTo: Collections.Videos;
value: EndpointVideo;
value: EndpointVideoPreview;
}
export interface RichTextUploadAudioNode extends RichTextUploadNode {
relationTo: Collections.Audios;
value: EndpointAudio;
value: EndpointAudioPreview;
}
export interface RichTextTextNode extends RichTextNode {

View File

@ -44,15 +44,15 @@ export type EndpointFolder = EndpointFolderPreview & {
}
| {
relationTo: Collections.Images;
value: EndpointImage;
value: EndpointImagePreview;
}
| {
relationTo: Collections.Audios;
value: EndpointAudio;
value: EndpointAudioPreview;
}
| {
relationTo: Collections.Videos;
value: EndpointVideo;
value: EndpointVideoPreview;
}
)[];
parentPages: EndpointSource[];
@ -231,11 +231,11 @@ export type EndpointCollectible = EndpointCollectiblePreview & {
}
| {
relationTo: Collections.Audios;
value: EndpointAudio;
value: EndpointAudioPreview;
}
| {
relationTo: Collections.Videos;
value: EndpointVideo;
value: EndpointVideoPreview;
}
| {
relationTo: Collections.GenericContents;
@ -420,25 +420,37 @@ export type EndpointSource =
| { type: "scans"; collectible: EndpointSourcePreview }
| { type: "gallery"; collectible: EndpointSourcePreview };
export type EndpointMedia = {
export type EndpointMediaPreview = {
id: string;
url: string;
filename: string;
mimeType: string;
filesize: number;
updatedAt: string;
createdAt: string;
attributes: EndpointAttribute[];
translations: {
language: string;
pretitle?: string;
title: string;
subtitle?: string;
description?: RichTextContent;
}[];
};
export type EndpointMedia = EndpointMediaPreview & {
filesize: number;
updatedAt: string;
createdAt: string;
translations: (EndpointMediaPreview["translations"][number] & {
description?: RichTextContent;
})[];
credits: EndpointCredit[];
};
export type EndpointImagePreview = EndpointMediaPreview & {
width: number;
height: number;
sizes: PayloadImage[];
openGraph?: PayloadImage;
};
export type EndpointImage = EndpointMedia & {
width: number;
height: number;
@ -446,11 +458,25 @@ export type EndpointImage = EndpointMedia & {
openGraph?: PayloadImage;
};
export type EndpointAudioPreview = EndpointMediaPreview & {
thumbnail?: EndpointPayloadImage;
duration: number;
};
export type EndpointAudio = EndpointMedia & {
thumbnail?: EndpointPayloadImage;
duration: number;
};
export type EndpointVideoPreview = EndpointMediaPreview & {
thumbnail?: EndpointPayloadImage;
subtitles: {
language: string;
url: string;
}[];
duration: number;
};
export type EndpointVideo = EndpointMedia & {
thumbnail?: EndpointPayloadImage;
subtitles: {

View File

@ -1,7 +1,7 @@
import { convertAudioToEndpointAudio } from "../collections/Audios/endpoints/getByID";
import { convertImageToEndpointImage } from "../collections/Images/endpoints/getByID";
import { convertAudioToEndpointAudioPreview } from "../collections/Audios/endpoints/getByID";
import { convertImageToEndpointImagePreview } from "../collections/Images/endpoints/getByID";
import { convertRecorderToEndpointRecorderPreview } from "../collections/Recorders/endpoints/getByID";
import { convertVideoToEndpointVideo } from "../collections/Videos/endpoints/getByID";
import { convertVideoToEndpointVideoPreview } from "../collections/Videos/endpoints/getByID";
import {
AttributeTypes,
RichTextBreakBlock,
@ -110,21 +110,21 @@ export const convertRTCToEndpointRTC = (
if (!isImage(value)) return errorUploadNode;
return {
...node,
value: convertImageToEndpointImage(value),
value: convertImageToEndpointImagePreview(value),
};
} else if (isUploadNodeAudioNode(node)) {
const value = node.value as unknown as Audio | string;
if (!isAudio(value)) return errorUploadNode;
return {
...node,
value: convertAudioToEndpointAudio(value),
value: convertAudioToEndpointAudioPreview(value),
};
} else if (isUploadNodeVideoNode(node)) {
const value = node.value as unknown as Video | string;
if (!isVideo(value)) return errorUploadNode;
return {
...node,
value: convertVideoToEndpointVideo(value),
value: convertVideoToEndpointVideoPreview(value),
};
}
}