mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-30 22:41:33 +01:00
[PM-16262] Make getEnvironment
observable and use it in SdkService (#12501)
* feat: re-implement getEnvironment as an observable * feat: deprecate `getEnvironment` * fix: use correct environment function in SdkService * fix: test
This commit is contained in:
parent
2041799174
commit
1d335bb164
@ -128,5 +128,10 @@ export abstract class EnvironmentService {
|
|||||||
/**
|
/**
|
||||||
* Get the environment from state. Useful if you need to get the environment for another user.
|
* Get the environment from state. Useful if you need to get the environment for another user.
|
||||||
*/
|
*/
|
||||||
|
abstract getEnvironment$(userId?: string): Observable<Environment | undefined>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link getEnvironment$} instead.
|
||||||
|
*/
|
||||||
abstract getEnvironment(userId?: string): Promise<Environment | undefined>;
|
abstract getEnvironment(userId?: string): Promise<Environment | undefined>;
|
||||||
}
|
}
|
||||||
|
@ -314,7 +314,7 @@ describe("EnvironmentService", () => {
|
|||||||
|
|
||||||
await switchUser(testUser);
|
await switchUser(testUser);
|
||||||
|
|
||||||
const env = await sut.getEnvironment();
|
const env = await firstValueFrom(sut.getEnvironment$());
|
||||||
expect(env.getHostname()).toBe(expectedHost);
|
expect(env.getHostname()).toBe(expectedHost);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -325,7 +325,7 @@ describe("EnvironmentService", () => {
|
|||||||
setGlobalData(region, new EnvironmentUrls());
|
setGlobalData(region, new EnvironmentUrls());
|
||||||
setUserData(Region.US, new EnvironmentUrls());
|
setUserData(Region.US, new EnvironmentUrls());
|
||||||
|
|
||||||
const env = await sut.getEnvironment();
|
const env = await firstValueFrom(sut.getEnvironment$());
|
||||||
expect(env.getHostname()).toBe(expectedHost);
|
expect(env.getHostname()).toBe(expectedHost);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -338,7 +338,7 @@ describe("EnvironmentService", () => {
|
|||||||
setGlobalData(region, new EnvironmentUrls());
|
setGlobalData(region, new EnvironmentUrls());
|
||||||
setUserData(Region.US, new EnvironmentUrls());
|
setUserData(Region.US, new EnvironmentUrls());
|
||||||
|
|
||||||
const env = await sut.getEnvironment(testUser);
|
const env = await firstValueFrom(sut.getEnvironment$(testUser));
|
||||||
expect(env.getHostname()).toBe(expectedHost);
|
expect(env.getHostname()).toBe(expectedHost);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -355,7 +355,7 @@ describe("EnvironmentService", () => {
|
|||||||
|
|
||||||
await switchUser(testUser);
|
await switchUser(testUser);
|
||||||
|
|
||||||
const env = await sut.getEnvironment(alternateTestUser);
|
const env = await firstValueFrom(sut.getEnvironment$(alternateTestUser));
|
||||||
expect(env.getHostname()).toBe(expectedHost);
|
expect(env.getHostname()).toBe(expectedHost);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -366,7 +366,7 @@ describe("EnvironmentService", () => {
|
|||||||
setGlobalData(Region.SelfHosted, globalSelfHostUrls);
|
setGlobalData(Region.SelfHosted, globalSelfHostUrls);
|
||||||
setUserData(Region.EU, new EnvironmentUrls());
|
setUserData(Region.EU, new EnvironmentUrls());
|
||||||
|
|
||||||
const env = await sut.getEnvironment();
|
const env = await firstValueFrom(sut.getEnvironment$());
|
||||||
expect(env.getHostname()).toBe("base.example.com");
|
expect(env.getHostname()).toBe("base.example.com");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -377,7 +377,7 @@ describe("EnvironmentService", () => {
|
|||||||
setGlobalData(Region.SelfHosted, globalSelfHostUrls);
|
setGlobalData(Region.SelfHosted, globalSelfHostUrls);
|
||||||
setUserData(Region.EU, new EnvironmentUrls());
|
setUserData(Region.EU, new EnvironmentUrls());
|
||||||
|
|
||||||
const env = await sut.getEnvironment();
|
const env = await firstValueFrom(sut.getEnvironment$());
|
||||||
expect(env.getHostname()).toBe("vault.example.com");
|
expect(env.getHostname()).toBe("vault.example.com");
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -391,7 +391,7 @@ describe("EnvironmentService", () => {
|
|||||||
|
|
||||||
await switchUser(testUser);
|
await switchUser(testUser);
|
||||||
|
|
||||||
const env = await sut.getEnvironment(alternateTestUser);
|
const env = await firstValueFrom(sut.getEnvironment$(alternateTestUser));
|
||||||
expect(env.getHostname()).toBe("base.example.com");
|
expect(env.getHostname()).toBe("base.example.com");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -271,23 +271,30 @@ export class DefaultEnvironmentService implements EnvironmentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getEnvironment(userId?: UserId): Promise<Environment | undefined> {
|
getEnvironment$(userId?: UserId): Observable<Environment | undefined> {
|
||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
return await firstValueFrom(this.environment$);
|
return this.environment$;
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = await this.getEnvironmentState(userId);
|
return this.activeAccountId$.pipe(
|
||||||
return this.buildEnvironment(state.region, state.urls);
|
switchMap((activeUserId) => {
|
||||||
|
// Previous rules dictated that we only get from user scoped state if there is an active user.
|
||||||
|
if (activeUserId == null) {
|
||||||
|
return this.globalState.state$;
|
||||||
|
}
|
||||||
|
return this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$;
|
||||||
|
}),
|
||||||
|
map((state) => {
|
||||||
|
return this.buildEnvironment(state?.region, state?.urls);
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getEnvironmentState(userId: UserId | null) {
|
/**
|
||||||
// Previous rules dictated that we only get from user scoped state if there is an active user.
|
* @deprecated Use getEnvironment$ instead.
|
||||||
const activeUserId = await firstValueFrom(this.activeAccountId$);
|
*/
|
||||||
return activeUserId == null
|
async getEnvironment(userId?: UserId): Promise<Environment | undefined> {
|
||||||
? await firstValueFrom(this.globalState.state$)
|
return firstValueFrom(this.getEnvironment$(userId));
|
||||||
: await firstValueFrom(
|
|
||||||
this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async seedUserEnvironment(userId: UserId) {
|
async seedUserEnvironment(userId: UserId) {
|
||||||
|
@ -56,6 +56,9 @@ describe("DefaultSdkService", () => {
|
|||||||
const userId = "user-id" as UserId;
|
const userId = "user-id" as UserId;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
environmentService.getEnvironment$
|
||||||
|
.calledWith(userId)
|
||||||
|
.mockReturnValue(new BehaviorSubject(mock<Environment>()));
|
||||||
accountService.accounts$ = of({
|
accountService.accounts$ = of({
|
||||||
[userId]: { email: "email", emailVerified: true, name: "name" } as AccountInfo,
|
[userId]: { email: "email", emailVerified: true, name: "name" } as AccountInfo,
|
||||||
});
|
});
|
||||||
|
@ -78,7 +78,7 @@ export class DefaultSdkService implements SdkService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const client$ = combineLatest([
|
const client$ = combineLatest([
|
||||||
this.environmentService.environment$,
|
this.environmentService.getEnvironment$(userId),
|
||||||
account$,
|
account$,
|
||||||
kdfParams$,
|
kdfParams$,
|
||||||
privateKey$,
|
privateKey$,
|
||||||
|
Loading…
Reference in New Issue
Block a user