B2B-88: add starter kit structure and elements
This commit is contained in:
48
packages/cms/core/src/content-renderer.tsx
Normal file
48
packages/cms/core/src/content-renderer.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { CmsType } from '@kit/cms-types';
|
||||
|
||||
const CMS_CLIENT = process.env.CMS_CLIENT as CmsType;
|
||||
|
||||
interface ContentRendererProps {
|
||||
content: unknown;
|
||||
type?: CmsType;
|
||||
}
|
||||
|
||||
export async function ContentRenderer({
|
||||
content,
|
||||
type = CMS_CLIENT,
|
||||
}: ContentRendererProps) {
|
||||
const Renderer = await getContentRenderer(type);
|
||||
|
||||
return Renderer ? <Renderer content={content} /> : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the content renderer for the specified CMS client.
|
||||
*
|
||||
* @param {CmsType} type - The type of CMS client.
|
||||
*/
|
||||
async function getContentRenderer(type: CmsType) {
|
||||
switch (type) {
|
||||
case 'keystatic': {
|
||||
const { KeystaticContentRenderer } = await import(
|
||||
'@kit/keystatic/renderer'
|
||||
);
|
||||
|
||||
return KeystaticContentRenderer;
|
||||
}
|
||||
|
||||
case 'wordpress': {
|
||||
const { WordpressContentRenderer } = await import(
|
||||
'@kit/wordpress/renderer'
|
||||
);
|
||||
|
||||
return WordpressContentRenderer;
|
||||
}
|
||||
|
||||
default: {
|
||||
console.error(`Unknown CMS client: ${type as string}`);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
packages/cms/core/src/create-cms-client.ts
Normal file
33
packages/cms/core/src/create-cms-client.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { CmsClient, CmsType } from '@kit/cms-types';
|
||||
import { createRegistry } from '@kit/shared/registry';
|
||||
|
||||
/**
|
||||
* The type of CMS client to use.
|
||||
*/
|
||||
const CMS_CLIENT = process.env.CMS_CLIENT as CmsType;
|
||||
|
||||
// Create a registry for CMS client implementations
|
||||
const cmsRegistry = createRegistry<CmsClient, CmsType>();
|
||||
|
||||
// Register the WordPress CMS client implementation
|
||||
cmsRegistry.register('wordpress', async () => {
|
||||
const { createWordpressClient } = await import('@kit/wordpress');
|
||||
return createWordpressClient();
|
||||
});
|
||||
|
||||
// Register the Keystatic CMS client implementation
|
||||
cmsRegistry.register('keystatic', async () => {
|
||||
const { createKeystaticClient } = await import('@kit/keystatic');
|
||||
return createKeystaticClient();
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates a CMS client based on the specified type.
|
||||
*
|
||||
* @param {CmsType} type - The type of CMS client to create. Defaults to the value of the CMS_CLIENT environment variable.
|
||||
* @returns {Promise<CmsClient>} A Promise that resolves to the created CMS client.
|
||||
* @throws {Error} If the specified CMS type is unknown.
|
||||
*/
|
||||
export async function createCmsClient(type: CmsType = CMS_CLIENT) {
|
||||
return cmsRegistry.get(type);
|
||||
}
|
||||
6
packages/cms/core/src/index.ts
Normal file
6
packages/cms/core/src/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Cms } from '@kit/cms-types';
|
||||
|
||||
export * from './create-cms-client';
|
||||
export * from './content-renderer';
|
||||
|
||||
export type { Cms };
|
||||
Reference in New Issue
Block a user