JSPM

  • Created
  • Published
  • Downloads 211985
  • Score
    100M100P100Q169170F
  • License MIT

Universal JavaScript SDK for Storyblok's API

Package Exports

  • storyblok-js-client

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (storyblok-js-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Universal JavaScript SDK for Storyblok's API

This client is a thin wrapper for the Storyblok API's to use in Node.js and the browser.

Install

npm install storyblok-js-client

Usage

Class Storyblok

Parameters

  • config Object
    • accessToken String, The preview token you can find in your space dashboard at https://app.storyblok.com
    • cache Object
      • type String, none or memory
  • (endpoint optional)

Example

// 1. Require the Storyblok client
const StoryblokClient = require('storyblok-js-client');

// 2. Initialize the client with the preview token 
// from your space dashboard at https://app.storyblok.com
let Storyblok = new StoryblokClient({
  accessToken: 'xf5dRMMjltLzKOcNgMaU9Att'
});

Activating request cache

The Storyblok client comes with a caching mechanism. When initializing the Storyblok client you can define a cache provider for caching the requests in memory. To clear the cache you can call Storyblok.flushCache(); or activate the automatic clear with clear: 'auto'.

let Storyblok = new StoryblokClient({
  accessToken: 'xf5dRMMjltLzKOcNgMaU9Att',
  cache: {
    clear: 'auto',
    type: 'memory'
  }
});

Method Storyblok#get

Parameters

  • [return] Promise, Object response
  • path String, Path (can be cdn/stories, cdn/stories/*, cdn/tags, cdn/datasources, cdn/links)
  • options Object, Options can be found in the API documentation.

Example

Storyblok
  .get('cdn/stories/home', {
    version: 'draft'
  })
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

Method Storyblok#flushCache

Parameters

  • [return] Object, returns the Storyblok client

Example

Storyblok.flushCache();

Code examples

Filter by content type values and path

const StoryblokClient = require('storyblok-js-client')

let client = new StoryblokClient({
  accessToken: 'zlRONoLBKrilxkz2k6fYuwtt'
})

// Filter by boolean value in content type
client.get('cdn/stories', {
    version: 'draft',
    filter_query: {
      is_featured: {
        all: '1'
      }
    }
  }).then((res) => {
    console.log(res.data.stories)
  })

// Get all news and author contents
client.get('cdn/stories', {
    version: 'draft',
    filter_query: {
      component: {
        in: 'news,author'
      }
    }
  }).then((res) => {
    console.log(res.data.stories)
  })

// Get all content from the news folder
client.get('cdn/stories', {
    version: 'draft',
    starts_with: 'news/'
  }).then((res) => {
    console.log(res.data.stories)
  })

Download all content from Storyblok

Following a code example using the storyblok-js-client to backup all content on your local filesystem inside a 'backup' folder.

const StoryblokClient = require('storyblok-js-client')
const fs = require('fs')

let client = new StoryblokClient({
  accessToken: 'WcdDcNgDm59K72EbsQg8Lgtt'
})

let lastPage = 1
let getStories = (page) => {
  client.get('cdn/stories', {
      version: 'draft',
      per_page: 25,
      page: page
    }).then((res) => {

    let stories = res.data.stories
    stories.forEach((story) => {
      fs.writeFile('./backup/' + story.id + '.json', JSON.stringify(story), (err) => {  
        if (err) throw err

        console.log(story.full_slug + ' backed up')
      })
    })

    let total = res.total
    lastPage = Math.ceil((res.total / res.perPage))

    if (page <= lastPage) {
      page++
      getStories(page)
    }
  })
}

getStories(1)

Contribution

Fork me on Github