4.7.0 • Published 5 years ago

storyblok-ts-client v4.7.0

Weekly downloads
29
License
MIT
Repository
github
Last release
5 years ago

Storyblok Management API Typescript Client

typescript library for working with Storyblok management API.

npm

Description

  • consists of:

    • a basic CRUD client "Storyblok" with throttling and failure-retry
    • a wrapper library over the CRUD client "ApiClient"
    • typescript conversion of the original storyblok-js-client - "StoryblokTS"
    • abstraction classes with limited functionalities:
      • Space
      • Component
      • Story
        • FolderIndex
        • Content
        • Folder
          • RootFolder
          • Subfolder
      • Asset
      • AssetFolder
  • ApiClient methods are categorized into the following:

  • API requests are throttled

  • images are compressed and resized using sharp

  • jest is setup for testing (not fully tested)

  • the management API is not fully implemented

Installation

npm install --save storyblok-ts-client

Usage

// Basic CRUD client
const {Storyblok} = require('storyblok-ts-client')
const storyblok = new Storyblok('fake_api_token')
return storyblok.get('/12345')
  .then(res => console.log('space id:', res.data.id))
  // => space id: 12345

// Management API wrapper
const {ApiClient} = require('storyblok-ts-client')
const apiClient = new ApiClient('fake_api_token', 12345)
return apiClient.spaces.get()
  .then(space => console.log('space id:', space.id))
  // => space id: 12345

// StoryblokTS
const {StoryblokTS} = require('storyblok-ts-client')
let storyblokTS = new StoryblokTS({oauthToken: 'YOUR_OAUTH_TOKEN'})
const spaceId = 12345
storyblokTS.post(`spaces/${spaceId}/stories`, {story: {name: 'xy', slug: 'xy'}})
storyblokTS.put(`spaces/${spaceId}/stories/1`, {story: {name: 'xy', slug: 'xy'}})
storyblokTS.delete(`spaces/${spaceId}/stories/1`, null)

CLI Scripts

# development and testing
npm run dev       # runs typescript transpiler in watch mode
npm start         # runs any executable code in the index.js

# test - remember to update .env file with a test Storyblok account api token and test space id
npm run dev:test  # runs jest in watch mode
npm test          # runs jest
npm run coverage  # runs jest and check code coverage stat

# build
npm run build

# tslint
npm run lint

# prettier
npm run format

API Reference - generated with jsdoc2md

Classes

Functions

ApiClient

Kind: global class
Implements: IStoryblokClass
Export:

new ApiClient(apiToken, spaceId)

ParamTypeDescription
apiTokenstringAPI access token.
spaceIdnumberStoryblok working space id.

Example

const {ApiClient} = require('storyblok-ts-client')
const apiClient = new ApiClient('fake_api_token', 12345)

apiClient.assetFolders

Kind: instance property of ApiClient
Read only: true

assetFolders.create ⇒ Promise

Kind: instance property of assetFolders
Fulfil: IAssetFolder Details of the asset folder created.
Reject: AxiosError Axios error.

ParamTypeDescription
namestringName of asset folder to create.

assetFolders.delete ⇒ Promise

Kind: instance property of assetFolders
Fulfil: void
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of asset folder to be deleted.

assetFolders.deleteExisting ⇒ Promise

Kind: instance property of assetFolders
Fulfil: void[]
Reject: AxiosError Axios error.

assetFolders.get ⇒ Promise

Kind: instance property of assetFolders
Fulfil: IAssetFolder Asset folder information.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the target asset folder.

assetFolders.getByName ⇒ Promise

Kind: instance property of assetFolders
Fulfil: IAssetFolder[] List of matched asset folders.
Reject: AxiosError Axios error.

ParamTypeDescription
searchStringstringString to search by.

assetFolders.getExisting ⇒ Promise

Kind: instance property of assetFolders
Fulfil: IAssetFolder[] List of existing asset folders.
Reject: AxiosError Axios error.

apiClient.assets

Kind: instance property of ApiClient
Read only: true

assets.count ⇒ Promise

Kind: instance property of assets
Fulfil: number A count of existing assets.
Reject: AxiosError Axios error.

assets.createFromImage ⇒ Promise

Kind: instance property of assets
Fulfil: string public access url of the new asset.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIPendingAssetAsset information.
filePathstringAbsolute file path to the image.
compressbooleanFlag to compress image.
sizeLimitnumberResizing dimension limit value.

assets.delete ⇒ Promise

Kind: instance property of assets
Fulfil: IAsset Information of the deleted asset.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the asset to be deleted.

assets.deleteExisting ⇒ Promise

Kind: instance property of assets
Fulfil: IAsset[] Information on the deleted assets.
Reject: AxiosError Axios error.

assets.get ⇒ Promise

Kind: instance property of assets
Fulfil: IAsset Details of the asset.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of asset to fetch.

assets.getByPage ⇒ Promise.<Array.<IAsset>>

Kind: instance property of assets
Fulfil: IAsset[] Assets on the pagination page.
Reject: AxiosError Axios error.

ParamTypeDefaultDescription
pagenumber1Pagination page.
perPagenumber25Assets per page.

assets.getByUrl ⇒ Promise

Kind: instance property of assets
Fulfil: IAsset Matched asset.
Reject: AxiosError Axios error.

ParamTypeDescription
urlstringUrl to match by.

assets.getExisting ⇒ Promise

Kind: instance property of assets
Fulfil: IAsset[] A list of existing assets.
Reject: AxiosError Axios error.

assets.register ⇒ Promise

Kind: instance property of assets
Fulfil: IRegistration Asset registration info (used for uploading).
Reject: AxiosError Axios error.

ParamTypeDescription
assetIPendingAssetInformation to create asset from.
asset.filenamestringFile name to register for.
asset.asset_folder_idnumber(optional) Assign a asset folder.
asset.idnumber(optional) Id of existing asset to replace with this new asset.

assets.upload ⇒ Promise

Kind: instance property of assets
Fulfil: string Access url of the uploaded asset.
Reject: AxiosError Axios error.

ParamTypeDescription
bufferBufferBuffered asset data.
registrationIRegistrationRegistration info.

apiClient.components

Kind: instance property of ApiClient
Read only: true

components.create ⇒ Promise

Kind: instance property of components
Fulfil: IComponent Details of the component that was created.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIPendingComponentInfo on component to be created.

components.delete ⇒ Promise

Kind: instance property of components
Fulfil: IComponent Details of the deleted component.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of component to be deleted.

components.deleteExisting ⇒ Promise

Kind: instance property of components
Fulfil: IComponent[] A list of deleted components details.
Reject: AxiosError Axios error.

components.get ⇒ Promise

Kind: instance property of components
Fulfil: IComponent Details of the component definition.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberComponent id to fetch by.

components.getExisting ⇒ Promise

Kind: instance property of components
Fulfil: IComponent[] A list of component definitions.
Reject: AxiosError Axios error.

components.update ⇒ Promise

Kind: instance property of components
Fulfil: IComponent Details of component that was updated.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIComponentStoryblok component data object with modified info.

components.create ⇒ Promise

Kind: instance property of components
Fulfil: IComponent Details of the component that was created.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIPendingComponentInfo on component to be created.

apiClient.spaces

Kind: instance property of ApiClient
Read only: true

spaces.get ⇒ Promise

Kind: instance property of spaces
Fulfil: ISpace Working space information.
Reject: AxiosError Axios error.

apiClient.stories

Kind: instance property of ApiClient
Read only: true

stories.count ⇒ Promise

Kind: instance property of stories
Fulfil: number A count of existing stories.
Reject: AxiosError Axios error.

stories.countPages ⇒ Promise

Kind: instance property of stories
Fulfil: number Total story pagination page count.
Reject: AxiosError Axios error.

ParamTypeDescription
perPagenumber(optional) How many stories per page. Defaults to 25.

stories.create ⇒ Promise

Kind: instance property of stories
Fulfil: IStory Details of story that was created.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIPendingStoryStoryblok story data object.

stories.delete ⇒ Promise

Kind: instance property of stories
Fulfil: IStory Details of the story that was deleted.
Reject: AxiosError Axios error.

ParamTypeDescription
idIStoryId of the story to be deleted.

