Skip to content

Commit

Permalink
feat: Remove GUID class; Export functions only
Browse files Browse the repository at this point in the history
BREAKING CHANGE: GUID class replaced with two functions
  • Loading branch information
derekfinlinson committed May 23, 2019
1 parent cfc9474 commit 66fb684
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 125 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ npm install --save-dev xrm-webapi

#### Browser
```typescript
import { Guid, retrieve, WebApiConfig } from "xrm-webapi";
import { parseGuid, retrieve, WebApiConfig } from "xrm-webapi";

const config = new WebApiConfig("8.2");

const account = await retrieve(config, "accounts", new Guid(""), "$select=name");
const account = await retrieve(config, "accounts", parseGuid("00000000-0000-0000-0000-000000000000"), "$select=name");

console.log(account.name);
```

#### Node
```typescript
import { Guid, retrieve, WebApiConfig } from "xrm-webapi/dist/xrm-webapi-node";
import { parseGuid, retrieve, WebApiConfig } from "xrm-webapi/dist/xrm-webapi-node";

const config = new WebApiConfig("8.2");

const account = await retrieve(config, "accounts", new Guid(""), "$select=name");
const account = await retrieve(config, "accounts", parseGuid("00000000-0000-0000-0000-000000000000"), "$select=name");

console.log(account.name);
```
Expand All @@ -46,11 +46,11 @@ For use in Angular applications, I'd first recommend using their built in [HttpC
pretty simple to construct. If you do want to use this library, the usage is the same as the browser usage:

```typescript
import { Guid, retrieveNode, WebApiConfig } from "xrm-webapi";
import { parseGuid, retrieveNode, WebApiConfig } from "xrm-webapi";

const config = new WebApiConfig("8.2");

const account = await retrieve(config, "accounts", new Guid(""), "$select=name");
const account = await retrieve(config, "accounts", parseGuid("00000000-0000-0000-0000-000000000000"), "$select=name");

console.log(account.name);
```
Expand Down
89 changes: 45 additions & 44 deletions samples/xrm-webapi-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,67 @@
// tslint:disable:no-empty

import {
associate,
batchOperation,
boundAction,
ChangeSet,
FunctionInput,
Guid,
create,
createWithReturnData,
retrieve,
retrieveMultiple,
update,
updateProperty,
deleteProperty,
deleteRecord,
disassociate,
associate,
boundAction,
FunctionInput,
parseGuid,
retrieve,
retrieveMultiple,
retrieveMultipleNextPage,
unboundAction,
batchOperation,
WebApiConfig,
retrieveMultipleNextPage
} from "../src/xrm-webapi";
update,
updateProperty,
WebApiConfig
} from '../src/xrm-webapi';

const config: WebApiConfig = new WebApiConfig("8.1");
const config: WebApiConfig = new WebApiConfig('8.1');

// demonstrate create
const account: any = {
name: "Test Account"
name: 'Test Account'
};

create(config, "accounts", account)
create(config, 'accounts', account)
.then(() => {
console.log();
}, (error) => {
console.log(error);
});

// demonstrate create with returned odata
createWithReturnData(config, "accounts", account, "$select=name,accountid")
createWithReturnData(config, 'accounts', account, '$select=name,accountid')
.then((created: any) => {
console.log(created.name);
});

// demonstrate retrieve
retrieve(config, "accounts", new Guid(""), "$select=name")
retrieve(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), '$select=name')
.then((retrieved) => {
console.log(retrieved.data.name);
}, (error) => {
console.log(error);
});

// demonstrate retrieve multiple
const options: string = "$filter=name eq 'Test Account'&$select=name,accountid";
const options: string = '$filter=name eq \'Test Account\'&$select=name,accountid';

