-
Notifications
You must be signed in to change notification settings - Fork 28
/
angular-http-batch.min.js
5 lines (5 loc) · 7.75 KB
/
angular-http-batch.min.js
1
2
3
4
5
/*
* angular-http-batcher - v1.12.0 - 2015-10-12
* https://github.com/jonsamwell/angular-http-batcher
* Copyright (c) 2015 Jon Samwell;*/
!function(a,b){"use strict";function c(){var a=[],c="httpBatchAdapter",d={maxBatchedRequestPerCall:10,minimumBatchSize:2,batchRequestCollectionDelay:100,ignoredVerbs:["head"],sendCookies:!1,enabled:!0,adapter:c,uniqueRequestName:null};this.setAllowedBatchEndpoint=function(e,f,g){var h=b.copy(d);void 0!==g&&(b.forEach(g,function(a,b){h[b]=a}),b.forEach(h.ignoredVerbs,function(a,b){h.ignoredVerbs[b]=a.toLowerCase()})),h.serviceUrl=e,h.batchEndpointUrl=f,h.adapter=h.adapter||c,a.push(h)},this.getBatchConfig=function(b){var c,d;for(d=0;d<a.length&&(c=a[d],!(b.indexOf(c.serviceUrl)>-1));d+=1)c=void 0;return c},this.canBatchCall=function(a,b){var c=this.getBatchConfig(a),d=c?c.canBatchRequest:void 0,e=!1;return c&&c.enabled===!0&&(e=d?d(a,b):c.batchEndpointUrl!==a&&-1===a.indexOf(c.batchEndpointUrl)&&-1===c.ignoredVerbs.indexOf(b.toLowerCase())),e},this.calculateBoundary=function(){return(new Date).getTime().toString()},this.$get=[function(){return this}]}function d(a,b,c,d,e){this.request=a,this.statusCode=b,this.statusText=c,this.data=d,this.headers=e}function e(c,d,e){function f(a,b){var d,f,g,h,i,k,l=e.calculateBoundary(),m={method:"POST",url:b.batchEndpointUrl,cache:!1,headers:b.batchRequestHeaders||{}},n=[];for(m.headers[p.contentType]="multipart/mixed; boundary="+l,f=0;f<a.length;f+=1){if(g=a[f],d=j(g.url),n.push(p.doubleDash+l),b.batchPartRequestHeaders)for(h in b.batchPartRequestHeaders)if(b.batchPartRequestHeaders.hasOwnProperty(h)){var o=h+p.colon+p.singleSpace+b.batchPartRequestHeaders[h];"content-disposition"===h.toLowerCase()&&null!==b.uniqueRequestName&&void 0!==b.uniqueRequestName&&(o+=p.semiColon+p.singleSpace+p.requestName+b.uniqueRequestName+f),n.push(o)}n.push("Content-Type: application/http; msgtype=request",p.emptyString),i=d.relativeUrl.split("?"),k=encodeURI(i[0])+(i.length>1?"?"+i[1]:""),n.push(g.method+" "+k+" "+p.httpVersion),n.push("Host: "+d.host);for(h in g.headers)n.push(h+p.colon+p.singleSpace+g.headers[h]);b.sendCookies===!0&&c[0].cookie&&c[0].cookie.length>0&&n.push("Cookie: "+c[0].cookie),n.push(p.emptyString),g.data&&n.push(g.data),n.push(p.emptyString)}return n.push(p.doubleDash+l+p.doubleDash),m.data=n.join(p.newline),m}function g(a,b,c){var d,e,f=[],g=k(b.headers()["content-type"]),h=b.data.split(p.doubleDash+g+p.newline),i=0;for(d=0;d<h.length;d+=1)e=h[d],e!==p.emptyString&&(f.push(n(e,a[i],g)),i+=1);return f}function h(a,b){return!0}function i(a){return a=a.trim?a.trim():a.replace(/^\s+|\s+$/g,"")}function j(a){var b,c,e,f,g;if(a.indexOf("./")>-1||a.indexOf("../")>-1){var h=document.createElement("a");h.href=a,a=h.href}return a.indexOf("://")>-1?(f=a.indexOf("://")+3,g=a.slice(f).split(p.forwardSlash),b=a.substring(0,f),c=g[0],e=function(){return delete g[0],g.join(p.forwardSlash)}()):(e=a,b=d.location.protocol,c=d.location.host),{protocol:b,host:c,relativeUrl:e}}function k(a){var b="boundary=",c=a.indexOf(b),d=a.indexOf(";",c),e=a.substring(c+b.length,d>0?d:a.length);return e=e.replace(/"/g,p.emptyString)}function l(a){return"string"==typeof a?a.replace(")]}',\n",""):a}function m(a,c){var d=c;return a=a.toLowerCase(),a.indexOf("json")>-1&&(c=l(c),d=b.fromJson(c)),d}function n(b,c,d){var e,f,g,h,j,k,l=b.split(p.newline),n={headers:{}},o=!1;for(f=0;f<l.length;f+=1)if(e=l[f],e!==p.emptyString){if(void 0===n.contentType&&-1!==e.indexOf("-Type")&&-1===e.indexOf("; msgtype=response"))n.contentType=e.split(p.forwardSlash)[1];else if(void 0!==n.contentType&&o===!1)k=e.split(p.colon),n.headers[k[0]]=i(k[1]);else if(void 0===n.statusCode&&-1!==e.indexOf(p.httpVersion))j=e.split(p.singleSpace),n.statusCode=parseInt(j[1],10),n.statusText=j.slice(2).join(p.singleSpace);else if(void 0===n.data&&o){for(n.data="",g=1,h=new RegExp("--"+d+"--","i");h.test(e)===!1&&f+g<=l.length;)n.data+=e,e=l[f+g],g+=1;n.data=m(n.contentType,n.data);break}}else o=void 0!==n.contentType;return n.headers[p.contentType]=n.contentType,new a.ahb.HttpBatchResponseData(c,n.statusCode,n.statusText,n.data,n.headers)}var o=this,p={httpVersion:"HTTP/1.1",contentType:"Content-Type",newline:"\r\n",emptyString:"",singleSpace:" ",forwardSlash:"/",doubleDash:"--",colon:":",semiColon:";",requestName:"name="};o.key="httpBatchAdapter",o.buildRequest=f,o.parseResponse=g,o.canBatchRequest=h}function f(){function b(a,b){var c,d,e,f,g={method:"GET",url:b.batchEndpointUrl+"?",cache:!1,headers:b.batchRequestHeaders||{}};for(d=0;d<a.length;d+=1)e=a[d],f=e.url.split("?"),c=f[0].replace(b.serviceUrl,""),f.length>1&&(c+="?"+encodeURIComponent(f[1])),d>0&&(g.url+="&"),g.url+=d.toString()+"="+c;return g}function c(b,c){var d,e,f,g=[],h=c.data;for(d=0;d<b.length;d+=1)e=b[d],f=h[d.toString()],g.push(new a.ahb.HttpBatchResponseData(e,f.statusCode,"",f.body,f.headers));return g}function d(a){return"GET"===a.method}var e=this;e.key="nodeJsMultiFetchAdapter",e.buildRequest=b,e.parseResponse=c,e.canBatchRequest=d}function g(a){var b,c="";for(b in a)c+=b+": "+a[b]+"\n";return c}function h(){var a=this.config.adapter;if("object"==typeof a){if(void 0===a.buildRequest||void 0===a.parseResponse)throw new Error('A custom adapter must contain the methods "buildRequest" and "parseResponse" - please see the docs')}else if("string"==typeof a&&(a=this.adapters[a],void 0===a))throw new Error("Unknown type of http batch adapter: "+this.config.adapter);return a}function i(a){return this.requests.push(a),this.requests.length>=this.config.maxBatchedRequestPerCall&&this.flush(),!0}function j(){var a=this,c=a.getAdapter(),d=c.buildRequest(a.requests,a.config);a.sendCallback(),a.$injector.get("$http")(d).then(function(d){var e=c.parseResponse(a.requests,d,a.config);b.forEach(e,function(a){a.request.callback(a.statusCode,a.data,g(a.headers),a.statusText)})},function(c){b.forEach(a.requests,function(a){a.callback(c.statusCode,c.data,c.headers,c.statusText)})})}function k(){this.$timeout.cancel(this.currentTimeoutToken),this.currentTimeoutToken=void 0,this.send()}function l(a,c,d,e,f){var g=this;this.$injector=a,this.$timeout=c,this.adapters=d,this.config=e,this.sendCallback=f,this.requests=[],this.currentTimeoutToken=c(function(){g.currentTimeoutToken=void 0,g.requests.length<g.config.minimumBatchSize?(g.sendCallback(),b.forEach(g.requests,function(a){a.continueDownNormalPipeline()})):g.send(a)},e.batchRequestCollectionDelay,!1)}function m(a,c,d,e,f){function g(a,b){return d.canBatchCall(a,b)}function h(b){var e=d.getBatchConfig(b.url),f=k[e.batchEndpointUrl];void 0===f&&(f=new l(a,c,m,e,function(){delete k[e.batchEndpointUrl]}),k[e.batchEndpointUrl]=f),f.addRequest(b)}function i(a){b.forEach(k,function(b,c){var d=void 0===a||a&&c.toLocaleLowerCase()===a.toLocaleLowerCase();d&&b.flush()})}var j=this,k={},m={httpBatchAdapter:e,nodeJsMultiFetchAdapter:f};j.canBatchRequest=g,j.batchRequest=h,j.flush=i}function n(a,c){var d=function(b,d,e,f,g,h,i,j){var k=this,l=arguments;return c.canBatchRequest(d,b)?void c.batchRequest({method:b,url:d,data:e,callback:f,headers:g,timeout:h,withCredentials:i,responseType:j,continueDownNormalPipeline:function(){a.apply(k,l)}}):a.apply(this,arguments)};return b.mock&&b.forEach(a,function(a,b){d[b]=a}),d}function o(a){a.decorator("$httpBackend",n)}a.ahb={name:"jcs.angular-http-batch"},b.module(a.ahb.name,[]),b.module(a.ahb.name).provider("httpBatchConfig",c),a.ahb.HttpBatchResponseData=d,e.$inject=["$document","$window","httpBatchConfig"],b.module(a.ahb.name).service("httpBatchAdapter",e),b.module(a.ahb.name).service("nodeJsMultiFetchAdapter",f),l.prototype.getAdapter=h,l.prototype.send=j,l.prototype.addRequest=i,l.prototype.flush=k,m.$inject=["$injector","$timeout","httpBatchConfig","httpBatchAdapter","nodeJsMultiFetchAdapter"],b.module(a.ahb.name).service("httpBatcher",m),n.$inject=["$delegate","httpBatcher"],o.$inject=["$provide"],b.module(a.ahb.name).config(o)}(window,angular);