Swagger integration

This commit is contained in:
Ritchie 2013-07-25 16:24:00 -07:00
parent 755b54f697
commit 35cafff877
3 changed files with 46 additions and 11 deletions

View File

@ -67,6 +67,21 @@ Get the app's exposed models.
console.log(Model.modelName); // color
});
#### app.docs(options)
Enable swagger REST api documentation.
**Options**
- `basePath` The basepath for your API - eg. 'http://localhost:3000'.
**Example**
// enable docs
app.docs({basePath: 'http://localhost:3000'});
Run your app then navigate to [the api explorer](http://petstore.swagger.wordnik.com/). Enter your API basepath to view your generated docs.
### Model
A Loopback `Model` is a vanilla JavaScript class constructor with an attached set of properties and options. A `Model` instance is created by passing a data object containing properties to the `Model` constructor. A `Model` constructor will clean the object passed to it and only set the values matching the properties you define.

View File

@ -5,7 +5,8 @@
var DataSource = require('loopback-data').DataSource
, ModelBuilder = require('loopback-data').ModelBuilder
, assert = require('assert')
, RemoteObjects = require('strong-remoting');
, RemoteObjects = require('strong-remoting')
, swagger = require('strong-remoting/ext/swagger');
/**
* Export the app prototype.
@ -52,6 +53,7 @@ app._models = [];
*/
app.model = function (Model) {
this.remotes().exports[Model.pluralModelName] = Model;
this._models.push(Model);
Model.shared = true;
Model.app = this;
@ -91,4 +93,30 @@ app.remoteObjects = function () {
app.remotes = function () {
return this._remotes || (this._remotes = RemoteObjects.create());
}
}
/**
* Enable documentation
*/
app.docs = function (options) {
var remotes = this.remotes();
swagger(remotes, options);
}
/*!
* Get a handler of the specified type from the handler cache.
*/
app.handler = function (type) {
var handlers = this._handlers || (this._handlers = {});
if(handlers[type]) {
return handlers[type];
}
var remotes = this.remotes();
var handler = this._handlers[type] = remotes.handler(type);
return handler;
}

View File

@ -18,15 +18,7 @@ module.exports = rest;
function rest() {
return function (req, res, next) {
var app = req.app;
var remotes = app.remotes();
// get all remote objects
var objs = app.remoteObjects();
// export remote objects
remotes.exports = objs;
var handler = remotes.handler('rest');
var handler = app.handler('rest');
if(req.url === '/routes') {
res.send(handler.adapter.allRoutes());