wordnik/swagger-node-express — pull request #4 by semu (2011-11-06 19:38:09+00:00)
Title: Some major improvements over the weekend

I added some new features to `swagger-node-express` and fixed a couple of minor and major bugs over the weekend. Please read the detailed changelog:

# Changelog Summary

- Updated: Rewritten express route binding
- Updated: Rewritten swagger external method binding
- Updated: Wrapped up action and spec definitions
- Updated: Ported petstore to new method binding
- New Features: Auto-Discover specs and actions
- New Features: Data Randomizer
- New Features: Sample app modelizer

# Detailed Information

## Rewritten express route binding

Rewritten internal route binding from using single calls for `app.get`, `app.post`, `app.put` and `app.delete` to a more cleaner way with calling `app[currentMethod](fullPath, function(req,res) {});` for dynamicly adding routes…

## Rewritten method binding

Set `appHandler` once and chain all needed methods:

swagger.setAppHandler(app);
swagger.addGet(petResources.findByStatus)
.addGet(petResources.findByTags)
.addGet(petResources.findById)
.addPost(petResources.addPet)
.addDelete(petResources.deletePet)
.addPut(petResources.updatePet);

Group methods for less lines of code:

swagger.setAppHandler(app);
swagger.addGet(petResources.findByStatus, petResources.findByTags, petResources.findById)
.addPost(petResources.addPet)
.addDelete(petResources.deletePet)
.addPut(petResources.updatePet);

## Rewritten action and spec definition

Wrapped up `spec` and `action` defintions for better hanlding:

exports.updatePet = {
'spec': {
"path" : "/pet.{format}",
"notes" : "updates a pet in the store",
"summary" : "Update an existing pet",
"params" : new Array(
swagger.postParam("Pet object that needs to be added to the store", "pet")
),
"errorResponses" : new Array(
swagger.error(400, "invalid ID supplied"),
swagger.error(404, "Pet not found"),
swagger.error(405, "validation exception")
),
"nickname" : "addPet"
},
'action': function(req, res) {
res.send(JSON.stringify({
"message" : "thanks for playing"
}));
}
};

## Auto discover of methods and specs

Just define `exports.[yourCustomActionName].spec.method` with a valid option (`GET`, `POST`, `PUT`, `DELETE`) and use auto discover feature:

swagger.setAppHandler(app);
swagger.discover(require('./petResources.js'));

Or pass file directly

swagger.discoverFile(__dirname + './petResources.js');

See includd `modelizer` example App for a working auto discover example.

## Added feature: Randomizer

Added simpel object for generating random values for swagger's basic datatypes. `Randomizer` helps getting started with modelling data structure and api designs.

var randomInt = swagger.Randomizer.int();
var randomString = swagger.Randomizer.string();
var randomDate = swagger.Randomizer.date();
var randomArrayOfDates = swagger.Randomizer.array('date');

## Added second sample application: modelizer

Added methods for creating dummy objects based on model defintion, modelizer fills up an object with swagger's basic datatypes:

{
"ID": "331",
"string": "v0DTC82eeWy6xEAd9zL2ZD5f",
"Date": "1892-10-14T16:25:12+01:00",
"boolean": true,
"double": 898936.7166068405,
"int": 6510,
"visits": [
"1970-01-15T14:01:24+01:00",
"1948-05-03T03:59:16+01:00",
"1838-07-27T16:41:26+01:00",
"1827-04-04T10:17:29+01:00"
],
"scores": [
5825,
7586
],
"photos": [
"SMDCvDhbbAz48AtthAMiBliV",
"wgLV9pIRnyzuhdNkG7WTXgHH",
"3tEAqAGztwAq4aEh0zQVdcBV"
]
}

See included `modelizer` application for how to create a sample object based on a model defintion.