Skip to content

Commit 311090e

Browse files
committed
feat(filter-request): Added ability to filter specific requests by defining optional configuration parameter function
1 parent 5af7af4 commit 311090e

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
1.0.5
2+
========
3+
Added ability to have fine grained control over the request that get batched by adding an optional canBatchRequest function on the configuration object.
4+
5+
1.0.4
6+
========
7+
Fix to header parsing that would truncate a return header if there was a space it's value i.e. Header: Some Value
8+
19
1.0.3
210
========
311
Build update

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Working demo https://plnkr.co/edit/OgvZ09iYO64VoXHLRGQa?p=preview
2525
- [Send Cookies](#config-sendCookies)
2626
- [Unique Request Name (content-dispositon header)](#config-uniqueRequestName)
2727
- [Http Batching Adapter](#config-httpBatchingAdapter)
28+
- [Can Batch Request](#config-canBatchRequest)
2829
- [On Before Send Batch Request Handler](#config-onBeforeSendBatchRequest)
2930
- [.Net WebApi Configuation](#config-net)
3031
- [Configuring for Java Servlet <= 3.1](#config-javaservlet)

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-http-batcher",
3-
"version": "1.0.3",
3+
"version": "1.0.5",
44
"description": "Angular (2+) HTTP batching module to reduce the number of HTTP requests and increase performance",
55
"scripts": {
66
"build": "./node_modules/.bin/ngc -p ./src",
@@ -26,10 +26,10 @@
2626
"module": "./index.js",
2727
"types": "./index.d.ts",
2828
"devDependencies": {
29-
"@angular/common": "^4.1.1",
30-
"@angular/compiler-cli": "^4.1.1",
31-
"@angular/platform-browser": "^4.1.1",
32-
"@angular/platform-browser-dynamic": "^4.1.1",
29+
"@angular/common": "^4.4.6",
30+
"@angular/compiler-cli": "^4.4.6",
31+
"@angular/platform-browser": "^4.4.6",
32+
"@angular/platform-browser-dynamic": "^4.4.6",
3333
"@types/jasmine": "^2.5.47",
3434
"@types/node": "^7.0.18",
3535
"clean-webpack-plugin": "^0.1.16",
@@ -49,15 +49,15 @@
4949
"ts-loader": "^2.0.3",
5050
"tslint": "^5.2.0",
5151
"tslint-loader": "^3.5.3",
52-
"typescript": "^2.3.2",
52+
"typescript": "^2.3.4",
5353
"uglify-js": "^2.8.23",
5454
"webpack": "^2.5.1"
5555
},
5656
"dependencies": {
57-
"@angular/compiler": "^4.1.1",
58-
"@angular/core": "^4.1.1",
59-
"@angular/http": "^4.1.1",
60-
"rxjs": "^5.3.0",
61-
"zone.js": "^0.8.9"
57+
"@angular/compiler": "^4.4.6",
58+
"@angular/core": "^4.4.6",
59+
"@angular/http": "^4.4.6",
60+
"rxjs": "^5.4.2",
61+
"zone.js": "^0.8.18"
6262
}
6363
}

src/batch-configuration.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export interface IHttpBatchConfigurationOptions {
9090
*/
9191
httpBatchingAdapter?: WellKnownHttpBatchingAdapters | IBatchHttpRequestAdapter;
9292

93+
/**
94+
* An optional function which determines if the request can be batched
95+
*
96+
* @memberof IHttpBatchConfigurationOptions
97+
* @type (request: Request) => boolean;
98+
*/
99+
canBatchRequest?: (request: Request) => boolean;
100+
93101
/**
94102
* Lifecycle hook to modify the batch request just before it is sent. This can be use to add aditional
95103
* headers to the request. eg: "Authorisation: Bearer ..."
@@ -111,6 +119,7 @@ export class HttpBatchConfiguration {
111119
public uniqueRequestName: string;
112120
public sendCookies: boolean;
113121
public httpBatchingAdapter: WellKnownHttpBatchingAdapters | IBatchHttpRequestAdapter;
122+
public canBatchRequest: (request: Request) => boolean;
114123
public onBeforeSendBatchRequest: (batchRequest: Request) => void;
115124

116125
public constructor(options: IHttpBatchConfigurationOptions) {
@@ -124,6 +133,7 @@ export class HttpBatchConfiguration {
124133
this.uniqueRequestName = options.uniqueRequestName;
125134
this.sendCookies = options.sendCookies !== undefined ? options.sendCookies : false;
126135
this.httpBatchingAdapter = options.httpBatchingAdapter || WellKnownHttpBatchingAdapters.Http_MultipartMixed;
136+
this.canBatchRequest = options.canBatchRequest || (() => true);
127137
// tslint:disable-next-line:no-empty
128138
this.onBeforeSendBatchRequest = options.onBeforeSendBatchRequest || (() => {});
129139
}

src/services/http-batcher.service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,28 @@ export class HttpBatcher extends Http {
3838
public request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
3939
const enpointUrl = url instanceof Request ? (url as Request).url : url;
4040
const configuration = this.batchingConfigurations.getConfigurationForUrl(enpointUrl);
41-
if (this.canBatchRequest(configuration)) {
41+
if (this.batchingEnabled(configuration)) {
4242
const request = url instanceof Request ?
4343
url as Request :
4444
new Request(this.mergeOptions(this._defaultOptions, options, RequestMethod.Get, url as string));
45-
return this.batchRequest(request, configuration);
45+
if (this.canBatchRequest(configuration, request)) {
46+
return this.batchRequest(request, configuration);
47+
} else {
48+
return super.request(url, options);
49+
}
4650
} else {
4751
return super.request(url, options);
4852
}
4953
}
5054

51-
protected canBatchRequest(configuration: HttpBatchConfiguration): boolean {
55+
protected batchingEnabled(configuration: HttpBatchConfiguration): boolean {
5256
return configuration !== undefined && configuration.enabled;
5357
}
5458

59+
protected canBatchRequest(configuration: HttpBatchConfiguration, request: Request): boolean {
60+
return configuration.canBatchRequest(request);
61+
}
62+
5563
protected batchRequest(request: Request, configuration: HttpBatchConfiguration): Observable<Response> {
5664
return new Observable<Response>((observer: Observer<Response>) => {
5765
const bacthRequest = {

0 commit comments

Comments
 (0)