UNPKG

14.4 kBMarkdownView Raw
1# contentful-management.js
2
3> JavaScript SDK for [Contentful's](https://www.contentful.com) Content Management API.
4
5[![npm](https://img.shields.io/npm/v/contentful-management.svg)](https://www.npmjs.com/package/contentful-management)
6[![Build Status](https://travis-ci.org/contentful/contentful-management.js.svg?branch=master)](https://travis-ci.org/contentful/contentful-management.js)
7[![codecov](https://codecov.io/gh/contentful/contentful-management.js/branch/master/graph/badge.svg)](https://codecov.io/gh/contentful/contentful-management.js)
8[![Dependency Status](https://david-dm.org/contentful/contentful-management.js.svg)](https://david-dm.org/contentful/contentful-management.js)
9[![devDependency Status](https://david-dm.org/contentful/contentful-management.js/dev-status.svg)](https://david-dm.org/contentful/contentful-management.js#info=devDependencies)
10
11[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
12[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
13[![npm downloads](https://img.shields.io/npm/dm/contentful-management.svg)](http://npm-stat.com/charts.html?package=contentful-management)
14[![gzip bundle size](http://img.badgesize.io/https://unpkg.com/contentful-management/dist/contentful-management.browser.min.js?compression=gzip
15)](https://unpkg.com/contentful-management/dist/contentful-management.browser.min.js)
16
17[Contentful](https://www.contentful.com) provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.
18
19## Features
20
21- Content management and retrieval through Contentful's [Content Management API](https://www.contentful.com/developers/docs/references/content-management-api/).
22- Built in rate limiting with recovery procedures
23- Asset processing helpers
24
25## Supported environments
26
27Browsers and Node.js:
28- Chrome
29- Firefox
30- Edge
31- Safari
32- node.js (LTS)
33- IE11 (with [legacy version](#legacy-browsers) of the library)
34
35Other browsers should also work, but at the moment we're only running automated tests on the browsers and Node.js versions specified above.
36
37# Getting started
38
39To get started with the Contentful Management JS SDK you'll need to install it, and then get credentials which will allow you to access your content in Contentful.
40
41- [Installation](#installation)
42- [Authentication](#authentication)
43- [Using ES6 import](#using-es6-import)
44- [Your first request](#your-first-request)
45- [Troubleshooting](#troubleshooting)
46- [Documentation/References](#documentationreferences)
47
48## Installation
49
50### Node:
51
52Using [npm](http://npmjs.org):
53
54``` sh
55npm install contentful-management
56```
57
58Using [yarn](https://yarnpkg.com/lang/en/):
59
60``` sh
61yarn add contentful-management
62```
63
64### Browser:
65
66For browsers, we recommend to download the SDK via npm or yarn to ensure 100% availability.
67
68If you'd like to use a standalone built file you can use the following script tag or download it from [jsDelivr](https://www.jsdelivr.com/package/npm/contentful-management), under the `dist` directory:
69
70``` html
71<script src="https://cdn.jsdelivr.net/npm/contentful-management@latest/dist/contentful-management.browser.min.js"></script>
72```
73**It's not recommended to use the above URL for production.**
74
75Using `contentful@latest` will always get you the latest version, but you can also specify a specific version number:
76
77``` html
78<!-- Avoid using the following url for production. You can not rely on its availability. -->
79<script src="https://cdn.jsdelivr.net/npm/contentful-management@4.0.1/dist/contentful-management.browser.min.js"></script>
80```
81
82The Contentful Management SDK will be accessible via the `contentfulManagement` global variable.
83
84Check the [releases](https://github.com/contentful/contentful-management.js/releases) page to know which versions are available.
85
86### Typings
87
88This library also comes with typings to use with typescript.
89
90### Legacy browsers:
91
92This library also comes with a legacy version to support Internet Explorer 11 and other older browsers. It already contains a polyfill for Promises.
93
94To support legacy browsers in your application, use `contentful-management.legacy.min.js` instead of `contentful-management.browser.min.js`
95
96## Authentication
97
98To get content from Contentful, an app should authenticate with an OAuth bearer token.
99
100If you want to use this SDK for a simple tool or a local app that you won't redistribute or make available to other users, you can get an API key for the Management API at our [Authentication page](https://www.contentful.com/developers/docs/references/authentication/).
101
102If you'd like to create an app which would make use of this SDK but that would be available for other users, where they could authenticate with their own Contentful credentials, make sure to also check out the section about [Creating an OAuth Application](https://www.contentful.com/developers/docs/references/authentication/#creating-an-oauth-20-application)
103
104## Using ES6 import
105You can use the es6 import with the SDK as follow
106
107```js
108// import createClient directly
109import {createClient} from 'contentful-management'
110var client = createClient({
111 // This is the access token for this space. Normally you get the token in the Contentful web app
112 accessToken: 'YOUR_ACCESS_TOKEN'
113})
114//....
115```
116OR
117```js
118// import everything from contentful
119import * as contentful from 'contentful-management'
120var client = contentful.createClient({
121 // This is the access token for this space. Normally you get the token in the Contentful web app
122 accessToken: 'YOUR_ACCESS_TOKEN'
123})
124// ....
125```
126
127## Your first request
128
129The following code snippet is the most basic one you can use to get content from Contentful with this SDK:
130
131```js
132var contentful = require('contentful-management')
133var client = contentful.createClient({
134 // This is the access token for this space. Normally you get the token in the Contentful web app
135 accessToken: 'YOUR_ACCESS_TOKEN'
136})
137
138// This API call will request a space with the specified ID
139client.getSpace('spaceId')
140.then((space) => {
141 // Now that we have a space, we can get entries from that space
142 space.getEntries()
143 .then((entries) => {
144 console.log(entries.items)
145 })
146
147 // let's get a content type
148 space.getContentType('product')
149 .then((contentType) => {
150 // and now let's update its name
151 contentType.name = 'New Product'
152 contentType.update()
153 .then((updatedContentType) => {
154 console.log('Update was successful')
155 })
156 })
157})
158```
159
160You can try and change the above example at [Tonic](https://tonicdev.com/npm/contentful-management).
161
162## Troubleshooting
163
164- **I can't Install the package via npm**
165 - Check your internet connection
166 - It is called `contentful-management` and not `contenful-management` ¯\\\_(ツ)_
167 - **Can I use the SDK in react native projects**
168 - Yes it is possible
169 - **I get the error: Unable to resolve module `http`**
170 - Our SDK is supplied as node and browser version. Most non-node environments, like React Native, act like a browser. To force using of the browser version, you can require it via: `const { createClient } = require('contentful-management/dist/contentful-management.browser.min.js')`
171- **Can I use it with typescript?**
172 - Yes, type definition file coming soon
173- **I am not sure what payload to send when creating and entity (Asset/Entity/ContentType etc...)**
174 - Check the Content Management API [docs](https://www.contentful.com/developers/docs/references/content-management-api/) or the examples in the reference [docs](https://contentful.github.io/contentful-management.js)
175 - Feel free to open an issue if you didn't find what you need in the above links
176- 😱 **something is wrong what should I do**
177 - If it is a bug related to the code create a GitHub issue and make sure to remove any credential for your code before sharing it.
178 - If you need to share your credentials, for example you have an issue with your space, please create a support ticket.
179 - Please **do not** share your management token in a GitHub issue
180
181## Documentation/References
182
183To help you get the most out of this SDK, we've prepared reference documentation, tutorials and other examples that will help you learn and understand how to use this library.
184
185### Configuration
186
187The `createClient` method supports several options you may set to achieve the expected behavior:
188
189```js
190contentful.createClient({
191 ... your config here ...
192})
193```
194
195#### accessToken (required)
196Your CMA access token.
197
198#### host (default: `'api.contentful.com'`)
199Set the host used to build the request URI's.
200
201#### hostUpload (default: `'upload.contentful.com'`)
202Set the host used to build the upload related request uri's.
203
204#### basePath (default: ``)
205This path gets appended to the host to allow request urls like `https://gateway.example.com/contentful/` for custom gateways/proxies.
206
207#### httpAgent (default: `undefined`)
208Custom agent to perform HTTP requests. Find further information in the [axios request config documentation](https://github.com/mzabriskie/axios#request-config).
209
210#### httpsAgent (default: `undefined`)
211Custom agent to perform HTTPS requests. Find further information in the [axios request config documentation](https://github.com/mzabriskie/axios#request-config).
212
213#### headers (default: `{}`)
214Additional headers to attach to the requests. We add/overwrite the following headers:
215
216* Content-Type: `application/vnd.contentful.management.v1+json`
217* X-Contentful-User-Agent: `sdk contentful-management.js/1.2.3; platform node.js/1.2.3; os macOS/1.2.3`
218 (Automatically generated)
219
220#### proxy (default: `undefined`)
221Axios proxy configuration. See the [axios request config documentation](https://github.com/mzabriskie/axios#request-config) for further information about the supported values.
222
223#### retryOnError (default: `true`)
224By default, this SDK is retrying requests which resulted in a 500 server error and 429 rate limit response. Set this to `false` to disable this behavior.
225
226#### logHandler (default: `function (level, data) {}`)
227Errors and warnings will be logged by default to the node or browser console. Pass your own log handler to intercept here and handle errors, warnings and info on your own.
228
229#### requestLogger (default: `function (config) {}`)
230Interceptor called on every request. Takes Axios request config as an arg. Default does nothing. Pass your own function to log any desired data.
231
232#### responseLogger (default: `function (response) {}`)
233Interceptor called on every response. Takes Axios response object as an arg. Default does nothing. Pass your own function to log any desired data.
234
235### Reference documentation
236
237The [Contentful's JS SDK reference](https://contentful.github.io/contentful-management.js) documents what objects and methods are exposed by this library, what arguments they expect and what kind of data is returned.
238
239Most methods also have examples which show you how to use them.
240
241You can start by looking at the top level `contentfulManagement` namespace.
242
243The `ContentfulClientAPI` namespace defines the methods at the Client level which allow you to create and get spaces.
244
245The `ContentfulSpaceAPI` namespace defines the methods at the Space level which allow you to create and get entries, assets, content types and other possible entities.
246
247The `Entry`, `Asset` and `ContentType` namespaces show you the instance methods you can use on each of these entities, once you retrieve them from the server.
248
249> From version 1.0.0 onwards, you can access documentation for a specific version by visiting `https://contentful.github.io/contentful-management.js/contentful-management/<VERSION>`
250
251### Contentful JavaScript resources
252
253Read the [Contentful for JavaScript](https://www.contentful.com/developers/docs/javascript/) page for Tutorials, Demo Apps, and more information on other ways of using JavaScript with Contentful
254
255### REST API reference
256
257This library is a wrapper around our Contentful Management REST API. Some more specific details such as search parameters and pagination are better explained on the [REST API reference](https://www.contentful.com/developers/docs/references/content-management-api/), and you can also get a better understanding of how the requests look under the hood.
258
259### Legacy contentful-management.js
260
261For versions prior to 1.0.0, you can access documentation at [https://github.com/contentful/contentful-management.js/tree/legacy](https://github.com/contentful/contentful.js/tree/legacy)
262
263## Versioning
264
265This project strictly follows [Semantic Versioning](http://semver.org/) by use of [semantic-release](https://github.com/semantic-release/semantic-release).
266
267This means that new versions are released automatically as fixes, features or breaking changes are released.
268
269You can check the changelog on the [releases](https://github.com/contentful/contentful-management.js/releases) page.
270
271## Migration from contentful-management.js 3.x
272
273The bundle for browsers is now called `contentful-management.browser.min.js` to mark it clearly as browser only bundle. If you need to support IE 11 or other old browsers, you may use the `contentful-management.legacy.min.js`. Node will automatically use the `contentful-management.node.min.js` while bundlers like Webpack will resolve to the new ES-modules version of the library.
274
275No changes to the API of the library were made.
276
277## Migration from contentful-management.js 1.x and older
278
279contentful.js 1.x was a major rewrite, with some API changes. While the base functionality remains the same, some method names have changed, as well as some internal behaviors.
280
281See the [migration guide](MIGRATION.md) for more information.
282
283## Support
284
285If you have a problem with this library, please file an [issue](https://github.com/contentful/contentful-management.js/issues/new) here on GitHub.
286
287If you have other problems with Contentful not related to this library, you can contact [Customer Support](https://support.contentful.com).
288
289## Contributing
290
291See [CONTRIBUTING.md](CONTRIBUTING.md)
292
293## License
294
295MIT