vue-use-async 1.13.4 → 1.13.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Xhr.d.ts +2 -0
- package/dist/index.js +1 -2
- package/dist/mockXhr.d.ts +11 -11
- package/package.json +20 -21
- package/dist/index.js.LICENSE.txt +0 -1
package/dist/Xhr.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ export default class Xhr<T> {
|
|
|
37
37
|
* Rewrite URL or send query parameters
|
|
38
38
|
* Return a Promise with a function to abort the xhr
|
|
39
39
|
*
|
|
40
|
+
* It's mandatory to don't have `async` in function signature, else the `abortXhr` is lost
|
|
41
|
+
*
|
|
40
42
|
* @returns {Promise}, consolidate the promise with the `abortXhr` function
|
|
41
43
|
*/
|
|
42
44
|
get(paramsObj?: XhrConfig): XhrGet<T>;
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
-
import{computed as t,isRef as e,onBeforeUnmount as r,ref as n,unref as o,watch as i}from"vue";function u(){var t,e,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var u=n&&n.prototype instanceof s?n:s,f=Object.create(u.prototype);return a(f,"_invoke",function(r,n,o){var i,u,a,s=0,f=o||[],l=!1,v={p:0,n:0,v:t,a:p,f:p.bind(t,4),d:function(e,r){return i=e,u=0,a=t,v.n=r,c}};function p(r,n){for(u=r,a=n,e=0;!l&&s&&!o&&e<f.length;e++){var o,i=f[e],p=v.p,h=i[2];r>3?(o=h===n)&&(a=i[(u=i[4])?5:(u=3,3)],i[4]=i[5]=t):i[0]<=p&&((o=r<2&&p<i[1])?(u=0,v.v=n,v.n=i[1]):p<h&&(o=r<3||i[0]>n||n>h)&&(i[4]=r,i[5]=n,v.n=h,u=0))}if(o||r>1)return c;throw l=!0,n}return function(o,f,h){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&p(f,h),u=f,a=h;(e=u<2?t:a)||!l;){i||(u?u<3?(u>1&&(v.n=-1),p(u,a)):v.n=a:v.v=a);try{if(s=2,i){if(u||(o="next"),e=i[o]){if(!(e=e.call(i,a)))throw TypeError("iterator result is not an object");if(!e.done)return e;a=e.value,u<2&&(u=0)}else 1===u&&(e=i.return)&&e.call(i),u<2&&(a=TypeError("The iterator does not provide a '"+o+"' method"),u=1);i=t}else if((e=(l=v.n<0)?a:r.call(n,v))!==c)break}catch(e){i=t,u=1,a=e}finally{s=1}}return{value:e,done:l}}}(r,o,i),!0),f}var c={};function s(){}function f(){}function l(){}e=Object.getPrototypeOf;var v=[][n]?e(e([][n]())):(a(e={},n,function(){return this}),e),p=l.prototype=s.prototype=Object.create(v);function h(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,a(t,o,"GeneratorFunction")),t.prototype=Object.create(p),t}return f.prototype=l,a(p,"constructor",l),a(l,"constructor",f),f.displayName="GeneratorFunction",a(l,o,"GeneratorFunction"),a(p),a(p,o,"Generator"),a(p,n,function(){return this}),a(p,"toString",function(){return"[object Generator]"}),(u=function(){return{w:i,m:h}})()}function a(t,e,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}a=function(t,e,r,n){function i(e,r){a(t,e,function(t){return this._invoke(e,r,t)})}e?o?o(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n}):t[e]=r:(i("next",0),i("throw",1),i("return",2))},a(t,e,r,n)}function c(t,e,r,n,o,i,u){try{var a=t[i](u),c=a.value}catch(t){return void r(t)}a.done?e(c):Promise.resolve(c).then(n,o)}var s=new Map;function f(t){return l.apply(this,arguments)}function l(){var t;return t=u().m(function t(e){var r,n,o,i,a,c;return u().w(function(t){for(;;)switch(t.n){case 0:if(n=(r=e||{}).id,o=r.xhr,i="string"!=typeof e.duration&&e.duration||200,"max"===e.duration&&(i=36e5),!s.get(n)&&o?(a=o(),s.set(n,a),a.then(function(t){return"[Log] add cache: ".concat(n),clearTimeout(c),c=window.setTimeout(function(){s.delete(n),"[Log] delete cache: ".concat(n),clearTimeout(c)},i),t},function(){s.delete(n),"[Log] delete cache: ".concat(n)})):n&&(a=s.get(n)),!a){t.n=1;break}return t.a(2,a);case 1:throw Error("Call to undefined cache");case 2:return t.a(2)}},t)}),l=function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function u(t){c(i,n,o,u,a,"next",t)}function a(t){c(i,n,o,u,a,"throw",t)}u(void 0)})},l.apply(this,arguments)}function v(){return s.size}function p(){return Array.from(s.keys())}function h(t){t?s.delete(t):s.forEach(function(t,e,r){r.delete(e)})}function d(t){return d="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},d(t)}function y(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,g(n.key),n)}}function m(t,e,r){return e&&y(t.prototype,e),r&&y(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function b(t,e,r){return(e=g(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function g(t){var e=function(t){if("object"!=d(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=d(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==d(e)?e:e+""}var w=m(function t(){var e=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),b(this,"resolve",void 0),b(this,"reject",void 0),b(this,"promise",void 0),b(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 j="10000000-1000-4000-8000-100000000000".replace(/[018]/g,function(t){return(+t^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+t/4).toString(16)});function _(t){return _="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},_(t)}function O(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 E(t,e,r){return e&&O(t.prototype,e),r&&O(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){if("object"!=_(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=_(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==_(e)?e:e+""}var k=E(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=j,this.value=e});function R(e,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(t){return t},o=void 0===e.value?r:e.value;return e.value=void 0===o?o:new k(null==n?void 0:n(o)),i(function(){return e.value},function(t){void 0===t||t instanceof k||(e.value=new k(null==n?void 0:n(t)))}),t({get:function(){var t;return void 0===(t=e.value instanceof k?e.value.value:e.value)?r:t},set:function(t){e.value=new k(t)}})}function T(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(e){for(var r=arguments.length,u=new Array(r>1?r-1:0),a=1;a<r;a++)u[a-1]=arguments[a];var c=u[0],s=u[1],f=null!=s?s:n(!0),l=n(),v=n(),p=n(null),h=[],d=[],y=[],m=n(null),b=t(function(){return"function"==typeof c?c():o(c)}),g=n(),w=t(function(){return"function"==typeof f?f():o(f)}),_=function(t){if(!w.value)return null;var r,n;l.value&&(null===(r=m.value)||void 0===r||null===(n=r.abortXhr)||void 0===n||n.call(r)),d.forEach(function(t){return t(b.value)}),l.value=!0,p.value=null;var o,i=e,u=e;return m.value=Array.isArray(t)?u.apply(void 0,function(t){if(Array.isArray(t))return T(t)}(o=t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(o)||function(t,e){if(t){if("string"==typeof t)return T(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)?T(t,e):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(t),m.value.catch(function(t){p.value=t||null,h.forEach(function(t){return t(p.value,b.value)})}),m.value.then(function(t){v.value=t,y.forEach(function(e){return e(t,b.value)})}),m.value.finally(function(){l.value=!1}),m.value},O=t(function(){return m.value});return i(function(){return w.value},function(t){!l.value&&t&&_(b.value)},{immediate:!1}),i(function(){return p.value},function(t){if(t)throw t}),i(function(){return b.value},function(t){var e=JSON.stringify(t);!l.value&&(void 0===t&&void 0===g.value||w.value&&e!==JSON.stringify(g.value))&&_(t),g.value=void 0===e?void 0:JSON.parse(e)},{immediate:w.value,deep:!0}),{isPending:l,data:t({get:function(){return v.value},set:function(t){t instanceof k&&t.uuid===j||console.warn('"useAsync" update a readonly field is not allowed'),v.value=t}}),error:p,reload:function(){return _(b.value)},onError:function(t){h.push(t)},onStart:function(t){d.push(t)},onEnd:function(t){y.push(t)},promise:O}};function X(e){var r=n(),o=n(),i=n(),u=[],a=[],c=n(null);return{mutate:function(t){for(var n=arguments.length,s=new Array(n>1?n-1:0),f=1;f<n;f++)s[f-1]=arguments[f];r.value=!0,i.value=null;var l=e;return c.value=l.apply(void 0,[t].concat(s)),c.value.catch(function(e){i.value=e||null,u.forEach(function(e){return e(i.value,s.length?[t].concat(s):t)}),i.value=e}),c.value.then(function(e){o.value=e,a.forEach(function(e){return e(o.value,s.length?[t].concat(s):t)})}),c.value.finally(function(){r.value=!1}),c.value},onError:function(t){return u.push(t)},onEnd:function(t){return a.push(t)},isPending:r,error:i,data:o,promise:t(function(){return c.value})}}function H(){return function(t){return X(t)}}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 U(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){B(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}function L(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,u,a=[],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)&&(a.push(n.value),a.length!==e);c=!0);}catch(t){s=!0,o=t}finally{try{if(!c&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(s)throw o}}return a}}(t,e)||function(t,e){if(t){if("string"==typeof t)return N(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)?N(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 N(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 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 I(){var t,e,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 D(s,"_invoke",function(r,n,o){var i,a,c,s=0,f=o||[],l=!1,v={p:0,n:0,v:t,a:p,f:p.bind(t,4),d:function(e,r){return i=e,a=0,c=t,v.n=r,u}};function p(r,n){for(a=r,c=n,e=0;!l&&s&&!o&&e<f.length;e++){var o,i=f[e],p=v.p,h=i[2];r>3?(o=h===n)&&(c=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=t):i[0]<=p&&((o=r<2&&p<i[1])?(a=0,v.v=n,v.n=i[1]):p<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 l=!0,n}return function(o,f,h){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&p(f,h),a=f,c=h;(e=a<2?t:c)||!l;){i||(a?a<3?(a>1&&(v.n=-1),p(a,c)):v.n=c:v.v=c);try{if(s=2,i){if(a||(o="next"),e=i[o]){if(!(e=e.call(i,c)))throw TypeError("iterator result is not an object");if(!e.done)return e;c=e.value,a<2&&(a=0)}else 1===a&&(e=i.return)&&e.call(i),a<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=t}else if((e=(l=v.n<0)?c:r.call(n,v))!==u)break}catch(e){i=t,a=1,c=e}finally{s=1}}return{value:e,done:l}}}(r,o,i),!0),s}var u={};function a(){}function c(){}function s(){}e=Object.getPrototypeOf;var f=[][n]?e(e([][n]())):(D(e={},n,function(){return this}),e),l=s.prototype=a.prototype=Object.create(f);function v(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,s):(t.__proto__=s,D(t,o,"GeneratorFunction")),t.prototype=Object.create(l),t}return c.prototype=s,D(l,"constructor",s),D(s,"constructor",c),c.displayName="GeneratorFunction",D(s,o,"GeneratorFunction"),D(l),D(l,o,"Generator"),D(l,n,function(){return this}),D(l,"toString",function(){return"[object Generator]"}),(I=function(){return{w:i,m:v}})()}function D(t,e,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}D=function(t,e,r,n){function i(e,r){D(t,e,function(t){return this._invoke(e,r,t)})}e?o?o(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n}):t[e]=r:(i("next",0),i("throw",1),i("return",2))},D(t,e,r,n)}function C(t,e,r,n,o,i,u){try{var a=t[i](u),c=a.value}catch(t){return void r(t)}a.done?e(c):Promise.resolve(c).then(n,o)}function F(t){return function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function u(t){C(i,n,o,u,a,"next",t)}function a(t){C(i,n,o,u,a,"throw",t)}u(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,M(n.key),n)}}function B(t,e,r){return(e=M(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function M(t){var e=function(t){if("object"!=G(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=G(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==G(e)?e:e+""}var q=function(){function t(e,r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),B(this,"onError",function(){}),B(this,"onStart",function(){}),B(this,"onAbort",function(){}),B(this,"onProgress",function(){}),B(this,"onEnd",function(){}),B(this,"token",null),B(this,"url",""),B(this,"params",{}),B(this,"sendAs","json"),B(this,"timeout",1e4),B(this,"port",80),B(this,"responseType","text"),B(this,"isPending",!1),B(this,"_eventsReady",void 0),B(this,"_oXHR",void 0),B(this,"_onEnd",function(){}),B(this,"_onError",function(){}),B(this,"_deferred",void 0),B(this,"_isXhrResolved",void 0),B(this,"_isXhrRejected",void 0),B(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(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:(f=F(I().m(function e(r){var n,o,i;return I().w(function(e){for(;;)if(0===e.n)return this._constructor(r),n=t.injectParamsInUrl(this.url,this.params),o=n.url,i=n.params,this.url=o,this.params=i,this._oXHR.open("POST",this._getUrl(),!0),this._send(),e.a(2,this._deferred.promise)},e,this)})),function(t){return f.apply(this,arguments)})},{key:"put",value:(s=F(I().m(function e(r){var n,o,i;return I().w(function(e){for(;;)if(0===e.n)return this._constructor(r),n=t.injectParamsInUrl(this.url,this.params),o=n.url,i=n.params,this.url=o,this.params=i,this._oXHR.open("PUT",this._getUrl(),!0),this._send(),e.a(2,this._deferred.promise)},e,this)})),function(t){return s.apply(this,arguments)})},{key:"get",value:(c=F(I().m(function e(r){var n,o,i,u;return I().w(function(e){for(;;)if(0===e.n)return this._constructor(r||{}),n=t.injectParamsInUrl(this.url,this.params),o=n.url,i=n.params,this.url=o,this.params=i,this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send(),(u=this._deferred.promise).abortXhr=this.abort.bind(this),e.a(2,u)},e,this)})),function(t){return c.apply(this,arguments)})},{key:"delete",value:(a=F(I().m(function e(r){var n,o,i;return I().w(function(e){for(;;)if(0===e.n)return this._constructor(r),n=t.injectParamsInUrl(this.url,this.params),o=n.url,i=n.params,this.url=o,this.params=i,this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),e.a(2,this._deferred.promise)},e,this)})),function(t){return a.apply(this,arguments)})},{key:"abort",value:(u=F(I().m(function t(){return I().w(function(t){for(;;)if(0===t.n)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"})),t.a(2,this._deferred.promise)},t,this)})),function(){return u.apply(this,arguments)})},{key:"resolve",value:(i=F(I().m(function t(e){return I().w(function(t){for(;;)if(0===t.n)return this._deferred.resolve(e),t.a(2,this._deferred.promise)},t,this)})),function(t){return i.apply(this,arguments)})},{key:"reject",value:(o=F(I().m(function t(e){return I().w(function(t){for(;;)if(0===t.n)return this._deferred.reject(e),t.a(2,this._deferred.promise)},t,this)})),function(t){return o.apply(this,arguments)})},{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"===G(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 w,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,this._onError=function(t){n.onError(t),n.removeEvents(),n._deferred.reject(t)},this._onEnd=function(){var e=F(I().m(function e(r){var o,i,u,a,c;return I().w(function(e){for(;;)switch(e.n){case 0:if(o=t.parseResult(n._oXHR),!(n._oXHR.status>=400)){e.n=3;break}return e.n=1,null===(i=(u=n._oXHR.response).text)||void 0===i?void 0:i.call(u);case 1:if(c=e.v){e.n=2;break}c=n._oXHR.response;case 2:a=c;try{a=JSON.parse(a)}catch(t){}return n._isXhrRejected=!0,n._onError(a),e.a(2,n._deferred.reject(a));case 3:return n.onEnd(o,r),n._isXhrResolved=!0,n.removeEvents(),e.a(2,n.resolve(o))}},e)}));return function(t){return e.apply(this,arguments)}}(),this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(t){var e=t.response;try{var r=t.getResponseHeader("Content-Type");null!=r&&r.toLowerCase().includes("json")&&(e=JSON.parse(t.response))}catch(r){e=t.response}return e}},{key:"extractQueryParams",value:function(t){var e=t,r={},n=e.search(/#/);n>-1&&(e=e.substring(0,n));var o=e.indexOf("?"),i=(!0).toString(),u=(!1).toString();return o>-1&&e.split("?")[1].split("&").reduce(function(t,e){var r=t,n=L(e.split("="),2),o=n[0],a=n[1],c=L(/^(?:"?([^"]+)"?)$/.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(e){t.onBeforeSendList.push(e)}},{key:"new",value:function(e){return new t(e)}},{key:"stringifyUrl",value:function(e){var r=e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=t.injectParamsInUrl(r,n);r=o.url,n=o.params;var i=r.includes("?")?"&":"?",u="";return n&&!Array.isArray(n)&&"object"===G(n)?Object.getOwnPropertyNames(n).filter(function(t){return void 0!==n[t]}).forEach(function(t){u+="".concat(i).concat(t,"=").concat(encodeURIComponent(JSON.stringify(n[t]))),i="&"}):n&&(u=i+encodeURIComponent(JSON.stringify(n))),(r=r.replace(/\/:[^/]*/gi,""))+u}},{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"!==G(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(G(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]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"GET";if(Array.isArray(r)||0===Object.keys(r).length)return{url:e,params:r};var o=r,i=e;if("GET"===n){i=decodeURIComponent(e);var u=t.extractQueryParams(i),a=i.search(/\?|#/);a>-1&&(i=i.substring(0,a)),o=Object.getOwnPropertyNames(r).reduce(function(t,e){return U(U({},t),{},B({},e,r[e]))},U({},u)),Object.keys(o).forEach(function(t){void 0!==u[t]&&(u[t]=o[t],delete o[t])});var c=Object.keys(u);if(c.length){var s="?";c.forEach(function(t){if(!0===u[t]||!1===u[t])i+="".concat(s).concat(t,"=").concat(u[t]);else if(null===u[t])i+="".concat(s).concat(t,"=").concat(u[t]);else{var e=Number(u[t]),r=Number.isNaN(e)?encodeURIComponent('"'.concat(u[t],'"')):e;i+="".concat(s).concat(t,"=").concat(r)}s="&"})}}return(i.match(/:[a-z0-9]+/gi)||[]).forEach(function(e){var r=e;r=r.substring(1),void 0!==o[r]&&(i=t._stringifyForPathParam(i,r,o),delete o[r])}),{url:i,params:o}}}],r&&J(e.prototype,r),n&&J(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,r,n,o,i,u,a,c,s,f}();function z(){var t,e,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 $(s,"_invoke",function(r,n,o){var i,a,c,s=0,f=o||[],l=!1,v={p:0,n:0,v:t,a:p,f:p.bind(t,4),d:function(e,r){return i=e,a=0,c=t,v.n=r,u}};function p(r,n){for(a=r,c=n,e=0;!l&&s&&!o&&e<f.length;e++){var o,i=f[e],p=v.p,h=i[2];r>3?(o=h===n)&&(c=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=t):i[0]<=p&&((o=r<2&&p<i[1])?(a=0,v.v=n,v.n=i[1]):p<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 l=!0,n}return function(o,f,h){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&p(f,h),a=f,c=h;(e=a<2?t:c)||!l;){i||(a?a<3?(a>1&&(v.n=-1),p(a,c)):v.n=c:v.v=c);try{if(s=2,i){if(a||(o="next"),e=i[o]){if(!(e=e.call(i,c)))throw TypeError("iterator result is not an object");if(!e.done)return e;c=e.value,a<2&&(a=0)}else 1===a&&(e=i.return)&&e.call(i),a<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=t}else if((e=(l=v.n<0)?c:r.call(n,v))!==u)break}catch(e){i=t,a=1,c=e}finally{s=1}}return{value:e,done:l}}}(r,o,i),!0),s}var u={};function a(){}function c(){}function s(){}e=Object.getPrototypeOf;var f=[][n]?e(e([][n]())):($(e={},n,function(){return this}),e),l=s.prototype=a.prototype=Object.create(f);function v(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,s):(t.__proto__=s,$(t,o,"GeneratorFunction")),t.prototype=Object.create(l),t}return c.prototype=s,$(l,"constructor",s),$(s,"constructor",c),c.displayName="GeneratorFunction",$(s,o,"GeneratorFunction"),$(l),$(l,o,"Generator"),$(l,n,function(){return this}),$(l,"toString",function(){return"[object Generator]"}),(z=function(){return{w:i,m:v}})()}function $(t,e,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}$=function(t,e,r,n){function i(e,r){$(t,e,function(t){return this._invoke(e,r,t)})}e?o?o(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n}):t[e]=r:(i("next",0),i("throw",1),i("return",2))},$(t,e,r,n)}function Q(t,e,r,n,o,i,u){try{var a=t[i](u),c=a.value}catch(t){return void r(t)}a.done?e(c):Promise.resolve(c).then(n,o)}function V(t){return function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function u(t){Q(i,n,o,u,a,"next",t)}function a(t){Q(i,n,o,u,a,"throw",t)}u(void 0)})}}function K(t){return K="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},K(t)}function W(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 Y(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?W(Object(r),!0).forEach(function(e){Z(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):W(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function Z(t,e,r){return(e=function(t){var e=function(t){if("object"!=K(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=K(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==K(e)?e:e+""}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}B(q,"onBeforeSendList",[]);var tt=function(){},et=function(t){return o(t)};function rt(u){var a=u||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},c=a.onError,s=a.onStart,l=a.onEnd,v=a.context,p=a.legacy,h=a.token,d=void 0===h?null:h,y=n([]),m=[],b=function(e,r,n){var i,u=n,a=t(function(){var t={},e=o(r||{});return u||(u=e.params||{}),t.params=o(u),d&&(t.token=et(d)),t.url=o(e.url),t}),c=new q,s=o(a.value.params)||{};switch(e){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 Y(Y({},A(V(z().m(function t(){return z().w(function(t){for(;;)if(0===t.n)return t.a(2,i(Y(Y({},a.value),{},{params:s})))},t)})))),{},{xhr:c})};return p||r(function(){y.value.forEach(function(t){t.isPending&&t.abort()}),m.forEach(function(t){return t()})}),{get:function(r,u,a){var p=u,h=new q,b=(c||tt).bind(v),g=(s||tt).bind(v),w=(l||tt).bind(v),_=[b],O=[g],E=[w],S=n(null);y.value.push(h);var P=n(),R=n(),T="",A=0,X=t(function(){var t,n={},i=o(r);"string"==typeof i?t=i:(t=o(i.url),p||(p=i.params||{}),A=i.cacheDuration,void 0!==i.enabled&&(n.enabled=i.enabled),void 0===n.enabled&&(n.enabled=a),n.params=o(p),n.cacheDuration=A),d&&(n.token=et(d));var u=o(n.params||p||{});return"function"==typeof u&&(u=u()),n.params=Y(Y({},u),e(p)?p.value||{}:p),T="function"==typeof t?t(n.params):o(t),n.url=T,n}),H=function(){var t=void 0!==X.value.enabled?X.value.enabled:a;return void 0===t||(e(t)?o(t):"function"==typeof t?t():!0===t)},x=n(),U=function(){if(H()){P.value&&h.abort();var t="object"===K(X.value.params)?X.value.params:{};O.forEach(function(e){return e(t,h)}),P.value=!0,S.value=null;var e=decodeURIComponent(q.stringifyUrl(String(T),t));x.value=f({id:e,xhr:h.get.bind(h,X.value),duration:A}),x.value.catch(function(t){_.forEach(function(e){return e(t,h)}),S.value=t}),x.value.then(function(t){R.value=t}),x.value.finally(function(){var e;(e=y.value.indexOf(h))>-1&&y.value.splice(e,1),P.value=!1,E.forEach(function(e){return e(R.value,t,h)})})}};return m.push(i(function(){return X.value},U,{immediate:H(),deep:!0})),m.push(i(function(){return S.value},function(t){if(t)throw t})),{isPending:t(function(){return P.value}),data:t({get:function(){return R.value},set:function(t){t instanceof k&&t.uuid===j||console.warn('"useXhr" update a readonly field is not allowed'),R.value=t}}),onError:function(t){return _.push(t)},onStart:function(t){return O.push(t)},onEnd:function(t){return E.push(t)},error:S,abort:function(){var t;return null===(t=x.value)||void 0===t?void 0:t.abortXhr()},promise:t(V(z().m(function t(){return z().w(function(t){for(;;)if(0===t.n)return t.a(2,x.value||new Promise(function(){}))},t)}))),reload:U,xhr:h}},post:function(t,e){return b("post",t,e)},put:function(t,e){return b("put",t,e)},delete:function(t,e){return b("delete",t,e)}}}function nt(e){var o,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,a=n(!1),c=0;return i(function(){return e.value},function(t){if(t&&(c=Date.now(),a.value=t),!1===t&&c>0){var e=Date.now()-c;clearTimeout(o),e>u?a.value=t:o=setTimeout(function(){a.value=t},u-e)}},{immediate:!0}),r(function(){clearTimeout(o)}),t(function(){return a.value})}export{w as Deferred,q as Xhr,f as cache,p as cacheIds,v as cacheSize,h as clearCache,A as useAsync,X as useMutation,H as useMutationWithError,R as useResult,nt as useSpinner,rt as useXhr};
|
|
1
|
+
import{computed as e,isRef as t,onBeforeUnmount as r,ref as s,unref as n,watch as o}from"vue";const i={log(e){},warn(e){},info(e){},error(e){},debug(e){}},a=new Map;async function u(e){const{id:t,xhr:r}=e||{};let s,n,o="string"!=typeof e.duration&&e.duration||200;if("max"===e.duration&&(o=36e5),!a.get(t)&&r?(s=r(),a.set(t,s),s.then(e=>(i.log(`[Log] add cache: ${t}`),clearTimeout(n),n=window.setTimeout(()=>{a.delete(t),i.log(`[Log] delete cache: ${t}`),clearTimeout(n)},o),e),()=>{a.delete(t),i.log(`[Log] delete cache: ${t}`)})):t&&(s=a.get(t)),s)return s;throw Error("Call to undefined cache")}function l(){return a.size}function h(){return Array.from(a.keys())}function c(e){e?a.delete(e):a.forEach((e,t,r)=>{r.delete(t)}),i.log("[Log] cache cleared")}function d(e,t,r){return(t=function(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class v{constructor(){d(this,"resolve",void 0),d(this,"reject",void 0),d(this,"promise",void 0),d(this,"done",!1),this.resolve=()=>{throw Error("Can t resolve")},this.reject=()=>{throw Error("Can t reject")},this.promise=new Promise((e,t)=>{this.resolve=e,this.reject=t}),this.promise.finally(()=>{this.done=!0})}}const p="10000000-1000-4000-8000-100000000000".replace(/[018]/g,e=>(+e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+e/4).toString(16));function f(e,t,r){return(t=function(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class m{constructor(e){f(this,"value",void 0),f(this,"uuid",void 0),this.uuid=p,this.value=e}}function y(t,r,s=e=>e){const n=void 0===t.value?r:t.value;return t.value=void 0===n?n:new m(s?.(n)),o(()=>t.value,e=>{void 0===e||e instanceof m||(t.value=new m(s?.(e)))}),e({get:()=>{let e;return e=t.value instanceof m?t.value.value:t.value,void 0===e?r:e},set:e=>{t.value=new m(e)}})}const g=(t,...[r,i])=>{const a=i??s(!0),u=s(),l=s(),h=s(null),c=[],d=[],v=[],f=s(null),y=e(()=>"function"==typeof r?r():n(r)),g=s(),b=e(()=>"function"==typeof a?a():n(a)),_=e=>{if(!b.value)return null;u.value&&f.value?.abortXhr?.(),d.forEach(e=>e(y.value)),u.value=!0,h.value=null;const r=t,s=t;return f.value=Array.isArray(e)?s(...e):r(e),f.value.catch(e=>{h.value=e||null,c.forEach(e=>e(h.value,y.value))}),f.value.then(e=>{l.value=e,v.forEach(t=>t(e,y.value))}),f.value.finally(()=>{u.value=!1}),f.value},E=e(()=>f.value);return o(()=>b.value,e=>{!u.value&&e&&_(y.value)},{immediate:!1}),o(()=>h.value,e=>{if(e)throw e}),o(()=>y.value,e=>{const t=JSON.stringify(e);!u.value&&(void 0===e&&void 0===g.value||b.value&&t!==JSON.stringify(g.value))&&_(e),g.value=void 0===t?void 0:JSON.parse(t)},{immediate:b.value,deep:!0}),{isPending:u,data:e({get:()=>l.value,set:e=>{e instanceof m&&e.uuid===p||console.warn('"useAsync" update a readonly field is not allowed'),l.value=e}}),error:h,reload:()=>_(y.value),onError:e=>{c.push(e)},onStart:e=>{d.push(e)},onEnd:e=>{v.push(e)},promise:E}};function b(t){const r=s(),n=s(),o=s(),i=[],a=[],u=s(null);return{mutate:(e,...s)=>{r.value=!0,o.value=null;const l=t;return u.value=l(e,...s),u.value.catch(t=>{o.value=t||null,i.forEach(t=>t(o.value,s.length?[e,...s]:e)),o.value=t}),u.value.then(t=>{n.value=t,a.forEach(t=>t(n.value,s.length?[e,...s]:e))}),u.value.finally(()=>{r.value=!1}),u.value},onError:e=>i.push(e),onEnd:e=>a.push(e),isPending:r,error:o,data:n,promise:e(()=>u.value)}}function _(){return function(e){return b(e)}}function E(e,t,r){return(t=function(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class R{static parseResult(e){let t=e.response;try{const r=e.getResponseHeader("Content-Type");r?.toLowerCase().includes("json")&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}static extractQueryParams(e){let t=e;const r={},s=t.search(/#/);s>-1&&(t=t.substring(0,s));const n=t.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&t.split("?")[1].split("&").reduce((e,t)=>{const r=e,[s,n=""]=t.split("="),[,a]=/^(?:"?([^"]+)"?)$/.exec(n)||[];if(a===o||a===i)return r[s]=a===o,r;const u=Number(a);return r[s]=""===a||Number.isNaN(u)?a:u,r},r),r}static onBeforeSend(e){R.onBeforeSendList.push(e)}constructor(e,t){E(this,"onError",()=>{}),E(this,"onStart",()=>{}),E(this,"onAbort",()=>{}),E(this,"onProgress",()=>{}),E(this,"onEnd",()=>{}),E(this,"token",null),E(this,"url",""),E(this,"params",{}),E(this,"sendAs","json"),E(this,"timeout",1e4),E(this,"port",80),E(this,"responseType","text"),E(this,"isPending",!1),E(this,"_eventsReady",void 0),E(this,"_oXHR",void 0),E(this,"_onEnd",()=>{}),E(this,"_onError",()=>{}),E(this,"_deferred",void 0),E(this,"_isXhrResolved",void 0),E(this,"_isXhrRejected",void 0),E(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(e||{},t)}static new(e){return new R(e)}_getUrl(e={}){const t=R.onBeforeSendList.reduce((e,t)=>t(e||{}),e);return R.stringifyUrl(this.url,t)}removeEvents(){const e=()=>{this._oXHR.removeEventListener("load",this._onEnd,!1),this._oXHR.removeEventListener("error",this._onError,!1),this._oXHR.removeEventListener("timeout",this._onError,!1),this._oXHR.removeEventListener("loadstart",this.onStart,!1),this._oXHR.removeEventListener("abort",this.onAbort,!1),this._oXHR.upload.removeEventListener("progress",this.onProgress,!1),this._oXHR.removeEventListener("progress",this.onProgress,!1)};this._deferred.promise.then(e,e)}async post(e){this._constructor(e);const{url:t,params:r}=R.injectParamsInUrl(this.url,this.params);return this.url=t,this.params=r,this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}async put(e){this._constructor(e);const{url:t,params:r}=R.injectParamsInUrl(this.url,this.params);return this.url=t,this.params=r,this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}get(e){this._constructor(e||{});const{url:t,params:r}=R.injectParamsInUrl(this.url,this.params);this.url=t,this.params=r,this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();const s=this._deferred.promise;return s.abortXhr=this.abort.bind(this),s}async delete(e){this._constructor(e);const{url:t,params:r}=R.injectParamsInUrl(this.url,this.params);return this.url=t,this.params=r,this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}async abort(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:`Xhr aborted: ${this.url}`,name:"HTTP-ABORTED"})),this._deferred.promise}static stringifyUrl(e,t={}){let r=e,s=t;({url:r,params:s}=R.injectParamsInUrl(r,s));let n=r.includes("?")?"&":"?",o="";return s&&!Array.isArray(s)&&"object"==typeof s?Object.getOwnPropertyNames(s).filter(e=>void 0!==s[e]).forEach(e=>{o+=`${n}${e}=${encodeURIComponent(JSON.stringify(s[e]))}`,n="&"}):s&&(o=n+encodeURIComponent(JSON.stringify(s))),r=r.replace(/\/:[^/]*/gi,""),r+o}static getFormData(e){const t=new FormData;let r;return Object.keys(e).forEach(s=>{if(e[s]instanceof FileList)for(let r=0;r<e[s].length;r+=1)t.append(s,e[s][r]);else e[s]instanceof File?t.append(s,e[s]):(r=e[s],"object"!=typeof r&&!Array.isArray(r)||null===r||(r=JSON.stringify(r)),t.append(s,r))}),t}async resolve(e){return this._deferred.resolve(e),this._deferred.promise}async reject(e){return this._deferred.reject(e),this._deferred.promise}static _stringifyForPathParam(e,t,r){let s=r[t];switch(typeof r[t]){case"boolean":case"string":case"bigint":case"number":s=String(s);break;default:s=null===s?"null":encodeURIComponent(JSON.stringify(s))}return e.replace(`:${t}`,s)}static injectParamsInUrl(e,t={},r="GET"){if(Array.isArray(t)||0===Object.keys(t).length)return{url:e,params:t};let s=t,n=e;if("GET"===r){n=decodeURIComponent(e);const r=R.extractQueryParams(n),o=n.search(/\?|#/);o>-1&&(n=n.substring(0,o)),s=Object.getOwnPropertyNames(t).reduce((e,r)=>({...e,[r]:t[r]}),{...r}),Object.keys(s).forEach(e=>{void 0!==r[e]&&(r[e]=s[e],delete s[e])});const i=Object.keys(r);if(i.length){let e="?";i.forEach(t=>{if(!0===r[t]||!1===r[t])n+=`${e}${t}=${r[t]}`;else if(null===r[t])n+=`${e}${t}=${r[t]}`;else{const s=Number(r[t]),o=Number.isNaN(s)?encodeURIComponent(`"${r[t]}"`):s;n+=`${e}${t}=${o}`}e="&"})}}return(n.match(/:[a-z0-9]+/gi)||[]).forEach(e=>{let t=e;t=t.substring(1),void 0!==s[t]&&(n=R._stringifyForPathParam(n,t,s),delete s[t])}),{url:n,params:s}}_setEvents(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))}_send(){let e;this.isPending=!0,"multipart"===this.sendAs?e=R.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 ${this.token}`),this._oXHR.send(e,this)}_constructor(e,t){e&&"object"==typeof 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=t||this.params),80!==this.port&&(this.url=`${window.location.protocol}//${window.location.hostname}:${this.port}${this.url}`),this._deferred=new v,this._deferred.promise.finally(()=>{this.isPending=!1}),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1,this._onError=e=>{this.onError(e),this.removeEvents(),this._deferred.reject(e)},this._onEnd=async e=>{const t=R.parseResult(this._oXHR);if(this._oXHR.status>=400){let e=await(this._oXHR.response.text?.())||this._oXHR.response;try{e=JSON.parse(e)}catch(e){}return this._isXhrRejected=!0,this._onError(e),this._deferred.reject(e)}return this.onEnd(t,e),this._isXhrResolved=!0,this.removeEvents(),this.resolve(t)},this._setEvents(this._oXHR)}}E(R,"onBeforeSendList",[]);const w=()=>{},j=e=>n(e);function P(i){const{onError:a,onStart:l,onEnd:h,context:c,legacy:d,token:v=null}=i||{onError:()=>{},onStart:()=>{},onEnd:()=>{},context:null,legacy:!1,token:null},f=s([]),y=[],b=(t,r,s)=>{let o=s;const i=e(()=>{const e={},t=n(r||{});return o||(o=t.params||{}),e.params=n(o),v&&(e.token=j(v)),e.url=n(t.url),e}),a=new R,u=n(i.value.params)||{};let l;switch(t){case"post":default:l=a.post.bind(a);break;case"put":l=a.put.bind(a);break;case"delete":l=a.delete.bind(a)}return{...g(async()=>l({...i.value,params:u})),xhr:a}};return d||r(()=>{f.value.forEach(e=>{e.isPending&&e.abort()}),y.forEach(e=>e())}),{get:function(r,i,d){let g=i;const b=new R,_=(a||w).bind(c),E=(l||w).bind(c),P=(h||w).bind(c),X=[_],S=[E],H=[P],A=s(null);f.value.push(b);const L=s(),O=s();let T="",$=0;const U=e(()=>{const e={},s=n(r);let o;"string"==typeof s?o=s:(o=n(s.url),g||(g=s.params||{}),$=s.cacheDuration,void 0!==s.enabled&&(e.enabled=s.enabled),void 0===e.enabled&&(e.enabled=d),e.params=n(g),e.cacheDuration=$),v&&(e.token=j(v));let i=n(e.params||g||{});return"function"==typeof i&&(i=i()),e.params={...i,...t(g)?g.value||{}:g},T="function"==typeof o?o(e.params):n(o),e.url=T,e}),N=()=>{const e=void 0!==U.value.enabled?U.value.enabled:d;return void 0===e||(t(e)?n(e):"function"==typeof e?e():!0===e)},k=s(),x=()=>{if(!N())return;L.value&&b.abort();const e="object"==typeof U.value.params?U.value.params:{};S.forEach(t=>t(e,b)),L.value=!0,A.value=null;const t=decodeURIComponent(R.stringifyUrl(String(T),e));k.value=u({id:t,xhr:b.get.bind(b,U.value),duration:$}),k.value.catch(e=>{X.forEach(t=>t(e,b)),A.value=e}),k.value.then(e=>{O.value=e}),k.value.finally(()=>{(()=>{const e=f.value.indexOf(b);e>-1&&f.value.splice(e,1)})(),L.value=!1,H.forEach(t=>t(O.value,e,b))})};return y.push(o(()=>U.value,x,{immediate:N(),deep:!0})),y.push(o(()=>A.value,e=>{if(e)throw e})),{isPending:e(()=>L.value),data:e({get:()=>O.value,set:e=>{e instanceof m&&e.uuid===p||console.warn('"useXhr" update a readonly field is not allowed'),O.value=e}}),onError:e=>X.push(e),onStart:e=>S.push(e),onEnd:e=>H.push(e),error:A,abort:()=>k.value?.abortXhr(),promise:e(async()=>k.value||new Promise(()=>{})),reload:x,xhr:b}},post:(e,t)=>b("post",e,t),put:(e,t)=>b("put",e,t),delete:(e,t)=>b("delete",e,t)}}function X(t,n=400){const i=s(!1);let a,u=0;return o(()=>t.value,e=>{if(e&&(u=Date.now(),i.value=e),!1===e&&u>0){const t=Date.now()-u;clearTimeout(a),t>n?i.value=e:a=setTimeout(()=>{i.value=e},n-t)}},{immediate:!0}),r(()=>{clearTimeout(a)}),e(()=>i.value)}export{v as Deferred,R as Xhr,u as cache,h as cacheIds,l as cacheSize,c as clearCache,g as useAsync,b as useMutation,_ as useMutationWithError,y as useResult,X as useSpinner,P as useXhr};
|
package/dist/mockXhr.d.ts
CHANGED
|
@@ -13,31 +13,31 @@ declare class MockXhr {
|
|
|
13
13
|
constructor();
|
|
14
14
|
get(condition: Condition): {
|
|
15
15
|
context: MockXhr;
|
|
16
|
-
|
|
16
|
+
abortXhr: () => void;
|
|
17
17
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
18
18
|
reject: (fakeResult: any, delay?: number) => void;
|
|
19
19
|
};
|
|
20
20
|
post(condition: Condition): {
|
|
21
21
|
context: MockXhr;
|
|
22
|
-
|
|
22
|
+
abortXhr: () => void;
|
|
23
23
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
24
24
|
reject: (fakeResult: any, delay?: number) => void;
|
|
25
25
|
};
|
|
26
26
|
delete(condition: Condition): {
|
|
27
27
|
context: MockXhr;
|
|
28
|
-
|
|
28
|
+
abortXhr: () => void;
|
|
29
29
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
30
30
|
reject: (fakeResult: any, delay?: number) => void;
|
|
31
31
|
};
|
|
32
32
|
put(condition: Condition): {
|
|
33
33
|
context: MockXhr;
|
|
34
|
-
|
|
34
|
+
abortXhr: () => void;
|
|
35
35
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
36
36
|
reject: (fakeResult: any, delay?: number) => void;
|
|
37
37
|
};
|
|
38
38
|
sendForm(form: Condition): {
|
|
39
39
|
context: MockXhr;
|
|
40
|
-
|
|
40
|
+
abortXhr: () => void;
|
|
41
41
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
42
42
|
reject: (fakeResult: any, delay?: number) => void;
|
|
43
43
|
};
|
|
@@ -47,7 +47,7 @@ declare class MockXhr {
|
|
|
47
47
|
static restore(): void;
|
|
48
48
|
_mockXMLHttpRequest(this: MockXhr, condition: Condition): {
|
|
49
49
|
context: MockXhr;
|
|
50
|
-
|
|
50
|
+
abortXhr: () => void;
|
|
51
51
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
52
52
|
reject: (fakeResult: any, delay?: number) => void;
|
|
53
53
|
};
|
|
@@ -55,31 +55,31 @@ declare class MockXhr {
|
|
|
55
55
|
export default function mockXhr(): {
|
|
56
56
|
get(condition: Condition): {
|
|
57
57
|
context: MockXhr;
|
|
58
|
-
|
|
58
|
+
abortXhr: () => void;
|
|
59
59
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
60
60
|
reject: (fakeResult: any, delay?: number) => void;
|
|
61
61
|
};
|
|
62
62
|
post(condition: Condition): {
|
|
63
63
|
context: MockXhr;
|
|
64
|
-
|
|
64
|
+
abortXhr: () => void;
|
|
65
65
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
66
66
|
reject: (fakeResult: any, delay?: number) => void;
|
|
67
67
|
};
|
|
68
68
|
delete(condition: Condition): {
|
|
69
69
|
context: MockXhr;
|
|
70
|
-
|
|
70
|
+
abortXhr: () => void;
|
|
71
71
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
72
72
|
reject: (fakeResult: any, delay?: number) => void;
|
|
73
73
|
};
|
|
74
74
|
put(condition: Condition): {
|
|
75
75
|
context: MockXhr;
|
|
76
|
-
|
|
76
|
+
abortXhr: () => void;
|
|
77
77
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
78
78
|
reject: (fakeResult: any, delay?: number) => void;
|
|
79
79
|
};
|
|
80
80
|
sendForm(condition: Condition): {
|
|
81
81
|
context: MockXhr;
|
|
82
|
-
|
|
82
|
+
abortXhr: () => void;
|
|
83
83
|
resolve: (fakeResult: any, delay?: number) => void;
|
|
84
84
|
reject: (fakeResult: any, delay?: number) => void;
|
|
85
85
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-use-async",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.6",
|
|
4
4
|
"description": "Xhr and async helpers",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,38 +31,37 @@
|
|
|
31
31
|
"@jest/globals": "^30.4.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@babel/plugin-
|
|
35
|
-
"@babel/
|
|
36
|
-
"@babel/preset-
|
|
37
|
-
"@babel/preset-typescript": "^7.29.7",
|
|
34
|
+
"@babel/plugin-transform-class-properties": "^8.0.1",
|
|
35
|
+
"@babel/preset-env": "^8.0.2",
|
|
36
|
+
"@babel/preset-typescript": "^8.0.1",
|
|
38
37
|
"@eslint/compat": "^2.1.0",
|
|
39
|
-
"@eslint/eslintrc": "^3.3.
|
|
40
|
-
"@eslint/js": "^9.39.
|
|
38
|
+
"@eslint/eslintrc": "^3.3.6",
|
|
39
|
+
"@eslint/js": "^9.39.5",
|
|
41
40
|
"@stylistic/eslint-plugin": "^5.10.0",
|
|
42
|
-
"@tcy/eslint-rules": "^1.6.
|
|
41
|
+
"@tcy/eslint-rules": "^1.6.4",
|
|
43
42
|
"@types/jest": "^30.0.0",
|
|
44
|
-
"@types/node": "^
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
46
|
-
"@typescript-eslint/parser": "^8.
|
|
47
|
-
"@typescript/native-preview": "^7.0.0-dev.
|
|
43
|
+
"@types/node": "^26.1.1",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^8.63.0",
|
|
45
|
+
"@typescript-eslint/parser": "^8.63.0",
|
|
46
|
+
"@typescript/native-preview": "^7.0.0-dev.20260707.2",
|
|
48
47
|
"babel-loader": "^10.1.1",
|
|
49
48
|
"babel-plugin-module-resolver": "^5.0.3",
|
|
50
49
|
"eslint": "^9.39.2",
|
|
51
|
-
"eslint-config-airbnb-extended": "^3.1.
|
|
52
|
-
"eslint-import-resolver-typescript": "^4.4.
|
|
53
|
-
"eslint-plugin-import-x": "^4.
|
|
54
|
-
"eslint-plugin-jest": "^29.15.
|
|
55
|
-
"eslint-plugin-n": "^18.
|
|
50
|
+
"eslint-config-airbnb-extended": "^3.1.1",
|
|
51
|
+
"eslint-import-resolver-typescript": "^4.4.5",
|
|
52
|
+
"eslint-plugin-import-x": "^4.17.1",
|
|
53
|
+
"eslint-plugin-jest": "^29.15.4",
|
|
54
|
+
"eslint-plugin-n": "^18.2.2",
|
|
56
55
|
"husky": "^9.1.7",
|
|
57
56
|
"jest": "^30.4.2",
|
|
58
57
|
"jest-environment-jsdom": "^30.4.1",
|
|
59
|
-
"lint-staged": "^17.0.
|
|
58
|
+
"lint-staged": "^17.0.8",
|
|
60
59
|
"rimraf": "^6.1.3",
|
|
61
60
|
"ts-node": "^10.9.2",
|
|
62
61
|
"typescript": "^6.0.3",
|
|
63
|
-
"typescript-eslint": "^8.
|
|
64
|
-
"webpack": "^5.
|
|
65
|
-
"webpack-cli": "^7.
|
|
62
|
+
"typescript-eslint": "^8.63.0",
|
|
63
|
+
"webpack": "^5.108.4",
|
|
64
|
+
"webpack-cli": "^7.2.1"
|
|
66
65
|
},
|
|
67
66
|
"peerDependencies": {
|
|
68
67
|
"vue": "*"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|