Skip to content

Instantly share code, notes, and snippets.

@HoverBaum
Created March 26, 2018 08:39
Show Gist options
  • Save HoverBaum/3127f3e2064c2d2d081f45cf866a9672 to your computer and use it in GitHub Desktop.
Save HoverBaum/3127f3e2064c2d2d081f45cf866a9672 to your computer and use it in GitHub Desktop.
const exportBlogposts = (apiUrl, log) => new Promise(resolve => {
const exportPageOfPosts = (apiUrl, page = 1, allPosts = []) => {
log(`Getting posts for page ${page}`)
const url = `${apiUrl}?page=${page}`
https.get(url, (res) => {
// When we get a 404 back we went one page over those with posts.
// So we are done now.
if(res.statusCode === 400) {
return resolve(allPosts)
}
let result = ''
res.on('data', (d) => {
result += d.toString()
})
res.on('end', async () => {
blogPosts = JSON.parse(result)
return exportPageOfPosts(apiUrl, page + 1, allPosts.concat(blogPosts))
})
}).on('error', (e) => {
throw(Error('Error while exporting blogposts', e))
})
}
exportPageOfPosts(apiUrl)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment