Skip to content

Commit ead49ba

Browse files
Hintondan-livefront
authored andcommitted
Add UUID helpers to the SDK (#14939)
* Add UUID helpers to the SDK * Address review feedback
1 parent 21a6b12 commit ead49ba

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

libs/common/src/platform/abstractions/sdk/sdk.service.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
import { Observable } from "rxjs";
22

3-
import { BitwardenClient } from "@bitwarden/sdk-internal";
3+
import { BitwardenClient, Uuid } from "@bitwarden/sdk-internal";
44

55
import { UserId } from "../../../types/guid";
66
import { Rc } from "../../misc/reference-counting/rc";
7+
import { Utils } from "../../misc/utils";
78

89
export class UserNotLoggedInError extends Error {
910
constructor(userId: UserId) {
1011
super(`User (${userId}) is not logged in`);
1112
}
1213
}
1314

15+
export class InvalidUuid extends Error {
16+
constructor(uuid: string) {
17+
super(`Invalid UUID: ${uuid}`);
18+
}
19+
}
20+
21+
/**
22+
* Converts a string to UUID. Will throw an error if the UUID is non valid.
23+
*/
24+
export function asUuid<T extends Uuid>(uuid: string): T {
25+
if (Utils.isGuid(uuid)) {
26+
return uuid as T;
27+
}
28+
29+
throw new InvalidUuid(uuid);
30+
}
31+
32+
/**
33+
* Converts a UUID to the string representation.
34+
*/
35+
export function uuidToString<T extends Uuid>(uuid: T): string {
36+
return uuid as unknown as string;
37+
}
38+
1439
export abstract class SdkService {
1540
/**
1641
* Retrieve the version of the SDK.

0 commit comments

Comments
 (0)