vue-use-async 1.9.0 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -23,7 +23,9 @@ export type XhrConfig = Partial<{
23
23
  port?: number | null;
24
24
  params?: Obj;
25
25
  onStart?: (e: ProgressEvent) => void;
26
- onEnd?: (e: ProgressEvent) => void;
26
+ onEnd?: (result: unknown, e: ProgressEvent & {
27
+ currentTarget: XMLHttpRequest;
28
+ }) => void;
27
29
  onProgress?: (e: ProgressEvent) => void;
28
30
  onAbort?: (e: ProgressEvent) => void;
29
31
  onError?: (e: ProgressEvent) => void;
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- import*as e from"vue";var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},r={};t.d(r,{cY:()=>h,TF:()=>D,PP:()=>i,LH:()=>a,Ej:()=>u,IL:()=>s,Yb:()=>R,n_:()=>k,bz:()=>S,p6:()=>M,cU:()=>z});const n={log:function(e){},warn:function(e){},info:function(e){},error:function(e){},debug:function(e){}};var o=new Map;function i(e){var t,r,i=e||{},u=i.id,a=i.xhr,s="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(s=36e5),!o.get(u)&&a?(t=a(),o.set(u,t),t.then((function(e){return n.log("[Log] add cache: ".concat(u)),clearTimeout(r),r=window.setTimeout((function(){o.delete(u),n.log("[Log] delete cache: ".concat(u)),clearTimeout(r)}),s),e}),(function(){return o.delete(u),n.log("[Log] delete cache: ".concat(u))}))):u&&(t=o.get(u)),t)return t;throw Error("Call to undefined cache")}function u(){return o.size}function a(){return Array.from(o.keys())}function s(e){e?o.delete(e):o.forEach((function(e,t,r){r.delete(t)})),n.log("[Log] cache cleared")}function c(e){return c="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},c(e)}function l(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,d(n.key),n)}}function f(e,t,r){return t&&l(e.prototype,t),r&&l(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function v(e,t,r){return(t=d(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){var t=function(e){if("object"!=c(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=c(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==c(t)?t:t+""}var h=f((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),v(this,"resolve",void 0),v(this,"reject",void 0),v(this,"promise",void 0),v(this,"done",!1),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})),this.promise.finally((function(){t.done=!0}))}));const p=(y={computed:()=>e.computed,isRef:()=>e.isRef,onBeforeUnmount:()=>e.onBeforeUnmount,ref:()=>e.ref,unref:()=>e.unref,watch:()=>e.watch},m={},t.d(m,y),m);var y,m;const b="10000000-1000-4000-8000-100000000000".replace(/[018]/g,(function(e){return(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16)}));function g(e){return g="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},g(e)}function w(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,j(n.key),n)}}function E(e,t,r){return t&&w(e.prototype,t),r&&w(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _(e,t,r){return(t=j(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function j(e){var t=function(e){if("object"!=g(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=g(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==g(t)?t:t+""}var O=E((function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),_(this,"value",void 0),_(this,"uuid",void 0),this.uuid=b,this.value=t}));function S(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(e){return e},n=void 0===e.value?t:e.value;return e.value=void 0===n?n:new O(null==r?void 0:r(n)),(0,p.watch)((function(){return e.value}),(function(t){void 0===t||t instanceof O||(e.value=new O(null==r?void 0:r(t)))})),(0,p.computed)({get:function(){var r;return void 0===(r=e.value instanceof O?e.value.value:e.value)?t:r},set:function(t){e.value=new O(t)}})}function P(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}const R=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,p.ref)(!0),n=(0,p.ref)(),o=(0,p.ref)(),i=(0,p.ref)(null),u=[],a=[],s=[],c=(0,p.ref)(null),l=(0,p.computed)((function(){return"function"==typeof t?t():(0,p.unref)(t)})),f=(0,p.ref)(),v=(0,p.computed)((function(){return"function"==typeof r?r():(0,p.unref)(r)})),d=function(t){if(!v.value)return null;var r,f;n.value&&(null===(r=c.value)||void 0===r||null===(f=r.abortXhr)||void 0===f||f.call(r)),a.forEach((function(e){return e(l.value)})),n.value=!0,i.value=null;var d,h=e,p=e;return c.value=Array.isArray(t)?p.apply(void 0,function(e){if(Array.isArray(e))return P(e)}(d=t)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(d)||function(e,t){if(e){if("string"==typeof e)return P(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?P(e,t):void 0}}(d)||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.")}()):h(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,l.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,l.value)}))})),c.value.finally((function(){n.value=!1})),c.value},h=(0,p.computed)((function(){return c.value}));return(0,p.watch)((function(){return v.value}),(function(e){!n.value&&e&&d(l.value)}),{immediate:!1}),(0,p.watch)((function(){return i.value}),(function(e){if(e)throw e})),(0,p.watch)((function(){return l.value}),(function(e){var t=JSON.stringify(e);(void 0===e&&void 0===f.value||v.value&&t!==JSON.stringify(f.value))&&d(e),f.value=void 0===t?void 0:JSON.parse(t)}),{immediate:v.value,deep:!0}),{isPending:n,data:(0,p.computed)({get:function(){return o.value},set:function(e){e instanceof O&&e.uuid===b||console.warn('"useAsync" update a readonly field is not allowed'),o.value=e}}),error:i,reload:function(){return d(l.value)},onError:function(e){u.push(e)},onStart:function(e){a.push(e)},onEnd:function(e){s.push(e)},promise:h}};function k(e){var t=(0,p.ref)(),r=(0,p.ref)(),n=(0,p.ref)(),o=[],i=[],u=(0,p.ref)(null);return{mutate:function(a){for(var s=arguments.length,c=new Array(s>1?s-1:0),l=1;l<s;l++)c[l-1]=arguments[l];t.value=!0,n.value=null;var f=e;return u.value=f.apply(void 0,[a].concat(c)),u.value.catch((function(e){n.value=e||null,o.forEach((function(e){return e(n.value,c.length?[a].concat(c):a)})),n.value=e})),u.value.then((function(e){r.value=e,i.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)}))})),u.value.finally((function(){t.value=!1})),u.value},onError:function(e){return o.push(e)},onEnd:function(e){return i.push(e)},isPending:t,error:n,data:r,promise:(0,p.computed)((function(){return u.value}))}}function A(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 X(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,u,a=[],s=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;s=!1}else for(;!(s=(n=i.call(r)).done)&&(a.push(n.value),a.length!==t);s=!0);}catch(e){c=!0,o=e}finally{try{if(!s&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(e,t)||function(e,t){if(e){if("string"==typeof e)return T(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?T(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function T(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function H(e){return H="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},H(e)}function L(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(n.key),n)}}function U(e,t,r){return(t=N(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function N(e){var t=function(e){if("object"!=H(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=H(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==H(t)?t:t+""}var D=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),U(this,"onError",(function(){})),U(this,"onStart",(function(){})),U(this,"onAbort",(function(){})),U(this,"onProgress",(function(){})),U(this,"onEnd",(function(){})),U(this,"token",null),U(this,"url",""),U(this,"params",{}),U(this,"sendAs","json"),U(this,"timeout",1e4),U(this,"port",80),U(this,"responseType","text"),U(this,"isPending",!1),U(this,"_eventsReady",void 0),U(this,"_oXHR",void 0),U(this,"_onEnd",(function(){})),U(this,"_onError",(function(){})),U(this,"_deferred",void 0),U(this,"_isXhrResolved",void 0),U(this,"_isXhrRejected",void 0),U(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}return t=e,r=[{key:"_getUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,r)}},{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("timeout",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)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var t=this._deferred.promise;return t.abortXhr=this.abort.bind(this),t}},{key:"delete",value:function(e){return this._constructor(e),this._oXHR.open("DELETE",this._getUrl(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({message:"Xhr aborted: ".concat(this.url),name:"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("timeout",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))}},{key:"_send",value:function(){var t;this.isPending=!0,"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"===H(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).bind(this),this.onStart=(t.onStart||this.onStart).bind(this),this.onAbort=(t.onAbort||this.onAbort).bind(this),this.onEnd=(t.onEnd||this.onEnd).bind(this),this.onError=(t.onError||this.onError).bind(this),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 h,this._deferred.promise.finally((function(){n.isPending=!1})),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),i=o.url,u=o.params;this.url=i,this.params=u,this._onError=function(e){n.onError(e),n.removeEvents(),n._deferred.reject(e)},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)}}],n=[{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}},{key:"extractQueryParams",value:function(e){var t={},r=e.search(/#/);r>-1&&(e=e.substring(0,r));var n=e.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&e.split("?")[1].split("&").reduce((function(e,t){var r=X(t.split("="),2),n=r[0],u=r[1],a=X((void 0===u?"":u).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(a===o||a===i)e[n]=a===o;else{var s=Number(a);e[n]=""===a||Number.isNaN(s)?a:s}return e}),t),t}},{key:"onBeforeSend",value:function(t){e.onBeforeSendList.push(t)}},{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 r&&!Array.isArray(r)&&"object"===H(r)?Object.getOwnPropertyNames(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(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"!==H(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_stringifyForPathParam",value:function(e,t,r){var n=r[t];switch(H(r[t])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return e.replace(":".concat(t),n)}},{key:"injectParamsInUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:t,params:r};var n=decodeURIComponent(t),o=e.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var u=Object.getOwnPropertyNames(r).reduce((function(e,t){return e[t]=r[t],e}),function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?A(Object(r),!0).forEach((function(t){U(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):A(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substring(1),void 0!==u[t]&&(n=e._stringifyForPathParam(n,t,u),delete u[t])})),Object.keys(u).forEach((function(e){void 0!==o[e]&&(o[e]=u[e],delete u[e])}));var a=Object.keys(o);if(a.length){var s="?";a.forEach((function(e){if(!0===o[e]||!1===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(s).concat(e,"=").concat(r)}s="&"}))}return{url:n,params:u}}}],r&&L(t.prototype,r),n&&L(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,r,n}();function I(e){return I="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},I(e)}function C(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 x(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?C(Object(r),!0).forEach((function(t){B(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):C(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function B(e,t,r){return(t=function(e){var t=function(e){if("object"!=I(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=I(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==I(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}U(D,"onBeforeSendList",[]);var F=function(){},J=function(e){return(0,p.unref)(e)};function z(e){var t=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},r=t.onError,n=t.onStart,o=t.onEnd,u=t.context,a=t.legacy,s=t.token,c=void 0===s?null:s,l=(0,p.ref)([]),f=[],v=function(e,t,r){var n,o=(0,p.computed)((function(){var e={},n=(0,p.unref)(t||{});return r||(r=n.params||{}),e.params=(0,p.unref)(r),c&&(e.token=J(c)),e.url=(0,p.unref)(n.url),e})),i=new D,u=(0,p.unref)(o.value.params)||{};switch(e){case"post":default:n=i.post.bind(i);break;case"put":n=i.put.bind(i);break;case"delete":n=i.delete.bind(i)}return x(x({},R((function(){return n(x(x({},o.value),{},{params:u}))}))),{},{xhr:i})};return a||(0,p.onBeforeUnmount)((function(){l.value.forEach((function(e){e.isPending&&e.abort()})),f.forEach((function(e){return e()}))})),{get:function(e,t,a){var s=new D,v=(r||F).bind(u),d=(n||F).bind(u),h=(o||F).bind(u),y=[v],m=[d],g=[h],w=(0,p.ref)(null);l.value.push(s);var E=(0,p.ref)(),_=(0,p.ref)(),j="",S=0,P=(0,p.computed)((function(){var r,n={},o=(0,p.unref)(e);"string"==typeof o?r=o:(r=(0,p.unref)(o.url),t||(t=o.params||{}),S=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=a),n.params=(0,p.unref)(t),n.cacheDuration=S),c&&(n.token=J(c));var i=(0,p.unref)(n.params||t||{});return"function"==typeof i&&(i=i()),n.params=x(x({},i),(0,p.isRef)(t)?t.value||{}:t),j="function"==typeof r?r(n.params):(0,p.unref)(r),n.url=j,n})),R=function(){var e=void 0!==P.value.enabled?P.value.enabled:a;return void 0===e||((0,p.isRef)(e)?(0,p.unref)(e):"function"==typeof e?e():!0===e)},k=(0,p.ref)(),A=function(){if(R()){E.value&&s.abort();var e="object"===I(P.value.params)?P.value.params:{};m.forEach((function(t){return t(e,s)})),E.value=!0,w.value=null;var t=decodeURIComponent(D.stringifyUrl(String(j),e));k.value=i({id:t,xhr:s.get.bind(s,P.value),duration:S}),k.value.catch((function(e){y.forEach((function(t){return t(e,s)})),w.value=e})),k.value.then((function(e){_.value=e})),k.value.finally((function(){var t;(t=l.value.indexOf(s))>-1&&l.value.splice(t,1),E.value=!1,g.forEach((function(t){return t(_.value,e,s)}))}))}};return f.push((0,p.watch)((function(){return P.value}),A,{immediate:R(),deep:!0})),f.push((0,p.watch)((function(){return w.value}),(function(e){if(e)throw e}))),{isPending:(0,p.computed)((function(){return E.value})),data:(0,p.computed)({get:function(){return _.value},set:function(e){e instanceof O&&e.uuid===b||console.warn('"useXhr" update a readonly field is not allowed'),_.value=e}}),onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return g.push(e)},error:w,abort:function(){var e;return null===(e=k.value)||void 0===e?void 0:e.abortXhr()},promise:(0,p.computed)((function(){return k.value||new Promise((function(){}))})),reload:A,xhr:s}},post:function(e,t){return v("post",e,t)},put:function(e,t){return v("put",e,t)},delete:function(e,t){return v("delete",e,t)}}}function M(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,p.ref)(!1),o=0;return(0,p.watch)((function(){return e.value}),(function(e){if(e&&(o=Date.now(),n.value=e),!1===e&&o>0){var i=Date.now()-o;clearTimeout(t),i>r?n.value=e:t=setTimeout((function(){n.value=e}),r-i)}}),{immediate:!0}),(0,p.onBeforeUnmount)((function(){clearTimeout(t)})),(0,p.computed)((function(){return n.value}))}var Y=r.cY,q=r.TF,$=r.PP,Q=r.LH,G=r.Ej,V=r.IL,K=r.Yb,W=r.n_,Z=r.bz,ee=r.p6,te=r.cU;export{Y as Deferred,q as Xhr,$ as cache,Q as cacheIds,G as cacheSize,V as clearCache,K as useAsync,W as useMutation,Z as useResult,ee as useSpinner,te as useXhr};
1
+ /*! For license information please see index.js.LICENSE.txt */
2
+ import*as t from"vue";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},r={};e.d(r,{cY:()=>p,TF:()=>D,PP:()=>i,LH:()=>u,Ej:()=>a,IL:()=>c,Yb:()=>L,n_:()=>R,bz:()=>P,p6:()=>M,cU:()=>Y});const n={log:function(t){},warn:function(t){},info:function(t){},error:function(t){},debug:function(t){}};var o=new Map;function i(t){var e,r,i=t||{},a=i.id,u=i.xhr,c="string"!=typeof t.duration&&t.duration||200;if("max"===t.duration&&(c=36e5),!o.get(a)&&u?(e=u(),o.set(a,e),e.then((function(t){return n.log("[Log] add cache: ".concat(a)),clearTimeout(r),r=window.setTimeout((function(){o.delete(a),n.log("[Log] delete cache: ".concat(a)),clearTimeout(r)}),c),t}),(function(){return o.delete(a),n.log("[Log] delete cache: ".concat(a))}))):a&&(e=o.get(a)),e)return e;throw Error("Call to undefined cache")}function a(){return o.size}function u(){return Array.from(o.keys())}function c(t){t?o.delete(t):o.forEach((function(t,e,r){r.delete(e)})),n.log("[Log] cache cleared")}function s(t){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s(t)}function l(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,v(n.key),n)}}function f(t,e,r){return e&&l(t.prototype,e),r&&l(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function h(t,e,r){return(e=v(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function v(t){var e=function(t){if("object"!=s(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=s(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==s(e)?e:e+""}var p=f((function t(){var e=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),h(this,"resolve",void 0),h(this,"reject",void 0),h(this,"promise",void 0),h(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(t,r){e.resolve=t,e.reject=r})),this.promise.finally((function(){e.done=!0}))}));const d=(y={computed:()=>t.computed,isRef:()=>t.isRef,onBeforeUnmount:()=>t.onBeforeUnmount,ref:()=>t.ref,unref:()=>t.unref,watch:()=>t.watch},m={},e.d(m,y),m);var y,m;const b="10000000-1000-4000-8000-100000000000".replace(/[018]/g,(function(t){return(+t^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+t/4).toString(16)}));function g(t){return g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g(t)}function w(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,j(n.key),n)}}function E(t,e,r){return e&&w(t.prototype,e),r&&w(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function _(t,e,r){return(e=j(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function j(t){var e=function(t){if("object"!=g(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=g(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==g(e)?e:e+""}var O=E((function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),_(this,"value",void 0),_(this,"uuid",void 0),this.uuid=b,this.value=e}));function P(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(t){return t},n=void 0===t.value?e:t.value;return t.value=void 0===n?n:new O(null==r?void 0:r(n)),(0,d.watch)((function(){return t.value}),(function(e){void 0===e||e instanceof O||(t.value=new O(null==r?void 0:r(e)))})),(0,d.computed)({get:function(){var r;return void 0===(r=t.value instanceof O?t.value.value:t.value)?e:r},set:function(e){t.value=new O(e)}})}function S(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}const L=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,d.ref)(!0),n=(0,d.ref)(),o=(0,d.ref)(),i=(0,d.ref)(null),a=[],u=[],c=[],s=(0,d.ref)(null),l=(0,d.computed)((function(){return"function"==typeof e?e():(0,d.unref)(e)})),f=(0,d.ref)(),h=(0,d.computed)((function(){return"function"==typeof r?r():(0,d.unref)(r)})),v=function(e){if(!h.value)return null;var r,f;n.value&&(null===(r=s.value)||void 0===r||null===(f=r.abortXhr)||void 0===f||f.call(r)),u.forEach((function(t){return t(l.value)})),n.value=!0,i.value=null;var v,p=t,d=t;return s.value=Array.isArray(e)?d.apply(void 0,function(t){if(Array.isArray(t))return S(t)}(v=e)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(v)||function(t,e){if(t){if("string"==typeof t)return S(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?S(t,e):void 0}}(v)||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.")}()):p(e),s.value.catch((function(t){i.value=t||null,a.forEach((function(t){return t(i.value,l.value)})),i.value=t})),s.value.then((function(t){o.value=t,c.forEach((function(e){return e(t,l.value)}))})),s.value.finally((function(){n.value=!1})),s.value},p=(0,d.computed)((function(){return s.value}));return(0,d.watch)((function(){return h.value}),(function(t){!n.value&&t&&v(l.value)}),{immediate:!1}),(0,d.watch)((function(){return i.value}),(function(t){if(t)throw t})),(0,d.watch)((function(){return l.value}),(function(t){var e=JSON.stringify(t);(void 0===t&&void 0===f.value||h.value&&e!==JSON.stringify(f.value))&&v(t),f.value=void 0===e?void 0:JSON.parse(e)}),{immediate:h.value,deep:!0}),{isPending:n,data:(0,d.computed)({get:function(){return o.value},set:function(t){t instanceof O&&t.uuid===b||console.warn('"useAsync" update a readonly field is not allowed'),o.value=t}}),error:i,reload:function(){return v(l.value)},onError:function(t){a.push(t)},onStart:function(t){u.push(t)},onEnd:function(t){c.push(t)},promise:p}};function R(t){var e=(0,d.ref)(),r=(0,d.ref)(),n=(0,d.ref)(),o=[],i=[],a=(0,d.ref)(null);return{mutate:function(u){for(var c=arguments.length,s=new Array(c>1?c-1:0),l=1;l<c;l++)s[l-1]=arguments[l];e.value=!0,n.value=null;var f=t;return a.value=f.apply(void 0,[u].concat(s)),a.value.catch((function(t){n.value=t||null,o.forEach((function(t){return t(n.value,s.length?[u].concat(s):u)})),n.value=t})),a.value.then((function(t){r.value=t,i.forEach((function(t){return t(r.value,s.length?[u].concat(s):u)}))})),a.value.finally((function(){e.value=!1})),a.value},onError:function(t){return o.push(t)},onEnd:function(t){return i.push(t)},isPending:e,error:n,data:r,promise:(0,d.computed)((function(){return a.value}))}}function k(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function x(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,u=[],c=!0,s=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=i.call(r)).done)&&(u.push(n.value),u.length!==e);c=!0);}catch(t){s=!0,o=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(s)throw o}}return u}}(t,e)||function(t,e){if(t){if("string"==typeof t)return A(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?A(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function A(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function X(){X=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",u=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function l(t,e,r,n){var i=e&&e.prototype instanceof m?e:m,a=Object.create(i.prototype),u=new x(n||[]);return o(a,"_invoke",{value:S(t,r,u)}),a}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=l;var h="suspendedStart",v="suspendedYield",p="executing",d="completed",y={};function m(){}function b(){}function g(){}var w={};s(w,a,(function(){return this}));var E=Object.getPrototypeOf,_=E&&E(E(A([])));_&&_!==r&&n.call(_,a)&&(w=_);var j=g.prototype=m.prototype=Object.create(w);function O(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function P(t,e){function r(o,i,a,u){var c=f(t[o],t,i);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==H(l)&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,a,u)}),(function(t){r("throw",t,a,u)})):e.resolve(l).then((function(t){s.value=t,a(s)}),(function(t){return r("throw",t,a,u)}))}u(c.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e((function(e,o){r(t,n,e,o)}))}return i=i?i.then(o,o):o()}})}function S(e,r,n){var o=h;return function(i,a){if(o===p)throw Error("Generator is already running");if(o===d){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var c=L(u,n);if(c){if(c===y)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=p;var s=f(e,r,n);if("normal"===s.type){if(o=n.done?d:v,s.arg===y)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=d,n.method="throw",n.arg=s.arg)}}}function L(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,L(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=f(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function R(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function x(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(R,this),this.reset(!0)}function A(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(H(e)+" is not iterable")}return b.prototype=g,o(j,"constructor",{value:g,configurable:!0}),o(g,"constructor",{value:b,configurable:!0}),b.displayName=s(g,c,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===b||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,s(t,c,"GeneratorFunction")),t.prototype=Object.create(j),t},e.awrap=function(t){return{__await:t}},O(P.prototype),s(P.prototype,u,(function(){return this})),e.AsyncIterator=P,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new P(l(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},O(j),s(j,c,"Generator"),s(j,a,(function(){return this})),s(j,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=A,x.prototype={constructor:x,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(k),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function o(n,o){return u.type="throw",u.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],u=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var c=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(c&&s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),k(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:A(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},e}function T(t,e,r,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function H(t){return H="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},H(t)}function N(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,I(n.key),n)}}function U(t,e,r){return(e=I(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function I(t){var e=function(t){if("object"!=H(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=H(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==H(e)?e:e+""}var D=function(){function t(e,r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),U(this,"onError",(function(){})),U(this,"onStart",(function(){})),U(this,"onAbort",(function(){})),U(this,"onProgress",(function(){})),U(this,"onEnd",(function(){})),U(this,"token",null),U(this,"url",""),U(this,"params",{}),U(this,"sendAs","json"),U(this,"timeout",1e4),U(this,"port",80),U(this,"responseType","text"),U(this,"isPending",!1),U(this,"_eventsReady",void 0),U(this,"_oXHR",void 0),U(this,"_onEnd",(function(){})),U(this,"_onError",(function(){})),U(this,"_deferred",void 0),U(this,"_isXhrResolved",void 0),U(this,"_isXhrRejected",void 0),U(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(e||{},r)}return e=t,r=[{key:"_getUrl",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.onBeforeSendList.reduce((function(t,e){return e(t||{})}),e);return t.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var t=this,e=function(){t._oXHR.removeEventListener("load",t._onEnd,!1),t._oXHR.removeEventListener("error",t._onError,!1),t._oXHR.removeEventListener("timeout",t._onError,!1),t._oXHR.removeEventListener("loadstart",t.onStart,!1),t._oXHR.removeEventListener("abort",t.onAbort,!1),t._oXHR.upload.removeEventListener("progress",t.onProgress,!1),t._oXHR.removeEventListener("progress",t.onProgress,!1)};this._deferred.promise.then(e,e)}},{key:"post",value:function(t){return this._constructor(t),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(t){return this._constructor(t),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(t){this._constructor(t||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var e=this._deferred.promise;return e.abortXhr=this.abort.bind(this),e}},{key:"delete",value:function(t){return this._constructor(t),this._oXHR.open("DELETE",this._getUrl(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({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(t){return this._deferred.resolve(t),this._deferred.promise}},{key:"reject",value:function(t){return this._deferred.reject(t),this._deferred.promise}},{key:"_setEvents",value:function(t){this._eventsReady||(this._eventReady=!0,t.addEventListener("load",this._onEnd,!1),t.addEventListener("error",this._onError,!1),t.addEventListener("timeout",this._onError,!1),t.addEventListener("loadstart",this.onStart,!1),t.addEventListener("abort",this.onAbort,!1),t.upload.addEventListener("progress",this.onProgress,!1),t.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var e;this.isPending=!0,"multipart"===this.sendAs?e=t.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),e=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(e,this)}},{key:"_constructor",value:function(e,r){var n=this;e&&"object"===H(e)?(this.sendAs=e.sendAs||this.sendAs,this.url=e.url||this.url,this.port=e.port||this.port,this.params=e.params||this.params,this.timeout=e.timeout||this.timeout,this.responseType=e.responseType||this.responseType,this.onProgress=(e.onProgress||this.onProgress).bind(this),this.onStart=(e.onStart||this.onStart).bind(this),this.onAbort=(e.onAbort||this.onAbort).bind(this),this.onEnd=(e.onEnd||this.onEnd).bind(this),this.onError=(e.onError||this.onError).bind(this),this.token=e.token||this.token):e&&"string"==typeof e&&(this.url=e,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 p,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=t.injectParamsInUrl(this.url,this.params),i=o.url,a=o.params;this.url=i,this.params=a,this._onError=function(t){n.onError(t),n.removeEvents(),n._deferred.reject(t)},this._onEnd=function(){var e,r=(e=X().mark((function e(r){var o,i;return X().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o=t.parseResult(n._oXHR),!(n._oXHR.status>=400)){e.next=9;break}return e.next=4,n._oXHR.response.text();case 4:i=e.sent;try{i=JSON.parse(i)}catch(t){}return n._isXhrRejected=!0,n._onError(i),e.abrupt("return",n._deferred.reject(i));case 9:return n.onEnd(o,r),n._isXhrResolved=!0,n.removeEvents(),e.abrupt("return",n.resolve(o));case 13:case"end":return e.stop()}}),e)})),function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(t){T(i,n,o,a,u,"next",t)}function u(t){T(i,n,o,a,u,"throw",t)}a(void 0)}))});return function(t){return r.apply(this,arguments)}}(),this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(t){var e=t.response;try{var r=t.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(e=JSON.parse(t.response))}catch(r){e=t.response}return e}},{key:"extractQueryParams",value:function(t){var e={},r=t.search(/#/);r>-1&&(t=t.substring(0,r));var n=t.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&t.split("?")[1].split("&").reduce((function(t,e){var r=x(e.split("="),2),n=r[0],a=r[1],u=x((void 0===a?"":a).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(u===o||u===i)t[n]=u===o;else{var c=Number(u);t[n]=""===u||Number.isNaN(c)?u:c}return t}),e),e}},{key:"onBeforeSend",value:function(e){t.onBeforeSendList.push(e)}},{key:"new",value:function(e){return new t(e)}},{key:"stringifyUrl",value:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.injectParamsInUrl(e,r);e=n.url,r=n.params;var o=e.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===H(r)?Object.getOwnPropertyNames(r).filter((function(t){return void 0!==r[t]})).forEach((function(t){i+="".concat(o).concat(t,"=").concat(encodeURIComponent(JSON.stringify(r[t]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(e=e.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(t){var e,r=new FormData;return Object.keys(t).forEach((function(n){if(t[n]instanceof FileList)for(var o=0;o<t[n].length;o+=1)r.append(n,t[n][o]);else t[n]instanceof File?r.append(n,t[n]):("object"!==H(e=t[n])&&!Array.isArray(e)||null===e||(e=JSON.stringify(e)),r.append(n,e))})),r}},{key:"_stringifyForPathParam",value:function(t,e,r){var n=r[e];switch(H(r[e])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return t.replace(":".concat(e),n)}},{key:"injectParamsInUrl",value:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:e,params:r};var n=decodeURIComponent(e),o=t.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var a=Object.getOwnPropertyNames(r).reduce((function(t,e){return t[e]=r[e],t}),function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?k(Object(r),!0).forEach((function(e){U(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):k(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(e){e=e.substring(1),void 0!==a[e]&&(n=t._stringifyForPathParam(n,e,a),delete a[e])})),Object.keys(a).forEach((function(t){void 0!==o[t]&&(o[t]=a[t],delete a[t])}));var u=Object.keys(o);if(u.length){var c="?";u.forEach((function(t){if(!0===o[t]||!1===o[t])n+="".concat(c).concat(t,"=").concat(o[t]);else if(null===o[t])n+="".concat(c).concat(t,"=").concat(o[t]);else{var e=Number(o[t]),r=Number.isNaN(e)?encodeURIComponent('"'.concat(o[t],'"')):e;n+="".concat(c).concat(t,"=").concat(r)}c="&"}))}return{url:n,params:a}}}],r&&N(e.prototype,r),n&&N(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,r,n}();function C(t){return C="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},C(t)}function F(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function B(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?F(Object(r),!0).forEach((function(e){J(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):F(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function J(t,e,r){return(e=function(t){var e=function(t){if("object"!=C(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=C(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==C(e)?e:e+""}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}U(D,"onBeforeSendList",[]);var G=function(){},z=function(t){return(0,d.unref)(t)};function Y(t){var e=t||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},r=e.onError,n=e.onStart,o=e.onEnd,a=e.context,u=e.legacy,c=e.token,s=void 0===c?null:c,l=(0,d.ref)([]),f=[],h=function(t,e,r){var n,o=(0,d.computed)((function(){var t={},n=(0,d.unref)(e||{});return r||(r=n.params||{}),t.params=(0,d.unref)(r),s&&(t.token=z(s)),t.url=(0,d.unref)(n.url),t})),i=new D,a=(0,d.unref)(o.value.params)||{};switch(t){case"post":default:n=i.post.bind(i);break;case"put":n=i.put.bind(i);break;case"delete":n=i.delete.bind(i)}return B(B({},L((function(){return n(B(B({},o.value),{},{params:a}))}))),{},{xhr:i})};return u||(0,d.onBeforeUnmount)((function(){l.value.forEach((function(t){t.isPending&&t.abort()})),f.forEach((function(t){return t()}))})),{get:function(t,e,u){var c=new D,h=(r||G).bind(a),v=(n||G).bind(a),p=(o||G).bind(a),y=[h],m=[v],g=[p],w=(0,d.ref)(null);l.value.push(c);var E=(0,d.ref)(),_=(0,d.ref)(),j="",P=0,S=(0,d.computed)((function(){var r,n={},o=(0,d.unref)(t);"string"==typeof o?r=o:(r=(0,d.unref)(o.url),e||(e=o.params||{}),P=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=u),n.params=(0,d.unref)(e),n.cacheDuration=P),s&&(n.token=z(s));var i=(0,d.unref)(n.params||e||{});return"function"==typeof i&&(i=i()),n.params=B(B({},i),(0,d.isRef)(e)?e.value||{}:e),j="function"==typeof r?r(n.params):(0,d.unref)(r),n.url=j,n})),L=function(){var t=void 0!==S.value.enabled?S.value.enabled:u;return void 0===t||((0,d.isRef)(t)?(0,d.unref)(t):"function"==typeof t?t():!0===t)},R=(0,d.ref)(),k=function(){if(L()){E.value&&c.abort();var t="object"===C(S.value.params)?S.value.params:{};m.forEach((function(e){return e(t,c)})),E.value=!0,w.value=null;var e=decodeURIComponent(D.stringifyUrl(String(j),t));R.value=i({id:e,xhr:c.get.bind(c,S.value),duration:P}),R.value.catch((function(t){y.forEach((function(e){return e(t,c)})),w.value=t})),R.value.then((function(t){_.value=t})),R.value.finally((function(){var e;(e=l.value.indexOf(c))>-1&&l.value.splice(e,1),E.value=!1,g.forEach((function(e){return e(_.value,t,c)}))}))}};return f.push((0,d.watch)((function(){return S.value}),k,{immediate:L(),deep:!0})),f.push((0,d.watch)((function(){return w.value}),(function(t){if(t)throw t}))),{isPending:(0,d.computed)((function(){return E.value})),data:(0,d.computed)({get:function(){return _.value},set:function(t){t instanceof O&&t.uuid===b||console.warn('"useXhr" update a readonly field is not allowed'),_.value=t}}),onError:function(t){return y.push(t)},onStart:function(t){return m.push(t)},onEnd:function(t){return g.push(t)},error:w,abort:function(){var t;return null===(t=R.value)||void 0===t?void 0:t.abortXhr()},promise:(0,d.computed)((function(){return R.value||new Promise((function(){}))})),reload:k,xhr:c}},post:function(t,e){return h("post",t,e)},put:function(t,e){return h("put",t,e)},delete:function(t,e){return h("delete",t,e)}}}function M(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,d.ref)(!1),o=0;return(0,d.watch)((function(){return t.value}),(function(t){if(t&&(o=Date.now(),n.value=t),!1===t&&o>0){var i=Date.now()-o;clearTimeout(e),i>r?n.value=t:e=setTimeout((function(){n.value=t}),r-i)}}),{immediate:!0}),(0,d.onBeforeUnmount)((function(){clearTimeout(e)})),(0,d.computed)((function(){return n.value}))}var q=r.cY,$=r.TF,Q=r.PP,V=r.LH,K=r.Ej,W=r.IL,Z=r.Yb,tt=r.n_,et=r.bz,rt=r.p6,nt=r.cU;export{q as Deferred,$ as Xhr,Q as cache,V as cacheIds,K as cacheSize,W as clearCache,Z as useAsync,tt as useMutation,et as useResult,rt as useSpinner,nt as useXhr};
@@ -0,0 +1 @@
1
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-use-async",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Xhr and async helpers",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,33 +27,33 @@
27
27
  "tsc:export-types": "tsc --outDir types"
28
28
  },
29
29
  "dependencies": {
30
- "vue": "^3.2.45"
30
+ "vue": "^3.5.12"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@babel/plugin-proposal-class-properties": "^7.18.6",
34
- "@babel/preset-env": "^7.25.8",
35
- "@babel/preset-typescript": "^7.25.7",
34
+ "@babel/preset-env": "^7.26.0",
35
+ "@babel/preset-typescript": "^7.26.0",
36
36
  "@eslint/eslintrc": "^3.1.0",
37
- "@types/jest": "^29.5.13",
38
- "@typescript-eslint/eslint-plugin": "^8.8.1",
39
- "@typescript-eslint/parser": "^8.8.1",
37
+ "@types/jest": "^29.5.14",
38
+ "@typescript-eslint/eslint-plugin": "^8.13.0",
39
+ "@typescript-eslint/parser": "^8.13.0",
40
40
  "babel-eslint": "^10.1.0",
41
41
  "babel-jest": "^29.7.0",
42
42
  "babel-loader": "^9.2.1",
43
43
  "babel-plugin-module-resolver": "^5.0.2",
44
- "eslint": "^9.12.0",
44
+ "eslint": "^9.14.0",
45
45
  "eslint-config-airbnb-base": "^15.0.0",
46
46
  "eslint-import-resolver-typescript": "^3.6.3",
47
47
  "eslint-plugin-import": "^2.31.0",
48
- "eslint-plugin-jest": "^28.8.3",
49
- "globals": "^15.11.0",
48
+ "eslint-plugin-jest": "^28.9.0",
49
+ "globals": "^15.12.0",
50
50
  "husky": "^9.1.6",
51
51
  "jest": "^29.7.0",
52
52
  "jest-environment-jsdom": "^29.7.0",
53
53
  "lint-staged": "^15.2.10",
54
54
  "typescript": "^5.6.3",
55
- "vue": "^3.5.11",
56
- "webpack": "^5.95.0",
55
+ "vue": "^3.5.12",
56
+ "webpack": "^5.96.1",
57
57
  "webpack-cli": "^5.1.4"
58
58
  },
59
59
  "peerDependencies": {