Created
December 11, 2013 05:54
-
-
Save nevill/7905669 to your computer and use it in GitHub Desktop.
A sample to use swagger-jack
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
module.exports = { | |
resourcePath: '/orgs', | |
apis: [{ | |
path: '/orgs', | |
operations: [{ | |
httpMethod: 'POST', | |
responseClass: 'void', | |
nickname: 'returnParams', | |
parameters: [{ | |
dataType: 'Org', | |
required: true, | |
paramType: 'body' | |
}] | |
}] | |
}], | |
models: { | |
Org: { | |
id: "Org", | |
properties: { | |
id: { | |
type: 'string', | |
required: true, | |
description: 'ID of the Org' | |
}, | |
name: { | |
type: 'string', | |
description: 'Name of the Org' | |
} | |
} | |
} | |
} | |
}; |
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
var http = require('http'); | |
var express = require('express'); | |
var swagger = require('swagger-jack'); | |
var app = express(); | |
var root = 'http://localhost:3000'; | |
app.use(express.bodyParser()) | |
.use(express.methodOverride()) | |
.use(swagger.generator(app, { | |
apiVersion: '1.0', | |
basePath: root + "/api", | |
}, | |
[ | |
{ | |
api: require('./creation.js'), | |
controller: { | |
returnParams: function(req, res) { | |
res.json(req.body); | |
} | |
} | |
} | |
])) | |
.use(swagger.validator(app)) | |
.use(swagger.errorHandler()); | |
http.createServer(app).listen(3000, function() { | |
console.log('Open your browser to visit', root); | |
}); |
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
Success output | |
> $ http -v -f localhost:3000/api/orgs id="whatever" name="TEsting org" | |
POST /api/orgs HTTP/1.1 | |
Accept: */* | |
Accept-Encoding: gzip, deflate, compress | |
Content-Length: 28 | |
Content-Type: application/x-www-form-urlencoded; charset=utf-8 | |
Host: localhost:3000 | |
User-Agent: HTTPie/0.7.2 | |
id=whatever&name=TEsting+org | |
HTTP/1.1 200 OK | |
Connection: keep-alive | |
Content-Length: 47 | |
Content-Type: application/json; charset=utf-8 | |
Date: Wed, 11 Dec 2013 05:49:20 GMT | |
X-Powered-By: Express | |
{ | |
"id": "whatever", | |
"name": "TEsting org" | |
} | |
============================================================================================================================= | |
Failed output | |
> $ http -v -f localhost:3000/api/orgs name="TEsting org" | |
POST /api/orgs HTTP/1.1 | |
Accept: */* | |
Accept-Encoding: gzip, deflate, compress | |
Content-Length: 16 | |
Content-Type: application/x-www-form-urlencoded; charset=utf-8 | |
Host: localhost:3000 | |
User-Agent: HTTPie/0.7.2 | |
name=TEsting+org | |
HTTP/1.1 400 Bad Request | |
Connection: keep-alive | |
Content-Length: 49 | |
Content-Type: application/json; charset=utf-8 | |
Date: Wed, 11 Dec 2013 05:49:17 GMT | |
X-Powered-By: Express | |
{ | |
"message": "body property 'id' is required" | |
} |
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
{ | |
"name": "swagger-test", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "BSD-2-Clause", | |
"dependencies": { | |
"swagger-jack": "~1.6.2", | |
"express": "~3.4.6" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment