bitwarden_wasm_internal/
client.rs1extern crate console_error_panic_hook;
2use std::{fmt::Display, sync::Arc};
3
4use bitwarden_core::{ClientSettings, key_management::state_bridge::StateBridgeClient};
5use bitwarden_error::bitwarden_error;
6use bitwarden_pm::{PasswordManagerClient as InnerPasswordManagerClient, clients::*};
7use bitwarden_policies::PolicyClient;
8use bitwarden_user_crypto_management::{UserCryptoManagementClient, UserCryptoManagementClientExt};
9use wasm_bindgen::prelude::*;
10
11use crate::platform::{
12 PlatformClient,
13 token_provider::{JsTokenProvider, WasmClientManagedTokens},
14};
15
16#[wasm_bindgen(typescript_custom_section)]
17const TOKEN_CUSTOM_TS_TYPE: &'static str = r#"
18/**
19 * @deprecated Use PasswordManagerClient instead
20 */
21export type BitwardenClient = PasswordManagerClient;
22"#;
23
24#[wasm_bindgen]
26pub struct PasswordManagerClient(pub(crate) InnerPasswordManagerClient);
27
28#[wasm_bindgen]
29impl PasswordManagerClient {
30 #[wasm_bindgen(constructor)]
32 pub fn new(token_provider: JsTokenProvider, settings: Option<ClientSettings>) -> Self {
33 let tokens = Arc::new(WasmClientManagedTokens::new(token_provider));
34 Self(InnerPasswordManagerClient::new_with_client_tokens(
35 settings, tokens,
36 ))
37 }
38
39 pub fn echo(&self, msg: String) -> String {
41 msg
42 }
43
44 pub fn version(&self) -> String {
46 #[cfg(feature = "bitwarden-license")]
47 return format!("COMMERCIAL-{}", env!("SDK_VERSION"));
48 #[cfg(not(feature = "bitwarden-license"))]
49 return env!("SDK_VERSION").to_owned();
50 }
51
52 pub fn throw(&self, msg: String) -> Result<(), TestError> {
54 Err(TestError(msg))
55 }
56
57 pub async fn http_get(&self, url: String) -> Result<String, String> {
59 let client = self.0.0.internal.get_http_client();
60 let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
61
62 res.text().await.map_err(|e| e.to_string())
63 }
64
65 pub fn auth(&self) -> AuthClient {
67 self.0.auth()
68 }
69
70 #[cfg(feature = "bitwarden-license")]
72 pub fn commercial(&self) -> bitwarden_pm::CommercialPasswordManagerClient {
73 self.0.commercial()
74 }
75
76 pub fn crypto(&self) -> CryptoClient {
78 self.0.0.crypto()
79 }
80
81 pub fn km_state_bridge(&self) -> StateBridgeClient {
83 self.0.0.km_state_bridge()
84 }
85
86 pub fn user_crypto_management(&self) -> UserCryptoManagementClient {
88 self.0.0.user_crypto_management()
89 }
90
91 pub fn vault(&self) -> VaultClient {
93 self.0.vault()
94 }
95
96 pub fn platform(&self) -> PlatformClient {
98 PlatformClient::new(self.0.0.clone())
99 }
100
101 pub fn generator(&self) -> GeneratorClient {
103 self.0.generator()
104 }
105
106 pub fn exporters(&self) -> ExporterClient {
108 self.0.exporters()
109 }
110
111 pub fn policies(&self) -> PolicyClient {
113 self.0.policies()
114 }
115
116 pub fn sends(&self) -> SendClient {
118 self.0.sends()
119 }
120}
121
122#[bitwarden_error(basic)]
123pub struct TestError(String);
124
125impl Display for TestError {
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 write!(f, "{}", self.0)
128 }
129}