retrieveMultiple(config, "accounts", options)
retrieveMultiple(config, 'accounts', options)
.then(
(results) => {
const accounts: any[] = [];
for (let record of results.value) {
for (const record of results.value) {
accounts.push(record);
}

// demonstrate getting next page from retreiveMultiple
retrieveMultipleNextPage(config, results["@odata.nextlink"]).then(
retrieveMultipleNextPage(config, results['@odata.nextlink']).then(
(moreResults) => {
console.log(moreResults.value.length);
}
Expand All @@ -76,61 +76,62 @@ retrieveMultiple(config, "accounts", options)
);

// demonstrate update. Update returns no content
update(config, "accounts", new Guid(""), account)
update(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), account)
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate update property. Update property returns no content
updateProperty(config, "accounts", new Guid(""), "name", "Updated Account")
updateProperty(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'name', 'Updated Account')
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate delete. Delete returns no content
deleteRecord(config, "accounts", new Guid(""))
deleteRecord(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'))
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate delete property. Delete property returns no content
deleteProperty(config, "accounts", new Guid(""), "address1_line1")
deleteProperty(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'address1_line1')
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate delete navigation property. Delete property returns no content
deleteProperty(config, "accounts", new Guid(""), "primarycontactid")
deleteProperty(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'primarycontactid')
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate associate. Associate returns no content
associate(config, "accounts", new Guid(""), "contact_customer_accounts", "contacts", new Guid(""))
associate(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'),
'contact_customer_accounts', 'contacts', parseGuid('00000000-0000-0000-0000-000000000000'))
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate disassociate. Disassociate returns no content
disassociate(config, "accounts", new Guid(""), "contact_customer_accounts")
disassociate(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'contact_customer_accounts')
.then(() => {}, (error) => {
console.log(error);
});

// demonstrate bound action
const inputs: object = {
NumberInput: 100,
StringInput: "Text",
StringInput: 'Text',
};

boundAction(config, "accounts", new Guid(""), "sample_BoundAction", inputs)
boundAction(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'sample_BoundAction', inputs)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
console.log(error);
});

unboundAction(config, "sample_UnboundAction", inputs)
unboundAction(config, 'sample_UnboundAction', inputs)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
Expand All @@ -141,11 +142,11 @@ unboundAction(config, "sample_UnboundAction", inputs)
const inputs3: FunctionInput[] = [];

inputs3.push({
name: "Argument",
value: "Value",
name: 'Argument',
value: 'Value',
});

boundAction(config, "accounts", new Guid(""), "sample_BoundFunction", inputs3)
boundAction(config, 'accounts', parseGuid('00000000-0000-0000-0000-000000000000'), 'sample_BoundFunction', inputs3)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
Expand All @@ -156,12 +157,12 @@ boundAction(config, "accounts", new Guid(""), "sample_BoundFunction", inputs3)
const inputs4: FunctionInput[] = [];

inputs4.push({
alias: "tid",
name: "Target",
value: "{'@odata.id':'accounts(87989176-0887-45D1-93DA-4D5F228C10E6)'}",
alias: 'tid',
name: 'Target',
value: '{\'@odata.id\':\'accounts(87989176-0887-45D1-93DA-4D5F228C10E6)\'}',
});

unboundAction(config, "sample_UnboundAction", inputs4)
unboundAction(config, 'sample_UnboundAction', inputs4)
.then((result: any) => {
console.log(result.annotationid);
}, (error) => {
Expand All @@ -172,23 +173,23 @@ unboundAction(config, "sample_UnboundAction", inputs4)
const changeSets: ChangeSet[] = [
{
entity: {
name: "Test 1"
name: 'Test 1'
},
queryString: "accounts",
queryString: 'accounts',
},
{
entity: {
name: "Test 2"
name: 'Test 2'
},
queryString: "accounts",
queryString: 'accounts',
},
];

const gets: string[] = [
"accounts?$select=name",
'accounts?$select=name',
];

batchOperation(config, "BATCH123", "CHANGESET123", changeSets, gets)
batchOperation(config, 'BATCH123', 'CHANGESET123', changeSets, gets)
.then((result) => {
console.log(result);
}, (error) => {
Expand Down
43 changes: 25 additions & 18 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class WebApiConfig {
* Constructor
* @param config WebApiConfig
*/
constructor(version: string, accessToken?: string, url?: string ) {
constructor(version: string, accessToken?: string, url?: string) {
// If URL not provided, get it from Xrm.Context
if (url == null) {
const context: Xrm.Context =
Expand Down Expand Up @@ -40,34 +40,42 @@ export interface WebApiRequestConfig {
queryOptions?: QueryOptions;
}

export class Guid {
public value: string;
/**
* Parse GUID by removing curly braces and converting to uppercase
* @param id GUID to parse
*/
export function parseGuid(id: string): string {
id = id.replace(/[{}]/g, '');

constructor(value: string) {
value = value.replace(/[{}]/g, '');

if (/^[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}$/.test(value)) {
this.value = value.toUpperCase();
} else {
throw Error(`Id ${value} is not a valid GUID`);
}
if (/^[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}$/.test(id)) {
return id.toUpperCase();
} else {
throw Error(`Id ${id} is not a valid GUID`);
}
}

public areEqual(compare: Guid): boolean {
if (this === null || compare === null || this === undefined || compare === undefined) {
return false;
}
/**
* Check if two GUIDs are equal
* @param id1 GUID 1
* @param id2 GUID 2
*/
export function areGuidsEqual(id1: string, id2: string): boolean {
id1 = parseGuid(id1);
id2 = parseGuid(id2);

return this.value.toLowerCase() === compare.value.toLowerCase();
if (id1 === null || id2 === null || id1 === undefined || id2 === undefined) {
return false;
}

return id1.toLowerCase() === id2.toLowerCase();
}

export interface QueryOptions {
includeFormattedValues?: boolean;
includeLookupLogicalNames?: boolean;
includeAssociatedNavigationProperties?: boolean;
maxPageSize?: number;
impersonateUser?: Guid;
impersonateUserId?: string;
representation?: boolean;
}

Expand All @@ -79,7 +87,6 @@ export interface RetrieveMultipleResponse {
value: Entity[];
'@odata.nextlink': string;
}

export interface ChangeSet {
queryString: string;
entity: object;
Expand Down

0 comments on commit 66fb684

Please sign in to comment.