├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── appveyor.yml ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | # vim 39 | *.swp 40 | *.swo 41 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: 2 | - linux 3 | - osx 4 | language: node_js 5 | node_js: 6 | - "6" 7 | - "5" 8 | - "4.7.2" 9 | script: 10 | - npm start 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Contentful 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Contentful JavaScript boilerplate project 2 | 3 | Boilerplate project for getting started using JavaScript with Contentful 4 | 5 | ## Prerequisites 6 | 7 | - **Node** v4.7.2 or greater 8 | 9 | ## Start the project 10 | 11 | ### :one: Clone the project using the following command: 12 | 13 | ```bash 14 | git clone https://github.com/contentful/boilerplate-javascript.git 15 | ``` 16 | 17 | ### :two: Connect it to your account: 18 | 19 | Open the _boilerplate-javascript_ directory and update the _index.js_ file with your API credentials as follows: 20 | 21 | ```js 22 | var SPACE_ID = '' 23 | var ACCESS_TOKEN = '' 24 | ``` 25 | 26 | ### :three: Install dependencies and start it: 27 | 28 | ```shell 29 | npm install && npm start 30 | ``` 31 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: "4.7.2" 4 | - nodejs_version: "6" 5 | platform: 6 | - x86 7 | - x64 8 | install: 9 | - ps: Install-Product node $env:nodejs_version 10 | test_script: 11 | - npm start 12 | build: off 13 | 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 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 = 'developer_bookshelf' 8 | const ACCESS_TOKEN = '0b7f6x59a0' 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() 103 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "boilerplate-javascript", 3 | "version": "1.0.0", 4 | "description": "A boilerplate javascript project", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "dev": "nodemon index.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/contentful/boilerplate-javascript.git" 13 | }, 14 | "engines": { 15 | "node": ">=4.7.2" 16 | }, 17 | "keywords": [ 18 | "Contentful", 19 | "boilerplate", 20 | "Getting-Started" 21 | ], 22 | "author": "Khaled Garbaya ", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/contentful/boilerplate-javascript/issues" 26 | }, 27 | "homepage": "https://github.com/contentful/boilerplate-javascript#readme", 28 | "dependencies": { 29 | "chalk": "^1.1.3", 30 | "cli-table2": "^0.2.0", 31 | "contentful": "^4.3.0" 32 | }, 33 | "devDependencies": { 34 | "nodemon": "^1.11.0" 35 | } 36 | } 37 | --------------------------------------------------------------------------------