ES6 Component for controllers with filter support.
import Controller from "forbin";
const logRequest = Symbol(),
authenticate = Symbol(),
logResponse = Symbol();
class ApplicationController extends Controller {
filters(...options) {
this.before(this[logRequest]);
this.before([this.changePassword, this.changeUsername], this[authenticate]);
this.after(this.changePassword, this[logResponse]);
this.skip(this.changePassword, this[logRequest]);
}
[logRequest](request, response, next) {
//log request before every action on the controller
next();
}
[logResponse](request, response, next) {
//log response after some action
next();
}
[authenticate](request, response, next) {
//authenticate user
next();
}
changeUsername(request, response) {
//here the request is logged and the client authenticated
//do your action and send the response
}
changePassword(request, response) {
//here the client is authenticated but the log skipped
//do your action and send the response
//after this action the response will be logged
}
getInfo(request, response) {
//here the request is logged but is a public operation
//do your action and send the response
}
}
Every single build and release is automatically tested on the following platforms:
If your platform is not listed above, you can test your local environment for compatibility by copying and pasting the following commands into your terminal:
npm install forbin
cd node_modules/forbin
gulp test-local
Copy and paste the following command into your terminal to install Forbin:
npm install forbin --save
// ES6
import Controller from "forbin";
// ES5
var Controller = require("forbin");
// Require.js
define(["require"] , function (require) {
var Controller = require("forbin");
});
You should extend the base Controller provided within forbin in order to apply filters and take advantage of the provided functionality.
As the above example shows, you can define a specific filter to every action on the controller, to a specific one, or to an array of actions.
There are three filter functions available right now:
- before
- after
- skip
All those functions share the same interface:
before(filterToApply); //this will apply a before filter to all actions
before(functionToApplyTheFilterTo, filterToApply); //this will apply a before filter to a specific action
before(arrayOfFunctionsToApplyTheFilterTo, filterToApply); //this will apply the filter to the provided array of actions
after(filterToApply); //this will apply an after filter to all actions
after(functionToApplyTheFilterTo, filterToApply); //this will apply an after filter to a specific action
after(arrayOfFunctionsToApplyTheFilterTo, filterToApply); //this will apply the filter to the provided array of actions
skip(filterToApply); //this will skip a filter on all actions
skip(functionToApplyTheFilterTo, filterToApply); //this will skip a filter on a specific action
skip(arrayOfFunctionsToApplyTheFilterTo, filterToApply); //this will skip the filter on the provided array of actions
See something that could use improvement? Have a great feature idea? We listen!
You can submit your ideas through our issues system, or make the modifications yourself and submit them to us in the form of a GitHub pull request.
We always aim to be friendly and helpful.
It's easy to run the test suite locally, and highly recommended if you're using Forbin.js on a platform we aren't automatically testing for.
npm test