bitwarden_wasm_internal/platform/
mod.rs

1use bitwarden_core::Client;
2use bitwarden_vault::{Cipher, Folder};
3use serde::{Deserialize, Serialize};
4use tsify::Tsify;
5use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
6
7mod repository;
8pub mod token_provider;
9
10/// Active feature flags for the SDK.
11#[derive(Serialize, Deserialize, Tsify)]
12#[tsify(into_wasm_abi, from_wasm_abi)]
13pub struct FeatureFlags {
14    /// We intentionally use a loose type here to allow for future flags without breaking changes.
15    #[serde(flatten)]
16    flags: std::collections::HashMap<String, bool>,
17}
18
19#[wasm_bindgen]
20pub struct PlatformClient(Client);
21
22impl PlatformClient {
23    pub fn new(client: Client) -> Self {
24        Self(client)
25    }
26}
27
28#[wasm_bindgen]
29impl PlatformClient {
30    pub fn state(&self) -> StateClient {
31        StateClient::new(self.0.clone())
32    }
33
34    /// Load feature flags into the client
35    pub fn load_flags(&self, flags: FeatureFlags) -> Result<(), JsValue> {
36        self.0.internal.load_flags(flags.flags);
37        Ok(())
38    }
39}
40
41#[wasm_bindgen]
42pub struct StateClient(Client);
43
44impl StateClient {
45    pub fn new(client: Client) -> Self {
46        Self(client)
47    }
48}
49
50repository::create_wasm_repository!(CipherRepository, Cipher, "Repository<Cipher>");
51repository::create_wasm_repository!(FolderRepository, Folder, "Repository<Folder>");
52
53#[wasm_bindgen]
54impl StateClient {
55    pub fn register_cipher_repository(&self, store: CipherRepository) {
56        let store = store.into_channel_impl();
57        self.0.platform().state().register_client_managed(store)
58    }
59
60    pub fn register_folder_repository(&self, store: FolderRepository) {
61        let store = store.into_channel_impl();
62        self.0.platform().state().register_client_managed(store)
63    }
64}