Skip to content

Commit ec7f65e

Browse files
finishing batch request implementation
1 parent d98d3e9 commit ec7f65e

File tree

8 files changed

+192
-107
lines changed

8 files changed

+192
-107
lines changed

lib/dynamics-web-api-callbacks.js

Lines changed: 129 additions & 68 deletions
Large diffs are not rendered by default.

lib/dynamics-web-api.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ function DynamicsWebApi(config) {
282282
//copy locally
283283
var isRef = request.select != null && request.select.length === 1 && request.select[0].endsWith("/$ref");
284284
return _makeRequest('GET', request, 'retrieve', { isRef: isRef }).then(function (response) {
285-
//if (select != null && select.length === 1 && select[0].endsWith("/$ref") && response.data["@odata.id"] != null) {
286-
// return Utility.convertToReferenceObject(response.data);
287-
//}
288-
289285
return response.data;
290286
});
291287
};
@@ -343,14 +339,9 @@ function DynamicsWebApi(config) {
343339

344340
//copy locally
345341
var ifmatch = request.ifmatch;
346-
return _makeRequest(method, request, 'update')
342+
return _makeRequest(method, request, 'update', { valueIfEmpty: true })
347343
.then(function (response) {
348-
if (response.data) {
349-
return response.data;
350-
}
351-
352-
return true; //updated
353-
344+
return response.data;
354345
}).catch(function (error) {
355346
if (ifmatch && error.status === 412) {
356347
//precondition failed - not updated
@@ -436,9 +427,7 @@ function DynamicsWebApi(config) {
436427

437428
return _makeRequest('PUT', request, 'updateSingleProperty')
438429
.then(function (response) {
439-
if (response.data) {
440-
return response.data;
441-
}
430+
return response.data;
442431
});
443432
};
444433

@@ -454,8 +443,8 @@ function DynamicsWebApi(config) {
454443

455444
//copy locally
456445
var ifmatch = request.ifmatch;
457-
return _makeRequest('DELETE', request, 'delete').then(function () {
458-
return true; //deleted
446+
return _makeRequest('DELETE', request, 'delete', { valueIfEmpty: true }).then(function (response) {
447+
return response.data;
459448
}).catch(function (error) {
460449
if (ifmatch && error.status === 412) {
461450
//precondition failed - not deleted
@@ -602,6 +591,7 @@ function DynamicsWebApi(config) {
602591
* @returns {Promise} D365 Web Api result
603592
*/
604593
this.retrieveAllRequest = function (request) {
594+
ErrorHelper.batchIncompatible('DynamicsWebApi.retrieveAllRequest', _isBatch);
605595
return _retrieveAllRequest(request);
606596
};
607597

@@ -641,6 +631,7 @@ function DynamicsWebApi(config) {
641631
* @returns {Promise} D365 Web Api result
642632
*/
643633
this.countAll = function (collection, filter, select) {
634+
ErrorHelper.batchIncompatible('DynamicsWebApi.countAll', _isBatch);
644635
return _retrieveAllRequest({
645636
collection: collection,
646637
filter: filter,
@@ -679,6 +670,7 @@ function DynamicsWebApi(config) {
679670
* @returns {Promise} D365 Web Api result
680671
*/
681672
this.retrieveAll = function (collection, select, filter) {
673+
ErrorHelper.batchIncompatible('DynamicsWebApi.retrieveAll', _isBatch);
682674
return _retrieveAllRequest({
683675
collection: collection,
684676
select: select,
@@ -710,13 +702,8 @@ function DynamicsWebApi(config) {
710702
fetchXml: fetchXml
711703
};
712704

713-
return _makeRequest("GET", request, 'executeFetchXml')
705+
return _makeRequest("GET", request, 'executeFetchXml', { pageNumber: pageNumber })
714706
.then(function (response) {
715-
716-
if (response.data['@' + DWA.Prefer.Annotations.FetchXmlPagingCookie] != null) {
717-
response.data.PagingInfo = Utility.getFetchXmlPagingCookie(response.data['@' + DWA.Prefer.Annotations.FetchXmlPagingCookie], pageNumber);
718-
}
719-
720707
return response.data;
721708
});
722709
};
@@ -762,6 +749,7 @@ function DynamicsWebApi(config) {
762749
};
763750

764751
var innerExecuteFetchXmlAll = function (collection, fetchXml, includeAnnotations, impersonateUserId) {
752+
ErrorHelper.batchIncompatible('DynamicsWebApi.executeFetchXmlAll', _isBatch);
765753
return _executeFetchXmlAll(collection, fetchXml, includeAnnotations, null, null, impersonateUserId);
766754
};
767755

@@ -933,9 +921,7 @@ function DynamicsWebApi(config) {
933921
};
934922

935923
return _makeRequest("GET", request, 'executeFunction').then(function (response) {
936-
if (response.data) {
937-
return response.data;
938-
}
924+
return response.data;
939925
});
940926
};
941927

@@ -979,9 +965,7 @@ function DynamicsWebApi(config) {
979965
};
980966

981967
var onSuccess = function (response) {
982-
if (response.data) {
983-
return response.data;
984-
}
968+
return response.data;
985969
};
986970

987971

@@ -1375,13 +1359,26 @@ function DynamicsWebApi(config) {
13751359
return this.retrieveMultipleRequest(request);
13761360
};
13771361

1378-
this.batch = function () {
1362+
/**
1363+
* Starts a batch request.
1364+
*
1365+
*/
1366+
this.startBatch = function () {
13791367
_isBatch = true;
13801368
};
13811369

1370+
/**
1371+
* Executes a batch request. Please call DynamicsWebApi.startBatch() first to start a batch request.
1372+
* @returns {Promise} D365 Web Api result
1373+
*/
13821374
this.executeBatch = function () {
1375+
ErrorHelper.batchNotStarted(_isBatch);
1376+
13831377
_isBatch = false;
1384-
return _makeRequest('POST', { collection: '$batch' }, 'executeBatch');
1378+
return _makeRequest('POST', { collection: '$batch' }, 'executeBatch')
1379+
.then(function (response) {
1380+
return response.data;
1381+
});
13851382
};
13861383

13871384
/**

lib/helpers/ErrorHelper.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ var ErrorHelper = {
167167
if (typeof callbackParameter != "function") {
168168
throwParameterError(functionName, parameterName, "Function");
169169
}
170+
},
171+
172+
batchIncompatible: function (functionName, isBatch) {
173+
if (isBatch) {
174+
throw new Error(functionName + " cannot be used in a BATCH request");
175+
}
176+
},
177+
178+
batchNotStarted: function (isBatch) {
179+
if (!isBatch) {
180+
throw new Error("Batch operation has not been started. Please call a DynamicsWebApi.startBatch() function prior to calling DynamicsWebApi.executeBatch() to perform a batch request correctly.");
181+
}
170182
}
171183
};
172184

lib/requests/helpers/parseResponse.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ function parseData(object, parseParams) {
9797
}
9898
}
9999

100+
if (parseParams) {
101+
if (parseParams.hasOwnProperty('pageNumber') && object['@' + DWA.Prefer.Annotations.FetchXmlPagingCookie] != null) {
102+
object.PagingInfo = Utility.getFetchXmlPagingCookie(object['@' + DWA.Prefer.Annotations.FetchXmlPagingCookie], parseParams.pageNumber);
103+
}
104+
}
105+
100106
return object;
101107
}
102108

@@ -139,6 +145,9 @@ function parseBatchResponse(response, parseParams, requestNumber) {
139145
if (entityUrl.length) {
140146
result.push(/([0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12})\)$/i.exec(entityUrl[0])[1]);
141147
}
148+
else if (parseParams[i].hasOwnProperty('valueIfEmpty')) {
149+
result.push(parseParams[i].valueIfEmpty);
150+
}
142151
}
143152
else {
144153
result.push(parseData(JSON.parse(responseData, dateReviver), parseParams[requestNumber]));
@@ -160,7 +169,6 @@ function parseBatchResponse(response, parseParams, requestNumber) {
160169
*/
161170
module.exports = function parseResponse(response, responseHeaders, parseParams) {
162171
var parseResult = undefined;
163-
164172
if (response.length) {
165173
if (response.indexOf('--batchresponse_') > -1) {
166174
var batch = parseBatchResponse(response, parseParams);
@@ -178,12 +186,13 @@ module.exports = function parseResponse(response, responseHeaders, parseParams)
178186
var entityUrl = responseHeaders['OData-EntityId']
179187
? responseHeaders['OData-EntityId']
180188
: responseHeaders['odata-entityid'];
189+
181190
parseResult = /([0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12})\)$/i.exec(entityUrl)[1];
182191
}
192+
else if (parseParams.length && parseParams[0].hasOwnProperty('valueIfEmpty')) {
193+
parseResult = parseParams[0].valueIfEmpty;
194+
}
183195
}
184196

185-
//clear response parse parameters array
186-
parseParams.length = 0;
187-
188197
return parseResult;
189198
};

lib/requests/http.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,13 @@ var httpRequest = function (method, uri, data, additionalHeaders, responseParams
8686
errorCallback(error);
8787
break;
8888
}
89+
90+
responseParams.length = 0;
8991
});
9092
});
9193

9294
request.on('error', function (error) {
95+
responseParams.length = 0;
9396
errorCallback(error);
9497
});
9598

lib/requests/xhr.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ var xhrRequest = function (method, uri, data, additionalHeaders, responseParams,
6060
}
6161

6262
request = null;
63+
responseParams.length = 0;
6364
}
6465
};
6566

6667
request.onerror = function () {
6768
errorCallback({ message: "Network Error" });
69+
responseParams.length = 0;
6870
request = null;
6971
};
7072

7173
request.ontimeout = function (error) {
7274
errorCallback({ message: "Request Timed Out" });
75+
responseParams.length = 0;
7376
request = null;
7477
};
7578

lib/utilities/RequestConverter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
120120
}
121121

122122
if (request.ifmatch != null && request.ifnonematch != null) {
123-
throw new Error('DynamicsWebApi.' + functionName + ". Either one of request.ifmatch or request.ifnonematch parameters should be used in a call, not both.")
123+
throw new Error('DynamicsWebApi.' + functionName + ". Either one of request.ifmatch or request.ifnonematch parameters should be used in a call, not both.");
124124
}
125125

126126
if (request.ifmatch) {

tests/main-tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ describe("promises -", function () {
19221922
collection: "tests",
19231923
entity: mocks.data.testEntity,
19241924
ifmatch: "match"
1925-
}
1925+
};
19261926

19271927
dynamicsWebApiTest
19281928
.updateRequest(dwaRequest)
@@ -1964,7 +1964,7 @@ describe("promises -", function () {
19641964
collection: "tests",
19651965
entity: mocks.data.testEntity,
19661966
ifmatch: "match"
1967-
}
1967+
};
19681968

19691969
dynamicsWebApiTest
19701970
.updateRequest(dwaRequest)
@@ -2006,7 +2006,7 @@ describe("promises -", function () {
20062006
collection: "tests",
20072007
entity: mocks.data.testEntity,
20082008
ifmatch: "match"
2009-
}
2009+
};
20102010

20112011
dynamicsWebApiTest
20122012
.updateRequest(dwaRequest)
@@ -2032,7 +2032,7 @@ describe("promises -", function () {
20322032
id: mocks.data.testEntityId,
20332033
collection: "tests",
20342034
entity: mocks.data.testEntity
2035-
}
2035+
};
20362036

20372037
var scope;
20382038
before(function () {

0 commit comments

Comments
 (0)