diff --git a/README.md b/README.md index c46c58f..7d194a5 100644 --- a/README.md +++ b/README.md @@ -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 + +``` + +Or you can use the dist file + +```html + +``` + +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 diff --git a/dist/xorq.min.js b/dist/xorq.min.js new file mode 100644 index 0000000..c41abf9 --- /dev/null +++ b/dist/xorq.min.js @@ -0,0 +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 diff --git a/src/xorq.js b/src/xorq.js new file mode 100644 index 0000000..e94a76f --- /dev/null +++ b/src/xorq.js @@ -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) \ No newline at end of file