@uni-use/request
Version:
An easy to use axios based http client combinatorial api with support for return sequentially
8 lines (7 loc) • 9.39 kB
JavaScript
/*!
* @uni-use/request v1.6.0-beta.1
* An easy to use axios based http client combinatorial api with support for return sequentially
* (c) 2022-2023 saqqdy<https://github.com/saqqdy>
* Released under the MIT License.
*/
this.useRequest=function(t,n){"use strict";var e=function(t,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e])},e(t,n)};function r(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var o=function(){return o=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t},o.apply(this,arguments)};function i(t,n,e,r){return new(e||(e=Promise))((function(o,i){function u(t){try{l(r.next(t))}catch(t){i(t)}}function a(t){try{l(r.throw(t))}catch(t){i(t)}}function l(t){var n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(u,a)}l((r=r.apply(t,n||[])).next())}))}function u(t,n){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(l){return function(a){if(e)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(e=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=n.call(t,u)}catch(t){a=[6,t],r=0}finally{e=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}function a(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function l(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,o,i=e.call(t),u=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u}"function"==typeof SuppressedError&&SuppressedError;var c=function(){function n(t,n){this.waiting={},this.unique=!1,this.orderly=!0,this.onCancel=null;n=Object.assign({unique:!1,orderly:!0},n||{}),this.axiosInstance=t,this.unique=n.unique,this.orderly=n.orderly,n.onCancel&&(this.onCancel=n.onCancel)}return n.prototype.request=function(n){return i(this,void 0,void 0,(function(){var e,r,o,i,a,l,c,s,f,h,p=this;return u(this,(function(u){return e=n.unique,r=void 0===e?this.unique:e,o=n.orderly,i=void 0===o?this.orderly:o,a=n.url,l=void 0===a?"":a,c=Symbol("promiseKey"),s=t.CancelToken.source(),n.cancelToken=s.token,"function"==typeof AbortController&&(f=new AbortController,n.signal=f.signal),r&&this.clear(l),h=new Promise((function(e,r){p.axiosInstance(n).then((function(t){i?p.wait(l,c).then((function(){e(t)})):e(t)})).catch((function(e){t.isCancel(e)?p.onCancel&&p.onCancel(e,n):r(e)})).finally((function(){var t=p.waiting[l].findIndex((function(t){return t.promiseKey===c}));t>-1&&p.waiting[l].splice(t,1)}))})),this.add(l,{promiseKey:c,promise:h,source:s,abortController:f}),[2,h]}))}))},n.prototype.clear=function(t){var n,e;for(var r in this.waiting)if(!t||r===t){var o=this.waiting[r]||[];try{for(var i=(n=void 0,a(o)),u=i.next();!u.done;u=i.next()){var l=u.value;l.source.cancel("request canceled"),l.abortController&&l.abortController.abort()}}catch(t){n={error:t}}finally{try{u&&!u.done&&(e=i.return)&&e.call(i)}finally{if(n)throw n.error}}this.waiting[r]=[]}},n.prototype.wait=function(t,n){return i(this,void 0,void 0,(function(){var e,r;return u(this,(function(o){switch(o.label){case 0:if(!this.orderly)return[2,Promise.resolve()];e=this.waiting[t]||[],r=e.findIndex((function(t){return t.promiseKey===n})),o.label=1;case 1:if(!(r>0))return[3,7];if(r--,!e[r]||e[r].promiseKey===n)return[3,6];o.label=2;case 2:return o.trys.push([2,4,,5]),[4,e[r].promise];case 3:return o.sent(),[3,5];case 4:return o.sent(),console.info("The task has been dropped"),[3,5];case 5:e.splice(r,1),o.label=6;case 6:return[3,1];case 7:return[2]}}))}))},n.prototype.add=function(t,n){t in this.waiting||(this.waiting[t]=[]),this.waiting[t].push(n)},n}();function s(t){return"function"==typeof t?t():n.unref(t)}var f,h,p=function(t,n){return void 0===n&&(n=!1),new Promise((function(e,r){return setTimeout(n?r:e,t)}))},y=function(){function t(t,n){void 0===n&&(n=!1),this.isNot=!1,this.r=t,this.isNot=null!=n&&n}return t.prototype.toMatch=function(t,e){var r=this,o=void 0===e?{}:e,i=o.flush,u=void 0===i?"sync":i,a=o.deep,l=void 0!==a&&a,c=o.timeout,f=o.throwOnTimeout,h=null,y=[new Promise((function(e){h=n.watch(r.r,(function(n){t(n)!==r.isNot&&(null==h||h(),e(n))}),{flush:u,deep:l,immediate:!0})}))];return null!=c&&y.push(p(c,f).then((function(){return s(r.r)})).finally((function(){return null==h?void 0:h()}))),Promise.race(y)},t.prototype.changed=function(t){return this.changedTimes(1,t)},t.prototype.changedTimes=function(t,n){void 0===t&&(t=1);var e=-1;return this.toMatch((function(){return(e+=1)>=t}),n)},Object.defineProperty(t.prototype,"not",{get:function(){return this.isNot=!this.isNot,this},enumerable:!1,configurable:!0}),t}(),v=function(t){function n(n){return t.call(this,n)||this}return r(n,t),n.prototype.toContains=function(n,e){return t.prototype.toMatch.call(this,(function(t){var e=Array.from(t);return e.includes(n)||e.includes(s(n))}),e)},n}(y),d=function(t){function e(n,e){return t.call(this,n,e)||this}return r(e,t),e.prototype.toBe=function(e,r){var o=this;if(!n.isRef(e))return t.prototype.toMatch.call(this,(function(t){return t===e}),r);var i=null!=r?r:{},u=i.flush,a=void 0===u?"sync":u,c=i.deep,f=void 0!==c&&c,h=i.timeout,y=i.throwOnTimeout,v=null,d=new Promise((function(t){v=n.watch([o.r,e],(function(n){var e=l(n,2),r=e[0],i=e[1];o.isNot!==(r===i)&&(null==v||v(),t(r))}),{flush:a,deep:f,immediate:!0})})),b=[d];return null!=h&&b.push(p(h,y).then((function(){return s(o.r)})).finally((function(){return null==v||v(),s(o.r)}))),Promise.race(b)},e.prototype.toBeTruthy=function(n){return t.prototype.toMatch.call(this,(function(t){return Boolean(t)}),n)},e.prototype.toBeNull=function(t){return this.toBe(null,t)},e.prototype.toBeUndefined=function(t){return this.toBe(void 0,t)},e.prototype.toBeNaN=function(n){return t.prototype.toMatch.call(this,Number.isNaN,n)},e}(y);function b(t){for(var n,e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return n=t,Object.prototype.toString.call(n).includes("Array")||0!==e.length?Promise.all([].concat(t).concat(e)).then((function(t){return[null,t]})).catch((function(t){return[t,void 0]})):t.then((function(t){return[null,t]})).catch((function(t){return[t,void 0]}))}var w={unique:!0,orderly:!0,refetch:!1,immediate:!0};return function(e,r,a){r=Object.assign({},w,r);var p=a||r||{},y=p.unique,m=p.orderly,g=p.immediate,x=p.beforeRequest,C=p.afterRequest,q=p.onResponseError,O=p.onError,P=n.shallowRef(null),T=n.shallowRef(null),N=n.ref(!1),S=n.ref(!1),j=n.ref(null),B=n.ref(""),k=n.shallowRef();function R(t){return i(this,void 0,void 0,(function(){var n,e,r,o,i;return u(this,(function(u){switch(u.label){case 0:return x?[4,x(t)]:[3,2];case 1:t=u.sent(),u.label=2;case 2:return S.value=!1,N.value=!0,[4,b(h(t))];case 3:if(n=l.apply(void 0,[u.sent(),2]),e=n[0],!(r=n[1]))return[3,10];if(k.value=r,j.value=r.status,B.value=r.statusText,!C)return[3,8];u.label=4;case 4:return u.trys.push([4,6,,7]),o=P,[4,C(r)];case 5:return o.value=u.sent(),[3,7];case 6:return i=u.sent(),e=i,q&&q(e),[3,7];case 7:return[3,9];case 8:P.value=r.data,u.label=9;case 9:return[3,11];case 10:(null==e?void 0:e.response)?(k.value=e.response,j.value=e.response.status,B.value=e.response.statusText,q&&q(k.value)):O&&O(e),u.label=11;case 11:return T.value=e,S.value=!0,N.value=!1,[2,P.value]}}))}))}f||(f=t.create({timeout:1e4})),h||(h=function(t,n){var e=new c(t,n);function r(t,n){var r;return"string"!=typeof t?n=t:(null!=n||(n={}),null!==(r=n.url)&&void 0!==r||(n.url=t)),e.request(n)}return r.series=e.waiting,r.clear=function(){e.clear()},r}(f,{unique:y,orderly:m,onCancel:function(t){console.info("onCancel => ",t.message,t.config)}}));var _={isFinished:S,statusCode:j,statusText:B,response:k,error:T,data:P,isFetching:N,request:R};function A(){return new Promise((function(t,n){var e,r;(e=S,Array.isArray(s(e))?new v(e):new d(e,r)).toBe(!0).then((function(){return t(_)})).catch((function(t){return n(t)}))}))}return g&&Promise.resolve().then((function(){return R(r)})),o(o({},_),{then:function(t,n){return A().then(t,n)}})}}(axios,VueDemi);