vue-use-async 1.10.3 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/useMutation.d.ts +13 -4
- package/package.json +14 -15
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue';
|
|
|
2
2
|
import cache, { cacheIds, cacheSize, clearCache } from './cache';
|
|
3
3
|
import Deferred from './Deferred';
|
|
4
4
|
import useAsync from './useAsync';
|
|
5
|
-
import useMutation from './useMutation';
|
|
5
|
+
import useMutation, { useMutationWithError } from './useMutation';
|
|
6
6
|
import useResult from './useResult';
|
|
7
7
|
import useXhr from './useXhr';
|
|
8
8
|
import Xhr from './Xhr';
|
|
@@ -60,4 +60,4 @@ export interface GetReturn<T> {
|
|
|
60
60
|
onEnd: (cb: (res: T, params: any, xhr: Xhr<T>) => any) => any;
|
|
61
61
|
xhr: Xhr<T>;
|
|
62
62
|
}
|
|
63
|
-
export { cache, clearCache, cacheSize, cacheIds, Deferred, useAsync, useMutation, useResult, useXhr, Xhr, useSpinner, };
|
|
63
|
+
export { cache, clearCache, cacheSize, cacheIds, Deferred, useAsync, useMutation, useMutationWithError, useResult, useXhr, Xhr, useSpinner, };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
-
import{computed as e,isRef as t,onBeforeUnmount as r,ref as n,unref as o,watch as i}from"vue";var u=new Map;function a(e){var t,r,n=e||{},o=n.id,i=n.xhr,a="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(a=36e5),!u.get(o)&&i?(t=i(),u.set(o,t),t.then(function(e){return"[Log] add cache: ".concat(o),clearTimeout(r),r=window.setTimeout(function(){u.delete(o),"[Log] delete cache: ".concat(o),clearTimeout(r)},a),e},function(){u.delete(o),"[Log] delete cache: ".concat(o)})):o&&(t=u.get(o)),t)return t;throw Error("Call to undefined cache")}function c(){return u.size}function s(){return Array.from(u.keys())}function l(e){e?u.delete(e):u.forEach(function(e,t,r){r.delete(t)})}function f(e){return f="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},f(e)}function v(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,p(n.key),n)}}function d(e,t,r){return t&&v(e.prototype,t),r&&v(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function h(e,t,r){return(t=p(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function p(e){var t=function(e){if("object"!=f(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=f(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==f(t)?t:t+""}var y=d(function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),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(e,r){t.resolve=e,t.reject=r}),this.promise.finally(function(){t.done=!0})});const m="10000000-1000-4000-8000-100000000000".replace(/[018]/g,function(e){return(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16)});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){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,w(n.key),n)}}function _(e,t,r){return t&&g(e.prototype,t),r&&g(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function E(e,t,r){return(t=w(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function w(e){var t=function(e){if("object"!=b(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=b(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==b(t)?t:t+""}var j=_(function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),E(this,"value",void 0),E(this,"uuid",void 0),this.uuid=m,this.value=t});function O(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(e){return e},o=void 0===t.value?r:t.value;return t.value=void 0===o?o:new j(null==n?void 0:n(o)),i(function(){return t.value},function(e){void 0===e||e instanceof j||(t.value=new j(null==n?void 0:n(e)))}),e({get:function(){var e;return void 0===(e=t.value instanceof j?t.value.value:t.value)?r:e},set:function(e){t.value=new j(e)}})}function S(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 P=function(t,r){var u=arguments.length>2&&void 0!==arguments[2]?arguments[2]:n(!0),a=n(),c=n(),s=n(null),l=[],f=[],v=[],d=n(null),h=e(function(){return"function"==typeof r?r():o(r)}),p=n(),y=e(function(){return"function"==typeof u?u():o(u)}),b=function(e){if(!y.value)return null;var r,n;a.value&&(null===(r=d.value)||void 0===r||null===(n=r.abortXhr)||void 0===n||n.call(r)),f.forEach(function(e){return e(h.value)}),a.value=!0,s.value=null;var o,i=t,u=t;return d.value=Array.isArray(e)?u.apply(void 0,function(e){if(Array.isArray(e))return S(e)}(o=e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(o)||function(e,t){if(e){if("string"==typeof e)return S(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)?S(e,t):void 0}}(o)||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.")}()):i(e),d.value.catch(function(e){s.value=e||null,l.forEach(function(e){return e(s.value,h.value)})}),d.value.then(function(e){c.value=e,v.forEach(function(t){return t(e,h.value)})}),d.value.finally(function(){a.value=!1}),d.value},g=e(function(){return d.value});return i(function(){return y.value},function(e){!a.value&&e&&b(h.value)},{immediate:!1}),i(function(){return s.value},function(e){if(e)throw e}),i(function(){return h.value},function(e){var t=JSON.stringify(e);!a.value&&(void 0===e&&void 0===p.value||y.value&&t!==JSON.stringify(p.value))&&b(e),p.value=void 0===t?void 0:JSON.parse(t)},{immediate:y.value,deep:!0}),{isPending:a,data:e({get:function(){return c.value},set:function(e){e instanceof j&&e.uuid===m||console.warn('"useAsync" update a readonly field is not allowed'),c.value=e}}),error:s,reload:function(){return b(h.value)},onError:function(e){l.push(e)},onStart:function(e){f.push(e)},onEnd:function(e){v.push(e)},promise:g}};function R(t){var r=n(),o=n(),i=n(),u=[],a=[],c=n(null);return{mutate:function(e){for(var n=arguments.length,s=new Array(n>1?n-1:0),l=1;l<n;l++)s[l-1]=arguments[l];r.value=!0,i.value=null;var f=t;return c.value=f.apply(void 0,[e].concat(s)),c.value.catch(function(t){i.value=t||null,u.forEach(function(t){return t(i.value,s.length?[e].concat(s):e)}),i.value=t}),c.value.then(function(t){o.value=t,a.forEach(function(t){return t(o.value,s.length?[e].concat(s):e)})}),c.value.finally(function(){r.value=!1}),c.value},onError:function(e){return u.push(e)},onEnd:function(e){return a.push(e)},isPending:r,error:i,data:o,promise:e(function(){return c.value})}}function k(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 A(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?k(Object(r),!0).forEach(function(t){D(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):k(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}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=[],c=!0,s=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=i.call(r)).done)&&(a.push(n.value),a.length!==t);c=!0);}catch(e){s=!0,o=e}finally{try{if(!c&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(s)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(){var e,t,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var c=n&&n.prototype instanceof a?n:a,s=Object.create(c.prototype);return L(s,"_invoke",function(r,n,o){var i,a,c,s=0,l=o||[],f=!1,v={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(t,r){return i=t,a=0,c=e,v.n=r,u}};function d(r,n){for(a=r,c=n,t=0;!f&&s&&!o&&t<l.length;t++){var o,i=l[t],d=v.p,h=i[2];r>3?(o=h===n)&&(c=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=e):i[0]<=d&&((o=r<2&&d<i[1])?(a=0,v.v=n,v.n=i[1]):d<h&&(o=r<3||i[0]>n||n>h)&&(i[4]=r,i[5]=n,v.n=h,a=0))}if(o||r>1)return u;throw f=!0,n}return function(o,l,h){if(s>1)throw TypeError("Generator is already running");for(f&&1===l&&d(l,h),a=l,c=h;(t=a<2?e:c)||!f;){i||(a?a<3?(a>1&&(v.n=-1),d(a,c)):v.n=c:v.v=c);try{if(s=2,i){if(a||(o="next"),t=i[o]){if(!(t=t.call(i,c)))throw TypeError("iterator result is not an object");if(!t.done)return t;c=t.value,a<2&&(a=0)}else 1===a&&(t=i.return)&&t.call(i),a<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=e}else if((t=(f=v.n<0)?c:r.call(n,v))!==u)break}catch(t){i=e,a=1,c=t}finally{s=1}}return{value:t,done:f}}}(r,o,i),!0),s}var u={};function a(){}function c(){}function s(){}t=Object.getPrototypeOf;var l=[][n]?t(t([][n]())):(L(t={},n,function(){return this}),t),f=s.prototype=a.prototype=Object.create(l);function v(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,s):(e.__proto__=s,L(e,o,"GeneratorFunction")),e.prototype=Object.create(f),e}return c.prototype=s,L(f,"constructor",s),L(s,"constructor",c),c.displayName="GeneratorFunction",L(s,o,"GeneratorFunction"),L(f),L(f,o,"Generator"),L(f,n,function(){return this}),L(f,"toString",function(){return"[object Generator]"}),(H=function(){return{w:i,m:v}})()}function L(e,t,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}L=function(e,t,r,n){function i(t,r){L(e,t,function(e){return this._invoke(t,r,e)})}t?o?o(e,t,{value:r,enumerable:!n,configurable:!n,writable:!n}):e[t]=r:(i("next",0),i("throw",1),i("return",2))},L(e,t,r,n)}function N(e,t,r,n,o,i,u){try{var a=e[i](u),c=a.value}catch(e){return void r(e)}a.done?t(c):Promise.resolve(c).then(n,o)}function U(e){return U="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},U(e)}function x(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,C(n.key),n)}}function D(e,t,r){return(t=C(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function C(e){var t=function(e){if("object"!=U(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=U(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==U(t)?t:t+""}var I=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),D(this,"onError",function(){}),D(this,"onStart",function(){}),D(this,"onAbort",function(){}),D(this,"onProgress",function(){}),D(this,"onEnd",function(){}),D(this,"token",null),D(this,"url",""),D(this,"params",{}),D(this,"sendAs","json"),D(this,"timeout",1e4),D(this,"port",80),D(this,"responseType","text"),D(this,"isPending",!1),D(this,"_eventsReady",void 0),D(this,"_oXHR",void 0),D(this,"_onEnd",function(){}),D(this,"_onError",function(){}),D(this,"_deferred",void 0),D(this,"_isXhrResolved",void 0),D(this,"_isXhrRejected",void 0),D(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"===U(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 y,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(){var t,r=(t=H().m(function t(r){var o,i,u,a,c;return H().w(function(t){for(;;)switch(t.n){case 0:if(o=e.parseResult(n._oXHR),!(n._oXHR.status>=400)){t.n=3;break}return t.n=1,null===(i=(u=n._oXHR.response).text)||void 0===i?void 0:i.call(u);case 1:if(c=t.v){t.n=2;break}c=n._oXHR.response;case 2:a=c;try{a=JSON.parse(a)}catch(e){}return n._isXhrRejected=!0,n._onError(a),t.a(2,n._deferred.reject(a));case 3:return n.onEnd(o,r),n._isXhrResolved=!0,n.removeEvents(),t.a(2,n.resolve(o))}},t)}),function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function u(e){N(i,n,o,u,a,"next",e)}function a(e){N(i,n,o,u,a,"throw",e)}u(void 0)})});return function(e){return r.apply(this,arguments)}}(),this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");null!=r&&r.toLowerCase().includes("json")&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t=e,r={},n=t.search(/#/);n>-1&&(t=t.substring(0,n));var o=t.indexOf("?"),i=(!0).toString(),u=(!1).toString();return o>-1&&t.split("?")[1].split("&").reduce(function(e,t){var r=e,n=X(t.split("="),2),o=n[0],a=n[1],c=X(/^(?:"?([^"]+)"?)$/.exec(void 0===a?"":a)||[],2)[1];if(c===i||c===u)return r[o]=c===i,r;var s=Number(c);return r[o]=""===c||Number.isNaN(s)?c:s,r},r),r}},{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=t,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=e.injectParamsInUrl(r,n);r=o.url,n=o.params;var i=r.includes("?")?"&":"?",u="";return n&&!Array.isArray(n)&&"object"===U(n)?Object.getOwnPropertyNames(n).filter(function(e){return void 0!==n[e]}).forEach(function(e){u+="".concat(i).concat(e,"=").concat(encodeURIComponent(JSON.stringify(n[e]))),i="&"}):n&&(u=i+encodeURIComponent(JSON.stringify(n))),(r=r.replace(/\/:[^/]*/gi,""))+u}},{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"!==U(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(U(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 A(A({},e),{},D({},t,r[t]))},A({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach(function(t){var r=t;r=r.substring(1),void 0!==u[r]&&(n=e._stringifyForPathParam(n,r,u),delete u[r])}),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 c="?";a.forEach(function(e){if(!0===o[e]||!1===o[e])n+="".concat(c).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(c).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(c).concat(e,"=").concat(r)}c="&"})}return{url:n,params:u}}}],r&&x(t.prototype,r),n&&x(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,r,n}();function F(e){return F="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},F(e)}function J(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function G(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?J(Object(r),!0).forEach(function(t){B(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):J(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function B(e,t,r){return(t=function(e){var t=function(e){if("object"!=F(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=F(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==F(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}D(I,"onBeforeSendList",[]);var M=function(){},q=function(e){return o(e)};function z(u){var c=u||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},s=c.onError,l=c.onStart,f=c.onEnd,v=c.context,d=c.legacy,h=c.token,p=void 0===h?null:h,y=n([]),b=[],g=function(t,r,n){var i,u=n,a=e(function(){var e={},t=o(r||{});return u||(u=t.params||{}),e.params=o(u),p&&(e.token=q(p)),e.url=o(t.url),e}),c=new I,s=o(a.value.params)||{};switch(t){case"post":default:i=c.post.bind(c);break;case"put":i=c.put.bind(c);break;case"delete":i=c.delete.bind(c)}return G(G({},P(function(){return i(G(G({},a.value),{},{params:s}))})),{},{xhr:c})};return d||r(function(){y.value.forEach(function(e){e.isPending&&e.abort()}),b.forEach(function(e){return e()})}),{get:function(r,u,c){var d=u,h=new I,g=(s||M).bind(v),_=(l||M).bind(v),E=(f||M).bind(v),w=[g],O=[_],S=[E],P=n(null);y.value.push(h);var R=n(),k=n(),A="",X=0,T=e(function(){var e,n={},i=o(r);"string"==typeof i?e=i:(e=o(i.url),d||(d=i.params||{}),X=i.cacheDuration,void 0!==i.enabled&&(n.enabled=i.enabled),void 0===n.enabled&&(n.enabled=c),n.params=o(d),n.cacheDuration=X),p&&(n.token=q(p));var u=o(n.params||d||{});return"function"==typeof u&&(u=u()),n.params=G(G({},u),t(d)?d.value||{}:d),A="function"==typeof e?e(n.params):o(e),n.url=A,n}),H=function(){var e=void 0!==T.value.enabled?T.value.enabled:c;return void 0===e||(t(e)?o(e):"function"==typeof e?e():!0===e)},L=n(),N=function(){if(H()){R.value&&h.abort();var e="object"===F(T.value.params)?T.value.params:{};O.forEach(function(t){return t(e,h)}),R.value=!0,P.value=null;var t=decodeURIComponent(I.stringifyUrl(String(A),e));L.value=a({id:t,xhr:h.get.bind(h,T.value),duration:X}),L.value.catch(function(e){w.forEach(function(t){return t(e,h)}),P.value=e}),L.value.then(function(e){k.value=e}),L.value.finally(function(){var t;(t=y.value.indexOf(h))>-1&&y.value.splice(t,1),R.value=!1,S.forEach(function(t){return t(k.value,e,h)})})}};return b.push(i(function(){return T.value},N,{immediate:H(),deep:!0})),b.push(i(function(){return P.value},function(e){if(e)throw e})),{isPending:e(function(){return R.value}),data:e({get:function(){return k.value},set:function(e){e instanceof j&&e.uuid===m||console.warn('"useXhr" update a readonly field is not allowed'),k.value=e}}),onError:function(e){return w.push(e)},onStart:function(e){return O.push(e)},onEnd:function(e){return S.push(e)},error:P,abort:function(){var e;return null===(e=L.value)||void 0===e?void 0:e.abortXhr()},promise:e(function(){return L.value||new Promise(function(){})}),reload:N,xhr:h}},post:function(e,t){return g("post",e,t)},put:function(e,t){return g("put",e,t)},delete:function(e,t){return g("delete",e,t)}}}function $(t){var o,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,a=n(!1),c=0;return i(function(){return t.value},function(e){if(e&&(c=Date.now(),a.value=e),!1===e&&c>0){var t=Date.now()-c;clearTimeout(o),t>u?a.value=e:o=setTimeout(function(){a.value=e},u-t)}},{immediate:!0}),r(function(){clearTimeout(o)}),e(function(){return a.value})}export{y as Deferred,I as Xhr,a as cache,s as cacheIds,c as cacheSize,l as clearCache,P as useAsync,R as useMutation,O as useResult,$ as useSpinner,z as useXhr};
|
|
2
|
+
import{computed as e,isRef as t,onBeforeUnmount as n,ref as r,unref as o,watch as i}from"vue";var u=new Map;function a(e){var t,n,r=e||{},o=r.id,i=r.xhr,a="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(a=36e5),!u.get(o)&&i?(t=i(),u.set(o,t),t.then(function(e){return"[Log] add cache: ".concat(o),clearTimeout(n),n=window.setTimeout(function(){u.delete(o),"[Log] delete cache: ".concat(o),clearTimeout(n)},a),e},function(){u.delete(o),"[Log] delete cache: ".concat(o)})):o&&(t=u.get(o)),t)return t;throw Error("Call to undefined cache")}function s(){return u.size}function c(){return Array.from(u.keys())}function l(e){e?u.delete(e):u.forEach(function(e,t,n){n.delete(t)})}function f(e){return f="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},f(e)}function v(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,p(r.key),r)}}function d(e,t,n){return t&&v(e.prototype,t),n&&v(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function h(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function p(e){var t=function(e){if("object"!=f(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,"string");if("object"!=f(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==f(t)?t:t+""}var y=d(function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),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(e,n){t.resolve=e,t.reject=n}),this.promise.finally(function(){t.done=!0})});const m="10000000-1000-4000-8000-100000000000".replace(/[018]/g,function(e){return(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16)});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){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,w(r.key),r)}}function _(e,t,n){return t&&g(e.prototype,t),n&&g(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function E(e,t,n){return(t=w(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function w(e){var t=function(e){if("object"!=b(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,"string");if("object"!=b(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==b(t)?t:t+""}var j=_(function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),E(this,"value",void 0),E(this,"uuid",void 0),this.uuid=m,this.value=t});function O(t,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(e){return e},o=void 0===t.value?n:t.value;return t.value=void 0===o?o:new j(null==r?void 0:r(o)),i(function(){return t.value},function(e){void 0===e||e instanceof j||(t.value=new j(null==r?void 0:r(e)))}),e({get:function(){var e;return void 0===(e=t.value instanceof j?t.value.value:t.value)?n:e},set:function(e){t.value=new j(e)}})}function S(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}const P=function(t,n){var u=arguments.length>2&&void 0!==arguments[2]?arguments[2]:r(!0),a=r(),s=r(),c=r(null),l=[],f=[],v=[],d=r(null),h=e(function(){return"function"==typeof n?n():o(n)}),p=r(),y=e(function(){return"function"==typeof u?u():o(u)}),b=function(e){if(!y.value)return null;var n,r;a.value&&(null===(n=d.value)||void 0===n||null===(r=n.abortXhr)||void 0===r||r.call(n)),f.forEach(function(e){return e(h.value)}),a.value=!0,c.value=null;var o,i=t,u=t;return d.value=Array.isArray(e)?u.apply(void 0,function(e){if(Array.isArray(e))return S(e)}(o=e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(o)||function(e,t){if(e){if("string"==typeof e)return S(e,t);var n={}.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)?S(e,t):void 0}}(o)||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.")}()):i(e),d.value.catch(function(e){c.value=e||null,l.forEach(function(e){return e(c.value,h.value)})}),d.value.then(function(e){s.value=e,v.forEach(function(t){return t(e,h.value)})}),d.value.finally(function(){a.value=!1}),d.value},g=e(function(){return d.value});return i(function(){return y.value},function(e){!a.value&&e&&b(h.value)},{immediate:!1}),i(function(){return c.value},function(e){if(e)throw e}),i(function(){return h.value},function(e){var t=JSON.stringify(e);!a.value&&(void 0===e&&void 0===p.value||y.value&&t!==JSON.stringify(p.value))&&b(e),p.value=void 0===t?void 0:JSON.parse(t)},{immediate:y.value,deep:!0}),{isPending:a,data:e({get:function(){return s.value},set:function(e){e instanceof j&&e.uuid===m||console.warn('"useAsync" update a readonly field is not allowed'),s.value=e}}),error:c,reload:function(){return b(h.value)},onError:function(e){l.push(e)},onStart:function(e){f.push(e)},onEnd:function(e){v.push(e)},promise:g}};function R(t){var n=r(),o=r(),i=r(),u=[],a=[],s=r(null);return{mutate:function(e){for(var r=arguments.length,c=new Array(r>1?r-1:0),l=1;l<r;l++)c[l-1]=arguments[l];n.value=!0,i.value=null;var f=t;return s.value=f.apply(void 0,[e].concat(c)),s.value.catch(function(t){i.value=t||null,u.forEach(function(t){return t(i.value,c.length?[e].concat(c):e)}),i.value=t}),s.value.then(function(t){o.value=t,a.forEach(function(t){return t(o.value,c.length?[e].concat(c):e)})}),s.value.finally(function(){n.value=!1}),s.value},onError:function(e){return u.push(e)},onEnd:function(e){return a.push(e)},isPending:n,error:i,data:o,promise:e(function(){return s.value})}}function k(){return function(e){return R(e)}}function A(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 X(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?A(Object(n),!0).forEach(function(t){C(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):A(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function T(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,i,u,a=[],s=!0,c=!1;try{if(i=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;s=!1}else for(;!(s=(r=i.call(n)).done)&&(a.push(r.value),a.length!==t);s=!0);}catch(e){c=!0,o=e}finally{try{if(!s&&null!=n.return&&(u=n.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(e,t)||function(e,t){if(e){if("string"==typeof e)return H(e,t);var n={}.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)?H(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 H(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function L(){var e,t,n="function"==typeof Symbol?Symbol:{},r=n.iterator||"@@iterator",o=n.toStringTag||"@@toStringTag";function i(n,r,o,i){var s=r&&r.prototype instanceof a?r:a,c=Object.create(s.prototype);return U(c,"_invoke",function(n,r,o){var i,a,s,c=0,l=o||[],f=!1,v={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(t,n){return i=t,a=0,s=e,v.n=n,u}};function d(n,r){for(a=n,s=r,t=0;!f&&c&&!o&&t<l.length;t++){var o,i=l[t],d=v.p,h=i[2];n>3?(o=h===r)&&(s=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=e):i[0]<=d&&((o=n<2&&d<i[1])?(a=0,v.v=r,v.n=i[1]):d<h&&(o=n<3||i[0]>r||r>h)&&(i[4]=n,i[5]=r,v.n=h,a=0))}if(o||n>1)return u;throw f=!0,r}return function(o,l,h){if(c>1)throw TypeError("Generator is already running");for(f&&1===l&&d(l,h),a=l,s=h;(t=a<2?e:s)||!f;){i||(a?a<3?(a>1&&(v.n=-1),d(a,s)):v.n=s:v.v=s);try{if(c=2,i){if(a||(o="next"),t=i[o]){if(!(t=t.call(i,s)))throw TypeError("iterator result is not an object");if(!t.done)return t;s=t.value,a<2&&(a=0)}else 1===a&&(t=i.return)&&t.call(i),a<2&&(s=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=e}else if((t=(f=v.n<0)?s:n.call(r,v))!==u)break}catch(t){i=e,a=1,s=t}finally{c=1}}return{value:t,done:f}}}(n,o,i),!0),c}var u={};function a(){}function s(){}function c(){}t=Object.getPrototypeOf;var l=[][r]?t(t([][r]())):(U(t={},r,function(){return this}),t),f=c.prototype=a.prototype=Object.create(l);function v(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,c):(e.__proto__=c,U(e,o,"GeneratorFunction")),e.prototype=Object.create(f),e}return s.prototype=c,U(f,"constructor",c),U(c,"constructor",s),s.displayName="GeneratorFunction",U(c,o,"GeneratorFunction"),U(f),U(f,o,"Generator"),U(f,r,function(){return this}),U(f,"toString",function(){return"[object Generator]"}),(L=function(){return{w:i,m:v}})()}function U(e,t,n,r){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}U=function(e,t,n,r){function i(t,n){U(e,t,function(e){return this._invoke(t,n,e)})}t?o?o(e,t,{value:n,enumerable:!r,configurable:!r,writable:!r}):e[t]=n:(i("next",0),i("throw",1),i("return",2))},U(e,t,n,r)}function N(e,t,n,r,o,i,u){try{var a=e[i](u),s=a.value}catch(e){return void n(e)}a.done?t(s):Promise.resolve(s).then(r,o)}function x(e){return x="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},x(e)}function D(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,I(r.key),r)}}function C(e,t,n){return(t=I(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function I(e){var t=function(e){if("object"!=x(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,"string");if("object"!=x(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==x(t)?t:t+""}var F=function(){function e(t,n){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),C(this,"onError",function(){}),C(this,"onStart",function(){}),C(this,"onAbort",function(){}),C(this,"onProgress",function(){}),C(this,"onEnd",function(){}),C(this,"token",null),C(this,"url",""),C(this,"params",{}),C(this,"sendAs","json"),C(this,"timeout",1e4),C(this,"port",80),C(this,"responseType","text"),C(this,"isPending",!1),C(this,"_eventsReady",void 0),C(this,"_oXHR",void 0),C(this,"_onEnd",function(){}),C(this,"_onError",function(){}),C(this,"_deferred",void 0),C(this,"_isXhrResolved",void 0),C(this,"_isXhrRejected",void 0),C(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},n)}return t=e,n=[{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("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,n){var r=this;t&&"object"===x(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 y,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),i=o.url,u=o.params;this.url=i,this.params=u,this._onError=function(e){r.onError(e),r.removeEvents(),r._deferred.reject(e)},this._onEnd=function(){var t,n=(t=L().m(function t(n){var o,i,u,a,s;return L().w(function(t){for(;;)switch(t.n){case 0:if(o=e.parseResult(r._oXHR),!(r._oXHR.status>=400)){t.n=3;break}return t.n=1,null===(i=(u=r._oXHR.response).text)||void 0===i?void 0:i.call(u);case 1:if(s=t.v){t.n=2;break}s=r._oXHR.response;case 2:a=s;try{a=JSON.parse(a)}catch(e){}return r._isXhrRejected=!0,r._onError(a),t.a(2,r._deferred.reject(a));case 3:return r.onEnd(o,n),r._isXhrResolved=!0,r.removeEvents(),t.a(2,r.resolve(o))}},t)}),function(){var e=this,n=arguments;return new Promise(function(r,o){var i=t.apply(e,n);function u(e){N(i,r,o,u,a,"next",e)}function a(e){N(i,r,o,u,a,"throw",e)}u(void 0)})});return function(e){return n.apply(this,arguments)}}(),this._setEvents(this._oXHR)}}],r=[{key:"parseResult",value:function(e){var t=e.response;try{var n=e.getResponseHeader("Content-Type");null!=n&&n.toLowerCase().includes("json")&&(t=JSON.parse(e.response))}catch(n){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t=e,n={},r=t.search(/#/);r>-1&&(t=t.substring(0,r));var o=t.indexOf("?"),i=(!0).toString(),u=(!1).toString();return o>-1&&t.split("?")[1].split("&").reduce(function(e,t){var n=e,r=T(t.split("="),2),o=r[0],a=r[1],s=T(/^(?:"?([^"]+)"?)$/.exec(void 0===a?"":a)||[],2)[1];if(s===i||s===u)return n[o]=s===i,n;var c=Number(s);return n[o]=""===s||Number.isNaN(c)?s:c,n},n),n}},{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=t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=e.injectParamsInUrl(n,r);n=o.url,r=o.params;var i=n.includes("?")?"&":"?",u="";return r&&!Array.isArray(r)&&"object"===x(r)?Object.getOwnPropertyNames(r).filter(function(e){return void 0!==r[e]}).forEach(function(e){u+="".concat(i).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),i="&"}):r&&(u=i+encodeURIComponent(JSON.stringify(r))),(n=n.replace(/\/:[^/]*/gi,""))+u}},{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"!==x(t=e[r])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),n.append(r,t))}),n}},{key:"_stringifyForPathParam",value:function(e,t,n){var r=n[t];switch(x(n[t])){case"boolean":case"string":case"bigint":case"number":r=String(r);break;default:r=null===r?"null":encodeURIComponent(JSON.stringify(r))}return e.replace(":".concat(t),r)}},{key:"injectParamsInUrl",value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(n)||0===Object.keys(n).length)return{url:t,params:n};var r=decodeURIComponent(t),o=e.extractQueryParams(r),i=r.search(/\?|#/);i>-1&&(r=r.substring(0,i));var u=Object.getOwnPropertyNames(n).reduce(function(e,t){return X(X({},e),{},C({},t,n[t]))},X({},o));(r.match(/:[a-z0-9]+/gi)||[]).forEach(function(t){var n=t;n=n.substring(1),void 0!==u[n]&&(r=e._stringifyForPathParam(r,n,u),delete u[n])}),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])r+="".concat(s).concat(e,"=").concat(o[e]);else if(null===o[e])r+="".concat(s).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),n=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;r+="".concat(s).concat(e,"=").concat(n)}s="&"})}return{url:r,params:u}}}],n&&D(t.prototype,n),r&&D(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,n,r}();function J(e){return J="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},J(e)}function B(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 G(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?B(Object(n),!0).forEach(function(t){M(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):B(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function M(e,t,n){return(t=function(e){var t=function(e){if("object"!=J(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,"string");if("object"!=J(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==J(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}C(F,"onBeforeSendList",[]);var q=function(){},z=function(e){return o(e)};function $(u){var s=u||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},c=s.onError,l=s.onStart,f=s.onEnd,v=s.context,d=s.legacy,h=s.token,p=void 0===h?null:h,y=r([]),b=[],g=function(t,n,r){var i,u=r,a=e(function(){var e={},t=o(n||{});return u||(u=t.params||{}),e.params=o(u),p&&(e.token=z(p)),e.url=o(t.url),e}),s=new F,c=o(a.value.params)||{};switch(t){case"post":default:i=s.post.bind(s);break;case"put":i=s.put.bind(s);break;case"delete":i=s.delete.bind(s)}return G(G({},P(function(){return i(G(G({},a.value),{},{params:c}))})),{},{xhr:s})};return d||n(function(){y.value.forEach(function(e){e.isPending&&e.abort()}),b.forEach(function(e){return e()})}),{get:function(n,u,s){var d=u,h=new F,g=(c||q).bind(v),_=(l||q).bind(v),E=(f||q).bind(v),w=[g],O=[_],S=[E],P=r(null);y.value.push(h);var R=r(),k=r(),A="",X=0,T=e(function(){var e,r={},i=o(n);"string"==typeof i?e=i:(e=o(i.url),d||(d=i.params||{}),X=i.cacheDuration,void 0!==i.enabled&&(r.enabled=i.enabled),void 0===r.enabled&&(r.enabled=s),r.params=o(d),r.cacheDuration=X),p&&(r.token=z(p));var u=o(r.params||d||{});return"function"==typeof u&&(u=u()),r.params=G(G({},u),t(d)?d.value||{}:d),A="function"==typeof e?e(r.params):o(e),r.url=A,r}),H=function(){var e=void 0!==T.value.enabled?T.value.enabled:s;return void 0===e||(t(e)?o(e):"function"==typeof e?e():!0===e)},L=r(),U=function(){if(H()){R.value&&h.abort();var e="object"===J(T.value.params)?T.value.params:{};O.forEach(function(t){return t(e,h)}),R.value=!0,P.value=null;var t=decodeURIComponent(F.stringifyUrl(String(A),e));L.value=a({id:t,xhr:h.get.bind(h,T.value),duration:X}),L.value.catch(function(e){w.forEach(function(t){return t(e,h)}),P.value=e}),L.value.then(function(e){k.value=e}),L.value.finally(function(){var t;(t=y.value.indexOf(h))>-1&&y.value.splice(t,1),R.value=!1,S.forEach(function(t){return t(k.value,e,h)})})}};return b.push(i(function(){return T.value},U,{immediate:H(),deep:!0})),b.push(i(function(){return P.value},function(e){if(e)throw e})),{isPending:e(function(){return R.value}),data:e({get:function(){return k.value},set:function(e){e instanceof j&&e.uuid===m||console.warn('"useXhr" update a readonly field is not allowed'),k.value=e}}),onError:function(e){return w.push(e)},onStart:function(e){return O.push(e)},onEnd:function(e){return S.push(e)},error:P,abort:function(){var e;return null===(e=L.value)||void 0===e?void 0:e.abortXhr()},promise:e(function(){return L.value||new Promise(function(){})}),reload:U,xhr:h}},post:function(e,t){return g("post",e,t)},put:function(e,t){return g("put",e,t)},delete:function(e,t){return g("delete",e,t)}}}function Q(t){var o,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,a=r(!1),s=0;return i(function(){return t.value},function(e){if(e&&(s=Date.now(),a.value=e),!1===e&&s>0){var t=Date.now()-s;clearTimeout(o),t>u?a.value=e:o=setTimeout(function(){a.value=e},u-t)}},{immediate:!0}),n(function(){clearTimeout(o)}),e(function(){return a.value})}export{y as Deferred,F as Xhr,a as cache,c as cacheIds,s as cacheSize,l as clearCache,P as useAsync,R as useMutation,k as useMutationWithError,O as useResult,Q as useSpinner,$ as useXhr};
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import type { ComputedRef, Ref } from 'vue';
|
|
2
2
|
import type { Obj, UnwrappedPromiseType } from '.';
|
|
3
3
|
type TypeAllowed = undefined | boolean | null | string | number | Obj;
|
|
4
|
-
type OnErrorCb<Z, A extends TypeAllowed[]> = (e: null |
|
|
4
|
+
type OnErrorCb<E = Error, Z = unknown, A extends TypeAllowed[] = any> = (e: null | E, params: A extends [] ? Z : [Z, ...A]) => unknown;
|
|
5
5
|
type OnEndCb<T, Z, A extends TypeAllowed[]> = (res: T, params: A extends [] ? Z : [Z, ...A]) => unknown;
|
|
6
|
-
export default function useMutation<T, Z extends TypeAllowed, A extends TypeAllowed[]>(func: (arg: Z, ...args: A) => Promise<T>): {
|
|
6
|
+
export default function useMutation<T, Z extends TypeAllowed, A extends TypeAllowed[], E = Error>(func: (arg: Z, ...args: A) => Promise<T>): {
|
|
7
7
|
mutate: (param?: Z, ...restParams: A) => Promise<UnwrappedPromiseType<typeof func>>;
|
|
8
|
-
onError: (cb: OnErrorCb<Z, A>) => unknown;
|
|
8
|
+
onError: (cb: OnErrorCb<E, Z, A>) => unknown;
|
|
9
9
|
onEnd: (cb: OnEndCb<UnwrappedPromiseType<typeof func>, Z, A>) => unknown;
|
|
10
10
|
isPending: Ref<boolean>;
|
|
11
|
-
error: Ref<null |
|
|
11
|
+
error: Ref<null | E>;
|
|
12
12
|
data: Ref<UnwrappedPromiseType<typeof func>>;
|
|
13
13
|
promise: ComputedRef<null | Promise<T>>;
|
|
14
14
|
};
|
|
15
|
+
export declare function useMutationWithError<E>(): <T, Z extends TypeAllowed, A extends TypeAllowed[]>(func: (arg: Z, ...args: A) => Promise<T>) => {
|
|
16
|
+
mutate: (param?: Z | undefined, ...restParams: A) => Promise<T>;
|
|
17
|
+
onError: (cb: OnErrorCb<E, Z, A>) => unknown;
|
|
18
|
+
onEnd: (cb: OnEndCb<T, Z, A>) => unknown;
|
|
19
|
+
isPending: Ref<boolean>;
|
|
20
|
+
error: Ref<E | null, E | null>;
|
|
21
|
+
data: Ref<T, T>;
|
|
22
|
+
promise: ComputedRef<Promise<T> | null>;
|
|
23
|
+
};
|
|
15
24
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-use-async",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Xhr and async helpers",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,31 +31,30 @@
|
|
|
31
31
|
"@babel/plugin-proposal-decorators": "^7.28.0",
|
|
32
32
|
"@babel/preset-env": "^7.28.3",
|
|
33
33
|
"@babel/preset-typescript": "^7.27.1",
|
|
34
|
-
"@eslint/compat": "^1.
|
|
34
|
+
"@eslint/compat": "^1.4.0",
|
|
35
35
|
"@eslint/eslintrc": "^3.3.1",
|
|
36
|
-
"@eslint/js": "^9.
|
|
36
|
+
"@eslint/js": "^9.37.0",
|
|
37
37
|
"@stylistic/eslint-plugin": "^3.1.0",
|
|
38
38
|
"@types/jest": "^30.0.0",
|
|
39
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
40
|
-
"@typescript-eslint/parser": "^8.
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^8.46.1",
|
|
40
|
+
"@typescript-eslint/parser": "^8.46.1",
|
|
41
41
|
"babel-loader": "^10.0.0",
|
|
42
42
|
"babel-plugin-module-resolver": "^5.0.2",
|
|
43
|
-
"eslint": "^9.
|
|
44
|
-
"eslint-config-airbnb-extended": "^2.
|
|
43
|
+
"eslint": "^9.37.0",
|
|
44
|
+
"eslint-config-airbnb-extended": "^2.3.2",
|
|
45
45
|
"eslint-import-resolver-typescript": "^4.4.4",
|
|
46
46
|
"eslint-plugin-import-x": "^4.16.1",
|
|
47
47
|
"eslint-plugin-jest": "^29.0.1",
|
|
48
|
-
"eslint-plugin-n": "^17.
|
|
49
|
-
"globals": "^16.3.0",
|
|
48
|
+
"eslint-plugin-n": "^17.23.1",
|
|
50
49
|
"husky": "^9.1.7",
|
|
51
|
-
"jest": "^30.0
|
|
52
|
-
"jest-environment-jsdom": "^30.0
|
|
53
|
-
"lint-staged": "^16.
|
|
50
|
+
"jest": "^30.2.0",
|
|
51
|
+
"jest-environment-jsdom": "^30.2.0",
|
|
52
|
+
"lint-staged": "^16.2.4",
|
|
54
53
|
"rimraf": "^6.0.1",
|
|
55
54
|
"ts-node": "^10.9.2",
|
|
56
|
-
"typescript": "^5.9.
|
|
57
|
-
"typescript-eslint": "^8.
|
|
58
|
-
"webpack": "^5.
|
|
55
|
+
"typescript": "^5.9.3",
|
|
56
|
+
"typescript-eslint": "^8.46.1",
|
|
57
|
+
"webpack": "^5.102.1",
|
|
59
58
|
"webpack-cli": "^6.0.1"
|
|
60
59
|
},
|
|
61
60
|
"peerDependencies": {
|