generator-hapi-wurst
v1.1.0generator-hapi-wurst
Yeoman Generator for hapi.js with wurst-based Routing
- Introduction
- Installation
- Project Generator
- Basic Auth Generator
- Hook Generator
- JWT Auth Generator
- Model Generator
- Query Generator
- Route Generator
- Testing
- Contribution
- License
Introduction
This generator scaffolds a project for a hapi.js based API in combination with the directory based autoloader wurst. For example it is perfect for manage the various versions of your API. The app
generator is quite simple and basic. Use the addional generators to enrich your application.
This plugin is implemented in ECMAScript 6 without any transpilers like babel
.
Additionally standard
and ava
are used to grant a high quality implementation.
Installation
First, install Yeoman and generator-hapi-wurst using Node Package Manager:
$ npm install -g yo
$ npm install -g generator-hapi-wurst
Altenatively use the Yarn Package Manager:
$ yarn global add yo
$ yarn global add generator-hapi-wurst
Project Generator
Navigate to the directory you'd like to use for your project, then run:
$ yo hapi-wurst
Features
wurst
: directory-based route autoloaderglue
: server composerjoi
/boom
/blipp
: helpful plugins of the hapi ecosystemgood
: logging of stdout and responsesnode-config
: environment-based configurationlodash
: helpful libarystandard
oreslint
/airbnb-base
: lintingnyc
/ava
: testing and coverage reports
Structure
/
├── config
│ ├── default.js
│ ├── development.js
│ ├── production.js
│ └── test.js
├── lib
│ ├── auth
│ │ └── index.js
│ ├── hooks
│ │ └── index.js
│ ├── models
│ │ └── index.js
│ ├── plugins
│ │ └── index.js
│ ├── routes
│ │ ├── handlers.js
│ │ └── index.js
│ └── utils
│ └── index.js
├── test
│ ├── helpers
│ │ ├── index.js
│ │ └── wrapp.js
│ └── index.spec.js
├── .eslintrc
├── .gitignore
├── composer.js
├── index.js
├── LICENSE
├── package.json
├── process.json
├── README.md
└── server.js
Just use all the subgenerators after initializing the project with the main generator.
Run the related commands in the project's root directory.
Basic-Auth Generator
Adds basic auth strategy, user
model and utilities.
$ yo hapi-wurst:basic-auth
Features
bcrypt-node
: BCrypt implementationhapi-auth-basic
: Basic auth strategy plugin
Structure
/
└── lib
└── auth
└── basic.js
Hints
/lib/auth/basic.js
Strategy and utilities for basic auth.
Register the strategy in/composer.js
.
Use the registered strategy within your routes./lib/models/user.js
See Model Generator.
Optional.
const basicAuth = require('hapi-auth-basic');
const { strategy } = require('./lib/auth/basic');
const options = {
relativeTo: __dirname,
preRegister: (server, next) => {
server.register([basicAuth], err => {
server.auth.strategy('basic', 'basic', { validateFunc: strategy });
});
next();
}
};
Hook Generator
Adds a basic hook scaffold.
$ yo hapi-wurst:hook
Structure
/
└── lib
└── hooks
└── ${hookName}.js
JWT-Auth Generator
Adds JSON Web Token strategy, user
model and utilities.
$ yo hapi-wurst:jwt-auth
Features
hapi-auth-jwt2
: JSON Web Token strategy plugin
Structure
/
└── lib
└── auth
└── jwt.js
Hints
/lib/auth/jwt.js
Strategy and utilities for JWT auth.
Register the strategy in/composer.js
.
Use the registered strategy within your routes./lib/models/user.js
See Model Generator.
Optional.
const jwtAuth = require('hapi-auth-jwt2');
const { strategy } = require('./lib/auth/jwt');
const options = {
relativeTo: __dirname,
preRegister: (server, next) => {
server.register([jwtAuth], err => {
server.auth.strategy('jwt', 'jwt', { validateFunc: strategy });
});
next();
}
};
Model Generator
Adds a basic mongoose schema/model scaffold or an user
.
$ yo hapi-wurst:model
Features
mongoose
: ODM for mongoDB
Structure
/
└── lib
└── models
├── ${modelName}.js
└── user.js
Hints
/lib/models/**.js
Basic model/schema for mongoose.
Require all models during setup.
Setup a mongoose connection in/composer.js
.
const mongoose = require('mongoose');
const options = {
relativeTo: __dirname,
preRegister: (server, next) => {
mongoose.connect('mongodb://localhost:27017/foobar', {});
next();
}
};
Query Generator
Adds REST-like actions for query parameters like sorting, searching & selecting.
$ yo hapi-wurst:query
Features
- sorting:
?sort=name,-age
- searching:
?name=foobar
- selecting:
?fields=name
Structure
/
└── lib
└── utils
└── query
├── index.js
├── search.js
├── select.js
└── sort.js
Hints
/lib/utils/query
Use the utils e.g. to define mongoose queries.
const Boom = require('boom');
const { query } = require('../utils');
const Model = require('mongoose').model('foobar');
requestHandler(request, reply) {
const fields = query.select(request.query);
const options = { sort: query.sort(request.query) };
const search = query.search(request.query);
Model.find(search, fields, options, (err, result) => {
if (err) return reply(Boom.badRequest(err));
if (!result) return reply(Boom.notFound());
return reply(result);
});
}
Route Generator
Adds a basic route scaffold with handlers and validators.
$ yo hapi-wurst:route
Structure
/
└── lib
└── routes
└── ${routeBase}
├── handlers.js
├── index.js
└── validators.js
Testing
First you have to install all dependencies with npm install
or yarn
.
npm test
Executes all unit tests once.npm run lint
Executes the set up linting.
Contribution
Fork this repository and push in your ideas.
Do not forget to add corresponding basic tests.
License
The MIT License
Copyright (c) 2017 Felix Heck
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.