UNPKG

3.53 kBJavaScriptView Raw
1"use strict";(function(){var e=this;if("undefined"==typeof t){if("function"!=typeof require)throw new Error('Portals requires "Promise" support');var t=require("promise/lib/es6-extensions")}var r=function(e){e=e||{};for(var t=1;t<arguments.length;t++){var o=arguments[t];if(o)for(var n in o)o.hasOwnProperty(n)&&("object"==typeof o[n]?(e[n]=e[n]||{},r(e[n],o[n])):e[n]=o[n])}return e},o=function(e){if("string"!=typeof e.method)throw new Error("Invalid method provided");if("string"!=typeof e.url)throw new Error("Invalid url provided");return"object"!=typeof e.headers&&(e.headers={}),e},n=function(e){return e=r({},this.globals,e)},s=function(e){return 0!==e.url.indexOf("http")&&(e.url=(e.hostname||"")+e.url),e},i=function(e){return(e.data||e.body)&&"string"==typeof e.headers["Content-Type"]&&-1!==e.headers["Content-Type"].indexOf("json")&&(e.body=JSON.stringify(e.data||e.body)),e},p=function(e){return e.headers["Content-Type"]&&-1!==e.headers["Content-Type"].indexOf("json")&&(e.body=JSON.parse(e.body)),e},u=function(){this.globals={hostname:"",method:"GET",headers:{Accept:"application/json","Content-Type":"application/json"}},this._requestInterceptors=[o],this._responseInterceptors=[]};u.prototype.send=function(e){if("object"!=typeof e)throw new Error("Options must be an object!");for(var r=0;r<this._requestInterceptors.length;r++)if(e=this._requestInterceptors[r].call(this,e),"object"!=typeof e)throw new Error("Options object is no longer an object after interception!");var o=this,n=new XMLHttpRequest;"withCredentials"in n||"undefined"==typeof XDomainRequest||(n=new XDomainRequest),n.open(e.method.toUpperCase(),e.url,!0),n.withCredentials=e.withCredentials||!1;var s=new t(function(e,t){n.onload=function(r){for(var n={status:this.status,headers:{"Content-Type":this.getResponseHeader("Content-Type"),"Cache-Control":this.getResponseHeader("Cache-Control"),Expires:this.getResponseHeader("Expires")},type:this.responseType,body:this.responseText,xhr:this},s=0;s<o._responseInterceptors.length;s++)if(n=o._responseInterceptors[s].call(o,n),"object"!=typeof n)throw new Error("Response object is no longer an object after interception!");200===this.status?e(n):t(n)},n.onerror=function(){t({status:0,headers:{"Content-Type":"text/plain"},type:"error",body:"Connection Error",xhr:this})}});for(var i in e.headers)n.setRequestHeader(i,e.headers[i]);return e.body&&"string"!=typeof e.body&&(e.body=JSON.stringify(e.body)),n.send(e.body||null),s},u.prototype.get=function(e){return this.send({method:"GET",url:e})},u.prototype.post=function(e,t){return this.send({method:"POST",url:e,data:t})},u.prototype.put=function(e,t){return this.send({method:"PUT",url:e,data:t})},u.prototype.del=function(e){return this.send({method:"DELETE",url:e})},u.prototype.useDefaultInterceptors=function(){return this.onRequest(n).onRequest(s).onRequest(i).onResponse(p)},u.prototype.onRequest=function(e){if("function"!=typeof e)throw new Error("Interceptor must be a function!");return this._requestInterceptors.push(e),this},u.prototype.onResponse=function(e){if("function"!=typeof e)throw new Error("Interceptor must be a function!");return this._responseInterceptors.push(e),this};var a=function(){var e=new u;return e.useDefaultInterceptors(),e},d={create:a,Portal:u,interceptors:{validateRequest:o,mergeGlobalsRequest:n,buildUrlRequest:s,encodeJsonRequest:i,parseJsonResponse:p}};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=d),exports.portals=d):e.portals=d}).call(this);
\No newline at end of file