Burdy Web Utils

Powerful utilities to simplify usage of the Burdy on the web.

Installation

npm i @burdy-cms/web-utils

Functionalities

createApi

Creates a CreateApiType instance that can be used to call Burdy Content Api.

Example for the NextJS project

//burdy-api.ts
import { createApi } from '@burdy-cms/web-utils';

export const burdyApi = createApi({
  xContentToken: process.env.BURDY_ACCESS_TOKEN as string,
  host: process.env.NEXT_PUBLIC_CMS_URL as string,
});

And later call it with:

import {burdyApi} from "../../common/burdy-api";
const page = await burdyApi.getPage(pagePath as string, options);

Or to search for the pages

const {results} = await burdyApi.searchPages({
  parent: pagePath as string
});

createRewrites

Creates a RewritesObject instance that can be used to rewrite paths based on the configuration.

Accepts rewriteMap and origin (optional) as object parameters.

import { createRewrites } from '@burdy-cms/web-utils'; 
import axios from 'axios';

const rewrites = createRewrites({
  origin: 'https://cms.website.com/api/content',
  rewrite: [
    {
      source: '/:lang(fr|de)/:path*',
      destination: {
        page: '/sites/{lang}/{path}',
        header: '/sites/{lang}/fragments/header',
        footer: '/sites/{lang}/fragments/footer'
      }
    },
    {
      source: '/:path*',
      destination: {
        page: '/sites/en/{path}',
        header: '/sites/en/fragments/header',
        footer: '/sites/en/fragments/footer'
      }
    }
  ]
});

/**
  Page = https://cms.website.com/api/content/sites/fr/fragments/home
  Header = https://cms.website.com/api/content/sites/fr/fragments/header
  Footer = https://cms.website.com/api/content/sites/fr/fragments/footer
**/
const { page, footer, header } = rewrites.rewrite('/fr/home');

// Fetch data from Burdy
const [pageRequest, footerRequest, headerRequest] = await Promise.all([
  page,
  footer,
  header
]);

richtextToHtml

A function for converting Richtext (DraftJS) Editor content to plain HTML.

This is a forked version of draftjs-to-html draftjs-to-html created by Jyoti Puri.

import { richtextToHtml } from '@burdy-cms/web-utils';

const rawContentState = BURDY_RICHTEXT_RESPONSE;

const markup = richtextToHtml(
  rawContentState
);

subscribeToPreview

Subscribes to Burdy preview (iframe parent). Returns a Subscription that has unsubscribe() method.

import { subscribeToPreview } from '@burdy-cms/web-utils';
const subscription = subscribeToPreview({
  onEdit: post => {
    updateView(post); // Updates view using new content
  }
});

// Unsubscribe at some point if needed
subscription.unsubscribe();

updatePreview

Sends the updates to Burdy preview (iframe parent). This allows Burdy to know when you have switched the page on the frontend.

import { updatePreview } from '@burdy-cms/web-utils';

onRouterChange((pageData) => {
  updatePreview(pageData);
});
Copyright © Burdy Technologies. All rights reserved.