Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const StoryblokClient = require('storyblok-js-client')
- const Storyblok = new StoryblokClient({
- oauthToken: 'TOKEN'
- })
- const spaceId = SPACE_ID
- const Sync = {
- dimensions: [],
- getAll(page) {
- return Storyblok.get(`spaces/${spaceId}/stories`, {
- per_page: 25,
- story_only: 1,
- starts_with: 'default/',
- page: page,
- with_alts: 1
- })
- },
- async processAllStories() {
- var rootFolders = await Storyblok.get(`spaces/${spaceId}/stories`, {folder_only: 1, with_parent: 0})
- this.dimensions = rootFolders.data.stories
- var sourceFolder = await Storyblok.get(`spaces/${spaceId}/stories`, {
- folder_only: 1,
- with_slug: 'default'
- })
- var page = 1
- var res = await this.getAll(page)
- var all = res.data.stories
- var total = res.total
- var lastPage = Math.ceil((res.total / 25))
- while (page < lastPage){
- page++
- res = await this.getAll(page)
- res.data.stories.forEach((story) => {
- all.push(story)
- })
- }
- for (var i = 0; i < all.length; i++) {
- for (var j = 0; j < all[i].alternates.length; j++) {
- let alternate = all[i].alternates[j]
- console.log(`merge ${all[i].full_slug} in ${alternate.full_slug} #${spaceId}`)
- var defaultStory = await Storyblok.get(`spaces/${spaceId}/stories/${all[i].id}`)
- var alternateStory = await Storyblok.get(`spaces/${spaceId}/stories/${alternate.id}`)
- // TODO: Enrich content from alternate with i18n by iterating dynamically
- var newContent = defaultStory.data.story.content
- newContent.headline__i18n__de = alternateStory.data.story.content.headline
- // TODO: Uncomment to push updated content:
- // var storyResult = await Storyblok.put(`spaces/${spaceId}/stories/${all[i].id}`, {
- // story: { content: newContent }
- // })
- }
- }
- return all
- }
- }
- Sync.processAllStories()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement