hapi-pkg
Hapi Plugin which provides a JSON API to object properties.
Usage
One simple source of object properties all our apps have is package.json
.
var Hapi = ; var server = ;server; server; // GET "/pkg" returns entire package.json// GET "/pkg/name" return {"name": "my-module"}// GET "/pkg/version" returns {"version": "1.0.0"}// GET "/pkg/dependencies" returns {"dependencies": { "lodash": "^3.7.0" } }// GET "/pkg/dependencies/lodash" returns {"lodash": "^3.7.0"}
Actually though, this plugin has nothing to do with package.json
Yeah, you can load up your package.json
. How often is all of that data actually useful? Like never.
So why did I even write this plugin? Well, load balancers have this thing called a "healthcheck", which is a route on
your app server which is expected to be 200 OK
. I found myself writing the same "healthcheck" route on every new
hapi server I make. With hapi-pkg
and it's endpoint option, I can configure a nice "health" route as part of
loading my plugins.
server;
GET "/health"
=> 200 OK {"status": "ok"}
Here's a crazy idea.
You could serve a mock api for a prototype based on some static data in JSON files or even hardcoded objects and arrays.
let Hapi = ;let hapiPkg = ; let server = ;server; // Let's hard code some arrays to be our "database".let authors = name: 'will' alias: 'nackjicholson' posts: 0 2 name: 'peter' alias: 'spiderman' posts: 1 ; let posts = title: 'Secret to Web Development' content: 'Regular stretching to prevent carpal tunnel syndrome' author: 0 title: 'Secret to Web Slinging' content: 'Regular stretching to prevent carpal tunnel syndrome' author: 1 title: 'How NOT to Build a Prototype' content: 'You are looking at it.' author: 0 ; // You can load this plugin multiple times!// Providing two new resource routes, one for "/authors" and another for "/posts"server; // GET "/authors" => authors collection.// GET "/authors/0" => {"name":"will","alias":"nackjicholson","posts": [0,2]}// GET "/authors/0/posts" => {"posts": [0,2]}// GET "/posts" => posts collection// GET "/posts/2/author" => {"author": 0}
I know that's not a perfect api, but it's pretty good for something I implemented on accident 😃 You're on your own for the PUT/POST/DELETE routes.
Options
pkg {object} required
The plugin will not load if this option is not provided as an object literal.
server;
endpoint {string} default:'pkg'
Routes for this plugin are prefixed with "/pkg" by default. If you'd like to customize that, you can use the endpoint
option.
server;
config {object} optional
Sets the route configuration options for the pkg routes. For instance this is how you would configure an authentication strategy for your pkg routes.
let server = ;server;server;
Contribute
Please open issues, and if you have something to add feel free to make a Pull Request. This plugin is written in ES6 and compiled for use by babel. All code contributions should be 100% covered by tests, and fully adherent to linting by jshint and .jscs configs.