mojax
Version:
extendable ajax request module
154 lines (134 loc) • 4.72 kB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.xhrFactory = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* @author jQuery
* @copyright jQuery foundation
* @license https://github.com/jquery/jquery/blob/master/LICENSE.txt
*
* Parses XML string content
* @param {string} data - content
* @returns {xml}
*/
function parseXML(data) {
"use strict";
var xml;
// Support: IE9
try {
xml = (new window.DOMParser()).parseFromString(data, "text/xml");
} catch (e) {
xml = undefined;
}
if (!xml || xml.getElementsByTagName("parsererror").length) {
throw new SyntaxError("");
}
return xml;
}
/**
* @memberof ajax
* @summary parses the response data based on provided responseType
* @private
* @param xhr {object} XMLHttpRequest object
* @returns {object|string}
*/
module.exports = function parseResponse(xhr) {
"use strict";
// only try to parse if not already parsed by xhr
if (typeof xhr.response === "string" && xhr.response !== "") {
if (xhr.responseType === "json") {
return JSON.parse(xhr.response);
} else if (xhr.type === "text/xml" || xhr.type === "xml") {
return parseXML(xhr.response);
}
}
return xhr.response;
};
},{}],2:[function(require,module,exports){
var parseResponse = require("./parseResponse");
/**
* @summary triggers existing listeners based on state of XMLHttpRequest.
* @param xhr
* @param {object} c - configuration object
*/
module.exports = function(xhr, c) {
"use strict";
var resp;
switch (xhr.readyState) {
case 1: {
if (c.onOpen.length) {
c.onOpen.forEach(function(cb) {
cb(xhr);
});
}
break;
}
case 2: {
if (c.onHeaders.length) {
c.onHeaders.forEach(function(cb) {
cb(xhr);
});
}
break;
}
case 4: {
// dispatch to callbacks
if (c.onSuccess.length && /2|3/.test(xhr.status.toString().charAt(0))) {
try {
resp = parseResponse(xhr.response, c.responseType);
} catch (e) {
if (e instanceof SyntaxError) {
throw new SyntaxError("unable to parse response of type: " + c.responseType + " for\n" + xhr.response);
} else {
throw Error(e);
}
}
// SUCCESS
c.onSuccess.forEach(function(cb) {
cb(xhr, resp);
});
} else if (c.onFailure.length && /4|5/.test(xhr.status.toString().charAt(0))) {
// FAILURE
c.onFailure.forEach(function(cb) {
cb(xhr);
});
}
break;
}
default: {
break;
}
}
};
},{"./parseResponse":1}],3:[function(require,module,exports){
var responseListener = require("./responseListener");
/**
* @memberof ajax
* @summary executes the XMLHttpRequest
* @private
* @param c {object} - request configuration hash
*/
module.exports = function xhrFactory(c) {
"use strict";
var xhr = new XMLHttpRequest();
// early abort, if config invalid
if (Object.prototype.toString.call(c) !== "[object Object]") {
return;
}
// bind listeners
xhr.onreadystatechange = function() {
responseListener(xhr, c);
};
// start XMLHttpRequest
xhr.open(c.method, c.url, true);
// set request headers
// must be done after open()
Object.keys(c.headers || {}).forEach(function(h) {
xhr.setRequestHeader(h, c.headers[h]);
});
// handle timeout
if (c.timeout) {
xhr.timeout = c.timeout;
}
xhr.send(c.data);
return xhr;
};
},{"./responseListener":2}]},{},[3])(3)
});