hapi-octopus

1.0.0 • Public • Published

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) => (* 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);
}
 

Package Sidebar

Install

npm i hapi-octopus

Weekly Downloads

0

Version

1.0.0

License

SEE LICENSE IN <LICENSE.md>

Unpacked Size

13.3 kB

Total Files

6

Last publish

Collaborators

  • ar4mirez