2020-01-21 19:19:18 +00:00
|
|
|
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2014-06-05 00:42:18 +00:00
|
|
|
/*!
|
2013-05-24 14:59:59 +00:00
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const g = require('../../lib/globalize');
|
|
|
|
const loopback = require('../../lib/loopback');
|
|
|
|
const async = require('async');
|
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());
|
|
|
|
* ```
|
2016-11-04 20:47:12 +00:00
|
|
|
* For more information, see [Exposing models over a REST API](http://loopback.io/doc/en/lb2/Exposing-models-over-REST.html).
|
2014-06-05 00:42:18 +00:00
|
|
|
* @header loopback.rest()
|
2013-05-24 14:59:59 +00:00
|
|
|
*/
|
|
|
|
|
2014-11-04 10:27:49 +00:00
|
|
|
function rest() {
|
2019-10-07 09:45:34 +00:00
|
|
|
let handlers; // Cached handlers
|
2015-01-15 17:03:22 +00:00
|
|
|
|
2014-10-22 21:29:56 +00:00
|
|
|
return function restApiHandler(req, res, next) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = req.app;
|
|
|
|
const registry = app.registry;
|
2014-02-04 15:17:32 +00:00
|
|
|
|
2015-01-15 17:03:22 +00:00
|
|
|
if (!handlers) {
|
|
|
|
handlers = [];
|
2019-10-07 09:45:34 +00:00
|
|
|
const remotingOptions = app.get('remoting') || {};
|
2014-11-04 10:27:49 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const contextOptions = remotingOptions.context;
|
2016-07-28 14:36:23 +00:00
|
|
|
if (contextOptions !== undefined && contextOptions !== false) {
|
2016-08-10 11:37:25 +00:00
|
|
|
throw new Error(g.f(
|
|
|
|
'%s was removed in version 3.0. See %s for more details.',
|
|
|
|
'remoting.context option',
|
2019-11-17 19:04:57 +00:00
|
|
|
'http://loopback.io/doc/en/lb2/Using-current-context.html',
|
2018-08-08 15:22:20 +00:00
|
|
|
));
|
2014-11-04 10:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (app.isAuthEnabled) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const AccessToken = registry.getModelByType('AccessToken');
|
2016-11-15 21:46:23 +00:00
|
|
|
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
|
|
|
}
|