vue-use-async
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@vue/composition-api"));else if("function"==typeof define&&define.amd)define(["@vue/composition-api"],t);else{var r="object"==typeof exports?t(require("@vue/composition-api")):t(e["@vue/composition-api"]);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(global,(function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([function(t,r){t.exports=e},function(e,t,r){e.exports=r(2)},function(e,t,r){"use strict";r.r(t),r.d(t,"cache",(function(){return i})),r.d(t,"clearCache",(function(){return u})),r.d(t,"cacheSize",(function(){return s})),r.d(t,"Deferred",(function(){return a})),r.d(t,"useAsync",(function(){return p})),r.d(t,"useXhr",(function(){return P})),r.d(t,"Xhr",(function(){return j})),r.d(t,"useSpinner",(function(){return S}));var n=function(e){0},o=new Map,i=function(e){var t,r,i=e||{},s=i.id,u=i.xhr,a="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(a=36e5),!o.get(s)&&u?(t=u(),o.set(s,t),t.then((function(e){return n("[Log] add cache: ".concat(s)),clearTimeout(r),r=setTimeout((function(){o.delete(s),n("[Log] delete cache: ".concat(s)),clearTimeout(r)}),a),e}),(function(){return o.delete(s),n("[Log] delete cache: ".concat(s))}))):s&&(t=o.get(s)),t)return t;throw Error("Call to undefined cache")};function s(){return o.size}function u(e){e?o.delete(e):o.forEach((function(e,t,r){r.delete(t)})),n("[Log] cache cleared")}var a=function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.resolve=void 0,this.reject=void 0,this.promise=void 0,this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r}))},c=r(0);function f(e){return function(e){if(Array.isArray(e))return l(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return l(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(r);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return l(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function h(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){return!0},n=Object(c.ref)(!0),o=Object(c.ref)(),i=Object(c.ref)(),s=Object(c.isRef)(t)?t:Object(c.ref)(t),u=[],l=Object(c.ref)(new a),p=function(t){if(r(t)){l.value=new a;var s=Array.isArray(t)?e.call.apply(e,[null].concat(f(t))):e(t);s.then((function(e){o.value=e,l.value.resolve(e)}),(function(e){i.value=e||null,u.forEach((function(e){return e(i.value)})),h.config.onError(e),l.value.reject(e)})),s.finally((function(){n.value=!1}))}},d=function(e){u.push(e)};return Object(c.watch)((function(){return s.value}),p),{isPending:n,data:o,error:i,reload:function(){return p(s.value)},onError:d,promise:Object(c.computed)((function(){return l.value.promise}))}}h.config={onError:function(e){}};var p=h;function d(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function v(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function y(e){return(y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function b(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function m(e,t,r){return t&&b(e.prototype,t),r&&b(e,r),e}var j=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.onError=function(){},this.onStart=function(){},this.onAbort=function(){},this.onProgress=function(){},this.onEnd=function(){},this.token=null,this.url="",this.params={},this.sendAs="json",this.timeout=1e4,this.port=80,this.responseType="text",this._eventsReady=void 0,this._oXHR=void 0,this._onEnd=void 0,this._deferred=void 0,this._isXhrResolved=void 0,this._isXhrRejected=void 0,this._eventReady=void 0,this._eventsReady=!1,this._constructor(t)}return m(e,[{key:"_onError",value:function(){var e;this.onError.apply(this,arguments),(e=this._deferred).reject.apply(e,arguments)}}],[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}}]),m(e,[{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e.onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("timeout",e.onError,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this.url,!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this.url,!0),this._send(),this._deferred.promise}},{key:"get",value:function(t){this._constructor(t),this._oXHR.open("GET",e.stringifyUrl(this.url,this.params),!0),this._send();var r=this._deferred.promise;return r.abortXhr=this.abort.bind(this),r}},{key:"delete",value:function(t){return this._constructor(t),this._oXHR.open("DELETE",e.stringifyUrl(this.url,this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({error:"Xhr aborted: ".concat(this.url),code:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this.onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1),e.addEventListener("timeout",this.onError,!1))}},{key:"_send",value:function(){var t;"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===y(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=t.onProgress||this.onProgress,this.onStart=t.onStart||this.onStart,this.onAbort=t.onAbort||this.onAbort,this.onEnd=t.onEnd||this.onEnd,this.onError=t.onError||this.onError,this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new a,this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e._injectParamsInUrl(this.url,this.params);this.url=o.url,this.params=o.params,this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],[{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e._injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return Object.keys(r).forEach((function(e){i+="".concat(o+e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==y(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_injectParamsInUrl",value:function(e,t){var r=function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?d(Object(r),!0).forEach((function(t){v(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):d(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},t);return((e=e.replace(/#/,"%23")).match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substr(1,t.length),void 0!==r[t]&&(e=e.replace(":".concat(t),null===r[t]||""===r[t]?"null":r[t]),delete r[t])})),{url:e,params:r}}}]),e}();function O(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function _(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?O(Object(r),!0).forEach((function(t){g(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):O(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function g(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function E(e){return(E="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function R(e){}var w=function(e){return Object(c.isRef)(e)?e.value:e},P=function(e){var t=e||{onError:function(e){return e},context:null,legacy:!1,token:null},r=t.onError,n=t.context,o=t.legacy,s=new j;Object(c.watch)((function(){return e?w(e.token):null}),(function(){s=new j;var t=e?w(e.token):null;t&&(s.token=t)}));var a=Object(c.ref)([]);return{get:function(t,f){var l,h=Object(c.ref)(!0),p=Object(c.ref)(),d=Object(c.ref)(),v=[],y="",b=0,m=(r||R).bind(n),O=Object(c.ref)(),g=function(){h.value&&s.abort(),h.value=!0;var r=function(){var r={};return"string"==typeof t?(y=t,r.url=y):t&&"object"===E(t)&&(y=t.url,f||(f=t.params||{}),b=t.cacheDuration,m=(t.onError||m).bind(n),r=_({},r,{},t)),e&&e.token&&(r.token=w(e.token)),f&&"object"===E(r)&&r.params&&(r.params=_({},r.params,{},Object(c.isRef)(f)?f.value||{}:f)),r}();l&&u(l),l=decodeURIComponent(j.stringifyUrl(y,"object"===E(r)?r.params:{})),O.value=i({id:l,xhr:s.get.bind(s,r),duration:b});var o=function(){var e=a.value.indexOf(s);e>-1&&a.value.splice(e,1)};O.value.then((function(e){o(),p.value=e}),(function(e){o(),d.value=e,v.forEach((function(e){return e(d.value)})),m(e)})),O.value.finally((function(){h.value=!1}))};return g(),o||Object(c.onBeforeUnmount)((function(){a.value.forEach((function(e){return e.abort()}))})),{isPending:h,data:p,onError:function(e){v.push(e)},error:d,abort:function(){return O.value.abortXhr()},promise:Object(c.computed)((function(){return O.value})),reload:g}},post:function(e,t){return p(s.post.bind(s,e),t)},put:function(e,t){return p(s.put.bind(s,e),t)},delete:function(e,t){return p(s.delete.bind(s,e),t)},abort:s.abort.bind(s),xhr:s}},S=function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=Object(c.ref)(!1);return Object(c.watch)((function(){return e.value}),(function(){n.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){n.value=e.value}),r)):n.value=e.value})),Object(c.computed)((function(){return n.value}))}}])})); | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@vue/composition-api"));else if("function"==typeof define&&define.amd)define(["@vue/composition-api"],t);else{var r="object"==typeof exports?t(require("@vue/composition-api")):t(e["@vue/composition-api"]);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(global,(function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([function(t,r){t.exports=e},function(e,t,r){e.exports=r(2)},function(e,t,r){"use strict";r.r(t),r.d(t,"cache",(function(){return i})),r.d(t,"clearCache",(function(){return u})),r.d(t,"cacheSize",(function(){return s})),r.d(t,"Deferred",(function(){return a})),r.d(t,"useAsync",(function(){return p})),r.d(t,"useXhr",(function(){return P})),r.d(t,"Xhr",(function(){return j})),r.d(t,"useSpinner",(function(){return S}));var n=function(e){0},o=new Map,i=function(e){var t,r,i=e||{},s=i.id,u=i.xhr,a="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(a=36e5),!o.get(s)&&u?(t=u(),o.set(s,t),t.then((function(e){return n("[Log] add cache: ".concat(s)),clearTimeout(r),r=setTimeout((function(){o.delete(s),n("[Log] delete cache: ".concat(s)),clearTimeout(r)}),a),e}),(function(){return o.delete(s),n("[Log] delete cache: ".concat(s))}))):s&&(t=o.get(s)),t)return t;throw Error("Call to undefined cache")};function s(){return o.size}function u(e){e?o.delete(e):o.forEach((function(e,t,r){r.delete(t)})),n("[Log] cache cleared")}var a=function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.resolve=void 0,this.reject=void 0,this.promise=void 0,this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r}))},c=r(0);function f(e){return function(e){if(Array.isArray(e))return l(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return l(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(r);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return l(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function h(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){return!0},n=Object(c.ref)(!0),o=Object(c.ref)(),i=Object(c.ref)(),s=Object(c.isRef)(t)?t:Object(c.ref)(t),u=[],l=Object(c.ref)(new a),p=function(t){if(r(t)){l.value=new a;var s=Array.isArray(t)?e.call.apply(e,[null].concat(f(t))):e(t);s.then((function(e){o.value=e,l.value.resolve(e)}),(function(e){i.value=e||null,u.forEach((function(e){return e(i.value)})),h.config.onError(e),l.value.reject(e)})),s.finally((function(){n.value=!1}))}},d=function(e){u.push(e)};return Object(c.watch)((function(){return s.value}),p),{isPending:n,data:o,error:i,reload:function(){return p(s.value)},onError:d,promise:Object(c.computed)((function(){return l.value.promise}))}}h.config={onError:function(e){}};var p=h;function d(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function v(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function y(e){return(y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function b(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function m(e,t,r){return t&&b(e.prototype,t),r&&b(e,r),e}var j=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.onError=function(){},this.onStart=function(){},this.onAbort=function(){},this.onProgress=function(){},this.onEnd=function(){},this.token=null,this.url="",this.params={},this.sendAs="json",this.timeout=1e4,this.port=80,this.responseType="text",this._eventsReady=void 0,this._oXHR=void 0,this._onEnd=void 0,this._deferred=void 0,this._isXhrResolved=void 0,this._isXhrRejected=void 0,this._eventReady=void 0,this._eventsReady=!1,this._constructor(t)}return m(e,[{key:"_onError",value:function(){var e;this.onError.apply(this,arguments),(e=this._deferred).reject.apply(e,arguments)}}],[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}}]),m(e,[{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e.onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("timeout",e.onError,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this.url,!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this.url,!0),this._send(),this._deferred.promise}},{key:"get",value:function(t){this._constructor(t),this._oXHR.open("GET",e.stringifyUrl(this.url,this.params),!0),this._send();var r=this._deferred.promise;return r.abortXhr=this.abort.bind(this),r}},{key:"delete",value:function(t){return this._constructor(t),this._oXHR.open("DELETE",e.stringifyUrl(this.url,this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({error:"Xhr aborted: ".concat(this.url),code:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this.onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1),e.addEventListener("timeout",this.onError,!1))}},{key:"_send",value:function(){var t;"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===y(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=t.onProgress||this.onProgress,this.onStart=t.onStart||this.onStart,this.onAbort=t.onAbort||this.onAbort,this.onEnd=t.onEnd||this.onEnd,this.onError=t.onError||this.onError,this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new a,this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e._injectParamsInUrl(this.url,this.params);this.url=o.url,this.params=o.params,this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],[{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e._injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return Object.keys(r).forEach((function(e){i+="".concat(o+e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==y(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_injectParamsInUrl",value:function(e,t){var r=function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?d(Object(r),!0).forEach((function(t){v(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):d(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},t);return((e=e.replace(/#/,"%23")).match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substr(1,t.length),void 0!==r[t]&&(e=e.replace(":".concat(t),null===r[t]||""===r[t]?"null":r[t]),delete r[t])})),{url:e,params:r}}}]),e}();function O(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function _(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?O(Object(r),!0).forEach((function(t){g(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):O(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function g(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function E(e){return(E="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function R(e){}var w=function(e){return Object(c.isRef)(e)?e.value:e},P=function(e){var t=e||{onError:function(e){return e},context:null,legacy:!1,token:null},r=t.onError,n=t.context,o=t.legacy,s=new j;Object(c.watch)((function(){return e?w(e.token):null}),(function(){s=new j;var t=e?w(e.token):null;t&&(s.token=t)}));var a=Object(c.ref)([]);return{get:function(t,f){var l,h=Object(c.ref)(),p=Object(c.ref)(),d=Object(c.ref)(),v=[],y="",b=0,m=(r||R).bind(n),O=Object(c.ref)(),g=function(){h.value&&s.abort(),h.value=!0;var r=function(){var r={};return"string"==typeof t?(y=t,r.url=y):t&&"object"===E(t)&&(y=t.url,f||(f=t.params||{}),b=t.cacheDuration,m=(t.onError||m).bind(n),r=_({},r,{},t)),e&&e.token&&(r.token=w(e.token)),f&&"object"===E(r)&&r.params&&(r.params=_({},r.params,{},Object(c.isRef)(f)?f.value||{}:f)),r}();l&&u(l),l=decodeURIComponent(j.stringifyUrl(y,"object"===E(r)?r.params:{})),O.value=i({id:l,xhr:s.get.bind(s,r),duration:b});var o=function(){var e=a.value.indexOf(s);e>-1&&a.value.splice(e,1)};O.value.then((function(e){o(),p.value=e}),(function(e){o(),d.value=e,v.forEach((function(e){return e(d.value)})),m(e)})),O.value.finally((function(){h.value=!1}))};return g(),o||Object(c.onBeforeUnmount)((function(){a.value.forEach((function(e){return e.abort()}))})),{isPending:h,data:p,onError:function(e){v.push(e)},error:d,abort:function(){return O.value.abortXhr()},promise:Object(c.computed)((function(){return O.value})),reload:g}},post:function(e,t){return p(s.post.bind(s,e),t)},put:function(e,t){return p(s.put.bind(s,e),t)},delete:function(e,t){return p(s.delete.bind(s,e),t)},abort:s.abort.bind(s),xhr:s}},S=function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=Object(c.ref)(!1);return Object(c.watch)((function(){return e.value}),(function(){n.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){n.value=e.value}),r)):n.value=e.value})),Object(c.computed)((function(){return n.value}))}}])})); |
{ | ||
"name": "vue-use-async", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Xhr and async helpers", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -13,8 +13,7 @@ # Xhr helpers to use in client (VueJS) projects | ||
# Usage | ||
```javascript | ||
import { Xhr, useXhr, useAsync } from 'vue-use-async'; | ||
``` | ||
- Xhr Class | ||
```javascript | ||
import { Xhr } from 'vue-use-async'; | ||
new Xhr().get({ url: '/' }); | ||
@@ -25,8 +24,16 @@ ``` | ||
```javascript | ||
const { get } = useXhr({ token }); | ||
const { | ||
data, | ||
isPending, | ||
promise, | ||
} = get({ url: '/', cacheDuration: 200 }); | ||
import { useXhr } from 'vue-use-async'; | ||
export default function () { | ||
const { get } = useXhr({ token }); | ||
const { | ||
data, | ||
isPending, | ||
promise, | ||
} = get({ url: '/', cacheDuration: 200 }); | ||
// ... | ||
} | ||
``` | ||
@@ -41,7 +48,28 @@ E.g; In `setup`, a computed bearer token can be used. Each query has data bind to be used | ||
```javascript | ||
const func = () => Promise.resolve('ok'); | ||
const { data } = useAsync(func, [params, condition]); | ||
import { useAsync } from 'vue-use-async'; | ||
export default function () { | ||
const func = () => Promise.resolve('ok'); | ||
const { data } = useAsync(func, [params, condition]); | ||
// ... | ||
} | ||
``` | ||
- useSpinner, useFull to bind the `isPending` to a spinner icon. A minimum duration | ||
```javascript | ||
import { useAsync, useSpinner } from 'vue-use-async'; | ||
export default function () { | ||
const func = () => Promise.resolve('ok'); | ||
const { data, isPending } = useAsync(func, [params, condition]); | ||
const isPendingSpinner = useSpinner(isPending); | ||
// ... | ||
} | ||
``` | ||
# FlowJS | ||
FlowJS is used as static types. `index.js.flow` import all definitions. |
23902
2.43%73
62.22%