vue-use-async 1.11.2 → 1.12.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Xhr.d.ts +3 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/Xhr.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Obj, XhrConfig, XhrGet } from '.';
|
|
2
2
|
import Deferred from './Deferred';
|
|
3
|
+
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
3
4
|
export default class Xhr<T> {
|
|
4
5
|
static parseResult(xhr: XMLHttpRequest): Response;
|
|
5
6
|
static extractQueryParams(url: string): Obj;
|
|
@@ -63,7 +64,7 @@ export default class Xhr<T> {
|
|
|
63
64
|
/**
|
|
64
65
|
* url with path params will be replaced by params values
|
|
65
66
|
*/
|
|
66
|
-
static injectParamsInUrl(url: string, params
|
|
67
|
+
static injectParamsInUrl(url: string, params: Obj | Array<unknown>, method?: Method): {
|
|
67
68
|
url: string;
|
|
68
69
|
params: Obj;
|
|
69
70
|
};
|
|
@@ -83,3 +84,4 @@ export default class Xhr<T> {
|
|
|
83
84
|
*/
|
|
84
85
|
_constructor(paramsObj: XhrConfig, params?: Obj): void;
|
|
85
86
|
}
|
|
87
|
+
export {};
|
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 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};
|
|
2
|
+
import{computed as t,isRef as e,onBeforeUnmount as r,ref as n,unref as o,watch as i}from"vue";var u=new Map;function a(t){var e,r,n=t||{},o=n.id,i=n.xhr,a="string"!=typeof t.duration&&t.duration||200;if("max"===t.duration&&(a=36e5),!u.get(o)&&i?(e=i(),u.set(o,e),e.then(function(t){return"[Log] add cache: ".concat(o),clearTimeout(r),r=window.setTimeout(function(){u.delete(o),"[Log] delete cache: ".concat(o),clearTimeout(r)},a),t},function(){u.delete(o),"[Log] delete cache: ".concat(o)})):o&&(e=u.get(o)),e)return e;throw Error("Call to undefined cache")}function s(){return u.size}function c(){return Array.from(u.keys())}function l(t){t?u.delete(t):u.forEach(function(t,e,r){r.delete(e)})}function f(t){return f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},f(t)}function v(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,d(n.key),n)}}function h(t,e,r){return e&&v(t.prototype,e),r&&v(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function p(t,e,r){return(e=d(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function d(t){var e=function(t){if("object"!=f(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=f(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==f(e)?e:e+""}var y=h(function t(){var e=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),p(this,"resolve",void 0),p(this,"reject",void 0),p(this,"promise",void 0),p(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 m="10000000-1000-4000-8000-100000000000".replace(/[018]/g,function(t){return(+t^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+t/4).toString(16)});function b(t){return b="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},b(t)}function g(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,j(n.key),n)}}function E(t,e,r){return e&&g(t.prototype,e),r&&g(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function _(t,e,r){return(e=j(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function j(t){var e=function(t){if("object"!=b(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=b(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==b(e)?e:e+""}var w=E(function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),_(this,"value",void 0),_(this,"uuid",void 0),this.uuid=m,this.value=e});function O(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 w(null==n?void 0:n(o)),i(function(){return e.value},function(t){void 0===t||t instanceof w||(e.value=new w(null==n?void 0:n(t)))}),t({get:function(){var t;return void 0===(t=e.value instanceof w?e.value.value:e.value)?r:t},set:function(t){e.value=new w(t)}})}function S(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 P=function(e,r){var u=arguments.length>2&&void 0!==arguments[2]?arguments[2]:n(!0),a=n(),s=n(),c=n(null),l=[],f=[],v=[],h=n(null),p=t(function(){return"function"==typeof r?r():o(r)}),d=n(),y=t(function(){return"function"==typeof u?u():o(u)}),b=function(t){if(!y.value)return null;var r,n;a.value&&(null===(r=h.value)||void 0===r||null===(n=r.abortXhr)||void 0===n||n.call(r)),f.forEach(function(t){return t(p.value)}),a.value=!0,c.value=null;var o,i=e,u=e;return h.value=Array.isArray(t)?u.apply(void 0,function(t){if(Array.isArray(t))return S(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 S(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)?S(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),h.value.catch(function(t){c.value=t||null,l.forEach(function(t){return t(c.value,p.value)})}),h.value.then(function(t){s.value=t,v.forEach(function(e){return e(t,p.value)})}),h.value.finally(function(){a.value=!1}),h.value},g=t(function(){return h.value});return i(function(){return y.value},function(t){!a.value&&t&&b(p.value)},{immediate:!1}),i(function(){return c.value},function(t){if(t)throw t}),i(function(){return p.value},function(t){var e=JSON.stringify(t);!a.value&&(void 0===t&&void 0===d.value||y.value&&e!==JSON.stringify(d.value))&&b(t),d.value=void 0===e?void 0:JSON.parse(e)},{immediate:y.value,deep:!0}),{isPending:a,data:t({get:function(){return s.value},set:function(t){t instanceof w&&t.uuid===m||console.warn('"useAsync" update a readonly field is not allowed'),s.value=t}}),error:c,reload:function(){return b(p.value)},onError:function(t){l.push(t)},onStart:function(t){f.push(t)},onEnd:function(t){v.push(t)},promise:g}};function R(e){var r=n(),o=n(),i=n(),u=[],a=[],s=n(null);return{mutate:function(t){for(var n=arguments.length,c=new Array(n>1?n-1:0),l=1;l<n;l++)c[l-1]=arguments[l];r.value=!0,i.value=null;var f=e;return s.value=f.apply(void 0,[t].concat(c)),s.value.catch(function(e){i.value=e||null,u.forEach(function(e){return e(i.value,c.length?[t].concat(c):t)}),i.value=e}),s.value.then(function(e){o.value=e,a.forEach(function(e){return e(o.value,c.length?[t].concat(c):t)})}),s.value.finally(function(){r.value=!1}),s.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 s.value})}}function k(){return function(t){return R(t)}}function A(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 X(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?A(Object(r),!0).forEach(function(e){D(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):A(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function T(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,u,a=[],s=!0,c=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;s=!1}else for(;!(s=(n=i.call(r)).done)&&(a.push(n.value),a.length!==e);s=!0);}catch(t){c=!0,o=t}finally{try{if(!s&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(t,e)||function(t,e){if(t){if("string"==typeof t)return H(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?H(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function H(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function U(){var t,e,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var s=n&&n.prototype instanceof a?n:a,c=Object.create(s.prototype);return L(c,"_invoke",function(r,n,o){var i,a,s,c=0,l=o||[],f=!1,v={p:0,n:0,v:t,a:h,f:h.bind(t,4),d:function(e,r){return i=e,a=0,s=t,v.n=r,u}};function h(r,n){for(a=r,s=n,e=0;!f&&c&&!o&&e<l.length;e++){var o,i=l[e],h=v.p,p=i[2];r>3?(o=p===n)&&(s=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=t):i[0]<=h&&((o=r<2&&h<i[1])?(a=0,v.v=n,v.n=i[1]):h<p&&(o=r<3||i[0]>n||n>p)&&(i[4]=r,i[5]=n,v.n=p,a=0))}if(o||r>1)return u;throw f=!0,n}return function(o,l,p){if(c>1)throw TypeError("Generator is already running");for(f&&1===l&&h(l,p),a=l,s=p;(e=a<2?t:s)||!f;){i||(a?a<3?(a>1&&(v.n=-1),h(a,s)):v.n=s:v.v=s);try{if(c=2,i){if(a||(o="next"),e=i[o]){if(!(e=e.call(i,s)))throw TypeError("iterator result is not an object");if(!e.done)return e;s=e.value,a<2&&(a=0)}else 1===a&&(e=i.return)&&e.call(i),a<2&&(s=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=t}else if((e=(f=v.n<0)?s:r.call(n,v))!==u)break}catch(e){i=t,a=1,s=e}finally{c=1}}return{value:e,done:f}}}(r,o,i),!0),c}var u={};function a(){}function s(){}function c(){}e=Object.getPrototypeOf;var l=[][n]?e(e([][n]())):(L(e={},n,function(){return this}),e),f=c.prototype=a.prototype=Object.create(l);function v(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,c):(t.__proto__=c,L(t,o,"GeneratorFunction")),t.prototype=Object.create(f),t}return s.prototype=c,L(f,"constructor",c),L(c,"constructor",s),s.displayName="GeneratorFunction",L(c,o,"GeneratorFunction"),L(f),L(f,o,"Generator"),L(f,n,function(){return this}),L(f,"toString",function(){return"[object Generator]"}),(U=function(){return{w:i,m:v}})()}function L(t,e,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}L=function(t,e,r,n){function i(e,r){L(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))},L(t,e,r,n)}function N(t,e,r,n,o,i,u){try{var a=t[i](u),s=a.value}catch(t){return void r(t)}a.done?e(s):Promise.resolve(s).then(n,o)}function x(t){return x="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},x(t)}function I(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,C(n.key),n)}}function D(t,e,r){return(e=C(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function C(t){var e=function(t){if("object"!=x(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=x(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==x(e)?e:e+""}var F=function(){function t(e,r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),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(e||{},r)}return e=t,r=[{key:"_getUrl",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.onBeforeSendList.reduce(function(t,e){return e(t||{})},e);return t.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var t=this,e=function(){t._oXHR.removeEventListener("load",t._onEnd,!1),t._oXHR.removeEventListener("error",t._onError,!1),t._oXHR.removeEventListener("timeout",t._onError,!1),t._oXHR.removeEventListener("loadstart",t.onStart,!1),t._oXHR.removeEventListener("abort",t.onAbort,!1),t._oXHR.upload.removeEventListener("progress",t.onProgress,!1),t._oXHR.removeEventListener("progress",t.onProgress,!1)};this._deferred.promise.then(e,e)}},{key:"post",value:function(e){this._constructor(e);var r=t.injectParamsInUrl(this.url,this.params),n=r.url,o=r.params;return this.url=n,this.params=o,this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){this._constructor(e);var r=t.injectParamsInUrl(this.url,this.params),n=r.url,o=r.params;return this.url=n,this.params=o,this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{});var r=t.injectParamsInUrl(this.url,this.params),n=r.url,o=r.params;this.url=n,this.params=o,this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var i=this._deferred.promise;return i.abortXhr=this.abort.bind(this),i}},{key:"delete",value:function(e){this._constructor(e);var r=t.injectParamsInUrl(this.url,this.params),n=r.url,o=r.params;return this.url=n,this.params=o,this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(t){return this._deferred.resolve(t),this._deferred.promise}},{key:"reject",value:function(t){return this._deferred.reject(t),this._deferred.promise}},{key:"_setEvents",value:function(t){this._eventsReady||(this._eventReady=!0,t.addEventListener("load",this._onEnd,!1),t.addEventListener("error",this._onError,!1),t.addEventListener("timeout",this._onError,!1),t.addEventListener("loadstart",this.onStart,!1),t.addEventListener("abort",this.onAbort,!1),t.upload.addEventListener("progress",this.onProgress,!1),t.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var e;this.isPending=!0,"multipart"===this.sendAs?e=t.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),e=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(e,this)}},{key:"_constructor",value:function(e,r){var n=this;e&&"object"===x(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 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,this._onError=function(t){n.onError(t),n.removeEvents(),n._deferred.reject(t)},this._onEnd=function(){var e,r=(e=U().m(function e(r){var o,i,u,a,s;return U().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(s=e.v){e.n=2;break}s=n._oXHR.response;case 2:a=s;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)}),function(){var t=this,r=arguments;return new Promise(function(n,o){var i=e.apply(t,r);function u(t){N(i,n,o,u,a,"next",t)}function a(t){N(i,n,o,u,a,"throw",t)}u(void 0)})});return function(t){return r.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=T(e.split("="),2),o=n[0],a=n[1],s=T(/^(?:"?([^"]+)"?)$/.exec(void 0===a?"":a)||[],2)[1];if(s===i||s===u)return r[o]=s===i,r;var c=Number(s);return r[o]=""===s||Number.isNaN(c)?s:c,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"===x(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"!==x(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(x(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 X(X({},t),{},D({},e,r[e]))},X({},u)),Object.keys(o).forEach(function(t){void 0!==u[t]&&(u[t]=o[t],delete o[t])});var s=Object.keys(u);if(s.length){var c="?";s.forEach(function(t){if(!0===u[t]||!1===u[t])i+="".concat(c).concat(t,"=").concat(u[t]);else if(null===u[t])i+="".concat(c).concat(t,"=").concat(u[t]);else{var e=Number(u[t]),r=Number.isNaN(e)?encodeURIComponent('"'.concat(u[t],'"')):e;i+="".concat(c).concat(t,"=").concat(r)}c="&"})}}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&&I(e.prototype,r),n&&I(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,r,n}();function J(t){return J="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},J(t)}function G(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function B(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?G(Object(r),!0).forEach(function(e){M(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):G(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function M(t,e,r){return(e=function(t){var e=function(t){if("object"!=J(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!=J(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==J(e)?e:e+""}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}D(F,"onBeforeSendList",[]);var q=function(){},z=function(t){return o(t)};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,h=s.legacy,p=s.token,d=void 0===p?null:p,y=n([]),b=[],g=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=z(d)),t.url=o(e.url),t}),s=new F,c=o(a.value.params)||{};switch(e){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 B(B({},P(function(){return i(B(B({},a.value),{},{params:c}))})),{},{xhr:s})};return h||r(function(){y.value.forEach(function(t){t.isPending&&t.abort()}),b.forEach(function(t){return t()})}),{get:function(r,u,s){var h=u,p=new F,g=(c||q).bind(v),E=(l||q).bind(v),_=(f||q).bind(v),j=[g],O=[E],S=[_],P=n(null);y.value.push(p);var R=n(),k=n(),A="",X=0,T=t(function(){var t,n={},i=o(r);"string"==typeof i?t=i:(t=o(i.url),h||(h=i.params||{}),X=i.cacheDuration,void 0!==i.enabled&&(n.enabled=i.enabled),void 0===n.enabled&&(n.enabled=s),n.params=o(h),n.cacheDuration=X),d&&(n.token=z(d));var u=o(n.params||h||{});return"function"==typeof u&&(u=u()),n.params=B(B({},u),e(h)?h.value||{}:h),A="function"==typeof t?t(n.params):o(t),n.url=A,n}),H=function(){var t=void 0!==T.value.enabled?T.value.enabled:s;return void 0===t||(e(t)?o(t):"function"==typeof t?t():!0===t)},U=n(),L=function(){if(H()){R.value&&p.abort();var t="object"===J(T.value.params)?T.value.params:{};O.forEach(function(e){return e(t,p)}),R.value=!0,P.value=null;var e=decodeURIComponent(F.stringifyUrl(String(A),t));U.value=a({id:e,xhr:p.get.bind(p,T.value),duration:X}),U.value.catch(function(t){j.forEach(function(e){return e(t,p)}),P.value=t}),U.value.then(function(t){k.value=t}),U.value.finally(function(){var e;(e=y.value.indexOf(p))>-1&&y.value.splice(e,1),R.value=!1,S.forEach(function(e){return e(k.value,t,p)})})}};return b.push(i(function(){return T.value},L,{immediate:H(),deep:!0})),b.push(i(function(){return P.value},function(t){if(t)throw t})),{isPending:t(function(){return R.value}),data:t({get:function(){return k.value},set:function(t){t instanceof w&&t.uuid===m||console.warn('"useXhr" update a readonly field is not allowed'),k.value=t}}),onError:function(t){return j.push(t)},onStart:function(t){return O.push(t)},onEnd:function(t){return S.push(t)},error:P,abort:function(){var t;return null===(t=U.value)||void 0===t?void 0:t.abortXhr()},promise:t(function(){return U.value||new Promise(function(){})}),reload:L,xhr:p}},post:function(t,e){return g("post",t,e)},put:function(t,e){return g("put",t,e)},delete:function(t,e){return g("delete",t,e)}}}function Q(e){var o,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,a=n(!1),s=0;return i(function(){return e.value},function(t){if(t&&(s=Date.now(),a.value=t),!1===t&&s>0){var e=Date.now()-s;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{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};
|