Created
September 3, 2017 14:12
-
-
Save ApprenticeGC/107e2a3b30facb5cf6f96697a04e4115 to your computer and use it in GitHub Desktop.
Rewrite Contentful sample in rxjs and async/await
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 contentTypes = await client.getContentTypes() | |
| const firstItem = contentTypes.items[0] | |
| const response = await client.getEntries({ | |
| content_type: firstItem.sys.id | |
| }) | |
| return { displayField: contentTypes.items[0].displayField, entries: response.items } | |
| }) | |
| .subscribe(({displayField, entries}) => { | |
| const table = new Table({ | |
| head: ['Id', 'Title'] | |
| }) | |
| entries.forEach((entry) => { | |
| table.push([entry.sys.id, entry.fields[displayField] || '[empty]']) | |
| }) | |
| console.log(table.toString()) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment