Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

storyblok-js-client

Package Overview
Dependencies
Maintainers
1
Versions
197
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

storyblok-js-client - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

backup/76104.json

7

package.json
{
"name": "storyblok-js-client",
"version": "1.0.3",
"version": "1.0.4",
"description": "Universal JavaScript SDK for Storyblok's API",
"main": "dist/index.js",
"main": "./dist/index.js",
"scripts": {

@@ -28,4 +28,5 @@ "build": "babel source --presets babel-preset-es2015 --out-dir dist"

"axios": "^0.17.1",
"object-hash": "^1.1.8"
"object-hash": "^1.1.8",
"throttled-queue": "^1.0.5"
}
}

@@ -86,4 +86,47 @@ # Universal JavaScript SDK for Storyblok's API

### Nodejs code example
Following a code example using the storyblok-js-client to backup all content on your local filesystem inside a 'backup' folder.
~~~javascript
let StoryblokClient = require('./dist/index.js')
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](https://github.com/storyblok/storyblok-js-client)

@@ -1,7 +0,8 @@

'use strict';
'use strict'
const API_ENDPOINT_DEFAULT = 'https://api.storyblok.com/v1';
const hash = require('object-hash');
const axios = require('axios');
let memory = {};
const API_ENDPOINT_DEFAULT = 'https://api.storyblok.com/v1'
const hash = require('object-hash')
const axios = require('axios')
const throttledQueue = require('throttled-queue')
let memory = {}

@@ -12,10 +13,11 @@ class Storyblok {

if (!endpoint) {
endpoint = API_ENDPOINT_DEFAULT;
endpoint = API_ENDPOINT_DEFAULT
}
let headers = Object.assign({}, {'X-Storyblok-Client': 'JS/1.0.0'}, config.headers);
let headers = Object.assign({}, {'X-Storyblok-Client': 'JS/1.0.0'}, config.headers)
this.cacheVersion = (this.cacheVersion || this.newVersion());
this.accessToken = config.accessToken;
this.cache = config.cache || {clear: 'manual'};
this.throttle = throttledQueue(5, 1000)
this.cacheVersion = (this.cacheVersion || this.newVersion())
this.accessToken = config.accessToken
this.cache = config.cache || {clear: 'manual'}
this.client = axios.create({

@@ -30,18 +32,18 @@ baseURL: endpoint,

let query = params || {}
let url = `/${slug}`;
let url = `/${slug}`
if (url.indexOf('/cdn/') > -1) {
if (!query.version) {
query.version = 'published';
query.version = 'published'
}
query.token = this.getToken();
query.cv = this.cacheVersion;
query.token = this.getToken()
query.cv = this.cacheVersion
}
return this.cacheResponse(url, query);
return this.cacheResponse(url, query)
}
getToken() {
return this.accessToken;
return this.accessToken
}

@@ -51,36 +53,38 @@

return new Promise((resolve, reject) => {
let cacheKey = hash({url: url, params: params});
let provider = this.cacheProvider();
let cache = provider.get(cacheKey);
let cacheKey = hash({url: url, params: params})
let provider = this.cacheProvider()
let cache = provider.get(cacheKey)
if (this.cache.clear === 'auto' && params.version === 'draft') {
this.flushCache();
this.flushCache()
}
if (params.version === 'published' && cache) {
resolve(cache);
resolve(cache)
} else {
this.client.get(url, {params: params})
.then((res) => {
let response = {data: res.data, headers: res.headers}
this.throttle(() => {
this.client.get(url, {params: params})
.then((res) => {
let response = {data: res.data, headers: res.headers}
if (res.headers['per-page']) {
response = Object.assign({}, response, {
perPage: parseInt(res.headers['per-page']),
total: parseInt(res.headers['total'])
})
}
if (res.headers['per-page']) {
response = Object.assign({}, response, {
perPage: parseInt(res.headers['per-page']),
total: parseInt(res.headers['total'])
})
}
if (res.status != 200) {
return reject(res);
}
if (res.status != 200) {
return reject(res)
}
if (params.version === 'published') {
provider.set(cacheKey, response);
}
resolve(response);
})
.catch((response) => {
reject(response);
});
if (params.version === 'published') {
provider.set(cacheKey, response)
}
resolve(response)
})
.catch((response) => {
reject(response)
})
})
}

@@ -91,3 +95,3 @@ })

newVersion() {
return new Date().getTime();
return new Date().getTime()
}

@@ -102,15 +106,15 @@

get(key) {
return memory[key];
return memory[key]
},
set(key, content) {
memory[key] = content;
memory[key] = content
},
flush() {
memory = {};
memory = {}
}
}
break;
break
default:
this.cacheVersion = this.newVersion();
this.cacheVersion = this.newVersion()

@@ -126,8 +130,8 @@ return {

flushCache() {
this.cacheVersion = this.newVersion();
this.cacheProvider().flush();
return this;
this.cacheVersion = this.newVersion()
this.cacheProvider().flush()
return this
}
}
module.exports = Storyblok;
module.exports = Storyblok

@@ -1,4 +0,35 @@

let StoryblokClient = require('./index.js')
let StoryblokClient = require('./dist/index.js')
const fs = require('fs')
let client = new StoryblokClient({})
console.log(client)
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)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc