Merge pull request #3681 from zipitwireless/master
Update nestRemoting to pass optionsFromContext
This commit is contained in:
commit
b045e4a6be
35
lib/model.js
35
lib/model.js
|
@ -947,38 +947,45 @@ module.exports = function(registry) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const lastArg = opts.accepts[opts.accepts.length - 1] || {};
|
||||||
|
const hasOptionsFromContext =
|
||||||
|
(lastArg.arg || lastArg.name) === 'options' &&
|
||||||
|
lastArg.type === 'object' && lastArg.http;
|
||||||
|
|
||||||
if (relation.multiple) {
|
if (relation.multiple) {
|
||||||
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
||||||
var args = Array.prototype.slice.call(arguments, 1);
|
const args = Array.prototype.slice.call(arguments, 1);
|
||||||
var last = args[args.length - 1];
|
const cb = args[args.length - 1];
|
||||||
var cb = typeof last === 'function' ? last : null;
|
const contextOptions =
|
||||||
this[getterName](fkId, function(err, inst) {
|
hasOptionsFromContext && args[args.length - 2] || {};
|
||||||
if (err && cb) return cb(err);
|
this[getterName](fkId, contextOptions, function(err, inst) {
|
||||||
|
if (err) return cb(err);
|
||||||
if (inst instanceof relation.modelTo) {
|
if (inst instanceof relation.modelTo) {
|
||||||
try {
|
try {
|
||||||
nestedFn.apply(inst, args);
|
nestedFn.apply(inst, args);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (cb) return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
} else if (cb) {
|
} else {
|
||||||
cb(err, null);
|
cb(err, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, method.isStatic);
|
}, method.isStatic);
|
||||||
} else {
|
} else {
|
||||||
sharedClass.defineMethod(methodName, opts, function() {
|
sharedClass.defineMethod(methodName, opts, function() {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
const args = Array.prototype.slice.call(arguments);
|
||||||
var last = args[args.length - 1];
|
const cb = args[args.length - 1];
|
||||||
var cb = typeof last === 'function' ? last : null;
|
const contextOptions =
|
||||||
this[getterName](function(err, inst) {
|
hasOptionsFromContext && args[args.length - 2] || {};
|
||||||
if (err && cb) return cb(err);
|
this[getterName](contextOptions, function(err, inst) {
|
||||||
|
if (err) return cb(err);
|
||||||
if (inst instanceof relation.modelTo) {
|
if (inst instanceof relation.modelTo) {
|
||||||
try {
|
try {
|
||||||
nestedFn.apply(inst, args);
|
nestedFn.apply(inst, args);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (cb) return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
} else if (cb) {
|
} else {
|
||||||
cb(err, null);
|
cb(err, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -695,7 +695,6 @@ describe('Multiple users with custom principalType', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fails when the access token belongs to a different user mode', () => {
|
it('fails when the access token belongs to a different user mode', () => {
|
||||||
debugger;
|
|
||||||
logServerErrorsOtherThan(403, app);
|
logServerErrorsOtherThan(403, app);
|
||||||
return supertest(app)
|
return supertest(app)
|
||||||
.post('/AnotherUsers/change-password')
|
.post('/AnotherUsers/change-password')
|
||||||
|
|
|
@ -1439,6 +1439,8 @@ describe('relations - integration', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('nested relations', function() {
|
describe('nested relations', function() {
|
||||||
|
let accessOptions;
|
||||||
|
|
||||||
before(function defineModels() {
|
before(function defineModels() {
|
||||||
var Book = app.registry.createModel(
|
var Book = app.registry.createModel(
|
||||||
'Book',
|
'Book',
|
||||||
|
@ -1486,7 +1488,8 @@ describe('relations - integration', function() {
|
||||||
throw new Error('This should not crash the app');
|
throw new Error('This should not crash the app');
|
||||||
};
|
};
|
||||||
|
|
||||||
Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'}});
|
Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'},
|
||||||
|
accepts: [{arg: 'options', type: 'object', http: 'optionsFromRequest'}]});
|
||||||
|
|
||||||
// Now `pages` has nestRemoting set to true and no need to call nestRemoting()
|
// Now `pages` has nestRemoting set to true and no need to call nestRemoting()
|
||||||
// Book.nestRemoting('pages');
|
// Book.nestRemoting('pages');
|
||||||
|
@ -1507,6 +1510,15 @@ describe('relations - integration', function() {
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Page.observe('access', function(ctx, next) {
|
||||||
|
accessOptions = ctx.options;
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(function resetAccessOptions() {
|
||||||
|
accessOptions = 'access hook not triggered';
|
||||||
});
|
});
|
||||||
|
|
||||||
before(function createBook(done) {
|
before(function createBook(done) {
|
||||||
|
@ -1619,6 +1631,7 @@ describe('relations - integration', function() {
|
||||||
it('enables nested relationship routes - belongsTo findById', function(done) {
|
it('enables nested relationship routes - belongsTo findById', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
|
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
|
||||||
|
.expect(200)
|
||||||
.end(function(err, res) {
|
.end(function(err, res) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
|
||||||
|
@ -1658,6 +1671,14 @@ describe('relations - integration', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('passes options to nested relationship routes', function() {
|
||||||
|
return this.get(`/api/books/${this.book.id}/pages/${this.page.id}/notes/${this.note.id}`)
|
||||||
|
.expect(200)
|
||||||
|
.then(res => {
|
||||||
|
expect(accessOptions).to.have.property('accessToken');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should nest remote hooks of ModelTo - hasMany findById', function(done) {
|
it('should nest remote hooks of ModelTo - hasMany findById', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id)
|
this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id)
|
||||||
|
|
Loading…
Reference in New Issue