Skip to content

Commit

Permalink
Merge pull request #5 from nsonic001/feature/more-http-methods
Browse files Browse the repository at this point in the history
Added support for more HTTP verbs
  • Loading branch information
neilco authored Aug 5, 2017
2 parents 44cbf01 + b240d0b commit ccd7314
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ __<span style="color: rgb(180,0,0);">Twix</span>__ is a lightweight JavaScript l

It may weigh-in at just over 1KB, but this puppy has teeth. __WOOF!__

Modelled after the ubiquitious jQuery's AJAX methods (_$.ajax_, _$.get_, and _$.post_), you can use this in places were jQuery will not load, such as __inside a WebWorker__<sup>#</sup>. You little ripper!
Modelled after the ubiquitious jQuery's AJAX methods (_$.ajax_, _$.get_, _$.post_, _$.patch, _$.put_, _$.delete_ and _$.options_), you can use this in places were jQuery will not load, such as __inside a WebWorker__<sup>#</sup>. You little ripper!

You may be like me and occasionally think that loading the entire jQuery library (about 80KB minified) just to make an AJAX call is just like filling a thimble using a fire hydrant. __<span style="color: rgb(180,0,0);">Twix</span>__ is written from the ground up to work in modern browsers (IE9+, Firefox, Chrome, Safari, etc. Anything that supports __XMLHttpRequest__, really) and has just enough magic sauce to get the job done. No more, no less.

Expand Down
51 changes: 33 additions & 18 deletions twix.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var Twix = (function () {
client.timeout = options.timeout;
client.ontimeout = function () {
options.error('timeout', 'timeout', client);
}
};
}
client.open(options.type, options.url, options.async);

Expand All @@ -51,7 +51,7 @@ var Twix = (function () {

client.send(options.data);
client.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.readyState == 4 && ((this.status >= 200 && this.status < 300) || this.status == 304)) {
var data = this.responseText;
var contentType = this.getResponseHeader('Content-Type');
if (contentType && contentType.match(/json/)) {
Expand All @@ -64,7 +64,7 @@ var Twix = (function () {
};

if (options.async == false) {
if (client.readyState == 4 && client.status == 200) {
if (client.readyState == 4 && ((client.status >= 200 && client.status < 300) || client.status == 304)) {
options.success(client.responseText, client);
} else if (client.readyState == 4) {
options.error(client.status, client.statusText, client);
Expand All @@ -73,33 +73,48 @@ var Twix = (function () {

return client;
};
Twix.get = function(url, data, callback) {

var _ajax = function(type, url, data, callback) {
if (typeof data === "function") {
callback = data;
data = undefined;
}

return Twix.ajax({
url: url,
data: data,
type: type,
success: callback
});
};


Twix.get = function(url, data, callback) {
return _ajax("GET", url, data, callback);
};

Twix.head = function(url, data, callback) {
return _ajax("HEAD", url, data, callback);
};

Twix.post = function(url, data, callback) {
if (typeof data === "function") {
callback = data;
data = undefined;
}

return Twix.ajax({
url: url,
type: 'POST',
data: data,
success: callback
});
return _ajax("POST", url, data, callback);
};

Twix.patch = function(url, data, callback) {
return _ajax("PATCH", url, data, callback);
};

Twix.put = function(url, data, callback) {
return _ajax("PUT", url, data, callback);
};

Twix.delete = function(url, data, callback) {
return _ajax("DELETE", url, data, callback);
};

Twix.options = function(url, data, callback) {
return _ajax("OPTIONS", url, data, callback);
};


return Twix;
})();
Expand Down
2 changes: 1 addition & 1 deletion twix.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Twix=function(){function t(){}return t.ajax=function(t){t=t||{url:""},t.type=t.type.toUpperCase()||"GET",t.headers=t.headers||{},t.timeout=parseInt(t.timeout)||0,t.success=t.success||function(){},t.error=t.error||function(){},t.async="undefined"==typeof t.async?!0:t.async;var e=new XMLHttpRequest;t.timeout>0&&(e.timeout=t.timeout,e.ontimeout=function(){t.error("timeout","timeout",e)}),e.open(t.type,t.url,t.async);for(var s in t.headers)t.headers.hasOwnProperty(s)&&e.setRequestHeader(s,t.headers[s]);return e.send(t.data),e.onreadystatechange=function(){if(4==this.readyState&&200==this.status){var e=this.responseText;this.getResponseHeader("Content-Type").match(/json/)&&(e=JSON.parse(this.responseText)),t.success(e,this.statusText,this)}else 4==this.readyState&&t.error(this.status,this.statusText,this)},0==t.async&&(4==e.readyState&&200==e.status?t.success(e.responseText,e):4==e.readyState&&t.error(e.status,e.statusText,e)),e},t.get=function(e,s,r){return"function"==typeof s&&(r=s,s=void 0),t.ajax({url:e,data:s,success:r})},t.post=function(e,s,r){return"function"==typeof s&&(r=s,s=void 0),t.ajax({url:e,type:"POST",data:s,success:r})},t}();__=Twix;
var Twix=function(){function t(){}t.ajax=function(t){t=t||{url:""},t.type=t.type.toUpperCase()||"GET",t.headers=t.headers||{},t.timeout=parseInt(t.timeout)||0,t.success=t.success||function(){},t.error=t.error||function(){},t.async="undefined"==typeof t.async?!0:t.async;var e=new XMLHttpRequest;t.timeout>0&&(e.timeout=t.timeout,e.ontimeout=function(){t.error("timeout","timeout",e)}),e.open(t.type,t.url,t.async);for(var s in t.headers)t.headers.hasOwnProperty(s)&&e.setRequestHeader(s,t.headers[s]);return e.send(t.data),e.onreadystatechange=function(){if(4==this.readyState&&(this.status>=200&&this.status<300||304==this.status)){var e=this.responseText,s=this.getResponseHeader("Content-Type");s&&s.match(/json/)&&(e=JSON.parse(this.responseText)),t.success(e,this.statusText,this)}else 4==this.readyState&&t.error(this.status,this.statusText,this)},0==t.async&&(4==e.readyState&&(e.status>=200&&e.status<300||304==e.status)?t.success(e.responseText,e):4==e.readyState&&t.error(e.status,e.statusText,e)),e};var e=function(e,s,n,r){return"function"==typeof n&&(r=n,n=void 0),t.ajax({url:s,data:n,type:e,success:r})};return t.get=function(t,s,n){return e("GET",t,s,n)},t.head=function(t,s,n){return e("HEAD",t,s,n)},t.post=function(t,s,n){return e("POST",t,s,n)},t.patch=function(t,s,n){return e("PATCH",t,s,n)},t.put=function(t,s,n){return e("PUT",t,s,n)},t["delete"]=function(t,s,n){return e("DELETE",t,s,n)},t.options=function(t,s,n){return e("OPTIONS",t,s,n)},t}();__=Twix;

0 comments on commit ccd7314

Please sign in to comment.