Add support for configuring remoting options

Allow remote-connector users to provide "options" property in the
datasource configuration, this "options" object is then passed down to
RemoteObjects and allows e.g. configuration of pass-through
authorization, where the remoting connector passes the access token
used to make the incoming request down to the backend service invoked.
This commit is contained in:
Kenny Sabir 2017-09-19 09:15:49 +10:00 committed by Miroslav Bajtoš
parent 07d6e1b66e
commit 816e989b4e
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
3 changed files with 23 additions and 2 deletions

View File

@ -29,14 +29,14 @@ module.exports = RemoteConnector;
function RemoteConnector(settings) {
assert(typeof settings ===
'object',
'cannot initiaze RemoteConnector without a settings object');
'cannot initialize RemoteConnector without a settings object');
this.client = settings.client;
this.adapter = settings.adapter || 'rest';
this.protocol = settings.protocol || 'http';
this.root = settings.root || '';
this.host = settings.host || 'localhost';
this.port = settings.port || 3000;
this.remotes = remoting.create();
this.remotes = remoting.create(settings.options);
this.name = 'remote-connector';
if (settings.url) {

View File

@ -12,6 +12,7 @@ var remoteConnector = require('..');
exports.createMemoryDataSource = createMemoryDataSource;
exports.createModel = createModel;
exports.createRemoteDataSource = createRemoteDataSource;
exports.createRemoteDataSourceWithOptions = createRemoteDataSourceWithOptions;
exports.createRestAppAndListen = createRestAppAndListen;
exports.getUserProperties = getUserProperties;
@ -44,6 +45,14 @@ function createRemoteDataSource(remoteApp) {
});
}
function createRemoteDataSourceWithOptions(remoteApp, options) {
return loopback.createDataSource({
url: 'http://anyURL.com',
connector: remoteConnector,
options: options
});
}
/**
* Used to create models based on a set of options. May associate or link to an
* app.

View File

@ -134,3 +134,15 @@ describe('Custom Path', function() {
});
});
});
describe('RemoteConnector with options', () => {
it('should have the remoting options passed to the remote object', () => {
const serverApp = helper.createRestAppAndListen();
const datasource = helper.createRemoteDataSourceWithOptions(
serverApp,
{'test': 'abc'});
assert.deepEqual(datasource.connector.remotes.options, {test: 'abc'});
});
});