Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"eqnull" : true, // true: Tolerate use of `== null`
"esnext" : true, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : true, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
Expand Down
2 changes: 1 addition & 1 deletion lib/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function serviceDiscovery(account, options) {
pathname: '/.well-known/' + options.accountType
});

var req = request.basic({ method: 'GET' });
var req = { method: 'GET' };
return options.xhr.send(req, uri, { sandbox: options.sandbox })
.then(function(xhr) {
if (xhr.status >= 300 && xhr.status < 400) {
Expand Down
12 changes: 4 additions & 8 deletions lib/request/address_book_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []
}),
{
depth: options.depth
}
);
return collectionQuery({
data: template.addressBookQuery(options),
depth: options.depth
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});
};
23 changes: 0 additions & 23 deletions lib/request/basic.js

This file was deleted.

14 changes: 4 additions & 10 deletions lib/request/calendar_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ var collectionQuery = require('./collection_query'),
* (String) timezone - VTIMEZONE calendar object.
*/
module.exports = function(options) {
return collectionQuery(
template.calendarQuery({
props: options.props || [],
filters: options.filters || [],
timezone: options.timezone
}),
{
depth: options.depth
}
);
return collectionQuery({
data: template.calendarQuery(options),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here

depth: options.depth
});
};
25 changes: 13 additions & 12 deletions lib/request/collection_query.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
'use strict';

var Request = require('./request'),
parser = require('../parser'),
var parser = require('../parser'),
util = require('./util');

