|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const contentful = require('contentful') |
| 4 | +const chalk = require('chalk') |
| 5 | +const Table = require('cli-table2') |
| 6 | + |
| 7 | +const SPACE_ID = 't7kcqcuo46f7' |
| 8 | +const ACCESS_TOKEN = '4mVIdqO-akfxhSiCF2kYCzhE5LAFiHbK0kPGKdrqvLo' |
| 9 | + |
| 10 | +const client = contentful.createClient({ |
| 11 | + // This is the space ID. A space is like a project folder in Contentful terms |
| 12 | + space: SPACE_ID, |
| 13 | + // This is the access token for this space. Normally you get both ID and the token in the Contentful web app |
| 14 | + accessToken: ACCESS_TOKEN |
| 15 | +}) |
| 16 | + |
| 17 | +console.log(chalk.green.bold('\nWelcome to the Contentful JS Boilerplate\n')) |
| 18 | +console.log('This is a simplified example to demonstrate the usage of the Contentful CDA\n') |
| 19 | + |
| 20 | +// Entry point of the boilerplate, called at the end. |
| 21 | +function runBoilerplate () { |
| 22 | + displayContentTypes() |
| 23 | + .then(displayEntries) |
| 24 | + .then(() => { |
| 25 | + console.log('Want to go further? Feel free to check out this guide:') |
| 26 | + console.log(chalk.cyan('https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/\n')) |
| 27 | + }) |
| 28 | + .catch((error) => { |
| 29 | + console.log(chalk.red('\nError occurred:')) |
| 30 | + if (error.stack) { |
| 31 | + console.error(error.stack) |
| 32 | + return |
| 33 | + } |
| 34 | + console.error(error) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +function displayContentTypes () { |
| 39 | + console.log(chalk.green('Fetching and displaying Content Types ...')) |
| 40 | + |
| 41 | + return fetchContentTypes() |
| 42 | + .then((contentTypes) => { |
| 43 | + // Display a table with Content Type information |
| 44 | + const table = new Table({ |
| 45 | + head: ['Id', 'Title', 'Fields'] |
| 46 | + }) |
| 47 | + contentTypes.forEach((contentType) => { |
| 48 | + const fieldNames = contentType.fields |
| 49 | + .map((field) => field.name) |
| 50 | + .sort() |
| 51 | + table.push([contentType.sys.id, contentType.name, fieldNames.join(', ')]) |
| 52 | + }) |
| 53 | + console.log(table.toString()) |
| 54 | + |
| 55 | + return contentTypes |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +function displayEntries (contentTypes) { |
| 60 | + console.log(chalk.green('Fetching and displaying Entries ...')) |
| 61 | + |
| 62 | + return Promise.all(contentTypes.map((contentType) => { |
| 63 | + return fetchEntriesForContentType(contentType) |
| 64 | + .then((entries) => { |
| 65 | + console.log(`\These are the first 100 Entries for Content Type ${chalk.cyan(contentType.name)}:\n`) |
| 66 | + |
| 67 | + // Display a table with Entry information |
| 68 | + const table = new Table({ |
| 69 | + head: ['Id', 'Title'] |
| 70 | + }) |
| 71 | + entries.forEach((entry) => { |
| 72 | + table.push([entry.sys.id, entry.fields[contentType.displayField] || '[empty]']) |
| 73 | + }) |
| 74 | + console.log(table.toString()) |
| 75 | + }) |
| 76 | + })) |
| 77 | +} |
| 78 | + |
| 79 | +// Load all Content Types in your space from Contentful |
| 80 | +function fetchContentTypes () { |
| 81 | + return client.getContentTypes() |
| 82 | + .then((response) => response.items) |
| 83 | + .catch((error) => { |
| 84 | + console.log(chalk.red('\nError occurred while fetching Content Types:')) |
| 85 | + console.error(error) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// Load all entries for a given Content Type from Contentful |
| 90 | +function fetchEntriesForContentType (contentType) { |
| 91 | + return client.getEntries({ |
| 92 | + content_type: contentType.sys.id |
| 93 | + }) |
| 94 | + .then((response) => response.items) |
| 95 | + .catch((error) => { |
| 96 | + console.log(chalk.red(`\nError occurred while fetching Entries for ${chalk.cyan(contentType.name)}:`)) |
| 97 | + console.error(error) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +// Start the boilerplate code |
| 102 | +runBoilerplate() |
0 commit comments