-
-
Save JamesTryand/10595654e3320dfec4fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Koa middleware for Swagger Framework. | |
*/ | |
'use strict'; | |
/** | |
* Initialize a new `Koa`. | |
* | |
* @param {Framework} framework | |
* @api public | |
*/ | |
function Koa(framework) { | |
if (!(this instanceof Koa)) { | |
return new Koa(framework); | |
} | |
this.framework = framework; | |
this.framework.setup(); | |
} | |
/** | |
* Create and return Koa middleware. | |
* | |
* @api public | |
*/ | |
Koa.prototype.middleware = function() { | |
var options = {}; | |
options.setup = function(req, res) { | |
var sf = req.sf; | |
req.sf.responseMessage = function(reply) { | |
sf._ctx.response.status = reply.statusCode; | |
sf._ctx.response.body = reply.body; | |
sf._done(null, true); | |
}; | |
}; | |
var route = this.framework.router.setup(options); | |
return function *(next) { | |
var ctx = this; | |
var match = route(ctx.req, ctx.res); | |
var sf = ctx.req.sf; | |
// match path | |
if (match && match.node && match.node.handle) { | |
sf._ctx = ctx; | |
sf._params = match.param; | |
function handled() { | |
return function(cb) { | |
sf._done = cb; | |
match.node.handle(ctx.req, ctx.res, cb); | |
} | |
} | |
if (yield handled()) return; | |
} | |
yield next | |
}; | |
}; | |
/** | |
* Expose swagger. | |
*/ | |
exports.Koa = Koa; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment