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

dolman

v0.4.0

Published

Light express app wrapper to develop an API comfortably.

Downloads

20

Readme

Build Status

Dolman

dolman is a very light wrapper around an express app meaning to provide some comfortable utilities for one developing a REST API.

As such, dolman overload express' response object to provide handy methods such as .ok or .notFound and provides a easy way to deploy routers using straightforward configuration.

Summary

Installation

You can easily install dolman using npm:

npm install --save dolman

Usage

Wrapping an express app

var express = require('express'),
    wrap = require('dolman');

var app = express();

// Wrapping the express app & providing some options:
var dolman = wrap(app);

// Example with a custom typology:
var dolman = wrap(app, {typology: myCustomTypology});

// Example with a custom logger:
var dolman = wrap(app, {
  logger: {
    error: function(msg, data) {},
    warn: ...
  }
});

Responses

A wrapped express app will have an enhanced response object:

var app = express(),
    dolman = wrap(app);

app.get('/hello', function(req, res) {
  return res.ok({hello: 'world'});
});

// The following methods are available:
res.ok([result]);
res.created([result]);
res.badRequest([reason]);
res.unauthorized();
res.forbidden();
res.notFound([reason]);
res.serverError([error]);

Note that the responses will be sent as JSON and will follow this standard:

// Typical success
{
  status: 'ok',
  code: 200,
  result: {
    hello: 'world'
  }
}

// Typical error
{
  status: 'error',
  code: 400,
  error: {
    message: 'Bad Request',
    reason: {
      source: 'body',
      expecting: {
        id: 'number'
      },
      sent: {
        id: 'whatever'
      }
    }
  }
}

Router

dolman exposes an easy way to create express routers by using lists of configuration objects representing a single route.

var app = express(),
    dolman = wrap(app);

// Creating a router with a single route
var router = dolman.router([
  {
    url: '/hello',
    action: function(req, res) {
      return res.ok({hello: req.query.name});
    }
  }
]);

// Creating the same router with beforehand authentication
var router = dolman.router(authMiddleware, routes);

app.use(router);
// or to use a namespace
app.use('/prefix', router);

A route can be described likewise:

{
  // [required] - The matching express pattern.
  // Remember that this could even be an array or a regex.
  url: '/hello/:name',

  // [required] - The action to perform.
  action: function(req, res) {
    return res.ok({hello: req.params.name});
  },

  // You can alternatively pass an array of functions
  action: [fn1, fn2],

  // [optional] - The route's name.
  name: 'hello',

  // [optional] - The route's description.
  description: 'Say hello to someone.',

  // [optional] - The accepted methods.
  // If not provided, defaults to 'ALL'
  method: 'POST',

  // The following also works:
  methods: ['POST', 'PUT'],

  // [optional] - Validation for params, query and body.
  validate: {
    params: {
      name: 'string'
    },
    query: {
      age: '?number'
    },
    body: {
      title: 'string',
      content: {
        id: '?number',
        text: 'string'
      }
    }
  },

  // [optional] - Cache specifications
  cache: 'hello',

  // Or, if the cached response is relative to the request's parameters:
  cache: {
    key: 'hello',
    hasher: function(req) {
      return req.params.name;
    }
  },

  // [optional] You can leverage HTTP Cache-Control headers as well.
  httpCache: 'no-cache, no-store, must-revalidate',

  // Or, shorthand and fluent way to set 'private, max-age':
  httpCache: {
    hours: 2 // seconds, minutes, hours, days, weeks
  },

  // [optional] - A typology definition used to check and clamp the data output.
  mask: {
    name: 'string',
    age: '?number'
  }
}

Note that the validation specifications are handled by the typology library.

Specs

dolman gives you the opportunity to retrieve very easily the specifications of your API if you need them, for instance, to hydrate a client elsewhere.

The output will follow the SPORE specifications.

var express = require('express'),
    wrap = require('dolman');

var app = express(),
    dolman = wrap(express);

var router = dolman.router([
  {
    url: '/hello',
    name: 'hello',
    description: 'Say hello.',
    action: function(req, res) {
      return res.ok({hello: 'world'});
    }
  }
]);

app.use('/nested', router);

dolman.specs();
>>> {
  formats: ['json'],
  methods: {
    hello: {
      path: '/nested/hello',
      name: 'hello',
      description: 'Say hello.'
    }
  }
}

Note that dolman will only output your routes having a name.

A dolman is a loose garment with narrow sleeves, an opening in the front and lavish braids. While originating from Turkey, this garment was mostly worn as a jacket by hussars.

License

MIT