@mindline/sync 1.0.25 → 1.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/hybridspa.ts CHANGED
@@ -1,5 +1,8 @@
1
- import { IPublicClientApplication, AuthenticationResult } from '@azure/msal-browser';
2
- import { User } from '@mindline/sync';
1
+ import {
2
+ IPublicClientApplication,
3
+ AuthenticationResult,
4
+ } from "@azure/msal-browser";
5
+ import { User } from "@mindline/sync";
3
6
 
4
7
  // Add here the endpoints for MS Graph API services you would like to use.
5
8
  const graphConfig = {
@@ -25,9 +28,9 @@ function updateUI(data, endpoint) {
25
28
  data.value.map((d, i) => {
26
29
  // Keeping it simple
27
30
  if (i < 10) {
28
- console.log(data.officeLocation);
29
- console.log(d.subject);
30
- console.log(d.from.emailAddress.address);
31
+ console.log(data.officeLocation);
32
+ console.log(d.subject);
33
+ console.log(d.from.emailAddress.address);
31
34
  }
32
35
  });
33
36
  }
@@ -74,7 +77,7 @@ function getTokenByCode(spaCode: string, instance: IPublicClientApplication) {
74
77
  }
75
78
 
76
79
  //See Profile
77
- function seeProfile(spaCode: string, instance: IPublicClientApplication) {
80
+ function seeProfile(spaCode: string, instance: IPublicClientApplication) {
78
81
  getTokenByCode(spaCode, instance)
79
82
  .then((response) => {
80
83
  callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, updateUI);
@@ -85,39 +88,49 @@ function seeProfile(spaCode: string, instance: IPublicClientApplication) {
85
88
  }
86
89
 
87
90
  //Get Tenant Info
88
- export function getTenantInfo(user: User, instance: IPublicClientApplication) {
91
+ export async function getTenantInfo(user: User, instance: IPublicClientApplication) {
89
92
  debugger;
90
93
 
91
- instance
92
- .acquireTokenByCode({ code: user.spacode })
93
- .then((result) => {
94
- // prepare access token in Authorization headers as part of options
95
- const headers = new Headers();
96
- const bearer = `Bearer ${result.accessToken}`;
97
- headers.append("Authorization", bearer);
98
- const options = {
99
- method: "GET",
100
- headers: headers,
101
- };
102
- // prepare tenant info endpoint
103
- var tenantEndpoint = graphConfig.graphTenantEndpoint;
104
- tenantEndpoint += "(tenantId='";
105
- tenantEndpoint += user.tid;
106
- tenantEndpoint += "')";
107
- console.log(
108
- "request made to Graph API tenant endpoint at: " + new Date().toString()
109
- );
110
- // make tenant endpoint call
111
- fetch(tenantEndpoint, options)
112
- .then((response) => response.json())
113
- .then((body) => {
114
- console.log("Successfully Fetched Data from Graph API:", body);
115
- user.companyName = body.displayName;
116
- user.companyDomain = body.defaultDomainName;
117
- })
118
- .catch((error) => console.log(error));
119
- })
120
- .catch((e: any) => {
121
- console.log(e);
122
- });
123
- }
94
+ // do we already have it?
95
+ if(user.companyName != "" && user.companyDomain != "") return;
96
+
97
+ // create tenant info endpoint
98
+ var tenantEndpoint = graphConfig.graphTenantEndpoint;
99
+ tenantEndpoint += "(tenantId='";
100
+ tenantEndpoint += user.tid;
101
+ tenantEndpoint += "')";
102
+ console.log("Time: " + new Date().toString());
103
+ console.log("Tenant Info Endpoint: " + tenantEndpoint);
104
+
105
+ // if needed, retrieve and cache access token
106
+ if (user.accessToken === "") {
107
+ try {
108
+ let response = await instance.acquireTokenByCode({ code: user.spacode });
109
+ user.accessToken = response.accessToken;
110
+ }
111
+ catch(error: any) {
112
+ console.log(error);
113
+ }
114
+ }
115
+
116
+ // prepare Authorization headers as part of options
117
+ const headers = new Headers();
118
+ const bearer = `Bearer ${user.accessToken}`;
119
+ headers.append("Authorization", bearer);
120
+ const options = { method: "GET", headers: headers };
121
+
122
+ // make tenant endpoint call
123
+ try {
124
+ let response = await fetch(tenantEndpoint, options);
125
+ let data = await response.json();
126
+ if(data.displayName !== undefined)
127
+ {
128
+ user.companyName = data.displayName;
129
+ user.companyDomain = data.defaultDomainName;
130
+ }
131
+ console.log("Fetched Data from Graph API:", data);
132
+ }
133
+ catch(error: any) {
134
+ console.log(error);
135
+ }
136
+ }
package/index.d.ts CHANGED
@@ -17,6 +17,7 @@ declare module "@mindline/sync" {
17
17
  workspaceIDs: string;
18
18
  session: string;
19
19
  spacode: string;
20
+ accessToken: string;
20
21
  }
21
22
 
22
23
  // target (Azure AD tenant, AD domain, Google workspace)
package/index.test.ts CHANGED
@@ -7,6 +7,6 @@ test("adds 1 + 2 to equal 3", () => {
7
7
  });
8
8
  test("loads config based on a user and expects function to return true", () => {
9
9
  let ii = new InitInfo();
10
- let bResult:boolean = InitGet(ii, stubbedPublicClientApplication);
10
+ let bResult:boolean = InitGet(ii, stubbedPublicClientApplication, true);
11
11
  expect(bResult);
12
12
  });
package/index.ts CHANGED
@@ -30,6 +30,7 @@ class User {
30
30
  workspaceIDs: string;
31
31
  session: string;
32
32
  spacode: string;
33
+ accessToken: string;
33
34
  constructor() {
34
35
  this.oid = "";
35
36
  this.name = "";
@@ -42,6 +43,7 @@ class User {
42
43
  this.workspaceIDs = "";
43
44
  this.session = "";
44
45
  this.spacode = "";
46
+ this.accessToken = "";
45
47
  }
46
48
  }
47
49
 
@@ -214,7 +216,7 @@ export function InitGet(ii: InitInfo, instance: IPublicClientApplication, debug:
214
216
  let user:User = ii.us[l-1];
215
217
  if(user.oid === "1") return true;
216
218
 
217
- // have real user: remove any dummy user, target, config, workspace if they exist
219
+ // we have a real user: remove existing dummy user, target, config, workspace
218
220
  let dummyIndex = ii.us.findIndex((u) => u.oid === "1");
219
221
  if(dummyIndex!==-1) ii.us.splice(dummyIndex, 1);
220
222
  dummyIndex = ii.ts.findIndex((u) => u.tid === "1");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mindline/sync",
3
3
  "type": "module",
4
- "version": "1.0.25",
4
+ "version": "1.0.26",
5
5
  "types": "index.d.ts",
6
6
  "exports": "./index.ts",
7
7
  "description": "sync is a node.js package encapsulating javscript classes required for configuring Mindline sync service.",