storyblok-js-client
Advanced tools
Comparing version 1.0.3 to 1.0.4
{ | ||
"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 |
37
test.js
@@ -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) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25819
11
255
132
0
3
1
+ Addedthrottled-queue@^1.0.5
+ Addedthrottled-queue@1.0.7(transitive)