@chunpu/http
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -210,2 +210,30 @@ [![CircleCI](https://circleci.com/gh/chunpu/http.svg?style=svg)](https://circleci.com/gh/chunpu/http) | ||
Cancel Requests | ||
--- | ||
compatible with axios Cancellation | ||
source is sth like deferred | ||
```js | ||
const source = http.CancelToken.source() | ||
http.get('/very/slow/api/1', { | ||
cancelToken: source.token | ||
}).catch(err => { | ||
console.error(err) // too slow, abort it | ||
}) | ||
http.get('/very/slow/api/2', { | ||
cancelToken: source.token | ||
}).catch(err => { | ||
console.error(err) // too slow, abort it | ||
}) | ||
setTimeout(() => { | ||
source.cancel('too slow, abort it') // will cancel all requests with this source | ||
}, 1000) | ||
``` | ||
Usage With Real Project | ||
@@ -212,0 +240,0 @@ --- |
@@ -1,2 +0,5 @@ | ||
/*! @chunpu/http@1.0.1 by chunpu */ | ||
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.http=t():n.http=t()}(window,function(){return function(n){var t={};function e(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return n[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:r})},e.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},e.t=function(n,t){if(1&t&&(n=e(n)),8&t)return n;if(4&t&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&t&&"string"!=typeof n)for(var i in n)e.d(r,i,function(t){return n[t]}.bind(null,i));return r},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,"a",t),t},e.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},e.p="",e(e.s=3)}([function(n,t,e){n.exports=e(5)},function(n,t,e){var r=e(6),i=[].slice,u=t;u.is=r,u.extend=u.assign=function(n){if(n){var t=i.call(arguments,1);c(t,function(t){h(t,function(t,e){r.undef(t)||(n[e]=t)})})}return n},u.each=c,u.map=function(n,t){var e=[];return c(n,function(n,r,i){e[r]=t(n,r,i)}),e},u.filter=function(n,t){var e=[];return c(n,function(n,r,i){t(n,r,i)&&e.push(n)}),e},u.some=function(n,t){return-1!=f(n,t)},u.every=function(n,t){return-1==f(n,l(t))},u.reduce=function(n,t,e){return c(n,function(r,i){e=t(e,r,i,n)}),e},u.findIndex=f,u.find=function(n,t){var e=u.findIndex(n,t);if(-1!=e)return n[e]},u.indexOf=p,u.includes=function(n,t){return-1!=p(n,t)},u.toArray=s,u.slice=function(n,t,e){var i=[],u=a(n);return u>=0&&(t=t||0,0!==e&&(e=e||u),r.fn(n.slice)||(n=s(n)),i=n.slice(t,e)),i},u.negate=l,u.forIn=h,u.keys=function(n){var t=[];return h(n,function(n,e){t.push(e)}),t};var o=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;function a(n){if(null!=n)return n.length}function c(n,t){var e=a(n);if(e&&r.fn(t))for(var i=0;i<e&&!1!==t(n[i],i,n);i++);return n}function f(n,t){var e=-1;return c(n,function(n,r,i){if(t(n,r,i))return e=r,!1}),e}function s(n){var t=[];return c(n,function(n){t.push(n)}),t}function l(n){return function(){return!n.apply(this,arguments)}}function p(n,t){return r.string(n)?n.indexOf(t):f(n,function(n){return t===n})}function h(n,t){if(n)for(var e in n)if(r.owns(n,e)&&!1===t(n[e],e,n))break;return n}u.trim=function(n){return null==n?"":(""+n).replace(o,"")},u.noop=function(){},u.len=a},function(n,t,e){var r=e(0),i=r.is,u={sep:"&",eq:"=",encode:encodeURIComponent,decode:decodeURIComponent,keepRaw:!1,sort:null,ignoreValues:[void 0]};function o(n,t,e){return e=r.find(arguments,function(n){return i.object(n)}),n=i.nos(n)?n:void 0,t=i.nos(t)?t:void 0,e=r.extend({},u,e,{sep:n,eq:t})}t.parse=function(n,t,e,i){n+="";var u=(i=o(t,e,i)).decode;return n=n.split(i.sep),r.reduce(n,function(n,t){if(2==(t=t.split(i.eq)).length){var e=t[0],r=t[1];if(!i.keepRaw)try{e=u(e),r=u(r)}catch(n){}n[e]=r}return n},{})},t.stringify=function(n,t,e,u){u=o(t,e,u);var a=r.keys(n),c=u.sort;c&&(i.fn(c)?a.sort(c):a.sort());var f=u.encode,s=[];return r.each(a,function(t){var e=n[t];r.includes(u.ignoreValues,e)||((i.nan(e)||null==e)&&(e=""),u.keepRaw||(t=f(t),e=f(e)),s.push(t+u.eq+e))}),s.join(u.sep)}},function(n,t,e){n.exports=e(4)},function(n,t,e){const r=e(0),i=new(e(16));function u(...n){return i.request(...n)}for(var o in n.exports=u,i){var a=i[o];r.isFunction(a)&&(a=r.bind(a,i)),u[o]=a}},function(n,t,e){var r=e(1);function i(n){if(!(this instanceof i))return new i(n);this.__value=n,this.__chain=!1}n.exports=r.extend(i,r),e(8)(i),e(9)(i),e(10)(i),e(11)(i),e(12)(i),e(14)(i),e(15)(i),i.mixin(i,i)},function(n,t,e){(function(n){var e=t,r=Object.prototype,i=(n=n||{}).navigator;function u(n){var t=r.toString.call(n);return t.substring(8,t.length-1).toLowerCase()}function o(n){return typeof n}function a(n,t){return r.hasOwnProperty.call(n,t)}e.browser=function(){return!(e.wechatApp()||!i||n.window!=n)},e.h5=function(){return!(!e.browser()||!i.geolocation)},e.mobile=function(){return!(!e.browser()||!/mobile/i.test(i.userAgent))},e.wechatApp=function(){return!("object"!=typeof wx||!wx||!e.fn(wx.createVideoContext))},e._class=u,e._type=o,e.owns=a,e.nan=function(n){return n!=n},e.bool=function(n){return"boolean"==u(n)},e.infinite=function(n){return n==1/0||n==-1/0},e.number=function(n){return!isNaN(n)&&"number"==u(n)},e.iod=function(n){return!(!e.number(n)||e.infinite(n))},e.decimal=function(n){return!!e.iod(n)&&0!=n%1},e.integer=function(n){return!!e.iod(n)&&0==n%1},e.oof=function(n){if(n){var t=o(n);return"object"==t||"function"==t}return!1},e.object=function(n){return e.oof(n)&&"function"!=u(n)},e.hash=e.plainObject=function(n){return!(!n||"object"!=u(n))&&(!n.nodeType&&!n.setInterval)},e.undef=function(n){return"undefined"==o(n)},e.fn=function(n){return"function"==u(n)},e.string=function(n){return"string"==u(n)},e.nos=function(n){return e.iod(n)||e.string(n)},e.array=function(n){return"array"==u(n)},e.arraylike=function(n){if(!e.window(n)&&e.object(n)){var t=n.length;if(e.integer(t)&&t>=0)return!0}return!1},e.window=function(n){return!(!n||n.window!=n)},e.empty=function(n){if(e.string(n)||e.arraylike(n))return 0===n.length;if(e.hash(n))for(var t in n)if(a(n,t))return!1;return!0},e.element=function(n){return!(!n||1!==n.nodeType)},e.regexp=function(n){return"regexp"==u(n)}}).call(this,e(7))},function(n,t){var e;e=function(){return this}();try{e=e||Function("return this")()||(0,eval)("this")}catch(n){"object"==typeof window&&(e=window)}n.exports=e},function(n,t){n.exports=function(n){var t=n.is;n.isString=t.string,n.isArray=t.array,n.isArrayLike=t.arraylike,n.isBoolean=t.bool,n.isElement=t.element,n.isEmpty=t.empty,n.isFunction=t.fn,n.isInteger=t.integer,n.isNaN=t.nan,n.isNumber=t.number,n.isObject=t.object,n.isPlainObject=t.plainObject,n.isRegExp=t.regexp,n.isString=t.string,n.isUndefined=t.undef}},function(n,t){n.exports=function(n){var t=n.is;n.now=function(){return+new Date},n.constant=function(n){return function(){return n}},n.identity=function(n){return n},n.random=function(n,t){return n+Math.floor(Math.random()*(t-n+1))},n.mixin=function(e,r,i){var u=n.functions(r);if(e)if(t.fn(e)){(i=i||{}).chain;var o=e.prototype;n.each(u,function(n){var t=r[n];o[n]=function(){var n=this,e=[n.__value];e.push.apply(e,arguments);var r=t.apply(n,e);return n.__chain?(n.__value=r,n):r}})}else n.each(u,function(n){e[n]=r[n]});return e},n.chain=function(t){var e=n(t);return e.__chain=!0,e},n.value=function(){return this.__chain=!1,this.__value}}},function(n,t){n.exports=function(n){var t=n.forEach=n.each,e=n.includes,r=n.is,i=Array.prototype;function u(t,e){var r=n.size(e);return t<0&&(t+=r),t<0&&(t=0),t>r&&(t=r),t||0}function o(t,e){var r=[],u=n.len(e);if(u)for(e=e.sort(function(n,t){return n-t});u--;){var o=e[u];r.push(i.splice.call(t,o,1)[0])}return r.reverse(),r}n.reject=function(t,e){return n.filter(t,function(n,t,r){return!e(n,t,r)})},n.without=function(t){var e=n.slice(arguments,1);return n.difference(t,e)},n.difference=function(t,r){var i=[];return n.each(t,function(n){e(r,n)||i.push(n)}),i},n.pluck=function(t,e){return n.map(t,function(n){if(n)return n[e]})},n.nth=function(t,e){return e=(e=u(e,t))||0,n.isString(t)?t.charAt(e):t[e]},n.first=function(t){if(t)return n.nth(t,0)},n.last=function(t){var e=n.len(t);if(e)return n.nth(t,e-1)},n.asyncMap=function(n,e,r){var i,u,o=[],a=0;t(n,function(t,c){u=!0,e(t,function(t,e){if(!i){if(a++,t)return i=!0,r(t);o[c]=e,a==n.length&&(i=!0,r(null,o))}})}),u||r(null)},n.uniq=function(t){return n.uniqBy(t)},n.uniqBy=function(n,i){var u=[],o=[];return r.fn(i)||(i=null),t(n,function(n){var t=n;i&&(t=i(n)),e(o,t)||(o.push(t),u.push(n))}),u},n.flatten=function(n){var e=[];return t(n,function(n){r.arraylike(n)?t(n,function(n){e.push(n)}):e.push(n)}),e},n.union=function(){return n.uniq(n.flatten(arguments))},n.sample=function(t,e){for(var r=n.toArray(t),i=r.length,u=Math.min(e||1,i),o=0;o<i;o++){var a=n.random(o,i-1),c=r[a];r[a]=r[o],r[o]=c}return r.length=u,null==e?r[0]:r},n.shuffle=function(t){return n.sample(t,1/0)},n.compact=function(t){return n.filter(t,n.identity)},n.rest=function(t){return n.slice(t,1)},n.invoke=function(){var t=arguments,e=t[0],i=t[1],u=r.fn(i);return t=n.slice(t,2),n.map(e,function(n){if(u)return i.apply(n,t);if(null!=n){var e=n[i];if(r.fn(e))return e.apply(n,t)}})},n.partition=function(t,e){var r=n.groupBy(t,function(n,t,r){return e(n,t,r)?1:2});return[r[1]||[],r[2]||[]]},n.groupBy=function(t,e){var r={};return n.each(t,function(n,t,i){var u=e(n,t,i);r[u]=r[u]||[],r[u].push(n)}),r},n.range=function(){var t=arguments;if(t.length<2)return n.range(t[1],t[0]);var e=t[0]||0,i=t[1]||0,u=t[2];r.number(u)||(u=1);var o=i-e;0!=u&&(o/=u);for(var a=[],c=e,f=0;f<o;f++)a.push(c),c+=u;return a},n.pullAt=function(t){return o(t,n.slice(arguments,1))},n.remove=function(t,e){for(var r=n.len(t)||0,i=[];r--;)e(t[r],r,t)&&i.push(r);return o(t,i)},n.fill=function(t,e,r,i){var o=n.size(t);r=u(r,t)||0,i=u(i,t)||o;for(var a=r;a<i;a++)t[a]=e;return t},n.size=function(t){var e=0;if(t){var r=t.length;n.isInteger(r)&&r>=0?e=r:n.isObject(t)&&(e=n.keys(t).length)}return e}}},function(n,t){n.exports=function(n){var t=n.is,e=(n.each,n.forIn);n.only=function(e,r){return e=e||{},t.string(r)&&(r=r.split(/ +/)),n.reduce(r,function(n,t){return null!=e[t]&&(n[t]=e[t]),n},{})},n.values=function(t){return n.map(n.keys(t),function(n){return t[n]})},n.pick=function(r,i){if(!t.fn(i))return n.pick(r,function(n,t){return t==i});var u={};return e(r,function(n,t,e){i(n,t,e)&&(u[t]=n)}),u},n.functions=function(e){return n.keys(n.pick(e,function(n){return t.fn(n)}))},n.mapKeys=function(n,t){var r={};return e(n,function(n,e,i){var u=t(n,e,i);r[u]=n}),r},n.mapObject=n.mapValues=function(n,t){var r={};return e(n,function(n,e,i){r[e]=t(n,e,i)}),r},n.get=function(t,e){if((e=u(e)).length&&n.every(e,function(n){if(null!=t)return t=t[n],!0}))return t},n.has=function(e,r){if((r=u(r)).length&&n.every(r,function(n){if(null!=e&&t.owns(e,n))return e=e[n],!0}))return!0;return!1},n.set=function(e,r,i){r=u(r);var o=e;return n.every(r,function(n,e){if(t.oof(o)){if(e+1!=r.length){if(null==(u=o[n])){var u={};~~n==n&&(u=[])}return o=o[n]=u,!0}o[n]=i}}),e},n.create=function(){function t(){}return function(e,r){return"object"!=typeof e&&(e=null),t.prototype=e,n.extend(new t,r)}}(),n.defaults=function(){var e=arguments,r=e[0],i=n.slice(e,1);return r&&n.each(i,function(e){n.mapObject(e,function(n,e){t.undef(r[e])&&(r[e]=n)})}),r},n.isMatch=function(n,t){var r=!0;return n=n||{},e(t,function(t,e){if(t!==n[e])return r=!1,!1}),r},n.toPlainObject=function(n){var t={};return e(n,function(n,e){t[e]=n}),t},n.invert=function(n){var t={};return e(n,function(n,e){t[n]=e}),t};var r=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g,i=/\\(\\)?/g;function u(e){if(t.array(e))return e;var u=[];return n.toString(e).replace(r,function(n,t,e,r){var o=t||n;e&&(o=r.replace(i,"$1")),u.push(o)}),u}}},function(n,t,e){n.exports=function(n){var t=n.is,r=n.slice;function i(n){var t=new i.Cache;function e(){var e=arguments,r=e[0];if(!t.has(r)){var i=n.apply(this,e);t.set(r,i)}return t.get(r)}return e.cache=t,e}n.bind=function(e,i){if(t.string(i)){var u=e;e=u[i],i=u}if(!t.fn(e))return e;var o=r(arguments,2);return i=i||this,function(){return e.apply(i,n.flatten([o,arguments]))}},n.inherits=function(t,e){t.super_=e,t.prototype=n.create(e.prototype,{constructor:t})},n.delay=function(t,e){var r=n.slice(arguments,2);return setTimeout(function(){t.apply(this,r)},e)},n.before=function(n,t){return function(){if(n>1)return n--,t.apply(this,arguments)}},n.once=function(t){return n.before(2,t)},n.after=function(n,t){return function(){if(!(n>1))return t.apply(this,arguments);n--}},n.throttle=function(t,e,r){return e=e||0,r=n.extend({leading:!0,trailing:!0,maxWait:e},r),n.debounce(t,e,r)},n.debounce=function(t,e,r){e=e||0;var i,u=(r=n.extend({leading:!1,trailing:!0},r)).maxWait,o=0,a=0,c=n.now();function f(t,e,r){return o=n.now(),t.apply(e,r)}function s(){i&&(clearTimeout(i),i=null)}function l(){var l=!((c=n.now())-o>e||u&&c-a>u);a=c;var p=this,h=arguments;s(),l?r.trailing&&(i=n.delay(function(){f(t,p,h)},e)):f(t,p,h)}return r.leading||(o=c),l.cancel=s,l},i.Cache=e(13),n.memoize=i,n.wrap=function(n,t){return function(){var e=[n];return e.push.apply(e,arguments),t.apply(this,e)}},n.curry=function(t){var e=t.length;return function r(i){return function(){var u=i.concat(n.slice(arguments));return u.length>=e?(u.length=e,t.apply(this,u)):r(u)}}([])}}},function(n,t,e){var r=e(1).is;function i(){this.data={}}n.exports=i;var u=i.prototype;u.has=function(n){return r.owns(this.data,n)},u.get=function(n){return this.data[n]},u.set=function(n,t){this.data[n]=t},u.delete=function(n){delete this.data[n]}},function(n,t){n.exports=function(n){n.tostr=n.toString=i;var t=n.indexOf;n.split=function(n,t,e){return(n=i(n)).split(t,e)},n.capitalize=function(n){return(n=i(n)).charAt(0).toUpperCase()+n.substr(1)},n.decapitalize=function(n){return(n=i(n)).charAt(0).toLowerCase()+n.substr(1)},n.camelCase=function(t){var e=(t=i(t)).split(/[^\w]|_+/);return e=n.map(e,function(t){return n.capitalize(t)}),n.decapitalize(e.join(""))},n.startsWith=function(n,e){return 0==t(n,e)},n.endsWith=function(t,e){return(e+="")==n.slice(t,n.len(t)-n.len(e))},n.toLower=n.lower=function(n){return i(n).toLowerCase()},n.toUpper=n.upper=function(n){return i(n).toUpperCase()},n.repeat=function(t,e){return n.map(n.range(e),function(){return t}).join("")},n.padStart=function(n,t,e){return n=i(n),r(e,(t=t||0)-n.length)+n},n.padEnd=function(n,t,e){return(n=i(n))+r(e,(t=t||0)-n.length)};var e={"&":"&","<":"<",">":">",'"':""","'":"'"};function r(t,e){t=i(t)||" ";var r=Math.floor(e/t.length)+1;return n.repeat(t,r).slice(0,e)}function i(n){return n||0==n?n+"":""}n.escape=function(n){return i(n).replace(/[&<>"']/g,function(n){return e[n]||n})},n.template=function(t){var e=['with(data) {var ret = ""'];n.each(n.split(t,"<%"),function(t,r){var i=t.split("%>");if(i[1])return function(t){var r=n.first(t);if("="===r||"-"===r){var i=n.slice(t,1);"-"===r&&(i="_.escape("+i+")"),e.push("ret += "+i)}else e.push(t)}(n.trim(i[0])),u(i[1]);u(i[0])}),e.push("return ret}");var r=new Function("data",e.join("\n")),i={_:n};return function(t){return r(n.extend({},i,t))};function u(n){e.push('ret += "'+n.replace(/('|"|\\)/g,"\\$1").replace(/\r/g,"\\r").replace(/\n/g,"\\n")+'"')}}}},function(n,t){n.exports=function(n){n.sum=function(t){return n.reduce(t,function(n,t){return n+t},0)},n.max=function(t,e){var r=-1,i=-1/0;return e=e||n.identity,n.each(t,function(n,t){(n=e(n))>i&&(i=n,r=t)}),r>-1?t[r]:i},n.min=function(t,e){var r=-1,i=1/0;return e=e||n.identity,n.each(t,function(n,t){(n=e(n))<i&&(i=n,r=t)}),r>-1?t[r]:i}}},function(n,t,e){const r=e(0),i=e(17),u=e(2),o=e(18),a=e(19),c="application/json",f="application/x-www-form-urlencoded",s=a.CONTENT_TYPE_KEY,l=["get","head","delete","options"],p=["post","put","patch"],h=[...l,...p];function d(n){this.defaults={baseURL:"",timeout:0,headers:{common:{}},withCredentials:!1},r.each(h,n=>{var t=this.defaults.headers[n]={};r.includes(p,"method")&&(t[n]=c)}),this.interceptors={request:new o,response:new o},this.qs=u,this.init(n)}d.qs=u;var v=d.prototype;v.init=function(n){n=r.extend({},n),this.defaults.headers.common=n.headers||{},delete n.headers,r.extend(this.defaults,n)},v.create=function(n){return new d(n)},v.request=function(n,t){if(r.isString(n))return this.request(r.extend({url:n},t));var e=n||{};e.headers=e.headers||{};var o=(e=r.extend({},this.defaults,e)).baseURL+e.url;o=i.appendQuery(o,e.params);var l=r.toLower(e.method)||"get",p=this.defaults.headers,h=r.extend({},p.common,p[l],e.headers),d=a.getContentType(h),v=d,m=e.data;r.isPlainObject(m)?(d===f?m=u.stringify(m):d===c&&(m=JSON.stringify(m)),v||r.isString(m)&&(v=f),r.isString(m)||(m=JSON.stringify(m),v=v||c),!d&&v&&(h[s]=v)):a.isFormData(m)&&delete h[s];var y=e.timeout;return e={url:o,data:m,headers:h,method:r.toUpper(l),withCredentials:e.withCredentials},y&&(e.timeout=y),Promise.resolve(e).then(n=>this.interceptors.request.exec(n)).then(n=>this.adapter(n)).then(n=>{if(r.isString(n.data)&&!this.axios){var t=n.data;try{n.data=JSON.parse(n.data)}catch(e){n.data=t}}return n.config=e,n.headers=r.mapKeys(n.headers,(n,t)=>r.toLower(t)),n}).then(n=>this.interceptors.response.exec(n))},v.adapter=function(n){var t=this.defaults;return t.wx?new Promise((e,r)=>{t.wx.request({url:n.url,data:n.data,header:n.headers,method:n.method,timeout:n.timeout,success(n){var{data:t,statusCode:r,header:i}=n;e({data:t,status:r,headers:i})},fail(n){r(n)}})}):t.axios?t.axios.request(n).then(n=>n):t.jQuery?new Promise((e,r)=>{t.jQuery.ajax({url:n.url,data:n.data,headers:n.headers,method:n.method,timeout:n.timeout,withCredentials:n.withCredentials,success(n,t,r){e({data:n,status:200,headers:a.parseHeadersFromXhr(r)})},error(n,t,e){r({errorThrown:e,textStatus:t,jqXHR:n})}})}):t.quickapp?new Promise((e,r)=>{t.quickapp.fetch({url:n.url,data:n.data,header:n.headers,method:n.method,timeout:n.timeout,success(n){var{data:t,code:r,headers:i}=n;e({data:t,status:r,headers:i})},fail(n,t){r({data:n,code:t})}})}):"function"==typeof XMLHttpRequest?new Promise((t,e)=>{var i=new XMLHttpRequest;i.onload=(n=>{t({status:i.status,data:i.responseText,headers:a.parseHeadersFromXhr(i)})}),i.ontimeout=(n=>{e(new Error("timeout"))}),i.onerror=(n=>{e(new Error("error"))}),i.open(n.method,n.url,!0),n.timeout&&(i.timeout=n.timeout),n.withCredentials&&(i.withCredentials=n.withCredentials),r.forIn(n.headers,(n,t)=>{i.setRequestHeader(t,n)}),i.send(n.data)}):void 0},r.each(l,n=>{v[n]=function(t,e){return this.request(r.extend({method:n,url:t},e))}}),r.each(p,n=>{v[n]=function(t,e,i){return this.request(r.extend({url:t,method:n,data:e},i))}}),n.exports=d},function(n,t,e){var r=e(2),i=e(0);t.parse=function(n,t){if("string"!=typeof n)return n;var e,u,a={};a.href=n,u=(e=o(n,"#"))[0],e[1]&&(a.hash="#"+e[1]),u=(e=function(n,t){var e="";return n=n.replace(t,function(n){return e=n,""}),[e,n]}(u,/^[a-zA-Z][a-zA-Z0-9+-.]*:/))[1],a.protocol=e[0].toLowerCase(),u=(e=o(u,"?"))[0];var c=e[1];if(t&&(c=r.parse(c)),a.query=c,"/"!=u.charAt(0)&&a.schema)return a.opaque=u,a;i.startsWith(u,"//")&&(e=o(u=u.slice(2),"/"),a.pathname="/"+unescape(e[1]||""),e=function(n){var t=o(n,"@"),e=t[0],r=t[1];r||(r=t[0],e=null);return[e,r]}(e[0]),a.auth=e[0],e=o(e[1],":"),a.hostname=e[0],a.port=~~e[1]);return a};var u="http https ftp gopher file".split(" ");function o(n,t){var e=[],r=i.indexOf(n,t);return-1==r?e[0]=n:(e[0]=n.slice(0,r),e[1]=n.slice(r+t.length)),e}t.format=function(n){if(!n||"object"!=typeof n)return n;var t=n.protocol,e=[t];t&&!i.includes(u,t.slice(0,t.length-1))||e.push("//"),n.auth&&e.push(n.auth,"@"),e.push(n.hostname),n.port&&e.push(":",n.port),e.push(n.pathname);var o=n.query;o&&("string"!=typeof o&&(o=r.stringify(o)),o&&e.push("?",o)),e.push(n.hash);for(var a=[],c=0;c<e.length;c++)e[c]&&a.push(e[c]);return a.join("")},t.appendQuery=function(n,t){var e=o(n,"#");n=e[0];var u=e[1];return i.isObject(t)&&(t=r.stringify(t)),i.includes(n,"?")?i.endsWith(n,"&")||i.endsWith(n,"?")||t&&(t="&"+t):t&&(t="?"+t),t&&(n+=t),u&&(n+="#"+u),n}},function(n,t,e){const r=e(0);function i(){this.queue=[]}n.exports=i,r.extend(i.prototype,{use(...n){return this.queue.push(n),this},exec(n){return r.reduce(this.queue,(n,t)=>n.then(...t),Promise.resolve(n))}})},function(n,t,e){const r=e(0),i=new RegExp("Content-Type","i");t.CONTENT_TYPE_KEY="Content-Type",t.getContentType=function(n){var t=r.keys(n);return n[r.find(t,n=>i.test(n))]},t.parseHeadersFromXhr=function(n){return r.chain(n.getAllResponseHeaders()).trim().split("\n").reduce((n,t)=>{var e=r.indexOf(t,":"),i=r.toLower(r.trim(r.slice(t,0,e))),u=r.trim(r.slice(t,e+1));return n[i]?n[i]=","+u:n[i]=u,n},{}).value()},t.isFormData=function(n){return"undefined"!=typeof FormData&&n instanceof FormData}}])}); | ||
/*! | ||
* @chunpu/http@1.2.0 by chunpu | ||
* 2018-10-22T07:25:51.141Z | ||
*/ | ||
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.http=t():n.http=t()}(window,function(){return function(n){var t={};function e(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return n[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:r})},e.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},e.t=function(n,t){if(1&t&&(n=e(n)),8&t)return n;if(4&t&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&t&&"string"!=typeof n)for(var i in n)e.d(r,i,function(t){return n[t]}.bind(null,i));return r},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,"a",t),t},e.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},e.p="",e(e.s=4)}([function(n,t,e){n.exports=e(6)},function(n,t,e){const r=e(0),i=new RegExp("Content-Type","i");t.CONTENT_TYPE_KEY="Content-Type",t.getContentType=function(n){var t=r.keys(n);return n[r.find(t,n=>i.test(n))]},t.parseHeadersFromXhr=function(n){return r.chain(n.getAllResponseHeaders()).trim().split("\n").reduce((n,t)=>{var e=r.indexOf(t,":"),i=r.toLower(r.trim(r.slice(t,0,e))),u=r.trim(r.slice(t,e+1));return n[i]?n[i]=","+u:n[i]=u,n},{}).value()},t.isFormData=function(n){return"undefined"!=typeof FormData&&n instanceof FormData},t.timeout=function n(t){return new Promise((e,r)=>{n&&setTimeout(()=>{r(new Error("timeout"))},t)})},t.clearTimer=function(n){n&&clearTimeout(n)},t.createError=function(n,t){var e=new Error(n);return r.extend(e,t),e}},function(n,t,e){var r=e(7),i=[].slice,u=t;u.is=r,u.extend=u.assign=function(n){if(n){var t=i.call(arguments,1);c(t,function(t){h(t,function(t,e){r.undef(t)||(n[e]=t)})})}return n},u.each=c,u.map=function(n,t){var e=[];return c(n,function(n,r,i){e[r]=t(n,r,i)}),e},u.filter=function(n,t){var e=[];return c(n,function(n,r,i){t(n,r,i)&&e.push(n)}),e},u.some=function(n,t){return-1!=f(n,t)},u.every=function(n,t){return-1==f(n,l(t))},u.reduce=function(n,t,e){return c(n,function(r,i){e=t(e,r,i,n)}),e},u.findIndex=f,u.find=function(n,t){var e=u.findIndex(n,t);if(-1!=e)return n[e]},u.indexOf=p,u.includes=function(n,t){return-1!=p(n,t)},u.toArray=s,u.slice=function(n,t,e){var i=[],u=a(n);return u>=0&&(t=t||0,0!==e&&(e=e||u),r.fn(n.slice)||(n=s(n)),i=n.slice(t,e)),i},u.negate=l,u.forIn=h,u.keys=function(n){var t=[];return h(n,function(n,e){t.push(e)}),t};var o=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;function a(n){if(null!=n)return n.length}function c(n,t){var e=a(n);if(e&&r.fn(t))for(var i=0;i<e&&!1!==t(n[i],i,n);i++);return n}function f(n,t){var e=-1;return c(n,function(n,r,i){if(t(n,r,i))return e=r,!1}),e}function s(n){var t=[];return c(n,function(n){t.push(n)}),t}function l(n){return function(){return!n.apply(this,arguments)}}function p(n,t){return r.string(n)?n.indexOf(t):f(n,function(n){return t===n})}function h(n,t){if(n)for(var e in n)if(r.owns(n,e)&&!1===t(n[e],e,n))break;return n}u.trim=function(n){return null==n?"":(""+n).replace(o,"")},u.noop=function(){},u.len=a},function(n,t,e){var r=e(0),i=r.is,u={sep:"&",eq:"=",encode:encodeURIComponent,decode:decodeURIComponent,keepRaw:!1,sort:null,ignoreValues:[void 0]};function o(n,t,e){return e=r.find(arguments,function(n){return i.object(n)}),n=i.nos(n)?n:void 0,t=i.nos(t)?t:void 0,e=r.extend({},u,e,{sep:n,eq:t})}t.parse=function(n,t,e,i){n+="";var u=(i=o(t,e,i)).decode;return n=n.split(i.sep),r.reduce(n,function(n,t){if(2==(t=t.split(i.eq)).length){var e=t[0],r=t[1];if(!i.keepRaw)try{e=u(e),r=u(r)}catch(n){}n[e]=r}return n},{})},t.stringify=function(n,t,e,u){u=o(t,e,u);var a=r.keys(n),c=u.sort;c&&(i.fn(c)?a.sort(c):a.sort());var f=u.encode,s=[];return r.each(a,function(t){var e=n[t];r.includes(u.ignoreValues,e)||((i.nan(e)||null==e)&&(e=""),u.keepRaw||(t=f(t),e=f(e)),s.push(t+u.eq+e))}),s.join(u.sep)}},function(n,t,e){n.exports=e(5)},function(n,t,e){e(0);const r=new(e(17));n.exports=r},function(n,t,e){var r=e(2);function i(n){if(!(this instanceof i))return new i(n);this.__value=n,this.__chain=!1}n.exports=r.extend(i,r),e(9)(i),e(10)(i),e(11)(i),e(12)(i),e(13)(i),e(15)(i),e(16)(i),i.mixin(i,i)},function(n,t,e){(function(n){var e=t,r=Object.prototype,i=(n=n||{}).navigator;function u(n){var t=r.toString.call(n);return t.substring(8,t.length-1).toLowerCase()}function o(n){return typeof n}function a(n,t){return r.hasOwnProperty.call(n,t)}e.browser=function(){return!(e.wechatApp()||!i||n.window!=n)},e.h5=function(){return!(!e.browser()||!i.geolocation)},e.mobile=function(){return!(!e.browser()||!/mobile/i.test(i.userAgent))},e.wechatApp=function(){return!("object"!=typeof wx||!wx||!e.fn(wx.createVideoContext))},e._class=u,e._type=o,e.owns=a,e.nan=function(n){return n!=n},e.bool=function(n){return"boolean"==u(n)},e.infinite=function(n){return n==1/0||n==-1/0},e.number=function(n){return!isNaN(n)&&"number"==u(n)},e.iod=function(n){return!(!e.number(n)||e.infinite(n))},e.decimal=function(n){return!!e.iod(n)&&0!=n%1},e.integer=function(n){return!!e.iod(n)&&0==n%1},e.oof=function(n){if(n){var t=o(n);return"object"==t||"function"==t}return!1},e.object=function(n){return e.oof(n)&&"function"!=u(n)},e.hash=e.plainObject=function(n){return!(!n||"object"!=u(n))&&(!n.nodeType&&!n.setInterval)},e.undef=function(n){return"undefined"==o(n)},e.fn=function(n){return"function"==u(n)},e.string=function(n){return"string"==u(n)},e.nos=function(n){return e.iod(n)||e.string(n)},e.array=function(n){return"array"==u(n)},e.arraylike=function(n){if(!e.window(n)&&e.object(n)){var t=n.length;if(e.integer(t)&&t>=0)return!0}return!1},e.window=function(n){return!(!n||n.window!=n)},e.empty=function(n){if(e.string(n)||e.arraylike(n))return 0===n.length;if(e.hash(n))for(var t in n)if(a(n,t))return!1;return!0},e.element=function(n){return!(!n||1!==n.nodeType)},e.regexp=function(n){return"regexp"==u(n)}}).call(this,e(8))},function(n,t){var e;e=function(){return this}();try{e=e||Function("return this")()||(0,eval)("this")}catch(n){"object"==typeof window&&(e=window)}n.exports=e},function(n,t){n.exports=function(n){var t=n.is;n.isString=t.string,n.isArray=t.array,n.isArrayLike=t.arraylike,n.isBoolean=t.bool,n.isElement=t.element,n.isEmpty=t.empty,n.isFunction=t.fn,n.isInteger=t.integer,n.isNaN=t.nan,n.isNumber=t.number,n.isObject=t.object,n.isPlainObject=t.plainObject,n.isRegExp=t.regexp,n.isString=t.string,n.isUndefined=t.undef}},function(n,t){n.exports=function(n){var t=n.is;n.now=function(){return+new Date},n.constant=function(n){return function(){return n}},n.identity=function(n){return n},n.random=function(n,t){return n+Math.floor(Math.random()*(t-n+1))},n.mixin=function(e,r,i){var u=n.functions(r);if(e)if(t.fn(e)){(i=i||{}).chain;var o=e.prototype;n.each(u,function(n){var t=r[n];o[n]=function(){var n=this,e=[n.__value];e.push.apply(e,arguments);var r=t.apply(n,e);return n.__chain?(n.__value=r,n):r}})}else n.each(u,function(n){e[n]=r[n]});return e},n.chain=function(t){var e=n(t);return e.__chain=!0,e},n.value=function(){return this.__chain=!1,this.__value}}},function(n,t){n.exports=function(n){var t=n.forEach=n.each,e=n.includes,r=n.is,i=Array.prototype;function u(t,e){var r=n.size(e);return t<0&&(t+=r),t<0&&(t=0),t>r&&(t=r),t||0}function o(t,e){var r=[],u=n.len(e);if(u)for(e=e.sort(function(n,t){return n-t});u--;){var o=e[u];r.push(i.splice.call(t,o,1)[0])}return r.reverse(),r}n.reject=function(t,e){return n.filter(t,function(n,t,r){return!e(n,t,r)})},n.without=function(t){var e=n.slice(arguments,1);return n.difference(t,e)},n.difference=function(t,r){var i=[];return n.each(t,function(n){e(r,n)||i.push(n)}),i},n.pluck=function(t,e){return n.map(t,function(n){if(n)return n[e]})},n.nth=function(t,e){return e=(e=u(e,t))||0,n.isString(t)?t.charAt(e):t[e]},n.first=function(t){if(t)return n.nth(t,0)},n.last=function(t){var e=n.len(t);if(e)return n.nth(t,e-1)},n.asyncMap=function(n,e,r){var i,u,o=[],a=0;t(n,function(t,c){u=!0,e(t,function(t,e){if(!i){if(a++,t)return i=!0,r(t);o[c]=e,a==n.length&&(i=!0,r(null,o))}})}),u||r(null)},n.uniq=function(t){return n.uniqBy(t)},n.uniqBy=function(n,i){var u=[],o=[];return r.fn(i)||(i=null),t(n,function(n){var t=n;i&&(t=i(n)),e(o,t)||(o.push(t),u.push(n))}),u},n.flatten=function(n){var e=[];return t(n,function(n){r.arraylike(n)?t(n,function(n){e.push(n)}):e.push(n)}),e},n.union=function(){return n.uniq(n.flatten(arguments))},n.sample=function(t,e){for(var r=n.toArray(t),i=r.length,u=Math.min(e||1,i),o=0;o<i;o++){var a=n.random(o,i-1),c=r[a];r[a]=r[o],r[o]=c}return r.length=u,null==e?r[0]:r},n.shuffle=function(t){return n.sample(t,1/0)},n.compact=function(t){return n.filter(t,n.identity)},n.rest=function(t){return n.slice(t,1)},n.invoke=function(){var t=arguments,e=t[0],i=t[1],u=r.fn(i);return t=n.slice(t,2),n.map(e,function(n){if(u)return i.apply(n,t);if(null!=n){var e=n[i];if(r.fn(e))return e.apply(n,t)}})},n.partition=function(t,e){var r=n.groupBy(t,function(n,t,r){return e(n,t,r)?1:2});return[r[1]||[],r[2]||[]]},n.groupBy=function(t,e){var r={};return n.each(t,function(n,t,i){var u=e(n,t,i);r[u]=r[u]||[],r[u].push(n)}),r},n.range=function(){var t=arguments;if(t.length<2)return n.range(t[1],t[0]);var e=t[0]||0,i=t[1]||0,u=t[2];r.number(u)||(u=1);var o=i-e;0!=u&&(o/=u);for(var a=[],c=e,f=0;f<o;f++)a.push(c),c+=u;return a},n.pullAt=function(t){return o(t,n.slice(arguments,1))},n.remove=function(t,e){for(var r=n.len(t)||0,i=[];r--;)e(t[r],r,t)&&i.push(r);return o(t,i)},n.fill=function(t,e,r,i){var o=n.size(t);r=u(r,t)||0,i=u(i,t)||o;for(var a=r;a<i;a++)t[a]=e;return t},n.size=function(t){var e=0;if(t){var r=t.length;n.isInteger(r)&&r>=0?e=r:n.isObject(t)&&(e=n.keys(t).length)}return e}}},function(n,t){n.exports=function(n){var t=n.is,e=(n.each,n.forIn);n.only=function(e,r){return e=e||{},t.string(r)&&(r=r.split(/ +/)),n.reduce(r,function(n,t){return null!=e[t]&&(n[t]=e[t]),n},{})},n.values=function(t){return n.map(n.keys(t),function(n){return t[n]})},n.pick=function(r,i){if(!t.fn(i))return n.pick(r,function(n,t){return t==i});var u={};return e(r,function(n,t,e){i(n,t,e)&&(u[t]=n)}),u},n.functions=function(e){return n.keys(n.pick(e,function(n){return t.fn(n)}))},n.mapKeys=function(n,t){var r={};return e(n,function(n,e,i){var u=t(n,e,i);r[u]=n}),r},n.mapObject=n.mapValues=function(n,t){var r={};return e(n,function(n,e,i){r[e]=t(n,e,i)}),r},n.get=function(t,e){if((e=u(e)).length&&n.every(e,function(n){if(null!=t)return t=t[n],!0}))return t},n.has=function(e,r){if((r=u(r)).length&&n.every(r,function(n){if(null!=e&&t.owns(e,n))return e=e[n],!0}))return!0;return!1},n.set=function(e,r,i){r=u(r);var o=e;return n.every(r,function(n,e){if(t.oof(o)){if(e+1!=r.length){if(null==(u=o[n])){var u={};~~n==n&&(u=[])}return o=o[n]=u,!0}o[n]=i}}),e},n.create=function(){function t(){}return function(e,r){return"object"!=typeof e&&(e=null),t.prototype=e,n.extend(new t,r)}}(),n.defaults=function(){var e=arguments,r=e[0],i=n.slice(e,1);return r&&n.each(i,function(e){n.mapObject(e,function(n,e){t.undef(r[e])&&(r[e]=n)})}),r},n.isMatch=function(n,t){var r=!0;return n=n||{},e(t,function(t,e){if(t!==n[e])return r=!1,!1}),r},n.toPlainObject=function(n){var t={};return e(n,function(n,e){t[e]=n}),t},n.invert=function(n){var t={};return e(n,function(n,e){t[n]=e}),t};var r=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g,i=/\\(\\)?/g;function u(e){if(t.array(e))return e;var u=[];return n.toString(e).replace(r,function(n,t,e,r){var o=t||n;e&&(o=r.replace(i,"$1")),u.push(o)}),u}}},function(n,t,e){n.exports=function(n){var t=n.is,r=n.slice;function i(n){var t=new i.Cache;function e(){var e=arguments,r=e[0];if(!t.has(r)){var i=n.apply(this,e);t.set(r,i)}return t.get(r)}return e.cache=t,e}n.bind=function(e,i){if(t.string(i)){var u=e;e=u[i],i=u}if(!t.fn(e))return e;var o=r(arguments,2);return i=i||this,function(){return e.apply(i,n.flatten([o,arguments]))}},n.inherits=function(t,e){t.super_=e,t.prototype=n.create(e.prototype,{constructor:t})},n.delay=function(t,e){var r=n.slice(arguments,2);return setTimeout(function(){t.apply(this,r)},e)},n.before=function(n,t){return function(){if(n>1)return n--,t.apply(this,arguments)}},n.once=function(t){return n.before(2,t)},n.after=function(n,t){return function(){if(!(n>1))return t.apply(this,arguments);n--}},n.throttle=function(t,e,r){return e=e||0,r=n.extend({leading:!0,trailing:!0,maxWait:e},r),n.debounce(t,e,r)},n.debounce=function(t,e,r){e=e||0;var i,u=(r=n.extend({leading:!1,trailing:!0},r)).maxWait,o=0,a=0,c=n.now();function f(t,e,r){return o=n.now(),t.apply(e,r)}function s(){i&&(clearTimeout(i),i=null)}function l(){var l=!((c=n.now())-o>e||u&&c-a>u);a=c;var p=this,h=arguments;s(),l?r.trailing&&(i=n.delay(function(){f(t,p,h)},e)):f(t,p,h)}return r.leading||(o=c),l.cancel=s,l},i.Cache=e(14),n.memoize=i,n.wrap=function(n,t){return function(){var e=[n];return e.push.apply(e,arguments),t.apply(this,e)}},n.curry=function(t){var e=t.length;return function r(i){return function(){var u=i.concat(n.slice(arguments));return u.length>=e?(u.length=e,t.apply(this,u)):r(u)}}([])}}},function(n,t,e){var r=e(2).is;function i(){this.data={}}n.exports=i;var u=i.prototype;u.has=function(n){return r.owns(this.data,n)},u.get=function(n){return this.data[n]},u.set=function(n,t){this.data[n]=t},u.delete=function(n){delete this.data[n]}},function(n,t){n.exports=function(n){n.tostr=n.toString=i;var t=n.indexOf;n.split=function(n,t,e){return(n=i(n)).split(t,e)},n.capitalize=function(n){return(n=i(n)).charAt(0).toUpperCase()+n.substr(1)},n.decapitalize=function(n){return(n=i(n)).charAt(0).toLowerCase()+n.substr(1)},n.camelCase=function(t){var e=(t=i(t)).split(/[^\w]|_+/);return e=n.map(e,function(t){return n.capitalize(t)}),n.decapitalize(e.join(""))},n.startsWith=function(n,e){return 0==t(n,e)},n.endsWith=function(t,e){return(e+="")==n.slice(t,n.len(t)-n.len(e))},n.toLower=n.lower=function(n){return i(n).toLowerCase()},n.toUpper=n.upper=function(n){return i(n).toUpperCase()},n.repeat=function(t,e){return n.map(n.range(e),function(){return t}).join("")},n.padStart=function(n,t,e){return n=i(n),r(e,(t=t||0)-n.length)+n},n.padEnd=function(n,t,e){return(n=i(n))+r(e,(t=t||0)-n.length)};var e={"&":"&","<":"<",">":">",'"':""","'":"'"};function r(t,e){t=i(t)||" ";var r=Math.floor(e/t.length)+1;return n.repeat(t,r).slice(0,e)}function i(n){return n||0==n?n+"":""}n.escape=function(n){return i(n).replace(/[&<>"']/g,function(n){return e[n]||n})},n.template=function(t){var e=['with(data) {var ret = ""'];n.each(n.split(t,"<%"),function(t,r){var i=t.split("%>");if(i[1])return function(t){var r=n.first(t);if("="===r||"-"===r){var i=n.slice(t,1);"-"===r&&(i="_.escape("+i+")"),e.push("ret += "+i)}else e.push(t)}(n.trim(i[0])),u(i[1]);u(i[0])}),e.push("return ret}");var r=new Function("data",e.join("\n")),i={_:n};return function(t){return r(n.extend({},i,t))};function u(n){e.push('ret += "'+n.replace(/('|"|\\)/g,"\\$1").replace(/\r/g,"\\r").replace(/\n/g,"\\n")+'"')}}}},function(n,t){n.exports=function(n){n.sum=function(t){return n.reduce(t,function(n,t){return n+t},0)},n.max=function(t,e){var r=-1,i=-1/0;return e=e||n.identity,n.each(t,function(n,t){(n=e(n))>i&&(i=n,r=t)}),r>-1?t[r]:i},n.min=function(t,e){var r=-1,i=1/0;return e=e||n.identity,n.each(t,function(n,t){(n=e(n))<i&&(i=n,r=t)}),r>-1?t[r]:i}}},function(n,t,e){const r=e(0),i=e(18),u=e(3),o=e(19),a=e(1),c=e(20),f=e(26),s="application/json",l="application/x-www-form-urlencoded",p=a.CONTENT_TYPE_KEY,h=["get","head","delete","options"],d=["post","put","patch"],v=[...h,...d];function m(n){this.defaults={baseURL:"",timeout:0,headers:{common:{}},withCredentials:!1},r.each(v,n=>{var t=this.defaults.headers[n]={};r.includes(d,"method")&&(t[n]=s)}),this.interceptors={request:new o,response:new o},this.qs=u,this.CancelToken=f,this.init(n)}m.qs=u;var y=m.prototype;y.init=function(n){n=r.extend({},n),this.defaults.headers.common=n.headers||{},delete n.headers,r.extend(this.defaults,n)},y.create=function(n){return new m(n)},y.request=function(n,t){if(r.isString(n))return this.request(r.extend({url:n},t));var e=n||{};e.headers=e.headers||{};var o=(e=r.extend({},this.defaults,e)).baseURL+e.url;o=i.appendQuery(o,e.params);var c=r.toLower(e.method)||"get",f=this.defaults.headers,h=r.extend({},f.common,f[c],e.headers),d=a.getContentType(h),v=d,m=e.data;r.isPlainObject(m)?(d===l?m=u.stringify(m):d===s&&(m=JSON.stringify(m)),v||r.isString(m)&&(v=l),r.isString(m)||(m=JSON.stringify(m),v=v||s),!d&&v&&(h[p]=v)):a.isFormData(m)&&delete h[p];var y=e.timeout;e={url:o,data:m,headers:h,method:r.toUpper(c),cancelToken:e.cancelToken,withCredentials:e.withCredentials},y&&(e.timeout=y);var g=Promise.resolve(e);return g=this.interceptors.request.intercept(g).then(n=>this.adapter.call(this,n)).then(n=>{if(r.isString(n.data)&&!this.axios){var t=n.data;try{n.data=JSON.parse(n.data)}catch(e){n.data=t}}return n.config=e,n.headers=r.mapKeys(n.headers,(n,t)=>r.toLower(t)),n}),g=this.interceptors.response.intercept(g)},y.adapter=function(n){var t=this.defaults;return t.wx?c.wx.call(this,n):t.axios?c.axios.call(this,n):t.jQuery?c.jquery.call(this,n):t.quickapp?c.quickapp.call(this,n):"function"==typeof XMLHttpRequest?c.xhr.call(this,n):void 0},r.each(h,n=>{y[n]=function(t,e){return this.request(r.extend({method:n,url:t},e))}}),r.each(d,n=>{y[n]=function(t,e,i){return this.request(r.extend({url:t,method:n,data:e},i))}}),n.exports=m},function(n,t,e){var r=e(3),i=e(0);t.parse=function(n,t){if("string"!=typeof n)return n;var e,u,a={};a.href=n,u=(e=o(n,"#"))[0],e[1]&&(a.hash="#"+e[1]),u=(e=function(n,t){var e="";return n=n.replace(t,function(n){return e=n,""}),[e,n]}(u,/^[a-zA-Z][a-zA-Z0-9+-.]*:/))[1],a.protocol=e[0].toLowerCase(),u=(e=o(u,"?"))[0];var c=e[1];if(t&&(c=r.parse(c)),a.query=c,"/"!=u.charAt(0)&&a.schema)return a.opaque=u,a;i.startsWith(u,"//")&&(e=o(u=u.slice(2),"/"),a.pathname="/"+unescape(e[1]||""),e=function(n){var t=o(n,"@"),e=t[0],r=t[1];r||(r=t[0],e=null);return[e,r]}(e[0]),a.auth=e[0],e=o(e[1],":"),a.hostname=e[0],a.port=~~e[1]);return a};var u="http https ftp gopher file".split(" ");function o(n,t){var e=[],r=i.indexOf(n,t);return-1==r?e[0]=n:(e[0]=n.slice(0,r),e[1]=n.slice(r+t.length)),e}t.format=function(n){if(!n||"object"!=typeof n)return n;var t=n.protocol,e=[t];t&&!i.includes(u,t.slice(0,t.length-1))||e.push("//"),n.auth&&e.push(n.auth,"@"),e.push(n.hostname),n.port&&e.push(":",n.port),e.push(n.pathname);var o=n.query;o&&("string"!=typeof o&&(o=r.stringify(o)),o&&e.push("?",o)),e.push(n.hash);for(var a=[],c=0;c<e.length;c++)e[c]&&a.push(e[c]);return a.join("")},t.appendQuery=function(n,t){var e=o(n,"#");n=e[0];var u=e[1];return i.isObject(t)&&(t=r.stringify(t)),i.includes(n,"?")?i.endsWith(n,"&")||i.endsWith(n,"?")||t&&(t="&"+t):t&&(t="?"+t),t&&(n+=t),u&&(n+="#"+u),n}},function(n,t,e){const r=e(0);function i(){this.queue=[]}n.exports=i,r.extend(i.prototype,{use(...n){return this.queue.push(n),this},intercept(n){return r.reduce(this.queue,(n,t)=>n.then(...t),n)}})},function(n,t,e){const r=e(21),i=e(22),u=e(23),o=e(24),a=e(25);t.wx=r,t.quickapp=i,t.axios=u,t.jquery=o,t.xhr=a},function(n,t,e){const r=e(1);n.exports=function(n){var t,e=this.defaults;if(e&&e.wx)return new Promise((i,u)=>{var o=e.wx.request({url:n.url,data:n.data,header:n.headers,method:n.method,success(n){r.clearTimer(t);var{data:e,statusCode:u,header:o}=n;i({data:e,status:u,headers:o})},fail(n){r.clearTimer(t),u(n)}});n.timeout&&(t=setTimeout(()=>{o&&o.abort&&o.abort,u(r.createError("timeout"))},n.timeout))})}},function(n,t,e){const r=e(1);n.exports=function(n){var t=this.defaults;if(t&&t.quickapp)return new Promise((e,i)=>{t.quickapp.fetch({url:n.url,data:n.data,header:n.headers,method:n.method,success(n){r.clearTimer(timer);var{data:t,code:i,headers:u}=n;e({data:t,status:i,headers:u})},fail(n,t){r.clearTimer(timer),i({data:n,code:t})}}),n.timeout&&(timer=setTimeout(()=>{i(r.createError("timeout"))},n.timeout))})}},function(n,t){n.exports=function(n){var t=this.defaults;if(t&&t.axios)return t.axios.request(n).then(n=>n)}},function(n,t,e){const r=e(1);n.exports=function(n){var t=this.defaults;if(t&&t.jQuery)return new Promise((e,i)=>{t.jQuery.ajax({url:n.url,data:n.data,headers:n.headers,method:n.method,timeout:n.timeout,withCredentials:n.withCredentials,success(n,t,i){e({data:n,status:200,headers:r.parseHeadersFromXhr(i)})},error(n,t,e){i(r.createError(e,{response:n,textStatus:t}))}})})}},function(n,t,e){const r=e(1),i=e(0);n.exports=function(n){return new Promise((t,e)=>{var u=new XMLHttpRequest;u.onload=(n=>{t({status:u.status,data:u.responseText,headers:r.parseHeadersFromXhr(u)})}),u.ontimeout=(n=>{e(r.createError("timeout"))}),u.onerror=(n=>{e(r.createError("error"))}),u.open(n.method,n.url,!0),n.timeout&&(u.timeout=n.timeout),n.withCredentials&&(u.withCredentials=n.withCredentials),i.forIn(n.headers,(n,t)=>{u.setRequestHeader(t,n)}),n.cancelToken&&n.cancelToken.promise.then(function(n){u&&(u.abort(),e(n),u=null)}),u.send(n.data)})}},function(n,t){function e(){this.promise=new Promise((n,t)=>{this.resolve=n,this.reject=t})}n.exports=e,e.source=(()=>{var n=new e;return{token:n,cancel(t){n.resolve(new Error(t))}}}),e.prototype.throwIfRequested=function(){}}])}); |
@@ -23,2 +23,8 @@ var express = require('express') | ||
.use('/timeout', (req, res) => { | ||
setTimeout(() => { | ||
res.send(req.body) | ||
}, 2000) | ||
}) | ||
.use('/text', (req, res) => { | ||
@@ -25,0 +31,0 @@ res.send('xxxxx') |
{ | ||
"name": "@chunpu/http", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Promise Based request / fetch / http For Real Project, Support multiple platforms", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -234,2 +234,30 @@ @chunpu/http | ||
Cancel Requests | ||
--- | ||
compatible with axios Cancellation | ||
source is sth like deferred | ||
```js | ||
const source = http.CancelToken.source() | ||
http.get('/very/slow/api/1', { | ||
cancelToken: source.token | ||
}).catch(err => { | ||
console.error(err) // too slow, abort it | ||
}) | ||
http.get('/very/slow/api/2', { | ||
cancelToken: source.token | ||
}).catch(err => { | ||
console.error(err) // too slow, abort it | ||
}) | ||
setTimeout(() => { | ||
source.cancel('too slow, abort it') // will cancel all requests with this source | ||
}, 1000) | ||
``` | ||
Usage With Real Project | ||
@@ -236,0 +264,0 @@ --- |
const utils = require('../utils') | ||
const _ = require('min-util') | ||
@@ -30,4 +31,15 @@ module.exports = function(config) { | ||
}) | ||
if (config.cancelToken) { | ||
config.cancelToken.promise.then(function onCancel(reason) { | ||
if (xhr) { | ||
xhr.abort() | ||
reject(reason) | ||
xhr = null | ||
} | ||
}) | ||
} | ||
xhr.send(config.data) | ||
}) | ||
} |
@@ -7,2 +7,3 @@ const _ = require('min-util') | ||
const adapters = require('./adapters') | ||
const CancelToken = require('./canceltoken') | ||
@@ -38,3 +39,3 @@ const JSON_TYPE = 'application/json' | ||
this.qs = qs | ||
this.CancelToken = CancelToken | ||
this.init(opt) | ||
@@ -112,2 +113,3 @@ } | ||
method: _.toUpper(method), | ||
cancelToken: config.cancelToken, | ||
withCredentials: config.withCredentials | ||
@@ -161,2 +163,4 @@ } | ||
// TODO add http.all http.spread like axios | ||
_.each(simpleMethods, method => { | ||
@@ -163,0 +167,0 @@ proto[method] = function (url, config) { |
@@ -6,14 +6,17 @@ const _ = require('min-util') | ||
module.exports = exports = http // always export a function | ||
module.exports = instance | ||
function http(...args) { | ||
return instance.request(...args) | ||
} | ||
// TODO sth wrong with quickapp, to be verified | ||
// module.exports = exports = http // always export a function | ||
for (var key in instance) { | ||
var val = instance[key] | ||
if (_.isFunction(val)) { | ||
val = _.bind(val, instance) | ||
} | ||
http[key] = val | ||
} | ||
// function http(...args) { | ||
// return instance.request(...args) | ||
// } | ||
// for (var key in instance) { | ||
// var val = instance[key] | ||
// if (_.isFunction(val)) { | ||
// val = _.bind(val, instance) // | ||
// } | ||
// http[key] = val | ||
// } |
const _ = require('min-util') | ||
const httpClient = require('..') | ||
const httpClient = require('../') | ||
const assert = require('assert') | ||
@@ -4,0 +4,0 @@ |
@@ -5,3 +5,9 @@ var webpack = require('webpack') | ||
var util = require('util') | ||
var now = new Date().toISOString() | ||
var banner = ` | ||
${pkg.name}@${pkg.version} by ${pkg.author} | ||
${now} | ||
`.trim() | ||
var config = { | ||
@@ -19,7 +25,6 @@ entry: { | ||
plugins: [ | ||
new webpack.BannerPlugin(util.format('%s@%s by %s', pkg.name, pkg.version, pkg.author)), | ||
new webpack.BannerPlugin(banner), | ||
new webpack.DefinePlugin({ | ||
VERSION: JSON.stringify(pkg.version) | ||
}), | ||
new webpack.optimize.UglifyJsPlugin({minimize: true}) | ||
}) | ||
] | ||
@@ -26,0 +31,0 @@ } |
Sorry, the diff of this file is not supported yet
64124
24
729
353