Skip to content

Commit d6b22a5

Browse files
committed
Tweaks to changelog
1 parent 555a537 commit d6b22a5

File tree

12 files changed

+198
-382
lines changed

12 files changed

+198
-382
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ The root endpoint url is simply the base address of your api and the endpoint ba
5353

5454
The setAllowedBatchEndpoint has some options that can be passed in as a third paramter to the call which are explained below.
5555

56-
```langauge-javscript
56+
```language-javascript
5757
{
5858
maxBatchedRequestPerCall: 10,
5959
minimumBatchSize: 2,
6060
batchRequestCollectionDelay: 100,
6161
ignoredVerbs: ['head'],
6262
sendCookies: false,
63-
enabled: true.
64-
63+
enabled: true
6564
}
6665
```
6766

dist/ChangeLog.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
15/07/2015 V1.11.0
2-
HUGE refactor of library geared towards supporting multiple different formats of batch request as response i.e.
1+
10/08/2015 V1.11.0
2+
HUGE refactor of the library geared towards supporting multiple different formats of batch request and response i.e.
33
http 1.1 batch, NodeJS, Facebook etc. The library now has the concept of batch adapters which are able to transform raw
44
http requests and responses into the correct batch formats for a particular server.
5+
Added the default http 1.1 adapter which supports .net / java
6+
Added a node js multifetch <https://github.com/debitoor/multifetch> adapter which supports fetch a batch of GET requests.
57

68
15/07/2015 V1.10.0
79
Added support for complex relative urls i.e. './api/products' or '../api/products'

dist/angular-http-batch.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* angular-http-batcher - v1.11.0 - 2015-08-08
2+
* angular-http-batcher - v1.11.0 - 2015-08-10
33
* https://github.com/jonsamwell/angular-http-batcher
44
* Copyright (c) 2015 Jon Samwell
55
*/
@@ -145,6 +145,7 @@ function HttpBatchConfigFn() {
145145
canBatch = canBatchRequestFn(url, method);
146146
} else {
147147
canBatch = config.batchEndpointUrl !== url &&
148+
url.indexOf(config.batchEndpointUrl) === -1 &&
148149
config.ignoredVerbs.indexOf(method.toLowerCase()) === -1;
149150
}
150151
}
@@ -267,11 +268,12 @@ BatchRequestManager.prototype.send = sendFn;
267268
BatchRequestManager.prototype.addRequest = addRequestFn;
268269
BatchRequestManager.prototype.flush = flushFn;
269270

270-
function HttpBatcherFn($injector, $timeout, httpBatchConfig, httpBatchAdapter) {
271+
function HttpBatcherFn($injector, $timeout, httpBatchConfig, httpBatchAdapter, nodeJsMultiFetchAdapter) {
271272
var self = this,
272273
currentBatchedRequests = {},
273274
adapters = {
274-
httpBatchAdapter: httpBatchAdapter
275+
httpBatchAdapter: httpBatchAdapter,
276+
nodeJsMultiFetchAdapter: nodeJsMultiFetchAdapter
275277
};
276278

277279
self.canBatchRequest = canBatchRequestFn;
@@ -312,7 +314,8 @@ HttpBatcherFn.$inject = [
312314
'$injector',
313315
'$timeout',
314316
'httpBatchConfig',
315-
'httpBatchAdapter'
317+
'httpBatchAdapter',
318+
'nodeJsMultiFetchAdapter'
316319
];
317320

318321
angular.module(window.ahb.name).service('httpBatcher', HttpBatcherFn);

dist/angular-http-batch.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-13.5 KB
Binary file not shown.

src/providers/httpBatchConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ function HttpBatchConfigFn() {
124124
canBatch = canBatchRequestFn(url, method);
125125
} else {
126126
canBatch = config.batchEndpointUrl !== url &&
127+
url.indexOf(config.batchEndpointUrl) === -1 &&
127128
config.ignoredVerbs.indexOf(method.toLowerCase()) === -1;
128129
}
129130
}
Lines changed: 30 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
function HttpBatchAdapter($document, $window, httpBatchConfig) {
2-
var self = this,
3-
constants = {
4-
httpVersion: 'HTTP/1.1',
5-
contentType: 'Content-Type',
6-
newline: '\r\n',
7-
emptyString: '',
8-
singleSpace: ' ',
9-
forwardSlash: '/',
10-
doubleDash: '--',
11-
colon: ':'
12-
};
1+
function NodeJsMultiFetchAdapter() {
2+
var self = this;
133

14-
self.key = 'httpBatchAdapter';
4+
self.key = 'nodeJsMultiFetchAdapter';
155
self.buildRequest = buildRequestFn;
166
self.parseResponse = parseResponseFn;
177
self.canBatchRequest = canBatchRequestFn;
@@ -28,53 +18,31 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
2818
* @returns {object} - a http config object.
2919
*/
3020
function buildRequestFn(requests, config) {
31-
var boundary = httpBatchConfig.calculateBoundary(),
32-
httpConfig = {
33-
method: 'POST',
34-
url: config.batchEndpointUrl,
21+
var httpConfig = {
22+
method: 'GET',
23+
url: config.batchEndpointUrl + '?',
3524
cache: false,
3625
headers: config.batchRequestHeaders || {}
3726
},
38-
batchBody = [],
39-
urlInfo, i, request, header;
40-
41-
httpConfig.headers[constants.contentType] = 'multipart/mixed; boundary=' + boundary;
27+
encodedUrl, i, request,
28+
urlParts;
4229

4330
for (i = 0; i < requests.length; i += 1) {
4431
request = requests[i];
45-
urlInfo = getUrlInfo(request.url);
46-
47-
batchBody.push(constants.doubleDash + boundary);
48-
if (config.batchPartRequestHeaders) {
49-
for (header in config.batchPartRequestHeaders) {
50-
batchBody.push(header + constants.colon + constants.singleSpace + config.batchPartRequestHeaders[header]);
51-
}
52-
}
53-
54-
batchBody.push('Content-Type: application/http; msgtype=request', constants.emptyString);
55-
56-
batchBody.push(request.method + ' ' + urlInfo.relativeUrl + ' ' + constants.httpVersion);
57-
batchBody.push('Host: ' + urlInfo.host);
32+
urlParts = request.url.split('?');
5833

59-
for (header in request.headers) {
60-
batchBody.push(header + constants.colon + constants.singleSpace + request.headers[header]);
34+
encodedUrl = urlParts[0].replace(config.serviceUrl, '');
35+
if (urlParts.length > 1) {
36+
encodedUrl += '?' + encodeURIComponent(urlParts[1]);
6137
}
6238

63-
if (config.sendCookies === true && $document[0].cookie && $document[0].cookie.length > 0) {
64-
batchBody.push('Cookie: ' + $document[0].cookie);
39+
if (i > 0) {
40+
httpConfig.url += '&';
6541
}
6642

67-
batchBody.push(constants.emptyString);
68-
69-
if (request.data) {
70-
batchBody.push(request.data);
71-
}
72-
73-
batchBody.push(constants.emptyString);
43+
httpConfig.url += i.toString() + '=' + encodedUrl;
7444
}
7545

76-
batchBody.push(constants.doubleDash + boundary + constants.doubleDash);
77-
httpConfig.data = batchBody.join(constants.newline);
7846
return httpConfig;
7947
}
8048

@@ -87,18 +55,20 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
8755
*/
8856
function parseResponseFn(requests, rawResponse, config) {
8957
var batchResponses = [],
90-
boundaryToken = findResponseBoundary(rawResponse.headers()['content-type']),
91-
parts = rawResponse.data.split(constants.doubleDash + boundaryToken + constants.newline),
92-
i,
93-
part,
94-
responseCount = 0;
58+
i, request,
59+
responseData = rawResponse.data,
60+
dataPart;
9561

96-
for (i = 0; i < parts.length; i += 1) {
97-
part = parts[i];
98-
if (part !== constants.emptyString) {
99-
batchResponses.push(processResponse(part, requests[responseCount], boundaryToken));
100-
responseCount += 1;
101-
}
62+
for (i = 0; i < requests.length; i += 1) {
63+
request = requests[i];
64+
dataPart = responseData[i.toString()];
65+
66+
batchResponses.push(new window.ahb.HttpBatchResponseData(
67+
request,
68+
dataPart.statusCode,
69+
'',
70+
dataPart.body,
71+
dataPart.headers));
10272
}
10373

10474
return batchResponses;
@@ -111,132 +81,8 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
11181
* @returns {boolean} false to indicate the request type is not supported.
11282
*/
11383
function canBatchRequestFn(request, config) {
114-
return true;
115-
}
116-
117-
/**
118-
* mainly here to polyfill ie8 :-(
119-
*/
120-
function trim(data) {
121-
if (data.trim) {
122-
data = data.trim();
123-
} else {
124-
data = data.replace(/^\s+|\s+$/g, '');
125-
}
126-
127-
return data;
128-
}
129-
130-
function getUrlInfo(url) {
131-
var protocol,
132-
host,
133-
relativeUrl,
134-
protocolEndIndex,
135-
urlParts;
136-
137-
if (url.indexOf('./') > -1 || url.indexOf('../') > -1) {
138-
// we have a complex relative url i.e. './api/products' or '../api/products
139-
var parser = document.createElement('a');
140-
parser.href = url;
141-
url = parser.href;
142-
}
143-
144-
if (url.indexOf('://') > -1) {
145-
protocolEndIndex = url.indexOf('://') + 3;
146-
urlParts = url.slice(protocolEndIndex).split(constants.forwardSlash);
147-
// we have an absolute url
148-
protocol = url.substring(0, protocolEndIndex);
149-
// Get the host portion of the url from '://' to the next'/'
150-
// [https://www.somedomain.com/]api/messages
151-
host = urlParts[0];
152-
relativeUrl = (function () {
153-
delete urlParts[0];
154-
return urlParts.join(constants.forwardSlash);
155-
}());
156-
} else {
157-
//we have a relative url
158-
relativeUrl = url;
159-
protocol = $window.location.protocol;
160-
host = $window.location.host;
161-
}
162-
163-
return {
164-
protocol: protocol,
165-
host: host,
166-
relativeUrl: relativeUrl
167-
};
168-
}
169-
170-
function findResponseBoundary(contentType) {
171-
var boundaryText = 'boundary=',
172-
startIndex = contentType.indexOf(boundaryText),
173-
boundary = contentType.substring(startIndex + boundaryText.length);
174-
175-
// the boundary might be quoted so remove the quotes
176-
boundary = boundary.replace(/"/g, constants.emptyString);
177-
return boundary;
178-
}
179-
180-
function convertDataToCorrectType(contentType, dataStr) {
181-
var data = dataStr;
182-
contentType = contentType.toLowerCase();
183-
184-
if (contentType.indexOf('json') > -1) {
185-
data = angular.fromJson(dataStr);
186-
}
187-
188-
return data;
189-
}
190-
191-
function processResponse(part, request, boundaryToken) {
192-
var responseParts = part.split(constants.newline),
193-
result = {
194-
headers: {}
195-
},
196-
responsePart,
197-
i, j, regex, lineParts, headerParts, parsedSpaceBetweenHeadersAndMessage = false;
198-
199-
for (i = 0; i < responseParts.length; i += 1) {
200-
responsePart = responseParts[i];
201-
if (responsePart === constants.emptyString) {
202-
parsedSpaceBetweenHeadersAndMessage = result.contentType !== undefined;
203-
continue;
204-
}
205-
206-
if (result.contentType === undefined && responsePart.indexOf('-Type') !== -1 && responsePart.indexOf('; msgtype=response') === -1) {
207-
result.contentType = responsePart.split(constants.forwardSlash)[1];
208-
} else if (result.contentType !== undefined && parsedSpaceBetweenHeadersAndMessage === false) {
209-
headerParts = responsePart.split(constants.colon);
210-
result.headers[headerParts[0]] = trim(headerParts[1]);
211-
} else if (result.statusCode === undefined && responsePart.indexOf(constants.httpVersion) !== -1) {
212-
lineParts = responsePart.split(constants.singleSpace);
213-
result.statusCode = parseInt(lineParts[1], 10);
214-
result.statusText = lineParts.slice(2).join(constants.singleSpace);
215-
} else if (result.data === undefined && parsedSpaceBetweenHeadersAndMessage) {
216-
// need to get all the lines left apart from the last multipart seperator.
217-
result.data = '';
218-
j = 1;
219-
regex = new RegExp('--' + boundaryToken + '--', 'i');
220-
while (regex.test(responsePart) === false && ((i + j) <= responseParts.length)) {
221-
result.data += responsePart;
222-
responsePart = responseParts[i + j];
223-
j += 1;
224-
}
225-
226-
result.data = convertDataToCorrectType(result.contentType, result.data);
227-
break;
228-
}
229-
}
230-
231-
result.headers[constants.contentType] = result.contentType;
232-
return new window.ahb.HttpBatchResponseData(request, result.statusCode, result.statusText, result.data, result.headers);
84+
return request.method === 'GET';
23385
}
23486
}
23587

236-
HttpBatchAdapter.$inject = [
237-
'$document',
238-
'$window',
239-
'httpBatchConfig'
240-
];
241-
242-
angular.module(window.ahb.name).service('httpBatchAdapter', HttpBatchAdapter);
88+
angular.module(window.ahb.name).service('nodeJsMultiFetchAdapter', NodeJsMultiFetchAdapter);

src/services/httpBatcher.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ BatchRequestManager.prototype.send = sendFn;
9292
BatchRequestManager.prototype.addRequest = addRequestFn;
9393
BatchRequestManager.prototype.flush = flushFn;
9494

95-
function HttpBatcherFn($injector, $timeout, httpBatchConfig, httpBatchAdapter) {
95+
function HttpBatcherFn($injector, $timeout, httpBatchConfig, httpBatchAdapter, nodeJsMultiFetchAdapter) {
9696
var self = this,
9797
currentBatchedRequests = {},
9898
adapters = {
99-
httpBatchAdapter: httpBatchAdapter
99+
httpBatchAdapter: httpBatchAdapter,
100+
nodeJsMultiFetchAdapter: nodeJsMultiFetchAdapter
100101
};
101102

102103
self.canBatchRequest = canBatchRequestFn;
@@ -137,7 +138,8 @@ HttpBatcherFn.$inject = [
137138
'$injector',
138139
'$timeout',
139140
'httpBatchConfig',
140-
'httpBatchAdapter'
141+
'httpBatchAdapter',
142+
'nodeJsMultiFetchAdapter'
141143
];
142144

143145
angular.module(window.ahb.name).service('httpBatcher', HttpBatcherFn);

0 commit comments

Comments
 (0)