summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-15 17:00:41 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-15 17:00:41 -0400
commitf9e2109c27af5ece546261c018d4b2781860ff1c (patch)
tree8b9362c873ce4ef5a98205499f829271fc9f8af9
parent92eccbf4f6784fbee7505996a97aa5bb27d12238 (diff)
feat: Client lib now supports path prefix
-rw-r--r--lib/client/src/channels.ts12
-rw-r--r--lib/client/src/content.ts13
-rw-r--r--lib/client/src/posts.ts14
-rw-r--r--lib/client/src/types.ts7
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 <https://www.gnu.org/licenses/>.
-import { find, isEqual } from 'lodash-es';
+import { defaultTo, find, isEqual } from 'lodash-es';
import { CMNextApi, CMNextIndex, ChannelMeta } from './types'
export interface ChannelApi extends CMNextApi<ChannelMeta> {
@@ -56,9 +56,11 @@ export interface ScopedChannelApi extends ChannelApi {
getContentDir(): Promise<string | undefined>;
}
-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<ChannelMeta | undefined> => {
const index = await channelApi.getIndex()
@@ -72,7 +74,7 @@ export const createScopedChannelApi = (channelFile: string, channelId: string):
const getPostIndexPath = async (): Promise<string | undefined> => {
//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<string | undefined> => {
@@ -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 <https://www.gnu.org/licenses/>.
-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<CMNextIndex<ContentMeta>> => {
//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<string> => {
//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<string> => {
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<CMNextIndex<PostMeta>> => {
//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<string> => {
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<string> => {
//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
}