bitwarden_wasm_internal/
client.rs

1extern crate console_error_panic_hook;
2use std::fmt::Display;
3
4use bitwarden_core::{key_management::CryptoClient, Client, ClientSettings};
5use bitwarden_error::bitwarden_error;
6use bitwarden_exporters::ExporterClientExt;
7use bitwarden_generators::GeneratorClientsExt;
8use bitwarden_vault::{VaultClient, VaultClientExt};
9use wasm_bindgen::prelude::*;
10
11use crate::platform::PlatformClient;
12
13#[allow(missing_docs)]
14#[wasm_bindgen]
15pub struct BitwardenClient(pub(crate) Client);
16
17#[wasm_bindgen]
18impl BitwardenClient {
19    #[allow(missing_docs)]
20    #[wasm_bindgen(constructor)]
21    pub fn new(settings: Option<ClientSettings>) -> Self {
22        Self(Client::new(settings))
23    }
24
25    /// Test method, echoes back the input
26    pub fn echo(&self, msg: String) -> String {
27        msg
28    }
29
30    #[allow(missing_docs)]
31    pub fn version(&self) -> String {
32        env!("SDK_VERSION").to_owned()
33    }
34
35    #[allow(missing_docs)]
36    pub fn throw(&self, msg: String) -> Result<(), TestError> {
37        Err(TestError(msg))
38    }
39
40    /// Test method, calls http endpoint
41    pub async fn http_get(&self, url: String) -> Result<String, String> {
42        let client = self.0.internal.get_http_client();
43        let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
44
45        res.text().await.map_err(|e| e.to_string())
46    }
47
48    #[allow(missing_docs)]
49    pub fn crypto(&self) -> CryptoClient {
50        self.0.crypto()
51    }
52
53    #[allow(missing_docs)]
54    pub fn vault(&self) -> VaultClient {
55        self.0.vault()
56    }
57
58    /// Constructs a specific client for platform-specific functionality
59    pub fn platform(&self) -> PlatformClient {
60        PlatformClient::new(self.0.clone())
61    }
62
63    /// Constructs a specific client for generating passwords and passphrases
64    pub fn generator(&self) -> bitwarden_generators::GeneratorClient {
65        self.0.generator()
66    }
67
68    #[allow(missing_docs)]
69    pub fn exporters(&self) -> bitwarden_exporters::ExporterClient {
70        self.0.exporters()
71    }
72}
73
74#[bitwarden_error(basic)]
75pub struct TestError(String);
76
77impl Display for TestError {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        write!(f, "{}", self.0)
80    }
81}