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-data

v1.2.0

Published

Get Contentful data and return as a javascript object.

Downloads

7

Readme

Get Contentful data and return as a javascript object.

About

This tool solves the problem of dowloading Contentful entires via the Contentful Delivery API and passing to templates during a static site build. Contentful is a content management platform for websites, web applications, mobile apps and connected devices. It allows you to create, edit & manange content in the cloud and publish it anywhere via a powerful API.

Features

Requirements

  • node.js (https://nodejs.org)

Getting Started

To use contentful-data you will need to install it and get credentials in order to access your content in Contentful.

Installation

In node, using npm:

npm install contentful-data

Authentication

In order to get content from Contentful, you have to pass in your credentials as params, which are handed off to Contentful's Javascript SDK under the hood.

To create API keys, login to Contentful, open the space from which you want to download content (top left corner lists all the spaces), and navigate to the APIs area. Open the API Keys section and create your first token. You will also need your Space ID.

For more information, refer to Contentful's REST API reference on Authentication.

Basic Usage (Gulp)

'use strict';

var gulp = require('gulp');
var contentfulData = require('contentful-data');

// Global object to store returned data.
var entries = null;

gulp.task('data', function(cb){
  // Only get entries once per build. Prevents numerous API calls if build run often after a gulp watch, for example.  
  var params = {
    apiKey: 'your-api-key',
    spaceId: 'your-space-id'
  };

  return contentfulData(params, function(err, data){
    if(!err) {
      entries = data;
      cb();
    }
  });
});

gulp.task('default', ['data'], function(){
  console.log('Retrieved contenful entries.');
});

Params

| name | type | description | |------|------|-------------| | apiKey | string | A production Content Delivery API key. | | spaceId | string | Alphanumeric id of the space to retrieve. | | opts | object | Object of available options. | | opts.filter | array | Array of content types to get. If no filter is passed, all content types will be downloaded. | | opts.level | number | Number of links to resolve. |
| opts.key | string | The id of the field to use for identifying an entry (e.g. slug). If no key is supplied, the default sys.id of the entry will be used instead. | | opts.locale | string | the value of locales requested from the contentful space. If no key is supplied, the default key, representing the default language of the space will be used instead. |

Examples

Gulp with Pug

This tool was primarily meant to work in a Gulp build using pug templates to generate a static site.

var contentfulData = require('contentful-data');
var pug = require('pug');
var entries = null;

gulp.task('data', function(cb){
  // Only get entries once per build. Prevents numerous API calls if build run often after a gulp watch, for example.
  if (!entries) {
    
    var params = {
      apiKey: 'your-api-key',
      spaceId: 'your-space-id',
      opts: {
        level: 2
      }
    };

    return contentfulData(params, function(err, data){
      if(!err) {
        entries = data;
        cb();
      } else {
        console.error('error:' + err);
      }
    });
  } else {
    cb();
  }
})

gulp.task('pug', ['data'], function(cb){
  for (var pageId in entries.page){

    // Assign nav and page data to template
    var locals = {
      navs: entries.navList,
      page: entries.page[pageId]
    }

    // Get pug template to use.
    var template = entries.page[pageId].fields.template;
    
    // Compile pug template
    var compiled = pug.compileFile('./src/templates/' + template + '.pug')(locals);
    
    // Define path to output template
    var path = 'path/to/output/compiled/template';
    path = path + '/' + locals.page.fields.slug;
    mkpath.sync(path);
    
    // Write compiled pug template
    var filename = path + '/index.html';
    fs.writeFileSync(filename,  compiled);
  }

  cb();
});

gulp.task('default', ['pug'], function(){
  console.log('Watching files for changes.');

  gulp.watch(['path-to-pug-templates'], ['pug']);
});

License

MIT