npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

contentful-js-client

v0.2.2

Published

A dependency free lightweight JavaScript client for Contentful's Content Delivery API

Downloads

10

Readme

Contentful JS Client

A dependency free lightweight JavaScript client for Contentful's Content Delivery API

Contentful JS Client is a simple and lightweight JavaScript client that is less than 2Kb minified. It has no external dependencies and can be used either in the browser or NodeJS. It relies upon window.fetch() or the native request class respectively.

Features:

  • Retrieves content using a single Promised method called getEntries()
  • Automatically denormalizes the returned data so that linked entries and assets are directly accessible on their respective keys
  • Can be used in the browser or NodeJS
  • Written in contemporary JavaScript (e.g. ES6+) and should work in all evergreen browsers
  • No jQuery, no Babel*, no Webpack, etc.

* Babel ESLint and Babili are used for linting and minification respectively but there is no transpiling in this project.

Why?

Contentful provides a rock solid JavaScript client with broad feature set. However, it does far more than I need for most of my Contenful driven applications and at 77Kb for the minified version is actually missing what I consider to be a critical feature; denormalizing returned results. By contrast Contentful JS Client is under 2Kb minified, can retrieve entries with modest query requirements and automatically denormalizes data.

Installation and Usage

First, install the package with your preferred package manager. E.g npm install contentful-js-client or yarn contentful-js-client

For Node

// Following read-only creds point to a copy of Contentful's sample photo galery
const config = {
  access_token: '0aaf00d1373121cf60bf386add9e16d4d71b92b27ba16fd39d71ce67cf35af81',
  space: 'r6crsmn3pvic'
}

const ContentfulClient = require('contentful-js-client');
const contentfulClient = new ContentfulClient(config);

contentfulClient.getEntries({ content_type: '7leLzv8hW06amGmke86y8G', fields: {}, include: 3 })
  .then(galleryItems => {
    console.log(JSON.stringify(galleryItems, null, 2));
  })
  .catch(reason => {
    console.error(reason);
  })

For the browser copy the built file from the repository to your scripts folder choosing the regular or minified versions depending on your build workflow. Then place the following at the bottom of your HTML just above the closing body tag.

<script src="src/js/contentfulClient.min.js"></script>
<script>
  // Following read-only creds point to a copy of Contentful's sample photo galery
  const config = {
    access_token: '0aaf00d1373121cf60bf386add9e16d4d71b92b27ba16fd39d71ce67cf35af81',
    space: 'r6crsmn3pvic'
  }
  
  const contentfulClient = new Contentful(config);

  contentfulClient.getEntries({ content_type: '7leLzv8hW06amGmke86y8G', fields: {}, include: 3 })
    .then(galleryItems => {
      document.getElementById('results').innerHTML = JSON.stringify(galleryItems, null, 2);
    })
    .catch(reason => {
      document.getElementById('results').innerHTML = reason;
    })
</script>

What is Missing?

Honestly, probably lots. While this lightweight client meets the need for three of my current Contentful driven projects it may not have all the features you need. While the goal is to keep the client as lightweight as possible I would not be averse to adding additional features on a case by case basis. Feel free to post an issue to Github or better, send me a PR.

One feature that will be added soon is support for ordering results.

Built With

Change Log

v0.2.2 (2017-07-29)

  • Added simple ordering capability with order: '-fields.{field-name}' syntax

v0.2.1 (2017-07-28)

  • Fixed embarrassing spelling mistake with the word dependency
  • Fixed mismatched names between package.json and the final output file

v0.2.0 (2017-07-26)

  • Moved to its own repository
  • Started creating proper tests
  • Improved error handling

v0.1.0 (2017-05-27)

  • Originally created as a standalone class in bigger project