Skip to content

Commit

Permalink
add new http methods, handeling some failure cases (canceled, failed..)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzarghili committed Mar 1, 2019
1 parent 055c185 commit cee8cf8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

Minimalistic xhr javascript library `experimental`

**561B** minified version
**603B** minified version

## Usage

The best optimal approach is to add the minified inline to avoid extra http calls

```html
<script>
((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);
((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);
</script>
```

Or you can use the dist file

```html
<script src="https://github.com/ezzarghili/xorq/releases/download/v1.0.0/xorq.min.js">
<script src="https://github.com/ezzarghili/xorq/releases/download/v1.1.0/xorq.min.js">
</script>
```

You can use the code this way

```javascript
// available methods GET, POST, PUT, PATCH, DELETE
// get resource
xorq.get("http://example.com")
.then(
Expand Down
2 changes: 1 addition & 1 deletion dist/xorq.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/xorq.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*MIT License
/*
MIT License
Copyright (c) 2019 mohamed ez-zarghili
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cee8cf8

Please sign in to comment.