1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-12 12:15:07 -05:00
mail/src/js/dao/rest-dao.js

123 lines
3.3 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
2014-02-28 14:14:32 -05:00
var config = require('js/app-config').config;
var RestDAO = function(baseUri) {
if (baseUri) {
this._baseUri = baseUri;
} else {
this._baseUri = config.cloudUrl;
}
};
/**
* GET (read) request
* @param {String} options.uri URI relative to the base uri to perform the GET request with.
* @param {String} options.type (optional) The type of data that you're expecting back from the server: json, xml, text. Default: json.
*/
RestDAO.prototype.get = function(options, callback) {
2014-06-06 12:36:23 -04:00
options.method = 'GET';
this._processRequest(options, callback);
};
/**
* POST (create) request
*/
RestDAO.prototype.post = function(item, uri, callback) {
this._processRequest({
method: 'POST',
payload: item,
uri: uri
}, callback);
};
/**
* PUT (update) request
*/
RestDAO.prototype.put = function(item, uri, callback) {
this._processRequest({
method: 'PUT',
payload: item,
uri: uri
}, callback);
};
/**
* DELETE (remove) request
*/
RestDAO.prototype.remove = function(uri, callback) {
this._processRequest({
method: 'DELETE',
uri: uri
}, callback);
};
//
// helper functions
//
RestDAO.prototype._processRequest = function(options, callback) {
var xhr, format;
if (typeof options.uri === 'undefined') {
callback({
code: 400,
errMsg: 'Bad Request! URI is a mandatory parameter.'
});
return;
}
options.type = options.type || 'json';
if (options.type === 'json') {
2014-06-06 12:36:23 -04:00
format = 'application/json';
} else if (options.type === 'xml') {
2014-06-06 12:36:23 -04:00
format = 'application/xml';
} else if (options.type === 'text') {
2014-06-06 12:36:23 -04:00
format = 'text/plain';
} else {
callback({
code: 400,
errMsg: 'Bad Request! Unhandled data type.'
});
return;
}
2014-02-28 14:14:32 -05:00
xhr = new XMLHttpRequest();
2014-06-06 12:36:23 -04:00
xhr.open(options.method, this._baseUri + options.uri);
xhr.setRequestHeader('Accept', format);
xhr.setRequestHeader('Content-Type', format);
2014-02-28 14:14:32 -05:00
xhr.onload = function() {
2014-06-06 12:36:23 -04:00
var res;
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201 || xhr.status === 304)) {
2014-02-28 14:14:32 -05:00
if (options.type === 'json') {
2014-06-06 12:36:23 -04:00
res = xhr.responseText ? JSON.parse(xhr.responseText) : xhr.responseText;
2014-02-28 14:14:32 -05:00
} else {
res = xhr.responseText;
}
2013-11-20 06:17:21 -05:00
callback(null, res, xhr.status);
2014-02-28 14:14:32 -05:00
return;
}
2014-02-28 14:14:32 -05:00
callback({
code: xhr.status,
errMsg: xhr.statusText
});
};
xhr.onerror = function() {
2014-02-28 14:14:32 -05:00
callback({
2014-05-23 04:52:34 -04:00
code: 42,
2014-06-06 12:36:23 -04:00
errMsg: 'Error calling ' + options.method + ' on ' + options.uri
2014-02-28 14:14:32 -05:00
});
};
2014-06-06 12:36:23 -04:00
xhr.send(options.payload ? JSON.stringify(options.payload) : undefined);
};
return RestDAO;
});