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 as InnerPasswordManagerClient, clients::*};
7use bitwarden_user_crypto_management::{UserCryptoManagementClient, UserCryptoManagementClientExt};
8use wasm_bindgen::prelude::*;
9
10use crate::platform::{
11 PlatformClient,
12 token_provider::{JsTokenProvider, WasmClientManagedTokens},
13};
14
15#[wasm_bindgen(typescript_custom_section)]
16const TOKEN_CUSTOM_TS_TYPE: &'static str = r#"
17/**
18 * @deprecated Use PasswordManagerClient instead
19 */
20export type BitwardenClient = PasswordManagerClient;
21"#;
22
23#[wasm_bindgen]
25pub struct PasswordManagerClient(pub(crate) InnerPasswordManagerClient);
26
27#[wasm_bindgen]
28impl PasswordManagerClient {
29 #[wasm_bindgen(constructor)]
31 pub fn new(token_provider: JsTokenProvider, settings: Option<ClientSettings>) -> Self {
32 let tokens = Arc::new(WasmClientManagedTokens::new(token_provider));
33 Self(InnerPasswordManagerClient::new_with_client_tokens(
34 settings, tokens,
35 ))
36 }
37
38 pub fn echo(&self, msg: String) -> String {
40 msg
41 }
42
43 pub fn version(&self) -> String {
45 #[cfg(feature = "bitwarden-license")]
46 return format!("COMMERCIAL-{}", env!("SDK_VERSION"));
47 #[cfg(not(feature = "bitwarden-license"))]
48 return env!("SDK_VERSION").to_owned();
49 }
50
51 pub fn throw(&self, msg: String) -> Result<(), TestError> {
53 Err(TestError(msg))
54 }
55
56 pub async fn http_get(&self, url: String) -> Result<String, String> {
58 let client = self.0.0.internal.get_http_client();
59 let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
60
61 res.text().await.map_err(|e| e.to_string())
62 }
63
64 pub fn auth(&self) -> AuthClient {
66 self.0.auth()
67 }
68
69 #[cfg(feature = "bitwarden-license")]
71 pub fn commercial(&self) -> bitwarden_pm::CommercialPasswordManagerClient {
72 self.0.commercial()
73 }
74
75 pub fn crypto(&self) -> CryptoClient {
77 self.0.0.crypto()
78 }
79
80 pub fn user_crypto_management(&self) -> UserCryptoManagementClient {
82 self.0.0.user_crypto_management()
83 }
84
85 pub fn vault(&self) -> VaultClient {
87 self.0.vault()
88 }
89
90 pub fn platform(&self) -> PlatformClient {
92 PlatformClient::new(self.0.0.clone())
93 }
94
95 pub fn generator(&self) -> GeneratorClient {
97 self.0.generator()
98 }
99
100 pub fn exporters(&self) -> ExporterClient {
102 self.0.exporters()
103 }
104}
105
106#[bitwarden_error(basic)]
107pub struct TestError(String);
108
109impl Display for TestError {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 write!(f, "{}", self.0)
112 }
113}