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;
|
2014-07-04 22:09:03 +00:00
|
|
|
var swagger = require('./lib/swagger');
|
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:
|
|
|
|
*
|
|
|
|
* var explorer = require('loopback-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-07-01 16:09:38 +00:00
|
|
|
var mountPath = options.mountPath || '/explorer';
|
|
|
|
loopbackApplication.use(mountPath, routes(loopbackApplication, options));
|
|
|
|
}
|
|
|
|
|
|
|
|
function routes(loopbackApplication, options) {
|
|
|
|
var loopback = loopbackApplication.loopback;
|
|
|
|
var loopbackMajor = loopback && loopback.version &&
|
|
|
|
loopback.version.split('.')[0] || 1;
|
|
|
|
|
|
|
|
if (loopbackMajor < 2) {
|
|
|
|
throw new Error('loopback-explorer requires loopback 2.0 or newer');
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
options = _defaults({}, options, {
|
|
|
|
resourcePath: 'resources',
|
2014-07-10 19:16:10 +00:00
|
|
|
apiInfo: loopbackApplication.get('apiInfo') || {}
|
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-07-01 16:09:38 +00:00
|
|
|
swagger(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
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
// 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
|
2014-07-09 22:38:05 +00:00
|
|
|
// to worry about constantly pulling in JS updates.
|
2014-10-22 08:55:25 +00:00
|
|
|
if (options.uiDirs) {
|
2014-12-31 21:49:06 +00:00
|
|
|
if (typeof options.uiDirs === 'string') {
|
2015-07-01 16:09:38 +00:00
|
|
|
router.use(loopback.static(options.uiDirs));
|
2014-12-31 21:49:06 +00:00
|
|
|
} else if (Array.isArray(options.uiDirs)) {
|
|
|
|
options.uiDirs.forEach(function(dir) {
|
2015-07-01 16:09:38 +00:00
|
|
|
router.use(loopback.static(dir));
|
2014-12-31 21:49:06 +00:00
|
|
|
});
|
|
|
|
}
|
2014-10-22 08:55:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
if (options.swaggerDistRoot) {
|
2014-10-22 08:55:25 +00:00
|
|
|
console.warn('loopback-explorer: `swaggerDistRoot` is deprecated,' +
|
|
|
|
' use `uiDirs` instead');
|
2015-07-01 16:09:38 +00:00
|
|
|
router.use(loopback.static(options.swaggerDistRoot));
|
2014-07-04 19:28:47 +00:00
|
|
|
}
|
2014-10-22 08:55:25 +00:00
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
// File in node_modules are overridden by a few customizations
|
2015-07-01 16:09:38 +00:00
|
|
|
router.use(loopback.static(STATIC_ROOT));
|
2014-10-22 08:55:25 +00:00
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
// Swagger UI distribution
|
2015-07-01 16:09:38 +00:00
|
|
|
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
|
|
|
}
|