Skip to content

Commit 98b18b5

Browse files
fixes #45; added TypeScript declaration files
1 parent 3d627f9 commit 98b18b5

17 files changed

+1590
-290
lines changed

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ obj
55

66
*.njsproj
77
*.sln
8-
.ntvs_analysis.dat
8+
.ntvs_analysis.dat
9+
10+
# Exclude type definitions for callbacks version
11+
/types/dynamics-web-api-callbacks.d.ts

DynamicsWebApi.njsproj

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<OutputPath>.</OutputPath>
2828
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
2929
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{349c5851-65df-11da-9384-00065b846f21};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
30-
<ProjectView>ProjectFiles</ProjectView>
30+
<ProjectView>ShowAllFiles</ProjectView>
3131
<NodejsPort>1337</NodejsPort>
3232
<StartWebBrowser>True</StartWebBrowser>
3333
</PropertyGroup>
@@ -51,7 +51,6 @@
5151
<DebugSymbols>true</DebugSymbols>
5252
</PropertyGroup>
5353
<ItemGroup>
54-
<Compile Include="gulpfile.js" />
5554
<Compile Include="lib\dwa.js" />
5655
<Compile Include="lib\dynamics-web-api-callbacks.js" />
5756
<Compile Include="lib\dynamics-web-api.js" />
@@ -134,22 +133,12 @@
134133
<Folder Include="types\" />
135134
<Folder Include="tests\" />
136135
<Folder Include="tests\browser\" />
137-
<Folder Include="typings\" />
138-
<Folder Include="typings\globals\" />
139-
<Folder Include="typings\globals\chai\" />
140-
<Folder Include="typings\globals\mocha\" />
141-
<Folder Include="typings\globals\node\" />
142-
<Folder Include="typings\globals\sinon\" />
143136
</ItemGroup>
144137
<ItemGroup>
138+
<TypeScriptCompile Include="types\dynamics-web-api.d.ts" />
145139
<TypeScriptCompile Include="types\dynamics-web-api-callbacks.d.ts">
146140
<SubType>Code</SubType>
147141
</TypeScriptCompile>
148-
<TypeScriptCompile Include="typings\globals\chai\index.d.ts" />
149-
<TypeScriptCompile Include="typings\globals\mocha\index.d.ts" />
150-
<TypeScriptCompile Include="typings\globals\node\index.d.ts" />
151-
<TypeScriptCompile Include="typings\globals\sinon\index.d.ts" />
152-
<TypeScriptCompile Include="typings\index.d.ts" />
153142
</ItemGroup>
154143
<!-- Do not delete the following Import Project. While this appears to do nothing it is a marker for setting TypeScript properties before our import that depends on them. -->
155144
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="False" />

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ please go to `node_modules\dynamics-web-api` of your application and remove `.gi
6868
* [Formatted Values and Lookup Properties](#formatted-values-and-lookup-properties)
6969
* [Using Alternate Keys](#using-alternate-keys)
7070
* [Making requests using Entity Logical Names](#making-requests-using-entity-logical-names)
71+
* [Using TypeScript Declaration Files](#using-typescript-declaration-files)
7172
* [JavaScript Promises](#javascript-promises)
7273
* [JavaScript Callbacks](#javascript-callbacks)
7374
* [Contributions](#contributions)
@@ -1852,6 +1853,49 @@ var collectionName = dynamicsWebApi.utility.getCollectionName('account');
18521853

18531854
Please note, everything said above will happen only if you set `useEntityNames: true` in the DynamicsWebApi config.
18541855

1856+
## Using TypeScript Declaration Files
1857+
1858+
TypeScript declaration files `d.ts` added with v.1.5.3.
1859+
If you are not familiar with declaration files, these files are used to provide TypeScript type information about an API that's written in JavaScript.
1860+
You want to consume those from your TypeScript code. [Quote](https://stackoverflow.com/a/21247316/2042071)
1861+
1862+
At this moment, DynamicsWebApi declaration files do not exist in a [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) repository but it is in my plans to upload them there.
1863+
1864+
### Node.Js
1865+
1866+
If you are using Node.Js with TypeScript, declarations will be fetched with an NPM package during an installation or an update process.
1867+
At the top of a necessary `.ts` file add the following:
1868+
1869+
```ts
1870+
import * as DynamicsWebApi from "dynamics-web-api";
1871+
//for CommonJS:
1872+
//import DynamicsWebApi = require("dynamics-web-api");
1873+
```
1874+
1875+
### Dynamics 365 web resource
1876+
If you are developing CRM Web Resources with TypeScript, you will need to download a necessary `d.ts` file manually from the following folder: [types](/types/).
1877+
As you may have noticed `types` folder contains two declaration files: `dynamics-web-api.d.ts` (Promises) and `dynamics-web-api-callbacks.d.ts` (Callbacks) - download the one that you need.
1878+
**Do not download both files! Otherwise you will have type declaration conflicts.**
1879+
In my web resources project I usually put a declaration file under "./types/" folder. For example:
1880+
1881+
```
1882+
[project root]/
1883+
-- src/
1884+
-- form_web_resource.ts
1885+
-- types/
1886+
-- dynamics-web-api/
1887+
-- dynamics-web-api-callbacks.d.ts
1888+
-- tsconfig.json
1889+
```
1890+
1891+
**Important!** Make sure that you include `types` folder in your `tsconfig.json`:
1892+
```
1893+
"include": [
1894+
"./src/**/*",
1895+
"./types/**/*"
1896+
]
1897+
```
1898+
18551899
### In Progress
18561900

18571901
- [X] Overloaded functions with rich request options for all Web API operations.
@@ -1866,9 +1910,10 @@ the config option "formatted" will enable developers to retrieve all information
18661910
- [X] Ability to use entity names instead of collection names. `Implemented in v.1.4.0`
18671911
- [X] Entity and Attribute Metadata helpers. `Implemented in v.1.4.3`
18681912
- [X] Entity Relationships and Global Option Sets helpers. `Implemented in v.1.4.6`
1869-
- [X] Batch requests.
1913+
- [X] Batch requests. `Implemented in v.1.5.0`
1914+
- [X] TypeScript declaration files `d.ts` `Added in v.1.5.3`.
1915+
- [ ] Upload DynamicsWebApi declaration files to DefinitelyTyped repository.
18701916
- [ ] Implement `Content-ID` header to reference a created entity in a batch operation.
1871-
- [ ] Intellisense for request objects.
18721917

18731918
Many more features to come!
18741919

dist/dwa.js

Lines changed: 0 additions & 151 deletions
This file was deleted.

dist/dynamics-web-api-callbacks.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ function DynamicsWebApi(config) {
11301130
}
11311131

11321132
/**
1133-
* Sets the configuration parameters for DynamicsWebApi helper.
1133+
* Sets DynamicsWebApi configuration parameters.
11341134
*
11351135
* @param {DWAConfig} config - configuration object
11361136
* @example
@@ -3061,7 +3061,7 @@ module.exports = BatchConverter;
30613061
* Builds parametes for a funciton. Returns '()' (if no parameters) or '([params])?[query]'
30623062
*
30633063
* @param {Object} [parameters] - Function's input parameters. Example: { param1: "test", param2: 3 }.
3064-
* @returns {string}
3064+
* @returns {string} - Function parameter result
30653065
*/
30663066
module.exports = function buildFunctionParameters(parameters) {
30673067
if (parameters) {
@@ -3073,13 +3073,24 @@ module.exports = function buildFunctionParameters(parameters) {
30733073
var parameterName = parameterNames[i - 1];
30743074
var value = parameters[parameterName];
30753075

3076+
if (value === null)
3077+
continue;
3078+
3079+
if (typeof value === "string") {
3080+
value = "'" + value + "'";
3081+
}
3082+
//fix #45
3083+
else if (typeof value === "object") {
3084+
value = JSON.stringify(value);
3085+
}
3086+
30763087
if (i > 1) {
30773088
functionParameters += ",";
30783089
urlQuery += "&";
30793090
}
30803091

30813092
functionParameters += parameterName + "=@p" + i;
3082-
urlQuery += "@p" + i + "=" + ((typeof value == "string") ? "'" + value + "'" : value);
3093+
urlQuery += "@p" + i + "=" + value;
30833094
}
30843095

30853096
return "(" + functionParameters + ")?" + urlQuery;

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,7 @@ module.exports = RequestConverter;
28762876
* Builds parametes for a funciton. Returns '()' (if no parameters) or '([params])?[query]'
28772877
*
28782878
* @param {Object} [parameters] - Function's input parameters. Example: { param1: "test", param2: 3 }.
2879-
* @returns {string}
2879+
* @returns {string} - Function parameter result
28802880
*/
28812881
module.exports = function buildFunctionParameters(parameters) {
28822882
if (parameters) {
@@ -2888,13 +2888,24 @@ module.exports = function buildFunctionParameters(parameters) {
28882888
var parameterName = parameterNames[i - 1];
28892889
var value = parameters[parameterName];
28902890

2891+
if (value === null)
2892+
continue;
2893+
2894+
if (typeof value === "string") {
2895+
value = "'" + value + "'";
2896+
}
2897+
//fix #45
2898+
else if (typeof value === "object") {
2899+
value = JSON.stringify(value);
2900+
}
2901+
28912902
if (i > 1) {
28922903
functionParameters += ",";
28932904
urlQuery += "&";
28942905
}
28952906

28962907
functionParameters += parameterName + "=@p" + i;
2897-
urlQuery += "@p" + i + "=" + ((typeof value == "string") ? "'" + value + "'" : value);
2908+
urlQuery += "@p" + i + "=" + value;
28982909
}
28992910

29002911
return "(" + functionParameters + ")?" + urlQuery;

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.

0 commit comments

Comments
 (0)