From 5abf920947bc7a77e626cfc07092fb3234b87fc1 Mon Sep 17 00:00:00 2001 From: Peng Boris Date: Wed, 30 Aug 2023 22:46:57 +0100 Subject: [PATCH] added getMediaSrc --- src/index.ts | 16 ++++++++++++++++ src/types/index.ts | 8 ++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 537dd59..421c087 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { CMSContent, CMSContentPath, CMSContents, + CMSMediaMeta, CMSQueries, NotFoundContents, } from "@/types"; @@ -277,4 +278,19 @@ export class CMSClient { cache )) as CMSContents; } + + /** + * Contructs the URL for a media item based on its type. + * @param {CMSMediaMeta} media - The media item to construct the URL for. + * @returns {string | undefined} The URL for the media item. + */ + public getMediaSrc(media: CMSMediaMeta): string | undefined { + if (media.type === "wagtailimages.Image") { + return this.baseURL + new URL(media.detail_url).pathname; + } else if (media.type === "wagtaildocs.Document") { + return this.baseURL + media.download_url; + } else { + return undefined; + } + } } diff --git a/src/types/index.ts b/src/types/index.ts index db06158..3338d8a 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -import { FetchError } from ".."; +import { FetchError } from "@/lib"; export interface ClientOptions { baseURL: string; @@ -32,16 +32,16 @@ export interface CMSPageMeta { slug: string; type: string; // TODO - locale: "en" | "fr" | "de" | "es" | string; + locale: "en" | "fr" | "de" | "es" | "it" | "ja" | "ko" | "pt" | string; html_url: string; - detail_url?: string; + detail_url: string; seo_title?: string; search_description?: string; } export interface CMSMediaMeta { type: string; - detail_url?: string; + detail_url: string; tags?: string[]; download_url?: string; }