Skip to content

Instantly share code, notes, and snippets.

@ApprenticeGC
Created September 3, 2017 14:12
Show Gist options
  • Select an option

  • Save ApprenticeGC/107e2a3b30facb5cf6f96697a04e4115 to your computer and use it in GitHub Desktop.

Select an option

Save ApprenticeGC/107e2a3b30facb5cf6f96697a04e4115 to your computer and use it in GitHub Desktop.
Rewrite Contentful sample in rxjs and async/await
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