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

hapi-octopus

v1.0.0

Published

HAPI plugin to auto-load routes, methods, pres, handlers and more.

Downloads

19

Readme

hapi-octopus

travis build codecov coverage version downloads SEE LINCESE semantic-release

hapi v17 version

A multi-purpose plugin that allows you to autoload methods, handlers, routes and decorators using a simple signature convention.

Is currently in a sort of stable version. I'm trying to keep it with 100% coverage but just be aware the API might change a little based on my needs or beacuse I love refactoring :p. Any help is apreciated if you are nice and polite.

Installation

npm i --save hapi-octopus

Usage

const octopus = require('hapi-octopus');

// using server register.
await server.register({
  plugin: octopus,
  options: {
    methods: {
      cwd: `${process.cwd()}/methods`
    },
    handlers: {
      cwd: `${process.cwd()}/handlers`
    },
    routes: {
      cwd: `${process.cwd()}/routes`
    },
    decorators: {
      cwd: `${process.cwd()}/decorators`
    }
  }
}});

// using manifest.
{
  ...
  registration: [
    {
      plugin: octopus,
      options: {
        methods: {
          cwd: `${process.cwd()}/methods`
        },
        handlers: {
          cwd: `${process.cwd()}/handlers`
        },
        routes: {
          cwd: `${process.cwd()}/routes`
        },
        decorators: {
          cwd: `${process.cwd()}/decorators`
        }
      }
    }
  ]
}

Register methods.

Signature

name: string: optional if not present export.key will be used instead. ex: exports.multiply

method: function: required body of method in hapijs. methods

options: object: optional same in hapijs. methods

prefix: string: optional if not present filename will be used instead. ex: math.js.


// /path/to/methods/math.js

exports.mulitply = {
  method: (a, b) => (a * b),
  options: {},
};
/*
  this method will be available as:
    - server.methods.math.multiply(2, 4);
  optional values prefix and name are the same as saying
    - prefix = 'math'
    - name = 'multiply'
*/

Register handlers.

Signature

name: string: optional if not present export.key will be used instead. ex: exports.multiply

method: function: required see in hapijs. handlers

options: object: optional same in hapijs. handlers

prefix: string: optional if not present filename will be used instead. ex: math.js.

// /path/to/handlers/customer.js

exports.all = {
  method: (route, options) => {
    return (request, h) => {
      ...
      return {
        total: 10,
        data: customers
      })
    }
  }
};

/*
  this method will be available as:
    - server.methods.math.multiply(2, 4);
  optional values prefix and name are the same as saying
    - prefix = 'customer'
    - name = 'all'
*/

// in a route you can use it like this.

server.route({
  method: 'GET',
  path: '/customer/create',
  handler: {
    customerAll: {} // always prefix+name camelCase.
  }
})

Register routes.

Routes can be registered in 2 ways: exporting an array|object or using a function.

Array| Object Signature

routes: array|object: required for this signature should return an array or object of hapijs routes.

exports.customers = {
  routes: [
    {method: 'GET', path: '/customers', handler: {customerAll: {}}},
    {method: 'POST', path: '/customers', handler: {customerCreate: {}}}
  ]
}

exports.update = {
  routes: {
    method: 'PATCH',
    path: '/customers/{id}',
    handler: {
      customerUpdate: {}
    }
  }
}

Function Signature

method: function: required for this signature should receive a server object.

options: object: optional options to be passed a long with server object.

exports.customers = {
  method: (server, options) => {
    console.log(options);

    server.route([
      {method: 'GET', path: '/customers', handler: {customerAll: {}}},
      {method: 'POST', path: '/customers', handler: {customerCreate: {}}}
    ])
  }
}

Register decorators.

decorate: string: required and accept only request|toolkit|server.

name: string: optional name must be unique since will be used as accessor from decoration, if name is not passed exports.key will be used instead.

method: any: required could be anything from object|array|function|string is what you'll get from decoration.

see more info about decorators

exports.reply404 = {
  decorate: 'toolkit',
  method: () => {
    return this.response({
      message: 'Standard 404 error'
    });
  }
};

// will be accesible from a handler like:

...
handler: (request, h) => {
  return h.response(({anotherMessage: 'ahh whatever really.'}).code(404);
}