Created
September 4, 2017 10:04
-
-
Save ApprenticeGC/21873a133d288ab893774a2110ad952c to your computer and use it in GitHub Desktop.
Retrieve sample blog from Contentful
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 contentful = require('contentful') | |
| const chalk = require('chalk') | |
| // const Table = require('cli-table2') | |
| const argv = require('yargs').argv | |
| const Rx = require('rxjs/Rx') | |
| const Observable = require('rxjs/Observable').Observable | |
| const spaceId = argv.spaceId | |
| const accessToken = argv.accessToken | |
| const client = contentful.createClient({ | |
| space: spaceId, | |
| accessToken: accessToken | |
| }) | |
| Observable | |
| .defer(async () => { | |
| const postEntry = await client.getEntries({ | |
| content_type: 'post' | |
| }) | |
| const posts = postEntry.items | |
| const all = await Promise.all( | |
| posts.map(async post => { | |
| const blockEntry = await client.getEntries({ | |
| content_type: 'block', | |
| 'fields.refPost.sys.id': post.sys.id | |
| }) | |
| return { | |
| id: post.sys.id, | |
| title: post.fields.title, | |
| blocks: blockEntry.items | |
| } | |
| })) | |
| return all | |
| }) | |
| .subscribe(response => { | |
| response.forEach(byPost => { | |
| console.log(chalk.bgBlue.bold(byPost.title)) | |
| byPost.blocks | |
| .sort((a, b) => a.fields.inPostIndex - b.fields.inPostIndex) | |
| .forEach(block => { | |
| // console.log(block.fields.kind) | |
| // console.log(block.fields.inPostIndex) | |
| if (block.fields.kind === 'code' || block.fields.kind === 'list') { | |
| console.log(chalk.yellow(JSON.stringify(block.fields.datas))) | |
| console.log('\n') | |
| } else { | |
| console.log(block.fields.data) | |
| console.log('\n') | |
| } | |
| }) | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment