Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion libs/common/src/platform/abstractions/sdk/sdk.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
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) {
super(`User (${userId}) is not logged in`);
}
}

export class InvalidUuid extends Error {
constructor(uuid: string) {
super(`Invalid UUID: ${uuid}`);

Check warning on line 17 in libs/common/src/platform/abstractions/sdk/sdk.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L17 was not covered by tests
}
}

/**
* Converts a string to UUID. Will throw an error if the UUID is non valid.
*/
export function asUuid<T extends Uuid>(uuid: string): T {
if (Utils.isGuid(uuid)) {
return uuid as T;

Check warning on line 26 in libs/common/src/platform/abstractions/sdk/sdk.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L26 was not covered by tests
}

throw new InvalidUuid(uuid);

Check warning on line 29 in libs/common/src/platform/abstractions/sdk/sdk.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L29 was not covered by tests
}

/**
* Converts a UUID to the string representation.
*/
export function uuidToString<T extends Uuid>(uuid: T): string {
return uuid as unknown as string;

Check warning on line 36 in libs/common/src/platform/abstractions/sdk/sdk.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L36 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: should we do a sanity check here just to double check that the value is a string/defined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but they need to use something that is tagged as uuid, feels pretty hard to accidentally insert something unexpected here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if it comes across a network or from a deserialization process?

}

export abstract class SdkService {
/**
* Retrieve the version of the SDK.
Expand Down
Loading