Skip to content

Commit 870743d

Browse files
2 parents 9e88ad2 + f09dff5 commit 870743d

File tree

8 files changed

+141
-6
lines changed

8 files changed

+141
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,6 @@ ModelManifest.xml
253253
/scripts
254254
/coverage
255255
/tests/browser
256+
257+
# Webstorm
258+
.idea

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Travis](https://img.shields.io/travis/AleksandrRogov/DynamicsWebApi.svg?style=flat-square)](https://travis-ci.org/AleksandrRogov/DynamicsWebApi)
44
[![Coveralls](https://img.shields.io/coveralls/AleksandrRogov/DynamicsWebApi.svg?style=flat-square)](https://coveralls.io/github/AleksandrRogov/DynamicsWebApi)
5+
![npm](https://img.shields.io/npm/dm/dynamics-web-api?style=flat-square)
6+
![npm](https://img.shields.io/npm/dt/dynamics-web-api?style=flat-square)
57

68
DynamicsWebApi is a Microsoft Dynamics 365 CE (CRM) / Common Data Service Web API helper library written in JavaScript.
79
It is compatible with: Common Data Service, Dynamics 365 CE (online), Dynamics 365 CE (on-premises), Dynamics CRM 2016, Dynamics CRM Online

lib/dynamics-web-api-callbacks.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,21 +1561,28 @@ function DynamicsWebApi(config) {
15611561

15621562
/**
15631563
* Executes a batch request. Please call DynamicsWebApi.startBatch() first to start a batch request.
1564+
* @param request
15641565
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
15651566
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
15661567
*/
1567-
this.executeBatch = function (successCallback, errorCallback) {
1568+
this.executeBatch = function (successCallback, errorCallback, request) {
1569+
1570+
request = request || {};
1571+
15681572
ErrorHelper.batchNotStarted(_isBatch);
15691573
ErrorHelper.callbackParameterCheck(successCallback, "DynamicsWebApi.executeBatch", "successCallback");
15701574
ErrorHelper.callbackParameterCheck(errorCallback, "DynamicsWebApi.executeBatch", "errorCallback");
1575+
ErrorHelper.parameterCheck(request, "DynamicsWebApi.executeBatch", "request");
15711576

15721577
_isBatch = false;
15731578

15741579
var onSuccess = function (response) {
15751580
successCallback(response.data);
15761581
};
15771582

1578-
_makeRequest('POST', { collection: '$batch' }, 'executeBatch', onSuccess, errorCallback);
1583+
request.collection = '$batch';
1584+
1585+
_makeRequest('POST', request, 'executeBatch', onSuccess, errorCallback);
15791586
};
15801587

15811588
/**

lib/dynamics-web-api.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,13 +1390,20 @@ function DynamicsWebApi(config) {
13901390

13911391
/**
13921392
* Executes a batch request. Please call DynamicsWebApi.startBatch() first to start a batch request.
1393+
* @param request
13931394
* @returns {Promise} D365 Web Api result
13941395
*/
1395-
this.executeBatch = function () {
1396+
this.executeBatch = function (request) {
1397+
1398+
request = request || {};
1399+
1400+
ErrorHelper.parameterCheck(request, 'DynamicsWebApi.executeBatch', 'request');
13961401
ErrorHelper.batchNotStarted(_isBatch);
13971402

1403+
request.collection = '$batch';
1404+
13981405
_isBatch = false;
1399-
return _makeRequest('POST', { collection: '$batch' }, 'executeBatch')
1406+
return _makeRequest('POST', request, 'executeBatch')
14001407
.then(function (response) {
14011408
return response.data;
14021409
});

tests/callbacks-tests.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,63 @@ describe("callbacks -", function () {
44914491
});
44924492
});
44934493

4494+
describe("update / delete passing a request parameter", function () {
4495+
var scope;
4496+
var rBody = mocks.data.batchUpdateDelete;
4497+
var rBodys = rBody.split('\n');
4498+
var checkBody = '';
4499+
for (var i = 0; i < rBodys.length; i++) {
4500+
checkBody += rBodys[i];
4501+
}
4502+
before(function () {
4503+
var response = mocks.responses.batchUpdateDelete;
4504+
scope = nock(mocks.webApiUrl + '$batch', {
4505+
reqheaders: {
4506+
Authorization: "Bearer 123"
4507+
}
4508+
})
4509+
.filteringRequestBody(function (body) {
4510+
body = body.replace(/dwa_batch_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, 'dwa_batch_XXX');
4511+
body = body.replace(/changeset_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, 'changeset_XXX');
4512+
var bodys = body.split('\n');
4513+
4514+
var resultBody = '';
4515+
for (var i = 0; i < bodys.length; i++) {
4516+
resultBody += bodys[i];
4517+
}
4518+
return resultBody;
4519+
})
4520+
.post("", checkBody)
4521+
.reply(response.status, response.responseText, response.responseHeaders);
4522+
});
4523+
4524+
after(function () {
4525+
nock.cleanAll();
4526+
});
4527+
4528+
it("returns a correct response", function (done) {
4529+
dynamicsWebApiTest.startBatch();
4530+
4531+
dynamicsWebApiTest.update(mocks.data.testEntityId2, 'records', { firstname: "Test", lastname: "Batch!" });
4532+
dynamicsWebApiTest.deleteRecord(mocks.data.testEntityId2, 'records', null, null, 'firstname');
4533+
4534+
dynamicsWebApiTest.executeBatch(function (object) {
4535+
expect(object.length).to.be.eq(2);
4536+
4537+
expect(object[0]).to.be.true;
4538+
expect(object[1]).to.be.undefined;
4539+
4540+
done();
4541+
}, function (object) {
4542+
done(object);
4543+
}, { token: '123' });
4544+
});
4545+
4546+
it("all requests have been made", function () {
4547+
expect(scope.isDone()).to.be.true;
4548+
});
4549+
});
4550+
44944551
describe("update / delete - returns an error", function () {
44954552
var scope;
44964553
var rBody = mocks.data.batchUpdateDelete;

tests/main-tests.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,6 +4904,64 @@ describe("promises -", function () {
49044904
});
49054905
});
49064906

4907+
describe("update / delete passing a request parameter", function () {
4908+
var scope;
4909+
var rBody = mocks.data.batchUpdateDelete;
4910+
var rBodys = rBody.split('\n');
4911+
var checkBody = '';
4912+
for (var i = 0; i < rBodys.length; i++) {
4913+
checkBody += rBodys[i];
4914+
}
4915+
before(function () {
4916+
var response = mocks.responses.batchUpdateDelete;
4917+
scope = nock(mocks.webApiUrl + '$batch', {
4918+
reqheaders: {
4919+
Authorization: "Bearer 123"
4920+
}
4921+
})
4922+
.filteringRequestBody(function (body) {
4923+
body = body.replace(/dwa_batch_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, 'dwa_batch_XXX');
4924+
body = body.replace(/changeset_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, 'changeset_XXX');
4925+
var bodys = body.split('\n');
4926+
4927+
var resultBody = '';
4928+
for (var i = 0; i < bodys.length; i++) {
4929+
resultBody += bodys[i];
4930+
}
4931+
return resultBody;
4932+
})
4933+
.post("", checkBody)
4934+
.reply(response.status, response.responseText, response.responseHeaders);
4935+
});
4936+
4937+
after(function () {
4938+
nock.cleanAll();
4939+
});
4940+
4941+
it("returns a correct response", function (done) {
4942+
dynamicsWebApiTest.startBatch();
4943+
4944+
dynamicsWebApiTest.update(mocks.data.testEntityId2, 'records', { firstname: "Test", lastname: "Batch!" });
4945+
dynamicsWebApiTest.deleteRecord(mocks.data.testEntityId2, 'records', 'firstname');
4946+
4947+
dynamicsWebApiTest.executeBatch({ token: '123' })
4948+
.then(function (object) {
4949+
expect(object.length).to.be.eq(2);
4950+
4951+
expect(object[0]).to.be.true;
4952+
expect(object[1]).to.be.undefined;
4953+
4954+
done();
4955+
}).catch(function (object) {
4956+
done(object);
4957+
});
4958+
});
4959+
4960+
it("all requests have been made", function () {
4961+
expect(scope.isDone()).to.be.true;
4962+
});
4963+
});
4964+
49074965
describe("update / delete - returns an error", function () {
49084966
var scope;
49094967
var rBody = mocks.data.batchUpdateDelete;

types/dynamics-web-api-callbacks.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,9 @@ declare class DynamicsWebApi {
529529
* Executes a batch request. Please call DynamicsWebApi.startBatch() first to start a batch request.
530530
* @param successCallback - The function that will be passed through and be called by a successful response.
531531
* @param errorCallback - The function that will be passed through and be called by a failed response.
532+
* @param request
532533
*/
533-
executeBatch(successCallback: Function, errorCallback: Function): void;
534+
executeBatch(successCallback: Function, errorCallback: Function, request?: Request): void;
534535
/**
535536
* Creates a new instance of DynamicsWebApi
536537
*

types/dynamics-web-api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ declare class DynamicsWebApi {
434434
/**
435435
* Executes a batch request. Please call DynamicsWebApi.startBatch() first to start a batch request.
436436
*/
437-
executeBatch(): Promise<any[]>;
437+
executeBatch(request?: Request): Promise<any[]>;
438438
/**
439439
* Creates a new instance of DynamicsWebApi
440440
*

0 commit comments

Comments
 (0)