Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. const StoryblokClient = require('storyblok-js-client')
  2.  
  3. const Storyblok = new StoryblokClient({
  4. oauthToken: 'TOKEN'
  5. })
  6.  
  7. const spaceId = SPACE_ID
  8.  
  9. const Sync = {
  10. dimensions: [],
  11.  
  12. getAll(page) {
  13. return Storyblok.get(`spaces/${spaceId}/stories`, {
  14. per_page: 25,
  15. story_only: 1,
  16. starts_with: 'default/',
  17. page: page,
  18. with_alts: 1
  19. })
  20. },
  21.  
  22. async processAllStories() {
  23. var rootFolders = await Storyblok.get(`spaces/${spaceId}/stories`, {folder_only: 1, with_parent: 0})
  24. this.dimensions = rootFolders.data.stories
  25.  
  26. var sourceFolder = await Storyblok.get(`spaces/${spaceId}/stories`, {
  27. folder_only: 1,
  28. with_slug: 'default'
  29. })
  30.  
  31. var page = 1
  32. var res = await this.getAll(page)
  33. var all = res.data.stories
  34. var total = res.total
  35. var lastPage = Math.ceil((res.total / 25))
  36.  
  37. while (page < lastPage){
  38. page++
  39. res = await this.getAll(page)
  40. res.data.stories.forEach((story) => {
  41. all.push(story)
  42. })
  43. }
  44.  
  45. for (var i = 0; i < all.length; i++) {
  46. for (var j = 0; j < all[i].alternates.length; j++) {
  47. let alternate = all[i].alternates[j]
  48.  
  49. console.log(`merge ${all[i].full_slug} in ${alternate.full_slug} #${spaceId}`)
  50.  
  51.  
  52. var defaultStory = await Storyblok.get(`spaces/${spaceId}/stories/${all[i].id}`)
  53. var alternateStory = await Storyblok.get(`spaces/${spaceId}/stories/${alternate.id}`)
  54.  
  55. // TODO: Enrich content from alternate with i18n by iterating dynamically
  56. var newContent = defaultStory.data.story.content
  57. newContent.headline__i18n__de = alternateStory.data.story.content.headline
  58.  
  59. // TODO: Uncomment to push updated content:
  60. // var storyResult = await Storyblok.put(`spaces/${spaceId}/stories/${all[i].id}`, {
  61. // story: { content: newContent }
  62. // })
  63. }
  64. }
  65.  
  66. return all
  67. }
  68. }
  69.  
  70. Sync.processAllStories()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement