vue-use-async 1.8.4 → 1.8.5
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.js +1 -1
- package/dist/useAsync.d.ts +3 -3
- package/dist/useXhr.d.ts +27 -27
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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:()=>d,TF:()=>A,PP:()=>i,LH:()=>a,Ej:()=>u,IL:()=>s,Yb:()=>g,n_:()=>E,bz:()=>_,p6:()=>D,cU:()=>N});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 f(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,h(n.key),n)}}function l(e,t,r){return t&&f(e.prototype,t),r&&f(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function v(e,t,r){return(t=h(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function h(e){var t=function(e,t){if("object"!=c(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=c(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==c(t)?t:String(t)}var d=l((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,watchEffect:()=>e.watchEffect},m={},t.d(m,y),m);var y,m;function b(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}const g=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),f=(0,p.computed)((function(){return"function"==typeof t?t():(0,p.unref)(t)})),l=(0,p.ref)(),v=(0,p.computed)((function(){return"function"==typeof r?r():(0,p.unref)(r)})),h=function(t){a.forEach((function(e){return e(f.value)})),n.value=!0,i.value=null;var r,l=e,v=e;return c.value=Array.isArray(t)?v.apply(void 0,function(e){if(Array.isArray(e))return b(e)}(r=t)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(r)||function(e,t){if(e){if("string"==typeof e)return b(e,t);var r=Object.prototype.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)?b(e,t):void 0}}(r)||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.")}()):l(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,f.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,f.value)}))})),c.value.finally((function(){n.value=!1})),c.value},d=(0,p.computed)((function(){return c.value}));return(0,p.watch)((function(){return v.value}),(function(e){!n.value&&e&&h(f.value)}),{immediate:!1}),(0,p.watch)((function(){return i.value}),(function(e){if(e)throw e})),(0,p.watch)((function(){return f.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===l.value||v.value&&t!==JSON.stringify(l.value))&&h(e),l.value=void 0===t?void 0:JSON.parse(t)}),{immediate:v.value,deep:!0}),{isPending:n,data:o,error:i,reload:function(){return h(f.value)},onError:function(e){u.push(e)},onStart:function(e){a.push(e)},onEnd:function(e){s.push(e)},promise:d}};function E(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),f=1;f<s;f++)c[f-1]=arguments[f];t.value=!0,n.value=null;var l=e;return u.value=l.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 _(e,t,r){var n=(0,p.ref)(t);return r||(r=function(e){return e}),void 0!==t&&(n.value=r(t)),(0,p.watchEffect)((function(){if(e){var t,o=(0,p.unref)(e);void 0!==o&&(n.value=null===(t=r)||void 0===t?void 0:t(o))}})),n}function j(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 w(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 O(e,t);var r=Object.prototype.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)?O(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 O(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 S(e){return S="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},S(e)}function P(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,k(n.key),n)}}function R(e,t,r){return(t=k(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function k(e){var t=function(e,t){if("object"!=S(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=S(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==S(t)?t:String(t)}var A=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),R(this,"onError",(function(){})),R(this,"onStart",(function(){})),R(this,"onAbort",(function(){})),R(this,"onProgress",(function(){})),R(this,"onEnd",(function(){})),R(this,"token",null),R(this,"url",""),R(this,"params",{}),R(this,"sendAs","json"),R(this,"timeout",1e4),R(this,"port",80),R(this,"responseType","text"),R(this,"isPending",!1),R(this,"_eventsReady",void 0),R(this,"_oXHR",void 0),R(this,"_onEnd",(function(){})),R(this,"_onError",(function(){})),R(this,"_deferred",void 0),R(this,"_isXhrResolved",void 0),R(this,"_isXhrRejected",void 0),R(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"===S(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 d,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=w(t.split("="),2),n=r[0],u=r[1],a=w((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"===S(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"!==S(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(S(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?j(Object(r),!0).forEach((function(t){R(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):j(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&&P(t.prototype,r),n&&P(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,r,n}();function X(e){return X="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},X(e)}function H(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 L(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?H(Object(r),!0).forEach((function(t){var n,o,i,u;n=e,o=t,i=r[t],u=function(e,t){if("object"!=X(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=X(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(o),(o="symbol"==X(u)?u:String(u))in n?Object.defineProperty(n,o,{value:i,enumerable:!0,configurable:!0,writable:!0}):n[o]=i})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):H(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}R(A,"onBeforeSendList",[]);var T=function(){},U=function(e){return(0,p.unref)(e)};function N(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,f=(0,p.ref)([]),l=[],v=function(e,t,r){var n,o=(0,p.computed)((function(){var e={},n=(0,p.unref)(t||{});r||(r=n.params||{}),e.params=(0,p.unref)(r),c&&(e.token=U(c));var o=(0,p.unref)(e.params||r||{});return"function"==typeof o&&(o=o()),e.url=(0,p.unref)(n.url),e})),i=new A,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 L(L({},g((function(){return n(L(L({},o.value),{},{params:u}))}))),{},{xhr:i})};return a||(0,p.onBeforeUnmount)((function(){f.value.forEach((function(e){e.isPending&&e.abort()})),l.forEach((function(e){return e()}))})),{get:function(e,t,a){var s=new A,v=(r||T).bind(u),h=(n||T).bind(u),d=(o||T).bind(u),y=[v],m=[h],b=[d],g=(0,p.ref)(null);f.value.push(s);var E=(0,p.ref)(),_=(0,p.ref)(),j="",w=0,O=(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||{}),w=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=w),c&&(n.token=U(c));var i=(0,p.unref)(n.params||t||{});return"function"==typeof i&&(i=i()),n.params=L(L({},i),(0,p.isRef)(t)?t.value||{}:t),j="function"==typeof r?r(n.params):(0,p.unref)(r),n.url=j,n})),S=function(){var e=void 0!==O.value.enabled?O.value.enabled:a;return void 0===e||((0,p.isRef)(e)?(0,p.unref)(e):"function"==typeof e?e():!0===e)},P=(0,p.ref)(),R=function(){if(S()){E.value&&s.abort();var e="object"===X(O.value.params)?O.value.params:{};m.forEach((function(t){return t(e,s)})),E.value=!0,g.value=null;var t=decodeURIComponent(A.stringifyUrl(String(j),e));P.value=i({id:t,xhr:s.get.bind(s,O.value),duration:w}),P.value.catch((function(e){y.forEach((function(t){return t(e,s)})),g.value=e})),P.value.then((function(e){_.value=e})),P.value.finally((function(){var t;(t=f.value.indexOf(s))>-1&&f.value.splice(t,1),E.value=!1,b.forEach((function(t){return t(_.value,e,s)}))}))}};return l.push((0,p.watch)((function(){return O.value}),R,{immediate:S(),deep:!0})),l.push((0,p.watch)((function(){return g.value}),(function(e){if(e)throw e}))),{isPending:(0,p.computed)((function(){return E.value})),data:_,onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return b.push(e)},error:g,abort:function(){var e;return null===(e=P.value)||void 0===e?void 0:e.abortXhr()},promise:(0,p.computed)((function(){return P.value||new Promise((function(){}))})),reload:R,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 D(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 I=r.cY,x=r.TF,C=r.PP,B=r.LH,F=r.Ej,J=r.IL,z=r.Yb,M=r.n_,Y=r.bz,q=r.p6,$=r.cU;export{I as Deferred,x as Xhr,C as cache,B as cacheIds,F as cacheSize,J as clearCache,z as useAsync,M as useMutation,Y as useResult,q as useSpinner,$ as useXhr};
|
|
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:()=>d,TF:()=>A,PP:()=>i,LH:()=>a,Ej:()=>u,IL:()=>s,Yb:()=>g,n_:()=>E,bz:()=>_,p6:()=>I,cU:()=>D});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 f(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,h(n.key),n)}}function l(e,t,r){return t&&f(e.prototype,t),r&&f(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function v(e,t,r){return(t=h(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function h(e){var t=function(e,t){if("object"!=c(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=c(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==c(t)?t:t+""}var d=l((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=(m={computed:()=>e.computed,isRef:()=>e.isRef,onBeforeUnmount:()=>e.onBeforeUnmount,ref:()=>e.ref,unref:()=>e.unref,watch:()=>e.watch,watchEffect:()=>e.watchEffect},y={},t.d(y,m),y);var m,y;function b(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 g=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),f=(0,p.computed)((function(){return"function"==typeof t?t():(0,p.unref)(t)})),l=(0,p.ref)(),v=(0,p.computed)((function(){return"function"==typeof r?r():(0,p.unref)(r)})),h=function(t){if(!v.value)return null;a.forEach((function(e){return e(f.value)})),n.value=!0,i.value=null;var r,l=e,h=e;return c.value=Array.isArray(t)?h.apply(void 0,function(e){if(Array.isArray(e))return b(e)}(r=t)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(r)||function(e,t){if(e){if("string"==typeof e)return b(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)?b(e,t):void 0}}(r)||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.")}()):l(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,f.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,f.value)}))})),c.value.finally((function(){n.value=!1})),c.value},d=(0,p.computed)((function(){return c.value}));return(0,p.watch)((function(){return v.value}),(function(e){!n.value&&e&&h(f.value)}),{immediate:!1}),(0,p.watch)((function(){return i.value}),(function(e){if(e)throw e})),(0,p.watch)((function(){return f.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===l.value||v.value&&t!==JSON.stringify(l.value))&&h(e),l.value=void 0===t?void 0:JSON.parse(t)}),{immediate:v.value,deep:!0}),{isPending:n,data:o,error:i,reload:function(){return h(f.value)},onError:function(e){u.push(e)},onStart:function(e){a.push(e)},onEnd:function(e){s.push(e)},promise:d}};function E(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),f=1;f<s;f++)c[f-1]=arguments[f];t.value=!0,n.value=null;var l=e;return u.value=l.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 _(e,t,r){var n=(0,p.ref)(t);return r||(r=function(e){return e}),void 0!==t&&(n.value=r(t)),(0,p.watchEffect)((function(){if(e){var t,o=(0,p.unref)(e);void 0!==o&&(n.value=null===(t=r)||void 0===t?void 0:t(o))}})),n}function j(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 w(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 O(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)?O(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 O(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 P(e){return P="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},P(e)}function S(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,k(n.key),n)}}function R(e,t,r){return(t=k(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function k(e){var t=function(e,t){if("object"!=P(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=P(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==P(t)?t:t+""}var A=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),R(this,"onError",(function(){})),R(this,"onStart",(function(){})),R(this,"onAbort",(function(){})),R(this,"onProgress",(function(){})),R(this,"onEnd",(function(){})),R(this,"token",null),R(this,"url",""),R(this,"params",{}),R(this,"sendAs","json"),R(this,"timeout",1e4),R(this,"port",80),R(this,"responseType","text"),R(this,"isPending",!1),R(this,"_eventsReady",void 0),R(this,"_oXHR",void 0),R(this,"_onEnd",(function(){})),R(this,"_onError",(function(){})),R(this,"_deferred",void 0),R(this,"_isXhrResolved",void 0),R(this,"_isXhrRejected",void 0),R(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"===P(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 d,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=w(t.split("="),2),n=r[0],u=r[1],a=w((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"===P(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"!==P(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(P(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?j(Object(r),!0).forEach((function(t){R(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):j(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&&S(t.prototype,r),n&&S(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,r,n}();function X(e){return X="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},X(e)}function H(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 L(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?H(Object(r),!0).forEach((function(t){T(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):H(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function T(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=X(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=X(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==X(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}R(A,"onBeforeSendList",[]);var U=function(){},N=function(e){return(0,p.unref)(e)};function D(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,f=(0,p.ref)([]),l=[],v=function(e,t,r){var n,o=(0,p.computed)((function(){var e={},n=(0,p.unref)(t||{});r||(r=n.params||{}),e.params=(0,p.unref)(r),c&&(e.token=N(c));var o=(0,p.unref)(e.params||r||{});return"function"==typeof o&&(o=o()),e.url=(0,p.unref)(n.url),e})),i=new A,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 L(L({},g((function(){return n(L(L({},o.value),{},{params:u}))}))),{},{xhr:i})};return a||(0,p.onBeforeUnmount)((function(){f.value.forEach((function(e){e.isPending&&e.abort()})),l.forEach((function(e){return e()}))})),{get:function(e,t,a){var s=new A,v=(r||U).bind(u),h=(n||U).bind(u),d=(o||U).bind(u),m=[v],y=[h],b=[d],g=(0,p.ref)(null);f.value.push(s);var E=(0,p.ref)(),_=(0,p.ref)(),j="",w=0,O=(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||{}),w=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=w),c&&(n.token=N(c));var i=(0,p.unref)(n.params||t||{});return"function"==typeof i&&(i=i()),n.params=L(L({},i),(0,p.isRef)(t)?t.value||{}:t),j="function"==typeof r?r(n.params):(0,p.unref)(r),n.url=j,n})),P=function(){var e=void 0!==O.value.enabled?O.value.enabled:a;return void 0===e||((0,p.isRef)(e)?(0,p.unref)(e):"function"==typeof e?e():!0===e)},S=(0,p.ref)(),R=function(){if(P()){E.value&&s.abort();var e="object"===X(O.value.params)?O.value.params:{};y.forEach((function(t){return t(e,s)})),E.value=!0,g.value=null;var t=decodeURIComponent(A.stringifyUrl(String(j),e));S.value=i({id:t,xhr:s.get.bind(s,O.value),duration:w}),S.value.catch((function(e){m.forEach((function(t){return t(e,s)})),g.value=e})),S.value.then((function(e){_.value=e})),S.value.finally((function(){var t;(t=f.value.indexOf(s))>-1&&f.value.splice(t,1),E.value=!1,b.forEach((function(t){return t(_.value,e,s)}))}))}};return l.push((0,p.watch)((function(){return O.value}),R,{immediate:P(),deep:!0})),l.push((0,p.watch)((function(){return g.value}),(function(e){if(e)throw e}))),{isPending:(0,p.computed)((function(){return E.value})),data:_,onError:function(e){return m.push(e)},onStart:function(e){return y.push(e)},onEnd:function(e){return b.push(e)},error:g,abort:function(){var e;return null===(e=S.value)||void 0===e?void 0:e.abortXhr()},promise:(0,p.computed)((function(){return S.value||new Promise((function(){}))})),reload:R,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 I(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 x=r.cY,C=r.TF,B=r.PP,F=r.LH,J=r.Ej,z=r.IL,M=r.Yb,Y=r.n_,q=r.bz,$=r.p6,Q=r.cU;export{x as Deferred,C as Xhr,B as cache,F as cacheIds,J as cacheSize,z as clearCache,M as useAsync,Y as useMutation,q as useResult,$ as useSpinner,Q as useXhr};
|
package/dist/useAsync.d.ts
CHANGED
|
@@ -8,12 +8,12 @@ type Params<Z, A extends unknown[]> = (() => (Z | [...A])) | ComputedRef<Z | [..
|
|
|
8
8
|
];
|
|
9
9
|
declare const useAsync: <T, Z extends TypeAllowed, A extends TypeAllowed[]>(func: ((...args: A) => Promise<T>) | ((args: Z) => Promise<T>), params?: Params<Z, A>, enabled?: Ref<boolean> | (() => boolean)) => {
|
|
10
10
|
isPending: Ref<undefined | boolean>;
|
|
11
|
-
data: Ref<
|
|
11
|
+
data: Ref<undefined | null | UnwrappedPromiseType<typeof func>>;
|
|
12
12
|
error: Ref<null | Error>;
|
|
13
13
|
reload: () => unknown;
|
|
14
14
|
onError: (cb: OnErrorCb<Params<Z, A>>) => unknown;
|
|
15
15
|
onStart: (cb: OnStartCb<Params<Z, A>>) => unknown;
|
|
16
|
-
onEnd: (cb: OnEndCb<
|
|
17
|
-
promise: ComputedRef<
|
|
16
|
+
onEnd: (cb: OnEndCb<UnwrappedPromiseType<typeof func>, Params<Z, A>>) => unknown;
|
|
17
|
+
promise: ComputedRef<null | ReturnType<typeof func>>;
|
|
18
18
|
};
|
|
19
19
|
export default useAsync;
|
package/dist/useXhr.d.ts
CHANGED
|
@@ -19,38 +19,38 @@ declare type UseXhr<T, Z extends TypeAllowed> = Partial<{
|
|
|
19
19
|
}>;
|
|
20
20
|
export default function <T, Z extends TypeAllowed>(args?: UseXhr<T, Z>): {
|
|
21
21
|
get: <TT = T, ZZ = Z>(parametersObj: GetConfig<ZZ>, params?: Params<ZZ>, enabled?: Enabled) => GetReturn<TT>;
|
|
22
|
-
post: <
|
|
23
|
-
xhr: Xhr<
|
|
24
|
-
isPending: Ref<
|
|
25
|
-
data: Ref<
|
|
26
|
-
error: Ref<
|
|
22
|
+
post: <TT = T, ZZ = Z>(xhrConfig?: $UpdateConfigArgs, params?: Params<ZZ>) => {
|
|
23
|
+
xhr: Xhr<TT>;
|
|
24
|
+
isPending: Ref<undefined | boolean>;
|
|
25
|
+
data: Ref<TT | null | undefined>;
|
|
26
|
+
error: Ref<null | Error>;
|
|
27
27
|
reload: () => unknown;
|
|
28
|
-
onError: (cb: (e:
|
|
29
|
-
onStart: (cb: (params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() =>
|
|
30
|
-
onEnd: (cb: (res:
|
|
31
|
-
promise: ComputedRef<Promise<
|
|
28
|
+
onError: (cb: (e: null | Error, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
29
|
+
onStart: (cb: (params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
30
|
+
onEnd: (cb: (res: TT, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
31
|
+
promise: ComputedRef<Promise<TT> | null>;
|
|
32
32
|
};
|
|
33
|
-
put: <
|
|
34
|
-
xhr: Xhr<
|
|
35
|
-
isPending: Ref<
|
|
36
|
-
data: Ref<
|
|
37
|
-
error: Ref<
|
|
33
|
+
put: <TT = T, ZZ = Z>(xhrConfig?: $UpdateConfigArgs, params?: Params<ZZ>) => {
|
|
34
|
+
xhr: Xhr<TT>;
|
|
35
|
+
isPending: Ref<undefined | boolean>;
|
|
36
|
+
data: Ref<TT | null | undefined>;
|
|
37
|
+
error: Ref<null | Error>;
|
|
38
38
|
reload: () => unknown;
|
|
39
|
-
onError: (cb: (e:
|
|
40
|
-
onStart: (cb: (params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() =>
|
|
41
|
-
onEnd: (cb: (res:
|
|
42
|
-
promise: ComputedRef<Promise<
|
|
39
|
+
onError: (cb: (e: null | Error, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
40
|
+
onStart: (cb: (params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
41
|
+
onEnd: (cb: (res: TT, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
42
|
+
promise: ComputedRef<Promise<TT> | null>;
|
|
43
43
|
};
|
|
44
|
-
delete: <
|
|
45
|
-
xhr: Xhr<
|
|
46
|
-
isPending: Ref<
|
|
47
|
-
data: Ref<
|
|
48
|
-
error: Ref<
|
|
44
|
+
delete: <TT = T, ZZ = Z>(xhrConfig?: $UpdateConfigArgs, params?: Params<ZZ>) => {
|
|
45
|
+
xhr: Xhr<TT>;
|
|
46
|
+
isPending: Ref<undefined | boolean>;
|
|
47
|
+
data: Ref<TT | null | undefined>;
|
|
48
|
+
error: Ref<null | Error>;
|
|
49
49
|
reload: () => unknown;
|
|
50
|
-
onError: (cb: (e:
|
|
51
|
-
onStart: (cb: (params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() =>
|
|
52
|
-
onEnd: (cb: (res:
|
|
53
|
-
promise: ComputedRef<Promise<
|
|
50
|
+
onError: (cb: (e: null | Error, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
51
|
+
onStart: (cb: (params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
52
|
+
onEnd: (cb: (res: TT, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => (TypeAllowed | [...[]]))) => unknown) => unknown;
|
|
53
|
+
promise: ComputedRef<Promise<TT> | null>;
|
|
54
54
|
};
|
|
55
55
|
};
|
|
56
56
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-use-async",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.5",
|
|
4
4
|
"description": "Xhr and async helpers",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"author": "Thomas CASTELLY",
|
|
11
|
+
"type": "module",
|
|
11
12
|
"main": "dist/index.js",
|
|
12
13
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"type": "module",
|
|
14
14
|
"scripts": {
|
|
15
15
|
"afterbuild": "node ./scripts/afterbuild.cjs",
|
|
16
16
|
"beforebuild": "node ./scripts/beforeBuild.cjs && npm run tsc:export-types",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"tsc:export-types": "tsc --outDir types"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"vue": "^3.4.
|
|
30
|
+
"vue": "^3.4.31"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
34
|
-
"@babel/preset-env": "^7.24.
|
|
35
|
-
"@babel/preset-typescript": "^7.24.
|
|
34
|
+
"@babel/preset-env": "^7.24.7",
|
|
35
|
+
"@babel/preset-typescript": "^7.24.7",
|
|
36
36
|
"@types/jest": "^29.5.12",
|
|
37
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
38
|
-
"@typescript-eslint/parser": "^7.
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^7.15.0",
|
|
38
|
+
"@typescript-eslint/parser": "^7.15.0",
|
|
39
39
|
"babel-eslint": "^10.1.0",
|
|
40
40
|
"babel-jest": "^29.7.0",
|
|
41
41
|
"babel-loader": "^9.1.3",
|
|
@@ -47,10 +47,10 @@
|
|
|
47
47
|
"husky": "^9.0.11",
|
|
48
48
|
"jest": "^29.7.0",
|
|
49
49
|
"jest-environment-jsdom": "^29.7.0",
|
|
50
|
-
"lint-staged": "^15.2.
|
|
51
|
-
"typescript": "^5.
|
|
52
|
-
"vue": "^3.4.
|
|
53
|
-
"webpack": "^5.
|
|
50
|
+
"lint-staged": "^15.2.7",
|
|
51
|
+
"typescript": "^5.5.3",
|
|
52
|
+
"vue": "^3.4.31",
|
|
53
|
+
"webpack": "^5.92.1",
|
|
54
54
|
"webpack-cli": "^5.1.4"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|