2014-07-05 19:32:00 +00:00
|
|
|
'use strict';
|
2014-07-04 22:09:03 +00:00
|
|
|
/**
|
|
|
|
* Expose the `Swagger` plugin.
|
|
|
|
*/
|
|
|
|
module.exports = Swagger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-07-05 19:32:00 +00:00
|
|
|
var path = require('path');
|
2014-07-10 18:49:09 +00:00
|
|
|
var urlJoin = require('./url-join');
|
2014-07-05 19:32:00 +00:00
|
|
|
var _defaults = require('lodash.defaults');
|
|
|
|
var classHelper = require('./class-helper');
|
|
|
|
var routeHelper = require('./route-helper');
|
2014-10-07 07:19:44 +00:00
|
|
|
var cors = require('cors');
|
2014-07-04 22:09:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a remotable Swagger module for plugging into `RemoteObjects`.
|
2014-07-09 22:38:05 +00:00
|
|
|
*
|
|
|
|
* @param {Application} loopbackApplication Host loopback application.
|
|
|
|
* @param {Application} swaggerApp Swagger application used for hosting
|
|
|
|
* these files.
|
|
|
|
* @param {Object} opts Options.
|
2014-07-04 22:09:03 +00:00
|
|
|
*/
|
2014-07-09 22:38:05 +00:00
|
|
|
function Swagger(loopbackApplication, swaggerApp, opts) {
|
2014-07-31 23:47:47 +00:00
|
|
|
opts = _defaults(opts || {}, {
|
2014-07-05 19:32:00 +00:00
|
|
|
swaggerVersion: '1.2',
|
2014-07-09 22:38:05 +00:00
|
|
|
basePath: loopbackApplication.get('restApiRoot') || '/api',
|
2014-07-05 19:32:00 +00:00
|
|
|
resourcePath: 'resources',
|
2014-08-12 16:44:16 +00:00
|
|
|
// Default consumes/produces
|
|
|
|
consumes: ['application/json', 'application/x-www-form-urlencoded'],
|
2014-08-04 04:48:16 +00:00
|
|
|
produces: ['application/json'],
|
2014-07-09 22:38:05 +00:00
|
|
|
version: getVersion()
|
2014-07-05 19:32:00 +00:00
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
|
|
|
// We need a temporary REST adapter to discover our available routes.
|
2014-07-09 22:38:05 +00:00
|
|
|
var remotes = loopbackApplication.remotes();
|
2014-07-04 22:09:03 +00:00
|
|
|
var adapter = remotes.handler('rest').adapter;
|
|
|
|
var routes = adapter.allRoutes();
|
|
|
|
var classes = remotes.classes();
|
|
|
|
|
2014-10-07 07:19:44 +00:00
|
|
|
setupCors(swaggerApp, remotes);
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
// These are the docs we will be sending from the /swagger endpoints.
|
|
|
|
var resourceDoc = generateResourceDoc(opts);
|
2014-07-04 22:09:03 +00:00
|
|
|
var apiDocs = {};
|
|
|
|
|
|
|
|
// A class is an endpoint root; e.g. /users, /products, and so on.
|
2014-07-05 19:32:00 +00:00
|
|
|
classes.forEach(function (aClass) {
|
2014-07-09 22:38:05 +00:00
|
|
|
var doc = apiDocs[aClass.name] = classHelper.generateAPIDoc(aClass, opts);
|
2014-07-05 19:32:00 +00:00
|
|
|
resourceDoc.apis.push(classHelper.generateResourceDocAPIEntry(aClass));
|
|
|
|
|
|
|
|
// Add the getter for this doc.
|
2014-07-10 18:49:09 +00:00
|
|
|
var docPath = urlJoin(opts.resourcePath, aClass.http.path);
|
2014-07-26 15:53:08 +00:00
|
|
|
addRoute(swaggerApp, docPath, doc, opts);
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// A route is an endpoint, such as /users/findOne.
|
2014-07-05 19:32:00 +00:00
|
|
|
routes.forEach(function(route) {
|
|
|
|
// Get the API doc matching this class name.
|
|
|
|
var className = route.method.split('.')[0];
|
|
|
|
var doc = apiDocs[className];
|
2014-07-04 22:09:03 +00:00
|
|
|
if (!doc) {
|
|
|
|
console.error('Route exists with no class: %j', route);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-05 15:54:19 +00:00
|
|
|
// Get the class definition matching this route.
|
2014-07-05 19:32:00 +00:00
|
|
|
var classDef = classes.filter(function (item) {
|
|
|
|
return item.name === className;
|
2014-07-04 22:09:03 +00:00
|
|
|
})[0];
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
routeHelper.addRouteToAPIDeclaration(route, classDef, doc);
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-10-07 07:19:44 +00:00
|
|
|
* The topmost Swagger resource is a description of all (non-Swagger)
|
|
|
|
* resources available on the system, and where to find more
|
2014-07-09 22:38:05 +00:00
|
|
|
* information about them.
|
2014-07-04 22:09:03 +00:00
|
|
|
*/
|
2014-07-26 15:53:08 +00:00
|
|
|
addRoute(swaggerApp, opts.resourcePath, resourceDoc, opts);
|
2014-07-04 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 07:19:44 +00:00
|
|
|
function setupCors(swaggerApp, remotes) {
|
|
|
|
var corsOptions = remotes.options && remotes.options.cors ||
|
|
|
|
{ origin: true, credentials: true };
|
|
|
|
|
|
|
|
swaggerApp.use(cors(corsOptions));
|
|
|
|
}
|
|
|
|
|
2014-07-04 22:09:03 +00:00
|
|
|
/**
|
2014-07-05 19:32:00 +00:00
|
|
|
* Add a route to this remoting extension.
|
2014-07-09 22:38:05 +00:00
|
|
|
* @param {Application} app Express application.
|
|
|
|
* @param {String} uri Path from which to serve the doc.
|
|
|
|
* @param {Object} doc Doc to serve.
|
2014-07-04 22:09:03 +00:00
|
|
|
*/
|
2014-07-26 15:53:08 +00:00
|
|
|
function addRoute(app, uri, doc, opts) {
|
2014-07-09 22:38:05 +00:00
|
|
|
|
|
|
|
var hasBasePath = Object.keys(doc).indexOf('basePath') !== -1;
|
|
|
|
var initialPath = doc.basePath || '';
|
|
|
|
|
2014-07-10 18:49:09 +00:00
|
|
|
app.get(urlJoin('/', uri), function(req, res) {
|
2014-07-09 22:38:05 +00:00
|
|
|
|
|
|
|
// There's a few forces at play that require this "hack". The Swagger spec
|
|
|
|
// requires a `basePath` to be set in the API descriptions. However, we
|
|
|
|
// can't guarantee this path is either reachable or desirable if it's set
|
|
|
|
// as a part of the options.
|
|
|
|
//
|
|
|
|
// The simplest way around this is to reflect the value of the `Host` HTTP
|
|
|
|
// header as the `basePath`. Because we pre-build the Swagger data, we don't
|
|
|
|
// know that header at the time the data is built.
|
|
|
|
if (hasBasePath) {
|
|
|
|
var headers = req.headers;
|
|
|
|
var host = headers.Host || headers.host;
|
2014-07-26 15:53:08 +00:00
|
|
|
doc.basePath = (opts.protocol || req.protocol) + '://' +
|
|
|
|
host + initialPath;
|
2014-07-09 22:38:05 +00:00
|
|
|
}
|
2014-07-28 22:40:08 +00:00
|
|
|
res.status(200).send(doc);
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
2014-07-05 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 22:09:03 +00:00
|
|
|
/**
|
2014-07-05 19:32:00 +00:00
|
|
|
* Generate a top-level resource doc. This is the entry point for swagger UI
|
|
|
|
* and lists all of the available APIs.
|
|
|
|
* @param {Object} opts Swagger options.
|
|
|
|
* @return {Object} Resource doc.
|
2014-07-04 22:09:03 +00:00
|
|
|
*/
|
2014-07-05 19:32:00 +00:00
|
|
|
function generateResourceDoc(opts) {
|
2014-07-04 22:09:03 +00:00
|
|
|
return {
|
2014-07-05 19:32:00 +00:00
|
|
|
swaggerVersion: opts.swaggerVersion,
|
|
|
|
apiVersion: opts.version,
|
|
|
|
apis: [],
|
|
|
|
// See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#513-info-object
|
|
|
|
info: opts.apiInfo
|
|
|
|
// TODO Authorizations
|
|
|
|
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#514-authorizations-object
|
|
|
|
// TODO Produces/Consumes
|
|
|
|
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#52-api-declaration
|
2014-07-04 22:09:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-05 19:32:00 +00:00
|
|
|
* Attempt to get the current API version from package.json.
|
|
|
|
* @return {String} API Version.
|
2014-07-04 22:09:03 +00:00
|
|
|
*/
|
2014-07-05 19:32:00 +00:00
|
|
|
function getVersion() {
|
|
|
|
var version;
|
|
|
|
try {
|
|
|
|
version = require(path.join(process.cwd(), 'package.json')).version;
|
|
|
|
} catch(e) {
|
|
|
|
version = '';
|
2014-07-04 22:09:03 +00:00
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
return version;
|
2014-07-04 22:09:03 +00:00
|
|
|
}
|