httpism
Version:
HTTP client with middleware and good defaults
2 lines (1 loc) • 9.02 kB
JavaScript
!function t(e,r,n){function o(s,u){if(!r[s]){if(!e[s]){var a="function"==typeof require&&require;if(!u&&a)return a(s,!0);if(i)return i(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var f=r[s]={exports:{}};e[s][0].call(f.exports,function(t){var r=e[s][1][t];return o(r?r:t)},f,f.exports,t,e,r,n)}return r[s].exports}for(var i="function"==typeof require&&require,s=0;s<n.length;s++)o(n[s]);return o}({1:[function(t,e,r){function n(t,e){return t.body instanceof Object&&(t.body=JSON.stringify(t.body),l.setHeaderTo(t,"content-type","application/json")),l.setHeaderTo(t,"accept","application/json"),e().then(function(e){return l.shouldParseAs(e,"json",t)&&(e.body=JSON.parse(e.body)),e})}function o(t,e){return"string"==typeof t.body&&l.setHeaderTo(t,"content-type","text/plain;charset=UTF-8"),e()}function i(t,e){return t.options.form&&t.body instanceof Object&&(s(t,y.stringify(t.body)),l.setHeaderTo(t,"content-type","application/x-www-form-urlencoded")),e().then(function(e){return l.shouldParseAs(e,"form",t)&&(e.body=y.parse(e.body)),e})}function s(t,e){t.body=e}function u(t){for(var e={},r=t.split("\n"),n=0;n<r.length;n++){var o=r[n],i=/^(.*?):(.*)/.exec(o);i&&(e[i[1].toLowerCase()]=i[2].trim())}return e}function a(t,e){for(var r=Object.keys(t),n=0;n<r.length;n++){var o=r[n];e.setRequestHeader(o,t[o])}}function c(t,e){var r=location.origin,n=t.responseURL;return n?n.substring(0,r.length)==r?n.substring(r.length):n:e}function f(t){function e(){n.abort();var e=new Error("aborted connection to "+t.method+" "+t.url);e.aborted=!0,r(e)}var r,n=new XMLHttpRequest,o=new Promise(function(e,o){r=o,n.open(t.method,t.url,!0),n.onload=function(r){var o=n.status,i={body:204==o?void 0:n.responseText,headers:u(n.getAllResponseHeaders()),statusCode:o,url:c(n,t.url),xhr:n,statusText:n.statusText};e(i)},n.onerror=function(e){r(new Error("failed to connect to "+t.method+" "+t.url))},p(t.url)||t.headers["x-requested-with"]||(t.headers["x-requested-with"]="XMLHttpRequest"),a(t.headers,n),n.withCredentials=!!t.options.withCredentials,n.send(t.body)});return h(o,e),o}function p(t){return/^https?:\/\//.test(t)}function h(t,e){var r=t.then;t.then=function(){var t=r.apply(this,arguments);return t.abort=e,h(t,e),t}}var d=t("./httpism"),l=t("./middlewareUtils"),y=t("qs");e.exports=d(void 0,{},[l.exception,i,n,o,l.querystring,f])},{"./httpism":2,"./middlewareUtils":4,qs:5}],2:[function(t,e,r){function n(t,e,r){return new o(t,e,r)}function o(t,e,r){this.url=t,this._options=e,this.middlewares=r}function i(t,e){return p.extend(new o(t.url,t._options,t.middlewares),e)}function s(t){o.prototype[t]=function(e,r){return this.send(t,e,void 0,r,this)}}function u(t){o.prototype[t]=function(e,r,n){return this.send(t,e,r,n,this)}}function a(t,e){return t?a(t,e):e}function c(){for(var t,e,r,n=0;n<arguments.length;n++){var o=arguments[n];"string"==typeof o?t=o:"function"==typeof o?r=[o]:o instanceof Array?r=o:o instanceof Object&&(e=o)}return{url:t,options:e,middlewares:r}}var f=t("./merge"),a=t("./resolveUrl"),p=t("./middlewareUtils");o.prototype.send=function(t,e,r,n,o){function s(t){if(t<p.middlewares.length){var e=p.middlewares[t];return e(c,function(){return s(t+1)},p)}}var u=f(n,this._options),c={method:t,url:a(this.url,e),headers:u.headers||{},body:r,options:u},p=this;return s(0).then(function(t){return i(p,t)},function(t){if(t.redirectResponse)return t.redirectResponse;throw t})},o.prototype.api=function(t,e,r){var n=c(t,e,r);return new o(a(this.url,n.url),f(n.options,this._options),n.middlewares?n.middlewares.concat(this.middlewares):this.middlewares)},s("get"),s("delete"),s("head"),u("post"),u("put"),u("patch"),u("options"),e.exports=n},{"./merge":3,"./middlewareUtils":4,"./resolveUrl":6}],3:[function(t,e,r){e.exports=function(t,e){if(t&&e){var r={};return Object.keys(e).forEach(function(t){r[t]=e[t]}),Object.keys(t).forEach(function(e){r[e]=t[e]}),r}return e?e:t}},{}],4:[function(t,e,r){function n(t,e){var r=new RegExp("^\\s*"+e+"\\s*($|;)");return r.test(t.headers["content-type"])}function o(t){return n(t,"text/.*")}function i(t,e){for(var r=Object.keys(e),n=0;n<r.length;n++){var o=r[n];t[o]=e[o]}return t}var s=t("./merge"),u=t("qs");e.exports.setHeaderTo=function(t,e,r){return t.headers[e]?void 0:t.headers[e]=r};var a={json:function(t){return n(t,"application/json")},text:function(t){return o(t)||n(t,"application/javascript")},form:function(t){return n(t,"application/x-www-form-urlencoded")},stream:function(t){return!1}};e.exports.shouldParseAs=function(t,e,r,n){void 0!==n&&n.hasOwnProperty(n,"contentType")&&void 0!==n.contentType?n.contentType:void 0;if(r.options.responseBody)return e===r.options.responseBody;var o=a[e];return o?o(t):void 0},r.extend=i,r.exception=function(t,e){return e().then(function(e){if(e.statusCode>=400&&t.options.exceptions!==!1){var r=t.method.toUpperCase()+" "+t.url+" => "+e.statusCode+" "+e.statusText,n=i(new Error(r),e);throw n}return e})},r.querystring=function(t,e){if(t.options.querystring instanceof Object){var r=t.url.split("?"),n=r[0],o=u.parse(r[1]),i=s(t.options.querystring,o);t.url=n+"?"+u.stringify(i)}return e()}},{"./merge":3,qs:5}],5:[function(t,e,r){function n(t,e){if(0==t[e].length)return t[e]={};var r={};for(var n in t[e])v.call(t[e],n)&&(r[n]=t[e][n]);return t[e]=r,r}function o(t,e,r,i){var s=t.shift();if(!Object.getOwnPropertyDescriptor(Object.prototype,r))if(s){var u=e[r]=e[r]||[];"]"==s?g(u)?""!=i&&u.push(i):"object"==typeof u?u[m(u).length]=i:u=e[r]=[e[r],i]:~b(s,"]")?(s=s.substr(0,s.length-1),!j.test(s)&&g(u)&&(u=n(e,r)),o(t,u,s,i)):(!j.test(s)&&g(u)&&(u=n(e,r)),o(t,u,s,i))}else g(e[r])?e[r].push(i):"object"==typeof e[r]?e[r]=i:"undefined"==typeof e[r]?e[r]=i:e[r]=[e[r],i]}function i(t,e,r){if(~b(e,"]")){var n=e.split("[");n.length;o(n,t,"base",r)}else{if(!j.test(e)&&g(t.base)){var i={};for(var s in t.base)i[s]=t.base[s];t.base=i}h(t.base,e,r)}return t}function s(t){if("object"!=typeof t)return t;if(g(t)){var e=[];for(var r in t)v.call(t,r)&&e.push(t[r]);return e}for(var n in t)t[n]=s(t[n]);return t}function u(t){var e={base:{}};return w(m(t),function(r){i(e,r,t[r])}),s(e.base)}function a(t){var e=x(String(t).split("&"),function(t,e){var r=b(e,"="),n=d(e),o=e.substr(0,n||r),s=e.substr(n||r,e.length),s=s.substr(b(s,"=")+1,s.length);return""==o&&(o=e,s=""),""==o?t:i(t,l(o),l(s))},{base:{}}).base;return s(e)}function c(t,e){if(!e)throw new TypeError("stringify expects an object");return e+"="+encodeURIComponent(t)}function f(t,e){var r=[];if(!e)throw new TypeError("stringify expects an object");for(var n=0;n<t.length;n++)r.push(O(t[n],e+"["+n+"]"));return r.join("&")}function p(t,e){for(var r,n=[],o=m(t),i=0,s=o.length;s>i;++i)r=o[i],""!=r&&(null==t[r]?n.push(encodeURIComponent(r)+"="):n.push(O(t[r],e?e+"["+encodeURIComponent(r)+"]":encodeURIComponent(r))));return n.join("&")}function h(t,e,r){var n=t[e];Object.getOwnPropertyDescriptor(Object.prototype,e)||(void 0===n?t[e]=r:g(n)?n.push(r):t[e]=[n,r])}function d(t){for(var e,r,n=t.length,o=0;n>o;++o)if(r=t[o],"]"==r&&(e=!1),"["==r&&(e=!0),"="==r&&!e)return o}function l(t){try{return decodeURIComponent(t.replace(/\+/g," "))}catch(e){return t}}var y=Object.prototype.toString,v=Object.prototype.hasOwnProperty,b="function"==typeof Array.prototype.indexOf?function(t,e){return t.indexOf(e)}:function(t,e){for(var r=0;r<t.length;r++)if(t[r]===e)return r;return-1},g=Array.isArray||function(t){return"[object Array]"==y.call(t)},m=Object.keys||function(t){var e=[];for(var r in t)t.hasOwnProperty(r)&&e.push(r);return e},w="function"==typeof Array.prototype.forEach?function(t,e){return t.forEach(e)}:function(t,e){for(var r=0;r<t.length;r++)e(t[r])},x=function(t,e,r){if("function"==typeof t.reduce)return t.reduce(e,r);for(var n=r,o=0;o<t.length;o++)n=e(n,t[o]);return n},j=/^[0-9]+$/;r.parse=function(t){return null==t||""==t?{}:"object"==typeof t?u(t):a(t)};var O=r.stringify=function(t,e){return g(t)?f(t,e):"[object Object]"==y.call(t)?p(t,e):"string"==typeof t?c(t,e):e+"="+encodeURIComponent(String(t))}},{}],6:[function(t,e,r){function n(t){var e=String(t).replace(/^\s+|\s+$/g,"").match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);return e?{href:e[0]||"",protocol:e[1]||"",authority:e[2]||"",host:e[3]||"",hostname:e[4]||"",port:e[5]||"",pathname:e[6]||"",search:e[7]||"",hash:e[8]||""}:null}e.exports=function(t,e){function r(t){var e=[];return t.replace(/^(\.\.?(\/|$))+/,"").replace(/\/(\.(\/|$))+/g,"/").replace(/\/\.\.$/,"/../").replace(/\/?[^\/]*/g,function(t){"/.."===t?e.pop():e.push(t)}),e.join("").replace(/^\//,"/"===t.charAt(0)?"/":"")}return e=n(e||""),t=n(t||""),e&&t?(e.protocol||t.protocol)+(e.protocol||e.authority?e.authority:t.authority)+r(e.protocol||e.authority||"/"===e.pathname.charAt(0)?e.pathname:e.pathname?(t.authority&&!t.pathname?"/":"")+t.pathname.slice(0,t.pathname.lastIndexOf("/")+1)+e.pathname:t.pathname)+(e.protocol||e.authority||e.pathname?e.search:e.search||t.search)+e.hash:null}},{}]},{},[1]);