Mashroom Content API
Plugin for Mashroom Server, a Microfrontend Integration Platform.
Part of the Mashroom Content extension.
This plugin adds an API abstraction that allows you to retrieve and manage content from a Headless CMS in Mashroom plugins (e.g. Portal Apps).
It allows it to transparently switch the Headless CMS/Content Provider.
Features
- Retrieval and update of content in Headless CMS systems
- Searching for content with MongoDB-like filter queries (if supported by the Headless CMS)
- Provides simple services that can be used on the server side and on the client side in Microfrontends (Portal Apps)
- Allows it to transparently switch the Headless CMS (via plugin)
- Support for versioning and i18n (if supported by the Headless CMS)
- Asset Uploads and Downloads
- Automatic image proxying with optimizations (format conversion and resizing on-the-fly)
- Caching (requires the @mashroom/mashroom-memory-cache plugin)
- CDN integration
Usage
If node_modules/@mashroom is configured as plugin path just add @mashroom-content/mashroom-content-api as dependency.
You can override the default config in your Mashroom config file like this:
{
"plugins": {
"Mashroom Content Services": {
"provider": "Mashroom Content Internal Storage Provider",
"cacheEnable": true,
"cacheTTLSec": 1800
},
"Mashroom Content Provider API": {
"path": "/content",
"uploadTmpDir": null
}
}
}
- provider: The Headless CMS provider plugin (Default: Mashroom Content Internal Storage Provider)
- cacheEnable: Enable server side and browser caching of content (server side requires @mashroom/mashroom-memory-cache) (Default: true)
- cacheTTLSec: Cache TTL in seconds (Default: 1800)
- path: The base path for the content API (Default: /content)
- uploadTmpDir: A specific upload folder (absolute or relative to the server config file)
Security
IMPORTANT NOTE: The Content API (and therefore the client service) has no security at all at the moment.
So, to make sure that normal users cannot update and delete content you should add an ACL configuration like this:
{
"/content/**": {
"*": {
"allow": {
"roles": ["Administrator"]
}
},
"GET": {
"allow": "any"
}
}
}
Service
On the Server side you can use the following new service accessible through pluginContext.services.content.service:
export interface MashroomContentService {
imageBreakpoints: Array<number>;
imagePreferredFormats: Array<MashroomContentAssetProcImageFormats>;
searchContent<T>(req: Request, type: string, filter?: MashroomContentApiFilter<T>, locale?: string, status?: MashroomContentApiStatus,
sort?: MashroomContentApiSort<T>, limit?: number, skip?: number): Promise<MashroomContentApiContentSearchResult<T>>;
getContent<T>(req: Request, type: string, id: string, locale?: string, version?: number): Promise<MashroomContentApiContentWrapper<T>>;
getContentVersions<T>(req: Request, type: string, id: string, locale?: string): Promise<MashroomContentVersionsResult<T>>;
insertContent<T>(req: Request, type: string, content: MashroomContentApiContentUpdateInsert<T>): Promise<MashroomContentApiContentWrapper<T>>;
updateContent<T>(req: Request, type: string, id: string, content: MashroomContentApiContentUpdateInsert<Partial<T>>): Promise<MashroomContentApiContentWrapper<T>>;
removeContent(req: Request, type: string, id: string): Promise<void>;
removeContentParts(req: Request, type: string, id: string, locales?: Array<string>, versions?: Array<number>): Promise<void>;
uploadAsset(req: Request, file: Readable, meta: MashroomContentAssetMeta, path?: string, contentRef?: MashroomContentAssetContentRef): Promise<MashroomContentAsset>;
searchAssets(req: Request, type?: string, titleContains?: string, limit?: number, skip?: number): Promise<MashroomContentAssetSearchResult>;
removeAsset(req: Request, id: string): Promise<void>;
}
Client Service
On the client side you can use MashroomContentClientService like this:
const bootstrap: MashroomPortalAppPluginBootstrapFunction = async (portalAppHostElement, portalAppSetup, clientServices) => {
const {appConfig, user} = portalAppSetup;
const contentService: MashroomContentClientService = clientServices.contentService;
const result = await contentService.getContent('my-content', 'sdfjkld');
}
The interface:
export interface MashroomContentClientService {
imageBreakpoints: Array<number>;
imagePreferredFormats: Array<MashroomContentAssetProcImageFormats>;
searchContent<T>(type: string, filter?: MashroomContentApiFilter<T>, locale?: string, status?: MashroomContentApiStatus,
sort?: MashroomContentApiSort<T>, limit?: number, skip?: number): Promise<MashroomContentApiContentSearchResult<T>>;
getContent<T>(type: string, id: string, locale?: string, version?: number): Promise<MashroomContentApiContentWrapper<T>>;
getContentVersions<T>(type: string, id: string, locale?: string): Promise<MashroomContentVersionsResult<T>>;
insertContent<T>(type: string, content: MashroomContentApiContentUpdateInsert<T>): Promise<MashroomContentApiContentWrapper<T>>;
updateContent<T>(type: string, id: string, content: MashroomContentApiContentUpdateInsert<Partial<T>>): Promise<MashroomContentApiContentWrapper<T>>;
removeContent(type: string, id: string): Promise<void>;
removeContentParts(type: string, id: string, locales?: Array<string>, versions?: Array<number>): Promise<void>;
uploadAsset(file: File, path?: string, contentRef?: MashroomContentAssetContentRef): PromiseWithProgressAndCancel<MashroomContentAsset>;
searchAssets(type?: string, titleContains?: string, limit?: number, skip?: number): Promise<MashroomContentAssetSearchResult>;
removeAsset(id: string): Promise<void>;
}
REST API
And you can directly use the REST API which is specified in spec/mashroom-content-api.yaml
Custom Provider Plugin
To register a custom memory-cache-provider plugin add this to package.json:
{
"mashroom": {
"plugins": [
{
"name": "My Content Provider",
"type": "content-provider",
"bootstrap": "./dist/mashroom-bootstrap.js",
"defaultConfig": {
"myProperty": "test"
}
}
]
}
}
The bootstrap returns the provider:
import MashroomContentProviderInternalStorageImpl from './MashroomContentProviderInternalStorageImpl';
import type {MashroomContentProviderPluginBootstrapFunction} from '@mashroom-content/mashroom-content-api/type-definitions';
const bootstrap: MashroomContentProviderPluginBootstrapFunction = async (pluginName, pluginConfig, pluginContextHolder) => {
return new MyContentProvider();
};
export default bootstrap;
And the provider has to implement the following interface:
export interface MashroomContentProvider {
searchContent<T>(req: Request, type: string, filter?: MashroomContentApiFilter<T>, locale?: string, status?: MashroomContentApiStatus,
sort?: MashroomContentApiSort<T>, limit?: number, skip?: number): Promise<MashroomContentApiContentSearchResult<T>>;
getContent<T>(req: Request, type: string, id: string, locale?: string, version?: number): Promise<MashroomContentApiContentWrapper<T>>;
getContentVersions<T>(req: Request, type: string, id: string, locale?: string): Promise<MashroomContentVersionsResult<T>>;
insertContent<T>(req: Request, type: string, content: MashroomContentApiContentUpdateInsert<T>): Promise<MashroomContentApiContentWrapper<T>>;
updateContent<T>(req: Request, type: string, id: string, content: MashroomContentApiContentUpdateInsert<Partial<T>>): Promise<MashroomContentApiContentWrapper<T>>;
removeContent(req: Request, type: string, id: string): Promise<void>;
removeContentParts(req: Request, type: string, id: string, locales?: Array<string>, versions?: Array<number>): Promise<void>;
uploadAsset(req: Request, file: Readable, meta: MashroomContentAssetMeta, path?: string, contentRef?: MashroomContentAssetContentRef): Promise<MashroomContentAsset>;
searchAssets(req: Request, type?: string, titleContains?: string, limit?: number, skip?: number): Promise<MashroomContentAssetSearchResult>;
removeAsset(req: Request, id: string): Promise<void>;
getAssetProxies(): MashroomContentApiAssetProxyConfigs;
}