Comparing version 0.0.1 to 0.0.2
@@ -49,3 +49,3 @@ var express = require('express') | ||
id: "cheeseList", | ||
type: "array", | ||
type: "Array", | ||
items: { | ||
@@ -52,0 +52,0 @@ //this reference will work in swagger, but not |
28
index.js
@@ -201,3 +201,3 @@ var express = require('express') | ||
function describe(nickname, shortDescription, longDescription) { | ||
var middleware = function(req, res, next) { next(); }; | ||
var middleware = function describeMiddleware(req, res, next) { next(); }; | ||
middleware.nickname = nickname; | ||
@@ -211,3 +211,3 @@ middleware.shortDescription = shortDescription; | ||
function makeValidator(schema) { | ||
return function(req, res, next) { | ||
return function paramValidator(req, res, next) { | ||
var errors = joi.validate(req.params, schema); | ||
@@ -259,3 +259,3 @@ if (errors) { | ||
var types = Array.prototype.slice.call(arguments) | ||
, middleware = function(req, res, next) { | ||
, middleware = function consumesMiddleware(req, res, next) { | ||
@@ -299,2 +299,5 @@ if (types.indexOf(req.header('content-type')) > -1) { | ||
middleware.models = []; | ||
middleware.errorResponses = [ | ||
{ code: 400, reason: "Invalid content-type" } | ||
]; | ||
types.forEach(function(type) { | ||
@@ -310,2 +313,6 @@ if (contentTypes[type]) { | ||
middleware.models.push(contentTypes[type]); | ||
middleware.errorResponses.push({ | ||
code: 400, | ||
reason: "Body does not match schema for " + type | ||
}); | ||
} else { | ||
@@ -321,5 +328,2 @@ middleware.params.push({ | ||
}); | ||
middleware.errorResponses = [ | ||
{ code: 400, reason: "Invalid content-type" } | ||
]; | ||
return middleware; | ||
@@ -330,3 +334,3 @@ } | ||
var types = Array.prototype.slice.call(arguments) | ||
, middleware = function(req, res, next) { | ||
, middleware = function producesMiddleware(req, res, next) { | ||
next(); | ||
@@ -336,4 +340,6 @@ }; | ||
middleware.produces = types; | ||
middleware.models = []; | ||
types.forEach(function(type) { | ||
if (contentTypes[type]) { | ||
middleware.models.push(contentTypes[type]); | ||
middleware.responseClass = contentTypes[type].id; | ||
@@ -367,3 +373,3 @@ } | ||
return function(req, res, next) { | ||
return function jsonBodyMiddleware(req, res, next) { | ||
if (req._body) return next(); | ||
@@ -385,6 +391,8 @@ req.body = req.body || {}; | ||
req.on('end', function(){ | ||
var first = buf.trim()[0]; | ||
buf = buf.trim(); | ||
if (0 == buf.length) { | ||
return next({ code: 400, msg: 'invalid json, empty body' }); | ||
var error = new Error("Invalid json, empty body"); | ||
error.status = 400; | ||
return next(error); | ||
} | ||
@@ -391,0 +399,0 @@ |
{ | ||
"name": "flair-doc", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Swagger-compliant REST API documentation generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -304,4 +304,4 @@ var should = require('should') | ||
flair.describe("newPants", "short pants", "long pants"), | ||
flair.consumes("application/vnd.pants+json", "application/vnd.pants2+json"), | ||
flair.produces("application/vnd.pants+json"), | ||
flair.consumes("application/vnd.custom+json", "application/vnd.custom2+json"), | ||
flair.produces("application/vnd.custom+json"), | ||
function(req, res) { | ||
@@ -323,8 +323,12 @@ res.json({id: 1, name: "pants"}); | ||
.post('/pants') | ||
.set('Content-Type', 'application/vnd.pants+json') | ||
.expect(200, function() { | ||
supertest(app) | ||
.post('/pants') | ||
.set('Content-Type', 'application/vnd.pants2+json') | ||
.expect(200, done); | ||
.set('Content-Type', 'application/vnd.custom+json') | ||
.expect(200, function(err, res) { | ||
if (err) { | ||
done(err); | ||
} else { | ||
supertest(app) | ||
.post('/pants') | ||
.set('Content-Type', 'application/vnd.custom2+json') | ||
.expect(200, done); | ||
} | ||
}); | ||
@@ -338,4 +342,4 @@ }); | ||
res.body.apis[0].operations[0].consumes.should.have.length(2); | ||
res.body.apis[0].operations[0].consumes.should.include('application/vnd.pants+json'); | ||
res.body.apis[0].operations[0].consumes.should.include('application/vnd.pants2+json'); | ||
res.body.apis[0].operations[0].consumes.should.include('application/vnd.custom+json'); | ||
res.body.apis[0].operations[0].consumes.should.include('application/vnd.custom2+json'); | ||
done(err); | ||
@@ -359,3 +363,3 @@ }); | ||
values: [ | ||
"application/vnd.pants+json", "application/vnd.pants2+json" | ||
"application/vnd.custom+json", "application/vnd.custom2+json" | ||
] | ||
@@ -374,3 +378,3 @@ } | ||
paramType: "body", | ||
description: "Request body (application/vnd.pants+json)", | ||
description: "Request body (application/vnd.custom+json)", | ||
dataType: "string", | ||
@@ -382,3 +386,3 @@ required: false, | ||
paramType: "body", | ||
description: "Request body (application/vnd.pants2+json)", | ||
description: "Request body (application/vnd.custom2+json)", | ||
dataType: "string", | ||
@@ -397,3 +401,3 @@ required: false, | ||
res.body.apis[0].operations[0].produces.should.have.length(1); | ||
res.body.apis[0].operations[0].produces[0].should.equal('application/vnd.pants+json'); | ||
res.body.apis[0].operations[0].produces[0].should.equal('application/vnd.custom+json'); | ||
done(err); | ||
@@ -505,5 +509,9 @@ }); | ||
.expect(200, function(err, res) { | ||
res.body.apis[0].operations[0].errorResponses.should.have.length(1); | ||
res.body.apis[0].operations[0].errorResponses[0].code.should.eql(400); | ||
res.body.apis[0].operations[0].errorResponses[0].reason.should.eql("Invalid content-type"); | ||
res.body.apis[0].operations[0].errorResponses.should.have.length(2); | ||
res.body.apis[0].operations[0].errorResponses.should.includeEql( | ||
{ code: 400, reason: "Invalid content-type" } | ||
); | ||
res.body.apis[0].operations[0].errorResponses.should.includeEql( | ||
{ code: 400, reason: "Body does not match schema for application/vnd.thing+json" } | ||
); | ||
done(err); | ||
@@ -516,4 +524,55 @@ }); | ||
it('should add the schema to the models'); | ||
it('should add a produces value to the operation'); | ||
var app = express(), flairedApp; | ||
flair.addContentType( | ||
"application/vnd.pants+json", | ||
{ | ||
id: "pants", | ||
type: "object", | ||
properties: { | ||
pants: { | ||
type: "string" | ||
} | ||
} | ||
} | ||
); | ||
app.get( | ||
'/pants', | ||
flair.describe("getPants", "short desc", "long desc"), | ||
flair.produces("application/vnd.pants+json"), | ||
function(req, res) { | ||
res.header('Content-Type', 'application/vnd.pants+json'); | ||
res.json({ pants: "pants" }); | ||
} | ||
); | ||
flairedApp = flair.swagger(app); | ||
it('should add the schema to the models', function(done) { | ||
supertest(flairedApp) | ||
.get('/api-doc/pants') | ||
.expect(200, function(err, res) { | ||
res.body.models.should.have.property("pants"); | ||
done(err); | ||
}); | ||
}); | ||
it('should add a responseClass to the operation', function(done) { | ||
supertest(flairedApp) | ||
.get('/api-doc/pants') | ||
.expect(200, function(err, res) { | ||
res.body.apis[0].operations[0].responseClass.should.equal("pants"); | ||
done(err); | ||
}); | ||
}); | ||
it('should add a produces value to the operation', function(done) { | ||
supertest(flairedApp) | ||
.get('/api-doc/pants') | ||
.expect(200, function(err, res) { | ||
res.body.apis[0].operations[0].produces.should.have.length(1); | ||
res.body.apis[0].operations[0].produces.should.includeEql("application/vnd.pants+json"); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
@@ -520,0 +579,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33163
965