Last active
May 13, 2019 11:36
-
-
Save onefriendaday/0d2334d24b9b964a3c1a8f10d2a3ed4b to your computer and use it in GitHub Desktop.
Script to access all alternates of a "default" folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment