Skip to content
Open
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
37 changes: 20 additions & 17 deletions ti.xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ XHR = function() {};
// ================

// GET
// @e (object) url is required field. supports onSucces, onError and extraParams.
// @e (object) url is required field. supports onSucces, onError and extraParams.
XHR.prototype.GET = function(e) {
// Create some default params
var onSuccess = e.onSuccess || function() {};
var onError = e.onError || function() {};

if (e.extraParams) {
var extraParams = addDefaultsToOptions(e.extraParams);
} else {
Expand All @@ -40,7 +40,7 @@ XHR.prototype.GET = function(e) {

// When there was an error
xhr.onerror = function(err) {
onError(handleError(xhr, err));
onError(handleError(xhr, err, extraParams));
};

xhr.send();
Expand Down Expand Up @@ -81,7 +81,7 @@ XHR.prototype.POST = function(e) {
// When there was an error
xhr.onerror = function(err) {
// Check the status of this
onError(handleError(xhr, err));
onError(handleError(xhr, err, extraParams));
};

xhr.send(extraParams.parseJSON ? JSON.stringify(e.data) : e.data);
Expand Down Expand Up @@ -111,7 +111,7 @@ XHR.prototype.PUT = function(e) {
// When there was an error
xhr.onerror = function(err) {
// Check the status of this
onError(handleError(xhr, err));
onError(handleError(xhr, err, extraParams));
};

xhr.send(extraParams.parseJSON ? JSON.stringify(e.data) : e.data);
Expand Down Expand Up @@ -141,7 +141,7 @@ XHR.prototype.PATCH = function(e) {
// When there was an error
xhr.onerror = function(err) {
// Check the status of this
onError(handleError(xhr, err));
onError(handleError(xhr, err, extraParams));
};

xhr.send(extraParams.parseJSON ? JSON.stringify(e.data) : e.data);
Expand Down Expand Up @@ -170,7 +170,7 @@ XHR.prototype.DELETE = function(e) {
// When there was an error
xhr.onerror = function(err) {
// Check the status of this
onError(handleError(xhr, err));
onError(handleError(xhr, err, extraParams));
};

xhr.send();
Expand Down Expand Up @@ -273,6 +273,7 @@ function addDefaultsToOptions(providedParams) {
extraParams.contentType = extraParams.contentType || "application/json";
extraParams.parseJSON = (extraParams.hasOwnProperty('parseJSON')) ? extraParams.parseJSON : false;
extraParams.returnXML = (extraParams.hasOwnProperty('returnXML')) ? extraParams.returnXML : false;
extraParams.expectingFile = (extraParams.hasOwnProperty('expectingFile')) ? extraParams.expectingFile : false;
extraParams.debug = (extraParams.hasOwnProperty('debug')) ? extraParams.debug : false;
extraParams.requestHeaders = providedParams.requestHeaders || [];
return extraParams;
Expand All @@ -292,31 +293,33 @@ function handleSuccess(xhr, extraParams) {
try {
if (extraParams.returnXML && xhr.responseXML) {
result.data = xhr.responseXML;
} else if (extraParams.expectingFile) {
result.data = xhr.responseData;
} else {
result.data = extraParams.parseJSON ? JSON.parse(xhr.responseText) : xhr.responseText;
result.data = (extraParams.parseJSON) ? JSON.parse(xhr.responseText) : xhr.responseText;
}
} catch(e) {
} catch (e) {
result.data = xhr.responseData;
}

return result;
}

// Return a standardized response
function handleError(xhr, error) {
function handleError(xhr, error, extraParams) {
var result = {};
result.result = "error";
result.status = xhr.status;
result.error = error.error;

// Parse error result body
try {
if (extraParams.returnXML && xhr.responseXML) {
result.data = xhr.responseXML;
} else {
result.data = extraParams.parseJSON ? JSON.parse(xhr.responseText) : xhr.responseText;
result.data = (extraParams.parseJSON) ? JSON.parse(xhr.responseText) : xhr.responseText;
}
} catch(e) {
} catch (e) {
result.data = xhr.responseData;
}
return result;
Expand All @@ -325,15 +328,15 @@ function handleError(xhr, error) {
function initXHRRequest(method, url, extraParams) {
// Create the HTTP connection
var xhr = Titanium.Network.createHTTPClient({
enableKeepAlive : false
enableKeepAlive: false
});

// Open the HTTP connection
xhr.open(method, url, extraParams.async);
xhr.setRequestHeader('Content-Type', extraParams.contentType);

// add extra provided request headers
if (extraParams.requestHeaders && extraParams.requestHeaders.length > 0){
if (extraParams.requestHeaders && extraParams.requestHeaders.length > 0) {
for (var i = 0; i < extraParams.requestHeaders.length; i++) {
xhr.setRequestHeader(extraParams.requestHeaders[i].key, extraParams.requestHeaders[i].value);
}
Expand Down Expand Up @@ -418,12 +421,12 @@ function writeCache(data, url, ttl) {

// Insert the cached object in the cache manager
cacheManager[hashedURL] = {
"timestamp" : (new Date().getTime()) + (ttl * 60 * 1000)
"timestamp": (new Date().getTime()) + (ttl * 60 * 1000)
};
updateCacheManager();

//Titanium.API.info("WROTE CACHE");
};

// Return everything
module.exports = XHR;
module.exports = XHR;