bitwarden_wasm_internal/
client.rs

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