Skip to content

Commit

Permalink
Remove need to depend on js-sys when consuming errors (#36)
Browse files Browse the repository at this point in the history
Switch from requiring consumers to depend on js-sys and let
bitwarden-error crate re-expose it. Do we need to do the same thing for
tsify?
  • Loading branch information
Hinton authored Nov 25, 2024
1 parent 3cdf320 commit 130ac6f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions crates/bitwarden-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ secrets = [] # Secrets manager API
wasm = [
"bitwarden-error/wasm",
"dep:wasm-bindgen",
"dep:js-sys",
"dep:tsify-next",
] # WASM support

Expand Down Expand Up @@ -54,7 +53,6 @@ wasm-bindgen = { workspace = true, optional = true }
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }
zxcvbn = { version = ">=3.0.1, <4.0", optional = true }
tsify-next = { workspace = true, optional = true }
js-sys = { workspace = true, optional = true }
bitwarden-error = { workspace = true }

[target.'cfg(not(target_arch="wasm32"))'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden-error-macro/src/basic/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ fn basic_error_wasm(

quote! {
const _: () = {
use wasm_bindgen::prelude::*;
use bitwarden_error::wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = #ts_code;

#[wasm_bindgen(js_name = #is_error_function_name, skip_typescript)]
pub fn is_error(error: &JsValue) -> bool {
let name_js_value = js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL);
let name_js_value = bitwarden_error::js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL);
let name = name_js_value.as_string().unwrap_or_default();
name == #export_as_identifier_str
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden-error-macro/src/flat/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ fn flat_error_wasm(

quote! {
const _: () = {
use wasm_bindgen::prelude::*;
use bitwarden_error::wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = #ts_code;

#[wasm_bindgen(js_name = #is_error_function_name, skip_typescript)]
pub fn is_error(error: &JsValue) -> bool {
let name_js_value = js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL);
let name_js_value = bitwarden_error::js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL);
let name = name_js_value.as_string().unwrap_or_default();
name == #export_as_identifier_str
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-error-macro/src/full/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn bitwarden_error_full(

let wasm_attributes = cfg!(feature = "wasm").then(|| {
quote! {
#[derive(tsify_next::Tsify)]
#[derive(bitwarden_error::tsify_next::Tsify)]
#[tsify(into_wasm_abi)]
}
});
Expand Down
11 changes: 8 additions & 3 deletions crates/bitwarden-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ license-file.workspace = true
keywords.workspace = true

[features]
wasm = ["bitwarden-error-macro/wasm", "dep:wasm-bindgen"]
wasm = [
"bitwarden-error-macro/wasm",
"dep:js-sys",
"dep:tsify-next",
"dep:wasm-bindgen",
]

[dependencies]
bitwarden-error-macro = { workspace = true }
js-sys = { workspace = true, optional = true }
tsify-next = { workspace = true, optional = true }
wasm-bindgen = { workspace = true, optional = true }

[lints]
workspace = true

[dev-dependencies]
js-sys = "0.3.72"
serde.workspace = true
trybuild = "1.0.101"
tsify-next.workspace = true
wasm-bindgen-test = "0.3.45"
13 changes: 13 additions & 0 deletions crates/bitwarden-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ pub mod flat_error;
#[cfg(feature = "wasm")]
pub mod wasm;

/// Re-export the `js_sys` crate since the proc macro depends on it.
#[cfg(feature = "wasm")]
#[doc(hidden)]
pub use ::js_sys;
/// Re-export the `tsify_next` crate since the proc macro depends on it.
#[cfg(feature = "wasm")]
#[doc(hidden)]
pub use ::tsify_next;
/// Re-export the `wasm_bindgen` crate since the proc macro depends on it.
#[cfg(feature = "wasm")]
#[doc(hidden)]
pub use ::wasm_bindgen;

pub mod prelude {
pub use bitwarden_error_macro::*;

Expand Down

0 comments on commit 130ac6f

Please sign in to comment.