Skip to content

Commit 795fdca

Browse files
authored
Extract init logic to separate function (#156)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective This PR moves the logging and panic handling initialization into a separate function so that it can be called without creating a `BitwardenClient` ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 57f8bb5 commit 795fdca

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

crates/bitwarden-wasm-internal/src/client.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,10 @@ use std::{fmt::Display, rc::Rc};
33

44
use bitwarden_core::{Client, ClientSettings};
55
use bitwarden_error::bitwarden_error;
6-
use log::{set_max_level, Level};
76
use wasm_bindgen::prelude::*;
87

98
use crate::{vault::VaultClient, CryptoClient};
109

11-
#[wasm_bindgen]
12-
pub enum LogLevel {
13-
Trace,
14-
Debug,
15-
Info,
16-
Warn,
17-
Error,
18-
}
19-
20-
fn convert_level(level: LogLevel) -> Level {
21-
match level {
22-
LogLevel::Trace => Level::Trace,
23-
LogLevel::Debug => Level::Debug,
24-
LogLevel::Info => Level::Info,
25-
LogLevel::Warn => Level::Warn,
26-
LogLevel::Error => Level::Error,
27-
}
28-
}
29-
3010
// Rc<...> is to avoid needing to take ownership of the Client during our async run_command
3111
// function https://github.com/rustwasm/wasm-bindgen/issues/2195#issuecomment-799588401
3212
#[wasm_bindgen]
@@ -35,13 +15,7 @@ pub struct BitwardenClient(pub(crate) Rc<Client>);
3515
#[wasm_bindgen]
3616
impl BitwardenClient {
3717
#[wasm_bindgen(constructor)]
38-
pub fn new(settings: Option<ClientSettings>, log_level: Option<LogLevel>) -> Self {
39-
console_error_panic_hook::set_once();
40-
let log_level = convert_level(log_level.unwrap_or(LogLevel::Info));
41-
if let Err(_e) = console_log::init_with_level(log_level) {
42-
set_max_level(log_level.to_level_filter())
43-
}
44-
18+
pub fn new(settings: Option<ClientSettings>) -> Self {
4519
Self(Rc::new(Client::new(settings)))
4620
}
4721

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use log::{set_max_level, Level};
2+
use wasm_bindgen::prelude::*;
3+
4+
#[wasm_bindgen]
5+
pub enum LogLevel {
6+
Trace,
7+
Debug,
8+
Info,
9+
Warn,
10+
Error,
11+
}
12+
13+
fn convert_level(level: LogLevel) -> Level {
14+
match level {
15+
LogLevel::Trace => Level::Trace,
16+
LogLevel::Debug => Level::Debug,
17+
LogLevel::Info => Level::Info,
18+
LogLevel::Warn => Level::Warn,
19+
LogLevel::Error => Level::Error,
20+
}
21+
}
22+
23+
#[wasm_bindgen]
24+
pub fn init_sdk(log_level: Option<LogLevel>) {
25+
console_error_panic_hook::set_once();
26+
let log_level = convert_level(log_level.unwrap_or(LogLevel::Info));
27+
if let Err(_e) = console_log::init_with_level(log_level) {
28+
set_max_level(log_level.to_level_filter())
29+
}
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod client;
22
mod crypto;
33
mod custom_types;
4+
mod init;
45
mod ssh;
56
mod vault;
67

78
pub use client::BitwardenClient;
89
pub use crypto::CryptoClient;
10+
pub use init::init_sdk;
911
pub use vault::{folders::ClientFolders, VaultClient};

0 commit comments

Comments
 (0)