stories.deleteExisting ⇒ Promise

Kind: instance property of stories
Fulfil: IStory[] A list of deleted stories details.
Reject: AxiosError Axios error.

stories.get ⇒ Promise

Kind: instance property of stories
Fulfil: IStory Details of content story.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the content story.

stories.getByPage ⇒ Promise

Kind: instance property of stories
Fulfil: IStory[] A page of stories.
Reject: AxiosError Axios error.

ParamTypeDescription
pagenumberPagination page number.
perPagenumber(optional) How many stories per page. Defaults to 25.

stories.getExisting ⇒ Promise

Kind: instance property of stories
Fulfil: IStory[] A list of existing content stories.
Reject: AxiosError Axios error.

stories.publish ⇒ Promise

Kind: instance property of stories
Fulfil: IStory Details of the published story
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the story to publish

stories.publishPendings ⇒ Promise

Kind: instance property of stories
Fulfil: IStory[] List of published stories.
Reject: AxiosError Axios error.

stories.reorder ⇒ Promise

Kind: instance property of stories
Fulfil: IStory Details of the moved story.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the story to be moved.
afterIdnumberId of reference story to position after.

stories.update ⇒ Promise

Kind: instance property of stories
Fulfil: IStory Details of story that was updated.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIStoryModified story info.

stories.publishPendings ⇒ Promise

Kind: instance property of stories
Fulfil: IStory[] List of published stories.
Reject: AxiosError Axios error.

apiClient.countAssets ⇒ Promise

Kind: instance property of ApiClient
Fulfil: number A count of existing assets.
Reject: AxiosError Axios error.

apiClient.countStories ⇒ Promise

Kind: instance property of ApiClient
Fulfil: number A count of existing stories.
Reject: AxiosError Axios error.

apiClient.countStoryPages ⇒ Promise

Kind: instance property of ApiClient
Fulfil: number Total story pagination page count.
Reject: AxiosError Axios error.

ParamTypeDescription
perPagenumber(optional) How many stories per page. Defaults to 25.

apiClient.createAssetFolder ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAssetFolder Details of the asset folder created.
Reject: AxiosError Axios error.

ParamTypeDescription
namestringName of asset folder to create.

apiClient.createAssetFromImage ⇒ Promise

Kind: instance property of ApiClient
Fulfil: string Public access url of the new asset.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIPendingAssetAsset information.
filePathstringAbsolute file path to the image.
compressbooleanFlag to compress image.
sizeLimitnumberResizing dimension limit value.

apiClient.createStory ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory Details of story that was created.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIPendingStoryStoryblok story data object.

apiClient.deleteAsset ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAsset Information of the deleted asset.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the asset to be deleted.

apiClient.deleteAssetFolder ⇒ Promise

Kind: instance property of ApiClient
Fulfil: void
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of asset folder to be deleted.

apiClient.deleteComponent ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IComponent Details of the deleted component.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of component to be deleted.

apiClient.deleteStory ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory Details of the story that was deleted.
Reject: AxiosError Axios error.

ParamTypeDescription
idIStoryId of the story to be deleted.

apiClient.deleteExistingAssetFolders ⇒ Promise

Kind: instance property of ApiClient
Fulfil: void[]
Reject: AxiosError Axios error.

apiClient.deleteExistingAssets ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAsset[] Information on the deleted assets.
Reject: AxiosError Axios error.

apiClient.deleteExistingComponents ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IComponent[] A list of deleted components details.
Reject: AxiosError Axios error.

apiClient.deleteExistingStories ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory[] A list of deleted stories details.
Reject: AxiosError Axios error.

apiClient.getAsset ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAsset Details of the asset.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of asset to fetch.

apiClient.getAssetByUrl ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAsset Matched asset.
Reject: AxiosError Axios error.

ParamTypeDescription
urlstringUrl to match by.

apiClient.getAssetFolder ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAssetFolder Asset folder information.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the target asset folder.

apiClient.getAssetFolderByName ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAssetFolder[] List of matched asset folders.
Reject: AxiosError Axios error.

ParamTypeDescription
searchStringstringString to search by.

apiClient.getComponent ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IComponent Details of the component definition.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberComponent id to fetch by.

