This module will parse the Contentful HTTP API response and resolve all nested Asset and Entry links for easier to work with data
import axios from 'axios'
import { generateItemObject, generateItemObjectArray } from 'contentful-response-parser'
const spaceId = 'xxxxxx'
const accessToken = 'xxxxxx'
const contenfulAPI = axios.create({
method: 'get',
baseURL: `https://cdn.contentful.com/spaces/${spaceId}/entries`,
headers: { Authorization: `Bearer ${accessToken}` }
})
export const getAllBlogPosts = () => {
const params = { content_type: 'blogPost', include: 10 }
return contenfulAPI.get('/', {params})
.then(response => response.data)
.then(generateItemObjectArray)
}
export const getBlogPost = (postSlug) => {
const params = { content_type: 'blogPost', include: 10, 'fields.slug': postSlug }
return contenfulAPI.get('/', {params})
.then(response => response.data)
.then(generateItemObject)
}
Currently this library does not support deep nested entries (PRs welcome!)
MIT