Skip to content

Commit b136701

Browse files
Added exclusions to collection names to allow fetching entity metadata.
1 parent ebfe0c2 commit b136701

File tree

8 files changed

+155
-83
lines changed

8 files changed

+155
-83
lines changed

DynamicsWebApi.njsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<OutputPath>.</OutputPath>
2727
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
2828
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{349c5851-65df-11da-9384-00065b846f21};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
29-
<ProjectView>ShowAllFiles</ProjectView>
29+
<ProjectView>ProjectFiles</ProjectView>
3030
<NodejsPort>1337</NodejsPort>
3131
<StartWebBrowser>True</StartWebBrowser>
3232
</PropertyGroup>

dist/dynamics-web-api-callbacks.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,27 +344,7 @@ function setStandardHeaders(additionalHeaders) {
344344
return additionalHeaders;
345345
}
346346

347-
/**
348-
* Sends a request to given URL with given parameters
349-
*
350-
* @param {string} method - Method of the request.
351-
* @param {string} uri - Request URI.
352-
* @param {Function} successCallback - A callback called on success of the request.
353-
* @param {Function} errorCallback - A callback called when a request failed.
354-
* @param {Object} config - DynamicsWebApi config.
355-
* @param {Object} [data] - Data to send in the request.
356-
* @param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
357-
* @param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
358-
* @returns {Promise}
359-
*/
360-
module.exports = function sendRequest(method, uri, config, data, additionalHeaders, successCallback, errorCallback, isAsync) {
361-
362-
if (!additionalHeaders) {
363-
additionalHeaders = {};
364-
}
365-
366-
additionalHeaders = setStandardHeaders(additionalHeaders);
367-
347+
function stringifyData(data, config) {
368348
var stringifiedData;
369349
if (data) {
370350
stringifiedData = JSON.stringify(data, function (key, value) {
@@ -390,15 +370,42 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
390370
});
391371
}
392372

373+
return stringifiedData;
374+
}
375+
376+
/**
377+
* Sends a request to given URL with given parameters
378+
*
379+
* @param {string} method - Method of the request.
380+
* @param {string} path - Request path.
381+
* @param {Function} successCallback - A callback called on success of the request.
382+
* @param {Function} errorCallback - A callback called when a request failed.
383+
* @param {Object} config - DynamicsWebApi config.
384+
* @param {Object} [data] - Data to send in the request.
385+
* @param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
386+
* @param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
387+
* @returns {Promise}
388+
*/
389+
module.exports = function sendRequest(method, path, config, data, additionalHeaders, successCallback, errorCallback, isAsync) {
390+
391+
if (!additionalHeaders) {
392+
additionalHeaders = {};
393+
}
394+
395+
additionalHeaders = setStandardHeaders(additionalHeaders);
396+
397+
//stringify passed data
398+
var stringifiedData = stringifyData(data, config);
399+
393400
//if the URL contains more characters than max possible limit, convert the request to a batch request
394-
if (uri.length > 2000) {
401+
if (path.length > 2000) {
395402
var batchBoundary = 'dwa_batch_' + generateUUID();
396403

397404
var batchBody = [];
398405
batchBody.push('--' + batchBoundary);
399406
batchBody.push('Content-Type: application/http');
400407
batchBody.push('Content-Transfer-Encoding: binary\n');
401-
batchBody.push(method + ' ' + config.webApiUrl + uri + ' HTTP/1.1');
408+
batchBody.push(method + ' ' + config.webApiUrl + path + ' HTTP/1.1');
402409

403410
for (var key in additionalHeaders) {
404411
batchBody.push(key + ': ' + additionalHeaders[key]);
@@ -411,7 +418,7 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
411418

412419
additionalHeaders = setStandardHeaders(additionalHeaders);
413420
additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchBoundary;
414-
uri = '$batch';
421+
path = '$batch';
415422
method = 'POST';
416423
}
417424

@@ -433,7 +440,7 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
433440
additionalHeaders['Authorization'] = 'Bearer ' + token.accessToken;
434441
}
435442

436-
executeRequest(method, config.webApiUrl + uri, stringifiedData, additionalHeaders, successCallback, errorCallback, isAsync);
443+
executeRequest(method, config.webApiUrl + path, stringifiedData, additionalHeaders, successCallback, errorCallback, isAsync);
437444
};
438445

439446
//call a token refresh callback only if it is set and there is no "Authorization" header set yet
@@ -595,6 +602,17 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
595602
return { url: url, query: requestArray.join(joinSymbol), headers: headers };
596603
}
597604

605+
/**
606+
* @param {string} collectionName - name of the collection to check
607+
*/
608+
function getCollectionName(collectionName) {
609+
var exceptions = ['EntityDefinitions'];
610+
611+
return exceptions.indexOf(collectionName) > -1
612+
? collectionName
613+
: collectionName.toLowerCase();
614+
}
615+
598616
/**
599617
* Converts a request object to URL link
600618
*
@@ -612,7 +630,7 @@ function convertRequest(request, functionName, config) {
612630
ErrorHelper.stringParameterCheck(request.collection, "DynamicsWebApi." + functionName, "request.collection");
613631
}
614632

615-
var url = request.collection.toLowerCase();
633+
var url = getCollectionName(request.collection);
616634

617635
if (request.id) {
618636
request.id = ErrorHelper.guidParameterCheck(request.id, "DynamicsWebApi." + functionName, "request.id");

dist/dynamics-web-api-callbacks.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/dynamics-web-api.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,27 +344,7 @@ function setStandardHeaders(additionalHeaders) {
344344
return additionalHeaders;
345345
}
346346

347-
/**
348-
* Sends a request to given URL with given parameters
349-
*
350-
* @param {string} method - Method of the request.
351-
* @param {string} uri - Request URI.
352-
* @param {Function} successCallback - A callback called on success of the request.
353-
* @param {Function} errorCallback - A callback called when a request failed.
354-
* @param {Object} config - DynamicsWebApi config.
355-
* @param {Object} [data] - Data to send in the request.
356-
* @param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
357-
* @param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
358-
* @returns {Promise}
359-
*/
360-
module.exports = function sendRequest(method, uri, config, data, additionalHeaders, successCallback, errorCallback, isAsync) {
361-
362-
if (!additionalHeaders) {
363-
additionalHeaders = {};
364-
}
365-
366-
additionalHeaders = setStandardHeaders(additionalHeaders);
367-
347+
function stringifyData(data, config) {
368348
var stringifiedData;
369349
if (data) {
370350
stringifiedData = JSON.stringify(data, function (key, value) {
@@ -390,15 +370,42 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
390370
});
391371
}
392372

373+
return stringifiedData;
374+
}
375+
376+
/**
377+
* Sends a request to given URL with given parameters
378+
*
379+
* @param {string} method - Method of the request.
380+
* @param {string} path - Request path.
381+
* @param {Function} successCallback - A callback called on success of the request.
382+
* @param {Function} errorCallback - A callback called when a request failed.
383+
* @param {Object} config - DynamicsWebApi config.
384+
* @param {Object} [data] - Data to send in the request.
385+
* @param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
386+
* @param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
387+
* @returns {Promise}
388+
*/
389+
module.exports = function sendRequest(method, path, config, data, additionalHeaders, successCallback, errorCallback, isAsync) {
390+
391+
if (!additionalHeaders) {
392+
additionalHeaders = {};
393+
}
394+
395+
additionalHeaders = setStandardHeaders(additionalHeaders);
396+
397+
//stringify passed data
398+
var stringifiedData = stringifyData(data, config);
399+
393400
//if the URL contains more characters than max possible limit, convert the request to a batch request
394-
if (uri.length > 2000) {
401+
if (path.length > 2000) {
395402
var batchBoundary = 'dwa_batch_' + generateUUID();
396403

397404
var batchBody = [];
398405
batchBody.push('--' + batchBoundary);
399406
batchBody.push('Content-Type: application/http');
400407
batchBody.push('Content-Transfer-Encoding: binary\n');
401-
batchBody.push(method + ' ' + config.webApiUrl + uri + ' HTTP/1.1');
408+
batchBody.push(method + ' ' + config.webApiUrl + path + ' HTTP/1.1');
402409

403410
for (var key in additionalHeaders) {
404411
batchBody.push(key + ': ' + additionalHeaders[key]);
@@ -411,7 +418,7 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
411418

412419
additionalHeaders = setStandardHeaders(additionalHeaders);
413420
additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchBoundary;
414-
uri = '$batch';
421+
path = '$batch';
415422
method = 'POST';
416423
}
417424

@@ -433,7 +440,7 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
433440
additionalHeaders['Authorization'] = 'Bearer ' + token.accessToken;
434441
}
435442

436-
executeRequest(method, config.webApiUrl + uri, stringifiedData, additionalHeaders, successCallback, errorCallback, isAsync);
443+
executeRequest(method, config.webApiUrl + path, stringifiedData, additionalHeaders, successCallback, errorCallback, isAsync);
437444
};
438445

439446
//call a token refresh callback only if it is set and there is no "Authorization" header set yet
@@ -595,6 +602,17 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
595602
return { url: url, query: requestArray.join(joinSymbol), headers: headers };
596603
}
597604

605+
/**
606+
* @param {string} collectionName - name of the collection to check
607+
*/
608+
function getCollectionName(collectionName) {
609+
var exceptions = ['EntityDefinitions'];
610+
611+
return exceptions.indexOf(collectionName) > -1
612+
? collectionName
613+
: collectionName.toLowerCase();
614+
}
615+
598616
/**
599617
* Converts a request object to URL link
600618
*
@@ -612,7 +630,7 @@ function convertRequest(request, functionName, config) {
612630
ErrorHelper.stringParameterCheck(request.collection, "DynamicsWebApi." + functionName, "request.collection");
613631
}
614632

615-
var url = request.collection.toLowerCase();
633+
var url = getCollectionName(request.collection);
616634

617635
if (request.id) {
618636
request.id = ErrorHelper.guidParameterCheck(request.id, "DynamicsWebApi." + functionName, "request.id");

dist/dynamics-web-api.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/requests/sendRequest.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,7 @@ function setStandardHeaders(additionalHeaders) {
2323
return additionalHeaders;
2424
}
2525

26-
/**
27-
* Sends a request to given URL with given parameters
28-
*
29-
* @param {string} method - Method of the request.
30-
* @param {string} uri - Request URI.
31-
* @param {Function} successCallback - A callback called on success of the request.
32-
* @param {Function} errorCallback - A callback called when a request failed.
33-
* @param {Object} config - DynamicsWebApi config.
34-
* @param {Object} [data] - Data to send in the request.
35-
* @param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
36-
* @param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
37-
* @returns {Promise}
38-
*/
39-
module.exports = function sendRequest(method, uri, config, data, additionalHeaders, successCallback, errorCallback, isAsync) {
40-
41-
if (!additionalHeaders) {
42-
additionalHeaders = {};
43-
}
44-
45-
additionalHeaders = setStandardHeaders(additionalHeaders);
46-
26+
function stringifyData(data, config) {
4727
var stringifiedData;
4828
if (data) {
4929
stringifiedData = JSON.stringify(data, function (key, value) {
@@ -69,15 +49,42 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
6949
});
7050
}
7151

52+
return stringifiedData;
53+
}
54+
55+
/**
56+
* Sends a request to given URL with given parameters
57+
*
58+
* @param {string} method - Method of the request.
59+
* @param {string} path - Request path.
60+
* @param {Function} successCallback - A callback called on success of the request.
61+
* @param {Function} errorCallback - A callback called when a request failed.
62+
* @param {Object} config - DynamicsWebApi config.
63+
* @param {Object} [data] - Data to send in the request.
64+
* @param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
65+
* @param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
66+
* @returns {Promise}
67+
*/
68+
module.exports = function sendRequest(method, path, config, data, additionalHeaders, successCallback, errorCallback, isAsync) {
69+
70+
if (!additionalHeaders) {
71+
additionalHeaders = {};
72+
}
73+
74+
additionalHeaders = setStandardHeaders(additionalHeaders);
75+
76+
//stringify passed data
77+
var stringifiedData = stringifyData(data, config);
78+
7279
//if the URL contains more characters than max possible limit, convert the request to a batch request
73-
if (uri.length > 2000) {
80+
if (path.length > 2000) {
7481
var batchBoundary = 'dwa_batch_' + generateUUID();
7582

7683
var batchBody = [];
7784
batchBody.push('--' + batchBoundary);
7885
batchBody.push('Content-Type: application/http');
7986
batchBody.push('Content-Transfer-Encoding: binary\n');
80-
batchBody.push(method + ' ' + config.webApiUrl + uri + ' HTTP/1.1');
87+
batchBody.push(method + ' ' + config.webApiUrl + path + ' HTTP/1.1');
8188

8289
for (var key in additionalHeaders) {
8390
batchBody.push(key + ': ' + additionalHeaders[key]);
@@ -90,7 +97,7 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
9097

9198
additionalHeaders = setStandardHeaders(additionalHeaders);
9299
additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchBoundary;
93-
uri = '$batch';
100+
path = '$batch';
94101
method = 'POST';
95102
}
96103

@@ -116,7 +123,7 @@ module.exports = function sendRequest(method, uri, config, data, additionalHeade
116123
additionalHeaders['Authorization'] = 'Bearer ' + token.accessToken;
117124
}
118125

119-
executeRequest(method, config.webApiUrl + uri, stringifiedData, additionalHeaders, successCallback, errorCallback, isAsync);
126+
executeRequest(method, config.webApiUrl + path, stringifiedData, additionalHeaders, successCallback, errorCallback, isAsync);
120127
};
121128

122129
//call a token refresh callback only if it is set and there is no "Authorization" header set yet

lib/utilities/RequestConverter.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
144144
return { url: url, query: requestArray.join(joinSymbol), headers: headers };
145145
}
146146

147+
/**
148+
* @param {string} collectionName - name of the collection to check
149+
*/
150+
function getCollectionName(collectionName) {
151+
var exceptions = ['EntityDefinitions'];
152+
153+
return exceptions.indexOf(collectionName) > -1
154+
? collectionName
155+
: collectionName.toLowerCase();
156+
}
157+
147158
/**
148159
* Converts a request object to URL link
149160
*
@@ -161,7 +172,7 @@ function convertRequest(request, functionName, config) {
161172
ErrorHelper.stringParameterCheck(request.collection, "DynamicsWebApi." + functionName, "request.collection");
162173
}
163174

164-
var url = request.collection.toLowerCase();
175+
var url = getCollectionName(request.collection);
165176

166177
if (request.id) {
167178
request.id = ErrorHelper.guidParameterCheck(request.id, "DynamicsWebApi." + functionName, "request.id");

0 commit comments

Comments
 (0)