Files
myeasycms-v2/packages/cms/core/src/create-cms-client.ts
giancarlo dbce7e38ae Refactor route handlers and CMS clients
Refactored the route handlers to use a new `enhanceRouteHandler` function for better control over request handlers and user authentication. CMS clients are now created using factory functions for better encapsulation and control over instance creation. Renamed `client.ts` in 'keystatic' to `keystatic-client.ts`.
2024-05-02 12:37:58 +07:00

70 lines
1.7 KiB
TypeScript

import { CmsClient } from './cms-client';
import { CmsType } from './cms.type';
/**
* 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 = process.env.CMS_CLIENT as CmsType,
): Promise<CmsClient> {
return cmsClientFactory(type);
}
async function cmsClientFactory(type: CmsType) {
switch (type) {
case 'wordpress':
return getWordpressClient();
case 'keystatic':
return getKeystaticClient();
default:
throw new Error(`Unknown CMS type`);
}
}
async function getWordpressClient() {
const { createWordpressClient } = await import(
'../../wordpress/src/wp-client'
);
return createWordpressClient();
}
async function getKeystaticClient() {
if (
process.env.NEXT_RUNTIME === 'nodejs' ||
process.env.KEYSTATIC_STORAGE_KIND !== 'local'
) {
const { createKeystaticClient } = await import(
'../../keystatic/src/keystatic-client'
);
return createKeystaticClient();
}
console.error(
`[CMS] Keystatic client using "Local" mode is only available in Node.js runtime. Please choose a different CMS client. Returning a mock client instead of throwing an error.`,
);
return mockCMSClient() as unknown as CmsClient;
}
function mockCMSClient() {
return {
getContentItems() {
return Promise.resolve({
items: [],
total: 0,
});
},
getContentItemBySlug() {
return Promise.resolve(undefined);
},
};
}