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

mudskipper

v3.0.1

Published

a resourceful routing plugin for hapi

Downloads

18

Readme

##mudskipper

This is a resourceful routing plugin for Hapi. Given an object describing the available resources, a route will be created for each method defined on the resource. For example:

var Hapi = require('hapi');

var resources = {
    users: {
        index: function (request) {
            request.reply(users.all());
        },
        show: function (request) {
            request.reply(users.one(request.params.user_id));
        }
    }
}

var server = new Hapi.Server();
server.register({
    register: require('mudskipper'),
    options: resources
}, function (err) {
    if (err) {
        console.error('failed to load resourceful routes:', err);
    }
});

The above example would create routes for

GET /users
GET /users/{userId}

You'll notice that here, we only supplied functions for the methods. This causes the function to be used as the handler for the route, and defaults applied to the other values. Alternatively, methods can be defined as an object containing anything that's available as part of a route. For example:

var resources = {
    users: {
        create: {
            handler: function (request) {
                request.reply(users.create(request.payload));
            },
            config: {
                payload: 'parse',
                validate: {
                    payload: {
                        username: Types.String().min(8).max(50)
                    }
                }
            }
        }
    }
}

Anything specified in the object will override any defaults that are set within the plugin. If a path is specified, it is not passed through directly. Some modification will be made by the plugin. For example

articles: {
    index: {
        path: '/test'
    }
}

Would create the index route at the path /test. If the articles resource were specified as a child of another resource, the path would be changed to /parent/{parentId}/test

If the path is specified without a leading /, such as

articles: {
    index: {
        path: 'test'
    }
}

Then the index route would be added at the path /articles/test and the nested resource would become /parent/{parentId}/articles/test

Nested resources can be created by using the 'hasOne' or 'hasMany' fields. Circular dependencies are handled appropriately.

var resources = {
    articles: {
        hasOne: 'users',
        index: function (request) {
        }
    },
    users: {
        hasMany: ['articles'],
        index: function (request) {
        }
    }
}

Children may be specified as a string, an object, or an array of any combination of those two. Strings must refer to a top level resource, and objects describe a new resource altogether.

var resources = {
    tests: {
        index: function (request) {
        },
        hasMany: [
            {
                extras: {
                    index: function (request) {
                    }
                }
            }
        ]
    }
}

###Available methods and their defaults

  • index
  • method: 'get'
  • path: '/{name}'
  • show
  • method: 'get'
  • path: '/{name}/{nameId}
  • create
  • method: 'post'
  • path: '/{name}'
  • payload: 'parse'
  • update
  • method: 'put'
  • path: '/{name}/{nameId}
  • payload: 'parse'
  • patch
  • method: 'patch'
  • path: '/{name}/{nameId}
  • payload: 'parse'
  • destroy
  • method: 'delete'
  • path: '/{name}/{nameId}

Note that 'name' is whatever key is found in the resources object (in the above examples, it would be 'users'). nameId will be that name, after an attempt to singularize, and the literal string 'Id' appended to it (the above examples would yield userId).

Additionally, you can specify a top level option uniqueIds: false and ids will be created such as /name/{id}/subname/{subId} rather than attempting to singularize name and subname.