vue-use-async 1.7.2 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Xhr.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/tests.js +1 -1
- package/package.json +13 -13
package/dist/Xhr.d.ts
CHANGED
|
@@ -63,7 +63,7 @@ export default class Xhr<T> {
|
|
|
63
63
|
/**
|
|
64
64
|
* url with path params will be replaced by params values
|
|
65
65
|
*/
|
|
66
|
-
static
|
|
66
|
+
static injectParamsInUrl(url: string, params?: Obj | Array<unknown>): {
|
|
67
67
|
url: string;
|
|
68
68
|
params: Obj;
|
|
69
69
|
};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("vue"));else if("function"==typeof define&&define.amd)define(["vue"],t);else{var r="object"==typeof exports?t(require("vue")):t(e.vue);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(e=>(()=>{"use strict";var t={748:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var i=r[e]={exports:{}};return t[e](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{Deferred:()=>d,Xhr:()=>P,cache:()=>r,cacheIds:()=>u,cacheSize:()=>i,clearCache:()=>a,useAsync:()=>y,useMutation:()=>m,useResult:()=>b,useSpinner:()=>L,useXhr:()=>x});const e={log:function(e){},warn:function(e){},info:function(e){},error:function(e){},debug:function(e){}};var t=new Map;function r(r){var n,o,i=r||{},u=i.id,a=i.xhr,s="string"!=typeof r.duration&&r.duration||200;if("max"===r.duration&&(s=36e5),!t.get(u)&&a?(n=a(),t.set(u,n),n.then((function(r){return e.log("[Log] add cache: ".concat(u)),clearTimeout(o),o=window.setTimeout((function(){t.delete(u),e.log("[Log] delete cache: ".concat(u)),clearTimeout(o)}),s),r}),(function(){return t.delete(u),e.log("[Log] delete cache: ".concat(u))}))):u&&(n=t.get(u)),n)return n;throw Error("Call to undefined cache")}function i(){return t.size}function u(){return Array.from(t.keys())}function a(r){r?t.delete(r):t.forEach((function(e,t,r){r.delete(t)})),e.log("[Log] cache cleared")}function s(e){return s="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},s(e)}function c(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,v(n.key),n)}}function l(e,t,r){return t&&c(e.prototype,t),r&&c(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function f(e,t,r){return(t=v(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function v(e){var t=function(e,t){if("object"!==s(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==s(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===s(t)?t:String(t)}var d=l((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),f(this,"resolve",void 0),f(this,"reject",void 0),f(this,"promise",void 0),f(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r})),this.promise.finally((function(){t.done=!0}))})),h=n(748);function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function y(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,h.ref)(!0),n=(0,h.ref)(),o=(0,h.ref)(),i=(0,h.ref)(null),u=[],a=[],s=[],c=(0,h.ref)(null),l=(0,h.computed)((function(){return"function"==typeof t?t():(0,h.unref)(t)})),f=(0,h.ref)(),v=(0,h.computed)((function(){return"function"==typeof r?r():(0,h.unref)(r)})),d=function(t){a.forEach((function(e){return e(l.value)})),n.value=!0,i.value=null;var r,f=e,v=e;return c.value=Array.isArray(t)?v.apply(void 0,function(e){if(Array.isArray(e))return p(e)}(r=t)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(r)||function(e,t){if(e){if("string"==typeof e)return p(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?p(e,t):void 0}}(r)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()):f(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,l.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,l.value)}))})),c.value.finally((function(){n.value=!1})),c.value};return(0,h.watch)((function(){return v.value}),(function(e){!n.value&&e&&d(l.value)}),{immediate:!1}),(0,h.watch)((function(){return i.value}),(function(e){if(e)throw e})),(0,h.watch)((function(){return l.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===f.value||v.value&&t!==JSON.stringify(f.value))&&d(e),f.value=void 0===t?void 0:JSON.parse(t)}),{immediate:v.value,deep:!0}),{isPending:n,data:o,error:i,reload:function(){return d(l.value)},onError:function(e){return u.push(e)},onStart:function(e){return a.push(e)},onEnd:function(e){return s.push(e)},promise:(0,h.computed)((function(){return c.value}))}}function m(e){var t=(0,h.ref)(),r=(0,h.ref)(),n=(0,h.ref)(),o=[],i=[],u=(0,h.ref)(null);return{mutate:function(a){for(var s=arguments.length,c=new Array(s>1?s-1:0),l=1;l<s;l++)c[l-1]=arguments[l];t.value=!0,n.value=null;var f=e;return u.value=f.apply(void 0,[a].concat(c)),u.value.catch((function(e){n.value=e||null,o.forEach((function(e){return e(n.value,c.length?[a].concat(c):a)})),n.value=e})),u.value.then((function(e){r.value=e,i.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)}))})),u.value.finally((function(){t.value=!1})),u.value},onError:function(e){return o.push(e)},onEnd:function(e){return i.push(e)},isPending:t,error:n,data:r,promise:(0,h.computed)((function(){return u.value}))}}function b(e,t,r){var n=(0,h.ref)(t);return r||(r=function(e){return e}),t&&(n.value=r(t)),(0,h.watchEffect)((function(){if(e){var t,o=(0,h.unref)(e);o&&(n.value=null===(t=r)||void 0===t?void 0:t(o))}})),n}function g(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function _(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,u,a=[],s=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;s=!1}else for(;!(s=(n=i.call(r)).done)&&(a.push(n.value),a.length!==t);s=!0);}catch(e){c=!0,o=e}finally{try{if(!s&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(e,t)||function(e,t){if(e){if("string"==typeof e)return E(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?E(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 E(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function 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 O(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,w(n.key),n)}}function S(e,t,r){return(t=w(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function w(e){var t=function(e,t){if("object"!==j(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.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:String(t)}var P=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),S(this,"onError",(function(){})),S(this,"onStart",(function(){})),S(this,"onAbort",(function(){})),S(this,"onProgress",(function(){})),S(this,"onEnd",(function(){})),S(this,"token",null),S(this,"url",""),S(this,"params",{}),S(this,"sendAs","json"),S(this,"timeout",1e4),S(this,"port",80),S(this,"responseType","text"),S(this,"isPending",!1),S(this,"_eventsReady",void 0),S(this,"_oXHR",void 0),S(this,"_onEnd",(function(){})),S(this,"_onError",(function(){})),S(this,"_deferred",void 0),S(this,"_isXhrResolved",void 0),S(this,"_isXhrRejected",void 0),S(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}var t,r,n;return t=e,r=[{key:"_getUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e._onError,!1),e._oXHR.removeEventListener("timeout",e._onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var t=this._deferred.promise;return t.abortXhr=this.abort.bind(this),t}},{key:"delete",value:function(e){return this._constructor(e),this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this._onError,!1),e.addEventListener("timeout",this._onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var t;this.isPending=!0,"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===j(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).bind(this),this.onStart=(t.onStart||this.onStart).bind(this),this.onAbort=(t.onAbort||this.onAbort).bind(this),this.onEnd=(t.onEnd||this.onEnd).bind(this),this.onError=(t.onError||this.onError).bind(this),this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new d,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e._injectParamsInUrl(this.url,this.params),i=o.url,u=o.params;this.url=i,this.params=u,this._onError=function(e){n.onError(e),n.removeEvents(),n._deferred.reject(e)},this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t={},r=e.search(/#/);r>-1&&(e=e.substring(0,r));var n=e.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&e.split("?")[1].split("&").reduce((function(e,t){var r=_(t.split("="),2),n=r[0],u=r[1],a=_((void 0===u?"":u).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(a===o||a===i)e[n]=a===o;else{var s=Number(a);e[n]=""===a||Number.isNaN(s)?a:s}return e}),t),t}},{key:"onBeforeSend",value:function(t){e.onBeforeSendList.push(t)}},{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e._injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===j(r)?Object.getOwnPropertyNames(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==j(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_stringifyForPathParam",value:function(e,t,r){var n=r[t];switch(j(r[t])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return e.replace(":".concat(t),n)}},{key:"_injectParamsInUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:t,params:r};var n=decodeURIComponent(t),o=e.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var u=Object.getOwnPropertyNames(r).reduce((function(e,t){return e[t]=r[t],e}),function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?g(Object(r),!0).forEach((function(t){S(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):g(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substring(1),void 0!==u[t]&&(n=e._stringifyForPathParam(n,t,u),delete u[t])})),Object.keys(u).forEach((function(e){void 0!==o[e]&&(o[e]=u[e],delete u[e])}));var a=Object.keys(o);if(a.length){var s="?";a.forEach((function(e){if(!0===o[e]||!1===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(s).concat(e,"=").concat(r)}s="&"}))}return{url:n,params:u}}}],r&&O(t.prototype,r),n&&O(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function R(e){return R="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},R(e)}function k(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function A(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?k(Object(r),!0).forEach((function(t){X(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):k(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function X(e,t,r){return(t=function(e){var t=function(e,t){if("object"!==R(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==R(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===R(t)?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}S(P,"onBeforeSendList",[]);var T=function(){},H=function(e){return(0,h.unref)(e)};function x(e){var t=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},n=t.onError,o=t.onStart,i=t.onEnd,u=t.context,a=t.legacy,s=t.token,c=void 0===s?null:s,l=(0,h.ref)([]),f=[],v=function(e,t,r){var n,o=(0,h.computed)((function(){var e={},n=(0,h.unref)(t||{});r||(r=n.params||{}),e.params=(0,h.unref)(r),c&&(e.token=H(c));var o=(0,h.unref)(e.params||r||{});return"function"==typeof o&&(o=o()),e.url=(0,h.unref)(n.url),e})),i=new P,u=(0,h.unref)(o.value.params)||{};switch(e){case"post":default:n=i.post.bind(i);break;case"put":n=i.put.bind(i);break;case"delete":n=i.delete.bind(i)}return A(A({},y((function(){return n(A(A({},o.value),{},{params:u}))}))),{},{xhr:i})};return a||(0,h.onBeforeUnmount)((function(){l.value.forEach((function(e){e.isPending&&e.abort()})),f.forEach((function(e){return e()}))})),{get:function(e,t,a){var s=new P,v=(n||T).bind(u),d=(o||T).bind(u),p=(i||T).bind(u),y=[v],m=[d],b=[p],g=(0,h.ref)(null);l.value.push(s);var _=(0,h.ref)(),E=(0,h.ref)(),j="",O=0,S=(0,h.computed)((function(){var r,n={},o=(0,h.unref)(e);"string"==typeof o?r=o:(r=(0,h.unref)(o.url),t||(t=o.params||{}),O=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=a),n.params=(0,h.unref)(t),n.cacheDuration=O),c&&(n.token=H(c));var i=(0,h.unref)(n.params||t||{});return"function"==typeof i&&(i=i()),n.params=A(A({},i),(0,h.isRef)(t)?t.value||{}:t),j="function"==typeof r?r(n.params):(0,h.unref)(r),n.url=j,n})),w=function(){var e=void 0!==S.value.enabled?S.value.enabled:a;return void 0===e||((0,h.isRef)(e)?(0,h.unref)(e):"function"==typeof e?e():!0===e)},k=(0,h.ref)(),X=function(){if(w()){_.value&&s.abort();var e="object"===R(S.value.params)?S.value.params:{};m.forEach((function(t){return t(e,s)})),_.value=!0,g.value=null;var t=decodeURIComponent(P.stringifyUrl(String(j),e));k.value=r({id:t,xhr:s.get.bind(s,S.value),duration:O}),k.value.catch((function(e){y.forEach((function(t){return t(e,s)})),g.value=e})),k.value.then((function(e){E.value=e})),k.value.finally((function(){var t;(t=l.value.indexOf(s))>-1&&l.value.splice(t,1),_.value=!1,b.forEach((function(t){return t(E.value,e,s)}))}))}};return f.push((0,h.watch)((function(){return S.value}),X,{immediate:w(),deep:!0})),f.push((0,h.watch)((function(){return g.value}),(function(e){if(e)throw e}))),{isPending:(0,h.computed)((function(){return _.value})),data:E,onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return b.push(e)},error:g,abort:function(){var e;return null===(e=k.value)||void 0===e?void 0:e.abortXhr()},promise:(0,h.computed)((function(){return k.value||new Promise((function(){}))})),reload:X,xhr:s}},post:function(e,t){return v("post",e,t)},put:function(e,t){return v("put",e,t)},delete:function(e,t){return v("delete",e,t)}}}function L(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,h.ref)(!1);return(0,h.watch)((function(){return e.value}),(function(){n.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){n.value=!0===e.value}),r)):n.value=!0===e.value}),{immediate:!0}),(0,h.computed)((function(){return n.value}))}})(),o})()));
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("vue"));else if("function"==typeof define&&define.amd)define(["vue"],t);else{var r="object"==typeof exports?t(require("vue")):t(e.vue);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(e=>(()=>{"use strict";var t={748:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var i=r[e]={exports:{}};return t[e](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{Deferred:()=>d,Xhr:()=>P,cache:()=>r,cacheIds:()=>u,cacheSize:()=>i,clearCache:()=>a,useAsync:()=>y,useMutation:()=>m,useResult:()=>b,useSpinner:()=>L,useXhr:()=>x});const e={log:function(e){},warn:function(e){},info:function(e){},error:function(e){},debug:function(e){}};var t=new Map;function r(r){var n,o,i=r||{},u=i.id,a=i.xhr,s="string"!=typeof r.duration&&r.duration||200;if("max"===r.duration&&(s=36e5),!t.get(u)&&a?(n=a(),t.set(u,n),n.then((function(r){return e.log("[Log] add cache: ".concat(u)),clearTimeout(o),o=window.setTimeout((function(){t.delete(u),e.log("[Log] delete cache: ".concat(u)),clearTimeout(o)}),s),r}),(function(){return t.delete(u),e.log("[Log] delete cache: ".concat(u))}))):u&&(n=t.get(u)),n)return n;throw Error("Call to undefined cache")}function i(){return t.size}function u(){return Array.from(t.keys())}function a(r){r?t.delete(r):t.forEach((function(e,t,r){r.delete(t)})),e.log("[Log] cache cleared")}function s(e){return s="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},s(e)}function c(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,v(n.key),n)}}function l(e,t,r){return t&&c(e.prototype,t),r&&c(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function f(e,t,r){return(t=v(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function v(e){var t=function(e,t){if("object"!==s(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==s(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===s(t)?t:String(t)}var d=l((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),f(this,"resolve",void 0),f(this,"reject",void 0),f(this,"promise",void 0),f(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r})),this.promise.finally((function(){t.done=!0}))})),h=n(748);function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function y(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:(0,h.ref)(!0),n=(0,h.ref)(),o=(0,h.ref)(),i=(0,h.ref)(null),u=[],a=[],s=[],c=(0,h.ref)(null),l=(0,h.computed)((function(){return"function"==typeof t?t():(0,h.unref)(t)})),f=(0,h.ref)(),v=(0,h.computed)((function(){return"function"==typeof r?r():(0,h.unref)(r)})),d=function(t){a.forEach((function(e){return e(l.value)})),n.value=!0,i.value=null;var r,f=e,v=e;return c.value=Array.isArray(t)?v.apply(void 0,function(e){if(Array.isArray(e))return p(e)}(r=t)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(r)||function(e,t){if(e){if("string"==typeof e)return p(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?p(e,t):void 0}}(r)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()):f(t),c.value.catch((function(e){i.value=e||null,u.forEach((function(e){return e(i.value,l.value)})),i.value=e})),c.value.then((function(e){o.value=e,s.forEach((function(t){return t(e,l.value)}))})),c.value.finally((function(){n.value=!1})),c.value};return(0,h.watch)((function(){return v.value}),(function(e){!n.value&&e&&d(l.value)}),{immediate:!1}),(0,h.watch)((function(){return i.value}),(function(e){if(e)throw e})),(0,h.watch)((function(){return l.value}),(function(e){var t=JSON.stringify(e);!n.value&&(void 0===e&&void 0===f.value||v.value&&t!==JSON.stringify(f.value))&&d(e),f.value=void 0===t?void 0:JSON.parse(t)}),{immediate:v.value,deep:!0}),{isPending:n,data:o,error:i,reload:function(){return d(l.value)},onError:function(e){return u.push(e)},onStart:function(e){return a.push(e)},onEnd:function(e){return s.push(e)},promise:(0,h.computed)((function(){return c.value}))}}function m(e){var t=(0,h.ref)(),r=(0,h.ref)(),n=(0,h.ref)(),o=[],i=[],u=(0,h.ref)(null);return{mutate:function(a){for(var s=arguments.length,c=new Array(s>1?s-1:0),l=1;l<s;l++)c[l-1]=arguments[l];t.value=!0,n.value=null;var f=e;return u.value=f.apply(void 0,[a].concat(c)),u.value.catch((function(e){n.value=e||null,o.forEach((function(e){return e(n.value,c.length?[a].concat(c):a)})),n.value=e})),u.value.then((function(e){r.value=e,i.forEach((function(e){return e(r.value,c.length?[a].concat(c):a)}))})),u.value.finally((function(){t.value=!1})),u.value},onError:function(e){return o.push(e)},onEnd:function(e){return i.push(e)},isPending:t,error:n,data:r,promise:(0,h.computed)((function(){return u.value}))}}function b(e,t,r){var n=(0,h.ref)(t);return r||(r=function(e){return e}),t&&(n.value=r(t)),(0,h.watchEffect)((function(){if(e){var t,o=(0,h.unref)(e);o&&(n.value=null===(t=r)||void 0===t?void 0:t(o))}})),n}function g(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function E(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,u,a=[],s=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;s=!1}else for(;!(s=(n=i.call(r)).done)&&(a.push(n.value),a.length!==t);s=!0);}catch(e){c=!0,o=e}finally{try{if(!s&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(e,t)||function(e,t){if(e){if("string"==typeof e)return _(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_(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 _(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function 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 O(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,w(n.key),n)}}function S(e,t,r){return(t=w(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function w(e){var t=function(e,t){if("object"!==j(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.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:String(t)}var P=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),S(this,"onError",(function(){})),S(this,"onStart",(function(){})),S(this,"onAbort",(function(){})),S(this,"onProgress",(function(){})),S(this,"onEnd",(function(){})),S(this,"token",null),S(this,"url",""),S(this,"params",{}),S(this,"sendAs","json"),S(this,"timeout",1e4),S(this,"port",80),S(this,"responseType","text"),S(this,"isPending",!1),S(this,"_eventsReady",void 0),S(this,"_oXHR",void 0),S(this,"_onEnd",(function(){})),S(this,"_onError",(function(){})),S(this,"_deferred",void 0),S(this,"_isXhrResolved",void 0),S(this,"_isXhrRejected",void 0),S(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}var t,r,n;return t=e,r=[{key:"_getUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e._onError,!1),e._oXHR.removeEventListener("timeout",e._onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var t=this._deferred.promise;return t.abortXhr=this.abort.bind(this),t}},{key:"delete",value:function(e){return this._constructor(e),this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this._onError,!1),e.addEventListener("timeout",this._onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var t;this.isPending=!0,"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===j(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).bind(this),this.onStart=(t.onStart||this.onStart).bind(this),this.onAbort=(t.onAbort||this.onAbort).bind(this),this.onEnd=(t.onEnd||this.onEnd).bind(this),this.onError=(t.onError||this.onError).bind(this),this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new d,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e.injectParamsInUrl(this.url,this.params),i=o.url,u=o.params;this.url=i,this.params=u,this._onError=function(e){n.onError(e),n.removeEvents(),n._deferred.reject(e)},this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t={},r=e.search(/#/);r>-1&&(e=e.substring(0,r));var n=e.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&e.split("?")[1].split("&").reduce((function(e,t){var r=E(t.split("="),2),n=r[0],u=r[1],a=E((void 0===u?"":u).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(a===o||a===i)e[n]=a===o;else{var s=Number(a);e[n]=""===a||Number.isNaN(s)?a:s}return e}),t),t}},{key:"onBeforeSend",value:function(t){e.onBeforeSendList.push(t)}},{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e.injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===j(r)?Object.getOwnPropertyNames(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==j(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_stringifyForPathParam",value:function(e,t,r){var n=r[t];switch(j(r[t])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return e.replace(":".concat(t),n)}},{key:"injectParamsInUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:t,params:r};var n=decodeURIComponent(t),o=e.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var u=Object.getOwnPropertyNames(r).reduce((function(e,t){return e[t]=r[t],e}),function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?g(Object(r),!0).forEach((function(t){S(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):g(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substring(1),void 0!==u[t]&&(n=e._stringifyForPathParam(n,t,u),delete u[t])})),Object.keys(u).forEach((function(e){void 0!==o[e]&&(o[e]=u[e],delete u[e])}));var a=Object.keys(o);if(a.length){var s="?";a.forEach((function(e){if(!0===o[e]||!1===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(s).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(s).concat(e,"=").concat(r)}s="&"}))}return{url:n,params:u}}}],r&&O(t.prototype,r),n&&O(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function R(e){return R="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},R(e)}function k(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function A(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?k(Object(r),!0).forEach((function(t){X(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):k(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function X(e,t,r){return(t=function(e){var t=function(e,t){if("object"!==R(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==R(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===R(t)?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}S(P,"onBeforeSendList",[]);var T=function(){},H=function(e){return(0,h.unref)(e)};function x(e){var t=e||{onError:function(){},onStart:function(){},onEnd:function(){},context:null,legacy:!1,token:null},n=t.onError,o=t.onStart,i=t.onEnd,u=t.context,a=t.legacy,s=t.token,c=void 0===s?null:s,l=(0,h.ref)([]),f=[],v=function(e,t,r){var n,o=(0,h.computed)((function(){var e={},n=(0,h.unref)(t||{});r||(r=n.params||{}),e.params=(0,h.unref)(r),c&&(e.token=H(c));var o=(0,h.unref)(e.params||r||{});return"function"==typeof o&&(o=o()),e.url=(0,h.unref)(n.url),e})),i=new P,u=(0,h.unref)(o.value.params)||{};switch(e){case"post":default:n=i.post.bind(i);break;case"put":n=i.put.bind(i);break;case"delete":n=i.delete.bind(i)}return A(A({},y((function(){return n(A(A({},o.value),{},{params:u}))}))),{},{xhr:i})};return a||(0,h.onBeforeUnmount)((function(){l.value.forEach((function(e){e.isPending&&e.abort()})),f.forEach((function(e){return e()}))})),{get:function(e,t,a){var s=new P,v=(n||T).bind(u),d=(o||T).bind(u),p=(i||T).bind(u),y=[v],m=[d],b=[p],g=(0,h.ref)(null);l.value.push(s);var E=(0,h.ref)(),_=(0,h.ref)(),j="",O=0,S=(0,h.computed)((function(){var r,n={},o=(0,h.unref)(e);"string"==typeof o?r=o:(r=(0,h.unref)(o.url),t||(t=o.params||{}),O=o.cacheDuration,void 0!==o.enabled&&(n.enabled=o.enabled),void 0===n.enabled&&(n.enabled=a),n.params=(0,h.unref)(t),n.cacheDuration=O),c&&(n.token=H(c));var i=(0,h.unref)(n.params||t||{});return"function"==typeof i&&(i=i()),n.params=A(A({},i),(0,h.isRef)(t)?t.value||{}:t),j="function"==typeof r?r(n.params):(0,h.unref)(r),n.url=j,n})),w=function(){var e=void 0!==S.value.enabled?S.value.enabled:a;return void 0===e||((0,h.isRef)(e)?(0,h.unref)(e):"function"==typeof e?e():!0===e)},k=(0,h.ref)(),X=function(){if(w()){E.value&&s.abort();var e="object"===R(S.value.params)?S.value.params:{};m.forEach((function(t){return t(e,s)})),E.value=!0,g.value=null;var t=decodeURIComponent(P.stringifyUrl(String(j),e));k.value=r({id:t,xhr:s.get.bind(s,S.value),duration:O}),k.value.catch((function(e){y.forEach((function(t){return t(e,s)})),g.value=e})),k.value.then((function(e){_.value=e})),k.value.finally((function(){var t;(t=l.value.indexOf(s))>-1&&l.value.splice(t,1),E.value=!1,b.forEach((function(t){return t(_.value,e,s)}))}))}};return f.push((0,h.watch)((function(){return S.value}),X,{immediate:w(),deep:!0})),f.push((0,h.watch)((function(){return g.value}),(function(e){if(e)throw e}))),{isPending:(0,h.computed)((function(){return E.value})),data:_,onError:function(e){return y.push(e)},onStart:function(e){return m.push(e)},onEnd:function(e){return b.push(e)},error:g,abort:function(){var e;return null===(e=k.value)||void 0===e?void 0:e.abortXhr()},promise:(0,h.computed)((function(){return k.value||new Promise((function(){}))})),reload:X,xhr:s}},post:function(e,t){return v("post",e,t)},put:function(e,t){return v("put",e,t)},delete:function(e,t){return v("delete",e,t)}}}function L(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:400,n=(0,h.ref)(!1);return(0,h.watch)((function(){return e.value}),(function(){n.value&&!e.value?(t&&clearTimeout(t),t=setTimeout((function(){n.value=!0===e.value}),r)):n.value=!0===e.value}),{immediate:!0}),(0,h.computed)((function(){return n.value}))}})(),o})()));
|
package/dist/tests.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{mockXhr:()=>P});function r(e){return r="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},r(e)}function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,s(n.key),n)}}function o(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(e,t,r){return(t=s(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function s(e){var t=function(e,t){if("object"!==r(e)||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var o=n.call(e,"string");if("object"!==r(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===r(t)?t:String(t)}var u=o((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),i(this,"resolve",void 0),i(this,"reject",void 0),i(this,"promise",void 0),i(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r})),this.promise.finally((function(){t.done=!0}))}));function a(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function c(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,s,u=[],a=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;a=!1}else for(;!(a=(n=i.call(r)).done)&&(u.push(n.value),u.length!==t);a=!0);}catch(e){c=!0,o=e}finally{try{if(!a&&null!=r.return&&(s=r.return(),Object(s)!==s))return}finally{if(c)throw o}}return u}}(e,t)||function(e,t){if(e){if("string"==typeof e)return l(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?l(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 l(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function 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 h(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,d(n.key),n)}}function p(e,t,r){return(t=d(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){var t=function(e,t){if("object"!==f(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.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:String(t)}var v=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),p(this,"onError",(function(){})),p(this,"onStart",(function(){})),p(this,"onAbort",(function(){})),p(this,"onProgress",(function(){})),p(this,"onEnd",(function(){})),p(this,"token",null),p(this,"url",""),p(this,"params",{}),p(this,"sendAs","json"),p(this,"timeout",1e4),p(this,"port",80),p(this,"responseType","text"),p(this,"isPending",!1),p(this,"_eventsReady",void 0),p(this,"_oXHR",void 0),p(this,"_onEnd",(function(){})),p(this,"_onError",(function(){})),p(this,"_deferred",void 0),p(this,"_isXhrResolved",void 0),p(this,"_isXhrRejected",void 0),p(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}var t,r,n;return t=e,r=[{key:"_getUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e._onError,!1),e._oXHR.removeEventListener("timeout",e._onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var t=this._deferred.promise;return t.abortXhr=this.abort.bind(this),t}},{key:"delete",value:function(e){return this._constructor(e),this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this._onError,!1),e.addEventListener("timeout",this._onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var t;this.isPending=!0,"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===f(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).bind(this),this.onStart=(t.onStart||this.onStart).bind(this),this.onAbort=(t.onAbort||this.onAbort).bind(this),this.onEnd=(t.onEnd||this.onEnd).bind(this),this.onError=(t.onError||this.onError).bind(this),this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new u,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e._injectParamsInUrl(this.url,this.params),i=o.url,s=o.params;this.url=i,this.params=s,this._onError=function(e){n.onError(e),n.removeEvents(),n._deferred.reject(e)},this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t={},r=e.search(/#/);r>-1&&(e=e.substring(0,r));var n=e.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&e.split("?")[1].split("&").reduce((function(e,t){var r=c(t.split("="),2),n=r[0],s=r[1],u=c((void 0===s?"":s).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(u===o||u===i)e[n]=u===o;else{var a=Number(u);e[n]=""===u||Number.isNaN(a)?u:a}return e}),t),t}},{key:"onBeforeSend",value:function(t){e.onBeforeSendList.push(t)}},{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e._injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===f(r)?Object.getOwnPropertyNames(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==f(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_stringifyForPathParam",value:function(e,t,r){var n=r[t];switch(f(r[t])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return e.replace(":".concat(t),n)}},{key:"_injectParamsInUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:t,params:r};var n=decodeURIComponent(t),o=e.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var s=Object.getOwnPropertyNames(r).reduce((function(e,t){return e[t]=r[t],e}),function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?a(Object(r),!0).forEach((function(t){p(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):a(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substring(1),void 0!==s[t]&&(n=e._stringifyForPathParam(n,t,s),delete s[t])})),Object.keys(s).forEach((function(e){void 0!==o[e]&&(o[e]=s[e],delete s[e])}));var u=Object.keys(o);if(u.length){var c="?";u.forEach((function(e){if(!0===o[e]||!1===o[e])n+="".concat(c).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(c).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(c).concat(e,"=").concat(r)}c="&"}))}return{url:n,params:s}}}],r&&h(t.prototype,r),n&&h(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function m(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,g(n.key),n)}}function b(e,t,r){return(t=g(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function g(e){var t=function(e,t){if("object"!==y(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==y(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===y(t)?t:String(t)}p(v,"onBeforeSendList",[]);var _=XMLHttpRequest.prototype,j=_.send,E=_.abort,R=_.open,S=_.setRequestHeader,O=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),b(this,"header",void 0),b(this,"xhr",void 0),b(this,"method",void 0),b(this,"pending",void 0),this.header=[],this.xhr=null,this.method=null,this.pending=new u}var t,r,n;return t=e,r=[{key:"get",value:function(e){return e.method="GET",this._mockXMLHttpRequest(e)}},{key:"post",value:function(e){return e.method="POST",this._mockXMLHttpRequest(e)}},{key:"delete",value:function(e){return e.method="DELETE",this._mockXMLHttpRequest(e)}},{key:"put",value:function(e){return e.method="PUT",this._mockXMLHttpRequest(e)}},{key:"sendForm",value:function(e){var t,r,n={method:null==e||null===(t=e.getAttribute)||void 0===t?void 0:t.call(e,"method"),url:null==e||null===(r=e.getAttribute)||void 0===r?void 0:r.call(e,"action")};return this._mockXMLHttpRequest(n)}},{key:"abort",value:function(t){var r=this;XMLHttpRequest.prototype.abort=function(){r.pending.promise.catch((function(e){return e.resolve(t)})).then(e.restore)}}},{key:"resolve",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;this.pending.promise.then((function(r){setTimeout((function(){r.resolve(e)}),t)}))}},{key:"reject",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;this.pending.promise.then((function(r){setTimeout((function(){r.reject(e)}),t)}))}},{key:"_mockXMLHttpRequest",value:function(e){var t=arguments,r=this;return XMLHttpRequest.prototype.open=function(n,o){o===("GET"===e.method?v.stringifyUrl(e.url||"",e.params):e.url)&&e.method===n?r.method=n:R.apply(r,t)},XMLHttpRequest.prototype.send=function(n,o){v.stringifyUrl(e.url||"",e.params)&&r.method===e.method?(r.xhr=o,r.pending.resolve(o)):j.apply(r,t)},XMLHttpRequest.prototype.setRequestHeader=function(e,t){r.header.push([e,t])},{context:this,abort:this.abort.bind(this,e),resolve:this.resolve.bind(this),reject:this.reject.bind(this)}}}],n=[{key:"restore",value:function(){XMLHttpRequest.prototype.abort=E,XMLHttpRequest.prototype.send=j,XMLHttpRequest.prototype.open=R,XMLHttpRequest.prototype.setRequestHeader=S}}],r&&m(t.prototype,r),n&&m(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function P(){return{get:function(e){return(new O).get(e)},post:function(e){return(new O).post(e)},delete:function(e){return(new O).delete(e)},put:function(e){return(new O).put(e)},sendForm:function(e){return(new O).sendForm(e)},restore:function(){O.restore()}}}return t})()));
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{mockXhr:()=>P});function r(e){return r="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},r(e)}function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,s(n.key),n)}}function o(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(e,t,r){return(t=s(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function s(e){var t=function(e,t){if("object"!==r(e)||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var o=n.call(e,"string");if("object"!==r(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===r(t)?t:String(t)}var u=o((function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),i(this,"resolve",void 0),i(this,"reject",void 0),i(this,"promise",void 0),i(this,"done",!1),this.resolve=function(){throw Error("Can t resolve")},this.reject=function(){throw Error("Can t reject")},this.promise=new Promise((function(e,r){t.resolve=e,t.reject=r})),this.promise.finally((function(){t.done=!0}))}));function a(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function c(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,s,u=[],a=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;a=!1}else for(;!(a=(n=i.call(r)).done)&&(u.push(n.value),u.length!==t);a=!0);}catch(e){c=!0,o=e}finally{try{if(!a&&null!=r.return&&(s=r.return(),Object(s)!==s))return}finally{if(c)throw o}}return u}}(e,t)||function(e,t){if(e){if("string"==typeof e)return l(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?l(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 l(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function 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 h(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,d(n.key),n)}}function p(e,t,r){return(t=d(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){var t=function(e,t){if("object"!==f(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.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:String(t)}var v=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),p(this,"onError",(function(){})),p(this,"onStart",(function(){})),p(this,"onAbort",(function(){})),p(this,"onProgress",(function(){})),p(this,"onEnd",(function(){})),p(this,"token",null),p(this,"url",""),p(this,"params",{}),p(this,"sendAs","json"),p(this,"timeout",1e4),p(this,"port",80),p(this,"responseType","text"),p(this,"isPending",!1),p(this,"_eventsReady",void 0),p(this,"_oXHR",void 0),p(this,"_onEnd",(function(){})),p(this,"_onError",(function(){})),p(this,"_deferred",void 0),p(this,"_isXhrResolved",void 0),p(this,"_isXhrRejected",void 0),p(this,"_eventReady",void 0),this._eventsReady=!1,this._constructor(t||{},r)}var t,r,n;return t=e,r=[{key:"_getUrl",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.onBeforeSendList.reduce((function(e,t){return t(e||{})}),t);return e.stringifyUrl(this.url,r)}},{key:"removeEvents",value:function(){var e=this,t=function(){e._oXHR.removeEventListener("load",e._onEnd,!1),e._oXHR.removeEventListener("error",e._onError,!1),e._oXHR.removeEventListener("timeout",e._onError,!1),e._oXHR.removeEventListener("loadstart",e.onStart,!1),e._oXHR.removeEventListener("abort",e.onAbort,!1),e._oXHR.upload.removeEventListener("progress",e.onProgress,!1),e._oXHR.removeEventListener("progress",e.onProgress,!1)};this._deferred.promise.then(t,t)}},{key:"post",value:function(e){return this._constructor(e),this._oXHR.open("POST",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"put",value:function(e){return this._constructor(e),this._oXHR.open("PUT",this._getUrl(),!0),this._send(),this._deferred.promise}},{key:"get",value:function(e){this._constructor(e||{}),this._oXHR.open("GET",this._getUrl("multipart"===this.sendAs?{}:this.params),!0),this._send();var t=this._deferred.promise;return t.abortXhr=this.abort.bind(this),t}},{key:"delete",value:function(e){return this._constructor(e),this._oXHR.open("DELETE",this._getUrl(this.params),!0),this._send(),this._deferred.promise}},{key:"abort",value:function(){return this._isXhrResolved&&!this._isXhrRejected||(this._oXHR.abort(null,this),this._isXhrRejected=!0,this._deferred.reject({message:"Xhr aborted: ".concat(this.url),name:"HTTP-ABORTED"})),this._deferred.promise}},{key:"resolve",value:function(e){return this._deferred.resolve(e),this._deferred.promise}},{key:"reject",value:function(e){return this._deferred.reject(e),this._deferred.promise}},{key:"_setEvents",value:function(e){this._eventsReady||(this._eventReady=!0,e.addEventListener("load",this._onEnd,!1),e.addEventListener("error",this._onError,!1),e.addEventListener("timeout",this._onError,!1),e.addEventListener("loadstart",this.onStart,!1),e.addEventListener("abort",this.onAbort,!1),e.upload.addEventListener("progress",this.onProgress,!1),e.addEventListener("progress",this.onProgress,!1))}},{key:"_send",value:function(){var t;this.isPending=!0,"multipart"===this.sendAs?t=e.getFormData(this.params):"json"===this.sendAs&&(this._oXHR.setRequestHeader("content-type","application/json; charset=utf-8"),t=JSON.stringify(this.params)),this.token&&this._oXHR.setRequestHeader("Authorization","Bearer ".concat(this.token)),this._oXHR.send(t,this)}},{key:"_constructor",value:function(t,r){var n=this;t&&"object"===f(t)?(this.sendAs=t.sendAs||this.sendAs,this.url=t.url||this.url,this.port=t.port||this.port,this.params=t.params||this.params,this.timeout=t.timeout||this.timeout,this.responseType=t.responseType||this.responseType,this.onProgress=(t.onProgress||this.onProgress).bind(this),this.onStart=(t.onStart||this.onStart).bind(this),this.onAbort=(t.onAbort||this.onAbort).bind(this),this.onEnd=(t.onEnd||this.onEnd).bind(this),this.onError=(t.onError||this.onError).bind(this),this.token=t.token||this.token):t&&"string"==typeof t&&(this.url=t,this.params=r||this.params),80!==this.port&&(this.url="".concat(window.location.protocol,"//").concat(window.location.hostname,":").concat(this.port).concat(this.url)),this._deferred=new u,this._deferred.promise.finally((function(){n.isPending=!1})),this._oXHR=new XMLHttpRequest,this._oXHR.timeout=this.timeout,this._oXHR.responseType=this.responseType,this._isXhrResolved=!1,this._isXhrRejected=!1;var o=e.injectParamsInUrl(this.url,this.params),i=o.url,s=o.params;this.url=i,this.params=s,this._onError=function(e){n.onError(e),n.removeEvents(),n._deferred.reject(e)},this._onEnd=function(t){var r=e.parseResult(n._oXHR);return n._oXHR.status>=400?(n._isXhrRejected=!0,n._onError(r),n._deferred.reject(r)):(n.onEnd(r,t),n._isXhrResolved=!0,n.removeEvents(),n.resolve(r))},this._setEvents(this._oXHR)}}],n=[{key:"parseResult",value:function(e){var t=e.response;try{var r=e.getResponseHeader("Content-Type");r&&r.toLowerCase().indexOf("json")>-1&&(t=JSON.parse(e.response))}catch(r){t=e.response}return t}},{key:"extractQueryParams",value:function(e){var t={},r=e.search(/#/);r>-1&&(e=e.substring(0,r));var n=e.indexOf("?"),o=(!0).toString(),i=(!1).toString();return n>-1&&e.split("?")[1].split("&").reduce((function(e,t){var r=c(t.split("="),2),n=r[0],s=r[1],u=c((void 0===s?"":s).match(/^(?:"?([^"]+)"?)$/)||[],2)[1];if(u===o||u===i)e[n]=u===o;else{var a=Number(u);e[n]=""===u||Number.isNaN(a)?u:a}return e}),t),t}},{key:"onBeforeSend",value:function(t){e.onBeforeSendList.push(t)}},{key:"new",value:function(t){return new e(t)}},{key:"stringifyUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e.injectParamsInUrl(t,r);t=n.url,r=n.params;var o=t.indexOf("?")>-1?"&":"?",i="";return r&&!Array.isArray(r)&&"object"===f(r)?Object.getOwnPropertyNames(r).filter((function(e){return void 0!==r[e]})).forEach((function(e){i+="".concat(o).concat(e,"=").concat(encodeURIComponent(JSON.stringify(r[e]))),o="&"})):r&&(i=o+encodeURIComponent(JSON.stringify(r))),(t=t.replace(/\/:[^/]*/gi,""))+i}},{key:"getFormData",value:function(e){var t,r=new FormData;return Object.keys(e).forEach((function(n){if(e[n]instanceof FileList)for(var o=0;o<e[n].length;o+=1)r.append(n,e[n][o]);else e[n]instanceof File?r.append(n,e[n]):("object"!==f(t=e[n])&&!Array.isArray(t)||null===t||(t=JSON.stringify(t)),r.append(n,t))})),r}},{key:"_stringifyForPathParam",value:function(e,t,r){var n=r[t];switch(f(r[t])){case"boolean":case"string":case"bigint":case"number":n=String(n);break;default:n=null===n?"null":encodeURIComponent(JSON.stringify(n))}return e.replace(":".concat(t),n)}},{key:"injectParamsInUrl",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Array.isArray(r)||0===Object.keys(r).length)return{url:t,params:r};var n=decodeURIComponent(t),o=e.extractQueryParams(n),i=n.search(/\?|#/);i>-1&&(n=n.substring(0,i));var s=Object.getOwnPropertyNames(r).reduce((function(e,t){return e[t]=r[t],e}),function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?a(Object(r),!0).forEach((function(t){p(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):a(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({},o));(n.match(/:[a-z0-9]+/gi)||[]).forEach((function(t){t=t.substring(1),void 0!==s[t]&&(n=e._stringifyForPathParam(n,t,s),delete s[t])})),Object.keys(s).forEach((function(e){void 0!==o[e]&&(o[e]=s[e],delete s[e])}));var u=Object.keys(o);if(u.length){var c="?";u.forEach((function(e){if(!0===o[e]||!1===o[e])n+="".concat(c).concat(e,"=").concat(o[e]);else if(null===o[e])n+="".concat(c).concat(e,"=").concat(o[e]);else{var t=Number(o[e]),r=Number.isNaN(t)?encodeURIComponent('"'.concat(o[e],'"')):t;n+="".concat(c).concat(e,"=").concat(r)}c="&"}))}return{url:n,params:s}}}],r&&h(t.prototype,r),n&&h(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function m(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,g(n.key),n)}}function b(e,t,r){return(t=g(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function g(e){var t=function(e,t){if("object"!==y(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==y(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===y(t)?t:String(t)}p(v,"onBeforeSendList",[]);var _=XMLHttpRequest.prototype,j=_.send,E=_.abort,R=_.open,S=_.setRequestHeader,O=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),b(this,"header",void 0),b(this,"xhr",void 0),b(this,"method",void 0),b(this,"pending",void 0),this.header=[],this.xhr=null,this.method=null,this.pending=new u}var t,r,n;return t=e,r=[{key:"get",value:function(e){return e.method="GET",this._mockXMLHttpRequest(e)}},{key:"post",value:function(e){return e.method="POST",this._mockXMLHttpRequest(e)}},{key:"delete",value:function(e){return e.method="DELETE",this._mockXMLHttpRequest(e)}},{key:"put",value:function(e){return e.method="PUT",this._mockXMLHttpRequest(e)}},{key:"sendForm",value:function(e){var t,r,n={method:null==e||null===(t=e.getAttribute)||void 0===t?void 0:t.call(e,"method"),url:null==e||null===(r=e.getAttribute)||void 0===r?void 0:r.call(e,"action")};return this._mockXMLHttpRequest(n)}},{key:"abort",value:function(t){var r=this;XMLHttpRequest.prototype.abort=function(){r.pending.promise.catch((function(e){return e.resolve(t)})).then(e.restore)}}},{key:"resolve",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;this.pending.promise.then((function(r){setTimeout((function(){r.resolve(e)}),t)}))}},{key:"reject",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;this.pending.promise.then((function(r){setTimeout((function(){r.reject(e)}),t)}))}},{key:"_mockXMLHttpRequest",value:function(e){var t=arguments,r=this;return XMLHttpRequest.prototype.open=function(n,o){o===("GET"===e.method?v.stringifyUrl(e.url||"",e.params):e.url)&&e.method===n?r.method=n:R.apply(r,t)},XMLHttpRequest.prototype.send=function(n,o){v.stringifyUrl(e.url||"",e.params)&&r.method===e.method?(r.xhr=o,r.pending.resolve(o)):j.apply(r,t)},XMLHttpRequest.prototype.setRequestHeader=function(e,t){r.header.push([e,t])},{context:this,abort:this.abort.bind(this,e),resolve:this.resolve.bind(this),reject:this.reject.bind(this)}}}],n=[{key:"restore",value:function(){XMLHttpRequest.prototype.abort=E,XMLHttpRequest.prototype.send=j,XMLHttpRequest.prototype.open=R,XMLHttpRequest.prototype.setRequestHeader=S}}],r&&m(t.prototype,r),n&&m(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}();function P(){return{get:function(e){return(new O).get(e)},post:function(e){return(new O).post(e)},delete:function(e){return(new O).delete(e)},put:function(e){return(new O).put(e)},sendForm:function(e){return(new O).sendForm(e)},restore:function(){O.restore()}}}return t})()));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-use-async",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.3",
|
|
4
4
|
"description": "Xhr and async helpers",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,31 +26,31 @@
|
|
|
26
26
|
"tsc:export-types": "tsc --outDir types"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"vue": "^3.
|
|
29
|
+
"vue": "^3.3.4"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
33
|
-
"@babel/preset-env": "^7.
|
|
34
|
-
"@babel/preset-typescript": "^7.
|
|
35
|
-
"@types/jest": "^29.5.
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
37
|
-
"@typescript-eslint/parser": "^5.
|
|
33
|
+
"@babel/preset-env": "^7.22.5",
|
|
34
|
+
"@babel/preset-typescript": "^7.22.5",
|
|
35
|
+
"@types/jest": "^29.5.2",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.59.11",
|
|
37
|
+
"@typescript-eslint/parser": "^5.59.11",
|
|
38
38
|
"babel-eslint": "^10.1.0",
|
|
39
39
|
"babel-jest": "^29.5.0",
|
|
40
40
|
"babel-loader": "^9.1.2",
|
|
41
41
|
"babel-plugin-module-resolver": "^5.0.0",
|
|
42
|
-
"eslint": "^8.
|
|
42
|
+
"eslint": "^8.42.0",
|
|
43
43
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
44
44
|
"eslint-import-resolver-typescript": "^3.5.5",
|
|
45
45
|
"eslint-plugin-import": "^2.27.5",
|
|
46
46
|
"husky": "^8.0.3",
|
|
47
47
|
"jest": "^29.5.0",
|
|
48
48
|
"jest-environment-jsdom": "^29.5.0",
|
|
49
|
-
"lint-staged": "^13.2.
|
|
50
|
-
"typescript": "^5.
|
|
51
|
-
"vue": "^3.
|
|
52
|
-
"webpack": "^5.
|
|
53
|
-
"webpack-cli": "^5.
|
|
49
|
+
"lint-staged": "^13.2.2",
|
|
50
|
+
"typescript": "^5.1.3",
|
|
51
|
+
"vue": "^3.3.4",
|
|
52
|
+
"webpack": "^5.86.0",
|
|
53
|
+
"webpack-cli": "^5.1.4"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"vue": "^3.2.45"
|