0.7.1 • Published 5 years ago

contents-node v0.7.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Installation

Install contents-node using Yarn or NPM:

yarn add contents-node
npm install --save contents-node

If you'd like to use a standlone pre-built version of the library for prototyping, you can include it via a <script> tag like so:

<script src="https://unpkg.com/contents-node@latest/lib/contents-node.umd.js"></script>

Be sure to change the version from @latest to whatever the current latest version is when you include it, otherwise the dependency is unpinned!

Configuration

Import the library and initialize it with an API token:

import Contents from 'contents-node'

const contents = new Contents('your_api_token')

All requests must include an API token, so the library will throw an error if you forget to provide one.

You can also choose to initialize with a different host to use the Preview API:

const contents = new Contents({
  host: 'preview',
  token: 'your_api_token',
})

Usage

The library is structured by resource, with methods for each action available. So for example, to find an entry by ID you would do:

const entry = await contents.entries.find({ entry: 'entry_id' })

Or, if you aren't using async/await yet, you can use promises directly:

contents.entry.find({ entry: 'entry_id' })
  .then(entry => {
    ...
  })
  .catch(error => {
    console.log(error)
  })

Identifiers

By default find and list methods take ID strings, like so:

const entry = await contents.entries.find({ entry: 'entry_id' })

However, some resources in the Contents API can be referred to either by id strings, or by a series of slug strings. This allows for easily using slugs as identifiers in a URL—like for blog articles for instance.

You can use either type of identifier with this library

// Find an entry by ID string.
const entry = await contents.entries.find({
  entry: 'entry_id',
})
// Find an entry by team, collection and entry slugs.
const entry = await contents.entries.find({
  team: 'team_slug',
  collection: 'collection_slug',
  entry: 'entry_slug',
})

This works for all methods, not just find. You can do the same with listing entries inside a collection, like so:

// List the entries in a collection by ID string.
const entries = await contents.entries.list({
  collection: 'collection_id',
})
// List the entries in a collection by team and collection slugs.
const entries = await contents.entries.list({
  team: 'team_slug',
  collection: 'collection_slug',
})

Parameters

Each method also takes a dictionary of parameters, which are passed directly in the query string. For example, you can use it to control pagination:

const entries = await contents.entries.list({
  collection: 'collection_id',
  parameters: {
    limit: 5,
    offset: 10,
  }
})

Methods

  • entries
    • entries.find({ entry: String, collection: String?, team: String?, parameters: Object? })
    • entries.list({ collection: String, team: String?, parameters: Object? })
0.7.1

5 years ago

0.7.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.12

6 years ago

0.4.11

6 years ago

0.4.10

6 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago