hapi-octopus
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 = ; // using server register.await server; // using manifest. ... registration: plugin: octopus options: methods: cwd: `/methods` handlers: cwd: `/handlers` routes: cwd: `/routes` decorators: 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 exportsmulitply = 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 exportsall = { return { ... 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
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.
exportscustomers = routes: method: 'GET' path: '/customers' handler: customerAll: {} method: 'POST' path: '/customers' handler: customerCreate: {} exportsupdate = 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.
exportscustomers = { console; server }
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
exportsreply404 = decorate: 'toolkit' { return this; }; // will be accesible from a handler like: ... { return h