2014-07-09 16:45:44 +00:00
|
|
|
'use strict';
|
2013-11-05 19:16:59 +00:00
|
|
|
/*!
|
|
|
|
* Adds dynamically-updated docs as /explorer
|
|
|
|
*/
|
2014-07-11 18:56:32 +00:00
|
|
|
var url = require('url');
|
2013-11-05 19:16:59 +00:00
|
|
|
var path = require('path');
|
2014-07-10 18:49:09 +00:00
|
|
|
var urlJoin = require('./lib/url-join');
|
2014-12-03 03:09:48 +00:00
|
|
|
var _defaults = require('lodash').defaults;
|
2015-09-03 08:37:09 +00:00
|
|
|
var cors = require('cors');
|
|
|
|
var createSwaggerObject = require('loopback-swagger').generateSwaggerSpec;
|
2015-08-11 16:52:56 +00:00
|
|
|
var SWAGGER_UI_ROOT = require('strong-swagger-ui/index').dist;
|
2013-11-05 19:16:59 +00:00
|
|
|
var STATIC_ROOT = path.join(__dirname, 'public');
|
|
|
|
|
|
|
|
module.exports = explorer;
|
2015-07-01 16:09:38 +00:00
|
|
|
explorer.routes = routes;
|
2013-11-05 19:16:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Example usage:
|
|
|
|
*
|
2015-09-16 20:38:09 +00:00
|
|
|
* var explorer = require('loopback-component-explorer');
|
2015-07-01 16:09:38 +00:00
|
|
|
* explorer(app, options);
|
2013-11-05 19:16:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
function explorer(loopbackApplication, options) {
|
2015-08-28 17:08:05 +00:00
|
|
|
options = _defaults({}, options, { mountPath: '/explorer' });
|
|
|
|
loopbackApplication.use(options.mountPath, routes(loopbackApplication, options));
|
2015-09-16 20:38:09 +00:00
|
|
|
loopbackApplication.set('loopback-component-explorer', options);
|
2015-07-01 16:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function routes(loopbackApplication, options) {
|
|
|
|
var loopback = loopbackApplication.loopback;
|
|
|
|
var loopbackMajor = loopback && loopback.version &&
|
|
|
|
loopback.version.split('.')[0] || 1;
|
|
|
|
|
|
|
|
if (loopbackMajor < 2) {
|
2015-09-16 20:38:09 +00:00
|
|
|
throw new Error('loopback-component-explorer requires loopback 2.0 or newer');
|
2015-07-01 16:09:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
options = _defaults({}, options, {
|
2015-08-13 15:20:11 +00:00
|
|
|
resourcePath: 'swagger.json',
|
2016-03-08 18:13:08 +00:00
|
|
|
apiInfo: loopbackApplication.get('apiInfo') || {},
|
|
|
|
swaggerUI: true
|
2014-07-05 19:32:00 +00:00
|
|
|
});
|
2014-01-07 15:13:34 +00:00
|
|
|
|
2015-07-01 16:09:38 +00:00
|
|
|
var router = new loopback.Router();
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2015-09-03 08:37:09 +00:00
|
|
|
mountSwagger(loopbackApplication, router, options);
|
2014-04-21 02:29:01 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
// config.json is loaded by swagger-ui. The server should respond
|
|
|
|
// with the relative URI of the resource doc.
|
2015-07-01 16:09:38 +00:00
|
|
|
router.get('/config.json', function(req, res) {
|
2014-07-11 18:56:32 +00:00
|
|
|
// Get the path we're mounted at. It's best to get this from the referer
|
|
|
|
// in case we're proxied at a deep path.
|
|
|
|
var source = url.parse(req.headers.referer || '').pathname;
|
|
|
|
// If no referer is available, use the incoming url.
|
|
|
|
if (!source) {
|
|
|
|
source = req.originalUrl.replace(/\/config.json(\?.*)?$/, '');
|
|
|
|
}
|
2013-11-29 15:17:59 +00:00
|
|
|
res.send({
|
2014-07-11 18:56:32 +00:00
|
|
|
url: urlJoin(source, '/' + options.resourcePath)
|
2013-11-29 15:17:59 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2016-03-08 18:13:08 +00:00
|
|
|
if (options.swaggerUI) {
|
|
|
|
// Allow specifying a static file roots for swagger files. Any files in
|
|
|
|
// these folders will override those in the swagger-ui distribution.
|
|
|
|
// In this way one could e.g. make changes to index.html without having
|
|
|
|
// to worry about constantly pulling in JS updates.
|
|
|
|
if (options.uiDirs) {
|
|
|
|
if (typeof options.uiDirs === 'string') {
|
|
|
|
router.use(loopback.static(options.uiDirs));
|
|
|
|
} else if (Array.isArray(options.uiDirs)) {
|
|
|
|
options.uiDirs.forEach(function(dir) {
|
|
|
|
router.use(loopback.static(dir));
|
|
|
|
});
|
|
|
|
}
|
2014-12-31 21:49:06 +00:00
|
|
|
}
|
2014-10-22 08:55:25 +00:00
|
|
|
|
2016-03-08 18:13:08 +00:00
|
|
|
// File in node_modules are overridden by a few customizations
|
|
|
|
router.use(loopback.static(STATIC_ROOT));
|
2014-10-22 08:55:25 +00:00
|
|
|
|
2016-03-08 18:13:08 +00:00
|
|
|
// Swagger UI distribution
|
|
|
|
router.use(loopback.static(SWAGGER_UI_ROOT));
|
|
|
|
}
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2015-07-01 16:09:38 +00:00
|
|
|
return router;
|
2013-11-05 19:16:59 +00:00
|
|
|
}
|
2015-09-03 08:37:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup Swagger documentation on the given express app.
|
|
|
|
*
|
|
|
|
* @param {Application} loopbackApplication The loopback application to
|
|
|
|
* document.
|
|
|
|
* @param {Application} swaggerApp Swagger application used for hosting
|
|
|
|
* swagger documentation.
|
|
|
|
* @param {Object} opts Options.
|
|
|
|
*/
|
|
|
|
function mountSwagger(loopbackApplication, swaggerApp, opts) {
|
|
|
|
var swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
|
|
|
2015-11-18 06:21:27 +00:00
|
|
|
// listening to modelRemoted event for updating the swaggerObject
|
|
|
|
// with the newly created model to appear in the Swagger UI.
|
|
|
|
loopbackApplication.on('modelRemoted', function() {
|
|
|
|
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
|
|
});
|
|
|
|
|
2015-09-03 08:37:09 +00:00
|
|
|
var resourcePath = opts && opts.resourcePath || 'swagger.json';
|
|
|
|
if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath;
|
|
|
|
|
|
|
|
var remotes = loopbackApplication.remotes();
|
|
|
|
setupCors(swaggerApp, remotes);
|
|
|
|
|
|
|
|
swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) {
|
|
|
|
res.status(200).send(swaggerObject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupCors(swaggerApp, remotes) {
|
|
|
|
var corsOptions = remotes.options && remotes.options.cors ||
|
|
|
|
{ origin: true, credentials: true };
|
|
|
|
|
|
|
|
// TODO(bajtos) Skip CORS when remotes.options.cors === false
|
|
|
|
swaggerApp.use(cors(corsOptions));
|
|
|
|
}
|