Skip to content

Commit f80a25d

Browse files
allow multiple nested expand options; add $apply request option; closes #67
1 parent 3e45b21 commit f80a25d

File tree

5 files changed

+110
-61
lines changed

5 files changed

+110
-61
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ If you feel that this project saved your time and you would like to support it,
1515
Please check [suggestions and contributions](#contributions) section to learn more on how you can help to improve this project.
1616

1717
**DynamicsWebApi v2 is coming!**
18+
There will be breaking changes between v1 and v2. One of them: v2 will not have simple requests, only the advanced ones, therefore I highly recommend using request objects for making requests.
19+
Also v2 will be written in TypeScript and include numerous optimizations. I am very excited to finally release the new version and I hope you too! Stay tuned!
1820

1921
**Important!** For some reason, npm was not removing `.git` folder from a published package,
2022
even though [it should have done it by default](https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package), therefore
@@ -234,6 +236,7 @@ an invalid property you will receive either an error saying that the request is
234236

235237
Property Name | Type | Operation(s) Supported | Description
236238
------------ | ------------- | ------------- | -------------
239+
apply | string | `retrieveMultipleRequest`, `retrieveAllRequest` | `v1.6.4+` Sets the $apply system query option to aggregate and group your data dynamically. [More Info](https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/query-data-web-api#aggregate-and-grouping-results)
237240
async | Boolean | All | **Important! XHR requests only!** Indicates whether the requests should be made synchronously or asynchronously. Default value is `true` (asynchronously).
238241
collection | String | All | The name of the Entity Collection (or Entity Logical name in `v1.4.0+`).
239242
contentId | String | `createRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | `v1.5.6+` **BATCH REQUESTS ONLY!** Sets Content-ID header or references request in a Change Set. [More Info](https://www.odata.org/documentation/odata-version-3-0/batch-processing/)
@@ -268,18 +271,13 @@ while request.expand property is an Array of Expand Objects for Advanced operati
268271

269272
Property Name | Type | Description
270273
------------ | ------------- | -------------
274+
expand | Array | An array of Expand Objects (described below the table) representing the $expand OData System Query Option value to control which related records are also returned.
271275
filter | String | Use the $filter system query option to set criteria for which related entities will be returned.
272276
orderBy | Array | An Array (of Strings) representing the order in which related items are returned using the $orderby system query option. Use the asc or desc suffix to specify ascending or descending order respectively. The default is ascending if the suffix isn't applied.
273277
property | String | A name of a single-valued navigation property which needs to be expanded.
274278
select | Array | An Array (of Strings) representing the $select OData System Query Option to control which attributes will be returned.
275279
top | Number | Limit the number of results returned by using the $top system query option.
276280

277-
The following seems to be fixed: ~~According to CRM developers ([here](http://stackoverflow.com/a/34742977/2042071) and [here](https://community.dynamics.com/crm/b/joegilldynamicscrm/archive/2016/03/23/web-api-querying-with-expand)
278-
$expand does not work for retrieveMultiple requests which is claimed as a bug of CRM Web API.~~
279-
Unconfirmed: Multi-level expands are not implemented yet. This situation may be changed with the future updates in the platform. Please look for the news!
280-
281-
For complex requests to Web API with multi-level expands use `executeFetchXml` function.
282-
283281
Starting from version 1.2.8, all requests to Web API that have long URLs (more than 2000 characters) are automatically converted to a Batch Request.
284282
This feature is very convenient when you make a call with big Fetch XMLs. No special parameters needed to do a convertation.
285283

lib/utilities/RequestConverter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
9696

9797
if (request.userQuery) {
9898
requestArray.push("userQuery=" + ErrorHelper.guidParameterCheck(request.userQuery, 'DynamicsWebApi.' + functionName, "request.userQuery"));
99-
}
99+
}
100+
101+
if (request.apply) {
102+
ErrorHelper.stringParameterCheck(request.apply, 'DynamicsWebApi.' + functionName, "request.apply");
103+
requestArray.push("$apply=" + request.apply);
104+
}
100105

101106
if (request.count) {
102107
ErrorHelper.boolParameterCheck(request.count, 'DynamicsWebApi.' + functionName, "request.count");

tests/main-tests.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2791,7 +2791,45 @@ describe("promises -", function () {
27912791
it("all requests have been made", function () {
27922792
expect(scope.isDone()).to.be.true;
27932793
});
2794-
});
2794+
});
2795+
2796+
describe("$apply", function () {
2797+
var scope;
2798+
before(function () {
2799+
var response = mocks.responses.multipleResponse;
2800+
scope = nock(mocks.webApiUrl, {
2801+
reqheaders: {
2802+
Prefer: 'odata.include-annotations="' + DWA.Prefer.Annotations.FormattedValue + '"'
2803+
}
2804+
})
2805+
.get(mocks.responses.collectionUrl + "?$apply=groupby((statuscode),aggregate(estimatedvalue with sum as total))")
2806+
.reply(response.status, response.responseText, response.responseHeaders);
2807+
});
2808+
2809+
after(function () {
2810+
nock.cleanAll();
2811+
});
2812+
2813+
it("returns a correct response", function (done) {
2814+
var dwaRequest = {
2815+
collection: "tests",
2816+
includeAnnotations: DWA.Prefer.Annotations.FormattedValue,
2817+
apply: "groupby((statuscode),aggregate(estimatedvalue with sum as total))"
2818+
};
2819+
2820+
dynamicsWebApiTest.retrieveMultipleRequest(dwaRequest)
2821+
.then(function (object) {
2822+
expect(object).to.deep.equal(mocks.responses.multiple());
2823+
done();
2824+
}).catch(function (object) {
2825+
done(object);
2826+
});
2827+
});
2828+
2829+
it("all requests have been made", function () {
2830+
expect(scope.isDone()).to.be.true;
2831+
});
2832+
});
27952833
});
27962834

27972835
describe("dynamicsWebApi.retrieveAllRequest -", function () {

0 commit comments

Comments
 (0)