Skip to content

Instantly share code, notes, and snippets.

@silas
Created March 10, 2014 01:56
Show Gist options
  • Save silas/9458207 to your computer and use it in GitHub Desktop.
Save silas/9458207 to your computer and use it in GitHub Desktop.
Example Swagger Framework usage in Koa
/**
* 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;
@lrvick
Copy link

lrvick commented Jul 15, 2014

I am not seeing here where you are making a reference to swagger at all. Is this just making a koa instance compatible with an existing node/swagger project? Can you elaborate how this is used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment