From 8b7b430e2344052bf89abb0e23b23788573b4e9f Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Thu, 26 Oct 2017 15:31:31 -0700 Subject: [PATCH] Make function signature of resume prettier for loadTransaction --- lib/server.js | 13 +++++++------ test/server.test.js | 20 ++++++++++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/server.js b/lib/server.js index 88ac1491..97c7bece 100644 --- a/lib/server.js +++ b/lib/server.js @@ -138,19 +138,20 @@ Server.prototype.authorization = function(options, validate, immediate, complete }; Server.prototype.resume = function(options, immediate, complete) { - var ld; + var loader; + if (typeof options == 'function' && typeof immediate == 'function' && typeof complete == 'function') { + options = { loadTransaction: options }; + } if (options && options.loadTransaction === false) { return resume(this, options, immediate, complete); } - if (options && typeof options.loadTransaction === 'function') { - ld = options.loadTransaction; + loader = options.loadTransaction; } else { - ld = transactionLoader(this, options); + loader = transactionLoader(this, options); } - - return [ld, resume(this, options, immediate, complete)]; + return [loader, resume(this, options, immediate, complete)]; }; /** diff --git a/test/server.test.js b/test/server.test.js index 82533e6b..321baad2 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -90,13 +90,25 @@ describe('Server', function() { expect(handler).to.have.length(3); }); - it('should employ custom transaction loader', function() { - function customTransaction(req, res, next) {}; - var handler = server.resume({ loadTransaction: customTransaction }, function(){}); + it('should create handler stack with custom transaction loader', function() { + function loadTransaction(req, res, next) {}; + var handler = server.resume({ loadTransaction: loadTransaction }, function(){}); expect(handler).to.be.an('array'); expect(handler).to.have.length(2); expect(handler[0]).to.be.a('function'); - expect(handler[0].name).to.equal('customTransaction'); + expect(handler[0].name).to.equal('loadTransaction'); + expect(handler[0]).to.have.length(3); + expect(handler[1]).to.be.a('function'); + expect(handler[1]).to.have.length(3); + }); + + it('should create handler stack with custom transaction loader using non-object signature', function() { + function loadTransaction(req, res, next) {}; + var handler = server.resume(loadTransaction, function(){}, function(){}); + expect(handler).to.be.an('array'); + expect(handler).to.have.length(2); + expect(handler[0]).to.be.a('function'); + expect(handler[0].name).to.equal('loadTransaction'); expect(handler[0]).to.have.length(3); expect(handler[1]).to.be.a('function'); expect(handler[1]).to.have.length(3);