node v4.9.1
version: 1.0.2
content-pull was created to wrap around contentful.js to provide additional functions as well as parsing responses before resolving promises.
const exampleSpaceId = process.env.CONTENTFUL_SPACE_ID; const exampleAccessKey = process.env.CONTENTFUL_ACCESS_KEY; const exampleContentType = 'book'; const Contentpull = require('contentpull'); const contentpull = new Contentpull(exampleSpaceId, exampleAccessKey); await contentpull.getEntriesByType(exampleContentType);
You can also extend the functionality to add additional functionality for simplifying and normalizing your contentful requests.
class Dal extends Contentpull { getBooks() { return this.getEntriesByType(exampleContentType); } } const dal = new Dal(exampleSpaceId, exampleAccessKey); await dal.getBooks();
Parsing is also another big functionality of content-pull. Parsing is done automatically when the Promise resolves.
await dal.getBooks().parse().then(books => { return `The first book's author is '${books.items[0]}'.` });
"If the above operation became common enough, you might consider writing your own parser. Parsers can be customized in the constructor as part of the configuration parameter. Each parser is seperated by contentful data-model structures: > Space > Entry > Asset > Array > Link When writing a parser, be sure to edit the passed argument directly. This is because contentful.js creates virtual references to linked content types that are referenced in multiple locations. To ensure that those objects are updated in all locations content-pull maintains these references. Below is an example of replacing the built-in Asset and Array parsers provided by content-pull, with alternative parsers configured in the constructur. By overriding the parser with a function that simply returns the raw object, the Asset parser will match exactly what contentful.js returns. The Array parser, on the other hand, will now delete the "sys" property, and parse all nested items.
const ParsingExample = new Contentpull(exampleSpaceId, exampleAccessKey, { parsers: { Asset: (asset) => { return asset; }, Array: (array, parser) => { = 'bar'; delete array.sys; => parser(item)); return array; } } }); await ParsingExample.getEntriesByType(exampleContentType).parse();

no comments

    sign in to comment