2014-06-05 00:42:18 +00:00
|
|
|
/*!
|
2013-05-24 14:59:59 +00:00
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-11-12 11:36:16 +00:00
|
|
|
var loopback = require('../../lib/loopback');
|
2014-10-22 21:29:56 +00:00
|
|
|
var async = require('async');
|
2015-02-25 19:48:08 +00:00
|
|
|
var deprecate = require('depd')('loopback');
|
2013-05-24 14:59:59 +00:00
|
|
|
|
2014-06-05 00:42:18 +00:00
|
|
|
/*!
|
2013-05-24 14:59:59 +00:00
|
|
|
* Export the middleware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = rest;
|
|
|
|
|
|
|
|
/**
|
2014-06-05 00:42:18 +00:00
|
|
|
* Expose models over REST.
|
2014-10-30 20:49:47 +00:00
|
|
|
*
|
2014-06-05 00:42:18 +00:00
|
|
|
* For example:
|
|
|
|
* ```js
|
|
|
|
* app.use(loopback.rest());
|
|
|
|
* ```
|
|
|
|
* For more information, see [Exposing models over a REST API](http://docs.strongloop.com/display/DOC/Exposing+models+over+a+REST+API).
|
|
|
|
* @header loopback.rest()
|
2013-05-24 14:59:59 +00:00
|
|
|
*/
|
|
|
|
|
2014-11-04 10:27:49 +00:00
|
|
|
function rest() {
|
2015-01-15 17:03:22 +00:00
|
|
|
var handlers; // Cached handlers
|
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
return function restApiHandler(req, res, next) {
|
2013-07-17 21:29:43 +00:00
|
|
|
var app = req.app;
|
2015-04-01 21:50:36 +00:00
|
|
|
var registry = app.registry;
|
2014-02-04 15:17:32 +00:00
|
|
|
|
2015-02-25 19:48:08 +00:00
|
|
|
// added for https://github.com/strongloop/loopback/issues/1134
|
|
|
|
if (app.get('legacyExplorer') !== false) {
|
|
|
|
deprecate(
|
|
|
|
'Routes "/methods" and "/models" are considered dangerous and should not be used.\n' +
|
|
|
|
'Disable them by setting "legacyExplorer=false" in "server/config.json" or via "app.set()".'
|
|
|
|
);
|
|
|
|
if (req.url === '/routes') {
|
|
|
|
return res.send(app.handler('rest').adapter.allRoutes());
|
|
|
|
} else if (req.url === '/models') {
|
|
|
|
return res.send(app.remotes().toJSON());
|
|
|
|
}
|
2014-10-22 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 17:03:22 +00:00
|
|
|
if (!handlers) {
|
|
|
|
handlers = [];
|
2014-11-04 10:27:49 +00:00
|
|
|
var remotingOptions = app.get('remoting') || {};
|
|
|
|
|
|
|
|
var contextOptions = remotingOptions.context;
|
|
|
|
if (contextOptions !== false) {
|
2015-01-15 17:03:22 +00:00
|
|
|
if (typeof contextOptions !== 'object') {
|
2014-11-04 10:27:49 +00:00
|
|
|
contextOptions = {};
|
2015-01-15 17:03:22 +00:00
|
|
|
}
|
|
|
|
handlers.push(loopback.context(contextOptions));
|
2014-11-04 10:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (app.isAuthEnabled) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var AccessToken = registry.getModelByType('AccessToken');
|
|
|
|
handlers.push(loopback.token({ model: AccessToken, app: app }));
|
2014-02-04 15:17:32 +00:00
|
|
|
}
|
2014-11-04 10:27:49 +00:00
|
|
|
|
2015-01-15 17:03:22 +00:00
|
|
|
handlers.push(function(req, res, next) {
|
|
|
|
// Need to get an instance of the REST handler per request
|
|
|
|
return app.handler('rest')(req, res, next);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (handlers.length === 1) {
|
|
|
|
return handlers[0](req, res, next);
|
|
|
|
}
|
|
|
|
async.eachSeries(handlers, function(handler, done) {
|
2014-10-22 21:29:56 +00:00
|
|
|
handler(req, res, done);
|
|
|
|
}, next);
|
2014-02-04 15:17:32 +00:00
|
|
|
};
|
2013-05-24 14:59:59 +00:00
|
|
|
}
|