You can clone with HTTPS, SSH, or Subversion.
Download ZIPLoading…
millermedeiros |
Simplify request logic
…
- Centralize the XHR header logic into the transport itself since it's needed by all the requests, removing the need of the individual `transformRequest` calls. - Remove `request.basic` since it can be represented as a plain object. - Instead of using `instanceof` we check if generated requests contains the desired properties. - Remove the `Request` constructor since it wasn't doing anything and was introducing an extra level of indirection and used a JSDoc @typedef instead. - Simplified the way XML templates are interpolated in the requests. |
fb1b3aa
|
Owner
gaye
added a note
I think I probably wrote this for the case when we get an options without props (we really shouldn't?) but I think the template might require props? Handlebars #each is smart enough. Shouldn't matter
|
Only those with write access to this repository can merge pull requests.
- Centralize the XHR header logic into the transport itself since it's needed by all the requests, removing the need of the individual `transformRequest` calls. - Remove `request.basic` since it can be represented as a plain object. - Instead of using `instanceof` we check if generated requests contains the desired properties. - Remove the `Request` constructor since it wasn't doing anything and was introducing an extra level of indirection and used a JSDoc @typedef instead. - Simplified the way XML templates are interpolated in the requests.
|
@@ -10,12 +10,8 @@ var collectionQuery = require('./collection_query'), | ||
* (Array.<Object>) props - list of props to request. | ||
*/ | ||
module.exports = function(options) { | ||
- return collectionQuery( | ||
- template.addressBookQuery({ | ||
- props: options.props || [] | ||
Owner
gaye
added a note
I think I probably wrote this for the case when we get an options without props (we really shouldn't?) but I think the template might require props? Handlebars #each is smart enough. Shouldn't matter
|
||
- }), | ||
- { | ||
- depth: options.depth | ||
- } | ||
- ); | ||
+ return collectionQuery({ | ||
+ data: template.addressBookQuery(options), | ||
+ depth: options.depth | ||
+ }); | ||
}; |
@@ -1,23 +0,0 @@ | ||
-'use strict'; | ||
- | ||
-var Request = require('./request'), | ||
- util = require('./util'); | ||
- | ||
-/** | ||
- * Options: | ||
- * | ||
- * (String) data - put request body. | ||
- * (String) method - http method. | ||
- * (String) etag - cached calendar object etag. | ||
- */ | ||
-module.exports = function(options) { | ||
- function transformRequest(xhr) { | ||
- util.setRequestHeaders(xhr, options); | ||
- } | ||
- | ||
- return new Request({ | ||
- method: options.method, | ||
- requestData: options.data, | ||
- transformRequest: transformRequest | ||
- }); | ||
-}; |
@@ -12,14 +12,8 @@ var collectionQuery = require('./collection_query'), | ||
* (String) timezone - VTIMEZONE calendar object. | ||
*/ | ||
module.exports = function(options) { | ||
- return collectionQuery( | ||
- template.calendarQuery({ | ||
Owner
gaye
added a note
Same thing here |
||
- props: options.props || [], | ||
- filters: options.filters || [], | ||
- timezone: options.timezone | ||
- }), | ||
- { | ||
- depth: options.depth | ||
- } | ||
- ); | ||
+ return collectionQuery({ | ||
+ data: template.calendarQuery(options), | ||
+ depth: options.depth | ||
+ }); | ||
}; |
@@ -1,6 +1,13 @@ | ||
-exports.Request = require('./request'); | ||
+/** | ||
+ * Request Object | ||
+ * @typedef {Object} Request | ||
+ * @property {string} method - Method of the request (eg. PROPFIND, REPORT, GET) | ||
+ * @property {string} data - Data to be sent with the Request. | ||
+ * @property {function(XMLHttprequest)} transformResponse - Callback that maps | ||
+ * the request result. | ||
+ */ | ||
+ | ||
exports.addressBookQuery = require('./address_book_query'); | ||
-exports.basic = require('./basic'); | ||
exports.calendarQuery = require('./calendar_query'); | ||
exports.propfind = require('./propfind'); | ||
exports.syncCollection = require('./sync_collection'); |
@@ -1,35 +0,0 @@ | ||
-'use strict'; | ||
- | ||
-function Request(options) { | ||
- for (var key in options) { | ||
- this[key] = options[key]; | ||
- } | ||
-} | ||
-module.exports = Request; | ||
- | ||
-Request.prototype = { | ||
- /** | ||
- * @type {String} | ||
- */ | ||
- method: null, | ||
- | ||
- /** | ||
- * @type {String} | ||
- */ | ||
- requestData: null, | ||
- | ||
- /** | ||
- * @type {Function} | ||
- */ | ||
- transformRequest: null, | ||
- | ||
- /** | ||
- * @type {Function} | ||
- */ | ||
- transformResponse: null, | ||
- | ||
- /** | ||
- * @type {Function} | ||
- */ | ||
- onerror: null | ||
-}; |
@@ -0,0 +1,17 @@ | ||
+'use strict'; | ||
+ | ||
+/** | ||
+ * @param {XMLHttpRequest} xhr | ||
+ * @param {Request} request | ||
+ */ | ||
+module.exports = function(xhr, request) { | ||
+ xhr.setRequestHeader('Content-Type', 'application/xml;charset=utf-8'); | ||
+ | ||
+ if (request.depth != null) { | ||
+ xhr.setRequestHeader('Depth', request.depth); | ||
+ } | ||
+ | ||
+ if (request.etag != null) { | ||
+ xhr.setRequestHeader('If-Match', request.etag); | ||
+ } | ||
+}; |
@@ -0,0 +1,35 @@ | ||
+'use strict'; | ||
+ | ||
+var setRequestHeader = require('../../../lib/transport/set_request_header'), | ||
+ sinon = require('sinon'); | ||
+ | ||
+suite('setRequestHeader', function() { | ||
+ var xhr; | ||
+ | ||
+ setup(function() { | ||
+ xhr = { | ||
+ setRequestHeader: sinon.spy() | ||
+ }; | ||
+ }); | ||
+ | ||
+ test('should set Content-Type to XML by default', function() { | ||
+ setRequestHeader(xhr, { depth: undefined }); | ||
+ | ||
+ sinon.assert.calledWithExactly(xhr.setRequestHeader, | ||
+ 'Content-Type', 'application/xml;charset=utf-8'); | ||
+ sinon.assert.calledOnce(xhr.setRequestHeader); | ||
+ }); | ||
+ | ||
+ test('should set depth and etag if provided', function() { | ||
+ setRequestHeader(xhr, { depth: '1234', etag: '789' }); | ||
+ | ||
+ sinon.assert.calledWithExactly(xhr.setRequestHeader, | ||
+ 'Content-Type', 'application/xml;charset=utf-8'); | ||
+ sinon.assert.calledWithExactly(xhr.setRequestHeader, | ||
+ 'Depth', '1234'); | ||
+ sinon.assert.calledWithExactly(xhr.setRequestHeader, | ||
+ 'If-Match', '789'); | ||
+ sinon.assert.calledThrice(xhr.setRequestHeader); | ||
+ }); | ||
+ | ||
+}); |
transformRequest
calls.request.basic
since it can be represented as a plain object.instanceof
we check if generated requests contains the desired properties.Request
constructor since it wasn't doing anything and was introducing an extra level of indirection and used a JSDoc@typedef
instead.