1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-30 16:23:53 +00:00

Wrap SDK functions in firstValueFrom

This commit is contained in:
Nik Gilmore
2026-01-07 16:49:19 -08:00
parent 8c5f90e124
commit 087d18ff24

View File

@@ -910,9 +910,9 @@ export class CipherService implements CipherServiceAbstraction {
userId: UserId,
orgAdmin?: boolean,
): Promise<CipherView | void> {
const resultCipherView = firstValueFrom(
const resultCipherView = await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
@@ -927,7 +927,7 @@ export class CipherService implements CipherServiceAbstraction {
return CipherView.fromSdkCipherView(result);
}),
catchError((error: unknown) => {
this.logService.error(`Failed to encrypt cipher: ${error}`);
this.logService.error(`Failed to create cipher: ${error}`);
return EMPTY;
}),
),
@@ -988,9 +988,9 @@ export class CipherService implements CipherServiceAbstraction {
originalCipherView?: CipherView,
orgAdmin?: boolean,
): Promise<CipherView> {
const resultCipherView = firstValueFrom(
const resultCipherView = await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
@@ -1009,7 +1009,7 @@ export class CipherService implements CipherServiceAbstraction {
return CipherView.fromSdkCipherView(result);
}),
catchError((error: unknown) => {
this.logService.error(`Failed to encrypt cipher: ${error}`);
this.logService.error(`Failed to update cipher: ${error}`);
return EMPTY;
}),
),
@@ -1425,22 +1425,24 @@ export class CipherService implements CipherServiceAbstraction {
}
private async deleteWithServer_sdk(id: string, userId: UserId, asAdmin = false): Promise<any> {
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value.vault().ciphers().admin().delete(asUuid(id));
} else {
await ref.value.vault().ciphers().delete(asUuid(id));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to encrypt cipher: ${error}`);
return EMPTY;
}),
await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value.vault().ciphers().admin().delete(asUuid(id));
} else {
await ref.value.vault().ciphers().delete(asUuid(id));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to delete cipher: ${error}`);
return EMPTY;
}),
),
);
await this.clearCache(userId);
}
@@ -1467,29 +1469,35 @@ export class CipherService implements CipherServiceAbstraction {
userId: UserId,
asAdmin = false,
): Promise<any> {
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value
.vault()
.ciphers()
.admin()
.delete_many(
ids.map((id) => asUuid(id)),
null, // TODO: This is required in the SDK - need to remove from SDK or require here
// But how did it work before????? The server also throws a 404 if not provided....
);
} else {
await ref.value
.vault()
.ciphers()
.delete_many(ids.map((id) => asUuid(id)));
}
}),
await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value
.vault()
.ciphers()
.admin()
.delete_many(
ids.map((id) => asUuid(id)),
null, // TODO: This is required in the SDK - need to remove from SDK or require here
// But how did it work before????? The server also throws a 404 if not provided....
);
} else {
await ref.value
.vault()
.ciphers()
.delete_many(ids.map((id) => asUuid(id)));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to delete multiple ciphers: ${error}`);
return EMPTY;
}),
),
);
await this.clearCache(userId);
}
@@ -1671,18 +1679,24 @@ export class CipherService implements CipherServiceAbstraction {
}
async softDeleteWithServer_sdk(id: string, userId: UserId, asAdmin = false): Promise<any> {
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value.vault().ciphers().admin().soft_delete(asUuid(id));
} else {
await ref.value.vault().ciphers().soft_delete(asUuid(id));
}
}),
await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value.vault().ciphers().admin().soft_delete(asUuid(id));
} else {
await ref.value.vault().ciphers().soft_delete(asUuid(id));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to soft delete cipher: ${error}`);
return EMPTY;
}),
),
);
await this.clearCache(userId);
}
@@ -1706,29 +1720,35 @@ export class CipherService implements CipherServiceAbstraction {
}
async softDeleteManyWithServer_sdk(ids: string[], userId: UserId, asAdmin = false): Promise<any> {
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value
.vault()
.ciphers()
.admin()
.soft_delete_many(
ids.map((id) => asUuid(id)),
null, // TODO: This is required in the SDK - need to remove from SDK or require here
// But how did it work before????? The server also throws a 404 if not provided....
);
} else {
await ref.value
.vault()
.ciphers()
.soft_delete_many(ids.map((id) => asUuid(id)));
}
}),
await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value
.vault()
.ciphers()
.admin()
.soft_delete_many(
ids.map((id) => asUuid(id)),
null, // TODO: This is required in the SDK - need to remove from SDK or require here
// But how did it work before????? The server also throws a 404 if not provided....
);
} else {
await ref.value
.vault()
.ciphers()
.soft_delete_many(ids.map((id) => asUuid(id)));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to soft delete multiple ciphers: ${error}`);
return EMPTY;
}),
),
);
await this.clearCache(userId);
}
@@ -1785,18 +1805,24 @@ export class CipherService implements CipherServiceAbstraction {
}
private async restoreWithServer_sdk(id: string, userId: UserId, asAdmin = false): Promise<any> {
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value.vault().ciphers().admin().restore(asUuid(id));
} else {
await ref.value.vault().ciphers().restore(asUuid(id));
}
}),
await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
if (asAdmin) {
await ref.value.vault().ciphers().admin().restore(asUuid(id));
} else {
await ref.value.vault().ciphers().restore(asUuid(id));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to restore cipher: ${error}`);
return EMPTY;
}),
),
);
await this.clearCache(userId);
}
@@ -1835,31 +1861,37 @@ export class CipherService implements CipherServiceAbstraction {
userId: UserId,
orgId?: string,
): Promise<void> {
this.sdkService.userClient$(userId).pipe(
map(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
if (!sdk) {
throw new Error("SDK not available");
}
using ref = sdk.take();
// No longer using an asAdmin Param. Org Vault bulkRestore will assess if an item is unassigned or editable
// The Org Vault will pass those ids an array as well as the orgId when calling bulkRestore
if (orgId) {
await ref.value
.vault()
.ciphers()
.admin()
.restore_many(
ids.map((id) => asUuid(id)),
asUuid(orgId),
);
} else {
await ref.value
.vault()
.ciphers()
.restore_many(ids.map((id) => asUuid(id)));
}
}),
// No longer using an asAdmin Param. Org Vault bulkRestore will assess if an item is unassigned or editable
// The Org Vault will pass those ids an array as well as the orgId when calling bulkRestore
if (orgId) {
await ref.value
.vault()
.ciphers()
.admin()
.restore_many(
ids.map((id) => asUuid(id)),
asUuid(orgId),
);
} else {
await ref.value
.vault()
.ciphers()
.restore_many(ids.map((id) => asUuid(id)));
}
}),
catchError((error: unknown) => {
this.logService.error(`Failed to restore multiple ciphers: ${error}`);
return EMPTY;
}),
),
);
await this.clearCache(userId);
}