bitwarden_wasm_internal/
client.rs1extern crate console_error_panic_hook;
2use std::{fmt::Display, sync::Arc};
3
4use bitwarden_core::ClientSettings;
5use bitwarden_error::bitwarden_error;
6use bitwarden_pm::{PasswordManagerClient, clients::*};
7use wasm_bindgen::prelude::*;
8
9use crate::platform::{
10 PlatformClient,
11 token_provider::{JsTokenProvider, WasmClientManagedTokens},
12};
13
14#[wasm_bindgen]
16pub struct BitwardenClient(pub(crate) PasswordManagerClient);
17
18#[wasm_bindgen]
19impl BitwardenClient {
20 #[wasm_bindgen(constructor)]
22 pub fn new(token_provider: JsTokenProvider, settings: Option<ClientSettings>) -> Self {
23 let tokens = Arc::new(WasmClientManagedTokens::new(token_provider));
24 Self(PasswordManagerClient::new_with_client_tokens(
25 settings, tokens,
26 ))
27 }
28
29 pub fn echo(&self, msg: String) -> String {
31 msg
32 }
33
34 pub fn version(&self) -> String {
36 #[cfg(feature = "bitwarden-license")]
37 return format!("COMMERCIAL-{}", env!("SDK_VERSION"));
38 #[cfg(not(feature = "bitwarden-license"))]
39 return env!("SDK_VERSION").to_owned();
40 }
41
42 pub fn throw(&self, msg: String) -> Result<(), TestError> {
44 Err(TestError(msg))
45 }
46
47 pub async fn http_get(&self, url: String) -> Result<String, String> {
49 let client = self.0.0.internal.get_http_client();
50 let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
51
52 res.text().await.map_err(|e| e.to_string())
53 }
54
55 pub fn auth(&self) -> AuthClient {
57 self.0.auth()
58 }
59
60 #[cfg(feature = "bitwarden-license")]
62 pub fn commercial(&self) -> bitwarden_pm::CommercialPasswordManagerClient {
63 self.0.commercial()
64 }
65
66 pub fn crypto(&self) -> CryptoClient {
68 self.0.0.crypto()
69 }
70
71 pub fn vault(&self) -> VaultClient {
73 self.0.vault()
74 }
75
76 pub fn platform(&self) -> PlatformClient {
78 PlatformClient::new(self.0.0.clone())
79 }
80
81 pub fn generator(&self) -> GeneratorClient {
83 self.0.generator()
84 }
85
86 pub fn exporters(&self) -> ExporterClient {
88 self.0.exporters()
89 }
90}
91
92#[bitwarden_error(basic)]
93pub struct TestError(String);
94
95impl Display for TestError {
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 write!(f, "{}", self.0)
98 }
99}