vue-use-async 1.1.19 → 1.1.20

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/cache.d.ts CHANGED
@@ -10,4 +10,5 @@ export default function <T>(params: {
10
10
  duration?: number | 'max' | null;
11
11
  }): XhrGet<T>;
12
12
  export declare function cacheSize(): number;
13
+ export declare function cacheIds(): string[];
13
14
  export declare function clearCache(id?: string): void;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("vue"));else if("function"==typeof define&&define.amd)define(["vue"],t);else{var r="object"==typeof exports?t(require("vue")):t(e.vue);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(function(e){return(()=>{"use strict";var t={748:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var i=r[e]={exports:{}};return t[e](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{Deferred:()=>c,Xhr:()=>g,cache:()=>t,cacheSize:()=>r,clearCache:()=>i,useAsync:()=>d,useMutation:()=>h,useResult:()=>p,useSpinner:()=>P,useXhr:()=>O});var e=new Map;function t(t){var r,n,o=t||{},i=o.id,u=o.xhr,a="string"!=typeof t.duration&&t.duration||200;if("max"===t.duration&&(a=36e5),!e.get(i)&&u?(r=u(),e.set(i,r),r.then((function(t){return"[Log] add cache: ".concat(i),clearTimeout(n),n=window.setTimeout((function(){e.delete(i),"[Log] delete cache: ".concat(i),clearTimeout(n)}),a),t}),(function(){return e.delete(i),void"[Log] delete cache: ".concat(i)}))):i&&(r=e.get(i)),r)return r;throw Error("Call to undefined cache")}function r(){return e.size}function i(t){t?e.delete(t):e.forEach((function(e,t,r){r.delete(t)}))}function u(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function a(e,t,r){return t&&u(e.prototype,t),r&&u(e,r),e}function s(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var c=a((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),s(this,"resolve",void 0),s(this,"reject",void 0),s(this,"promise",void 0),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r}))})),l=n(748);function f(e){return function(e){if(Array.isArray(e))return v(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return v(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)?v(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function v(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 d(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,l.ref)(!0),n=(0,l.ref)(),o=(0,l.ref)(),i=(0,l.ref)(null),u=[],a=[],s=[],c=(0,l.ref)(null),v=(0,l.computed)((function(){return"function"==typeof t?t():(0,l.unref)(t)})),d=(0,l.ref)(),h=(0,l.computed)((function(){return"function"==typeof r?r():(0,l.unref)(r)})),p=function(t){a.forEach((function(e){return e(v.value)})),n.value=!0,i.value=null;var r=e,l=e;return c.value=Array.isArray(t)?l.apply(void 0,f(t)):r(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,v.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,v.value)}))})),c.value.finally((function(){n.value=!1})),c.value};return(0,l.watch)((function(){return h.value}),(function(e){!n.value&&e&&p(v.value)}),{immediate:!1}),(0,l.watch)((function(){return v.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===d.value||h.value&&t!==JSON.stringify(d.value))&&p(e),d.value=void 0===t?void 0:JSON.parse(t)}),{immediate:h.value,deep:!0}),{isPending:n,data:o,error:i,reload:function(){return p(v.value)},onError:function(e){return u.push(e)},onStart:function(e){return a.push(e)},onEnd:function(e){return s.push(e)},promise:(0,l.computed)((function(){return c.value}))}}function h(e){var t=(0,l.ref)(),r=(0,l.ref)(),n=(0,l.ref)(),o=[],i=[],u=(0,l.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.then((function(e){r.value=e,i.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)}))}),(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.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,l.computed)((function(){return u.value}))}}function p(e,t,r){var n=(0,l.ref)(t);return r||(r=function(e){return e}),t&&(n.value=r(t)),(0,l.watchEffect)((function(){if(e){var t,o=(0,l.unref)(e);o&&(n.value=null===(t=r)||void 0===t?void 0:t(o))}})),n}function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function m(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function b(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var g=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),b(this,"onError",(function(){})),b(this,"onStart",(function(){})),b(this,"onAbort",(function(){})),b(this,"onProgress",(function(){})),b(this,"onEnd",(function(){})),b(this,"token",null),b(this,"url",""),b(this,"params",{}),b(this,"sendAs","json"),b(this,"timeout",1e4),b(this,"port",80),b(this,"responseType","text"),b(this,"isPending",!1),b(this,"_eventsReady",void 0),b(this,"_oXHR",void 0),b(this,"_onEnd",void 0),b(this,"_deferred",void 0),b(this,"_isXhrResolved",void 0),b(this,"_isXhrRejected",void 0),b(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}var t,r,n;return t=e,r=[{key:"_onError",value:function(e){this.onError(e),this._deferred.reject(e)}},{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("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("timeout",e.onError,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._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({error:"Xhr aborted: ".concat(this.url),code:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this.onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1),e.addEventListener("timeout",this.onError,!1))}},{key:"_send",value:function(){var t;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"===y(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).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 c,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);this.url=o.url,this.params=o.params,this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],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:"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 Object.keys(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o+e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==y(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_injectParamsInUrl",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(t))return{url:e,params:t};var r=Object.getOwnPropertyNames(t).filter((function(e){return!e.match(/__[a-zA-Z0-9_]+__/gi)})).reduce((function(e,r){return e[r]=t[r],e}),{});return((e=e.replace(/#/,"%23")).match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substr(1,t.length),void 0!==r[t]&&(e=e.replace(":".concat(t),null===r[t]||""===r[t]?"null":r[t]),delete r[t])})),{url:e,params:r}}}],r&&m(t.prototype,r),n&&m(t,n),e}();function _(e){return _="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_(e)}function E(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 j(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?E(Object(r),!0).forEach((function(t){R(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):E(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function R(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}b(g,"onBeforeSendList",[]);var w=function(){},S=function(e){return(0,l.isRef)(e)?e.value:e};function O(e){var r=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},n=r.onError,o=r.onStart,u=r.onEnd,a=r.context,s=r.legacy,c=r.token,f=void 0===c?null:c,v=function(e){var t=S(f);return t?((0,l.isRef)(e)?e.value.token=t:e.token=t,e):e},h=(0,l.ref)([]);return s||(0,l.onBeforeUnmount)((function(){h.value.forEach((function(e){e.isPending&&e.abort()}))})),{get:function(e,r,s){var c=new g,v=(n||w).bind(a),d=(o||w).bind(a),p=(u||w).bind(a),y=[v],m=[d],b=[p],E=(0,l.ref)(null);h.value.push(c);var R,O,P=(0,l.ref)(),X=(0,l.ref)(),k="",A=0,H=(0,l.computed)((function(){var t,n={},o=(0,l.unref)(e);"string"==typeof o?t=o:(t=o.url,r||(r=o.params||{}),A=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=s),n.params=r,n.cacheDuration=A),f&&(n.token=S(f));var i=(0,l.unref)(n.params||r||{});return"function"==typeof i&&(i=i()),n.params=j(j({},i),(0,l.isRef)(r)?r.value||{}:r),k="function"==typeof t?t(n.params):(0,l.unref)(t),n.url=k,n})),L=void 0!==H.value.enabled?H.value.enabled:s;R=void 0===L?(0,l.ref)(!0):(0,l.isRef)(L)?L:"function"==typeof L?(0,l.ref)(L()):(0,l.ref)(!0===L);var T=(0,l.ref)(),x=function(){P.value&&c.abort();var e="object"===_(H.value.params)?H.value.params:{};m.forEach((function(t){return t(e,c)})),P.value=!0,E.value=null,O&&i(O),O=decodeURIComponent(g.stringifyUrl(String(k),e)),T.value=t({id:O,xhr:c.get.bind(c,H.value),duration:A}),T.value.then((function(e){X.value=e}),(function(e){y.forEach((function(t){return t(e,c)})),E.value=e})),T.value.finally((function(){var t;(t=h.value.indexOf(c))>-1&&h.value.splice(t,1),P.value=!1,b.forEach((function(t){return t(X.value,e,c)}))}))};return(0,l.watch)((function(){return H.value}),(function(){R.value&&x()}),{immediate:R.value}),(0,l.watch)((function(){return R.value}),(function(e){!P.value&&e&&x()}),{immediate:!1}),{isPending:(0,l.computed)((function(){return P.value})),data:X,onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return b.push(e)},error:E,abort:function(){var e;return null===(e=T.value)||void 0===e?void 0:e.abortXhr()},promise:(0,l.computed)((function(){return T.value||new Promise((function(){}))})),reload:x,xhr:c}},post:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=new g;return j(j({},d((function(){return r.post(e||{})}),v(t))),{},{xhr:r})},put:function(e,t){var r=new g;return j(j({},d((function(){return r.put(e||{})}),v(t||{}))),{},{xhr:r})},delete:function(e,t){var r=new g;return j(j({},d((function(){return r.delete(e||{})}),v(t||{}))),{},{xhr:r})}}}function P(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,l.ref)(!1);return(0,l.watch)((function(){return e.value}),(function(){n.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){n.value=!0===e.value}),r)):n.value=!0===e.value}),{immediate:!0}),(0,l.computed)((function(){return n.value}))}})(),o})()}));
1
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("vue"));else if("function"==typeof define&&define.amd)define(["vue"],t);else{var r="object"==typeof exports?t(require("vue")):t(e.vue);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(function(e){return(()=>{"use strict";var t={748:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var i=r[e]={exports:{}};return t[e](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{Deferred:()=>c,Xhr:()=>g,cache:()=>t,cacheSize:()=>r,clearCache:()=>i,useAsync:()=>d,useMutation:()=>h,useResult:()=>p,useSpinner:()=>P,useXhr:()=>S});var e=new Map;function t(t){var r,n,o=t||{},i=o.id,u=o.xhr,a="string"!=typeof t.duration&&t.duration||200;if("max"===t.duration&&(a=36e5),!e.get(i)&&u?(r=u(),e.set(i,r),r.then((function(t){return"[Log] add cache: ".concat(i),clearTimeout(n),n=window.setTimeout((function(){e.delete(i),"[Log] delete cache: ".concat(i),clearTimeout(n)}),a),t}),(function(){return e.delete(i),void"[Log] delete cache: ".concat(i)}))):i&&(r=e.get(i)),r)return r;throw Error("Call to undefined cache")}function r(){return e.size}function i(t){t?e.delete(t):e.forEach((function(e,t,r){r.delete(t)}))}function u(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function a(e,t,r){return t&&u(e.prototype,t),r&&u(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function s(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var c=a((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),s(this,"resolve",void 0),s(this,"reject",void 0),s(this,"promise",void 0),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r}))})),l=n(748);function f(e){return function(e){if(Array.isArray(e))return v(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return v(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)?v(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function v(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 d(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,l.ref)(!0),n=(0,l.ref)(),o=(0,l.ref)(),i=(0,l.ref)(null),u=[],a=[],s=[],c=(0,l.ref)(null),v=(0,l.computed)((function(){return"function"==typeof t?t():(0,l.unref)(t)})),d=(0,l.ref)(),h=(0,l.computed)((function(){return"function"==typeof r?r():(0,l.unref)(r)})),p=function(t){a.forEach((function(e){return e(v.value)})),n.value=!0,i.value=null;var r=e,l=e;return c.value=Array.isArray(t)?l.apply(void 0,f(t)):r(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,v.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,v.value)}))})),c.value.finally((function(){n.value=!1})),c.value};return(0,l.watch)((function(){return h.value}),(function(e){!n.value&&e&&p(v.value)}),{immediate:!1}),(0,l.watch)((function(){return v.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===d.value||h.value&&t!==JSON.stringify(d.value))&&p(e),d.value=void 0===t?void 0:JSON.parse(t)}),{immediate:h.value,deep:!0}),{isPending:n,data:o,error:i,reload:function(){return p(v.value)},onError:function(e){return u.push(e)},onStart:function(e){return a.push(e)},onEnd:function(e){return s.push(e)},promise:(0,l.computed)((function(){return c.value}))}}function h(e){var t=(0,l.ref)(),r=(0,l.ref)(),n=(0,l.ref)(),o=[],i=[],u=(0,l.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.then((function(e){r.value=e,i.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)}))}),(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.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,l.computed)((function(){return u.value}))}}function p(e,t,r){var n=(0,l.ref)(t);return r||(r=function(e){return e}),t&&(n.value=r(t)),(0,l.watchEffect)((function(){if(e){var t,o=(0,l.unref)(e);o&&(n.value=null===(t=r)||void 0===t?void 0:t(o))}})),n}function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function m(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function b(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var g=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),b(this,"onError",(function(){})),b(this,"onStart",(function(){})),b(this,"onAbort",(function(){})),b(this,"onProgress",(function(){})),b(this,"onEnd",(function(){})),b(this,"token",null),b(this,"url",""),b(this,"params",{}),b(this,"sendAs","json"),b(this,"timeout",1e4),b(this,"port",80),b(this,"responseType","text"),b(this,"isPending",!1),b(this,"_eventsReady",void 0),b(this,"_oXHR",void 0),b(this,"_onEnd",void 0),b(this,"_deferred",void 0),b(this,"_isXhrResolved",void 0),b(this,"_isXhrRejected",void 0),b(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}var t,r,n;return t=e,r=[{key:"_onError",value:function(e){this.onError(e),this._deferred.reject(e)}},{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("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("timeout",e.onError,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._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({error:"Xhr aborted: ".concat(this.url),code:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this.onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1),e.addEventListener("timeout",this.onError,!1))}},{key:"_send",value:function(){var t;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"===y(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).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 c,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);this.url=o.url,this.params=o.params,this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],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:"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 Object.keys(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o+e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==y(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_injectParamsInUrl",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(t))return{url:e,params:t};var r=Object.getOwnPropertyNames(t).filter((function(e){return!e.match(/__[a-zA-Z0-9_]+__/gi)})).reduce((function(e,r){return e[r]=t[r],e}),{});return((e=e.replace(/#/,"%23")).match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substr(1,t.length),void 0!==r[t]&&(e=e.replace(":".concat(t),null===r[t]||""===r[t]?"null":r[t]),delete r[t])})),{url:e,params:r}}}],r&&m(t.prototype,r),n&&m(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function _(e){return _="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_(e)}function E(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 j(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?E(Object(r),!0).forEach((function(t){w(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):E(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function w(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}b(g,"onBeforeSendList",[]);var R=function(){},O=function(e){return(0,l.isRef)(e)?e.value:e};function S(e){var r=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},n=r.onError,o=r.onStart,u=r.onEnd,a=r.context,s=r.legacy,c=r.token,f=void 0===c?null:c,v=function(e){var t=O(f);return t?((0,l.isRef)(e)?e.value.token=t:e.token=t,e):e},h=(0,l.ref)([]);return s||(0,l.onBeforeUnmount)((function(){h.value.forEach((function(e){e.isPending&&e.abort()}))})),{get:function(e,r,s){var c=new g,v=(n||R).bind(a),d=(o||R).bind(a),p=(u||R).bind(a),y=[v],m=[d],b=[p],E=(0,l.ref)(null);h.value.push(c);var w,S,P=(0,l.ref)(),X=(0,l.ref)(),k="",A=0,H=(0,l.computed)((function(){var t,n={},o=(0,l.unref)(e);"string"==typeof o?t=o:(t=o.url,r||(r=o.params||{}),A=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=s),n.params=r,n.cacheDuration=A),f&&(n.token=O(f));var i=(0,l.unref)(n.params||r||{});return"function"==typeof i&&(i=i()),n.params=j(j({},i),(0,l.isRef)(r)?r.value||{}:r),k="function"==typeof t?t(n.params):(0,l.unref)(t),n.url=k,n})),L=void 0!==H.value.enabled?H.value.enabled:s;w=void 0===L?(0,l.ref)(!0):(0,l.isRef)(L)?L:"function"==typeof L?(0,l.ref)(L()):(0,l.ref)(!0===L);var T=(0,l.ref)(),x=function(){P.value&&c.abort();var e="object"===_(H.value.params)?H.value.params:{};m.forEach((function(t){return t(e,c)})),P.value=!0,E.value=null,S&&i(S),S=decodeURIComponent(g.stringifyUrl(String(k),e)),T.value=t({id:S,xhr:c.get.bind(c,H.value),duration:A}),T.value.then((function(e){X.value=e}),(function(e){y.forEach((function(t){return t(e,c)})),E.value=e})),T.value.finally((function(){var t;(t=h.value.indexOf(c))>-1&&h.value.splice(t,1),P.value=!1,b.forEach((function(t){return t(X.value,e,c)}))}))};return(0,l.watch)((function(){return H.value}),(function(){w.value&&x()}),{immediate:w.value}),(0,l.watch)((function(){return w.value}),(function(e){!P.value&&e&&x()}),{immediate:!1}),{isPending:(0,l.computed)((function(){return P.value})),data:X,onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return b.push(e)},error:E,abort:function(){var e;return null===(e=T.value)||void 0===e?void 0:e.abortXhr()},promise:(0,l.computed)((function(){return T.value||new Promise((function(){}))})),reload:x,xhr:c}},post:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=new g;return j(j({},d((function(){return r.post(e||{})}),v(t))),{},{xhr:r})},put:function(e,t){var r=new g;return j(j({},d((function(){return r.put(e||{})}),v(t||{}))),{},{xhr:r})},delete:function(e,t){var r=new g;return j(j({},d((function(){return r.delete(e||{})}),v(t||{}))),{},{xhr:r})}}}function P(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,l.ref)(!1);return(0,l.watch)((function(){return e.value}),(function(){n.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){n.value=!0===e.value}),r)):n.value=!0===e.value}),{immediate:!0}),(0,l.computed)((function(){return n.value}))}})(),o})()}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-use-async",
3
- "version": "1.1.19",
3
+ "version": "1.1.20",
4
4
  "description": "Xhr and async helpers",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,23 +20,23 @@
20
20
  "lint:quiet": "eslint --quiet \"{tests,src}/**/*.{js,ts,tsx,vue}\" && npm run tsc:check-types",
21
21
  "lint:fix": "npm run tsc:check-types && eslint --fix --ext .js,.ts src tests",
22
22
  "precommit": "lint-staged",
23
- "prepublishOnly": "npm run lint && npm run build",
24
- "test": "jest tests/** --no-cache",
23
+ "prepublishOnly": "npm run lint && npm run test && npm run build",
24
+ "test": "NODE_OPTIONS=--unhandled-rejections=warn jest tests/** --no-cache",
25
25
  "tsc:check-types": "tsc --noemit",
26
26
  "tsc:export-types": "tsc --outDir types"
27
27
  },
28
28
  "dependencies": {
29
- "vue": "^3.0.11"
29
+ "vue": "^3.2.30"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@babel/plugin-proposal-class-properties": "^7.16.7",
33
33
  "@babel/preset-env": "^7.16.11",
34
34
  "@babel/preset-typescript": "^7.16.7",
35
35
  "@types/jest": "^27.4.0",
36
- "@typescript-eslint/eslint-plugin": "^5.10.2",
37
- "@typescript-eslint/parser": "^5.10.2",
36
+ "@typescript-eslint/eslint-plugin": "^5.11.0",
37
+ "@typescript-eslint/parser": "^5.11.0",
38
38
  "babel-eslint": "^10.1.0",
39
- "babel-jest": "^27.4.6",
39
+ "babel-jest": "^27.5.1",
40
40
  "babel-loader": "^8.2.3",
41
41
  "babel-plugin-module-resolver": "^4.1.0",
42
42
  "eslint": "^8.8.0",
@@ -44,8 +44,8 @@
44
44
  "eslint-import-resolver-typescript": "^2.5.0",
45
45
  "eslint-plugin-import": "^2.25.4",
46
46
  "husky": "^7.0.4",
47
- "jest": "^27.4.7",
48
- "lint-staged": "^12.3.2",
47
+ "jest": "^27.5.1",
48
+ "lint-staged": "^12.3.3",
49
49
  "typescript": "^4.5.5",
50
50
  "vue": "^3.2.11",
51
51
  "webpack": "^5.68.0",