bitwarden_wasm_internal/
client.rs1extern crate console_error_panic_hook;
2use std::{fmt::Display, sync::Arc};
3
4use bitwarden_auth::{AuthClient, AuthClientExt};
5use bitwarden_core::{key_management::CryptoClient, Client, ClientSettings};
6use bitwarden_error::bitwarden_error;
7use bitwarden_exporters::ExporterClientExt;
8use bitwarden_generators::GeneratorClientsExt;
9use bitwarden_vault::{VaultClient, VaultClientExt};
10use wasm_bindgen::prelude::*;
11
12use crate::platform::{
13 token_provider::{JsTokenProvider, WasmClientManagedTokens},
14 PlatformClient,
15};
16
17#[allow(missing_docs)]
18#[wasm_bindgen]
19pub struct BitwardenClient(pub(crate) Client);
20
21#[wasm_bindgen]
22impl BitwardenClient {
23 #[allow(missing_docs)]
24 #[wasm_bindgen(constructor)]
25 pub fn new(token_provider: JsTokenProvider, settings: Option<ClientSettings>) -> Self {
26 let tokens = Arc::new(WasmClientManagedTokens::new(token_provider));
27 Self(Client::new_with_client_tokens(settings, tokens))
28 }
29
30 pub fn echo(&self, msg: String) -> String {
32 msg
33 }
34
35 #[allow(missing_docs)]
36 pub fn version(&self) -> String {
37 env!("SDK_VERSION").to_owned()
38 }
39
40 #[allow(missing_docs)]
41 pub fn throw(&self, msg: String) -> Result<(), TestError> {
42 Err(TestError(msg))
43 }
44
45 pub async fn http_get(&self, url: String) -> Result<String, String> {
47 let client = self.0.internal.get_http_client();
48 let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
49
50 res.text().await.map_err(|e| e.to_string())
51 }
52
53 pub fn auth(&self) -> AuthClient {
55 self.0.auth_new()
56 }
57
58 #[allow(missing_docs)]
59 pub fn crypto(&self) -> CryptoClient {
60 self.0.crypto()
61 }
62
63 #[allow(missing_docs)]
64 pub fn vault(&self) -> VaultClient {
65 self.0.vault()
66 }
67
68 pub fn platform(&self) -> PlatformClient {
70 PlatformClient::new(self.0.clone())
71 }
72
73 pub fn generator(&self) -> bitwarden_generators::GeneratorClient {
75 self.0.generator()
76 }
77
78 #[allow(missing_docs)]
79 pub fn exporters(&self) -> bitwarden_exporters::ExporterClient {
80 self.0.exporters()
81 }
82}
83
84#[bitwarden_error(basic)]
85pub struct TestError(String);
86
87impl Display for TestError {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 write!(f, "{}", self.0)
90 }
91}