Skip to content

Instantly share code, notes, and snippets.

@onefriendaday
Last active February 7, 2018 11:47
Show Gist options
  • Save onefriendaday/9410587e4a81d8ac2aab5380942b3ea2 to your computer and use it in GitHub Desktop.
Save onefriendaday/9410587e4a81d8ac2aab5380942b3ea2 to your computer and use it in GitHub Desktop.
// About
// This script exports all stories as XML in a specific folder
// -------
// How to install?
// 1. Create a folder for the project and inside a "data" folder
// 2. $ npm init
// 3. $ npm install storyblok-js-client --save
// 4. Exchange SPACE_ID and TOKEN and startWith
// 5. $ node index.js
const StoryblokClient = require('storyblok-js-client')
const fs = require('fs')
const startWith = 'fr/'
const spaceId = 'SPACE_ID'
const oAuthToken = 'TOKEN'
let Storyblok = new StoryblokClient({
headers: {'Authorization': oAuthToken}
})
var StoryblokExporter = {
getAll(page) {
return Storyblok.get('spaces/' + spaceId + '/stories', {
per_page: 25,
search: startWith,
page: page
})
},
export(id) {
return Storyblok.get('spaces/' + spaceId + '/stories/' + id + '/export.xml')
}
}
async function getAllStories(){
var page = 1
var res = await StoryblokExporter.getAll(page)
var all = res.data.stories
var total = res.total
var lastPage = Math.ceil((res.total / 25))
while (page < lastPage){
page++
res = await StoryblokExporter.getAll(page)
res.data.stories.forEach((story) => {
all.push(story)
})
}
for (var i = 0; i < all.length; i++) {
exportResult = await StoryblokExporter.export(all[i].id)
console.log('Saving ' + all[i].id)
fs.writeFile('./data/' + all[i].id + '.xml', exportResult.data, (err) => {
if (err) throw err
})
}
return all
}
getAllStories().then((result) => {
console.log('Finished')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment