@jeli/http
Version:
jeli-js http
14 lines • 4.96 kB
JavaScript
!function(e,t){"function"==typeof define&&define.amd?define("@jeli/http",["@jeli/core","@jeli/helpers","@jeli/helpers/utils"],t):"object"==typeof exports&&"undefined"!==module?t(exports,require("@jeli/core"),require("@jeli/helpers"),require("@jeli/helpers/utils")):t(((e=e||self).jeli=e.jeli||{},e.jeli["http"]=e.jeli["http"]||{}),e.jeli["core"],e.jeli["helpers"],e.jeli["helpers/utils"])}(this,
function(e,t,s,r){"use strict";function a(e){this.errorType=null,this.message=e,this.status=400}a.prototype.setMessage=function(e){this.message=e},a.prototype.setErrorType=function(e){this.errorType=e};var n={"Accept-Charset":!0,"Accept-Encoding":!0,Connection:!0,"Content-Length":!0,Cookie:!0,Cookie2:!0,"Content-Transfer-Encoding":!0,Date:!0,Expect:!0,Host:!0,"Keep-Alive":!0,Referer:!0,TE:!0,
Trailer:!0,"Transfer-Encoding":!0,Upgrade:!0,"User-Agent":!0,Via:!0};function c(e,t){!t&&isobject(e)&&(t=e),this.url=t.url||e,this.type=(t.type||"GET").toLowerCase(),this.processData=!!isundefined(option.processData)||option.processData,this.headers=Object.assign({Accept:"application/json","CONTENT-TYPE":"application/json"},t.headers||{}),this.asynchronous=!!isundefined(option.asynchronous
)||option.asynchronous,this.data=t.data||"",this.xhrInstance=new XMLHttpRequest,this.cache=!!isundefined(t.cache)||t.cache}function p(t){this.status=t.xhrInstance.status,this.readyState=t.xhrInstance.readyState,this.url=t.url,this.data=null,this.success=200<=t.xhrInstance.status&&t.xhrInstance.status<300||304==t.xhrInstance.status||0==t.xhrInstance.status&&t.xhrInstance.responseText,
this.headers=Object({getResponseHeader:function(e){return isobject(e)?e.map(t.xhrInstance.getResponseHeader):t.xhrInstance.getResponseHeader(e)}})}c.prototype.progress=function(e){this.xhrInstance.addEventListener("progress",e,!1)},c.prototype.setHeaders=function(e){Object.assign(this.headers,e)},c.prototype.processRequest=function(){this.processData=!1,isstring(this.data)||(
"get"===this.type?this.data=serialize(this.data):this.data=JSON.stringify(this.data)),"get"===this.type&&(this.data&&(this.url+=(/\?/.test(this.url)?"&":"?")+this.data),this.cache||(this.url+=(/\?/.test(this.url)?"&":"?")+"_="+Date.now()))},c.prototype.processRequestHeaders=function(){for(var e in this.headers){if(n[e]||/^(Sec-|Proxy-)/.test(e))throw new Error(
'Refused to set unsafe header "'+e+'"');this.xhrInstance.setRequestHeader(e,this.headers[e])}};var u=new ProviderToken("interceptors",!0);function i(e,t,s){var r=new Subject,n=new c(e,t),o=new a("");function i(){var e=new p(n);if(4==n.xhrInstance.readyState){try{var t=function(t){var s,r=!!t;try{s=JSON.parse(t)}catch(e){if(r)throw new Error(e);s=t}return s}(n.xhrInstance.responseText);r.next(t,e)
}catch(e){r.error(o)}r.completed(),s()}}return InterceptorResolver(u,n).then(function(e){if(!e)return o.setMessage("HTTP: Interceptor should return a value"),r.error(o);function t(e){o.setErrorType(e.type),r.error(o)}s&&s(),e.processData&&e.processRequest(),n.xhrInstance.addEventListener("load",i),n.xhrInstanceaddEventListener("timeout",t),n.xhrInstance.addEventListener("error",t),
n.xhrInstance.addEventListener("abort",t),n.xhrInstance.open(n.type,n.url,n.asynchronous),n.processRequestHeaders();e=null;inarray(n.type,["post","put","delete"])&&(e=n.data);try{n.xhrInstance.send(e)}catch(e){o.setMessage(e),r.error(o)}}),r}var h=["put","get","post","request","patch","delete"],o=(d.ctors={DI:[ChangeDetector]},d);function d(s){function o(e,t){return i(e,t,s)}return h.forEach(
function(n){o[n]=function(e,t,s){var r={};return r.type=n,r.data=t,r.headers=s||{},o(e,r)}}),o}function l(){}y.prototype.set=function(e,t,s){var r,n,o;isnull(t)||isundefined(t)||(null===t&&(t="",s.expires=-1),r="",n="number"==typeof s.expires,s.expires&&(n||s.expires.toUTCString)&&(n?(o=new Date).setTime(o.getTime()+24*s.expires*60*60*1e3):o=s.expires,r="; expires="+o.toUTCString()),
n=s.path?"; path="+s.path:"",o=s.domain?"; domain="+s.domain:"",s=s.secure?"; secure":"",document.cookie=[e,"=",encodeURIComponent(t),r,n,o,s].join(""))},y.prototype.get=function(e){return this.cookies[e]},y.prototype._getAll=function(){var e={},t=(document.cookie||"").split(";");if(t.length)for(var s=0;s<t.length;s++){var r=t[s].trim().split("=");e[r[0]]=r[1]}return e},y.ctors={};var f=y;
function y(){Object.defineProperty(this,"cookies",{get:function(){return this._getAll()}})}var g={cookieName:"X-CSRF-TOKEN",headerName:"X-CSRF-TOKEN"},x=(v.ctors={DI:[f]},v);function v(n){this.resolve=function(e,t){var s,r=n.get(g.cookieName);r&&((s={})[g.headerName]=r,e.setHeaders(s)),t()}}T.setCookieValue=function(e){Object.assign(g,e)},u.register({useClass:x},!0);var j=T;function T(){}
e.HttpModule=l,e.HttpService=o,e.HttpRequestError=a,e.HttpRequest=c,e.HttpResponse=p,e.HTTP_INTERCEPTORS=u,e.HttpCSRFModule=j,e.HttpCookieManager=f,e.csrfCookieConfig=g,e.CSRFCookieHeaderService=x,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=jeli-http-umd.min.js.map