Middleware router that merges swagger docs with matching controllers.
Quick reference:
- Controllers for a route are determined by the first tag in the route+method docs
spaggerDoc.paths['/users'].get.tags[0]
-> controller name
- Authentication is set per route by including an
authenticate
tag for the route+methodswaggerdoc.paths['/users/:id/edit'].tags = [fooController, authenticate]
- Validate is run on all routes
- Fleek has a build in validator matching inputs from the docs with inputs from the route. Custom validation can be used
- Controllers are pulled from the
./controllers
directory - Swagger docs will be retrieved from
./api.json
,/swagger.json
,/config/api.json
, or/config/swagger.json
in that order - Full example
var koa = require('koa');
var fleekRouter = require('fleek-router');
var app = koa();
fleekRouter(app);
app.listen(3000);
- Controllers are pulled from the
./custom/controllers
directory - Swagger docs are pulled from
./custom/docs.json
- TODO Full example
var koa = require('koa');
var fleekRouter = require('fleek-router');
var app = koa();
fleekRouter(app, {
controllers : './controllers',
swagger : './custom/docs.json'
authenicate : require('./some_auth_middleware'),
validate : require('./some_val_middleware'),
response : true
});
app.listen(3000);
- Controllers are mapped to the object
- Swagger docs are parsed from the object provided
- TODO Full example
var koa = require('koa');
var fleekRouter = require('fleek-router');
var app = koa();
fleekRouter(app, {
authenicate : require('./some/auth/middleware'),
validate : require('./some/val/middleware'),
swagger : require('./some/swagger/generator')(),
controllers : {
foo : function () {
this.body = { success: true };
}
}
});
app.listen(3000);
- Controllers are pulled from the
./controllers
directory - Swagger docs will be retrieved from
./api.json
,/swagger.json
,/config/api.json
, or/config/swagger.json
in that order - validate and authenticate will use the fleek middleware
- TODO Full example
var koa = require('koa');
var fleekRouter = require('fleek-router');
var app = koa();
fleekRouter(app, {
authenicate : true,
validate : { // powered by fleek-validator
error : function *(err, next) {
this.body = 'uh oh! validation failed',
console.log(err);
}
}
});
app.listen(3000);
- adds a version prefix
String
- use the string passed in as the prefixBoolean
- if true, use theversion_prefix
property from the swagger docs
config.versionPrefix = 'v1';
// OR
config.versionPrefix = true;
- adds a version prefix
String
- use the string passed in as the prefixBoolean
- if true, use thelanguage_prefix
property from the swagger docs
config.languagePrefix = 'en';
// OR
config.languagePrefix = true;
- adds a version prefix
String
- use the string passed in as the prefixBoolean
- if true, use thebasePath
property from the swagger docs
config.basePath = 'v1/en';
// OR
config.basePath = true;
- sets the swagger documentation source for compiling routes.
Object
- javascript object mapping to the swagger doc format exactlyString
- path to the swagger json file (relative or absolute)Undefined
- attempts to find the config in the following places./api.json
,./swagger.json
,./config/api.json
, and./config/swagger.json
config.swagger = undefined; // attempts to resolve internally
// OR
config.swagger = './some/relative/swag.json';
// OR
config.swagger = '/some/absolute/swagger.json';
// OR
config.swagger = require('./a/function/returning/swag')();
- sets the controllers used to map routes from the swagger documentation
- controllers are mapped using the first element in the
tag
array of the route+method documenation - nested controllers are allowed, but must be
.
delimited in the tag
Object
- javascript object of controllersString
- path to the controllers directory (relative or absolute)Undefined
- defaults to
config.controllers = undefined; // defaults to ./controllers
// OR
config.controllers = './some/relative/controllers';
// OR
config.controllers = '/some/absolute/controllers';
// OR
config.controllers = {
foo : function *() {}, // maps to `foo` tag
bar : {
biz : function *() {} // maps to `bar.biz` tag
}
};
- sets the authentication method for the secured routes
- applies to any route with the
authenticate
tag
Function
- function to use for authenticationBoolean
- if true, use thefluent-authenticate
middleware
config.authenticate = true; // use `fluent-authenticate`
// OR
config.authenticate = function *(next) {
var isAuth = someAuthCheck(this);
if (isAuth) {
yield next;
} else {
this.status = 401;
this.body = 'Not Authorized';
}
}
- sets the validation method
Function
- function to use for validationBoolean
- if true, use thefluent-validate
middleware
config.validate = true; // use `fluent-validate`
// OR
config.validate = function *(next) {
var valid = true;
if (this.pathname === '/') {
valid = someValidator.rooValidate(this);
} else if (this.pathname === 'user') {
valid = someValidator.userValidator(this);
}
if (valid) {
yield next;
} else {
this.status = 400;
this.body = 'Invalid data Submitted'
}
}
- middleware to fire before the controller
- used to make changes for every request after all fleek middleware has fired
Function*
- function to executeArray
- array of functions to execture in order
config.middleware = function *(next) {
console.log('Route middleware passed!');
console.log('Executing controller: ' + this.routeConfig.controller);
yield next;
}
// OR
config.middleware = [
function *(next) {
console.log('1');
yield next;
},
function *(next) {
console.log('2');
yield next;
}
]
Built and maintained with by the Hart team.