2016-05-06 00:10:55 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-component-explorer
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-04-29 14:21:07 +00:00
|
|
|
'use strict';
|
2016-07-27 22:45:24 +00:00
|
|
|
|
|
|
|
var SG = require('strong-globalize');
|
|
|
|
SG.SetRootDir(__dirname);
|
|
|
|
var g = SG();
|
|
|
|
|
2013-11-05 19:16:59 +00:00
|
|
|
/*!
|
|
|
|
* Adds dynamically-updated docs as /explorer
|
|
|
|
*/
|
2016-09-21 09:08:49 +00:00
|
|
|
var deprecated = require('depd')('loopback-explorer');
|
2016-04-29 13:36:44 +00:00
|
|
|
var url = require('url');
|
|
|
|
var path = require('path');
|
|
|
|
var urlJoin = require('./lib/url-join');
|
|
|
|
var _defaults = require('lodash').defaults;
|
|
|
|
var cors = require('cors');
|
|
|
|
var createSwaggerObject = require('loopback-swagger').generateSwaggerSpec;
|
2016-09-27 18:11:40 +00:00
|
|
|
var SWAGGER_UI_ROOT = require('swagger-ui/index').dist;
|
2016-04-29 13:36:44 +00:00
|
|
|
var STATIC_ROOT = path.join(__dirname, 'public');
|
|
|
|
|
|
|
|
module.exports = explorer;
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
function explorer(loopbackApplication, options) {
|
|
|
|
options = _defaults({}, options, { mountPath: '/explorer' });
|
|
|
|
loopbackApplication.use(options.mountPath, routes(loopbackApplication, options));
|
|
|
|
loopbackApplication.set('loopback-component-explorer', options);
|
|
|
|
}
|
2015-07-01 16:09:38 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
function routes(loopbackApplication, options) {
|
|
|
|
var loopback = loopbackApplication.loopback;
|
|
|
|
var loopbackMajor = loopback && loopback.version &&
|
|
|
|
loopback.version.split('.')[0] || 1;
|
2015-07-01 16:09:38 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
if (loopbackMajor < 2) {
|
2016-07-27 22:45:24 +00:00
|
|
|
throw new Error(g.f('{{loopback-component-explorer}} requires ' +
|
|
|
|
'{{loopback}} 2.0 or newer'));
|
2016-04-29 13:36:44 +00:00
|
|
|
}
|
2015-07-01 16:09:38 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
options = _defaults({}, options, {
|
|
|
|
resourcePath: 'swagger.json',
|
|
|
|
apiInfo: loopbackApplication.get('apiInfo') || {},
|
|
|
|
swaggerUI: true,
|
|
|
|
});
|
2014-01-07 15:13:34 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
var router = new loopback.Router();
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2016-04-29 13:36:44 +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.
|
2016-04-29 13:36:44 +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.
|
2016-04-29 13:36:44 +00:00
|
|
|
var source = url.parse(req.headers.referer || '').pathname;
|
2014-07-11 18:56:32 +00:00
|
|
|
// If no referer is available, use the incoming url.
|
2016-04-29 13:36:44 +00:00
|
|
|
if (!source) {
|
|
|
|
source = req.originalUrl.replace(/\/config.json(\?.*)?$/, '');
|
|
|
|
}
|
|
|
|
res.send({
|
|
|
|
url: urlJoin(source, '/' + options.resourcePath),
|
2013-11-29 15:17:59 +00:00
|
|
|
});
|
2016-04-29 13:36:44 +00:00
|
|
|
});
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
if (options.swaggerUI) {
|
2016-03-08 18:13:08 +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
|
|
|
|
// to worry about constantly pulling in JS updates.
|
2016-04-29 13:36:44 +00:00
|
|
|
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));
|
|
|
|
});
|
2016-03-08 18:13:08 +00:00
|
|
|
}
|
2016-04-29 13:36:44 +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
|
2016-04-29 13:36:44 +00:00
|
|
|
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
|
2016-04-29 13:36:44 +00:00
|
|
|
router.use(loopback.static(SWAGGER_UI_ROOT));
|
2016-04-22 21:09:48 +00:00
|
|
|
}
|
2015-09-03 08:37:09 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
return router;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-04-29 13:36:44 +00:00
|
|
|
function mountSwagger(loopbackApplication, swaggerApp, opts) {
|
|
|
|
var swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
2015-09-03 08:37:09 +00:00
|
|
|
|
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.
|
2016-04-29 13:36:44 +00:00
|
|
|
loopbackApplication.on('modelRemoted', function() {
|
|
|
|
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
|
|
});
|
2015-11-18 06:21:27 +00:00
|
|
|
|
2017-09-15 17:44:45 +00:00
|
|
|
// listening to started event for updating the swaggerObject
|
|
|
|
// when a call to app.models.[modelName].nestRemoting([modelName])
|
|
|
|
// to appear that method in the Swagger UI.
|
|
|
|
loopbackApplication.on('remoteMethodAdded', function() {
|
|
|
|
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
|
|
});
|
|
|
|
|
2016-04-29 21:01:48 +00:00
|
|
|
// listening to remoteMethodDisabled event for updating the swaggerObject
|
|
|
|
// when a remote method is disabled to hide that method in the Swagger UI.
|
|
|
|
loopbackApplication.on('remoteMethodDisabled', function() {
|
|
|
|
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
|
|
});
|
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
var resourcePath = opts && opts.resourcePath || 'swagger.json';
|
|
|
|
if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath;
|
2015-09-03 08:37:09 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
var remotes = loopbackApplication.remotes();
|
|
|
|
setupCors(swaggerApp, remotes);
|
2015-09-03 08:37:09 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
swaggerApp.get(resourcePath, function sendSwaggerObject(req, res) {
|
|
|
|
res.status(200).send(swaggerObject);
|
|
|
|
});
|
|
|
|
}
|
2015-09-03 08:37:09 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
function setupCors(swaggerApp, remotes) {
|
2016-09-21 09:08:49 +00:00
|
|
|
var corsOptions = remotes.options && remotes.options.cors;
|
|
|
|
if (corsOptions === false)
|
|
|
|
return;
|
|
|
|
|
|
|
|
deprecated(g.f(
|
|
|
|
'The built-in CORS middleware provided by loopback-component-explorer ' +
|
|
|
|
'was deprecated. See %s for more details.',
|
|
|
|
'https://docs.strongloop.com/display/public/LB/Security+considerations'
|
|
|
|
));
|
|
|
|
|
|
|
|
if (corsOptions === undefined) {
|
|
|
|
corsOptions = { origin: true, credentials: true };
|
|
|
|
}
|
2015-09-03 08:37:09 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
swaggerApp.use(cors(corsOptions));
|
|
|
|
}
|