module.exports = function(requestData, options) {
function transformRequest(xhr) {
util.setRequestHeaders(xhr, options);
}

/**
* Options:
*
* (String) depth - optional value for Depth header.
* (String) data - request data to be sent.
*/
module.exports = function(options) {
function transformResponse(xhr) {
var multistatus = parser.multistatus(xhr.responseText);
return multistatus.response.map(function(response) {
return { href: response.href, props: util.getProps(response.propstat) };
});
}

return new Request({
return {
method: 'REPORT',
requestData: requestData,
transformRequest: transformRequest,
transformResponse: transformResponse
});
data: options.data,
transformResponse: transformResponse,
depth: options.depth
};
};
11 changes: 9 additions & 2 deletions lib/request/index.js
Original file line number Diff line number Diff line change
@@ -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');
19 changes: 6 additions & 13 deletions lib/request/propfind.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var Request = require('./request'),
parser = require('../parser'),
var parser = require('../parser'),
template = require('../template'),
util = require('./util');

Expand All @@ -12,12 +11,6 @@ var Request = require('./request'),
* (Array.<Object>) props - list of props to request.
*/
module.exports = function(options) {
var requestData = template.propfind({ props: options.props });

function transformRequest(xhr) {
util.setRequestHeaders(xhr, options);
}

function transformResponse(xhr) {
var multistatus = parser.multistatus(xhr.responseText);
var responses = multistatus.response.map(function(response) {
Expand Down Expand Up @@ -45,10 +38,10 @@ module.exports = function(options) {
return { props: merged, hrefs: hrefs };
}

return new Request({
return {
method: 'PROPFIND',
requestData: requestData,
transformRequest: transformRequest,
transformResponse: transformResponse
});
data: template.propfind(options),
transformResponse: transformResponse,
depth: options.depth
};
};
35 changes: 0 additions & 35 deletions lib/request/request.js

This file was deleted.

23 changes: 6 additions & 17 deletions lib/request/sync_collection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var Request = require('./request'),
parser = require('../parser'),
var parser = require('../parser'),
template = require('../template'),
util = require('./util');

Expand All @@ -14,16 +13,6 @@ var Request = require('./request'),
* (String) syncToken - synchronization token provided by the server.
*/
module.exports = function(options) {
var requestData = template.syncCollection({
props: options.props,
syncLevel: options.syncLevel,
syncToken: options.syncToken
});

function transformRequest(xhr) {
util.setRequestHeaders(xhr, options);
}

function transformResponse(xhr) {
var multistatus = parser.multistatus(xhr.responseText);
var responses = multistatus.response.map(function(response) {
Expand All @@ -33,10 +22,10 @@ module.exports = function(options) {
return { responses: responses, syncToken: multistatus.syncToken };
}

return new Request({
return {
method: 'REPORT',
requestData: requestData,
transformRequest: transformRequest,
transformResponse: transformResponse
});
data: template.syncCollection(options),
transformResponse: transformResponse,
depth: options.depth
};
};
12 changes: 0 additions & 12 deletions lib/request/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,3 @@ exports.getProps = function(propstats) {
})
);
};

exports.setRequestHeaders = function(request, options) {
request.setRequestHeader('Content-Type', 'application/xml;charset=utf-8');

if ('depth' in options) {
request.setRequestHeader('Depth', options.depth);
}

if ('etag' in options) {
request.setRequestHeader('If-Match', options.etag);
}
};
8 changes: 3 additions & 5 deletions lib/transport/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Transport = require('./transport'),
XMLHttpRequest = require('./xmlhttprequest'),
setRequestHeader = require('./set_request_header'),
util = require('util');

/**
Expand All @@ -15,7 +16,6 @@ module.exports = Basic;

Basic.prototype.send = function(request, url, options) {
var sandbox = options && options.sandbox,
transformRequest = request.transformRequest,
transformResponse = request.transformResponse,
onerror = request.onerror;

Expand All @@ -33,11 +33,9 @@ Basic.prototype.send = function(request, url, options) {
this.credentials.password
);

if (transformRequest) {
transformRequest(xhr);
}
setRequestHeader(xhr, request);

var promise = xhr.send(request.requestData)
var promise = xhr.send(request.data)
.then(function() {
return transformResponse ? transformResponse(xhr) : xhr;
});
Expand Down
6 changes: 2 additions & 4 deletions lib/transport/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Transport = require('./transport'),
XMLHttpRequest = require('./xmlhttprequest'),
querystring = require('querystring'),
setRequestHeader = require('./set_request_header'),
util = require('util');

/**
Expand All @@ -18,7 +19,6 @@ module.exports = OAuth2;
OAuth2.prototype.send = function(request, url, options) {
options = options || {};
var sandbox = options.sandbox,
transformRequest = request.transformRequest,
transformResponse = request.transformResponse,
onerror = request.onerror;

Expand Down Expand Up @@ -46,9 +46,7 @@ OAuth2.prototype.send = function(request, url, options) {

xhr.setRequestHeader('Authorization', 'Bearer ' + token);

if (transformRequest) {
transformRequest(xhr);
}
setRequestHeader(xhr, request);

return xhr.send(request.requestData);
})
Expand Down
17 changes: 17 additions & 0 deletions lib/transport/set_request_header.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
6 changes: 3 additions & 3 deletions lib/webdav.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ var debug = require('debug')('dav:webdav'),
* @param {String} objectData webdav object data.
*/
exports.createObject = function(objectUrl, objectData, options) {
var req = request.basic({ method: 'PUT', data: objectData });
var req = { method: 'PUT', data: objectData };
return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
};

exports.updateObject = function(objectUrl, objectData, etag, options) {
var req = request.basic({ method: 'PUT', data: objectData, etag: etag });
var req = { method: 'PUT', data: objectData, etag: etag };
return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
};

exports.deleteObject = function(objectUrl, etag, options) {
var req = request.basic({ method: 'DELETE', etag: etag });
var req = { method: 'DELETE', etag: etag };
return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
};

Expand Down
8 changes: 4 additions & 4 deletions test/unit/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ suite('Client', function() {

test('#send', function() {
var url = 'https://mail.mozilla.com/';
var req = dav.request.basic({
var req = {
method: 'PUT',
data: 'BEGIN:VCALENDAR\nEND:VCALENDAR',
etag: 'abc123'
});
};

var sandbox = dav.createSandbox();
client.send(req, url, { sandbox: sandbox });
sinon.assert.calledWith(send, req, url, { sandbox: sandbox });
});

test('#send with relative url', function() {
var req = dav.request.basic({
var req = {
method: 'PUT',
data: 'BEGIN:VCALENDAR\nEND:VCALENDAR',
etag: 'abc123'
});
};

client.send(req, '/calendars/123.ics');
sinon.assert.calledWith(
Expand Down
Loading