From cee8cf851df2bfc06f8a7bea3156ea9886078593 Mon Sep 17 00:00:00 2001 From: Mohamed Ez-zarghili Date: Fri, 1 Mar 2019 21:53:39 +0100 Subject: [PATCH] add new http methods, handeling some failure cases (canceled, failed..) --- README.md | 7 ++++--- dist/xorq.min.js | 2 +- src/xorq.js | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c19b1a0..4f5d090 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Minimalistic xhr javascript library `experimental` -**561B** minified version +**603B** minified version ## Usage @@ -10,20 +10,21 @@ The best optimal approach is to add the minified inline to avoid extra http call ```html ``` Or you can use the dist file ```html - ``` You can use the code this way ```javascript +// available methods GET, POST, PUT, PATCH, DELETE // get resource xorq.get("http://example.com") .then( diff --git a/dist/xorq.min.js b/dist/xorq.min.js index c41abf9..67bb58f 100644 --- a/dist/xorq.min.js +++ b/dist/xorq.min.js @@ -1 +1 @@ -((e,t,r)=>{e.xorq={},t=((t,o,s,a)=>((s=new XMLHttpRequest).open(o,t,!0),(r=e.xorq.timeout)&&(s.timeout=r),s.onreadystatechange=s.then=((e,t,r,o,n)=>{if(e&&e instanceof Function&&(a=[,e,t]),a&&4==s.readyState){if(r=a[0|s.status/200]){n=s.responseText;try{o=JSON.parse(n)}catch(e){o=n}r(o,s)}}else t&&t({error:"error accured"},s)}),s)),methods=["GET","POST"],methods.map(r=>{e.xorq[r.toLowerCase()]=((o,s,a={},n=e.xorq.headers)=>(xhr=t(o,r),n||(n={}),a=Object.assign({},n,a),Object.keys(a).forEach(e=>{xhr.setRequestHeader(e,a[e])}),xhr.send(s),xhr))})})(window); \ No newline at end of file +((e,t,r)=>{e.xorq={},t=((t,o,s,a)=>((s=new XMLHttpRequest).open(o,t,!0),(r=e.xorq.timeout)&&(s.timeout=r),s.onreadystatechange=s.then=((e,t,r,o,n)=>{if(e&&e instanceof Function&&(a=[,e,t]),a&&4==s.readyState){if(r=a[0|s.status/200]){n=s.responseText;try{o=JSON.parse(n)}catch(e){o=n}return void r(o,s)}a[2]&&a[2]({error:"net/timout... error"},s)}}),s)),methods=["GET","POST","PUT","PATCH","DELETE"],methods.map(r=>{e.xorq[r.toLowerCase()]=((o,s,a={},n=e.xorq.headers)=>(xhr=t(o,r),n||(n={}),a=Object.assign({},n,a),Object.keys(a).forEach(e=>{xhr.setRequestHeader(e,a[e])}),xhr.send(s),xhr))})})(window); \ No newline at end of file diff --git a/src/xorq.js b/src/xorq.js index e94a76f..d14b629 100644 --- a/src/xorq.js +++ b/src/xorq.js @@ -1,4 +1,5 @@ -/*MIT License +/* +MIT License Copyright (c) 2019 mohamed ez-zarghili @@ -44,16 +45,16 @@ SOFTWARE.*/ data = respenseData } callback(data, xhr) + return } - return + fns[2] && fns[2]({ + error: "net/timout... error" + }, xhr) } - error && error({ - error: "error accured" - }, xhr); } return xhr } - methods = ['GET', "POST"]; + methods = ['GET', "POST", "PUT", "PATCH", "DELETE"]; methods.map(method => { window.xorq[method.toLowerCase()] = (url, data, headers = {}, xh = window.xorq.headers) => { xhr = _prep(url, method);