vue-use-async 1.8.8 → 1.9.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -2
- package/dist/useAsync.d.ts +1 -1
- package/dist/useResult.d.ts +2 -2
- package/dist/useXhr.d.ts +4 -4
- package/package.json +1 -1
- package/dist/index.js.LICENSE.txt +0 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
-
import*as t from"vue";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},r={};e.d(r,{cY:()=>p,TF:()=>C,PP:()=>i,LH:()=>u,Ej:()=>a,IL:()=>c,Yb:()=>A,n_:()=>x,bz:()=>R,p6:()=>q,cU:()=>M});const n={log:function(t){},warn:function(t){},info:function(t){},error:function(t){},debug:function(t){}};var o=new Map;function i(t){var e,r,i=t||{},a=i.id,u=i.xhr,c="string"!=typeof t.duration&&t.duration||200;if("max"===t.duration&&(c=36e5),!o.get(a)&&u?(e=u(),o.set(a,e),e.then((function(t){return n.log("[Log] add cache: ".concat(a)),clearTimeout(r),r=window.setTimeout((function(){o.delete(a),n.log("[Log] delete cache: ".concat(a)),clearTimeout(r)}),c),t}),(function(){return o.delete(a),n.log("[Log] delete cache: ".concat(a))}))):a&&(e=o.get(a)),e)return e;throw Error("Call to undefined cache")}function a(){return o.size}function u(){return Array.from(o.keys())}function c(t){t?o.delete(t):o.forEach((function(t,e,r){r.delete(e)})),n.log("[Log] cache cleared")}function s(t){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s(t)}function f(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,v(n.key),n)}}function l(t,e,r){return e&&f(t.prototype,e),r&&f(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function h(t,e,r){return(e=v(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function v(t){var e=function(t,e){if("object"!=s(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=s(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==s(e)?e:e+""}var p=l((function t(){var e=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),h(this,"resolve",void 0),h(this,"reject",void 0),h(this,"promise",void 0),h(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(t,r){e.resolve=t,e.reject=r})),this.promise.finally((function(){e.done=!0}))}));const d=(y={computed:()=>t.computed,isRef:()=>t.isRef,onBeforeUnmount:()=>t.onBeforeUnmount,ref:()=>t.ref,unref:()=>t.unref,watch:()=>t.watch,watchEffect:()=>t.watchEffect},m={},e.d(m,y),m);var y,m;const b="10000000-1000-4000-8000-100000000000".replace(/[018]/g,(function(t){return(+t^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+t/4).toString(16)}));function g(t){return g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g(t)}function w(){w=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",u=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function f(t,e,r,n){var i=e&&e.prototype instanceof m?e:m,a=Object.create(i.prototype),u=new X(n||[]);return o(a,"_invoke",{value:R(t,r,u)}),a}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=f;var h="suspendedStart",v="suspendedYield",p="executing",d="completed",y={};function m(){}function b(){}function E(){}var _={};s(_,a,(function(){return this}));var j=Object.getPrototypeOf,O=j&&j(j(T([])));O&&O!==r&&n.call(O,a)&&(_=O);var S=E.prototype=m.prototype=Object.create(_);function P(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function L(t,e){function r(o,i,a,u){var c=l(t[o],t,i);if("throw"!==c.type){var s=c.arg,f=s.value;return f&&"object"==g(f)&&n.call(f,"__await")?e.resolve(f.__await).then((function(t){r("next",t,a,u)}),(function(t){r("throw",t,a,u)})):e.resolve(f).then((function(t){s.value=t,a(s)}),(function(t){return r("throw",t,a,u)}))}u(c.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e((function(e,o){r(t,n,e,o)}))}return i=i?i.then(o,o):o()}})}function R(e,r,n){var o=h;return function(i,a){if(o===p)throw Error("Generator is already running");if(o===d){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var c=k(u,n);if(c){if(c===y)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=p;var s=l(e,r,n);if("normal"===s.type){if(o=n.done?d:v,s.arg===y)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=d,n.method="throw",n.arg=s.arg)}}}function k(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,k(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=l(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function A(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function x(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function X(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(A,this),this.reset(!0)}function T(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(g(e)+" is not iterable")}return b.prototype=E,o(S,"constructor",{value:E,configurable:!0}),o(E,"constructor",{value:b,configurable:!0}),b.displayName=s(E,c,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===b||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,E):(t.__proto__=E,s(t,c,"GeneratorFunction")),t.prototype=Object.create(S),t},e.awrap=function(t){return{__await:t}},P(L.prototype),s(L.prototype,u,(function(){return this})),e.AsyncIterator=L,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new L(f(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},P(S),s(S,c,"Generator"),s(S,a,(function(){return this})),s(S,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=T,X.prototype={constructor:X,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(x),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function o(n,o){return u.type="throw",u.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],u=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var c=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(c&&s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),x(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;x(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:T(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},e}function E(t,e,r,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function _(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var i=t.apply(e,r);function a(t){E(i,n,o,a,u,"next",t)}function u(t){E(i,n,o,a,u,"throw",t)}a(void 0)}))}}function j(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,P(n.key),n)}}function O(t,e,r){return e&&j(t.prototype,e),r&&j(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function S(t,e,r){return(e=P(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function P(t){var e=function(t,e){if("object"!=g(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=g(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==g(e)?e:e+""}var L=O((function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),S(this,"value",void 0),S(this,"uuid",void 0),this.uuid=b,this.value=e}));function R(t,e,r){var n=(0,d.ref)(e);return r||(r=function(t){return t}),void 0!==e&&(n.value=r(e)),(0,d.watchEffect)(_(w().mark((function e(){var o,i;return w().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:t&&void 0!==(o=(0,d.unref)(t))&&(n.value=null===(i=r)||void 0===i?void 0:i(o));case 1:case"end":return e.stop()}}),e)})))),(0,d.watch)((function(){return n.value}),(function(e){void 0!==e&&void 0!==t.value&&e!==n.value&&JSON.stringify(e)!==JSON.stringify(n.value)&&(t.value=new L(e))})),n}function k(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}const A=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,d.ref)(!0),n=(0,d.ref)(),o=(0,d.ref)(),i=(0,d.ref)(null),a=[],u=[],c=[],s=(0,d.ref)(null),f=(0,d.computed)((function(){return"function"==typeof e?e():(0,d.unref)(e)})),l=(0,d.ref)(),h=(0,d.computed)((function(){return"function"==typeof r?r():(0,d.unref)(r)})),v=function(e){if(!h.value)return null;u.forEach((function(t){return t(f.value)})),n.value=!0,i.value=null;var r,l=t,v=t;return s.value=Array.isArray(e)?v.apply(void 0,function(t){if(Array.isArray(t))return k(t)}(r=e)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(r)||function(t,e){if(t){if("string"==typeof t)return k(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?k(t,e):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(e),s.value.catch((function(t){i.value=t||null,a.forEach((function(t){return t(i.value,f.value)})),i.value=t})),s.value.then((function(t){o.value=t,c.forEach((function(e){return e(t,f.value)}))})),s.value.finally((function(){n.value=!1})),s.value},p=(0,d.computed)((function(){return s.value}));return(0,d.watch)((function(){return h.value}),(function(t){!n.value&&t&&v(f.value)}),{immediate:!1}),(0,d.watch)((function(){return i.value}),(function(t){if(t)throw t})),(0,d.watch)((function(){return f.value}),(function(t){var e=JSON.stringify(t);!n.value&&(void 0===t&&void 0===l.value||h.value&&e!==JSON.stringify(l.value))&&v(t),l.value=void 0===e?void 0:JSON.parse(e)}),{immediate:h.value,deep:!0}),{isPending:n,data:(0,d.computed)({get:function(){return o.value},set:function(t){t instanceof L&&t.uuid===b?o.value=t.value:(console.warn('"useAsync" Update a readonly field is not allowed'),o.value=t)}}),error:i,reload:function(){return v(f.value)},onError:function(t){a.push(t)},onStart:function(t){u.push(t)},onEnd:function(t){c.push(t)},promise:p}};function x(t){var e=(0,d.ref)(),r=(0,d.ref)(),n=(0,d.ref)(),o=[],i=[],a=(0,d.ref)(null);return{mutate:function(u){for(var c=arguments.length,s=new Array(c>1?c-1:0),f=1;f<c;f++)s[f-1]=arguments[f];e.value=!0,n.value=null;var l=t;return a.value=l.apply(void 0,[u].concat(s)),a.value.catch((function(t){n.value=t||null,o.forEach((function(t){return t(n.value,s.length?[u].concat(s):u)})),n.value=t})),a.value.then((function(t){r.value=t,i.forEach((function(t){return t(r.value,s.length?[u].concat(s):u)}))})),a.value.finally((function(){e.value=!1})),a.value},onError:function(t){return o.push(t)},onEnd:function(t){return i.push(t)},isPending:e,error:n,data:r,promise:(0,d.computed)((function(){return a.value}))}}function X(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function T(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,u=[],c=!0,s=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=i.call(r)).done)&&(u.push(n.value),u.length!==e);c=!0);}catch(t){s=!0,o=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(s)throw o}}return u}}(t,e)||function(t,e){if(t){if("string"==typeof t)return H(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?H(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function H(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function N(t){return N="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},N(t)}function U(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,D(n.key),n)}}function I(t,e,r){return(e=D(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function D(t){var e=function(t,e){if("object"!=N(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=N(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==N(e)?e:e+""}var C=function(){function t(e,r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),I(this,"onError",(function(){})),I(this,"onStart",(function(){})),I(this,"onAbort",(function(){})),I(this,"onProgress",(function(){})),I(this,"onEnd",(function(){})),I(this,"token",null),I(this,"url",""),I(this,"params",{}),I(this,"sendAs","json"),I(this,"timeout",1e4),I(this,"port",80),I(this,"responseType","text"),I(this,"isPending",!1),I(this,"_eventsReady",void 0),I(this,"_oXHR",void 0),I(this,"_onEnd",(function(){})),I(this,"_onError",(function(){})),I(this,"_deferred",void 0),I(this,"_isXhrResolved",void 0),I(this,"_isXhrRejected",void 0),I(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(e||{},r)}return e=t,r=[{key:"_getUrl",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.onBeforeSendList.reduce((function(t,e){return e(t||{})}),e);return t.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var t=this,e=function(){t._oXHR.removeEventListener("load",t._onEnd,!1),t._oXHR.removeEventListener("error",t._onError,!1),t._oXHR.removeEventListener("timeout",t._onError,!1),t._oXHR.removeEventListener("loadstart",t.onStart,!1),t._oXHR.removeEventListener("abort",t.onAbort,!1),t._oXHR.upload.removeEventListener("progress",t.onProgress,!1),t._oXHR.removeEventListener("progress",t.onProgress,!1)};this._deferred.promise.then(e,e)}},{key:"post",value:function(t){return this._constructor(t),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(t){return this._constructor(t),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(t){this._constructor(t||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var e=this._deferred.promise;return e.abortXhr=this.abort.bind(this),e}},{key:"delete",value:function(t){return this._constructor(t),this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(t){return this._deferred.resolve(t),this._deferred.promise}},{key:"reject",value:function(t){return this._deferred.reject(t),this._deferred.promise}},{key:"_setEvents",value:function(t){this._eventsReady||(this._eventReady=!0,t.addEventListener("load",this._onEnd,!1),t.addEventListener("error",this._onError,!1),t.addEventListener("timeout",this._onError,!1),t.addEventListener("loadstart",this.onStart,!1),t.addEventListener("abort",this.onAbort,!1),t.upload.addEventListener("progress",this.onProgress,!1),t.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var e;this.isPending=!0,"multipart"===this.sendAs?e=t.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),e=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(e,this)}},{key:"_constructor",value:function(e,r){var n=this;e&&"object"===N(e)?(this.sendAs=e.sendAs||this.sendAs,this.url=e.url||this.url,this.port=e.port||this.port,this.params=e.params||this.params,this.timeout=e.timeout||this.timeout,this.responseType=e.responseType||this.responseType,this.onProgress=(e.onProgress||this.onProgress).bind(this),this.onStart=(e.onStart||this.onStart).bind(this),this.onAbort=(e.onAbort||this.onAbort).bind(this),this.onEnd=(e.onEnd||this.onEnd).bind(this),this.onError=(e.onError||this.onError).bind(this),this.token=e.token||this.token):e&&"string"==typeof e&&(this.url=e,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new p,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=t.injectParamsInUrl(this.url,this.params),i=o.url,a=o.params;this.url=i,this.params=a,this._onError=function(t){n.onError(t),n.removeEvents(),n._deferred.reject(t)},this._onEnd=function(e){var r=t.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,e),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(t){var e=t.response;try{var r=t.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(e=JSON.parse(t.response))}catch(r){e=t.response}return e}},{key:"extractQueryParams",value:function(t){var e={},r=t.search(/#/);r>-1&&(t=t.substring(0,r));var n=t.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&t.split("?")[1].split("&").reduce((function(t,e){var r=T(e.split("="),2),n=r[0],a=r[1],u=T((void 0===a?"":a).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(u===o||u===i)t[n]=u===o;else{var c=Number(u);t[n]=""===u||Number.isNaN(c)?u:c}return t}),e),e}},{key:"onBeforeSend",value:function(e){t.onBeforeSendList.push(e)}},{key:"new",value:function(e){return new t(e)}},{key:"stringifyUrl",value:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.injectParamsInUrl(e,r);e=n.url,r=n.params;var o=e.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===N(r)?Object.getOwnPropertyNames(r).filter((function(t){return void 0!==r[t]})).forEach((function(t){i+="".concat(o).concat(t,"=").concat(encodeURIComponent(JSON.stringify(r[t]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(e=e.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(t){var e,r=new FormData;return Object.keys(t).forEach((function(n){if(t[n]instanceof FileList)for(var o=0;o<t[n].length;o+=1)r.append(n,t[n][o]);else t[n]instanceof File?r.append(n,t[n]):("object"!==N(e=t[n])&&!Array.isArray(e)||null===e||(e=JSON.stringify(e)),r.append(n,e))})),r}},{key:"_stringifyForPathParam",value:function(t,e,r){var n=r[e];switch(N(r[e])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return t.replace(":".concat(e),n)}},{key:"injectParamsInUrl",value:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:e,params:r};var n=decodeURIComponent(e),o=t.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var a=Object.getOwnPropertyNames(r).reduce((function(t,e){return t[e]=r[e],t}),function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?X(Object(r),!0).forEach((function(e){I(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):X(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(e){e=e.substring(1),void 0!==a[e]&&(n=t._stringifyForPathParam(n,e,a),delete a[e])})),Object.keys(a).forEach((function(t){void 0!==o[t]&&(o[t]=a[t],delete a[t])}));var u=Object.keys(o);if(u.length){var c="?";u.forEach((function(t){if(!0===o[t]||!1===o[t])n+="".concat(c).concat(t,"=").concat(o[t]);else if(null===o[t])n+="".concat(c).concat(t,"=").concat(o[t]);else{var e=Number(o[t]),r=Number.isNaN(e)?encodeURIComponent('"'.concat(o[t],'"')):e;n+="".concat(c).concat(t,"=").concat(r)}c="&"}))}return{url:n,params:a}}}],r&&U(e.prototype,r),n&&U(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,r,n}();function F(t){return F="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},F(t)}function J(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function B(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?J(Object(r),!0).forEach((function(e){G(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):J(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function G(t,e,r){return(e=function(t){var e=function(t,e){if("object"!=F(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=F(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==F(e)?e:e+""}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}I(C,"onBeforeSendList",[]);var z=function(){},Y=function(t){return(0,d.unref)(t)};function M(t){var e=t||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},r=e.onError,n=e.onStart,o=e.onEnd,a=e.context,u=e.legacy,c=e.token,s=void 0===c?null:c,f=(0,d.ref)([]),l=[],h=function(t,e,r){var n,o=(0,d.computed)((function(){var t={},n=(0,d.unref)(e||{});r||(r=n.params||{}),t.params=(0,d.unref)(r),s&&(t.token=Y(s));var o=(0,d.unref)(t.params||r||{});return"function"==typeof o&&(o=o()),t.url=(0,d.unref)(n.url),t})),i=new C,a=(0,d.unref)(o.value.params)||{};switch(t){case"post":default:n=i.post.bind(i);break;case"put":n=i.put.bind(i);break;case"delete":n=i.delete.bind(i)}return B(B({},A((function(){return n(B(B({},o.value),{},{params:a}))}))),{},{xhr:i})};return u||(0,d.onBeforeUnmount)((function(){f.value.forEach((function(t){t.isPending&&t.abort()})),l.forEach((function(t){return t()}))})),{get:function(t,e,u){var c=new C,h=(r||z).bind(a),v=(n||z).bind(a),p=(o||z).bind(a),y=[h],m=[v],g=[p],w=(0,d.ref)(null);f.value.push(c);var E=(0,d.ref)(),_=(0,d.ref)(),j="",O=0,S=(0,d.computed)((function(){var r,n={},o=(0,d.unref)(t);"string"==typeof o?r=o:(r=(0,d.unref)(o.url),e||(e=o.params||{}),O=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=u),n.params=(0,d.unref)(e),n.cacheDuration=O),s&&(n.token=Y(s));var i=(0,d.unref)(n.params||e||{});return"function"==typeof i&&(i=i()),n.params=B(B({},i),(0,d.isRef)(e)?e.value||{}:e),j="function"==typeof r?r(n.params):(0,d.unref)(r),n.url=j,n})),P=function(){var t=void 0!==S.value.enabled?S.value.enabled:u;return void 0===t||((0,d.isRef)(t)?(0,d.unref)(t):"function"==typeof t?t():!0===t)},R=(0,d.ref)(),k=function(){if(P()){E.value&&c.abort();var t="object"===F(S.value.params)?S.value.params:{};m.forEach((function(e){return e(t,c)})),E.value=!0,w.value=null;var e=decodeURIComponent(C.stringifyUrl(String(j),t));R.value=i({id:e,xhr:c.get.bind(c,S.value),duration:O}),R.value.catch((function(t){y.forEach((function(e){return e(t,c)})),w.value=t})),R.value.then((function(t){_.value=t})),R.value.finally((function(){var e;(e=f.value.indexOf(c))>-1&&f.value.splice(e,1),E.value=!1,g.forEach((function(e){return e(_.value,t,c)}))}))}};return l.push((0,d.watch)((function(){return S.value}),k,{immediate:P(),deep:!0})),l.push((0,d.watch)((function(){return w.value}),(function(t){if(t)throw t}))),{isPending:(0,d.computed)((function(){return E.value})),data:(0,d.computed)({get:function(){return _.value},set:function(t){t instanceof L&&t.uuid===b?_.value=t.value:(console.warn('"useXhr" Update a readonly field is not allowed'),_.value=t)}}),onError:function(t){return y.push(t)},onStart:function(t){return m.push(t)},onEnd:function(t){return g.push(t)},error:w,abort:function(){var t;return null===(t=R.value)||void 0===t?void 0:t.abortXhr()},promise:(0,d.computed)((function(){return R.value||new Promise((function(){}))})),reload:k,xhr:c}},post:function(t,e){return h("post",t,e)},put:function(t,e){return h("put",t,e)},delete:function(t,e){return h("delete",t,e)}}}function q(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,d.ref)(!1),o=0;return(0,d.watch)((function(){return t.value}),(function(t){if(t&&(o=Date.now(),n.value=t),!1===t&&o>0){var i=Date.now()-o;clearTimeout(e),i>r?n.value=t:e=setTimeout((function(){n.value=t}),r-i)}}),{immediate:!0}),(0,d.onBeforeUnmount)((function(){clearTimeout(e)})),(0,d.computed)((function(){return n.value}))}var $=r.cY,Q=r.TF,V=r.PP,K=r.LH,W=r.Ej,Z=r.IL,tt=r.Yb,et=r.n_,rt=r.bz,nt=r.p6,ot=r.cU;export{$ as Deferred,Q as Xhr,V as cache,K as cacheIds,W as cacheSize,Z as clearCache,tt as useAsync,et as useMutation,rt as useResult,nt as useSpinner,ot 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:()=>h,TF:()=>D,PP:()=>i,LH:()=>a,Ej:()=>u,IL:()=>s,Yb:()=>R,n_:()=>k,bz:()=>S,p6:()=>M,cU:()=>z});const n={log:function(e){},warn:function(e){},info:function(e){},error:function(e){},debug:function(e){}};var o=new Map;function i(e){var t,r,i=e||{},u=i.id,a=i.xhr,s="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(s=36e5),!o.get(u)&&a?(t=a(),o.set(u,t),t.then((function(e){return n.log("[Log] add cache: ".concat(u)),clearTimeout(r),r=window.setTimeout((function(){o.delete(u),n.log("[Log] delete cache: ".concat(u)),clearTimeout(r)}),s),e}),(function(){return o.delete(u),n.log("[Log] delete cache: ".concat(u))}))):u&&(t=o.get(u)),t)return t;throw Error("Call to undefined cache")}function u(){return o.size}function a(){return Array.from(o.keys())}function s(e){e?o.delete(e):o.forEach((function(e,t,r){r.delete(t)})),n.log("[Log] cache cleared")}function c(e){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},c(e)}function l(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,d(n.key),n)}}function f(e,t,r){return t&&l(e.prototype,t),r&&l(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function v(e,t,r){return(t=d(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){var t=function(e,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 h=f((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),v(this,"resolve",void 0),v(this,"reject",void 0),v(this,"promise",void 0),v(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r})),this.promise.finally((function(){t.done=!0}))}));const p=(y={computed:()=>e.computed,isRef:()=>e.isRef,onBeforeUnmount:()=>e.onBeforeUnmount,ref:()=>e.ref,unref:()=>e.unref,watch:()=>e.watch},m={},t.d(m,y),m);var y,m;const b="10000000-1000-4000-8000-100000000000".replace(/[018]/g,(function(e){return(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16)}));function g(e){return g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},g(e)}function w(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,j(n.key),n)}}function E(e,t,r){return t&&w(e.prototype,t),r&&w(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _(e,t,r){return(t=j(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function j(e){var t=function(e,t){if("object"!=g(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=g(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==g(t)?t:t+""}var O=E((function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),_(this,"value",void 0),_(this,"uuid",void 0),this.uuid=b,this.value=t}));function S(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(e){return e},n=void 0===e.value?t:e.value;return e.value=void 0===n?n:new O(null==r?void 0:r(n)),(0,p.watch)((function(){return e.value}),(function(t){void 0===t||t instanceof O||(e.value=new O(null==r?void 0:r(t)))})),(0,p.computed)({get:function(){var r;return void 0===(r=e.value instanceof O?e.value.value:e.value)?t:r},set:function(t){e.value=new O(t)}})}function P(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}const R=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,p.ref)(!0),n=(0,p.ref)(),o=(0,p.ref)(),i=(0,p.ref)(null),u=[],a=[],s=[],c=(0,p.ref)(null),l=(0,p.computed)((function(){return"function"==typeof t?t():(0,p.unref)(t)})),f=(0,p.ref)(),v=(0,p.computed)((function(){return"function"==typeof r?r():(0,p.unref)(r)})),d=function(t){if(!v.value)return null;a.forEach((function(e){return e(l.value)})),n.value=!0,i.value=null;var r,f=e,d=e;return c.value=Array.isArray(t)?d.apply(void 0,function(e){if(Array.isArray(e))return P(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 P(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?P(e,t):void 0}}(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.")}()):f(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,l.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,l.value)}))})),c.value.finally((function(){n.value=!1})),c.value},h=(0,p.computed)((function(){return c.value}));return(0,p.watch)((function(){return v.value}),(function(e){!n.value&&e&&d(l.value)}),{immediate:!1}),(0,p.watch)((function(){return i.value}),(function(e){if(e)throw e})),(0,p.watch)((function(){return l.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===f.value||v.value&&t!==JSON.stringify(f.value))&&d(e),f.value=void 0===t?void 0:JSON.parse(t)}),{immediate:v.value,deep:!0}),{isPending:n,data:(0,p.computed)({get:function(){return o.value},set:function(e){e instanceof O&&e.uuid===b||console.warn('"useAsync" Update a readonly field is not allowed'),o.value=e}}),error:i,reload:function(){return d(l.value)},onError:function(e){u.push(e)},onStart:function(e){a.push(e)},onEnd:function(e){s.push(e)},promise:h}};function k(e){var t=(0,p.ref)(),r=(0,p.ref)(),n=(0,p.ref)(),o=[],i=[],u=(0,p.ref)(null);return{mutate:function(a){for(var s=arguments.length,c=new Array(s>1?s-1:0),l=1;l<s;l++)c[l-1]=arguments[l];t.value=!0,n.value=null;var f=e;return u.value=f.apply(void 0,[a].concat(c)),u.value.catch((function(e){n.value=e||null,o.forEach((function(e){return e(n.value,c.length?[a].concat(c):a)})),n.value=e})),u.value.then((function(e){r.value=e,i.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)}))})),u.value.finally((function(){t.value=!1})),u.value},onError:function(e){return o.push(e)},onEnd:function(e){return i.push(e)},isPending:t,error:n,data:r,promise:(0,p.computed)((function(){return u.value}))}}function A(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function X(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,u,a=[],s=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;s=!1}else for(;!(s=(n=i.call(r)).done)&&(a.push(n.value),a.length!==t);s=!0);}catch(e){c=!0,o=e}finally{try{if(!s&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(e,t)||function(e,t){if(e){if("string"==typeof e)return T(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?T(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function T(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function H(e){return H="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},H(e)}function L(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,N(n.key),n)}}function U(e,t,r){return(t=N(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function N(e){var t=function(e,t){if("object"!=H(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=H(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==H(t)?t:t+""}var D=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),U(this,"onError",(function(){})),U(this,"onStart",(function(){})),U(this,"onAbort",(function(){})),U(this,"onProgress",(function(){})),U(this,"onEnd",(function(){})),U(this,"token",null),U(this,"url",""),U(this,"params",{}),U(this,"sendAs","json"),U(this,"timeout",1e4),U(this,"port",80),U(this,"responseType","text"),U(this,"isPending",!1),U(this,"_eventsReady",void 0),U(this,"_oXHR",void 0),U(this,"_onEnd",(function(){})),U(this,"_onError",(function(){})),U(this,"_deferred",void 0),U(this,"_isXhrResolved",void 0),U(this,"_isXhrRejected",void 0),U(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}return t=e,r=[{key:"_getUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e._onError,!1),e._oXHR.removeEventListener("timeout",e._onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var t=this._deferred.promise;return t.abortXhr=this.abort.bind(this),t}},{key:"delete",value:function(e){return this._constructor(e),this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this._onError,!1),e.addEventListener("timeout",this._onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var t;this.isPending=!0,"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===H(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).bind(this),this.onStart=(t.onStart||this.onStart).bind(this),this.onAbort=(t.onAbort||this.onAbort).bind(this),this.onEnd=(t.onEnd||this.onEnd).bind(this),this.onError=(t.onError||this.onError).bind(this),this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new h,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e.injectParamsInUrl(this.url,this.params),i=o.url,u=o.params;this.url=i,this.params=u,this._onError=function(e){n.onError(e),n.removeEvents(),n._deferred.reject(e)},this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t={},r=e.search(/#/);r>-1&&(e=e.substring(0,r));var n=e.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&e.split("?")[1].split("&").reduce((function(e,t){var r=X(t.split("="),2),n=r[0],u=r[1],a=X((void 0===u?"":u).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(a===o||a===i)e[n]=a===o;else{var s=Number(a);e[n]=""===a||Number.isNaN(s)?a:s}return e}),t),t}},{key:"onBeforeSend",value:function(t){e.onBeforeSendList.push(t)}},{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e.injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===H(r)?Object.getOwnPropertyNames(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==H(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_stringifyForPathParam",value:function(e,t,r){var n=r[t];switch(H(r[t])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return e.replace(":".concat(t),n)}},{key:"injectParamsInUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:t,params:r};var n=decodeURIComponent(t),o=e.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var u=Object.getOwnPropertyNames(r).reduce((function(e,t){return e[t]=r[t],e}),function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?A(Object(r),!0).forEach((function(t){U(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):A(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substring(1),void 0!==u[t]&&(n=e._stringifyForPathParam(n,t,u),delete u[t])})),Object.keys(u).forEach((function(e){void 0!==o[e]&&(o[e]=u[e],delete u[e])}));var a=Object.keys(o);if(a.length){var s="?";a.forEach((function(e){if(!0===o[e]||!1===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(s).concat(e,"=").concat(r)}s="&"}))}return{url:n,params:u}}}],r&&L(t.prototype,r),n&&L(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,r,n}();function I(e){return I="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},I(e)}function C(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function x(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?C(Object(r),!0).forEach((function(t){B(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):C(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function B(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=I(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=I(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==I(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}U(D,"onBeforeSendList",[]);var F=function(){},J=function(e){return(0,p.unref)(e)};function z(e){var t=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},r=t.onError,n=t.onStart,o=t.onEnd,u=t.context,a=t.legacy,s=t.token,c=void 0===s?null:s,l=(0,p.ref)([]),f=[],v=function(e,t,r){var n,o=(0,p.computed)((function(){var e={},n=(0,p.unref)(t||{});r||(r=n.params||{}),e.params=(0,p.unref)(r),c&&(e.token=J(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 D,u=(0,p.unref)(o.value.params)||{};switch(e){case"post":default:n=i.post.bind(i);break;case"put":n=i.put.bind(i);break;case"delete":n=i.delete.bind(i)}return x(x({},R((function(){return n(x(x({},o.value),{},{params:u}))}))),{},{xhr:i})};return a||(0,p.onBeforeUnmount)((function(){l.value.forEach((function(e){e.isPending&&e.abort()})),f.forEach((function(e){return e()}))})),{get:function(e,t,a){var s=new D,v=(r||F).bind(u),d=(n||F).bind(u),h=(o||F).bind(u),y=[v],m=[d],g=[h],w=(0,p.ref)(null);l.value.push(s);var E=(0,p.ref)(),_=(0,p.ref)(),j="",S=0,P=(0,p.computed)((function(){var r,n={},o=(0,p.unref)(e);"string"==typeof o?r=o:(r=(0,p.unref)(o.url),t||(t=o.params||{}),S=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=a),n.params=(0,p.unref)(t),n.cacheDuration=S),c&&(n.token=J(c));var i=(0,p.unref)(n.params||t||{});return"function"==typeof i&&(i=i()),n.params=x(x({},i),(0,p.isRef)(t)?t.value||{}:t),j="function"==typeof r?r(n.params):(0,p.unref)(r),n.url=j,n})),R=function(){var e=void 0!==P.value.enabled?P.value.enabled:a;return void 0===e||((0,p.isRef)(e)?(0,p.unref)(e):"function"==typeof e?e():!0===e)},k=(0,p.ref)(),A=function(){if(R()){E.value&&s.abort();var e="object"===I(P.value.params)?P.value.params:{};m.forEach((function(t){return t(e,s)})),E.value=!0,w.value=null;var t=decodeURIComponent(D.stringifyUrl(String(j),e));k.value=i({id:t,xhr:s.get.bind(s,P.value),duration:S}),k.value.catch((function(e){y.forEach((function(t){return t(e,s)})),w.value=e})),k.value.then((function(e){_.value=e})),k.value.finally((function(){var t;(t=l.value.indexOf(s))>-1&&l.value.splice(t,1),E.value=!1,g.forEach((function(t){return t(_.value,e,s)}))}))}};return f.push((0,p.watch)((function(){return P.value}),A,{immediate:R(),deep:!0})),f.push((0,p.watch)((function(){return w.value}),(function(e){if(e)throw e}))),{isPending:(0,p.computed)((function(){return E.value})),data:(0,p.computed)({get:function(){return _.value},set:function(e){e instanceof O&&e.uuid===b||console.warn('"useXhr" Update a readonly field is not allowed'),_.value=e}}),onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return g.push(e)},error:w,abort:function(){var e;return null===(e=k.value)||void 0===e?void 0:e.abortXhr()},promise:(0,p.computed)((function(){return k.value||new Promise((function(){}))})),reload:A,xhr:s}},post:function(e,t){return v("post",e,t)},put:function(e,t){return v("put",e,t)},delete:function(e,t){return v("delete",e,t)}}}function M(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,p.ref)(!1),o=0;return(0,p.watch)((function(){return e.value}),(function(e){if(e&&(o=Date.now(),n.value=e),!1===e&&o>0){var i=Date.now()-o;clearTimeout(t),i>r?n.value=e:t=setTimeout((function(){n.value=e}),r-i)}}),{immediate:!0}),(0,p.onBeforeUnmount)((function(){clearTimeout(t)})),(0,p.computed)((function(){return n.value}))}var Y=r.cY,q=r.TF,$=r.PP,Q=r.LH,G=r.Ej,V=r.IL,K=r.Yb,W=r.n_,Z=r.bz,ee=r.p6,te=r.cU;export{Y as Deferred,q as Xhr,$ as cache,Q as cacheIds,G as cacheSize,V as clearCache,K as useAsync,W as useMutation,Z as useResult,ee as useSpinner,te as useXhr};
|
package/dist/useAsync.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ 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:
|
|
11
|
+
data: ComputedRef<undefined | null | UnwrappedPromiseType<typeof func>>;
|
|
12
12
|
error: Ref<null | Error>;
|
|
13
13
|
reload: () => unknown;
|
|
14
14
|
onError: (cb: OnErrorCb<Params<Z, A>>) => unknown;
|
package/dist/useResult.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
2
|
type NonNullable<T> = Exclude<T, null | undefined>;
|
|
3
3
|
type Res<T, Z extends T> = NonNullable<Z extends never[] ? T : Z>;
|
|
4
4
|
export declare class Result<T> {
|
|
@@ -6,5 +6,5 @@ export declare class Result<T> {
|
|
|
6
6
|
value: T;
|
|
7
7
|
uuid: string;
|
|
8
8
|
}
|
|
9
|
-
export default function <T, Z extends T, U = Res<T, Z>>(
|
|
9
|
+
export default function <T, Z extends T, U = Res<T, Z>>(input: Ref<T>, defaultRes: NonNullable<Z>, map?: (r: Res<T, Z>) => NonNullable<U>): Ref<U>;
|
|
10
10
|
export {};
|
package/dist/useXhr.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComputedRef, Ref } from 'vue';
|
|
1
|
+
import type { ComputedRef, Ref } from 'vue';
|
|
2
2
|
import type { $UpdateConfigArgs, GetConfig, GetReturn, TypeAllowed } from './index';
|
|
3
3
|
import Xhr from './Xhr';
|
|
4
4
|
type Enabled = undefined | null | (() => boolean) | Ref<boolean> | ComputedRef<boolean> | boolean;
|
|
@@ -22,7 +22,7 @@ export default function <T, Z extends TypeAllowed>(args?: UseXhr<T, Z>): {
|
|
|
22
22
|
post: <TT = T, ZZ = Z>(xhrConfig?: $UpdateConfigArgs, params?: Params<ZZ>) => {
|
|
23
23
|
xhr: Xhr<TT>;
|
|
24
24
|
isPending: Ref<undefined | boolean>;
|
|
25
|
-
data:
|
|
25
|
+
data: ComputedRef<TT | null | undefined>;
|
|
26
26
|
error: Ref<null | Error>;
|
|
27
27
|
reload: () => unknown;
|
|
28
28
|
onError: (cb: (e: null | Error, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => [] | TypeAllowed)) => unknown) => unknown;
|
|
@@ -33,7 +33,7 @@ export default function <T, Z extends TypeAllowed>(args?: UseXhr<T, Z>): {
|
|
|
33
33
|
put: <TT = T, ZZ = Z>(xhrConfig?: $UpdateConfigArgs, params?: Params<ZZ>) => {
|
|
34
34
|
xhr: Xhr<TT>;
|
|
35
35
|
isPending: Ref<undefined | boolean>;
|
|
36
|
-
data:
|
|
36
|
+
data: ComputedRef<TT | null | undefined>;
|
|
37
37
|
error: Ref<null | Error>;
|
|
38
38
|
reload: () => unknown;
|
|
39
39
|
onError: (cb: (e: null | Error, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => [] | TypeAllowed)) => unknown) => unknown;
|
|
@@ -44,7 +44,7 @@ export default function <T, Z extends TypeAllowed>(args?: UseXhr<T, Z>): {
|
|
|
44
44
|
delete: <TT = T, ZZ = Z>(xhrConfig?: $UpdateConfigArgs, params?: Params<ZZ>) => {
|
|
45
45
|
xhr: Xhr<TT>;
|
|
46
46
|
isPending: Ref<undefined | boolean>;
|
|
47
|
-
data:
|
|
47
|
+
data: ComputedRef<TT | null | undefined>;
|
|
48
48
|
error: Ref<null | Error>;
|
|
49
49
|
reload: () => unknown;
|
|
50
50
|
onError: (cb: (e: null | Error, params: [] | TypeAllowed | ComputedRef<[] | TypeAllowed> | Ref<[] | TypeAllowed> | (() => [] | TypeAllowed)) => unknown) => unknown;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
|