Skip to content

Commit 858b39b

Browse files
fixing #153; moving breaking changes and a list of new features to md files; removing unused code; adding more unit tests; adding v1 github action to test the package
1 parent a0f16a2 commit 858b39b

18 files changed

+832
-2307
lines changed

.github/BREAKING_CHANGES_V2.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Breaking Changes in v2 (v1 -> v2)
2+
3+
### 1. DynamicsWebApi Callbacks is fully removed
4+
All modern browsers support Promises (including await/async). The main reason why the Callbacks version was added in the first place was Internet Explorer which has been replaced with Edge and is neither maintained or supported by Microsoft anymore.
5+
6+
### 2. Configuration object changes
7+
`webApiUrl` and `webApiVersion` have been removed and replaced with `serverUrl`, `dataApi` and `searchApi`. [#139](https://github.com/AleksandrRogov/DynamicsWebApi/issues/139)
8+
9+
**Node.JS**
10+
**Before**
11+
```js
12+
new DynamicsWebApi({
13+
webApiUrl: "https://<YOUR ORG HERE>.api.crm.dynamics.com/api/data/v9.1/"
14+
});
15+
```
16+
**Now**
17+
```js
18+
new DynamicsWebApi({
19+
serverUrl: "https://<YOUR ORG HERE>.api.crm.dynamics.com",
20+
dataApi: {
21+
version: "9.1"
22+
}
23+
});
24+
```
25+
**Web Resource**
26+
**Before**
27+
```js
28+
new DynamicsWebApi({
29+
webApiVersion: "9.1"
30+
});
31+
```
32+
**Now**
33+
```js
34+
new DynamicsWebApi({
35+
dataApi: {
36+
version: "9.1"
37+
}
38+
});
39+
```
40+
41+
### 3. All request functions with multiple parameters are removed.
42+
No more "simple" functions with multiple parameters. This has been done to streamline the request parameters and not introduce breaking changes when new parameters have been added. Furthermore, now all functions have access to impersonation, cache and data tracking functionality.
43+
44+
### 4. Changes to the names of some request functions and properties
45+
46+
| Old Name | New Name |
47+
|--------|--------|
48+
| createRequest | create |
49+
| updateRequest | update |
50+
| upsertRequest | upsert |
51+
| deleteRequest | deleteRecord |
52+
| retrieveRequest | retrieve |
53+
| retrieveMultipleRequest | retrieveMultiple |
54+
| retrieveAllRequest | retrieveAll |
55+
| executeFetchXml | fetch |
56+
| executeFetchXmlAll | fetchAll |
57+
| executeBoundFunction | callFunction |
58+
| executeUnboundFunction | callFunction |
59+
| executeBoundAction | callAction |
60+
| executeUnboundAction | callAction |
61+
| utility | Utility |
62+
63+
### 5. In some requests `entity` property is replaced with `data`
64+
Affected requests: `createRequest`, `updateRequest` and `upsertRequest` and their corresponding new names: `create`, `update` and `upsert`.
65+
66+
Example:
67+
```js
68+
const lead = {
69+
subject: "Test WebAPI",
70+
firstname: "Test",
71+
lastname: "WebAPI",
72+
jobtitle: "Title"
73+
};
74+
75+
const result = await dynamicsWebApi.create({
76+
collection: "leads",
77+
data: lead,
78+
returnRepresentation: true
79+
});
80+
```
81+
82+
### 6. `config.onTokenRefresh` is now a promise-based function
83+
Usage example:
84+
```ts
85+
const cca = new MSAL.ConfidentialClientApplication(msalConfig);
86+
const serverUrl = 'https://<YOUR ORG HERE>.api.crm.dynamics.com';
87+
88+
//function calls an external functionality that acquires a token and passes it to DynamicsWebApi
89+
const acquireToken = async () => {
90+
try {
91+
return await cca.acquireTokenByClientCredential({
92+
scopes: [`${serverUrl}/.default`],
93+
});
94+
}
95+
catch (error) {
96+
//error logging here
97+
//or a fallback authentication
98+
99+
//to abort a request just return null
100+
//or re-throw an error
101+
return null;
102+
}
103+
}
104+
105+
const dynamicsWebApi = new DynamicsWebApi({
106+
serverUrl: serverUrl,
107+
onTokenRefresh: acquireToken
108+
});
109+
```
110+
111+
### 7. Removed `id` property from the request object.
112+
`id` has been deprecated for quite some time and got removed in v2. Use `key` instead.
113+
114+
### 8. Default version of Dataverse API is set to `9.2`
115+
In case you still need version `8.0`, please set `dataApi.version` to `8.0`.
116+
117+
### 9. Supported minimum version of Node.js raised to v.15.0.0.
118+
It was time to finally bump up the JavaScript specification from ES5 to ES2020 for the project. It is widely supported in all modern browsers and all currently maintained versions of Node. Even though the minimum Node.js version that supports ES2020 is 14.5.0, I would recommend running DynamicsWebApi on at least v.15.0.0 for all features to work correctly. In all versions prior to 15 the `AbortSignal` functionality won't work because it did not exist there yet.

.github/NEW_IN_V2.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# What's New in Version 2
2+
3+
DynamicsWebApi v2 has been fully rewritten in TypeScript. The process of rewriting the code made it possible to refactor and improve many modules of the library and organize them in a more logical way as well as to remove redundant features that were no longer needed. As a result, DynamicsWebApi now has many new features such as:
4+
5+
### Microsoft Dataverse Search API
6+
v2 brings the power of Search, Suggest and Autocomplete capabilities of Microsoft Dataverse Search API.
7+
8+
### `AbortController` and `AbortSignal` support (for Node.js 15+ and Browser)
9+
Each request can now be aborted when it's no longer need to be completed via the `AbortController`.
10+
11+
### All requests support Impersonation, NoCache and other common properties
12+
v1 did not have full support of mentioned properties in some requests. v2 fixes that by making all request parameters implement a single `BaseRequest` interface.
13+
14+
### On demand Changesets in Batch Operations
15+
Control what requests should be included or excluded from the changesets by setting `inChangeSet` parameter to `false` (it's `true` when no set). By default, all requests (except for GET) are included in a changeset. Thus, it does not break the core logic introduced in v1.
16+
17+
### CSDL $metadata document
18+
Retrieve the org's CSDL $metadata document with a single call of `retrieveCsdlMetadata` function. The library returns the raw text and does not parse or process it in any way.
19+
20+
### NPM Package contents
21+
NPM package now includes a pre-bundled code of DynamicsWebApi to simplify a compilation process of the projects that depend on it. There are 4 separate bundles:
22+
- `dist/dynamics-web-api.js` - a Browser ready version (to use as a Dynamics 365 web resource) [IIFE] + it's minified version `.min.js`.
23+
- `dist/cjs/dynamics-web-api.js` - a Node.js module [CommonJS].
24+
- `dist/esm/dynamics-web-api.mjs` - a Node.js module [ESM].
25+
- `dist/browser/esm/dynamics-web-api.js` - an ESM module for a Browser (to use as a Dynamics 365 web resource).
26+
27+
Type definition for the library also moved into the `dist` folder.
28+
29+
Please let me know (create an issue) if we need to add a `cjs` bundle for the Browser as well. I did not have any case where I had to use cjs specifically for the browser but it's just me :blush: and you may have a different case.
30+
31+
### Other changes
32+
Not all changes are visible outside, some changes and fixes I've done were in the core of the library itself. Here are some of them:
33+
- All request objects passed as function parameters are fully cloned and remain untouched. Which means, whatever has been passed into the functions is not getting modified in any way. This was not always true in v1 (and I admit that it was my mistake). It is a positive change because _input parameters should never be changed inside the functions_ (there are exceptions but not in this case).
34+
- All deprecated JavaScript functions used by the library have been replaced with their modern alternatives.
35+
- The core has been _promisified_, except for `xhr` and `http` modules. This makes the overall code cleaner and easier to follow.
36+
- Rewriting to TypeScript gave me a chance to go through the code line by line and identify suboptimal functions/features which were successfully tweaked or removed.
37+
38+
...and other.
39+
40+
### Migration
41+
42+
If you are currently using v1 and planning on migrating to v2 please consult with a list of [breaking changes](/.github/BREAKING_CHANGES_V2.md).

.github/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ As well as Microsoft Dynamics 365 CE (online), Microsoft Dynamics 365 CE (on-pre
1212

1313
### **This documentation is for version 2.x. If you are working with version 1.x, please check [this instead](https://github.com/AleksandrRogov/DynamicsWebApi/tree/v1).**
1414

15-
Please check [DynamicsWebApi Wiki](../../wiki/) where you will find documentation to DynamicsWebApi API and more.
15+
Please check [DynamicsWebApi Wiki](../../../wiki/) where you will find documentation to DynamicsWebApi API and more.
1616

1717
Browser-compiled script and type definitions can be found in a [dist](/dist/) folder.
1818

1919
## Main Features
2020

21-
- **Microsoft Dataverse Search API**. Access full power of its Search, Suggestion and Autocomplete capabilities.
22-
- **Batch Requests**. Convert all requests into a Batch operation with a single line of code.
21+
- **Microsoft Dataverse Search API**. Access the full power of its Search, Suggestion and Autocomplete capabilities.
22+
- **Batch Requests**. Convert all requests into a Batch operation with two lines of code.
2323
- **Simplicity and Automation**. Such as automated paging, big file downloading/uploading in chunks of data, automated conversion of requests with long URLs into a Batch Request in the background and more!
2424
- **CRUD operations**. Including Fetch XML, Actions and Functions in Microsoft Dataverse Web API.
2525
- **Table Definitions (Entity Metadata)**. Query and modify Table, Column, Choice (Option Set) and Relationship definitions.
@@ -46,6 +46,8 @@ Also, please check [suggestions and contributions](#contributions) section to le
4646

4747
## Table of Contents
4848

49+
* v2 breaking changes are [here](/.github/BREAKING_CHANGES_V2.md)
50+
* List of new features in v2 is [here](/.github/NEW_IN_V2.md)
4951
* [Getting Started](#getting-started)
5052
* [Dynamics 365 Web Resource](#dynamics-365-web-resource)
5153
* [Node.js](#nodejs)
@@ -121,7 +123,7 @@ Also, please check [suggestions and contributions](#contributions) section to le
121123
### Dynamics 365 Web Resource
122124
To use DynamicsWebApi inside Dynamics 365 you need to download a browser version of the library, it can be found in [dist](/dist/) folder.
123125

124-
Upload a script as a JavaScript Web Resource, add it to a table form or reference it in your HTML Web Resource and then initialize the main object:
126+
Upload a script as a JavaScript Web Resource, add it to a table form or reference it in the HTML Web Resource and then initialize the main object:
125127

126128
```ts
127129
//By default DynamicsWebApi makes calls to
@@ -192,16 +194,15 @@ const acquireToken = async () => {
192194
}
193195
}
194196

195-
//create DynamicsWebApi
197+
//create DynamicsWebApi;
198+
//By default DynamicsWebApi makes calls to
199+
//Web API v9.2 and Search API v1.0
196200
const dynamicsWebApi = new DynamicsWebApi({
197201
serverUrl: serverUrl,
198-
dataApi: {
199-
version: "9.2"
200-
},
201202
onTokenRefresh: acquireToken
202203
});
203204

204-
try{
205+
try {
205206
//call any function
206207
const response = await dynamicsWebApi.callFunction("WhoAmI");
207208
console.log(`Hello from Dynamics 365! My id is: ${response.UserId}`);
@@ -269,7 +270,7 @@ If you are using `DynamicsWebApi` **outside Microsoft Dynamics 365** and set `us
269270
| path | `string` | A path to API, for example: "data" or "search". Optional. |
270271
| version | `string` | API Version, for example: "1.0" or "9.2". Optional. |
271272

272-
Both `dataApi` and `seatchApi` can be omitted from a configuration. Their default values are:
273+
Both `dataApi` and `searchApi` can be omitted from a configuration. Their default values are:
273274

274275
```js
275276
//dataApi
@@ -293,7 +294,7 @@ Both `dataApi` and `seatchApi` can be omitted from a configuration. Their defaul
293294

294295
## Request Examples
295296

296-
Please use [DynamicsWebApi Wiki](../../wiki/) for an object reference. It is automatically generated and I could not find a better doc generator, pardon me for that. If you know a good ".d.ts -> .md" doc generator - let me know!
297+
Please use [DynamicsWebApi Wiki](./../../wiki/) for an object reference. It is automatically generated and I could not find a better doc generator, pardon me for that. If you know a good ".d.ts -> .md" doc generator - let me know!
297298

298299
The following table describes all __possible__ properties that can be set in `request` object.
299300

@@ -2373,7 +2374,8 @@ If you are developing CRM Web Resources with TypeScript (and are not using NPM),
23732374
The declaration file is an ESM module, so if you are not using any bundler, you will have to add another d.ts file (let's call it `dynamics-web-api.browser.d.ts` and put it in the `types` folder) that will make DynamicsWebApi available globally. Here is an example (the same folder structure as mentioned above):
23742375
23752376
```ts
2376-
//import a class from dynamics-web-api.d.ts file
2377+
//dynamics-web-api.browser.d.ts
2378+
//import a DynamicsWebApi class from dynamics-web-api.d.ts file
23772379
import { DynamicsWebApi } from "./dynamicsWebApi"
23782380
//make the DynamicsWebApi class available globally
23792381
export = DynamicsWebApi;
@@ -2408,7 +2410,7 @@ the config option "formatted" will enable developers to retrieve all information
24082410
- [X] Full proxy support. `Added in v.1.7.2`.
24092411
- [X] Refactoring and conversion to TypeScript - coming with `v.2.0`! Stay tuned!
24102412
- [X] Implement [Dataverse Search API](https://docs.microsoft.com/en-us/power-apps/developer/data-platform/webapi/relevance-search).
2411-
- [ ] Allow custom headers to be passed to the request. #151
2413+
- [ ] Allow custom headers to be passed to the request. [#151](https://github.com/AleksandrRogov/DynamicsWebApi/issues/151)
24122414
24132415
Many more features to come!
24142416
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3+
4+
name: DynamicsWebApi V1 CI
5+
6+
on:
7+
push:
8+
branches: [ "v1" ]
9+
pull_request:
10+
branches: [ "v1" ]
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [14.x, 16.x, 18.x]
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: 'npm'
25+
- run: npm ci
26+
- run: npm run build --if-present
27+
- run: npm test
28+
# todo: add coveralls for v1
29+
# - name: Prepare coverage
30+
# run: npm run test-coverage
31+
# - name: Publish to Coveralls
32+
# if: ${{ matrix.node-version == 16.x }}
33+
# uses: coverallsapp/github-action@master
34+
# with:
35+
# github-token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Browser-compiled script and type definitions can be found in a [dist](/dist/) fo
2626

2727
**Please note!** "Dynamics 365" in this readme refers to Microsoft Dataverse (formerly known as Microsoft Common Data Service) / Microsoft Dynamics 365 Customer Engagement / Micorosft Dynamics CRM. **NOT** Microsoft Dynamics 365 Finance and Operations.
2828

29-
### For a full documentation please check [DynamicsWebApi on GitHub](https://github.com/AleksandrRogov/DynamicsWebApi).
29+
## Usage examples
3030

31-
## Usage samples
31+
### For a full documentation please check [DynamicsWebApi on GitHub](https://github.com/AleksandrRogov/DynamicsWebApi).
3232

3333
### Dynamics 365 Web Resource
3434
To use DynamicsWebApi inside Dynamics 365 you need to download a browser version of the library, it can be found in [dist](/dist/) folder.
@@ -87,6 +87,8 @@ const contactId = responses[0];
8787
const salesorderId = responses[1];
8888
```
8989

90+
### For a full documentation please check [DynamicsWebApi on GitHub](https://github.com/AleksandrRogov/DynamicsWebApi).
91+
9092
## Contributions
9193

9294
First of all, I would like to thank you for using `DynamicsWebApi` library in your Dynamics 365 CE / Common Data Service project, the fact that my project helps someone to achieve their development goals already makes me happy.

0 commit comments

Comments
 (0)