Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzarghili committed Mar 1, 2019
1 parent d9802c5 commit 88bcf74
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
# xorq
Minimal xhr javascript library

Minimalistic xhr javascript library `experimental`

**561B** minified version

## Usage

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

```javascript
<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);
</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>
```

You can use the code this way

```javascript
// get resource
xorq.get("http://example.com")
.then(
// success
(data, xhr) => console.log(data, xhr),
// error
(data, xhr) => console.error(data, xhr)
)
// post data
xorq.post("http://example.com", data)
.then(
// success
(data, xhr) => console.log(data, xhr),
// error
(data, xhr) => console.error(data, xhr)
)
// set global headers
xorq.headers = {"X-API-VERSION":"v1", "X-USER-ID":"@1"}
// per request headers
headers = {"X-USER-ID":"@2"}
xorq.post("http://example.com", data, headers)
.then(
// success
(data, xhr) => console.log(data),
// error
(data, xhr) => console.error(data, xhr)
)
// if but global and request are set from previous example, result:
{"X-API-VERSION":"v1", "X-USER-ID":"@2"} // request headers override global ones

// set timout
xorq.timeout = 400 // in miliseconds
```

## License

MIT
1 change: 1 addition & 0 deletions dist/xorq.min.js

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

69 changes: 69 additions & 0 deletions src/xorq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*MIT License
Copyright (c) 2019 mohamed ez-zarghili
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/

// Omited values for args are placeholders to ovoid using var
// No test are available yet for this code so use at your own risk
// I am aware that this still need optimization but is good enough for my use cases for now
((window, _prep, timeout) => {
window.xorq = {}
_prep = (url, method, xhr, fns) => {
xhr = new XMLHttpRequest();
xhr.open(method, url, true);
timeout = window.xorq.timeout;
timeout && (xhr.timeout = timeout);
xhr.onreadystatechange = xhr.then = (success, error, callback, data, respenseData) => {
if (success && success instanceof Function) {
fns = [, success, error];
}
if (fns && xhr.readyState == 4) {
callback = fns[0 | xhr.status / 2e2];
if (callback) {
respenseData = xhr.responseText
try {
data = JSON.parse(respenseData)
} catch (e) {
data = respenseData
}
callback(data, xhr)
}
return
}
error && error({
error: "error accured"
}, xhr);
}
return xhr
}
methods = ['GET', "POST"];
methods.map(method => {
window.xorq[method.toLowerCase()] = (url, data, headers = {}, xh = window.xorq.headers) => {
xhr = _prep(url, method);
xh || (xh = {})
headers = Object.assign({}, xh, headers);
Object.keys(headers).forEach(key => {
xhr.setRequestHeader(key, headers[key]);
});
xhr.send(data)
return xhr
}
});
})(window)

0 comments on commit 88bcf74

Please sign in to comment.