Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ gulp.task('compress', function() {
}))
.pipe(gulp.dest('dist/bundles/'))
});

gulp.task('copy', function () {
gulp.src(['package.json','*.md'])
.pipe(gulp.dest('dist'));
});
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"scripts": {
"build": "./node_modules/.bin/ngc -p ./src",
"test": "karma start ./tests/karma.conf.js",
"bundle": "npm run build && webpack && gulp compress",
"postbundle": "copy \"package.json\" \"dist/package.json\" && copy \"CHANGELOG.md\" \"dist/CHANGELOG.md\" && copy \"README.md\" \"dist/README.md\"",
"bundle": "npm run build && webpack && gulp compress && gulp copy",
"lint": "tslint -c tslint.json \"src/**/*.ts\""
},
"keywords": [
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/batching-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Request, Response } from "@angular/http";
import { Observable } from "rxjs/Observable";

export enum WellKnownHttpBatchingAdapters {
Http_MultipartMixed = 0
Http_MultipartMixed = 0,
Http_MultiFetch
}

export interface IBatchHttpRequestAdapter {
Expand Down
55 changes: 55 additions & 0 deletions src/adapters/http-multifetch-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Headers, Request, RequestMethod, RequestOptions,
Response, ResponseOptions, ResponseType } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { HttpBatchConfiguration } from "../batch-configuration";
import { IBatchHttpRequestAdapter } from "./batching-adapter";

export class HttpMultiFetchAdapter implements IBatchHttpRequestAdapter {

public constructor(private configuration: HttpBatchConfiguration, private defaultRequestOptions: RequestOptions) { }

public batch(requests: Request[]): Request {
const batchRequest = new Request({
...this.defaultRequestOptions,
});
batchRequest.method = RequestMethod.Get;
batchRequest.url = this.configuration.batchEndpointUrl + "?";

requests.forEach((r, i) => {
const urlParts = r.url.split("?");
let encodedUrl: string = urlParts[0].replace(this.configuration.rootEndpointUrl, "");
if (urlParts.length > 1) {
encodedUrl += "?" + encodeURIComponent(urlParts[1]);
}

if (i > 0) {
batchRequest.url += "&";
} else {
batchRequest.headers = r.headers;
}

batchRequest.url += i.toString() + "=" + encodedUrl;
});
return batchRequest;
}

public parse(response: Response): Response[] {

const batchResponses: Response[] = [];
const responseData = response.json();
let dataPart: any;
let i: number = 0;

do {
dataPart = responseData[i];
if (dataPart) {
dataPart.status = dataPart.statusCode;
batchResponses.push(new Response(new ResponseOptions(dataPart)));
i += 1;
}
} while (dataPart);

return batchResponses;
}

}
6 changes: 5 additions & 1 deletion src/services/http-batcher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Observable } from "rxjs/Observable";
import { Observer } from "rxjs/Observer";

import { IBatchHttpRequestAdapter, WellKnownHttpBatchingAdapters } from "../adapters/batching-adapter";
import { HttpMultiFetchAdapter } from "../adapters/http-multifetch-adapter";
import { HttpMultipartMixedBoundaryAdapter } from "../adapters/http-multipart-mixed-boundary-adapter";
import { HttpBatchConfiguration, HttpBatchConfigurationCollection } from "../batch-configuration";
import { BatchScheduler } from "./batch-scheduler";
Expand Down Expand Up @@ -57,7 +58,8 @@ export class HttpBatcher extends Http {
}

protected canBatchRequest(configuration: HttpBatchConfiguration, request: Request): boolean {
return configuration.canBatchRequest(request);
return (configuration.ignoredHttpVerbs.indexOf(request.method) > -1) ?
false : configuration.canBatchRequest(request);
}

protected batchRequest(request: Request, configuration: HttpBatchConfiguration): Observable<Response> {
Expand All @@ -78,6 +80,8 @@ export class HttpBatcher extends Http {
switch (configuration.httpBatchingAdapter) {
case WellKnownHttpBatchingAdapters.Http_MultipartMixed:
return new HttpMultipartMixedBoundaryAdapter(configuration, this._defaultOptions);
case WellKnownHttpBatchingAdapters.Http_MultiFetch:
return new HttpMultiFetchAdapter(configuration, this._defaultOptions);
default:
return configuration.httpBatchingAdapter as IBatchHttpRequestAdapter;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/adapters/http-multifetch-adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Headers, Request, RequestMethod, RequestOptions } from "@angular/http";
import { HttpMultiFetchAdapter } from "../../src/adapters/http-multifetch-adapter";
import { HttpBatchConfiguration } from "../../src/batch-configuration";

describe("HttpMultifetchAdapter", () => {
it("Can be created", () => {
const config = new HttpBatchConfiguration({ batchEndpointUrl: "", rootEndpointUrl: "" });
const defaultRequestOptions = new RequestOptions();
const adapter = new HttpMultiFetchAdapter(config, defaultRequestOptions);
expect(adapter).toBeDefined();
});

describe("batchRequests", () => {
it("Should configure a single get batch request correctly", () => {
const rootUrl = "https://api.abc.com/";
const batchUrl = `${rootUrl}$batch`;
const config = new HttpBatchConfiguration({
batchEndpointUrl: batchUrl,
rootEndpointUrl: rootUrl
});
const defaultRequestOptions = new RequestOptions();
const adapter = new HttpMultiFetchAdapter(config, defaultRequestOptions);

const requests = [new Request({ url: `${rootUrl}users`, method: RequestMethod.Get })];
const batchRequest = adapter.batch(requests);

expect(batchRequest).toBeDefined();
expect(batchRequest.url).toEqual(batchUrl + "?0=users");
});

});
});