@hsui/request
Version:
Hundsun frontend runtime request framework
2 lines (1 loc) • 4.68 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@hsui/sdk")):"function"==typeof define&&define.amd?define(["exports","@hsui/sdk"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).request={},e.huiSdk)}(this,(function(e,t){"use strict";function r(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function n(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 o(e){for(var t=1;arguments.length>t;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?n(Object(o),!0).forEach((function(t){r(e,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))}))}return e}var i=Object.assign;function a(){}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);t>r;r++)n[r]=e[r];return n}function c(e){return function(e){if(Array.isArray(e))return u(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return u(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)?u(e,t):void 0}}(e)||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.")}()}function l(e,t){for(var r=0;t.length>r;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}var s=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.type=t,this.middlewares=[],this.finalHook=a}var t,r,n;return t=e,r=[{key:"final",value:function(e){this.finalHook=e}},{key:"concat",value:function(e){this.middlewares=this.middlewares.concat(e)}},{key:"push",value:function(e){this.middlewares.push(e)}},{key:"justRun",value:function(){this.args=Array.prototype.slice.call(arguments);var e=this;(function t(r){this.middlewares.length>r?this.middlewares[r].apply(null,[].concat(c(this.args),[function(){t.call(e,++r)}])):this.finalHook.apply(null,this.args)}).call(e,0)}},{key:"runAndReturn",value:function(){this.args=Array.prototype.slice.call(arguments);var e=this;return new Promise((function(t,r){(function n(o){if(this.middlewares.length>o)try{this.middlewares[o].apply(null,[].concat(c(this.args),[function(){n.call(e,++o)}]))}catch(e){r(e)}else this.finalHook.apply(null,this.args).then(t).catch(r)}).call(e,0)}))}}],r&&l(t.prototype,r),n&&l(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}(),f=t.createLogger(),d=function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];return f.info.apply(f,t)};function h(e,t){return e?Promise.reject(e):Promise.resolve(t)}function p(e,t){var r=o(o({},e),t);if(r.shouldMergeHeaders){var n=t.headers,i=o(o({},e.headers),n);r.headers=i}return r}f.levels.forEach((function(e){d[e]=function(){for(var t=arguments.length,r=Array(t),n=0;t>n;n++)r[n]=arguments[n];return f[e].apply(f,r)}}));var y=new WeakMap;e.initFetch=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.defaultConfig,n=void 0===r?{method:"post",data:{},timeout:5e3,headers:{},withCredentials:!1}:r,o=e.beforeRequestSendMiddlewares,a=void 0===o?[]:o,u=e.afterRequestSendMiddlewares,c=void 0===u?[]:u,l=new s("before-request-send");l.concat(a);var f,b=new s("after-request-send");b.concat(c),b.final(h),l.final((function(e){return new Promise((function(r,n){var o,i;(f=t.ajax(e)).then((function(e){return i=e})).catch((function(e){return o=e})).finally((function(){b.runAndReturn(o,i).then(r).catch(n)}))}))}));var v=function(e){d.debug("Fetch 实例初始化选项",p(n,e));var t=l.runAndReturn(p(n,e));return y.set(t,f),t};return v.get=function(e,t){return t?i(t,{url:e,method:"get"}):t={url:e,method:"get"},v(t)},v.post=function(e,t,r){return r?i(r,{url:e,method:"post",data:t}):r={url:e,method:"post",data:t},v(r)},v.cancel=function(){for(var e=arguments.length,r=Array(e),n=0;e>n;n++)r[n]=arguments[n];r.forEach((function(e,r){var n=y.get(e),o=t.ajaxCancelMap.get(n);o?o():d.debug("没有找到下标为 ".concat(r," 对应的 cancel 函数"))}))},v},Object.defineProperty(e,"__esModule",{value:!0})}));