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
|
|
|
|
|
2018-04-06 16:15:31 +00:00
|
|
|
'use strict';
|
2016-07-27 22:45:24 +00:00
|
|
|
|
2019-02-05 07:57:18 +00:00
|
|
|
const SG = require('strong-globalize');
|
2016-07-27 22:45:24 +00:00
|
|
|
SG.SetRootDir(__dirname);
|
2019-02-05 07:57:18 +00:00
|
|
|
const g = SG();
|
2016-07-27 22:45:24 +00:00
|
|
|
|
2013-11-05 19:16:59 +00:00
|
|
|
/*!
|
|
|
|
* Adds dynamically-updated docs as /explorer
|
|
|
|
*/
|
2019-02-05 07:57:18 +00:00
|
|
|
const url = require('url');
|
|
|
|
const path = require('path');
|
|
|
|
const urlJoin = require('./lib/url-join');
|
|
|
|
const _defaults = require('lodash').defaults;
|
|
|
|
const createSwaggerObject = require('loopback-swagger').generateSwaggerSpec;
|
|
|
|
const SWAGGER_UI_ROOT = require('swagger-ui/index').dist;
|
|
|
|
const STATIC_ROOT = path.join(__dirname, 'public');
|
2016-04-29 13:36:44 +00:00
|
|
|
|
|
|
|
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) {
|
2019-02-05 07:57:18 +00:00
|
|
|
options = _defaults({}, options, {mountPath: '/explorer'});
|
2018-04-06 17:47:04 +00:00
|
|
|
loopbackApplication.use(
|
|
|
|
options.mountPath,
|
|
|
|
routes(loopbackApplication, options)
|
|
|
|
);
|
2018-04-06 16:15:31 +00:00
|
|
|
loopbackApplication.set('loopback-component-explorer', options);
|
2016-04-29 13:36:44 +00:00
|
|
|
}
|
2015-07-01 16:09:38 +00:00
|
|
|
|
2016-04-29 13:36:44 +00:00
|
|
|
function routes(loopbackApplication, options) {
|
2019-02-05 07:57:18 +00:00
|
|
|
const loopback = loopbackApplication.loopback;
|
|
|
|
const loopbackMajor =
|
2018-04-06 17:47:04 +00:00
|
|
|
(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) {
|
2018-04-06 17:47:04 +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, {
|
2018-04-06 16:15:31 +00:00
|
|
|
resourcePath: 'swagger.json',
|
|
|
|
apiInfo: loopbackApplication.get('apiInfo') || {},
|
|
|
|
swaggerUI: true,
|
2016-04-29 13:36:44 +00:00
|
|
|
});
|
2014-01-07 15:13:34 +00:00
|
|
|
|
2019-02-05 07:57:18 +00:00
|
|
|
const 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.
|
2018-04-06 16:15:31 +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.
|
2019-02-05 07:57:18 +00:00
|
|
|
let source = url.parse(req.headers.referer || '').pathname;
|
2018-08-14 13:29:51 +00:00
|
|
|
// strip index.html if present in referer
|
|
|
|
if (source && /\/index\.html$/.test(source)) {
|
|
|
|
source = source.replace(/\/index\.html$/, '');
|
|
|
|
}
|
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) {
|
2018-04-06 16:15:31 +00:00
|
|
|
source = req.originalUrl.replace(/\/config.json(\?.*)?$/, '');
|
2016-04-29 13:36:44 +00:00
|
|
|
}
|
|
|
|
res.send({
|
2018-04-06 16:15:31 +00:00
|
|
|
url: urlJoin(source, '/' + options.resourcePath),
|
2018-09-05 13:16:46 +00:00
|
|
|
auth: options.auth,
|
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) {
|
2018-04-06 16:15:31 +00:00
|
|
|
if (typeof options.uiDirs === 'string') {
|
2016-04-29 13:36:44 +00:00
|
|
|
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) {
|
2019-02-05 07:57:18 +00:00
|
|
|
let 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.
|
2018-04-16 12:41:32 +00:00
|
|
|
loopbackApplication.on('modelRemoted', rebuildSwaggerObject);
|
|
|
|
|
|
|
|
loopbackApplication.on('modelDeleted', rebuildSwaggerObject);
|
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.
|
2018-04-16 12:41:32 +00:00
|
|
|
loopbackApplication.on('remoteMethodAdded', rebuildSwaggerObject);
|
2017-09-15 17:44:45 +00:00
|
|
|
|
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.
|
2018-04-16 12:41:32 +00:00
|
|
|
loopbackApplication.on('remoteMethodDisabled', rebuildSwaggerObject);
|
2016-04-29 21:01:48 +00:00
|
|
|
|
2019-02-05 07:57:18 +00:00
|
|
|
let resourcePath = (opts && opts.resourcePath) || 'swagger.json';
|
2018-04-06 16:15:31 +00:00
|
|
|
if (resourcePath[0] !== '/') resourcePath = '/' + resourcePath;
|
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);
|
|
|
|
});
|
2018-04-16 12:41:32 +00:00
|
|
|
|
|
|
|
function rebuildSwaggerObject() {
|
|
|
|
swaggerObject = createSwaggerObject(loopbackApplication, opts);
|
|
|
|
}
|
2016-04-29 13:36:44 +00:00
|
|
|
}
|