1.2.18 • Published 3 years ago

contentful-parsers v1.2.18

Weekly downloads
3,476
License
MIT
Repository
github
Last release
3 years ago

🧰 contentful-parsers

npm NPM npm Coveralls github CircleCI Snyk Vulnerabilities for GitHub Repo

Toolbox of useful parsers to use when working with Contentful API responses.

Install

Via npm

npm install contentful-parsers

Via Yarn

yarn add contentful-parsers

Parsers

fieldsParser

Probably the most common parser of the lot. This will take a Contentful response, either an array or a single item, and parse the items to flatten all of the fields objects and remove the majority of the sys objects, except for a reference to the sys.contentType.sys.id, in case you are doing any based on that for your rendering.

How to use

import contentful from 'contentful';
import { fieldsParser } from 'contentful-parsers';

const client = contentful.createClient({
  space: '[SPACE_ID]',
  accessToken: '[ACCESS_TOKEN]',
});

const response = await client.getEntry('[ENTRY_ID]');
const data = fieldsParser(response);

graphqlParser

Takes a standard standard REST response from the Contentful API and restructures it to allow it to be queryable via a GraphQL query.

Since this will probably commonly be used with graphql-anywhere for performing the queries against the parsed data, a basic resolver is included in the paackage as well, contentfulResolver.

How to use

import { graphql } from 'graphql-anywhere/lib/async'
import contentful from 'contentful';
import { graphqlParser, contentfulResolver } from 'contentful-parsers';

const entryQuery = gql`
  entry {
    sys {
      id
    }
    title
    copy
  }
`

const client = contentful.createClient({
  space: '[SPACE_ID]',
  accessToken: '[ACCESS_TOKEN]',
});

const response = await client.getEntry('[ENTRY_ID]');
const parsedData = graphqlParser('entry', response);

graphql(
  contentfulResolver,
  entryQuery,
  parsedData,
)
  .then(data => {
    // data -> Contentful model filtered via GraphQL query
  })
  .catch(error => console.error(error));

License

MIT © Ryan Hefner

1.2.18

3 years ago

1.2.17

3 years ago

1.2.16

4 years ago

1.2.14

4 years ago

1.2.15

4 years ago

1.2.13

4 years ago

1.2.8

4 years ago

1.2.9

4 years ago

1.2.12

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago