npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mashroom-content/mashroom-content-api

v1.0.3

Published

Mashroom Content API

Downloads

8

Readme

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 {
    /**
     * Configured image breakpoints
     */
    imageBreakpoints: Array<number>;
    /**
     * Configured preferred image formats
     */
    imagePreferredFormats: Array<MashroomContentAssetProcImageFormats>;
    /**
     * Search for content of given type. You can use skip and limit for pagination.
     *
     * May throw 'Invalid Filter' (as error message).
     */
    searchContent<T>(req: Request, type: string, filter?: MashroomContentApiFilter<T>, locale?: string, status?: MashroomContentApiStatus,
                     sort?: MashroomContentApiSort<T>, limit?: number, skip?: number): Promise<MashroomContentApiContentSearchResult<T>>;
    /**
     * Get a specific content element identified by an ID.
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    getContent<T>(req: Request, type: string, id: string, locale?: string, version?: number): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Get all available versions of given content.
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    getContentVersions<T>(req: Request, type: string, id: string, locale?: string): Promise<MashroomContentVersionsResult<T>>;
    /**
     * Insert content for given type.
     * For most content providers the 'type' needs to be defined in the CMS backend first.
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    insertContent<T>(req: Request, type: string, content: MashroomContentApiContentUpdateInsert<T>): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Partially update content with given type and ID.
     * This includes adding new localizations.
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    updateContent<T>(req: Request, type: string, id: string, content: MashroomContentApiContentUpdateInsert<Partial<T>>): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Remove content with given type and ID.
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    removeContent(req: Request, type: string, id: string): Promise<void>;
    /**
     * Remove content parts (specific languages or versions).
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    removeContentParts(req: Request, type: string, id: string, locales?: Array<string>, versions?: Array<number>): Promise<void>;
    /**
     * Upload an asset (image or video) to given path (if folders are supported by the provider).
     * If a contentRef is given the asset URL will be linked to given content.
     *
     * Will return download URL for the created asset.
     */
    uploadAsset(req: Request, file: Readable, meta: MashroomContentAssetMeta, path?: string, contentRef?: MashroomContentAssetContentRef): Promise<MashroomContentAsset>;
    /**
     * Search for existing assets.
     *
     * type can be something like image/png or just image
     */
    searchAssets(req: Request, type?: string, titleContains?: string, limit?: number, skip?: number): Promise<MashroomContentAssetSearchResult>;
    /**
     * Remove given asset
     */
    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 {
    /**
     * Configured image breakpoints
     */
    imageBreakpoints: Array<number>;
    /**
     * Configured preferred image formats
     */
    imagePreferredFormats: Array<MashroomContentAssetProcImageFormats>;
    /**
     * Search for content of given type.
     */
    searchContent<T>(type: string, filter?: MashroomContentApiFilter<T>, locale?: string, status?: MashroomContentApiStatus,
                     sort?: MashroomContentApiSort<T>, limit?: number, skip?: number): Promise<MashroomContentApiContentSearchResult<T>>;
    /**
     * Get a specific content element identified by an ID.
     */
    getContent<T>(type: string, id: string, locale?: string, version?: number): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Get all available versions of given content.
     */
    getContentVersions<T>(type: string, id: string, locale?: string): Promise<MashroomContentVersionsResult<T>>;
    /**
     * Insert content for given type.
     */
    insertContent<T>(type: string, content: MashroomContentApiContentUpdateInsert<T>): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Partially update content with given type and ID.
     * This includes adding new localizations.
     */
    updateContent<T>(type: string, id: string, content: MashroomContentApiContentUpdateInsert<Partial<T>>): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Remove content with given type and ID.
     */
    removeContent(type: string, id: string): Promise<void>;
    /**
     * Remove content parts (specific languages or versions).
     */
    removeContentParts(type: string, id: string, locales?: Array<string>, versions?: Array<number>): Promise<void>;
    /**
     * Upload an asset (image or video).
     * If a contentRef is given the asset URL will be linked to given content.
     *
     * Will return download URL for the created asset.
     */
    uploadAsset(file: File, path?: string, contentRef?: MashroomContentAssetContentRef): PromiseWithProgressAndCancel<MashroomContentAsset>;
    /**
     * Search for existing assets.
     *
     * type can be something like image/png or just image
     */
    searchAssets(type?: string, titleContains?: string, limit?: number, skip?: number): Promise<MashroomContentAssetSearchResult>;
    /**
     * Remove given asset
     */
    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 {
    /**
     * Search for content of given type.
     */
    searchContent<T>(req: Request, type: string, filter?: MashroomContentApiFilter<T>, locale?: string, status?: MashroomContentApiStatus,
                     sort?: MashroomContentApiSort<T>, limit?: number, skip?: number): Promise<MashroomContentApiContentSearchResult<T>>;
    /**
     * Get a specific content element identified by an ID.
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    getContent<T>(req: Request, type: string, id: string, locale?: string, version?: number): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Get all available versions of given content.
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    getContentVersions<T>(req: Request, type: string, id: string, locale?: string): Promise<MashroomContentVersionsResult<T>>;
    /**
     * Insert content for given type.
     * For most content providers the 'type' needs to be defined in the CMS backend first.
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    insertContent<T>(req: Request, type: string, content: MashroomContentApiContentUpdateInsert<T>): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Partially update content with given type and ID.
     * This includes adding new localizations.
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    updateContent<T>(req: Request, type: string, id: string, content: MashroomContentApiContentUpdateInsert<Partial<T>>): Promise<MashroomContentApiContentWrapper<T>>;
    /**
     * Remove content with given type and ID.
     * If locales or versions are given only specific translations or versions are removed.
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    removeContent(req: Request, type: string, id: string): Promise<void>;
    /**
     * Remove content parts (specific languages or versions).
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    removeContentParts(req: Request, type: string, id: string, locales?: Array<string>, versions?: Array<number>): Promise<void>;
    /**
     * Upload an asset (image or video) to given path (if folders are supported by the provider).
     * The path can be ignored if folders are not supported.
     * If a contentRef is given the asset URL should be linked to given content.
     *
     * Must return the download URL for the new asset. The URL will be translated if a corresponding URL proxy is set.
     */
    uploadAsset(req: Request, file: Readable, meta: MashroomContentAssetMeta, path?: string, contentRef?: MashroomContentAssetContentRef): Promise<MashroomContentAsset>;
    /**
     * Search for existing assets.
     * The provider should return the assets sorted by upload timestamp (desc).
     *
     * type can be something like image/png or just image
     */
    searchAssets(req: Request, type?: string, titleContains?: string, limit?: number, skip?: number): Promise<MashroomContentAssetSearchResult>;
    /**
     * Remove given asset.
     *
     * May throw 'Access Denied' or 'Not Found' (as error message).
     */
    removeAsset(req: Request, id: string): Promise<void>;
    /**
     * Get a list of asset URIs that need to be proxied (and rewritten in the content)
     */
    getAssetProxies(): MashroomContentApiAssetProxyConfigs;
}