What is contentful-sdk-core?
The contentful-sdk-core npm package provides core functionalities for interacting with the Contentful Content Delivery API. It is designed to be a foundational library that other Contentful SDKs can build upon, offering utilities for making HTTP requests, handling responses, and managing errors.
What are contentful-sdk-core's main functionalities?
HTTP Client
This feature allows you to create an HTTP client configured to interact with the Contentful API. The code sample demonstrates how to create an HTTP client using Axios and make a GET request to fetch entries.
const { createHttpClient } = require('contentful-sdk-core');
const axios = require('axios');
const httpClient = createHttpClient(axios, {
accessToken: 'your_access_token',
space: 'your_space_id'
});
httpClient.get('/entries')
.then(response => console.log(response.data))
.catch(error => console.error(error));
Error Handling
This feature provides a standardized way to handle errors that occur during API requests. The code sample shows how to use the errorHandler function to manage errors from a GET request.
const { createHttpClient, errorHandler } = require('contentful-sdk-core');
const axios = require('axios');
const httpClient = createHttpClient(axios, {
accessToken: 'your_access_token',
space: 'your_space_id'
});
httpClient.get('/entries')
.then(response => console.log(response.data))
.catch(error => errorHandler(error));
Request Configuration
This feature allows you to configure various aspects of the HTTP requests, such as headers and query parameters. The code sample demonstrates how to add custom headers to the HTTP client.
const { createHttpClient } = require('contentful-sdk-core');
const axios = require('axios');
const httpClient = createHttpClient(axios, {
accessToken: 'your_access_token',
space: 'your_space_id',
headers: {
'X-Custom-Header': 'customValue'
}
});
httpClient.get('/entries')
.then(response => console.log(response.data))
.catch(error => console.error(error));
Other packages similar to contentful-sdk-core
contentful
The 'contentful' package is a higher-level SDK that builds on top of contentful-sdk-core. It provides more abstracted and user-friendly methods for interacting with the Contentful API, such as fetching entries, assets, and content types. It is more feature-rich and easier to use for common tasks compared to contentful-sdk-core.
contentful-management
The 'contentful-management' package is another higher-level SDK that focuses on the Contentful Management API. It allows you to manage content types, entries, assets, and more. Like the 'contentful' package, it builds on top of contentful-sdk-core but is tailored for content management tasks rather than content delivery.
axios
While 'axios' is a general-purpose HTTP client and not specific to Contentful, it is often used in conjunction with contentful-sdk-core to make HTTP requests. It provides a flexible and powerful API for making HTTP requests, handling responses, and managing errors, similar to the core functionalities provided by contentful-sdk-core.
contentful-sdk-core
This package contains some core modules and utilities used by both the contentful.js and contentful-management.js SDKs.
About
Contentful 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.
Installation
npm install --saveDev contentful-sdk-core
Use case
This package contains some core modules and utilities used by both the contentful.js and contentful-management.js SDKs.
Support
We support LTS versions of Node.js and all current browsers in their last 3 versions including IE 11.
ES2015 modules version
For bundlers like webpack2 and rollup we support the module
&
jsnext:main
entry in the package.json
Development
Create the default and the es-modules build:
npm run build
Run Tests:
Run only the unit tests:
npm run test
Run unit tests including coverage report:
npm run test:cover
Run unit tests with coverage report and display the result in your browser:
npm run browser-coverage
Emulate a CI test run:
npm run test:ci-emulate
Enable debug mode for tests:
npm run test:debug