hapi-resource-z

This is a resource function created for hapi.js in order to in order to reduce the amount of code when writing hapi routes.

Usage no npm install needed!

<script type="module">
  import hapiResourceZ from 'https://cdn.skypack.dev/hapi-resource-z';
</script>

README

hapi-resource

This is a resource function created for hapi.js to reduce the amount of code when writing hapi routes. this package include plural (s/ies) feature

INSTALLATION

npm install hapi-resource-z

USAGE

Given an API controller:

const joi             = require('joi');
var PostsController = {

  index: function(request, reply) {
   ....
  },

  show: function(request, reply) {
    .....
  },

  create: function(request, reply) {
    .....
  },  
}

you can now write:

server.route(
  resource({
    name: 'post',
    controller: PostsController,
    validate: payload: {
      name: joi.string().alphanum().required(),
    },
    methods: ['all', 'show', 'create', 'update', 'delete' ] // optional if not defined (by default * included)
  })
);

Instead of writing:

hapiServer.route([
  {
    method : "GET",
    path : "/posts",
    handler : PostsController.index
  },
  {
    method : "GET",
    path : "/posts/{id}",
    handler : PostsController.show
  },
  ...
]);

You can also easily namespace your routes:


var resource = require('hapi-resource-z');

server.route(
  resource({
    name: 'user',
    controller: UsersController,
    namespace: '/admin'
  })
);

is equivalent to:

hapiServer.route([
  {
    method : "GET",
    path : "/admin/users",
    handler : UsersController.index
  },
  {
    method : "GET",
    path : "/admin/users/{id}",
    handler : UsersController.show
  },
  ...
]);