Skip to content

Commit a9df87a

Browse files
optimizations and refactoring; moving js tests to ts
1 parent 5ce02d3 commit a9df87a

File tree

11 files changed

+613
-487
lines changed

11 files changed

+613
-487
lines changed

.github/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ data | `Object` or `ArrayBuffer` / `Buffer` (for node.js) | `create`, `update`,
358358
duplicateDetection | `boolean` | `create`, `update`, `upsert` | **D365 Web API v9+** Boolean that enables duplicate detection. [More Info](https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/update-delete-entities-using-web-api#check-for-duplicate-records)
359359
expand | `Expand[]` | `retrieve`, `retrieveMultiple`, `create`, `update`, `upsert` | 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.
360360
fetchXml | `string` | `fetch`, `fetchAll` | Property that sets FetchXML - a proprietary query language that provides capabilities to perform aggregation.
361-
fieldName | `string` | `uploadFile`, `downloadFile`, `deleteRequest` | **D365 Web API v9.1+** Use this option to specify the name of the file attribute in Dynamics 365. [More Info](https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/file-attributes)
361+
fieldName | `string` | `uploadFile`, `downloadFile`, `deleteRecord` | **D365 Web API v9.1+. Deprecated, use `property` instead** Use this option to specify the name of the file attribute in Dynamics 365. [More Info](https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/file-attributes)
362362
fileName | `string` | `uploadFile` | **D365 Web API v9.1+** Specifies the name of the file
363363
filter | String | `retrieve`, `retrieveMultiple`, `retrieveAll`, `callFunction` | Use the $filter system query option to set criteria for which entities will be returned.
364364
functionName | `string` | `callFunction` | **Deprecated from v2.1.3** Use `name` instead. Name of a D365 Web Api function.
@@ -382,6 +382,7 @@ pageNumber | `number` | `fetch` | Sets a page number for Fetch XML request ONLY!
382382
pagingCookie | `string` | `fetch` | Sets a paging cookie for Fetch XML request ONLY!
383383
parameters | `Object` | `callFunction` | Function's input parameters. Example: `{ param1: "test", param2: 3 }`.
384384
partitionId | `string` | `create`, `update`, `upsert`, `delete`, `retrieve`, `retrieveMultiple` | Sets a unique partition key value of a logical partition for non-relational custom entity data stored in NoSql tables of Azure heterogenous storage. [More Info](https://docs.microsoft.com/en-us/power-apps/developer/data-platform/webapi/azure-storage-partitioning)
385+
property | `string` | `uploadFile`, `downloadFile`, `deleteRecord` | `v2.1.6+` **D365 Web API v9.1+.** Use this option to specify the name of the column or of the file attribute in Dynamics 365. [More Info](https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/file-attributes)
385386
queryParams | `string[]` | All | Custom query parameters. Can also be used to set the [parameter aliases](https://docs.microsoft.com/en-us/power-apps/developer/data-platform/webapi/query-data-web-api#use-parameter-aliases-with-system-query-options) for "$filter" and "$orderBy". **Important!** These parameters ARE NOT URI encoded!
386387
returnRepresentation | `boolean` | `create`, `update`, `upsert` | Sets Prefer header request with value "return=representation". Use this property to return just created or updated entity in a single request.
387388
savedQuery | `string` | `retrieve` | A String representing the GUID value of the saved query.
@@ -542,7 +543,7 @@ else{
542543
const request: DynamicsWebApi.DeleteRequest = {
543544
key: leadId,
544545
collection: "leads",
545-
fieldName: "subject"
546+
property: "subject"
546547
}
547548

548549
await dynamicsWebApi.deleteRecord(request);
@@ -2130,7 +2131,7 @@ fr.onload = function(){
21302131
dynamicsWebApi.uploadFile({
21312132
collection: "dwa_filestorages",
21322133
key: "00000000-0000-0000-0000-000000000001",
2133-
fieldName: "dwa_file",
2134+
property: "dwa_file",
21342135
fileName: fileName,
21352136
data: fileData
21362137
}).then(function(){
@@ -2151,7 +2152,7 @@ fs.readFile(filename, (err, data) => {
21512152
dynamicsWebApi.uploadFile({
21522153
collection: "dwa_filestorages",
21532154
key: "00000000-0000-0000-0000-000000000001",
2154-
fieldName: "dwa_file",
2155+
property: "dwa_file",
21552156
fileName: filename
21562157
data: data,
21572158
}).then(function() {
@@ -2168,7 +2169,7 @@ fs.readFile(filename, (err, data) => {
21682169
const donwloadResponse = await dynamicsWebApi.downloadFile({
21692170
collection: "dwa_filestorages",
21702171
key: "00000000-0000-0000-0000-000000000001",
2171-
fieldName: "dwa_file"
2172+
property: "dwa_file"
21722173
});
21732174

21742175
//Uint8Array for browser and Buffer for Node.js
@@ -2183,7 +2184,7 @@ const fileSize = donwloadResponse.fileSize;
21832184
const isDeleted = await dynamicsWebApi.deleteRecord({
21842185
collection: "dwa_filestorages",
21852186
key: "00000000-0000-0000-0000-000000000001",
2186-
fieldName: "dwa_file"
2187+
property: "dwa_file"
21872188
});
21882189
```
21892190

src/dynamics-web-api.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ export interface CreateRequest<T = any> extends CRUDRequest {
12581258
expand?: Expand[];
12591259
/**Sets Prefer header with value "odata.include-annotations=" and the specified annotation.Annotations provide additional information about lookups, options sets and other complex attribute types. */
12601260
includeAnnotations?: string;
1261-
/**A String representing the name of a single - valued navigation property.Useful when needed to retrieve information about a related record in a single request. */
1261+
/**A String representing the name of a single - valued navigation property. Useful when needed to retrieve information about a related record in a single request. */
12621262
navigationProperty?: string;
12631263
/**A String representing navigation property's Primary Key (GUID) or Alternate Key(s). (For example, to retrieve Attribute Metadata). */
12641264
navigationPropertyKey?: string;
@@ -1293,7 +1293,7 @@ export interface UpdateRequestBase<T = any> extends CRUDRequest {
12931293
contentId?: string;
12941294
/**Casts the AttributeMetadata to a specific type. (Used in requests to Attribute Metadata). */
12951295
metadataAttributeType?: string;
1296-
/**A String representing the name of a single - valued navigation property.Useful when needed to retrieve information about a related record in a single request. */
1296+
/**A String representing the name of a single - valued navigation property. Useful when needed to retrieve information about a related record in a single request. */
12971297
navigationProperty?: string;
12981298
/**A String representing navigation property's Primary Key (GUID) or Alternate Key(s). (For example, to retrieve Attribute Metadata). */
12991299
navigationPropertyKey?: string;
@@ -1335,8 +1335,13 @@ export interface DeleteRequest extends CRUDRequest {
13351335
ifmatch?: string;
13361336
/**BATCH REQUESTS ONLY! Sets Content-ID header or references request in a Change Set. */
13371337
contentId?: string;
1338-
/**Field name that needs to be cleared (for example File Field) */
1338+
/**
1339+
* Field name that needs to be cleared (for example File Field)
1340+
* @deprecated Use "property".
1341+
*/
13391342
fieldName?: string;
1343+
/**Single property that needs to be cleared (including the File property) */
1344+
property?: string;
13401345
}
13411346

13421347
export interface RetrieveRequest extends CRUDRequest {
@@ -1352,7 +1357,7 @@ export interface RetrieveRequest extends CRUDRequest {
13521357
includeAnnotations?: string;
13531358
/**Casts the AttributeMetadata to a specific type. (Used in requests to Attribute Metadata). */
13541359
metadataAttributeType?: string;
1355-
/**A String representing the name of a single - valued navigation property.Useful when needed to retrieve information about a related record in a single request. */
1360+
/**A String representing the name of a single - valued navigation property. Useful when needed to retrieve information about a related record in a single request. */
13561361
navigationProperty?: string;
13571362
/**A String representing navigation property's Primary Key (GUID) or Alternate Key(s). (For example, to retrieve Attribute Metadata). */
13581363
navigationPropertyKey?: string;
@@ -1619,12 +1624,22 @@ export interface UploadRequest extends CRUDRequest {
16191624
data: Uint8Array | Buffer;
16201625
/**Name of the file */
16211626
fileName: string;
1622-
/**File Field Name */
1627+
/**The name of File Column (field) */
1628+
property?: string;
1629+
/**
1630+
* File Field Name
1631+
* @deprecated Use "property".
1632+
*/
16231633
fieldName: string;
16241634
}
16251635

16261636
export interface DownloadRequest extends CRUDRequest {
1627-
/**File Field Name */
1637+
/**The name of File Column (field) */
1638+
property?: string;
1639+
/**
1640+
* File Field Name
1641+
* @deprecated Use "property".
1642+
*/
16281643
fieldName: string;
16291644
}
16301645

src/helpers/Regex.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1-
const uuid = "[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}";
1+
const uuid = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
2+
3+
const uuidRegExp = new RegExp(uuid, "i");
4+
const extractUuidRegExp = new RegExp("^{?(" + uuid + ")}?$", "i");
5+
const extractUuidFromUrlRegExp = new RegExp("(" + uuid + ")\\)$", "i");
6+
//global here is fine because the state is reset inside string.replace function
7+
const removeBracketsFromGuidReg = new RegExp(`{(${uuid})}`, "g");
28

39
export function isUuid(value: string): boolean {
4-
const match = new RegExp(uuid, "i").exec(value);
10+
const match = uuidRegExp.exec(value);
511
return !!match;
612
}
713

814
export function extractUuid(value: string): string | null {
9-
const match = new RegExp("^{?(" + uuid + ")}?$", "i").exec(value);
15+
const match = extractUuidRegExp.exec(value);
1016
return match ? match[1] : null;
1117
}
1218

1319
export function extractUuidFromUrl(url: string): string | null {
14-
const match = new RegExp("(" + uuid + ")\\)$", "i").exec(url);
20+
const match = extractUuidFromUrlRegExp.exec(url);
1521
return match ? match[1] : null;
1622
}
23+
24+
export function removeCurlyBracketsFromUuid(value: string): string {
25+
return value.replace(removeBracketsFromGuidReg, (_match, p1) => p1);
26+
}
27+
28+
export function safelyRemoveCurlyBracketsFromUrl(url: string): string {
29+
//todo: in future I will need to replace this with a negative lookbehind and lookahead
30+
31+
// Split the filter string by quotation marks
32+
const parts = url.split(/(["'].*?["'])/);
33+
return parts
34+
.map((part, index) => {
35+
// Only process parts that are not within quotes
36+
if (index % 2 === 0) {
37+
return removeCurlyBracketsFromUuid(part);
38+
}
39+
return part;
40+
})
41+
.join("");
42+
}

src/types.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export interface InternalRequest {
7171
/**Sets If-Match header value that enables to use conditional retrieval or optimistic concurrency in applicable requests.*/
7272
ifmatch?: string;
7373
/**Sets Prefer header with value "odata.include-annotations=" and the specified annotation.Annotations provide additional information about lookups, options sets and other complex attribute types. */
74-
includeAnnotations?: string;
74+
includeAnnotations?: string | null;
7575
/**Sets Prefer header request with value "return=representation".Use this property to return just created or updated entity in a single request. */
76-
returnRepresentation?: boolean;
76+
returnRepresentation?: boolean | null;
7777
/**Prefer header values */
7878
prefer?: string | string[];
7979
/**An Array(of Strings) representing the $select OData System Query Option to control which attributes will be returned. */
@@ -95,21 +95,24 @@ export interface InternalRequest {
9595
/**Boolean that sets the $count system query option with a value of true to include a count of entities that match the filter criteria up to 5000(per page).Do not use $top with $count! */
9696
count?: boolean;
9797
/**Sets the odata.maxpagesize preference value to request the number of entities returned in the response. */
98-
maxPageSize?: number;
98+
maxPageSize?: number | null;
9999
/**An Array(of string) representing the order in which 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. */
100100
orderBy?: string[];
101101
/**Limit the number of results returned by using the $top system query option.Do not use $top with $count! */
102102
top?: number;
103103
/**Sets Prefer header with value 'odata.track-changes' to request that a delta link be returned which can subsequently be used to retrieve entity changes. */
104104
trackChanges?: boolean;
105-
/**v.1.7.0+ Web API v9.1+ only! Use this option to specify the name of the file attribute in Dynamics 365. */
105+
/**v.1.7.0+ Web API v9.1+ only! Use this option to specify the name of the file attribute in Dynamics 365.
106+
* @deprecated use property
107+
*/
106108
fieldName?: string;
107109
/**v.1.7.0+ Web API v9.1+ only! Specifies the name of the file */
108110
fileName?: string;
109111
/**v.1.7.7+ A unique partition key value of a logical partition for non-relational custom entity data stored in NoSql tables of Azure heterogenous storage. */
110112
partitionId?: string;
111113
/**v.1.7.7+ Additional query parameters that either have not been implemented yet or they are parameter aliases for "$filter" and "$orderBy". Important! These parameters ARE NOT URI encoded! */
112114
queryParams?: string[];
115+
property?: string;
113116
contentRange?: string;
114117
url?: string;
115118
parameters?: Record<string, any>;

0 commit comments

Comments
 (0)