oro-sdk-apis 5.17.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,4 +22,10 @@ export declare class ApisPracticeManager {
22
22
  * @returns a promise holding a `ServiceCollection`
23
23
  */
24
24
  get(practiceUuid?: string): Promise<ServiceCollection>;
25
+ /**
26
+ * Uses the refresh intance to fetch a new auth token for the given practice
27
+ * @param practiceUuid the uuid of the practice or key of a specific instance
28
+ * @returns a new authentication token
29
+ */
30
+ authTokenFunc(practiceUuidOrInstanceName?: string): Promise<AuthTokenResponse>;
25
31
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.17.0",
2
+ "version": "6.0.0",
3
3
  "main": "dist/index.js",
4
4
  "typings": "dist/index.d.ts",
5
5
  "files": [
@@ -19,7 +19,11 @@ export class ApisPracticeManager {
19
19
  private serviceCollReq: ServiceCollectionRequest,
20
20
  private getAuthTokenCbk: (guard: GuardService, practiceUuid?: string) => Promise<AuthTokenResponse>,
21
21
  private useLocalStorage = false
22
- ) {}
22
+ ) {
23
+ // The refreshInstance will be a single instance that is used to refresh the tokens of others this way it will not interfere with requests made by other services
24
+ const newPracticeInstance = init(this.serviceCollReq, undefined, this.useLocalStorage)
25
+ this.practiceInstances.set('refreshInstance', newPracticeInstance)
26
+ }
23
27
 
24
28
  /**
25
29
  * This function is used to get a `ServiceCollection` associated to a practice. If missing, it will initialize a new `ServiceCollection`.
@@ -32,25 +36,25 @@ export class ApisPracticeManager {
32
36
  if (practiceInstance) return practiceInstance
33
37
 
34
38
  const newPracticeInstance = init(this.serviceCollReq, undefined, this.useLocalStorage)
35
-
36
- // Create one auth token callback per practice since the practice uuid needs to change
37
- const authTokenFunc = async () => {
38
- if (newPracticeInstance.guardService) {
39
- console.log(`\x1b[36m[Auth] Refresh auth called (practiceUuid: ${practiceUuid})\x1b[36m`)
40
- return await this.getAuthTokenCbk(newPracticeInstance.guardService, practiceUuid)
41
- } else {
42
- throw Error('[Auth] Unable to refresh token guard service is undefined')
43
- }
44
- }
45
-
46
- // Initialize the M2M token
47
- await authTokenFunc()
48
-
49
- // Set the refresh tokens callback
50
- newPracticeInstance.apiService.setAuthRefreshFn(authTokenFunc)
51
-
39
+ newPracticeInstance.apiService.setAuthRefreshFn(() => this.authTokenFunc(practiceUuid))
52
40
  this.practiceInstances.set(cacheKey, newPracticeInstance)
53
41
 
54
42
  return newPracticeInstance
55
43
  }
44
+
45
+ /**
46
+ * Uses the refresh intance to fetch a new auth token for the given practice
47
+ * @param practiceUuid the uuid of the practice or key of a specific instance
48
+ * @returns a new authentication token
49
+ */
50
+ public async authTokenFunc(practiceUuidOrInstanceName?: string): Promise<AuthTokenResponse> {
51
+ // fetch the refresh intance and refresh the token for another practice
52
+ const newPracticeInstance = await this.get('refreshInstance');
53
+ if (newPracticeInstance.guardService) {
54
+ console.log(`\x1b[36m[Auth] Refresh auth called (practiceUuid: ${practiceUuidOrInstanceName})\x1b[36m`)
55
+ return await this.getAuthTokenCbk(newPracticeInstance.guardService, practiceUuidOrInstanceName)
56
+ } else {
57
+ throw Error('[Auth] Unable to refresh token guard service is undefined')
58
+ }
59
+ }
56
60
  }