vue-use-async 1.1.18 → 1.1.19
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/Xhr.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +16 -16
package/dist/Xhr.d.ts
CHANGED
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 n="object"==typeof exports?t(require("vue")):t(e.vue);for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(self,(function(e){return(()=>{"use strict";var t={748:t=>{t.exports=e}},n={};function r(e){var o=n[e];if(void 0!==o)return o.exports;var i=n[e]={exports:{}};return t[e](i,i.exports,r),i.exports}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{r.r(o),r.d(o,{Deferred:()=>a,Xhr:()=>m,cache:()=>t,cacheSize:()=>n,clearCache:()=>i,useAsync:()=>f,useMutation:()=>v,useResult:()=>d,useSpinner:()=>S,useXhr:()=>w});var e=new Map;function t(t){var n,r,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?(n=u(),e.set(i,n),n.then((function(t){return"[Log] add cache: ".concat(i),clearTimeout(r),r=window.setTimeout((function(){e.delete(i),"[Log] delete cache: ".concat(i),clearTimeout(r)}),a),t}),(function(){return e.delete(i),void"[Log] delete cache: ".concat(i)}))):i&&(n=e.get(i)),n)return n;throw Error("Call to undefined cache")}function n(){return e.size}function i(t){t?e.delete(t):e.forEach((function(e,t,n){n.delete(t)}))}function u(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var a=function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),u(this,"resolve",void 0),u(this,"reject",void 0),u(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,n){t.resolve=e,t.reject=n}))},s=r(748);function c(e){return function(e){if(Array.isArray(e))return l(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 l(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?l(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 l(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function f(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,s.ref)(!0),r=(0,s.ref)(),o=(0,s.ref)(),i=(0,s.ref)(null),u=[],a=[],l=[],f=(0,s.ref)(null),v=(0,s.computed)((function(){return"function"==typeof t?t():(0,s.unref)(t)})),d=(0,s.ref)(),h=(0,s.computed)((function(){return"function"==typeof n?n():(0,s.unref)(n)})),p=function(t){a.forEach((function(e){return e(v.value)})),r.value=!0,i.value=null;var n=e,s=e;return f.value=Array.isArray(t)?s.apply(void 0,c(t)):n(t),f.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,v.value)})),i.value=e})),f.value.then((function(e){o.value=e,l.forEach((function(t){return t(e,v.value)}))})),f.value.finally((function(){r.value=!1})),f.value};return(0,s.watch)((function(){return h.value}),(function(e){!r.value&&e&&p(v.value)}),{immediate:!1}),(0,s.watch)((function(){return v.value}),(function(e){var t=JSON.stringify(e);!r.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:r,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 l.push(e)},promise:(0,s.computed)((function(){return f.value}))}}function v(e){var t=(0,s.ref)(),n=(0,s.ref)(),r=(0,s.ref)(),o=[],i=[],u=(0,s.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,r.value=null;var f=e;return u.value=f.apply(void 0,[a].concat(c)),u.value.then((function(e){n.value=e,i.forEach((function(e){return e(n.value,c.length?[a].concat(c):a)}))}),(function(e){r.value=e||null,o.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)})),r.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:r,data:n,promise:(0,s.computed)((function(){return u.value}))}}function d(e,t,n){var r=(0,s.ref)(t);return n||(n=function(e){return e}),t&&(r.value=n(t)),(0,s.watchEffect)((function(){if(e){var t,o=(0,s.unref)(e);o&&(r.value=null===(t=n)||void 0===t?void 0:t(o))}})),r}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 p(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function y(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var m=function(){function e(t,n){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),y(this,"onError",(function(){})),y(this,"onStart",(function(){})),y(this,"onAbort",(function(){})),y(this,"onProgress",(function(){})),y(this,"onEnd",(function(){})),y(this,"token",null),y(this,"url",""),y(this,"params",{}),y(this,"sendAs","json"),y(this,"timeout",1e4),y(this,"port",80),y(this,"responseType","text"),y(this,"isPending",!1),y(this,"_eventsReady",void 0),y(this,"_oXHR",void 0),y(this,"_onEnd",void 0),y(this,"_deferred",void 0),y(this,"_isXhrResolved",void 0),y(this,"_isXhrRejected",void 0),y(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},n)}var t,n,r;return t=e,n=[{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]:{},n=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,n)}},{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,n){var r=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=n||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new a,this._deferred.promise.finally((function(){r.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 n=e.parseResult(r._oXHR);return r._oXHR.status>=400?(r._isXhrRejected=!0,r._onError(n),r._deferred.reject(n)):(r.onEnd(n,t),r._isXhrResolved=!0,r.removeEvents(),r.resolve(n))},this._setEvents(this._oXHR)}}],r=[{key:"parseResult",value:function(e){var t=e.response;try{var n=e.getResponseHeader("Content-Type");n&&n.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(n){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 n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=e._injectParamsInUrl(t,n);t=r.url,n=r.params;var o=t.indexOf("?")>-1?"&":"?",i="";return Object.keys(n).filter((function(e){return void 0!==n[e]})).forEach((function(e){i+="".concat(o+e,"=").concat(encodeURIComponent(JSON.stringify(n[e]))),o="&"})),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,n=new FormData;return Object.keys(e).forEach((function(r){if(e[r]instanceof FileList)for(var o=0;o<e[r].length;o+=1)n.append(r,e[r][o]);else e[r]instanceof File?n.append(r,e[r]):("object"!==h(t=e[r])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),n.append(r,t))})),n}},{key:"_injectParamsInUrl",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=Object.getOwnPropertyNames(t).filter((function(e){return!e.match(/__[a-zA-Z0-9_]+__/gi)})).reduce((function(e,n){return e[n]=t[n],e}),{});return((e=e.replace(/#/,"%23")).match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substr(1,t.length),void 0!==n[t]&&(e=e.replace(":".concat(t),null===n[t]||""===n[t]?"null":n[t]),delete n[t])})),{url:e,params:n}}}],n&&p(t.prototype,n),r&&p(t,r),e}();function b(e){return b="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},b(e)}function g(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function _(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?g(Object(n),!0).forEach((function(t){E(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):g(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function E(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}y(m,"onBeforeSendList",[]);var j=function(){},R=function(e){return(0,s.isRef)(e)?e.value:e};function w(e){var n=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},r=n.onError,o=n.onStart,u=n.onEnd,a=n.context,c=n.legacy,l=n.token,v=void 0===l?null:l,d=function(e){var t=R(v);return t?((0,s.isRef)(e)?e.value.token=t:e.token=t,e):e},h=(0,s.ref)([]);return c||(0,s.onBeforeUnmount)((function(){h.value.forEach((function(e){e.isPending&&e.abort()}))})),{get:function(e,n,c){var l=new m,f=(r||j).bind(a),d=(o||j).bind(a),p=(u||j).bind(a),y=[f],g=[d],E=[p],w=(0,s.ref)(null);h.value.push(l);var S,O,P=(0,s.ref)(),X=(0,s.ref)(),k="",A=0,H=(0,s.computed)((function(){var t,r={},o=(0,s.unref)(e);"string"==typeof o?t=o:(t=o.url,n||(n=o.params||{}),A=o.cacheDuration,void 0!==o.enabled&&(r.enabled=o.enabled),void 0===r.enabled&&(r.enabled=c),r.params=n,r.cacheDuration=A),v&&(r.token=R(v));var i=(0,s.unref)(r.params||n||{});return"function"==typeof i&&(i=i()),r.params=_(_({},i),(0,s.isRef)(n)?n.value||{}:n),k="function"==typeof t?t(r.params):(0,s.unref)(t),r.url=k,r})),L=void 0!==H.value.enabled?H.value.enabled:c;S=void 0===L?(0,s.ref)(!0):(0,s.isRef)(L)?L:"function"==typeof L?(0,s.ref)(L()):(0,s.ref)(!0===L);var T=(0,s.ref)(),x=function(){P.value&&l.abort();var e="object"===b(H.value.params)?H.value.params:{};g.forEach((function(t){return t(e,l)})),P.value=!0,w.value=null,O&&i(O),O=decodeURIComponent(m.stringifyUrl(String(k),e)),T.value=t({id:O,xhr:l.get.bind(l,H.value),duration:A}),T.value.then((function(e){X.value=e}),(function(e){y.forEach((function(t){return t(e,l)})),w.value=e})),T.value.finally((function(){var t;(t=h.value.indexOf(l))>-1&&h.value.splice(t,1),P.value=!1,E.forEach((function(t){return t(X.value,e,l)}))}))};return(0,s.watch)((function(){return H.value}),(function(){S.value&&x()}),{immediate:S.value}),(0,s.watch)((function(){return S.value}),(function(e){!P.value&&e&&x()}),{immediate:!1}),{isPending:(0,s.computed)((function(){return P.value})),data:X,onError:function(e){return y.push(e)},onStart:function(e){return g.push(e)},onEnd:function(e){return E.push(e)},error:w,abort:function(){var e;return null===(e=T.value)||void 0===e?void 0:e.abortXhr()},promise:(0,s.computed)((function(){return T.value||new Promise((function(){}))})),reload:x,xhr:l}},post:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=new m;return _(_({},f((function(){return n.post(e||{})}),d(t))),{},{xhr:n})},put:function(e,t){var n=new m;return _(_({},f((function(){return n.put(e||{})}),d(t||{}))),{},{xhr:n})},delete:function(e,t){var n=new m;return _(_({},f((function(){return n.delete(e||{})}),d(t||{}))),{},{xhr:n})}}}function S(e){var t,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,r=(0,s.ref)(!1);return(0,s.watch)((function(){return e.value}),(function(){r.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){r.value=!0===e.value}),n)):r.value=!0===e.value}),{immediate:!0}),(0,s.computed)((function(){return r.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:()=>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})()}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-use-async",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.19",
|
|
4
4
|
"description": "Xhr and async helpers",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,7 +20,7 @@
|
|
|
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
|
|
23
|
+
"prepublishOnly": "npm run lint && npm run build",
|
|
24
24
|
"test": "jest tests/** --no-cache",
|
|
25
25
|
"tsc:check-types": "tsc --noemit",
|
|
26
26
|
"tsc:export-types": "tsc --outDir types"
|
|
@@ -29,27 +29,27 @@
|
|
|
29
29
|
"vue": "^3.0.11"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@babel/plugin-proposal-class-properties": "^7.16.
|
|
33
|
-
"@babel/preset-env": "^7.16.
|
|
34
|
-
"@babel/preset-typescript": "^7.16.
|
|
35
|
-
"@types/jest": "^27.0
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
37
|
-
"@typescript-eslint/parser": "^5.
|
|
32
|
+
"@babel/plugin-proposal-class-properties": "^7.16.7",
|
|
33
|
+
"@babel/preset-env": "^7.16.11",
|
|
34
|
+
"@babel/preset-typescript": "^7.16.7",
|
|
35
|
+
"@types/jest": "^27.4.0",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
|
37
|
+
"@typescript-eslint/parser": "^5.10.2",
|
|
38
38
|
"babel-eslint": "^10.1.0",
|
|
39
|
-
"babel-jest": "^27.
|
|
39
|
+
"babel-jest": "^27.4.6",
|
|
40
40
|
"babel-loader": "^8.2.3",
|
|
41
41
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
42
|
-
"eslint": "^8.
|
|
42
|
+
"eslint": "^8.8.0",
|
|
43
43
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
44
44
|
"eslint-import-resolver-typescript": "^2.5.0",
|
|
45
|
-
"eslint-plugin-import": "^2.25.
|
|
45
|
+
"eslint-plugin-import": "^2.25.4",
|
|
46
46
|
"husky": "^7.0.4",
|
|
47
|
-
"jest": "^27.
|
|
48
|
-
"lint-staged": "^12.
|
|
49
|
-
"typescript": "^4.5.
|
|
47
|
+
"jest": "^27.4.7",
|
|
48
|
+
"lint-staged": "^12.3.2",
|
|
49
|
+
"typescript": "^4.5.5",
|
|
50
50
|
"vue": "^3.2.11",
|
|
51
|
-
"webpack": "^5.
|
|
52
|
-
"webpack-cli": "^4.9.
|
|
51
|
+
"webpack": "^5.68.0",
|
|
52
|
+
"webpack-cli": "^4.9.2"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"vue": "^3.2.11"
|