apiClient.getExistingAssets ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAsset[] A list of existing assets.
Reject: AxiosError Axios error.

apiClient.getExistingComponents ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IComponent[] A list of component definitions.
Reject: AxiosError Axios error.

apiClient.getExistingAssetFolders ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IAssetFolder[] List of existing asset folders.
Reject: AxiosError Axios error.

apiClient.getExistingStories ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory[] A list of existing content stories.
Reject: AxiosError Axios error.

apiClient.getSpace ⇒ Promise

Kind: instance property of ApiClient
Fulfil: ISpace Working space information.
Reject: AxiosError Axios error.

apiClient.getStoriesByPage ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory[] A page of stories.
Reject: AxiosError Axios error.

ParamTypeDescription
pagenumberPagination page number.
perPagenumber(optional) How many stories per page. Defaults to 25.

apiClient.publishStory ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory Details of the published story
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the story to publish

apiClient.registerAsset ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IRegistration Asset registration info (used for uploading).
Reject: AxiosError Axios error.

ParamTypeDescription
assetIPendingAssetInformation to create asset from.
asset.filenamestringFile name to register for.
asset.asset_folder_idnumber(optional) Assign a asset folder.
asset.idnumber(optional) Id of existing asset to replace with this new asset.

apiClient.reorderStory ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory Details of the moved story.
Reject: AxiosError Axios error.

ParamTypeDescription
idnumberId of the story to be moved.
afterIdnumberReference story to position after.

apiClient.updateComponent ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IComponent Details of component that was updated.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIComponentStoryblok component data object with modified info.

apiClient.updateStory ⇒ Promise

Kind: instance property of ApiClient
Fulfil: IStory Details of story that was updated.
Reject: AxiosError Axios error.

ParamTypeDescription
dataIStoryStoryblok story data object with modified info.

apiClient.uploadAsset ⇒ Promise

Kind: instance property of ApiClient
Fulfil: string Access url of the uploaded asset.
Reject: AxiosError Axios error.

ParamTypeDescription
bufferBufferBuffered asset data.
registrationIRegistrationRegistration info.

apiClient.getAssetsByPage(page, perPage) ⇒ Promise.<Array.<IAsset>>

Kind: instance method of ApiClient
Fulfil: IAsset[] Assets on the pagination page.
Reject: AxiosError Axios error.

ParamTypeDefaultDescription
pagenumber1Pagination page.
perPagenumber25Assets per page.

Storyblok

Kind: global class
Implements: IStoryblokClass
Export:

new Storyblok(apiToken)

ParamTypeDescription
apiTokenstringAPI access token.

Example

const {Storyblok} = require('storyblok-ts-client')
const storyblok = new Storyblok('fake_api_token')

storyblok.delete ⇒ Promise.<any>

Kind: instance property of Storyblok

ParamTypeDefaultDescription
urlstring"'/'"Request url.
configICustomAxiosRequestConfigRequest config.

Example

const {Storyblok} = require('storyblok-ts-client')
const storyblok = new Storyblok('fake_api_token')
const spaceId = 12345
const storyId = 123456
const url = `/${spaceId}/stories/${storyId}`
storyblok.delete(url, {retries: 3, retryDelay: 1000})
  .then(res => console.log('deleted story id:', res.story.id))
// => deleted story id: 123456

storyblok.get ⇒ Promise.<any>

Kind: instance property of Storyblok

ParamTypeDefaultDescription
urlstring"'/'"Request url.
configICustomAxiosRequestConfigRequest config.

Example

const {Storyblok} = require('storyblok-ts-client')
const storyblok = new Storyblok('fake_api_token')
const spaceId = 12345
const url = `/${spaceId}`
storyblok.get(url, {retries: 3, retryDelay: 1000})
  .then(res => console.log('space id:', res.space.id))
// => space id: 12345

storyblok.post ⇒ Promise.<any>

Kind: instance property of Storyblok

ParamTypeDefaultDescription
urlstring"'/'"Request url.
dataanyRequest data body.
configICustomAxiosRequestConfigRequest config.

Example

