diff --git a/libs/fetcher.client.js b/libs/fetcher.client.js index 0442bb9d..6f656181 100644 --- a/libs/fetcher.client.js +++ b/libs/fetcher.client.js @@ -253,6 +253,11 @@ module.exports = function createFetcherClient (options) { * @private */ _sync: function (resource, operation, params, body, config, callback) { + if (typeof config === 'function') { + callback = config; + config = {}; + } + config = config || {}; config.xhr = Fetcher.pathPrefix; diff --git a/libs/fetcher.js b/libs/fetcher.js index addf2702..5533c5e5 100644 --- a/libs/fetcher.js +++ b/libs/fetcher.js @@ -87,6 +87,7 @@ module.exports = function createFetcherClass (options) { var defaultPath = Fetcher.pathPrefix + '/resource/', path = req.path.substr(defaultPath.length).split(';'); request = { + req: req, resource: path.shift(), operation: OP_READ, params: qs.parse(path.join('&')), @@ -102,7 +103,7 @@ module.exports = function createFetcherClass (options) { var requests = req.body.requests; if (!requests || requests.length === 0) { - res.send(204); + res.status(204).end(); } var DEFAULT_GUID = 'g0', @@ -116,7 +117,7 @@ module.exports = function createFetcherClass (options) { config: singleRequest.config, callback: function(err, data) { if(err) { - res.send('400', 'request failed'); + res.status(400).send('request failed'); } var responseObj = {}; responseObj[DEFAULT_GUID] = {data: data}; @@ -161,7 +162,14 @@ module.exports = function createFetcherClass (options) { body = request.body, config = request.config, callback = request.callback, - args = [req, resource, params, config, callback]; + args; + + if (typeof config === 'function') { + callback = config; + config = {}; + } + + args = [req, resource, params, config, callback]; if ((op === OP_CREATE) || (op === OP_UPDATE)) { args.splice(3, 0, body); diff --git a/tests/unit/libs/fetcher.client.js b/tests/unit/libs/fetcher.client.js index cc1fee42..e71281d0 100644 --- a/tests/unit/libs/fetcher.client.js +++ b/tests/unit/libs/fetcher.client.js @@ -101,18 +101,34 @@ describe('Client Fetcher', function () { var operation = 'create'; fetcher[operation](resource, params, body, config, callback(operation, done)); }); + it('should handle CREATE w/ no config', function (done) { + var operation = 'create'; + fetcher[operation](resource, params, body, callback(operation, done)); + }); it('should handle READ', function (done) { var operation = 'read'; fetcher[operation](resource, params, config, done); }); + it('should handle READ w/ no config', function (done) { + var operation = 'read'; + fetcher[operation](resource, params, done); + }); it('should handle UPDATE', function (done) { var operation = 'update'; fetcher[operation](resource, params, body, config, callback(operation, done)); }); + it('should handle UPDATE w/ no config', function (done) { + var operation = 'update'; + fetcher[operation](resource, params, body, callback(operation, done)); + }); it('should handle DELETE', function (done) { var operation = 'del'; fetcher[operation](resource, params, config, callback('delete', done)); }); + it('should handle DELETE w/ no config', function (done) { + var operation = 'del'; + fetcher[operation](resource, params, callback('delete', done)); + }); }); }); diff --git a/tests/unit/libs/fetcher.js b/tests/unit/libs/fetcher.js index 1c6a0035..3aa527a2 100644 --- a/tests/unit/libs/fetcher.js +++ b/tests/unit/libs/fetcher.js @@ -19,8 +19,6 @@ var expect = chai.expect, qs = require('querystring'); describe('Server Fetcher', function () { -// var Fetcher = fetchr(), -// fetcher = new Fetcher(); it('should register fetchers', function () { var fn = Fetcher.getFetcher.bind(fetcher, mockFetcher.name); @@ -40,8 +38,11 @@ describe('Server Fetcher', function () { json: function () { console.log('Not Expected: middleware responded with json'); }, - send: function (code) { + status: function (code) { console.log('Not Expected: middleware responded with', code); + }, + send: function () { + console.log('Not Expected: middleware responded with'); } }, next = function () { @@ -133,19 +134,30 @@ describe('Server Fetcher', function () { body = {}, config = {}; - it('should handle CREATE', function (done) { fetcher.create(resource, params, body, config, done); }); + it('should handle CREATE w/ no config', function (done) { + fetcher.create(resource, params, body, done); + }); it('should handle READ', function (done) { fetcher.read(resource, params, config, done); }); + it('should handle READ w/ no config', function (done) { + fetcher.read(resource, params, done); + }); it('should handle UPDATE', function (done) { fetcher.update(resource, params, body, config, done); }); + it('should handle UPDATE w/ no config', function (done) { + fetcher.update(resource, params, body, done); + }); it('should handle DELETE', function (done) { fetcher.del(resource, params, config, done); }); + it('should handle DELETE w/ no config', function (done) { + fetcher.del(resource, params, done); + }); }); });