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.