Skip to content

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

License

Notifications You must be signed in to change notification settings

danhayden/contentful-response-parser

Repository files navigation

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

About

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

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published