Skip to content

Commit

Permalink
added getMediaSrc
Browse files Browse the repository at this point in the history
  • Loading branch information
itzomen committed Aug 30, 2023
1 parent 27c4c0a commit 5abf920
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CMSContent,
CMSContentPath,
CMSContents,
CMSMediaMeta,
CMSQueries,
NotFoundContents,
} from "@/types";
Expand Down Expand Up @@ -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;
}
}
}
8 changes: 4 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchError } from "..";
import { FetchError } from "@/lib";

export interface ClientOptions {
baseURL: string;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5abf920

Please sign in to comment.