From 48cc52ac24db30b9f0b3697a818e5ce63431f26d Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sat, 15 Jun 2024 14:33:56 +0200 Subject: [PATCH] Reduced media data sent by endpoints --- src/collections/Audios/endpoints/getByID.ts | 42 +++++++----- .../endpoints/getBySlugEndpoint.ts | 14 ++-- .../Folders/endpoints/getBySlugEndpoint.ts | 18 +++-- src/collections/Images/endpoints/getByID.ts | 45 ++++++++----- src/collections/Videos/endpoints/getByID.ts | 65 +++++++++++-------- src/constants.ts | 8 +-- src/sdk.ts | 46 ++++++++++--- src/utils/endpoints.ts | 12 ++-- 8 files changed, 159 insertions(+), 91 deletions(-) diff --git a/src/collections/Audios/endpoints/getByID.ts b/src/collections/Audios/endpoints/getByID.ts index 046737a..8ce5449 100644 --- a/src/collections/Audios/endpoints/getByID.ts +++ b/src/collections/Audios/endpoints/getByID.ts @@ -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), + }; +}; diff --git a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts index 2d752bc..bb3f6f2 100644 --- a/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Collectibles/endpoints/getBySlugEndpoint.ts @@ -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: diff --git a/src/collections/Folders/endpoints/getBySlugEndpoint.ts b/src/collections/Folders/endpoints/getBySlugEndpoint.ts index 8bc8d5a..f4e754f 100644 --- a/src/collections/Folders/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Folders/endpoints/getBySlugEndpoint.ts @@ -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 []; } diff --git a/src/collections/Images/endpoints/getByID.ts b/src/collections/Images/endpoints/getByID.ts index 949f346..d4640f0 100644 --- a/src/collections/Images/endpoints/getByID.ts +++ b/src/collections/Images/endpoints/getByID.ts @@ -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), + }; +}; diff --git a/src/collections/Videos/endpoints/getByID.ts b/src/collections/Videos/endpoints/getByID.ts index 6b3d3de..0548fef 100644 --- a/src/collections/Videos/endpoints/getByID.ts +++ b/src/collections/Videos/endpoints/getByID.ts @@ -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), + }; +}; diff --git a/src/constants.ts b/src/constants.ts index 7d13235..80e67a2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 { diff --git a/src/sdk.ts b/src/sdk.ts index 5f66119..222a4c0 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -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: { diff --git a/src/utils/endpoints.ts b/src/utils/endpoints.ts index 1c67350..983acd9 100644 --- a/src/utils/endpoints.ts +++ b/src/utils/endpoints.ts @@ -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), }; } }