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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@episerver/content-delivery

v1.1.0

Published

SDK for the Episerver Content Delivery API.

Downloads

2,438

Readme

Optimizely Content Delivery JavaScript SDK - Content Loader, Content Resolver and Site Loader

SDK for loading and resolving content from an Optimizely application running the Content Delivery API. The SDK is written in TypeScript and includes types.

Please visit Optimizely World for full documentation of the APIs.

Note that this SDK requires you to use the flattened JSON format, see code example below how to configure the API to use this format.

services.AddContentDeliveryApi(options => 
{
    options.FlattenPropertyModel = true;
});

Installing

Using npm:

$ npm install @episerver/content-delivery@latest

using yarn:

$ yarn install @episerver/content-delivery@latest

Examples

Content Loader

Given we have imported the content loader and instantiated it:

import { ContentLoader } from '@episerver/content-delivery';

const contentLoader = new ContentLoader();

The constructor takes a ContentDeliveryConfig object.

Loading content by an identifier:

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d')
  .then((content) => {
    // Content was successfully loaded
  })
  .catch((contentLoaderError) => {
    console.error('Content not loaded', contentLoaderError.errorMessage);
  });

The function takes an optional ContentRequest object where we can specify which language branch to load with the branch parameter:

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { branch: 'en' })
  .then((content) => {
    // Content was successfully loaded
  });

We can specify which properties should be included in the response with the select parameter:

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { select: ['heading', 'body'] })
  .then((content) => {
    // Content was successfully loaded
  });

We can specify which properties should be expanded in the response with the expand parameter:

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { expand: ['teasers'] })
  .then((content) => {
    // Content was successfully loaded
  });

Reference properties (content areas and content references) only includes the reference to the content by default. By expanding these properties the content of the referenced content is also included in the response. See the Content API documentation for more information.

Loading children by a parent identifier:

contentLoader.getChildren('38963a25-b6ec-493b-aca4-f4fbce1aed4d')
  .then((children) => {
    // Children was successfully loaded
  });

The function takes an optional ContentCollectionRequest object. In addition to the ContentRequest parameters we can also limit the number of content items to load:

contentLoader.getChildren('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { top: 10 })
  .then((collection) => {
    // Children was successfully loaded
    var children = collection.items;
    var continuationToken = collection.continuationToken;
  });

For loading the next set of content items, use the continuation token:

contentLoader.getChildren('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { continuationToken: continuationToken })
  .then((collection) => {
    // Children was successfully loaded
    var children = collection.items;
    // When we reached the last set, the continuation token won't be present.
    var continuationToken = collection.continuationToken;
  });

Note that the continuation token contains how many items that should be loaded, no need to provide the top parameter again.

Loading ancestors by an identifier:

contentLoader.getAncestors('38963a25-b6ec-493b-aca4-f4fbce1aed4d')
  .then((ancestors) => {
    // Ancestors was successfully loaded
  });

The function takes an optional ContentRequest object.

ContentRequest schema

{
  branch: 'en',
  select: [''],
  expand: [''],
}

ContentCollectionRequest schema

{
  branch: 'en',
  select: [''],
  expand: [''],
  top: 0,
  continuationToken: '',
}

ContentLoaderError schema

{
  data: {},
  errorCode: 404,
  errorMessage: '',
}

Content Resolver

Given we have imported the content resolver and instantiated it:

import { ContentResolver } from '@episerver/content-delivery';

const contentResolver = new ContentResolver();

The constructor takes a ContentDeliveryConfig object.

Resolving content by an URL:

contentResolver.resolveContent('https://site.com/en/page1/page2/doesnt-exist/', false)
  .then((resolvedContent) => {
    // Content was resolved. Note that the promise is fulfilled
    // even though the content was not found. The return object contains a
    // status property you can check.
    var content = resolvedContent.content;
  })
  .catch((contentResolverError) => {
    // Promise is only rejected if there was a service error.
    console.error('Content not resolved', contentResolverError.errorMessage);
  });

The second parameter, matchExact, defines whether the URL should be a partial or exact match. The remaining path of the URL, if a partial match, is available in remainingPath in the returned object, see ResolvedContent. The function also takes an optional ResolveContentRequest object.

ResolveContentRequest schema

{
  select: [''],
  expand: [''],
}

ResolvedContent schema

{
  content: {},
  branch: 'en',
  status: 'UNKNOWN|RESOLVED|NOTFOUND|UNAUTHORIZED|ACCESSDENIED',
  mode: 'DEFAULT|PREVIEW|EDIT',
  remainingPath: 'doesnt-exist/',
  siteId: '6ed7799f-efd8-4421-8eb3-c95bfbf7281d',
  startPageId: 'ff2e86ae-27a9-4417-b503-ecb9c2fdd63b',
}

ContentResolverError schema

{
  errorMessage: '',
}

Site Loader

Given we have imported the site loader and instantiated it:

import { SiteLoader } from '@episerver/content-delivery';

const siteLoader = new SiteLoader();

The constructor takes a ContentDeliveryConfig object.

Loading site by an identifier:

siteLoader.getSite('45872cd5-e63c-363e-caa4-35e6ab1ad6d4')
  .then((site) => {
    // Site was successfully loaded
  })
  .catch((siteLoaderError) => {
    console.error('Site not loaded', siteLoaderError.errorMessage);
  });

Loading all sites:

siteLoader.getSites()
  .then((sites) => {
    // Sites was successfully listed
  });

Configuration

ContentDeliveryConfig schema

{
  apiUrl: 'https://site.com/api/episerver/v2.0',
  getAccessToken: function (path) { 
    // Return access token.
  },
  selectAllProperties: true,
  expandAllProperties: false,
}

Change the default configuration

The default configuration is combined with the configuration you pass in to the constructors of the content loader and resolver classes.

import { defaultConfig } from '@episerver/content-delivery';

defaultConfig.apiUrl = 'https://site.com/api/episerver/v2.0';
defaultConfig.getAccessToken = () => authService.getAccessToken();
defaultConfig.selectAllProperties = true;
defaultConfig.expandAllProperties = true;