/contentful-response-parser

Parse Contentful HTTP API response and resolve all nested Asset and Entry links

Primary LanguageJavaScriptMIT LicenseMIT

Contentful Response Parser

version MIT License Standard Standard Version Size

This module will parse the Contentful HTTP API response and resolve all nested Asset and Entry links for easier to work with data

Usage

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)
}

Limitations

Currently this library does not support deep nested entries (PRs welcome!)

License

MIT