0.0.5 • Published 9 months ago

@p8marketing/arboretum-sdk v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Introduction

@p8marketing/arboretum-sdk is a library for building sitemaps in Contentful.

  • Dynamic: Arboretum SDK builds sitemap dynamically based on the pages structure defined in Contentful. There are no hard-coded paths, so editors have full power over the sitemap.
  • Customizable: Content types that represent pages are configurable, so you can integrate Arboretum SDK even in existing projects.
  • Efficient: Number of requests to Contentful is minimalized.
  • Lazy: By default sitemap is built only for locales that were explicitly requested.
  • In memory: Sitemap is stored in memory, so no other dependencies (e.g. database) are required.

Installation

To install the stable version:

yarn add @p8marketing/arboretum-sdk

Create client

Basically, there are 3 ways to create Arboretum SDK client:

  • By providing required credentials by hand
import { createArboretumClientFromCdaParams } from "@p8marketing/arboretum-sdk";

(async () => {
  const { client, warnings } = await createArboretumClientFromCdaParams({
    preview: false,
    contentful: {
      space: "[CONTENTFUL SPACE]",
      environment: "[CONTENTFUL ENVIRONMENT]",
      accessToken: "[CONTENTFUL CDA/CPA ACCESS TOKEN]",
      // Sample page content type configuration
      options: {
        pageContentTypes: {
          page: {
            titleFieldId: "name",
            slugFieldId: "slug",
            childPagesFieldId: "childPages",
          },
        },
      },
    },
    // Client configuration options
    options: {},
  });
})();
  • By providing contentful CDA client
import { createArboretumClientFromCdaClient } from "@p8marketing/arboretum-sdk";
import { createClient } from "contentful";

const contentfulClient = createClient({
  space: "[CONTENTFUL SPACE]",
  environment: "[CONTENTFUL ENVIRONMENT]",
  accessToken: "[CONTENTFUL CDA/CPA ACCESS TOKEN]",
});

(async () => {
  const { client, warnings } = await createArboretumClientFromCdaClient({
    preview: false,
    contentful: {
      client: contentfulClient,
      // Sample page content type configuration
      options: {
        pageContentTypes: {
          page: {
            titleFieldId: "name",
            slugFieldId: "slug",
            childPagesFieldId: "childPages",
          },
        },
      },
    },
    // Client configuration options
    options: {},
  });
})();
  • By providing contentful CMA client (especially useful in contentful apps)
import { createArboretumClientFromCma } from "@p8marketing/arboretum-sdk";
import { createClient } from "contentful-management";

async () => {
  const cmaClient = createClient({
    accessToken: "[CONTENTFUL CDA/CPA ACCESS TOKEN]",
  });

  const spaceClient = await cmaClient.getSpace("[CONTENTFUL SPACE]");

  const environmentClient = await spaceClient.getEnvironment(
    "[CONTENTFUL ENVIRONMENT]"
  );

  const { client, warnings } = await createArboretumClientFromCma({
    preview: false,
    contentful: {
      client: environmentClient,
      // Sample page content type configuration
      options: {
        pageContentTypes: {
          page: {
            titleFieldId: "name",
            slugFieldId: "slug",
            childPagesFieldId: "childPages",
          },
        },
      },
    },
    // Client configuration options
    options: {},
  });
};

In this step all data required to calculate sitemap is fetched and stored in internal memory. That's the only place where dynamic requests to Contentful are happening. All subsequent interactions with the Arboretum SDK client rely on the data fetched in this step.

*** Important: Remember to provide valid content types configuration unique to your contentful environment and mark entry representing the home page with the tag pageHome (you can customize this tag using homePageTagId configuration).

Client configuration options

DefaultDescription
eagerylyfalseBy default localized sitemap is generated only for locales that were explicitly requested. When set to true sitemap is generated for every locale in advance.
dataYou can cache data computed during previous Arboretum SDK client initialization (client.cachedData method) and use it to create a new client. When this parameter is defined all requests to Contentful are skipped.
includeEntryStatusfalseOnly for Arboretum SDK clients that were created based on Contentful CMA. When set to true page's cmaOnlyStatus field will be defined.

Contentful configuration options

DefaultDefaultDescription
homePageTagIdpageHomeSitemap root tag id
pageContentTypesRequired. Object consisting of keys that are content type id's and values described here. Sample value: {page:{"titleFieldId":"name","slugFieldId":"slug","childPagesFieldId":"childPages"}}
redirectContentTypeObject described here. Sample value: {"id":"redirect","titleFieldId":"name","typeFieldId":"type","pageFieldId":"page","pathFieldId":"path"}

Contentful page content types configuration

NameDefaultDescription
titleFieldIdId of the field that represents the page title. This field should be of type Symbol.
slugFieldIdRequired. Id of the field that represents the page slug that should match the following regexp ^((\/)\|(([\/\w\-\._~:!$&'\(\)*+,;@]\|(%\d+))+))$. This field should be of type Symbol.
childPagesFieldIdId of the field that represents the references to child pages. This field should be of type Array and include references to other entries that were configured as pages.

Contentful redirect content type configuration

NameDefaultDescription
idRequired. Id of the redirect content type.
titleFieldIdId of the field that represents the redirect title. This field should be of type Symbol.
typeFieldIdRequired. Id of the field that represents the redirect type (should be either alias or redirect). This field should be of type Symbol.
pageFieldIdRequired. Id of the field that represents the page reference. This field should be of type Link and accept references only to entries that were configured as pages.
pathFieldIdRequired. Id of the field that represents the redirect path that should match the following regexp \/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?$. This field should be of type Symbol.

Basic usage

Most of the results returned by Arboretum SDK are wrapped by Eider (inspired by fp-ts Eider) where successful responses are marked with _tag equal to "Right" and failed responses are marked with _tag equal to "Left".

const homePage = client.pageByPath("/en");
console.log(homePage);
/* prints 
  {
    _tag: 'Right',
    right: {
      type: 'page',
      id: 'ZCFBfxFPfz3iy9Rojvxva',
      title: 'home',
      localeCode: 'en',
      path: '/en',
      slug: 'home',
      totalDirectChildrenCount: 2
    }
  }

or

  { _tag: 'Left', left: 'Failed to find locale by code: en' }
  
 */

const blogPage = client.pageById("en", "4bkXR9tM8c1cGzTe7BMIgn");
console.log(blogPage);
/* prints 
  {
    _tag: "Right",
    right: {
      type: "page",
      id: "4bkXR9tM8c1cGzTe7BMIgn",
      title: "blog",
      localeCode: "en",
      path: "/en/blog",
      slug: "blog",
      totalDirectChildrenCount: 2,
    },
  }

or

  {
    _tag: "Left",
    left: "Failed to find page by id: 4bkXR9tM8c1cGzTe7BMIgn and locale: en",
  }
  
 */

Documentation

0.0.5

9 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago