diff --git a/libs/common/src/platform/abstractions/sdk/sdk.service.ts b/libs/common/src/platform/abstractions/sdk/sdk.service.ts index 3adf3291bbfe..07dfb2aa0df4 100644 --- a/libs/common/src/platform/abstractions/sdk/sdk.service.ts +++ b/libs/common/src/platform/abstractions/sdk/sdk.service.ts @@ -1,9 +1,10 @@ import { Observable } from "rxjs"; -import { BitwardenClient } from "@bitwarden/sdk-internal"; +import { BitwardenClient, Uuid } from "@bitwarden/sdk-internal"; import { UserId } from "../../../types/guid"; import { Rc } from "../../misc/reference-counting/rc"; +import { Utils } from "../../misc/utils"; export class UserNotLoggedInError extends Error { constructor(userId: UserId) { @@ -11,6 +12,30 @@ export class UserNotLoggedInError extends Error { } } +export class InvalidUuid extends Error { + constructor(uuid: string) { + super(`Invalid UUID: ${uuid}`); + } +} + +/** + * Converts a string to UUID. Will throw an error if the UUID is non valid. + */ +export function asUuid(uuid: string): T { + if (Utils.isGuid(uuid)) { + return uuid as T; + } + + throw new InvalidUuid(uuid); +} + +/** + * Converts a UUID to the string representation. + */ +export function uuidToString(uuid: T): string { + return uuid as unknown as string; +} + export abstract class SdkService { /** * Retrieve the version of the SDK.