Created
March 26, 2018 08:39
-
-
Save HoverBaum/3127f3e2064c2d2d081f45cf866a9672 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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