bitwarden_server_communication_config/wasm/
client.rs

1use wasm_bindgen::prelude::*;
2
3use crate::{
4    ServerCommunicationConfig, ServerCommunicationConfigClient,
5    wasm::{JsServerCommunicationConfigRepository, RawJsServerCommunicationConfigRepository},
6};
7
8/// JavaScript wrapper for ServerCommunicationConfigClient
9///
10/// This provides TypeScript access to the server communication configuration client,
11/// allowing clients to check bootstrap requirements and retrieve cookies for HTTP requests.
12#[wasm_bindgen(js_name = ServerCommunicationConfigClient)]
13pub struct JsServerCommunicationConfigClient {
14    client: ServerCommunicationConfigClient<JsServerCommunicationConfigRepository>,
15}
16
17#[wasm_bindgen(js_class = ServerCommunicationConfigClient)]
18impl JsServerCommunicationConfigClient {
19    /// Creates a new ServerCommunicationConfigClient with a JavaScript repository
20    ///
21    /// The repository should be backed by StateProvider (or equivalent
22    /// storage mechanism) for persistence.
23    ///
24    /// # Arguments
25    ///
26    /// * `repository` - JavaScript implementation of the repository interface
27    #[wasm_bindgen(constructor)]
28    pub fn new(repository: RawJsServerCommunicationConfigRepository) -> Self {
29        let js_repository = JsServerCommunicationConfigRepository::new(repository);
30        Self {
31            client: ServerCommunicationConfigClient::new(js_repository),
32        }
33    }
34
35    /// Retrieves the server communication configuration for a hostname
36    ///
37    /// If no configuration exists, returns a default Direct bootstrap configuration.
38    ///
39    /// # Arguments
40    ///
41    /// * `hostname` - The server hostname (e.g., "vault.acme.com")
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if the repository fails to retrieve the configuration.
46    #[wasm_bindgen(js_name = getConfig)]
47    pub async fn get_config(&self, hostname: String) -> Result<ServerCommunicationConfig, String> {
48        self.client.get_config(hostname).await
49    }
50
51    /// Determines if cookie bootstrapping is needed for this hostname
52    ///
53    /// # Arguments
54    ///
55    /// * `hostname` - The server hostname (e.g., "vault.acme.com")
56    #[wasm_bindgen(js_name = needsBootstrap)]
57    pub async fn needs_bootstrap(&self, hostname: String) -> bool {
58        self.client.needs_bootstrap(hostname).await
59    }
60
61    /// Returns all cookies that should be included in requests to this server
62    ///
63    /// Returns an array of [cookie_name, cookie_value] pairs.
64    ///
65    /// # Arguments
66    ///
67    /// * `hostname` - The server hostname (e.g., "vault.acme.com")
68    #[wasm_bindgen(js_name = cookies)]
69    pub async fn cookies(&self, hostname: String) -> Result<JsValue, JsError> {
70        let cookies = self.client.cookies(hostname).await;
71        serde_wasm_bindgen::to_value(&cookies).map_err(|e| JsError::new(&e.to_string()))
72    }
73}