ajax-client
Advanced tools
Comparing version
/*! For license information please see ajax-client.js.LICENSE */ | ||
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var s in r)("object"==typeof exports?exports:t)[s]=r[s]}}(this,(function(){return function(t){var e={};function r(s){if(e[s])return e[s].exports;var n=e[s]={i:s,l:!1,exports:{}};return t[s].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=t,r.c=e,r.d=function(t,e,s){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)r.d(s,n,function(e){return t[e]}.bind(null,n));return s},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="/",r(r.s=0)}([function(t,e,r){t.exports=r(9)},function(t,e,r){"use strict";var s=r(2),n=r(5);function o(){this.protocol=null,this.slashes=null,this.auth=null,this.host=null,this.port=null,this.hostname=null,this.hash=null,this.search=null,this.query=null,this.pathname=null,this.path=null,this.href=null}e.parse=v,e.resolve=function(t,e){return v(t,!1,!0).resolve(e)},e.resolveObject=function(t,e){return t?v(t,!1,!0).resolveObject(e):e},e.format=function(t){n.isString(t)&&(t=v(t));return t instanceof o?t.format():o.prototype.format.call(t)},e.Url=o;var a=/^([a-z0-9.+-]+:)/i,i=/:[0-9]*$/,c=/^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,h=["{","}","|","\\","^","`"].concat(["<",">",'"',"`"," ","\r","\n","\t"]),u=["'"].concat(h),p=["%","/","?",";","#"].concat(u),l=["/","?","#"],f=/^[+a-z0-9A-Z_-]{0,63}$/,d=/^([+a-z0-9A-Z_-]{0,63})(.*)$/,y={javascript:!0,"javascript:":!0},m={javascript:!0,"javascript:":!0},g={http:!0,https:!0,ftp:!0,gopher:!0,file:!0,"http:":!0,"https:":!0,"ftp:":!0,"gopher:":!0,"file:":!0},x=r(6);function v(t,e,r){if(t&&n.isObject(t)&&t instanceof o)return t;var s=new o;return s.parse(t,e,r),s}o.prototype.parse=function(t,e,r){if(!n.isString(t))throw new TypeError("Parameter 'url' must be a string, not "+typeof t);var o=t.indexOf("?"),i=-1!==o&&o<t.indexOf("#")?"?":"#",h=t.split(i);h[0]=h[0].replace(/\\/g,"/");var v=t=h.join(i);if(v=v.trim(),!r&&1===t.split("#").length){var j=c.exec(v);if(j)return this.path=v,this.href=v,this.pathname=j[1],j[2]?(this.search=j[2],this.query=e?x.parse(this.search.substr(1)):this.search.substr(1)):e&&(this.search="",this.query={}),this}var b=a.exec(v);if(b){var w=(b=b[0]).toLowerCase();this.protocol=w,v=v.substr(b.length)}if(r||b||v.match(/^\/\/[^@\/]+@[^@\/]+/)){var O="//"===v.substr(0,2);!O||b&&m[b]||(v=v.substr(2),this.slashes=!0)}if(!m[b]&&(O||b&&!g[b])){for(var T,C,_=-1,P=0;P<l.length;P++){-1!==(S=v.indexOf(l[P]))&&(-1===_||S<_)&&(_=S)}-1!==(C=-1===_?v.lastIndexOf("@"):v.lastIndexOf("@",_))&&(T=v.slice(0,C),v=v.slice(C+1),this.auth=decodeURIComponent(T)),_=-1;for(P=0;P<p.length;P++){var S;-1!==(S=v.indexOf(p[P]))&&(-1===_||S<_)&&(_=S)}-1===_&&(_=v.length),this.host=v.slice(0,_),v=v.slice(_),this.parseHost(),this.hostname=this.hostname||"";var A="["===this.hostname[0]&&"]"===this.hostname[this.hostname.length-1];if(!A)for(var I=this.hostname.split(/\./),U=(P=0,I.length);P<U;P++){var E=I[P];if(E&&!E.match(f)){for(var q="",F=0,R=E.length;F<R;F++)E.charCodeAt(F)>127?q+="x":q+=E[F];if(!q.match(f)){var k=I.slice(0,P),L=I.slice(P+1),N=E.match(d);N&&(k.push(N[1]),L.unshift(N[2])),L.length&&(v="/"+L.join(".")+v),this.hostname=k.join(".");break}}}this.hostname.length>255?this.hostname="":this.hostname=this.hostname.toLowerCase(),A||(this.hostname=s.toASCII(this.hostname));var M=this.port?":"+this.port:"",J=this.hostname||"";this.host=J+M,this.href+=this.host,A&&(this.hostname=this.hostname.substr(1,this.hostname.length-2),"/"!==v[0]&&(v="/"+v))}if(!y[w])for(P=0,U=u.length;P<U;P++){var H=u[P];if(-1!==v.indexOf(H)){var D=encodeURIComponent(H);D===H&&(D=escape(H)),v=v.split(H).join(D)}}var $=v.indexOf("#");-1!==$&&(this.hash=v.substr($),v=v.slice(0,$));var z=v.indexOf("?");if(-1!==z?(this.search=v.substr(z),this.query=v.substr(z+1),e&&(this.query=x.parse(this.query)),v=v.slice(0,z)):e&&(this.search="",this.query={}),v&&(this.pathname=v),g[w]&&this.hostname&&!this.pathname&&(this.pathname="/"),this.pathname||this.search){M=this.pathname||"";var B=this.search||"";this.path=M+B}return this.href=this.format(),this},o.prototype.format=function(){var t=this.auth||"";t&&(t=(t=encodeURIComponent(t)).replace(/%3A/i,":"),t+="@");var e=this.protocol||"",r=this.pathname||"",s=this.hash||"",o=!1,a="";this.host?o=t+this.host:this.hostname&&(o=t+(-1===this.hostname.indexOf(":")?this.hostname:"["+this.hostname+"]"),this.port&&(o+=":"+this.port)),this.query&&n.isObject(this.query)&&Object.keys(this.query).length&&(a=x.stringify(this.query));var i=this.search||a&&"?"+a||"";return e&&":"!==e.substr(-1)&&(e+=":"),this.slashes||(!e||g[e])&&!1!==o?(o="//"+(o||""),r&&"/"!==r.charAt(0)&&(r="/"+r)):o||(o=""),s&&"#"!==s.charAt(0)&&(s="#"+s),i&&"?"!==i.charAt(0)&&(i="?"+i),e+o+(r=r.replace(/[?#]/g,(function(t){return encodeURIComponent(t)})))+(i=i.replace("#","%23"))+s},o.prototype.resolve=function(t){return this.resolveObject(v(t,!1,!0)).format()},o.prototype.resolveObject=function(t){if(n.isString(t)){var e=new o;e.parse(t,!1,!0),t=e}for(var r=new o,s=Object.keys(this),a=0;a<s.length;a++){var i=s[a];r[i]=this[i]}if(r.hash=t.hash,""===t.href)return r.href=r.format(),r;if(t.slashes&&!t.protocol){for(var c=Object.keys(t),h=0;h<c.length;h++){var u=c[h];"protocol"!==u&&(r[u]=t[u])}return g[r.protocol]&&r.hostname&&!r.pathname&&(r.path=r.pathname="/"),r.href=r.format(),r}if(t.protocol&&t.protocol!==r.protocol){if(!g[t.protocol]){for(var p=Object.keys(t),l=0;l<p.length;l++){var f=p[l];r[f]=t[f]}return r.href=r.format(),r}if(r.protocol=t.protocol,t.host||m[t.protocol])r.pathname=t.pathname;else{for(var d=(t.pathname||"").split("/");d.length&&!(t.host=d.shift()););t.host||(t.host=""),t.hostname||(t.hostname=""),""!==d[0]&&d.unshift(""),d.length<2&&d.unshift(""),r.pathname=d.join("/")}if(r.search=t.search,r.query=t.query,r.host=t.host||"",r.auth=t.auth,r.hostname=t.hostname||t.host,r.port=t.port,r.pathname||r.search){var y=r.pathname||"",x=r.search||"";r.path=y+x}return r.slashes=r.slashes||t.slashes,r.href=r.format(),r}var v=r.pathname&&"/"===r.pathname.charAt(0),j=t.host||t.pathname&&"/"===t.pathname.charAt(0),b=j||v||r.host&&t.pathname,w=b,O=r.pathname&&r.pathname.split("/")||[],T=(d=t.pathname&&t.pathname.split("/")||[],r.protocol&&!g[r.protocol]);if(T&&(r.hostname="",r.port=null,r.host&&(""===O[0]?O[0]=r.host:O.unshift(r.host)),r.host="",t.protocol&&(t.hostname=null,t.port=null,t.host&&(""===d[0]?d[0]=t.host:d.unshift(t.host)),t.host=null),b=b&&(""===d[0]||""===O[0])),j)r.host=t.host||""===t.host?t.host:r.host,r.hostname=t.hostname||""===t.hostname?t.hostname:r.hostname,r.search=t.search,r.query=t.query,O=d;else if(d.length)O||(O=[]),O.pop(),O=O.concat(d),r.search=t.search,r.query=t.query;else if(!n.isNullOrUndefined(t.search)){if(T)r.hostname=r.host=O.shift(),(A=!!(r.host&&r.host.indexOf("@")>0)&&r.host.split("@"))&&(r.auth=A.shift(),r.host=r.hostname=A.shift());return r.search=t.search,r.query=t.query,n.isNull(r.pathname)&&n.isNull(r.search)||(r.path=(r.pathname?r.pathname:"")+(r.search?r.search:"")),r.href=r.format(),r}if(!O.length)return r.pathname=null,r.search?r.path="/"+r.search:r.path=null,r.href=r.format(),r;for(var C=O.slice(-1)[0],_=(r.host||t.host||O.length>1)&&("."===C||".."===C)||""===C,P=0,S=O.length;S>=0;S--)"."===(C=O[S])?O.splice(S,1):".."===C?(O.splice(S,1),P++):P&&(O.splice(S,1),P--);if(!b&&!w)for(;P--;P)O.unshift("..");!b||""===O[0]||O[0]&&"/"===O[0].charAt(0)||O.unshift(""),_&&"/"!==O.join("/").substr(-1)&&O.push("");var A,I=""===O[0]||O[0]&&"/"===O[0].charAt(0);T&&(r.hostname=r.host=I?"":O.length?O.shift():"",(A=!!(r.host&&r.host.indexOf("@")>0)&&r.host.split("@"))&&(r.auth=A.shift(),r.host=r.hostname=A.shift()));return(b=b||r.host&&O.length)&&!I&&O.unshift(""),O.length?r.pathname=O.join("/"):(r.pathname=null,r.path=null),n.isNull(r.pathname)&&n.isNull(r.search)||(r.path=(r.pathname?r.pathname:"")+(r.search?r.search:"")),r.auth=t.auth||r.auth,r.slashes=r.slashes||t.slashes,r.href=r.format(),r},o.prototype.parseHost=function(){var t=this.host,e=i.exec(t);e&&(":"!==(e=e[0])&&(this.port=e.substr(1)),t=t.substr(0,t.length-e.length)),t&&(this.hostname=t)}},function(t,e,r){(function(t,s){var n;!function(o){e&&e.nodeType,t&&t.nodeType;var a="object"==typeof s&&s;a.global!==a&&a.window!==a&&a.self;var i,c=2147483647,h=/^xn--/,u=/[^\x20-\x7E]/,p=/[\x2E\u3002\uFF0E\uFF61]/g,l={overflow:"Overflow: input needs wider integers to process","not-basic":"Illegal input >= 0x80 (not a basic code point)","invalid-input":"Invalid input"},f=Math.floor,d=String.fromCharCode;function y(t){throw new RangeError(l[t])}function m(t,e){for(var r=t.length,s=[];r--;)s[r]=e(t[r]);return s}function g(t,e){var r=t.split("@"),s="";return r.length>1&&(s=r[0]+"@",t=r[1]),s+m((t=t.replace(p,".")).split("."),e).join(".")}function x(t){for(var e,r,s=[],n=0,o=t.length;n<o;)(e=t.charCodeAt(n++))>=55296&&e<=56319&&n<o?56320==(64512&(r=t.charCodeAt(n++)))?s.push(((1023&e)<<10)+(1023&r)+65536):(s.push(e),n--):s.push(e);return s}function v(t){return m(t,(function(t){var e="";return t>65535&&(e+=d((t-=65536)>>>10&1023|55296),t=56320|1023&t),e+=d(t)})).join("")}function j(t,e){return t+22+75*(t<26)-((0!=e)<<5)}function b(t,e,r){var s=0;for(t=r?f(t/700):t>>1,t+=f(t/e);t>455;s+=36)t=f(t/35);return f(s+36*t/(t+38))}function w(t){var e,r,s,n,o,a,i,h,u,p,l,d=[],m=t.length,g=0,x=128,j=72;for((r=t.lastIndexOf("-"))<0&&(r=0),s=0;s<r;++s)t.charCodeAt(s)>=128&&y("not-basic"),d.push(t.charCodeAt(s));for(n=r>0?r+1:0;n<m;){for(o=g,a=1,i=36;n>=m&&y("invalid-input"),((h=(l=t.charCodeAt(n++))-48<10?l-22:l-65<26?l-65:l-97<26?l-97:36)>=36||h>f((c-g)/a))&&y("overflow"),g+=h*a,!(h<(u=i<=j?1:i>=j+26?26:i-j));i+=36)a>f(c/(p=36-u))&&y("overflow"),a*=p;j=b(g-o,e=d.length+1,0==o),f(g/e)>c-x&&y("overflow"),x+=f(g/e),g%=e,d.splice(g++,0,x)}return v(d)}function O(t){var e,r,s,n,o,a,i,h,u,p,l,m,g,v,w,O=[];for(m=(t=x(t)).length,e=128,r=0,o=72,a=0;a<m;++a)(l=t[a])<128&&O.push(d(l));for(s=n=O.length,n&&O.push("-");s<m;){for(i=c,a=0;a<m;++a)(l=t[a])>=e&&l<i&&(i=l);for(i-e>f((c-r)/(g=s+1))&&y("overflow"),r+=(i-e)*g,e=i,a=0;a<m;++a)if((l=t[a])<e&&++r>c&&y("overflow"),l==e){for(h=r,u=36;!(h<(p=u<=o?1:u>=o+26?26:u-o));u+=36)w=h-p,v=36-p,O.push(d(j(p+w%v,0))),h=f(w/v);O.push(d(j(h,0))),o=b(r,g,s==n),r=0,++s}++r,++e}return O.join("")}i={version:"1.4.1",ucs2:{decode:x,encode:v},decode:w,encode:O,toASCII:function(t){return g(t,(function(t){return u.test(t)?"xn--"+O(t):t}))},toUnicode:function(t){return g(t,(function(t){return h.test(t)?w(t.slice(4).toLowerCase()):t}))}},void 0===(n=function(){return i}.call(e,r,e,t))||(t.exports=n)}()}).call(this,r(3)(t),r(4))},function(t,e){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children||(t.children=[]),Object.defineProperty(t,"loaded",{enumerable:!0,get:function(){return t.l}}),Object.defineProperty(t,"id",{enumerable:!0,get:function(){return t.i}}),t.webpackPolyfill=1),t}},function(t,e){var r;r=function(){return this}();try{r=r||new Function("return this")()}catch(t){"object"==typeof window&&(r=window)}t.exports=r},function(t,e,r){"use strict";t.exports={isString:function(t){return"string"==typeof t},isObject:function(t){return"object"==typeof t&&null!==t},isNull:function(t){return null===t},isNullOrUndefined:function(t){return null==t}}},function(t,e,r){"use strict";e.decode=e.parse=r(7),e.encode=e.stringify=r(8)},function(t,e,r){"use strict";function s(t,e){return Object.prototype.hasOwnProperty.call(t,e)}t.exports=function(t,e,r,o){e=e||"&",r=r||"=";var a={};if("string"!=typeof t||0===t.length)return a;var i=/\+/g;t=t.split(e);var c=1e3;o&&"number"==typeof o.maxKeys&&(c=o.maxKeys);var h=t.length;c>0&&h>c&&(h=c);for(var u=0;u<h;++u){var p,l,f,d,y=t[u].replace(i,"%20"),m=y.indexOf(r);m>=0?(p=y.substr(0,m),l=y.substr(m+1)):(p=y,l=""),f=decodeURIComponent(p),d=decodeURIComponent(l),s(a,f)?n(a[f])?a[f].push(d):a[f]=[a[f],d]:a[f]=d}return a};var n=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)}},function(t,e,r){"use strict";var s=function(t){switch(typeof t){case"string":return t;case"boolean":return t?"true":"false";case"number":return isFinite(t)?t:"";default:return""}};t.exports=function(t,e,r,i){return e=e||"&",r=r||"=",null===t&&(t=void 0),"object"==typeof t?o(a(t),(function(a){var i=encodeURIComponent(s(a))+r;return n(t[a])?o(t[a],(function(t){return i+encodeURIComponent(s(t))})).join(e):i+encodeURIComponent(s(t[a]))})).join(e):i?encodeURIComponent(s(i))+r+encodeURIComponent(s(t)):""};var n=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function o(t,e){if(t.map)return t.map(e);for(var r=[],s=0;s<t.length;s++)r.push(e(t[s],s));return r}var a=Object.keys||function(t){var e=[];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.push(r);return e}},function(t,e,r){"use strict";r.r(e),r.d(e,"AjaxClient",(function(){return s})),r.d(e,"AjaxClient2",(function(){return o}));class s{constructor(){}getAsync(t){t.type="get",this.ajax(t)}postAsync(t){t.type="post",this.ajax(t)}async get(t){if(t.type="get","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=t=>{e({success:!0,data:t})},t.error=t=>{e({success:!1,cause:"error",error:t})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t})},this.ajax(t)})}async post(t){if(t.type="post","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=t=>{e({success:!0,data:t})},t.error=t=>{e({success:!1,cause:"error",error:t})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t})},this.ajax(t)})}ajax(t){const e=new XMLHttpRequest;if(!t)throw Error("Please specify options like #ajax(options)");if(!t.url)throw Error("Please specify url.");if(t.type&&"post"===t.type.toLowerCase())e.open("POST",t.url,!0);else{if(!t.type||"get"!==t.type.toLowerCase())throw Error("type:".concat(t.type," is not supported"));e.open("GET",t.url,!0)}if(t.dataType&&"json"===t.dataType)e.responseType="text";else{if(!t.dataType||"text"!==t.dataType)throw Error("Please check dataType:".concat(t.dataType,'. "json" or "text" is supported as dataType now.'));e.responseType="text"}if(t.contentType)try{e.setRequestHeader("Content-Type",t.contentType)}catch(e){throw Error("Invalid content type ".concat(t.contentType))}else if(t.type&&"post"===t.type.toLowerCase()||t.type&&"put"===t.type.toLowerCase())throw Error("Please specify contentType.");if(t.headers)for(const r in t.headers){const s=t.headers[r];e.setRequestHeader(r,s)}if(t.xhrFields){const{xhrFields:r}=t;!0===r.withCredentials&&(e.withCredentials=!0)}if(t.timeoutMillis&&(e.timeout=t.timeoutMillis),e.onload=r=>{if(200==e.status){let r="";r="json"==t.dataType?JSON.parse(e.response):e.response,t.success&&t.success(r,e)}else t.error&&t.error(r,e)},t.timeout&&(e.ontimeout=r=>{t.timeout(r,e)}),t.error&&(e.onerror=r=>{t.error(r,e)}),t.type&&"post"===t.type.toLowerCase()){if(!t.data)throw Error('.data is not specified.data must be specified on "POST" mode.');if(t.contentType.startsWith("application/x-www-form-urlencoded")){const r=(t=>{const e=[];for(const r in t){const s=t[r],n=encodeURIComponent(r)+"="+encodeURIComponent(s);e.push(n)}return e.join("&").replace(/%20/g,"+")})(t.data);e.send(r)}else e.send(t.data)}else t.type&&"get"===t.type.toLowerCase()&&e.send(null)}typeOf(t){return Object.prototype.toString.call(t).slice(8,-1)}}const{URLSearchParams:n}=r(1);class o{constructor(t){this.opt=t||{}}getAsync(t){t.type="get",this.ajax(t)}postAsync(t){t.type="post",this.ajax(t)}async get(t){if(t.type="get","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=t=>{e({success:!0,data:t})},t.error=t=>{e({success:!1,cause:"error",error:t})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t})},this.ajax(t)})}async post(t){if(t.type="post","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=t=>{e({success:!0,data:t})},t.error=t=>{e({success:!1,cause:"error",error:t})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t})},this.ajax(t)})}ajax(t){const e=this._createUrl(t),r=t.type,s=t.dataType,n=t.data,o=t.headers,a=t.contentType;let i=null;if(!e)throw Error("Please specify url.");if(r&&"post"===r.toLowerCase()&&(i=n,"jsonp"===s))throw new Error("type:'POST' and 'dataType:jsonp' are specified together.\n 'POST' and 'jsonp' can not be specified together");if(o&&"jsonp"===s)throw new Error("'headers' and 'dataType:jsonp' can not be specified together.\n Http headers cannot be sent when using jsonp.");const c={url:e,method:r,body:i,contentType:a};if(o&&(c.headers=o),"json"===s||"text"===s)return this._handleData(c,s,t);if("jsonp"===s)return this._handleJsonp(c,t);throw new Error("Please check dataType:".concat(s," dataType must be 'json' or 'jsonp'"))}_getFetchParam(t,e){const r=t.method.toUpperCase(),s={method:r,mode:"cors",cache:"no-cache",headers:{}};if(t.contentType)s.headers["Content-Type"]=t.contentType;else if("POST"===r||"PUT"===r)throw Error("Please specify contentType.");if(e.xhrFields){const{xhrFields:t}=e;!0===t.withCredentials&&(s.credentials="include")}if(t.credentials&&(s.credentials=t.credentials),t.headers)for(const e in t.headers){const r=t.headers[e];s.headers[e]=r}if(t.body)if(t.contentType.startsWith("application/x-www-form-urlencoded")){const e=t=>{const e=[];for(const r in t){const s=t[r],n=encodeURIComponent(r)+"="+encodeURIComponent(s);e.push(n)}return e.join("&").replace(/%20/g,"+")},r=e(t.body);s.body=r}else s.body=t.body;return s}_handleData(t,e,r){const s=new a,n=this._getFetchParam(t,r);return((t,r)=>new Promise((s,n)=>{r&&r.timeoutMillis&&setTimeout(()=>{n("timeout")},r.timeoutMillis),t.then(t=>{if(!t.ok){const e=t.statusText;n({data:e,response:t})}"json"===e&&t.json().then(e=>{s({data:e,response:t})}),"text"===e&&t.text().then(e=>{s({data:e,response:t})})}).catch(t=>{n(t)})}))((this.opt.fetch||fetch)(t.url,n),r).then(t=>{s._success(t.data),r&&r.success&&r.success(t.data,t.response)}).catch(t=>{const e=t;s._fail(e),"timeout"===t?r&&r.timeout&&r.timeout():r&&r.error&&r.error(t,t.response)}),s}_handleJsonp(t,e){const r=new a,s=document.createElement("script"),n="ajaxclient2_".concat(this._createUUID());let o=t.url;o.indexOf("?")>=0?o+="&callback=".concat(n):o+="?callback=".concat(n),s.src=o,s.addEventListener("error",t=>{r._fail(t),e&&e.error&&e.error(t,null)}),window[n]=t=>{delete window[n],r._success(t),e&&e.success&&e.success(t,null)};return(document.getElementsByTagName("head")?document.getElementsByTagName("head")[0]:document.body).appendChild(s),r}_createUrl(t){if(t.type&&"post"===t.type.toLowerCase())return t.url;if(t.type&&"get"===t.type.toLowerCase()){let e=t.url;if(t.data){const r=JSON.parse(t.data);e+="?";for(let t of Object.keys(r)){const s=r[t];e+="".concat(t,"=").concat(s,"&")}e=e.substring(0,e.length-1)}return e}throw Error("Invalid type ".concat(t.type," is not supported"))}_createUUID(){const t=(new Date).getTime();return"xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const r=(t+16*Math.random())%16|0;return("x"==e?r:3&r|8).toString(16)}))}typeOf(t){return Object.prototype.toString.call(t).slice(8,-1)}}class a{constructor(){this._successFunc=()=>{},this._failFunc=()=>{}}done(t){return this._successFunc=t,this}fail(t){return this._failFunc=t,this}_success(t){this._successFunc&&this._successFunc(t)}_fail(t){this._failFunc&&this._failFunc(t)}}}])})); | ||
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var s in r)("object"==typeof exports?exports:t)[s]=r[s]}}(this,(function(){return function(t){var e={};function r(s){if(e[s])return e[s].exports;var n=e[s]={i:s,l:!1,exports:{}};return t[s].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=t,r.c=e,r.d=function(t,e,s){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)r.d(s,n,function(e){return t[e]}.bind(null,n));return s},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="/",r(r.s=0)}([function(t,e,r){t.exports=r(9)},function(t,e,r){"use strict";var s=r(2),n=r(5);function o(){this.protocol=null,this.slashes=null,this.auth=null,this.host=null,this.port=null,this.hostname=null,this.hash=null,this.search=null,this.query=null,this.pathname=null,this.path=null,this.href=null}e.parse=v,e.resolve=function(t,e){return v(t,!1,!0).resolve(e)},e.resolveObject=function(t,e){return t?v(t,!1,!0).resolveObject(e):e},e.format=function(t){n.isString(t)&&(t=v(t));return t instanceof o?t.format():o.prototype.format.call(t)},e.Url=o;var a=/^([a-z0-9.+-]+:)/i,i=/:[0-9]*$/,c=/^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,h=["{","}","|","\\","^","`"].concat(["<",">",'"',"`"," ","\r","\n","\t"]),u=["'"].concat(h),p=["%","/","?",";","#"].concat(u),l=["/","?","#"],f=/^[+a-z0-9A-Z_-]{0,63}$/,d=/^([+a-z0-9A-Z_-]{0,63})(.*)$/,y={javascript:!0,"javascript:":!0},m={javascript:!0,"javascript:":!0},g={http:!0,https:!0,ftp:!0,gopher:!0,file:!0,"http:":!0,"https:":!0,"ftp:":!0,"gopher:":!0,"file:":!0},x=r(6);function v(t,e,r){if(t&&n.isObject(t)&&t instanceof o)return t;var s=new o;return s.parse(t,e,r),s}o.prototype.parse=function(t,e,r){if(!n.isString(t))throw new TypeError("Parameter 'url' must be a string, not "+typeof t);var o=t.indexOf("?"),i=-1!==o&&o<t.indexOf("#")?"?":"#",h=t.split(i);h[0]=h[0].replace(/\\/g,"/");var v=t=h.join(i);if(v=v.trim(),!r&&1===t.split("#").length){var j=c.exec(v);if(j)return this.path=v,this.href=v,this.pathname=j[1],j[2]?(this.search=j[2],this.query=e?x.parse(this.search.substr(1)):this.search.substr(1)):e&&(this.search="",this.query={}),this}var b=a.exec(v);if(b){var w=(b=b[0]).toLowerCase();this.protocol=w,v=v.substr(b.length)}if(r||b||v.match(/^\/\/[^@\/]+@[^@\/]+/)){var O="//"===v.substr(0,2);!O||b&&m[b]||(v=v.substr(2),this.slashes=!0)}if(!m[b]&&(O||b&&!g[b])){for(var T,C,_=-1,P=0;P<l.length;P++){-1!==(S=v.indexOf(l[P]))&&(-1===_||S<_)&&(_=S)}-1!==(C=-1===_?v.lastIndexOf("@"):v.lastIndexOf("@",_))&&(T=v.slice(0,C),v=v.slice(C+1),this.auth=decodeURIComponent(T)),_=-1;for(P=0;P<p.length;P++){var S;-1!==(S=v.indexOf(p[P]))&&(-1===_||S<_)&&(_=S)}-1===_&&(_=v.length),this.host=v.slice(0,_),v=v.slice(_),this.parseHost(),this.hostname=this.hostname||"";var A="["===this.hostname[0]&&"]"===this.hostname[this.hostname.length-1];if(!A)for(var I=this.hostname.split(/\./),U=(P=0,I.length);P<U;P++){var E=I[P];if(E&&!E.match(f)){for(var q="",F=0,R=E.length;F<R;F++)E.charCodeAt(F)>127?q+="x":q+=E[F];if(!q.match(f)){var k=I.slice(0,P),L=I.slice(P+1),N=E.match(d);N&&(k.push(N[1]),L.unshift(N[2])),L.length&&(v="/"+L.join(".")+v),this.hostname=k.join(".");break}}}this.hostname.length>255?this.hostname="":this.hostname=this.hostname.toLowerCase(),A||(this.hostname=s.toASCII(this.hostname));var M=this.port?":"+this.port:"",J=this.hostname||"";this.host=J+M,this.href+=this.host,A&&(this.hostname=this.hostname.substr(1,this.hostname.length-2),"/"!==v[0]&&(v="/"+v))}if(!y[w])for(P=0,U=u.length;P<U;P++){var H=u[P];if(-1!==v.indexOf(H)){var D=encodeURIComponent(H);D===H&&(D=escape(H)),v=v.split(H).join(D)}}var $=v.indexOf("#");-1!==$&&(this.hash=v.substr($),v=v.slice(0,$));var z=v.indexOf("?");if(-1!==z?(this.search=v.substr(z),this.query=v.substr(z+1),e&&(this.query=x.parse(this.query)),v=v.slice(0,z)):e&&(this.search="",this.query={}),v&&(this.pathname=v),g[w]&&this.hostname&&!this.pathname&&(this.pathname="/"),this.pathname||this.search){M=this.pathname||"";var B=this.search||"";this.path=M+B}return this.href=this.format(),this},o.prototype.format=function(){var t=this.auth||"";t&&(t=(t=encodeURIComponent(t)).replace(/%3A/i,":"),t+="@");var e=this.protocol||"",r=this.pathname||"",s=this.hash||"",o=!1,a="";this.host?o=t+this.host:this.hostname&&(o=t+(-1===this.hostname.indexOf(":")?this.hostname:"["+this.hostname+"]"),this.port&&(o+=":"+this.port)),this.query&&n.isObject(this.query)&&Object.keys(this.query).length&&(a=x.stringify(this.query));var i=this.search||a&&"?"+a||"";return e&&":"!==e.substr(-1)&&(e+=":"),this.slashes||(!e||g[e])&&!1!==o?(o="//"+(o||""),r&&"/"!==r.charAt(0)&&(r="/"+r)):o||(o=""),s&&"#"!==s.charAt(0)&&(s="#"+s),i&&"?"!==i.charAt(0)&&(i="?"+i),e+o+(r=r.replace(/[?#]/g,(function(t){return encodeURIComponent(t)})))+(i=i.replace("#","%23"))+s},o.prototype.resolve=function(t){return this.resolveObject(v(t,!1,!0)).format()},o.prototype.resolveObject=function(t){if(n.isString(t)){var e=new o;e.parse(t,!1,!0),t=e}for(var r=new o,s=Object.keys(this),a=0;a<s.length;a++){var i=s[a];r[i]=this[i]}if(r.hash=t.hash,""===t.href)return r.href=r.format(),r;if(t.slashes&&!t.protocol){for(var c=Object.keys(t),h=0;h<c.length;h++){var u=c[h];"protocol"!==u&&(r[u]=t[u])}return g[r.protocol]&&r.hostname&&!r.pathname&&(r.path=r.pathname="/"),r.href=r.format(),r}if(t.protocol&&t.protocol!==r.protocol){if(!g[t.protocol]){for(var p=Object.keys(t),l=0;l<p.length;l++){var f=p[l];r[f]=t[f]}return r.href=r.format(),r}if(r.protocol=t.protocol,t.host||m[t.protocol])r.pathname=t.pathname;else{for(var d=(t.pathname||"").split("/");d.length&&!(t.host=d.shift()););t.host||(t.host=""),t.hostname||(t.hostname=""),""!==d[0]&&d.unshift(""),d.length<2&&d.unshift(""),r.pathname=d.join("/")}if(r.search=t.search,r.query=t.query,r.host=t.host||"",r.auth=t.auth,r.hostname=t.hostname||t.host,r.port=t.port,r.pathname||r.search){var y=r.pathname||"",x=r.search||"";r.path=y+x}return r.slashes=r.slashes||t.slashes,r.href=r.format(),r}var v=r.pathname&&"/"===r.pathname.charAt(0),j=t.host||t.pathname&&"/"===t.pathname.charAt(0),b=j||v||r.host&&t.pathname,w=b,O=r.pathname&&r.pathname.split("/")||[],T=(d=t.pathname&&t.pathname.split("/")||[],r.protocol&&!g[r.protocol]);if(T&&(r.hostname="",r.port=null,r.host&&(""===O[0]?O[0]=r.host:O.unshift(r.host)),r.host="",t.protocol&&(t.hostname=null,t.port=null,t.host&&(""===d[0]?d[0]=t.host:d.unshift(t.host)),t.host=null),b=b&&(""===d[0]||""===O[0])),j)r.host=t.host||""===t.host?t.host:r.host,r.hostname=t.hostname||""===t.hostname?t.hostname:r.hostname,r.search=t.search,r.query=t.query,O=d;else if(d.length)O||(O=[]),O.pop(),O=O.concat(d),r.search=t.search,r.query=t.query;else if(!n.isNullOrUndefined(t.search)){if(T)r.hostname=r.host=O.shift(),(A=!!(r.host&&r.host.indexOf("@")>0)&&r.host.split("@"))&&(r.auth=A.shift(),r.host=r.hostname=A.shift());return r.search=t.search,r.query=t.query,n.isNull(r.pathname)&&n.isNull(r.search)||(r.path=(r.pathname?r.pathname:"")+(r.search?r.search:"")),r.href=r.format(),r}if(!O.length)return r.pathname=null,r.search?r.path="/"+r.search:r.path=null,r.href=r.format(),r;for(var C=O.slice(-1)[0],_=(r.host||t.host||O.length>1)&&("."===C||".."===C)||""===C,P=0,S=O.length;S>=0;S--)"."===(C=O[S])?O.splice(S,1):".."===C?(O.splice(S,1),P++):P&&(O.splice(S,1),P--);if(!b&&!w)for(;P--;P)O.unshift("..");!b||""===O[0]||O[0]&&"/"===O[0].charAt(0)||O.unshift(""),_&&"/"!==O.join("/").substr(-1)&&O.push("");var A,I=""===O[0]||O[0]&&"/"===O[0].charAt(0);T&&(r.hostname=r.host=I?"":O.length?O.shift():"",(A=!!(r.host&&r.host.indexOf("@")>0)&&r.host.split("@"))&&(r.auth=A.shift(),r.host=r.hostname=A.shift()));return(b=b||r.host&&O.length)&&!I&&O.unshift(""),O.length?r.pathname=O.join("/"):(r.pathname=null,r.path=null),n.isNull(r.pathname)&&n.isNull(r.search)||(r.path=(r.pathname?r.pathname:"")+(r.search?r.search:"")),r.auth=t.auth||r.auth,r.slashes=r.slashes||t.slashes,r.href=r.format(),r},o.prototype.parseHost=function(){var t=this.host,e=i.exec(t);e&&(":"!==(e=e[0])&&(this.port=e.substr(1)),t=t.substr(0,t.length-e.length)),t&&(this.hostname=t)}},function(t,e,r){(function(t,s){var n;!function(o){e&&e.nodeType,t&&t.nodeType;var a="object"==typeof s&&s;a.global!==a&&a.window!==a&&a.self;var i,c=2147483647,h=/^xn--/,u=/[^\x20-\x7E]/,p=/[\x2E\u3002\uFF0E\uFF61]/g,l={overflow:"Overflow: input needs wider integers to process","not-basic":"Illegal input >= 0x80 (not a basic code point)","invalid-input":"Invalid input"},f=Math.floor,d=String.fromCharCode;function y(t){throw new RangeError(l[t])}function m(t,e){for(var r=t.length,s=[];r--;)s[r]=e(t[r]);return s}function g(t,e){var r=t.split("@"),s="";return r.length>1&&(s=r[0]+"@",t=r[1]),s+m((t=t.replace(p,".")).split("."),e).join(".")}function x(t){for(var e,r,s=[],n=0,o=t.length;n<o;)(e=t.charCodeAt(n++))>=55296&&e<=56319&&n<o?56320==(64512&(r=t.charCodeAt(n++)))?s.push(((1023&e)<<10)+(1023&r)+65536):(s.push(e),n--):s.push(e);return s}function v(t){return m(t,(function(t){var e="";return t>65535&&(e+=d((t-=65536)>>>10&1023|55296),t=56320|1023&t),e+=d(t)})).join("")}function j(t,e){return t+22+75*(t<26)-((0!=e)<<5)}function b(t,e,r){var s=0;for(t=r?f(t/700):t>>1,t+=f(t/e);t>455;s+=36)t=f(t/35);return f(s+36*t/(t+38))}function w(t){var e,r,s,n,o,a,i,h,u,p,l,d=[],m=t.length,g=0,x=128,j=72;for((r=t.lastIndexOf("-"))<0&&(r=0),s=0;s<r;++s)t.charCodeAt(s)>=128&&y("not-basic"),d.push(t.charCodeAt(s));for(n=r>0?r+1:0;n<m;){for(o=g,a=1,i=36;n>=m&&y("invalid-input"),((h=(l=t.charCodeAt(n++))-48<10?l-22:l-65<26?l-65:l-97<26?l-97:36)>=36||h>f((c-g)/a))&&y("overflow"),g+=h*a,!(h<(u=i<=j?1:i>=j+26?26:i-j));i+=36)a>f(c/(p=36-u))&&y("overflow"),a*=p;j=b(g-o,e=d.length+1,0==o),f(g/e)>c-x&&y("overflow"),x+=f(g/e),g%=e,d.splice(g++,0,x)}return v(d)}function O(t){var e,r,s,n,o,a,i,h,u,p,l,m,g,v,w,O=[];for(m=(t=x(t)).length,e=128,r=0,o=72,a=0;a<m;++a)(l=t[a])<128&&O.push(d(l));for(s=n=O.length,n&&O.push("-");s<m;){for(i=c,a=0;a<m;++a)(l=t[a])>=e&&l<i&&(i=l);for(i-e>f((c-r)/(g=s+1))&&y("overflow"),r+=(i-e)*g,e=i,a=0;a<m;++a)if((l=t[a])<e&&++r>c&&y("overflow"),l==e){for(h=r,u=36;!(h<(p=u<=o?1:u>=o+26?26:u-o));u+=36)w=h-p,v=36-p,O.push(d(j(p+w%v,0))),h=f(w/v);O.push(d(j(h,0))),o=b(r,g,s==n),r=0,++s}++r,++e}return O.join("")}i={version:"1.4.1",ucs2:{decode:x,encode:v},decode:w,encode:O,toASCII:function(t){return g(t,(function(t){return u.test(t)?"xn--"+O(t):t}))},toUnicode:function(t){return g(t,(function(t){return h.test(t)?w(t.slice(4).toLowerCase()):t}))}},void 0===(n=function(){return i}.call(e,r,e,t))||(t.exports=n)}()}).call(this,r(3)(t),r(4))},function(t,e){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children||(t.children=[]),Object.defineProperty(t,"loaded",{enumerable:!0,get:function(){return t.l}}),Object.defineProperty(t,"id",{enumerable:!0,get:function(){return t.i}}),t.webpackPolyfill=1),t}},function(t,e){var r;r=function(){return this}();try{r=r||new Function("return this")()}catch(t){"object"==typeof window&&(r=window)}t.exports=r},function(t,e,r){"use strict";t.exports={isString:function(t){return"string"==typeof t},isObject:function(t){return"object"==typeof t&&null!==t},isNull:function(t){return null===t},isNullOrUndefined:function(t){return null==t}}},function(t,e,r){"use strict";e.decode=e.parse=r(7),e.encode=e.stringify=r(8)},function(t,e,r){"use strict";function s(t,e){return Object.prototype.hasOwnProperty.call(t,e)}t.exports=function(t,e,r,o){e=e||"&",r=r||"=";var a={};if("string"!=typeof t||0===t.length)return a;var i=/\+/g;t=t.split(e);var c=1e3;o&&"number"==typeof o.maxKeys&&(c=o.maxKeys);var h=t.length;c>0&&h>c&&(h=c);for(var u=0;u<h;++u){var p,l,f,d,y=t[u].replace(i,"%20"),m=y.indexOf(r);m>=0?(p=y.substr(0,m),l=y.substr(m+1)):(p=y,l=""),f=decodeURIComponent(p),d=decodeURIComponent(l),s(a,f)?n(a[f])?a[f].push(d):a[f]=[a[f],d]:a[f]=d}return a};var n=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)}},function(t,e,r){"use strict";var s=function(t){switch(typeof t){case"string":return t;case"boolean":return t?"true":"false";case"number":return isFinite(t)?t:"";default:return""}};t.exports=function(t,e,r,i){return e=e||"&",r=r||"=",null===t&&(t=void 0),"object"==typeof t?o(a(t),(function(a){var i=encodeURIComponent(s(a))+r;return n(t[a])?o(t[a],(function(t){return i+encodeURIComponent(s(t))})).join(e):i+encodeURIComponent(s(t[a]))})).join(e):i?encodeURIComponent(s(i))+r+encodeURIComponent(s(t)):""};var n=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function o(t,e){if(t.map)return t.map(e);for(var r=[],s=0;s<t.length;s++)r.push(e(t[s],s));return r}var a=Object.keys||function(t){var e=[];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.push(r);return e}},function(t,e,r){"use strict";r.r(e),r.d(e,"AjaxClient",(function(){return s})),r.d(e,"AjaxClient2",(function(){return o}));class s{constructor(){}getAsync(t){t.type="get",this.ajax(t)}postAsync(t){t.type="post",this.ajax(t)}async get(t){if(t.type="get","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=(t,r)=>{e({success:!0,data:t,response:r})},t.error=(t,r)=>{e({success:!1,cause:"error",error:t,response:r})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t,response:null})},this.ajax(t)})}async post(t){if(t.type="post","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=t=>{e({success:!0,data:t})},t.error=t=>{e({success:!1,cause:"error",error:t})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t})},this.ajax(t)})}ajax(t){const e=new XMLHttpRequest;if(!t)throw Error("Please specify options like #ajax(options)");if(!t.url)throw Error("Please specify url.");if(t.type&&"post"===t.type.toLowerCase())e.open("POST",t.url,!0);else{if(!t.type||"get"!==t.type.toLowerCase())throw Error("type:".concat(t.type," is not supported"));e.open("GET",t.url,!0)}if(t.dataType&&"json"===t.dataType)e.responseType="text";else{if(!t.dataType||"text"!==t.dataType)throw Error("Please check dataType:".concat(t.dataType,'. "json" or "text" is supported as dataType now.'));e.responseType="text"}if(t.contentType)try{e.setRequestHeader("Content-Type",t.contentType)}catch(e){throw Error("Invalid content type ".concat(t.contentType))}else if(t.type&&"post"===t.type.toLowerCase()||t.type&&"put"===t.type.toLowerCase())throw Error("Please specify contentType.");if(t.headers)for(const r in t.headers){const s=t.headers[r];e.setRequestHeader(r,s)}if(t.xhrFields){const{xhrFields:r}=t;!0===r.withCredentials&&(e.withCredentials=!0)}if(t.timeoutMillis&&(e.timeout=t.timeoutMillis),e.onload=r=>{if(200==e.status){let r="";r="json"==t.dataType?JSON.parse(e.response):e.response,t.success&&t.success(r,e)}else t.error&&t.error(r,e)},t.timeout&&(e.ontimeout=r=>{t.timeout(r,e)}),t.error&&(e.onerror=r=>{t.error(r,e)}),t.type&&"post"===t.type.toLowerCase()){if(!t.data)throw Error('.data is not specified.data must be specified on "POST" mode.');if(t.contentType.startsWith("application/x-www-form-urlencoded")){const r=(t=>{const e=[];for(const r in t){const s=t[r],n=encodeURIComponent(r)+"="+encodeURIComponent(s);e.push(n)}return e.join("&").replace(/%20/g,"+")})(t.data);e.send(r)}else e.send(t.data)}else t.type&&"get"===t.type.toLowerCase()&&e.send(null)}typeOf(t){return Object.prototype.toString.call(t).slice(8,-1)}}const{URLSearchParams:n}=r(1);class o{constructor(t){this.opt=t||{}}getAsync(t){t.type="get",this.ajax(t)}postAsync(t){t.type="post",this.ajax(t)}async get(t){if(t.type="get","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=(t,r)=>{e({success:!0,data:t,response:r})},t.error=(t,r)=>{e({success:!1,cause:"error",error:t,response:r})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t,response:null})},this.ajax(t)})}async post(t){if(t.type="post","application/json"===t.contentType)if("String"===this.typeOf(t.data));else{const e=t.data;t.data=JSON.stringify(e)}return new Promise(e=>{t.success=(t,r)=>{e({success:!0,data:t,response:r})},t.error=(t,r)=>{e({success:!1,cause:"error",error:t,response:r})},t.timeout=t=>{e({success:!1,cause:"timeout",error:t,response:null})},this.ajax(t)})}ajax(t){const e=this._createUrl(t),r=t.type,s=t.dataType,n=t.data,o=t.headers,a=t.contentType;let i=null;if(!e)throw Error("Please specify url.");if(r&&"post"===r.toLowerCase()&&(i=n,"jsonp"===s))throw new Error("type:'POST' and 'dataType:jsonp' are specified together.\n 'POST' and 'jsonp' can not be specified together");if(o&&"jsonp"===s)throw new Error("'headers' and 'dataType:jsonp' can not be specified together.\n Http headers cannot be sent when using jsonp.");const c={url:e,method:r,body:i,contentType:a};if(o&&(c.headers=o),"json"===s||"text"===s)return this._handleData(c,s,t);if("jsonp"===s)return this._handleJsonp(c,t);throw new Error("Please check dataType:".concat(s," dataType must be 'json' or 'jsonp'"))}_getFetchParam(t,e){const r=t.method.toUpperCase(),s={method:r,mode:"cors",cache:"no-cache",headers:{}};if(t.contentType)s.headers["Content-Type"]=t.contentType;else if("POST"===r||"PUT"===r)throw Error("Please specify contentType.");if(e.xhrFields){const{xhrFields:t}=e;!0===t.withCredentials&&(s.credentials="include")}if(t.credentials&&(s.credentials=t.credentials),t.headers)for(const e in t.headers){const r=t.headers[e];s.headers[e]=r}if(t.body)if(t.contentType.startsWith("application/x-www-form-urlencoded")){const e=t=>{const e=[];for(const r in t){const s=t[r],n=encodeURIComponent(r)+"="+encodeURIComponent(s);e.push(n)}return e.join("&").replace(/%20/g,"+")},r=e(t.body);s.body=r}else s.body=t.body;return s}_handleData(t,e,r){const s=new a,n=this._getFetchParam(t,r);return((t,r)=>new Promise((s,n)=>{r&&r.timeoutMillis&&setTimeout(()=>{n("timeout")},r.timeoutMillis),t.then(t=>{if(!t.ok){const e=t.statusText;n({data:e,response:t})}"json"===e&&t.json().then(e=>{s({data:e,response:t})}),"text"===e&&t.text().then(e=>{s({data:e,response:t})})}).catch(t=>{n(t)})}))((this.opt.fetch||fetch)(t.url,n),r).then(t=>{s._success(t.data),r&&r.success&&r.success(t.data,t.response)}).catch(t=>{const e=t;s._fail(e),"timeout"===t?r&&r.timeout&&r.timeout():r&&r.error&&r.error(t,t.response)}),s}_handleJsonp(t,e){const r=new a,s=document.createElement("script"),n="ajaxclient2_".concat(this._createUUID());let o=t.url;o.indexOf("?")>=0?o+="&callback=".concat(n):o+="?callback=".concat(n),s.src=o,s.addEventListener("error",t=>{r._fail(t),e&&e.error&&e.error(t,null)}),window[n]=t=>{delete window[n],r._success(t),e&&e.success&&e.success(t,null)};return(document.getElementsByTagName("head")?document.getElementsByTagName("head")[0]:document.body).appendChild(s),r}_createUrl(t){if(t.type&&"post"===t.type.toLowerCase())return t.url;if(t.type&&"get"===t.type.toLowerCase()){let e=t.url;if(t.data){const r=JSON.parse(t.data);e+="?";for(let t of Object.keys(r)){const s=r[t];e+="".concat(t,"=").concat(s,"&")}e=e.substring(0,e.length-1)}return e}throw Error("Invalid type ".concat(t.type," is not supported"))}_createUUID(){const t=(new Date).getTime();return"xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const r=(t+16*Math.random())%16|0;return("x"==e?r:3&r|8).toString(16)}))}typeOf(t){return Object.prototype.toString.call(t).slice(8,-1)}}class a{constructor(){this._successFunc=()=>{},this._failFunc=()=>{}}done(t){return this._successFunc=t,this}fail(t){return this._failFunc=t,this}_success(t){this._successFunc&&this._successFunc(t)}_fail(t){this._failFunc&&this._failFunc(t)}}}])})); |
{ | ||
"name": "ajax-client", | ||
"version": "1.5.8", | ||
"version": "1.6.0", | ||
"description": "A simple ajax client with 'jquery-like' API", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajax-client.js", |
@@ -22,14 +22,21 @@ # ajax-client | ||
- AjaxClient is based on the XmlHttpRequest2 | ||
- AjaxClient2(**recommended**) is base on fetch API | ||
```javascript | ||
import {AjaxClient} from 'ajax-client' | ||
import {AjaxClient2 as AjaxClient} from 'ajax-client' | ||
``` | ||
- AjaxClient2 is base on fetch API | ||
```javascript | ||
import ajax_client from 'ajax-client'; | ||
const { AjaxClient2 } = ajax_client; | ||
``` | ||
- AjaxClient is based on the XmlHttpRequest2 | ||
```javascript | ||
import {AjaxClient2 as AjaxClient} from 'ajax-client' | ||
import {AjaxClient} from 'ajax-client' | ||
``` | ||
- use from CDN | ||
@@ -49,3 +56,3 @@ | ||
const client = new AjaxClient(); | ||
const client = new AjaxClient2(); | ||
@@ -87,2 +94,3 @@ //Data object to send | ||
```javascript | ||
const client = new AjaxClient2(); | ||
const data = { | ||
@@ -124,3 +132,4 @@ message: "hello" | ||
```javascript | ||
const result = await client.post({ | ||
const client = new AjaxClient2(); | ||
const result = await client.post({ | ||
url: 'http://localhost:9999/api', | ||
@@ -138,2 +147,8 @@ headers: { | ||
```js | ||
console.log(result.success); // true if success | ||
console.log(result.data);// get JSON-parsed data | ||
console.log(result.response.status);// get status code 200 | ||
``` | ||
#### Success Response | ||
@@ -145,2 +160,3 @@ | ||
data:{ },// response payload from server | ||
response:{}, // get raw response.You can get response.status,response.statusText etc. | ||
} | ||
@@ -151,2 +167,5 @@ ``` | ||
- server error | ||
```javascript | ||
@@ -157,10 +176,22 @@ { | ||
error:e,// error object | ||
response:{}, // get raw response.You can get response.status,response.statusText etc. | ||
} | ||
``` | ||
- timeout error | ||
```javascript | ||
{ | ||
success: false; | ||
cause:'timeout',// 'error' or 'timeout' | ||
error:e,// error object | ||
response:null, | ||
} | ||
``` | ||
### Get | ||
```javascript | ||
const client = new AjaxClient(); | ||
const client = new AjaxClient2(); | ||
@@ -181,7 +212,14 @@ client.ajax({ | ||
```js | ||
console.log(result.success); // true if success | ||
console.log(result.data);// get JSON-parsed data | ||
console.log(result.response.status);// get status code 200 | ||
``` | ||
### Get with Async/Await | ||
```javascript | ||
const result = await client.get({ | ||
const client = new AjaxClient2(); | ||
const result = await client.get({ | ||
url: 'http://localhost:9999/api', | ||
@@ -203,2 +241,4 @@ headers: { | ||
data:{ },// response payload from server | ||
response:{}, // get raw response.You can get response.status,response.statusText etc. | ||
} | ||
@@ -209,2 +249,4 @@ ``` | ||
- server error | ||
```javascript | ||
@@ -215,5 +257,17 @@ { | ||
error:e,// error object | ||
response:{}, // get raw response.You can get response.status,response.statusText etc. | ||
} | ||
``` | ||
- timeout error | ||
```javascript | ||
{ | ||
success: false; | ||
cause:'timeout',// 'error' or 'timeout' | ||
error:e,// error object | ||
response:null, | ||
} | ||
``` | ||
## Example (using ajax-client) | ||
@@ -232,3 +286,3 @@ | ||
<script> | ||
const ajax = new AjaxClient(); | ||
const ajax = new AjaxClient2(); | ||
@@ -235,0 +289,0 @@ //Data object to send |
38539
3.37%443
13.88%10
-9.09%