const {Storyblok} = require('storyblok-ts-client')
const storyblok = new Storyblok('fake_api_token')
const spaceId = 12345
const url = `/${spaceId}/stories`
const story = {
  name: 'test',
  slug: 'test',
}
storyblok.post(url, {story}, {retries: 3, retryDelay: 1000})
  .then(res => console.log('new story id:', res.story.id))
// => new story id: 123456

storyblok.put ⇒ Promise.<any>

Kind: instance property of Storyblok

ParamTypeDefaultDescription
urlstring"'/'"Request url.
dataanyRequest data body.
configICustomAxiosRequestConfigRequest config.

Example

const {Storyblok} = require('storyblok-ts-client')
const storyblok = new Storyblok('fake_api_token')
const spaceId = 12345
const url = `/${spaceId}/stories`
const story = {name: 'test', slug: 'test'}
storyblok.post(url, {story}, {retries: 3, retryDelay: 1000})
  .then(res => {
    const newStoryId = res.story.id
    console.log('new story id:', newStoryId)
    console.log('new story name:', res.story.name)
    const updateContent = {name: 'new test', slug: 'test'}
    return storyblok.put(
      url + `/${newStoryId}`,
      {story: updateContent},
      {retries: 3, retryDelay: 1000}
    )
  })
  .then(res => console.log('updated story name:', res.story.name))
  .catch(e => console.log(e.config))
// => new story id: 123456
// => new story name: test
// => updated story name: new test

StoryblokTS

Kind: global class
Export:

new StoryblokTS(config, rateLimit, endpoint)

ParamTypeDescription
configanyConfigurations.
config.accessTokenstringThe preview token you can find in your space dashboard at https://app.storyblok.com.
config.cacheanyCache types.
config.cache.typestring'none' or 'memory'.
config.cache.clearstring'auto' or 'manual'.
config.headersanyRequest headers.
config.regionstringRegion.
config.httpsbooleanSwitch for https.
config.oauthTokenstringManagement API key.
rateLimitnumberThrottle value (defaults to 3 for management api and 5 for cdn api).
endpointstringAPI endpoint.

Example

// Example for using the content delivery api
// 1. Require the Storyblok client
const {StoryblokTS} = require('storyblok-ts-client')
// 2. Initialize the client with the preview
//    token from your space dashboard at
//    https://app.storyblok.com
let Storyblok = new StoryblokClient({
  accessToken: 'your_access_token'
})

// Example for using the content management api
// 1. Require the Storyblok client
const {StoryblokTS} = require('storyblok-ts-client')
const spaceId = 12345
// 2. Initialize the client with the oauth token
//    from the my account area at
//    https://app.storyblok.com
let Storyblok = new StoryblokClient({
  oauthToken: 'YOUR_OAUTH_TOKEN'
})
Storyblok.post(`spaces/${spaceId}/stories`, {story: {name: 'xy', slug: 'xy'}})
Storyblok.put(`spaces/${spaceId}/stories/1`, {story: {name: 'xy', slug: 'xy'}})
Storyblok.delete(`spaces/${spaceId}/stories/1`, null)

imageToBuffer(filePath, compress, sizeLimit, forceFormat) ⇒ Promise

Kind: global function
Fulfil: Buffer Buffered image data.
Reject: Error Error value.

ParamTypeDescription
filePathstringAbsolute path to image file.
compressbooleanFlag to compress image.
sizeLimitnumberResizing dimension limit value.
forceFormatstringForce convert to a particular format.

resizeImage(image, sizeLimit) ⇒ Promise

Kind: global function
Fulfil: Sharp Resized sharp object.
Reject: Error Error value.

ParamTypeDefaultDescription
imageSharpsharp object.
sizeLimitnumber640Size (in pixels) to limit image dimension.

© 2018 juniorCitizen

4.7.0

5 years ago

4.6.0

5 years ago

4.5.1

5 years ago

4.5.0

5 years ago

4.4.0

5 years ago

4.3.11

6 years ago

4.3.10

6 years ago

4.3.9

6 years ago

4.3.8

6 years ago

4.3.7

6 years ago

4.3.6

6 years ago

4.3.5

6 years ago

4.3.4

6 years ago

4.3.3

6 years ago

4.3.2

6 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.2

6 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago