Skip to content

Instantly share code, notes, and snippets.

@message
Created July 27, 2015 11:25
Show Gist options
  • Save message/60c5851f110dbcc0894a to your computer and use it in GitHub Desktop.
Save message/60c5851f110dbcc0894a to your computer and use it in GitHub Desktop.
! function(e) {
"use strict";
var t = function(e) {
var t = function(e, t, n) {
n = "function" == typeof n ? n() : null === n ? "" : void 0 === n ? "" : n, e[e.length] = encodeURIComponent(t) + "=" + encodeURIComponent(n)
},
n = function(e, o, s) {
var r, i, a;
if ("[object Array]" === Object.prototype.toString.call(o))
for (r = 0, i = o.length; i > r; r++) n(e + "[" + ("object" == typeof o[r] ? r : "") + "]", o[r], s);
else if (o && "[object Object]" === o.toString())
for (a in o) o.hasOwnProperty(a) && (e ? n(e + "[" + a + "]", o[a], s, t) : n(a, o[a], s, t));
else if (e) t(s, e, o);
else
for (a in o) t(s, a, o[a]);
return s
};
return n("", e, []).join("&").replace(/%20/g, "+")
};
"object" == typeof module && "object" == typeof module.exports ? module.exports = t : "function" == typeof define && define.amd ? define([], function() {
return t
}) : e.param = t
}(this),
function(e) {
function t(e) {
return "function" == typeof e
}
function n(e) {
return "object" == typeof e
}
function o(e) {
"undefined" != typeof setImmediate ? setImmediate(e) : "undefined" != typeof process && process.nextTick ? process.nextTick(e) : setTimeout(e, 0)
}
var s;
e[0][e[1]] = function r(e) {
var i, a = [],
p = [],
c = function(e, t) {
return null == i && null != e && (i = e, a = t, p.length && o(function() {
for (var e = 0; e < p.length; e++) p[e]()
})), i
};
return c.then = function(c, d) {
var u = r(e),
m = function() {
function e(o) {
var r, i = 0;
try {
if (o && (n(o) || t(o)) && t(r = o.then)) {
if (o === u) throw new TypeError;
r.call(o, function() {
i++ || e.apply(s, arguments)
}, function(e) {
i++ || u(!1, [e])
})
} else u(!0, arguments)
} catch (a) {
i++ || u(!1, [a])
}
}
try {
var o = i ? c : d;
t(o) ? e(o.apply(s, a || [])) : u(i, a)
} catch (r) {
u(!1, [r])
}
};
return null != i ? o(m) : p.push(m), u
}, e && (c = e(c)), c
}
}("undefined" == typeof module ? [window, "pinkySwear"] : [module, "exports"]),
function(e, t, n) {
"undefined" != typeof module && module.exports ? module.exports = n : "function" == typeof define && define.amd ? define(n) : e[t] = n
}(this, "qwest", function() {
var win = window,
doc = document,
defaultXdrResponseType = "json",
limit = null,
requests = 0,
request_stack = [],
getXHR = function() {
return win.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP")
},
xhr2 = "" === getXHR().responseType,
qwest = function(method, url, data, options, before) {
method = method.toUpperCase(), data = data || null, options = options || {};
var nativeResponseParsing = !1,
crossOrigin, xhr, xdr = !1,
timeoutInterval, aborted = !1,
attempts = 0,
headers = {},
mimeTypes = {
text: "*/*",
xml: "text/xml",
json: "application/json",
post: "application/x-www-form-urlencoded"
},
accept = {
text: "*/*",
xml: "application/xml; q=1.0, text/xml; q=0.8, */*; q=0.1",
json: "application/json; q=1.0, text/*; q=0.8, */*; q=0.1"
},
contentType = "Content-Type",
vars = "",
i, j, serialized, response, sending = !1,
delayed = !1,
timeout_start, promise = require("pinkySwear")(function(e) {
return e["catch"] = function(t) {
return e.then(null, t)
}, e.complete = function(t) {
return e.then(t, t)
}, e.send = function() {
if (!sending) {
if (sending = !0, limit && ++requests == limit) return void request_stack.push(e);
if (timeout_start = Date.now(), xhr = getXHR(), crossOrigin && ("withCredentials" in xhr || !win.XDomainRequest || (xhr = new XDomainRequest, xdr = !0, "GET" != method && "POST" != method && (method = "POST"))), xdr ? xhr.open(method, url) : (xhr.open(method, url, options.async, options.user, options.password), xhr2 && options.async && (xhr.withCredentials = options.withCredentials)), !xdr)
for (var t in headers) xhr.setRequestHeader(t, headers[t]);
if (xhr2 && "document" != options.responseType && "auto" != options.responseType) try {
xhr.responseType = options.responseType, nativeResponseParsing = xhr.responseType == options.responseType
} catch (n) {}
xhr2 || xdr ? (xhr.onload = handleResponse, xhr.onerror = handleError) : xhr.onreadystatechange = function() {
4 == xhr.readyState && handleResponse()
}, "auto" != options.responseType && "overrideMimeType" in xhr && xhr.overrideMimeType(mimeTypes[options.responseType]), before && before(xhr), xdr ? setTimeout(function() {
xhr.send("GET" != method ? data : null)
}, 0) : xhr.send("GET" != method ? data : null)
}
}, e
}),
handleResponse = function() {
var i, responseType;
if (--requests, sending = !1, Date.now() - timeout_start >= options.timeout) return void(options.attempts && ++attempts == options.attempts ? promise(!1, [xhr, response, new Error("Timeout (" + url + ")")]) : promise.send());
request_stack.length && request_stack.shift().send();
try {
if (nativeResponseParsing && "response" in xhr && null !== xhr.response) response = xhr.response;
else if ("document" == options.responseType) {
var frame = doc.createElement("iframe");
frame.style.display = "none", doc.body.appendChild(frame), frame.contentDocument.open(), frame.contentDocument.write(xhr.response), frame.contentDocument.close(), response = frame.contentDocument, doc.body.removeChild(frame)
} else {
if (responseType = options.responseType, "auto" == responseType)
if (xdr) responseType = defaultXdrResponseType;
else {
var ct = xhr.getResponseHeader(contentType) || "";
responseType = ct.indexOf(mimeTypes.json) > -1 ? "json" : ct.indexOf(mimeTypes.xml) > -1 ? "xml" : "text"
}
switch (responseType) {
case "json":
try {
response = "JSON" in win ? JSON.parse(xhr.responseText) : eval("(" + xhr.responseText + ")")
} catch (e) {
throw "Error while parsing JSON body : " + e
}
break;
case "xml":
try {
win.DOMParser ? response = (new DOMParser).parseFromString(xhr.responseText, "text/xml") : (response = new ActiveXObject("Microsoft.XMLDOM"), response.async = "false", response.loadXML(xhr.responseText))
} catch (e) {
response = void 0
}
if (!response || !response.documentElement || response.getElementsByTagName("parsererror").length) throw "Invalid XML";
break;
default:
response = xhr.responseText
}
}
if ("status" in xhr && !/^2|1223/.test(xhr.status)) throw xhr.status + " (" + xhr.statusText + ")";
promise(!0, [xhr, response])
} catch (e) {
promise(!1, [xhr, response, e])
}
},
handleError = function(e) {
--requests, promise(!1, [xhr, null, new Error("Connection aborted")])
};
switch (options.async = "async" in options ? !!options.async : !0, options.cache = "cache" in options ? !!options.cache : "GET" != method, options.dataType = "dataType" in options ? options.dataType.toLowerCase() : "post", options.responseType = "responseType" in options ? options.responseType.toLowerCase() : "auto", options.user = options.user || "", options.password = options.password || "", options.withCredentials = !!options.withCredentials, options.timeout = "timeout" in options ? parseInt(options.timeout, 10) : 3e4, options.attempts = "attempts" in options ? parseInt(options.attempts, 10) : 1, i = url.match(/\/\/(.+?)\//), crossOrigin = i && (i[1] ? i[1] != location.host : !1), "ArrayBuffer" in win && data instanceof ArrayBuffer ? options.dataType = "arraybuffer" : "Blob" in win && data instanceof Blob ? options.dataType = "blob" : "Document" in win && data instanceof Document ? options.dataType = "document" : "FormData" in win && data instanceof FormData && (options.dataType = "formdata"), options.dataType) {
case "json":
data = JSON.stringify(data);
break;
case "post":
data = require("jquery-param")(data)
}
if (options.headers) {
var format = function(e, t, n) {
return t + n.toUpperCase()
};
for (i in options.headers) headers[i.replace(/(^|-)([^-])/g, format)] = options.headers[i]
}
return headers[contentType] || "GET" == method || options.dataType in mimeTypes && mimeTypes[options.dataType] && (headers[contentType] = mimeTypes[options.dataType]), headers.Accept || (headers.Accept = options.responseType in accept ? accept[options.responseType] : "*/*"), crossOrigin || headers["X-Requested-With"] || (headers["X-Requested-With"] = "XMLHttpRequest"), "GET" == method && data && (vars += data), options.cache || (vars && (vars += "&"), vars += "__t=" + +new Date), vars && (url += (/\?/.test(url) ? "&" : "?") + vars), options.async && promise.send(), promise
};
return {
base: "",
get: function(e, t, n, o) {
return qwest("GET", this.base + e, t, n, o)
},
post: function(e, t, n, o) {
return qwest("POST", this.base + e, t, n, o)
},
put: function(e, t, n, o) {
return qwest("PUT", this.base + e, t, n, o)
},
"delete": function(e, t, n, o) {
return qwest("DELETE", this.base + e, t, n, o)
},
map: function(e, t, n, o, s) {
return qwest(e.toUpperCase(), this.base + t, n, o, s)
},
xhr2: xhr2,
limit: function(e) {
limit = e
},
setDefaultXdrResponseType: function(e) {
defaultXdrResponseType = e.toLowerCase()
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment