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

loveboat

v1.5.0

Published

the hapi route config preprocessor

Downloads

10

Readme

loveboat

the hapi route config preprocessor

Build Status Coverage Status

Usage

Loveboat is a system for creating and registering transformations to be applied to hapi route configurations. This can be used to make your route configurations more communicative and more expressive!

Imagine the ability to add multiple paths on a single route, define route post-requisites (in addition to pre-requisites), or have your routes give themselves tags based upon other pieces of their definition.

Loveboat transforms are defined in such a way that they cannot conflict with each other. That is, two transformations can only act on the same portion of a route configuration if they explicitly say that they are aware of each other and have a well-defined order in which they act. So go ahead and transform your route configurations without worry!

Community Transforms

Example

Here's a long-form example using a custom transform,

const Hapi = require('hapi');
const Joi = require('joi');
const Loveboat = require('loveboat');

const server = new Hapi.Server();
server.connection();

server.register(Loveboat, (err) => {

    // Register route config transforms to use
    // specifically for this server (or plugin).
    server.routeTransforms([{
        name: 'patch-to-post',
        root: 'method',
        match: Joi.string().valid('patch'),
        handler: (method) => 'post'
    }]);

    // Transform and register routes!
    server.loveboat([
        {
            method: 'patch',  // This route definition will be transformed
            path: '/',        // to use POST rather than PATCH.
            handler: function (request, reply) {
                reply('love');
            }
        }, {
            method: 'get',    // This route definition will not be transformed
            path: '/',        // because it doesn't have a matching method.
            handler: function (request, reply) {
                reply('boat');
            }
        }
    ]);

});

API

Plugin Registration

Loveboat is a hapi plugin with the following options,

  • transforms - an object or array of objects where each one is either,
    • a route transform, or
    • an object with the following,
      • transform - a route transform.
      • options - options to be passed to the transform's handler when called.
      • before - a name or list of transform names before which this transform should be applied.
      • after - a name or list of transform names after which this transform should be applied.

Decorations

Loveboat places several decorations on the hapi Server object.

server.loveboat(routes, [transforms, [onlySpecified]])

Registers the specified routes passed through those transforms specified,

  1. by the optional transforms argument (of the same form as the transforms registration option),
  2. for the current plugin (active realm) via server.routeTransforms(), and
  3. for the entire server via loveboat's registration options.

However, if onlySpecified is true then only transforms will be applied.

server.routeTransforms(transforms)

Registers a transform or list of transforms to be used specifically with the current plugin (in the active realm). The transforms argument is of the same form as the transforms registration option.

Transforms

A transform specifies a piece of hapi a route configuration that it may act on, a schema that determines if it should act, and a handler that performs the transformation. A transform may also specify whether it comes before or after other transforms. It is an object of the form,

  • name - (required) a name for this transform.
  • root - (required) a string specifying which piece of route configuration this transform acts upon, e.g. 'config.auth.strategies' (see Hoek.reach()). If the transform acts on the entire route configuration, null should be passed.
  • match - (required) a Joi schema or a function used to determine if the configuration root (as described above) should be acted upon by this transform. The piece of route configuration at transform.root is passed through this validation.

When passed as a function match has the signature function(root, route) where,

  • root - the configuration root derived from a route configuration at transform.root.
  • route - the entire route configuration from which root is derived.

The function should return an object having error and value keys identically to Joi.validate().

  • joi - a list of options to use with Joi when match is a Joi schema.
  • consume - a string or array of strings specifying pieces of route configuration that are consumed by this transform (see Hoek.reach()). The listed paths are safely removed from the route definition prior to registering the route. This should be utilized when the transform would like to extend the hapi route config API with properties that do not otherwise exist.
  • handler - (required) a function that performs a transformation on this transform's root. It has signature function(root, route, server, options) where,
    • root - the configuration value derived from a route configuration at transform.root.
    • route - the entire route configuration from which root is derived.
    • server - the server onto which this route will be registered (possibly that of a plugin).
    • options - options passed during the transform's registration.

The function should not mutate root or route. It should return a new value for root or list of values for root.

If a list (array) is returned, then each item represents a value for root in a new route configuration minimally-deep-cloned from route. In that case, one route configuration would pass through the transform and multiple route configurations would result with different values at transform.root.

  • before - a name or list of transform names before which this transform should be applied.
  • after - a name or list of transform names after which this transform should be applied.