From f9e2109c27af5ece546261c018d4b2781860ff1c Mon Sep 17 00:00:00 2001 From: vnugent Date: Fri, 15 Mar 2024 17:00:41 -0400 Subject: feat: Client lib now supports path prefix --- lib/client/src/channels.ts | 12 +++++++----- lib/client/src/content.ts | 13 ++++++++----- lib/client/src/posts.ts | 14 +++++++------- lib/client/src/types.ts | 7 ++++++- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/client/src/channels.ts b/lib/client/src/channels.ts index d5dcc8f..337b8da 100644 --- a/lib/client/src/channels.ts +++ b/lib/client/src/channels.ts @@ -1,4 +1,4 @@ -// Copyright (C) 2023 Vaughn Nugent +// Copyright (C) 2024 Vaughn Nugent // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -13,7 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { find, isEqual } from 'lodash-es'; +import { defaultTo, find, isEqual } from 'lodash-es'; import { CMNextApi, CMNextIndex, ChannelMeta } from './types' export interface ChannelApi extends CMNextApi { @@ -56,9 +56,11 @@ export interface ScopedChannelApi extends ChannelApi { getContentDir(): Promise; } -export const createScopedChannelApi = (channelFile: string, channelId: string): ScopedChannelApi => { +export const createScopedChannelApi = (channelFile: string, channelId: string, urlPrefix? :string): ScopedChannelApi => { const channelApi = createChannelApi(channelFile); + + urlPrefix = defaultTo(urlPrefix, ''); const getSelectedChannel = async (): Promise => { const index = await channelApi.getIndex() @@ -72,7 +74,7 @@ export const createScopedChannelApi = (channelFile: string, channelId: string): const getPostIndexPath = async (): Promise => { //Await the selected channel index const channel = await index - return channel ? `${channel.path}/${channel.index}` : undefined; + return channel ? `${urlPrefix}${channel.path}/${channel.index}` : undefined; } const getContentDir = async (): Promise => { @@ -91,7 +93,7 @@ export const createScopedChannelApi = (channelFile: string, channelId: string): //Await the selected channel index const channel = await index //Get the post index from the channel - return channel ? `${channel.path}/content.json` : undefined; + return channel ? `${urlPrefix}${channel.path}/content.json` : undefined; } return{ diff --git a/lib/client/src/content.ts b/lib/client/src/content.ts index c1a62aa..9626c65 100644 --- a/lib/client/src/content.ts +++ b/lib/client/src/content.ts @@ -1,4 +1,4 @@ -// Copyright (C) 2023 Vaughn Nugent +// Copyright (C) 2024 Vaughn Nugent // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -13,7 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { startsWith } from 'lodash-es'; +import { defaultTo, startsWith } from 'lodash-es'; import { CMNextApi, CMNextAutoConfig, CMNextEntity, CMNextIndex, ContentMeta } from "./types"; import { createScopedChannelApi } from "./channels"; @@ -88,9 +88,11 @@ export const createManualContentApi = ({ channelRootDir, contentIndexPath, conte * @param param0 The CMNext configuration for the auto content api * @returns The automatic discovery content api */ -export const createAutoContentApi = ({ cmsChannelIndexPath, channelId }: CMNextAutoConfig): ContentApi => { +export const createAutoContentApi = ({ cmsChannelIndexPath, channelId, urlPrefix }: CMNextAutoConfig): ContentApi => { - const channelApi = createScopedChannelApi(cmsChannelIndexPath, channelId); + urlPrefix = defaultTo(urlPrefix, ''); + + const channelApi = createScopedChannelApi(cmsChannelIndexPath, channelId, urlPrefix); const getIndex = async () : Promise> => { //Get the content index path from the channel api @@ -106,7 +108,8 @@ export const createAutoContentApi = ({ cmsChannelIndexPath, channelId }: CMNextA const getContentUrl = async (item: ContentMeta): Promise => { //Content resides in the content dir within the channel dir const contentDir = await channelApi.getContentDir(); - return contentDir ? `${contentDir}/${item.path}` : '' + const baseDir = await channelApi.getBaseDir(); + return contentDir ? `${urlPrefix}${baseDir}/${contentDir}/${item.path}` : '' } const getStringContent = async (item: ContentMeta): Promise => { diff --git a/lib/client/src/posts.ts b/lib/client/src/posts.ts index d2c8791..63819a1 100644 --- a/lib/client/src/posts.ts +++ b/lib/client/src/posts.ts @@ -1,4 +1,4 @@ -// Copyright (C) 2023 Vaughn Nugent +// Copyright (C) 2024 Vaughn Nugent // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -71,9 +71,9 @@ export interface PostApiManualConfig { * @param channelId The id of the channel to get posts from * @returns A post api that can be used to get posts from the channel */ -export const createAutoPostApi = ({ cmsChannelIndexPath, channelId }: CMNextAutoConfig): AutoPostApi => { +export const createAutoPostApi = ({ cmsChannelIndexPath, channelId, urlPrefix }: CMNextAutoConfig): AutoPostApi => { //Use scoped channel api - const channelApi = createScopedChannelApi(cmsChannelIndexPath, channelId); + const channelApi = createScopedChannelApi(cmsChannelIndexPath, channelId, urlPrefix); const getIndex = async (): Promise> => { //Await the selected channel index @@ -99,16 +99,16 @@ export const createAutoPostApi = ({ cmsChannelIndexPath, channelId }: CMNextAuto return "" } - const itemId = defaultTo(post.id, post) + const itemId = defaultTo((post as PostMeta).id, post) //Fetch the content as text because it is html - const res = await fetch(`${baseDir}${contentDir}/${itemId}${extension}`) + const res = await fetch(`${urlPrefix}${baseDir}${contentDir}/${itemId}${extension}`) return await res.text() } const getIndexFilePath = async () : Promise => { const indexUrl = await channelApi.getPostIndexPath(); - return indexUrl || "" + return defaultTo(indexUrl, "") } return{ @@ -147,7 +147,7 @@ export const createManualPostApi = ({ channelRootDir, contentDir, postIndexPath const getPostContent = async (post: PostMeta | string, extension = '.html'): Promise => { //Get the content url - const contentUrl = `${getItemPath(contentDir)}/${defaultTo(post.id, post)}${extension}` + const contentUrl = `${getItemPath(contentDir)}/${defaultTo((post as PostMeta).id, post)}${extension}` //Fetch the content as text because it is html const res = await fetch(contentUrl) diff --git a/lib/client/src/types.ts b/lib/client/src/types.ts index ed62533..a675dda 100644 --- a/lib/client/src/types.ts +++ b/lib/client/src/types.ts @@ -1,4 +1,4 @@ -// Copyright (C) 2023 Vaughn Nugent +// Copyright (C) 2024 Vaughn Nugent // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -22,6 +22,11 @@ export interface CMNextAutoConfig{ * The id of the channel to load */ readonly channelId: string; + + /** + * Optional url prefix to use for the channel + */ + readonly urlPrefix?: string } -- cgit