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

leisure

v0.1.0

Published

Add hypermedia awareness to your REST services.

Downloads

24

Readme

leisure

Add hypermedia awareness to your REST services.

leisure offers:

  • Content Negotiation for preferred media type.
  • Automatically sets response Content-Type.
  • Work in terms of hypermedia.

Example

app.js

var express = require('express');
var leisure = require('leisure');
var accept = leisure.accept;

var app = express.createServer();

var dashboard = require('./media/dashboard');

app.get('/', accept(dashboard.mediaTypes), function(req, res) {
  var media = dashboard.create(req.accepted);
  res.send(media);
});

app.listen(3000);

./media/dashboard.js

exports.mediaTypes = [
  { contentType: 'application/vnd.shop.Dashboard', formats: ['json', 'xml'] },
  { contentType: 'application/json' },
  { contentType: 'text/xml' }
];

exports.create = function(mediaType) {
  var mediaFactory = {
    'application/vnd.shop.Dashboard': createDashboardMedia(mediaType.format),
    'application/json': createDashboardMedia('json'),
    'text/xml': createDashboardMedia('xml')
  };

  var media = mediaFactory[mediaType.contentType]();
  return media;
};

function createDashboardMedia(format) {
    return function mediaFormat() {
      var formats = {
        'json': '{ "account": { "href":  "/account" }, "products": { "href": "/products" } }',
        'xml': '<dashboard><account href="/account" /><products href="/products" /></dashboard>'
      };

      return formats[format];
    }
}

Run

Launch the app.

$ node app.js

Set the Accept header on a curl request to http://localhost:3000.

$ curl -H "Accept: application/vnd.shop.Dashboard+json" -X GET "http://localhost:3000"
{ "account": { "href":  "/account" }, "products": { "href": "/products" } }

Voila!

Install

$ npm install leisure

Usage

leisure.accept(mediaTypes, [options])

leisure.accept is connect-compatible middleware that compares a list of accepted media types to those supported by the client via the HTTP Accept header and selects the most preferred media type available. The preferred media type is appended to the request as req.accepted. The mediaTypes parameter is required.

mediaTypes

mediaTypes is an array of acceptable media types for the request. Each media type in the array needs a contentType property with the appropriate media type and an optional formats property listing acceptable formats.

Example:

[
  { contentType: 'application/vnd.shop.Order', formats: ['json', 'xml'] }, 
  { contentType: 'application/json' }
]

The mediaTypes array is prioritized by index. In the example above, the preference order is:

  1. application/vnd.shop.Order+json
  2. application/vnd.shop.Order+xml
  3. application/json

options

options is a collection of options for the current middleware instance.

Example:

leisure.accept([{ contentType: 'text/xml' }], { strictMode: true });
options.strictMode

strictMode sends a 406 Not Acceptable response when no media type is matched. The default value is taken from leisure.options.strictMode.

leisure.options

Global options in leisure.

console.log(leisure.options.strictMode); // => false

leisure.setOption(key, val)

setOption sets a global option in leisure.

strictMode

When the strictMode option is true, leisure sends a 406 Not Acceptable response when there is no matching Accept value in the request. When strictMode is false, leisure will respond with the most preferable media type. The default value is false. This can be overriden for each middleware instance (see leisure.accept).

leisure.setOptions('strictMode', true);

Tests

Tests are written in mocha. To run, use:

$ npm test

License

MIT/X11