extend-ajax
Advanced tools
Comparing version
# Changes | ||
# 2.1.0 | ||
- Add (AjaxOptions): autoAbort. | ||
- Add(index.d.ts): HttpMethod, EventType, HttpHeader, AjaxOptions, ExtendAjax exports | ||
# 2.0.1 | ||
- Fix(index.d.ts): time unit error. | ||
- Refator(test): test demo. | ||
# 1.2.5 | ||
@@ -62,22 +73,1 @@ - Fix: cache's hash calculation error | ||
- remove: the 'error' event | ||
# 0.1.0 | ||
- Add a new method: ajax.form | ||
- Add: when the request failed, res still have res data. | ||
- Fix: when the way of request is not post and get, it happen error | ||
- Fix: when ajax(url, options) is called, options will be considered as request method | ||
# 0.0.4 | ||
- Add:support AMD | ||
# 0.0.3 | ||
- Modify: improve 40% performance of cache than previous version. | ||
- Fix: when the size of cache is smaller than the specified size,the lasted cache is remove. | ||
# 0.0.2 | ||
- Add:cacheExp | ||
- Modify: improve the performance of cache. | ||
- Fix:when method is 'get', data can't be sent in some cases. |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.extendAjax=e():t.extendAjax=e()}(this,(function(){return(()=>{var t={831:(t,e,o)=>{var r=o(311).Symbol;t.exports=r},648:(t,e,o)=>{var r=o(831),n=o(20),i=o(825),a=r?r.toStringTag:void 0;t.exports=function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":a&&a in Object(t)?n(t):i(t)}},884:(t,e,o)=>{var r="object"==typeof o.g&&o.g&&o.g.Object===Object&&o.g;t.exports=r},20:(t,e,o)=>{var r=o(831),n=Object.prototype,i=n.hasOwnProperty,a=n.toString,s=r?r.toStringTag:void 0;t.exports=function(t){var e=i.call(t,s),o=t[s];try{t[s]=void 0;var r=!0}catch(t){}var n=a.call(t);return r&&(e?t[s]=o:delete t[s]),n}},825:t=>{var e=Object.prototype.toString;t.exports=function(t){return e.call(t)}},311:(t,e,o)=>{var r=o(884),n="object"==typeof self&&self&&self.Object===Object&&self,i=r||n||Function("return this")();t.exports=i},469:t=>{var e=Array.isArray;t.exports=e},814:(t,e,o)=>{var r=o(648),n=o(667);t.exports=function(t){if(!n(t))return!1;var e=r(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e}},362:(t,e,o)=>{var r=o(648),n=o(880);t.exports=function(t){return"number"==typeof t||n(t)&&"[object Number]"==r(t)}},667:t=>{t.exports=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}},880:t=>{t.exports=function(t){return null!=t&&"object"==typeof t}},440:(t,e,o)=>{"use strict";o.r(e),o.d(e,{default:()=>T});var r=o(814),n=o.n(r),i=o(362),a=o.n(i),s=o(667),c=o.n(s),u=o(469),l=o.n(u),p={xml:"application/xml, text/xml",html:"text/html",script:"text/javascript",json:"application/json",text:"text/plain",_default:"*/*"},f={html:"text/html",json:"application/json",file:"multipart/form-data",text:"text/plain",form:"application/x-www-form-urlencoded",_default:"application/x-www-form-urlencoded"},h={timeout:!1,isAsync:!0,header:{Accept:p._default,"Content-Type":f._default},charset:"utf-8",host:"",cacheSize:0,cacheExp:300,jsonpName:"jsonpCallback",jsonpParam:"callback"};function d(t){if(!/^\w+(-\w+)+/.test(t))return t;var e=t.split("-");return e.forEach((function(t,o){e[o]=t.replace(/^\w/,(function(t){return t.toUpperCase()}))})),e.join("-")}function v(t){for(var e in t){var o=t[e];delete t[e],t[d(e)]=o}}function y(t){for(var e in t){var o=t[e];"Content-Type"===e&&(t[e]=f[o]||o),"Accept"===e&&(t[e]=p[o]||o)}}var m=function(t,e,o){for(var r in void 0===o&&(o=""),e){var n=e[r];null!=n&&(c()(n)||l()(n)?m(t,n,r):t.push({key:o+r,val:n}))}};function b(t){var e=[],o="";return m(e,t),e.sort((function(t,e){return t.key>e.key?1:t.key===e.key?0:-1})),e.forEach((function(t){var e=t.key,r=t.val;o+=e+"="+r})),o}var x={"application/json":function(t){return c()(t)?JSON.stringify(t):""},"text/plain":function(t){return"string"==typeof t?t:""},"application/x-www-form-urlencoded":function(t){if(!c()(t))return"";var e="";for(var o in t){var r=t[o];void 0!==r&&(e+="&"+o+"="+encodeURI(r))}return e.substring(1)},formData:function(t){var e=new FormData;if(!c()(t))return e;for(var o in t){var r=t[o];if(r instanceof FileList)for(var n=0;n<r.length;n++)e.append(o,r[n]);else void 0!==r&&e.append(o,r)}return e}};const j=function(t,e,o){return void 0===e&&(e="application/x-www-form-urlencoded"),"get"===o&&(e="text/plain"),f[e]&&(e=f[e]),x[e]?x[e](t):""};var w=function(){return(w=Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)},g=function(){for(var t=0,e=0,o=arguments.length;e<o;e++)t+=arguments[e].length;var r=Array(t),n=0;for(e=0;e<o;e++)for(var i=arguments[e],a=0,s=i.length;a<s;a++,n++)r[n]=i[a];return r},S=window,C=function(){function t(e){for(var o=[],r=1;r<arguments.length;r++)o[r-1]=arguments[r];this.options={},this.events={},this.pool=[];var n={};o[1]?(n=o[1],this.method=o[0]):"string"==typeof o[0]?(this.method=o[0],n={}):(this.method="get",n=o[0]),this.url=e,this.xhr="jsonp"===this.method?null:this.getXHR(),this.options=w(w({},t.options),n),this.options.header&&(v(this.options.header),y(this.options.header))}return t.prototype.verifyCache=function(e,o){return e&&t.options.cacheSize&&(e.exp+e.time<+new Date||t.cacheCurSize>t.options.cacheSize)?(delete t.cache[o],--t.cacheCurSize,!1):!(!e||0===t.options.cacheSize)},t.prototype.addXHREventListener=function(e,o){var r=this,i=this.options,s=i.convert,c=i.timeout,u=i.query,l=i.header,p={};e.onreadystatechange=function(){var o=e.readyState,i=e.status;if(p.status=i,1===o)return r.emit("start"),void(a()(c)&&(r.timer=window.setTimeout((function(){p.error=new Error("request timeout"),r.emit("timeout")}),c)));if(4===o)if(4===o&&(p.header=function(t){var e=t.getAllResponseHeaders().split("\n"),o={};return e.pop(),e.forEach((function(t){var e=t.replace(" ","").split(":");o[e[0].toLowerCase()]=e[1]})),o}(e),p.data=n()(s)?s(e.responseText):e.responseText),i>=200&&i<300||304===i){var f=b({body:r.data,query:u,header:l,url:r.url});if(!t.cache[f]&&t.options.cacheExp&&t.options.cacheSize&&t.options.cacheSize>0&&(t.cache[f]={res:p,exp:1e3*t.options.cacheExp,time:+new Date},++t.cacheCurSize,t.cacheCurSize>t.options.cacheSize)){var h=Object.keys(t.cache)[0];delete t.cache[h]}r.emit("success",p)}else p.error=new Error("request fail:"+i),r.emit("fail",p)}},t.prototype.sendJSONP=function(t){var e=this,o=this.script=document.createElement("script"),r=this.options,n=r.timeout,i=r.jsonpName,a=void 0===i?"jsonpCallback":i;o.src=t,o.type="text/javascript",console.log(o),n&&(this.timer=window.setTimeout((function(){e.timer=o.onload=o.onerror=null,document.body.removeChild(o),e.emit("timeout")}),n)),this.emit("start"),S[a]=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];o.setAttribute("data-load","true"),e.emit.apply(e,g(["success"],t)),S[a]=null,clearTimeout(e.timer)},o.onload=function(){console.log("加载完成"),o.getAttribute("data-load")||(e.emit("fail"),o.onerror=null,document.body.removeChild(o)),o.onload=null},o.onerror=function(){e.emit("fail"),clearTimeout(e.timer),S[a]=null,document.body.removeChild(o)},document.body.append(o),console.log(o,o.src)},t.prototype.getXHR=function(){return this.pool.shift()||new XMLHttpRequest},t.prototype.emit=function(t){for(var e,o,r=this,n=[],i=1;i<arguments.length;i++)n[i-1]=arguments[i];if(this.promise)switch(t){case"success":null===(e=this.resolve)||void 0===e||e.apply(this.options.scope,n||[]);break;case"fail":null===(o=this.reject)||void 0===o||o.apply(this.options.scope,n||[])}var a=this.events[t];a&&a.forEach((function(t){t.call(r.options.scope||null,n)}))},t.prototype.on=function(t,e){var o=this.events[t];o?o.push(e):this.events[t]=[e]},t.prototype.send=function(e){return o=this,r=void 0,i=function(){var o,r,n,i,a,s,c,u,l,p,f,h,d,v,y,m,x,w=this;return function(t,e){var o,r,n,i,a={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(o)throw new TypeError("Generator is already executing.");for(;a;)try{if(o=1,r&&(n=2&i[0]?r.return:i[0]?r.throw||((n=r.return)&&n.call(r),0):r.next)&&!(n=n.call(r,i[1])).done)return n;switch(r=0,n&&(i=[2&i[0],n.value]),i[0]){case 0:case 1:n=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((n=(n=a.trys).length>0&&n[n.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!n||i[1]>n[0]&&i[1]<n[3])){a.label=i[1];break}if(6===i[0]&&a.label<n[1]){a.label=n[1],n=i;break}if(n&&a.label<n[2]){a.label=n[2],a.ops.push(i);break}n[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{o=n=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}(this,(function(g){return this.data=e,this.promise=new Promise((function(t,e){w.resolve=t,w.reject=e,w.xhr&&w.addXHREventListener(w.xhr,w)})),o=this.options,r=o.query,n=o.header,i=o.withCredentials,a=o.isAsync,s=void 0===a||a,c=o.charset,u=void 0===c?"utf-8":c,l=o.jsonpName,p=void 0===l?"jsonpCallback":l,f=o.jsonpParam,h=void 0===f?"callback":f,d=o.host,v=(void 0===d?"":d)+this.url,this.xhr?(y=b({body:e,query:r,header:n,url:this.url}),m=t.cache[y],this.verifyCache(m,y)?this.emit("success",m.res):(r&&(v+="?"+j(r)),x="get"===this.method?j(e):j(e,n&&n["Content-Type"]),this.xhr.open(this.method,v,s),this.xhr.withCredentials=!!i,function(t,e,o){for(var r in e){var n=e[r];"formData"!==n&&"Content-Type"===r&&(n+=";charset="+o),"Content-Type"===r&&"formData"===n||t.setRequestHeader(r,n)}}(this.xhr,n||{},u),this.xhr.send(x))):(v+="?"+h+"="+p,this.sendJSONP(v)),[2,this.promise]}))},new((n=void 0)||(n=Promise))((function(t,e){function a(t){try{c(i.next(t))}catch(t){e(t)}}function s(t){try{c(i.throw(t))}catch(t){e(t)}}function c(e){var o;e.done?t(e.value):(o=e.value,o instanceof n?o:new n((function(t){t(o)}))).then(a,s)}c((i=i.apply(o,r||[])).next())}));var o,r,n,i},t.prototype.abort=function(){if(this.xhr)this.xhr.abort(),this.emit("abort");else{var t=this.options.jsonpName;t&&S[t]&&(S[t]=null),this.script&&(document.body.removeChild(this.script),delete this.script),this.emit("abort")}},t.options=h,t.cache={},t.cacheCurSize=0,t}(),O=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return new(C.bind.apply(C,g([void 0,t],e)))};O.config=function(t){Object.assign(C.options,t)};const T=O}},e={};function o(r){if(e[r])return e[r].exports;var n=e[r]={exports:{}};return t[r](n,n.exports,o),n.exports}return o.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return o.d(e,{a:e}),e},o.d=(t,e)=>{for(var r in e)o.o(e,r)&&!o.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},o.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o(440)})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.extendAjax=e():t.extendAjax=e()}(this,(function(){return(()=>{var t={831:(t,e,o)=>{var n=o(311).Symbol;t.exports=n},648:(t,e,o)=>{var n=o(831),r=o(20),i=o(825),a=n?n.toStringTag:void 0;t.exports=function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":a&&a in Object(t)?r(t):i(t)}},884:(t,e,o)=>{var n="object"==typeof o.g&&o.g&&o.g.Object===Object&&o.g;t.exports=n},20:(t,e,o)=>{var n=o(831),r=Object.prototype,i=r.hasOwnProperty,a=r.toString,s=n?n.toStringTag:void 0;t.exports=function(t){var e=i.call(t,s),o=t[s];try{t[s]=void 0;var n=!0}catch(t){}var r=a.call(t);return n&&(e?t[s]=o:delete t[s]),r}},825:t=>{var e=Object.prototype.toString;t.exports=function(t){return e.call(t)}},311:(t,e,o)=>{var n=o(884),r="object"==typeof self&&self&&self.Object===Object&&self,i=n||r||Function("return this")();t.exports=i},469:t=>{var e=Array.isArray;t.exports=e},814:(t,e,o)=>{var n=o(648),r=o(667);t.exports=function(t){if(!r(t))return!1;var e=n(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e}},362:(t,e,o)=>{var n=o(648),r=o(880);t.exports=function(t){return"number"==typeof t||r(t)&&"[object Number]"==n(t)}},667:t=>{t.exports=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}},880:t=>{t.exports=function(t){return null!=t&&"object"==typeof t}},712:(t,e,o)=>{"use strict";o.r(e),o.d(e,{default:()=>E});var n=o(814),r=o.n(n),i=o(362),a=o.n(i),s=o(667),c=o.n(s),u=o(469),l=o.n(u),p={xml:"application/xml, text/xml",html:"text/html",script:"text/javascript",json:"application/json",text:"text/plain",_default:"*/*"},f={html:"text/html",json:"application/json",file:"multipart/form-data",text:"text/plain",form:"application/x-www-form-urlencoded",_default:"application/x-www-form-urlencoded"},h={timeout:!1,isAsync:!0,header:{Accept:p._default,"Content-Type":f._default},charset:"utf-8",host:"",cacheSize:0,cacheExp:3e5,jsonpName:"jsonpCallback",jsonpParam:"callback",autoAbort:!1};function d(t){if(!/^\w+(-\w+)+/.test(t))return t;var e=t.split("-");return e.forEach((function(t,o){e[o]=t.replace(/^\w/,(function(t){return t.toUpperCase()}))})),e.join("-")}function v(t){for(var e in t){var o=t[e];delete t[e],t[d(e)]=o}}function y(t){for(var e in t){var o=t[e];"Content-Type"===e&&(t[e]=f[o]||o),"Accept"===e&&(t[e]=p[o]||o)}}var m=function(t,e,o){for(var n in void 0===o&&(o=""),e){var r=e[n];null!=r&&(c()(r)||l()(r)?m(t,r,n):t.push({key:o+n,val:r}))}};function b(t){var e=[],o="";return m(e,t),e.sort((function(t,e){return t.key>e.key?1:t.key===e.key?0:-1})),e.forEach((function(t){var e=t.key,n=t.val;o+=e+"="+n})),o}var x={"application/json":function(t){return c()(t)?JSON.stringify(t):""},"text/plain":function(t){return"string"==typeof t?t:""},"application/x-www-form-urlencoded":function(t){if(!c()(t))return"";var e="";for(var o in t){var n=t[o];void 0!==n&&(e+="&"+o+"="+encodeURI(n))}return e.substring(1)},formData:function(t){var e=new FormData;if(!c()(t))return e;for(var o in t){var n=t[o];if(n instanceof FileList)for(var r=0;r<n.length;r++)e.append(o,n[r]);else void 0!==n&&e.append(o,n)}return e}};const j=function(t,e,o){return void 0===e&&(e="application/x-www-form-urlencoded"),"get"===o&&(e="text/plain"),f[e]&&(e=f[e]),x[e]?x[e](t):""};var w=[],g="";const S=function(t){var e=window.location.href;g&&e!==g&&w.length&&(w.forEach((function(t){t.abort()})),w=[]),w.push(t),g=e;var o=w.length-1;t.on("end",(function(){w.splice(o,1)}))};var C=function(){return(C=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)},O=function(){for(var t=0,e=0,o=arguments.length;e<o;e++)t+=arguments[e].length;var n=Array(t),r=0;for(e=0;e<o;e++)for(var i=arguments[e],a=0,s=i.length;a<s;a++,r++)n[r]=i[a];return n},T=window,k=function(){function t(e){for(var o=[],n=1;n<arguments.length;n++)o[n-1]=arguments[n];this.options={},this.events={},this.pool=[];var r={};o[1]?(r=o[1],this.method=o[0]):"string"==typeof o[0]?(this.method=o[0],r={}):(this.method="get",r=o[0]),this.url=e,this.xhr="jsonp"===this.method?null:this.getXHR(),this.options=C(C({},t.options),r),this.options.header&&(v(this.options.header),y(this.options.header))}return t.prototype.verifyCache=function(e,o){return e&&t.options.cacheSize&&(e.exp+e.time<+new Date||t.cacheCurSize>t.options.cacheSize)?(delete t.cache[o],--t.cacheCurSize,!1):!(!e||0===t.options.cacheSize)},t.prototype.addXHREventListener=function(e){var o=this,n=this.options,i=n.convert,s=n.timeout,c=n.query,u=n.header,l={};e.onreadystatechange=function(){var n=e.readyState,p=e.status;if(l.status=p,1===n)return o.emit("start"),void(a()(s)&&(o.timer=window.setTimeout((function(){l.error=new Error("request timeout"),o.emit("timeout")}),s)));if(4===n)if(4===n&&(l.header=function(t){var e=t.getAllResponseHeaders().split("\n"),o={};return e.pop(),e.forEach((function(t){var e=t.replace(" ","").split(":");o[e[0].toLowerCase()]=e[1]})),o}(e),l.data=r()(i)?i(e.responseText):e.responseText),p>=200&&p<300||304===p){var f=b({body:o.data,query:c,header:u,url:o.url});if(!t.cache[f]&&t.options.cacheExp&&t.options.cacheSize&&t.options.cacheSize>0&&(t.cache[f]={res:l,exp:t.options.cacheExp,time:+new Date},++t.cacheCurSize,t.cacheCurSize>t.options.cacheSize)){var h=Object.keys(t.cache)[0];delete t.cache[h]}o.emit("success",l)}else l.error=new Error("request fail:"+p),o.emit("fail",l)}},t.prototype.sendJSONP=function(t){var e=this,o=this.script=document.createElement("script"),n=this.options,r=n.timeout,i=n.jsonpName,a=void 0===i?"jsonpCallback":i;o.src=t,o.type="text/javascript",r&&(this.timer=window.setTimeout((function(){e.timer=o.onload=o.onerror=null,document.body.removeChild(o),e.emit("timeout")}),r)),this.emit("start"),T[a]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];o.setAttribute("data-load","true"),e.emit.apply(e,O(["success"],t)),T[a]=null,clearTimeout(e.timer)},o.onload=function(){o.getAttribute("data-load")||(e.emit("fail"),o.onerror=null,document.body.removeChild(o)),o.onload=null},o.onerror=function(){e.emit("fail"),clearTimeout(e.timer),T[a]=null,document.body.removeChild(o)},document.body.append(o)},t.prototype.getXHR=function(){return this.pool.shift()||new XMLHttpRequest},t.prototype.emit=function(t){for(var e,o,n=this,r=[],i=1;i<arguments.length;i++)r[i-1]=arguments[i];if(this.promise)switch(t){case"success":null===(e=this.resolve)||void 0===e||e.apply(this.options.scope,r||[]);break;case"fail":null===(o=this.reject)||void 0===o||o.apply(this.options.scope,r||[])}var a=this.events[t];a&&a.forEach((function(t){t.apply(n.options.scope||null,r)}))},t.prototype.on=function(t,e){var o=this.events[t];o?o.push(e):this.events[t]=[e]},t.prototype.send=function(e){return o=this,n=void 0,i=function(){var o,n,r,i,a,s,c,u,l,p,f,h,d,v,y,m,x,w=this;return function(t,e){var o,n,r,i,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(o)throw new TypeError("Generator is already executing.");for(;a;)try{if(o=1,n&&(r=2&i[0]?n.return:i[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,i[1])).done)return r;switch(n=0,r&&(i=[2&i[0],r.value]),i[0]){case 0:case 1:r=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((r=(r=a.trys).length>0&&r[r.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!r||i[1]>r[0]&&i[1]<r[3])){a.label=i[1];break}if(6===i[0]&&a.label<r[1]){a.label=r[1],r=i;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(i);break}r[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],n=0}finally{o=r=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}(this,(function(g){return this.data=e,this.promise=new Promise((function(t,e){w.resolve=t,w.reject=e,w.xhr&&w.addXHREventListener(w.xhr)})),this.emit("beforeSend",this.options),o=this.options,n=o.query,r=o.header,i=o.withCredentials,a=o.isAsync,s=void 0===a||a,c=o.charset,u=void 0===c?"utf-8":c,l=o.jsonpName,p=void 0===l?"jsonpCallback":l,f=o.jsonpParam,h=void 0===f?"callback":f,d=o.host,v=(void 0===d?"":d)+this.url,this.xhr?(y=b({body:e,query:n,header:r,url:this.url}),m=t.cache[y],this.verifyCache(m,y)?this.emit("success",m.res):(n&&(v+="?"+j(n)),x="get"===this.method?j(e):j(e,r&&r["Content-Type"]),this.xhr.open(this.method,v,s),this.xhr.withCredentials=!!i,function(t,e,o){for(var n in e){var r=e[n];"formData"!==r&&"Content-Type"===n&&(r+=";charset="+o),"Content-Type"===n&&"formData"===r||t.setRequestHeader(n,r)}}(this.xhr,r||{},u),this.xhr.send(x))):(v+="?"+h+"="+p,this.sendJSONP(v)),[2,this.promise]}))},new((r=void 0)||(r=Promise))((function(t,e){function a(t){try{c(i.next(t))}catch(t){e(t)}}function s(t){try{c(i.throw(t))}catch(t){e(t)}}function c(e){var o;e.done?t(e.value):(o=e.value,o instanceof r?o:new r((function(t){t(o)}))).then(a,s)}c((i=i.apply(o,n||[])).next())}));var o,n,r,i},t.prototype.abort=function(){if(this.xhr)this.xhr.abort(),this.emit("abort");else if("jsonp"===this.method){var t=this.options.jsonpName;t&&T[t]&&(T[t]=null),this.script&&(document.body.removeChild(this.script),delete this.script),this.emit("abort")}},t.options=h,t.cache={},t.cacheCurSize=0,t}(),A=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];var n=new(k.bind.apply(k,O([void 0,t],e)));return n.on("beforeSend",(function(t){t.autoAbort&&S(n)})),n};A.config=function(t){Object.assign(k.options,t)};const E=A}},e={};function o(n){if(e[n])return e[n].exports;var r=e[n]={exports:{}};return t[n](r,r.exports,o),r.exports}return o.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return o.d(e,{a:e}),e},o.d=(t,e)=>{for(var n in e)o.o(e,n)&&!o.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},o.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o(712)})()})); |
@@ -1,5 +0,5 @@ | ||
declare type HTTP_METHOD = 'get' | 'post' | 'delete' | 'put' | 'jsonp'; | ||
declare type CALLBACK = (data?: any) => void; | ||
declare type EVENT_TYPE = 'success' | 'fail' | 'start' | 'end' | 'timeout' | 'abort' | 'progress'; | ||
declare type AjaxConfig = [method: HTTP_METHOD, options?: AjaxOptions] | [options: AjaxOptions] | [] | ||
declare type HttpMethod = 'get' | 'post' | 'delete' | 'put' | 'jsonp'; | ||
declare type CallBack = (data?: any) => void; | ||
declare type EventType = 'success' | 'fail' | 'start' | 'end' | 'timeout' | 'abort' | 'progress'; | ||
declare type AjaxConfig = [method: HttpMethod, options?: AjaxOptions] | [options: AjaxOptions] | [] | ||
declare interface HttpHeader { | ||
@@ -18,3 +18,3 @@ 'Content-Type'?: 'text' | 'json' | 'form' | 'formData' | 'html' | string; | ||
/** The expire time of ajax request cache. (default: 300, unit: ms) */ | ||
/** The expire time of ajax request cache. (default: 300000, unit: ms) */ | ||
cacheExp?: number; | ||
@@ -43,3 +43,3 @@ | ||
/** The timeout of the request. */ | ||
/** The timeout of the request. (unit: ms) */ | ||
timeout?: number | false; | ||
@@ -52,2 +52,5 @@ | ||
query?: Record<string, string | number> | ||
/** Whether to abort requests when the page url changed and does not refresh(For single page application, default: false)*/ | ||
autoAbort?: boolean; | ||
} | ||
@@ -79,3 +82,3 @@ | ||
*/ | ||
on(eventName: EVENT_TYPE, cb: CALLBACK): void; | ||
on(eventName: EventType, cb: CallBack): void; | ||
@@ -88,2 +91,7 @@ /** | ||
send(data?: string | Record<string, unknown> | null): Promise<AjaxResData>; | ||
/** | ||
* Abort the request | ||
*/ | ||
abort(): void; | ||
} | ||
@@ -97,5 +105,5 @@ | ||
*/ | ||
declare function ejax(url: string, ...configs: AjaxConfig): ExtendAjax; | ||
declare function eAjax(url: string, ...configs: AjaxConfig): ExtendAjax; | ||
declare namespace ejax { | ||
declare namespace eAjax { | ||
/** | ||
@@ -106,5 +114,12 @@ * Create an Ajax request object. | ||
export function config(options: AjaxOptions): void; | ||
export { | ||
HttpHeader, | ||
EventType, | ||
HttpMethod, | ||
AjaxOptions, | ||
ExtendAjax | ||
} | ||
} | ||
export as namespace ejax; | ||
export = ejax; | ||
export as namespace eAjax; | ||
export = eAjax; |
{ | ||
"name": "extend-ajax", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "an AJAX request library", | ||
@@ -5,0 +5,0 @@ "main": "dist/extend-ajax.js", |
@@ -118,4 +118,4 @@ # extend-ajax | ||
- withCredentials \<boolean> 是否允许ajax请求携带验证信息,例如:cookie. | ||
- autoAbort \<Boolean> default: false, 当页面路由异步更新时会主动中断上一个路由中的请求,通常应用于SPA应用中。 | ||
### ajax.send(data) | ||
@@ -122,0 +122,0 @@ |
@@ -120,2 +120,3 @@ # extend-ajax | ||
- withCredentials \<Boolean> default: false, whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates | ||
- autoAbort \<Boolean> default: false, Whether to abort requests where from the previousroute page when the page url changed and does not refresh(For single page application). | ||
@@ -122,0 +123,0 @@ ### ajax.send(data) |
@@ -33,5 +33,7 @@ export const ACCEPT_MAP:{ | ||
cacheSize: 0, | ||
cacheExp: 300, | ||
cacheExp: 300000, | ||
jsonpName: 'jsonpCallback', | ||
jsonpParam: 'callback' | ||
jsonpParam: 'callback', | ||
autoAbort: false | ||
}; | ||
type HTTP_METHOD = 'get' | 'post' | 'delete' | 'put' | 'jsonp'; | ||
type CALLBACK = (data?: any) => void | ||
type EVENT_TYPE = 'success' | 'fail' | 'start' | 'end' | 'timeout' | 'abort' | 'progress'; | ||
type EVENT_TYPE = 'success' | 'fail' | 'start' | 'end' | 'timeout' | 'abort' | 'progress' | 'beforeSend'; | ||
type AjaxEvents = Record<EVENT_TYPE, CALLBACK[]>; | ||
@@ -12,3 +12,3 @@ interface HttpHeader { | ||
interface AjaxOptions { | ||
isAsync?: boolean; // 默认:true | ||
isAsync?: boolean; | ||
cacheSize?: number; | ||
@@ -25,3 +25,4 @@ cacheExp?: number; | ||
withCredentials?: boolean; | ||
query?: Record<string, string | number> | ||
query?: Record<string, string | number>; | ||
autoAbort?: boolean; | ||
} | ||
@@ -28,0 +29,0 @@ |
@@ -12,2 +12,4 @@ import isFunction from 'lodash/isFunction'; | ||
import formatData from './formatData'; | ||
import handleAutoAbort from './handleAutoAbort'; | ||
const win = window as Record<string, any>; | ||
@@ -69,3 +71,3 @@ class ExtendAjax { | ||
} | ||
private addXHREventListener (xhr:XMLHttpRequest, that: any) { | ||
private addXHREventListener (xhr:XMLHttpRequest) { | ||
const { | ||
@@ -112,3 +114,3 @@ convert, | ||
res, | ||
exp: ExtendAjax.options.cacheExp * 1000, | ||
exp: ExtendAjax.options.cacheExp, | ||
time: +new Date() | ||
@@ -137,3 +139,3 @@ }; | ||
script.type = 'text/javascript'; | ||
console.log(script); | ||
if (timeout) { | ||
@@ -154,3 +156,2 @@ this.timer = window.setTimeout(() => { | ||
script.onload = () => { | ||
console.log('加载完成'); | ||
if (!script.getAttribute('data-load')) { | ||
@@ -170,3 +171,2 @@ this.emit('fail'); | ||
document.body.append(script); | ||
console.log(script, script.src); | ||
} | ||
@@ -192,3 +192,3 @@ private getXHR(): XMLHttpRequest { | ||
events.forEach((cb) => { | ||
cb.call(this.options.scope || null, data); | ||
cb.apply(this.options.scope || null, data); | ||
}); | ||
@@ -213,4 +213,5 @@ } | ||
} | ||
this.addXHREventListener(this.xhr, this); | ||
this.addXHREventListener(this.xhr); | ||
}); | ||
this.emit('beforeSend', this.options); | ||
const { | ||
@@ -227,2 +228,3 @@ query, | ||
let url = host + this.url; | ||
if (this.xhr) { | ||
@@ -259,3 +261,3 @@ const cacheKey = getHashKey({ | ||
this.emit('abort'); | ||
} else { // jsonp | ||
} else if (this.method === 'jsonp') { // jsonp | ||
const { jsonpName } = this.options; | ||
@@ -275,3 +277,10 @@ if (jsonpName && win[jsonpName]) { | ||
const ajax = function (url: string, ...configs: AjaxConfig):ExtendAjax { | ||
return new ExtendAjax(url, ...configs); | ||
const _ajax = new ExtendAjax(url, ...configs); | ||
_ajax.on('beforeSend', function (options: AjaxOptions) { | ||
if (!options.autoAbort) { | ||
return; | ||
} | ||
handleAutoAbort(_ajax); | ||
}); | ||
return _ajax; | ||
} | ||
@@ -283,1 +292,2 @@ | ||
export default ajax; | ||
export type { ExtendAjax }; |
44027
1.12%19
5.56%647
6.77%161
0.63%