Skip to content

Commit cad194e

Browse files
committed
Merge pull request #68 from yahoo/qsparse
Use JSON.parse on query string values in case they are arrays or objects
2 parents db2cdaf + cc6ba67 commit cad194e

File tree

8 files changed

+140
-100
lines changed

8 files changed

+140
-100
lines changed

libs/fetcher.client.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ Queue.prototype = {
247247
}
248248

249249
config = config || {};
250-
config.xhr = this.xhrPath;
251250

252251
var self = this,
253252
request = {
@@ -361,7 +360,7 @@ Queue.prototype = {
361360
callback = request.callback || lodash.noop,
362361
use_post,
363362
allow_retry_post,
364-
uri = config.uri || config.xhr || this.xhrPath,
363+
uri = config.uri || this.xhrPath,
365364
get_uri,
366365
requests,
367366
data;
@@ -434,7 +433,7 @@ Queue.prototype = {
434433
lodash.forEach(requests, function (request) {
435434
var uri, batch, group_id;
436435
if (request.config) {
437-
uri = request.config.uri || request.config.xhr || '';
436+
uri = request.config.uri || this.xhrPath;
438437
batch = request.config.batch;
439438
}
440439
group_id = 'uri:' + uri;
@@ -478,7 +477,7 @@ Queue.prototype = {
478477
return false;
479478
}, this);
480479

481-
uri = config.uri || config.xhr || this.xhrPath;
480+
uri = config.uri || this.xhrPath;
482481

483482
data = {
484483
requests: {},

libs/fetcher.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@
66
/**
77
* list of registered fetchers
88
*/
9-
var OP_READ = 'read',
10-
OP_CREATE = 'create',
11-
OP_UPDATE = 'update',
12-
GET = 'GET',
13-
qs = require('querystring');
9+
var OP_READ = 'read';
10+
var OP_CREATE = 'create';
11+
var OP_UPDATE = 'update';
12+
var GET = 'GET';
13+
var qs = require('querystring');
14+
var debug = require('debug')('Fetchr');
15+
16+
function parseValue(value) {
17+
// take care of value of type: array, object
18+
try {
19+
return JSON.parse(value);
20+
} catch (e) {
21+
return value;
22+
}
23+
}
24+
25+
function parseParamValues (params) {
26+
return Object.keys(params).reduce(function (parsed, curr) {
27+
parsed[curr] = parseValue(params[curr]);
28+
return parsed;
29+
}, {});
30+
}
1431

1532
/*
1633
* @module createFetcherClass
1734
* @param {object} options
1835
*/
1936

20-
var debug = require('debug')('Fetchr');
2137

2238
/**
2339
* @class Fetcher
@@ -82,7 +98,7 @@ var OP_READ = 'read',
8298
req: req,
8399
resource: path.shift(),
84100
operation: OP_READ,
85-
params: qs.parse(path.join('&')),
101+
params: parseParamValues(qs.parse(path.join('&'))),
86102
config: {},
87103
callback: function (err, data, meta) {
88104
if (err) {
@@ -151,7 +167,6 @@ var OP_READ = 'read',
151167
* @static
152168
*/
153169
Fetcher.single = function (request) {
154-
debug(request.resource);
155170
var fetcher = Fetcher.getFetcher(request.resource.split('.')[0]),
156171
op = request.operation,
157172
req = request.req,

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
"dependencies": {
2424
"debug": "^2.0.0",
2525
"lodash": "^3.3.0",
26-
"object-assign": "^2.0.0",
2726
"xhr": "^2.0.0"
2827
},
2928
"devDependencies": {
29+
"body-parser": "^1.12.2",
3030
"chai": "^2.0.0",
3131
"coveralls": "^2.11.1",
32+
"express": "^4.12.3",
3233
"istanbul": "^0.3.2",
3334
"jshint": "^2.5.1",
3435
"mocha": "^2.0.1",
3536
"mockery": "^1.4.0",
36-
"precommit-hook": "^1.0.2"
37+
"precommit-hook": "^1.0.2",
38+
"supertest": "^0.15.0"
3739
},
3840
"jshintConfig": {
3941
"node": true

tests/mock/fakeErrorFetcher.js renamed to tests/mock/MockErrorService.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Copyright 2014, Yahoo! Inc.
33
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
44
*/
5-
var Fetcher = {
6-
name: 'fake_error_fetcher',
5+
var MockErrorService = {
6+
name: 'mock_error_service',
77

88
// ------------------------------------------------------------------
99
// CRUD Methods
@@ -16,11 +16,11 @@ var Fetcher = {
1616
* @param {String} resource The resource name
1717
* @param {Object} params The parameters identify the resource, and along with information
1818
* carried in query and matrix parameters in typical REST API
19-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
19+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
2020
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
2121
* @static
2222
*/
23-
read: function (req, resource, params, context, callback) {
23+
read: function (req, resource, params, config, callback) {
2424
callback({
2525
statusCode: parseInt(params.statusCode),
2626
message: params.message,
@@ -35,11 +35,11 @@ var Fetcher = {
3535
* @param {Object} params The parameters identify the resource, and along with information
3636
* carried in query and matrix parameters in typical REST API
3737
* @param {Object} body The JSON object that contains the resource data that is being created
38-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
38+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
3939
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
4040
* @static
4141
*/
42-
create: function (req, resource, params, body, context, callback) {
42+
create: function (req, resource, params, body, config, callback) {
4343
callback({
4444
statusCode: parseInt(params.statusCode),
4545
message: params.message,
@@ -54,11 +54,11 @@ var Fetcher = {
5454
* @param {Object} params The parameters identify the resource, and along with information
5555
* carried in query and matrix parameters in typical REST API
5656
* @param {Object} body The JSON object that contains the resource data that is being updated
57-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
57+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
5858
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
5959
* @static
6060
*/
61-
update: function (req, resource, params, body, context, callback) {
61+
update: function (req, resource, params, body, config, callback) {
6262
callback({
6363
statusCode: parseInt(params.statusCode),
6464
message: params.message,
@@ -72,11 +72,11 @@ var Fetcher = {
7272
* @param {String} resource The resource name
7373
* @param {Object} params The parameters identify the resource, and along with information
7474
* carried in query and matrix parameters in typical REST API
75-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
75+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
7676
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
7777
* @static
7878
*/
79-
delete: function (req, resource, params, context, callback) {
79+
delete: function (req, resource, params, config, callback) {
8080
callback({
8181
statusCode: parseInt(params.statusCode),
8282
message: params.message,
@@ -86,4 +86,4 @@ var Fetcher = {
8686

8787
};
8888

89-
module.exports = Fetcher;
89+
module.exports = MockErrorService;

tests/mock/fakeFetcher.js renamed to tests/mock/MockService.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Copyright 2014, Yahoo! Inc.
33
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
44
*/
5-
var Fetcher = {
6-
name: 'fake_fetcher',
5+
var debug = require('debug')('mservice');
6+
var MockService = {
7+
name: 'mock_service',
78

89
// ------------------------------------------------------------------
910
// CRUD Methods
@@ -16,20 +17,20 @@ var Fetcher = {
1617
* @param {String} resource The resource name
1718
* @param {Object} params The parameters identify the resource, and along with information
1819
* carried in query and matrix parameters in typical REST API
19-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
20+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
2021
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
2122
* @static
2223
*/
23-
read: function (req, resource, params, context, callback) {
24+
read: function (req, resource, params, config, callback) {
25+
debug([].splice.call(arguments, 1));
2426
callback(null, {
2527
operation: {
2628
name: 'read',
2729
success: true
2830
},
2931
args: {
3032
resource: resource,
31-
params: params,
32-
context: context
33+
params: params
3334
}
3435
}, this.meta);
3536
this.meta = null;
@@ -42,20 +43,19 @@ var Fetcher = {
4243
* @param {Object} params The parameters identify the resource, and along with information
4344
* carried in query and matrix parameters in typical REST API
4445
* @param {Object} body The JSON object that contains the resource data that is being created
45-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
46+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
4647
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
4748
* @static
4849
*/
49-
create: function (req, resource, params, body, context, callback) {
50+
create: function (req, resource, params, body, config, callback) {
5051
callback(null, {
5152
operation: {
5253
name: 'create',
5354
success: true
5455
},
5556
args: {
5657
resource: resource,
57-
params: params,
58-
context: context
58+
params: params
5959
}
6060
}, this.meta);
6161
this.meta = null;
@@ -68,20 +68,19 @@ var Fetcher = {
6868
* @param {Object} params The parameters identify the resource, and along with information
6969
* carried in query and matrix parameters in typical REST API
7070
* @param {Object} body The JSON object that contains the resource data that is being updated
71-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
71+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
7272
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
7373
* @static
7474
*/
75-
update: function (req, resource, params, body, context, callback) {
75+
update: function (req, resource, params, body, config, callback) {
7676
callback(null, {
7777
operation: {
7878
name: 'update',
7979
success: true
8080
},
8181
args: {
8282
resource: resource,
83-
params: params,
84-
context: context
83+
params: params
8584
}
8685
}, this.meta);
8786
this.meta = null;
@@ -93,25 +92,24 @@ var Fetcher = {
9392
* @param {String} resource The resource name
9493
* @param {Object} params The parameters identify the resource, and along with information
9594
* carried in query and matrix parameters in typical REST API
96-
* @param {Object} [context={}] The context object. It can contain "config" for per-request config data.
95+
* @param {Object} [config={}] The config object. It can contain "config" for per-request config data.
9796
* @param {Fetcher~fetcherCallback} callback callback invoked when fetcher is complete.
9897
* @static
9998
*/
100-
'delete': function (req, resource, params, context, callback) {
99+
'delete': function (req, resource, params, config, callback) {
101100
callback(null, {
102101
operation: {
103102
name: 'delete',
104103
success: true
105104
},
106105
args: {
107106
resource: resource,
108-
params: params,
109-
context: context
107+
params: params
110108
}
111109
}, this.meta);
112110
this.meta = null;
113111
}
114112

115113
};
116114

117-
module.exports = Fetcher;
115+
module.exports = MockService;

tests/mock/app.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2014, Yahoo! Inc.
3+
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4+
*/
5+
6+
var http = require('http');
7+
var express = require('express');
8+
var bodyParser = require('body-parser');
9+
var Fetcher = require('../../libs/fetcher.js');
10+
var mockService = require('./MockService');
11+
var mockErrorService = require('./MockErrorService');
12+
13+
Fetcher.registerFetcher(mockService);
14+
Fetcher.registerFetcher(mockErrorService);
15+
16+
var app = express();
17+
app.use(bodyParser.json());
18+
app.use('/api', Fetcher.middleware());
19+
var port = process.env.PORT || 3000;
20+
var server = http.createServer(app).listen(port);
21+
console.log('Listening on port ' + port);
22+
module.exports = server;
23+
module.exports.cleanup = function () {
24+
Fetcher.fetchers = {};
25+
};

0 commit comments

Comments
 (0)