Skip to content

Commit

Permalink
feat: hacky implemenation of tsify-next
Browse files Browse the repository at this point in the history
  • Loading branch information
coroiu committed Sep 25, 2024
1 parent 88a531b commit 46e9463
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions crates/bitwarden-vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ uniffi = [
"bitwarden-crypto/uniffi",
"dep:uniffi",
] # Uniffi bindings
wasm = [] # Typescript and WASM bindings

[dependencies]
base64 = ">=0.22.1, <0.23"
Expand All @@ -42,6 +43,11 @@ thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.28.1", optional = true }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }

# [target.'cfg(feature = "wasm")'.dependencies]
serde-wasm-bindgen = "0.6.5"
tsify-next = { version = "0.5.4", features = ["js"], default-features = false }
wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] }

[dev-dependencies]
tokio = { version = "1.36.0", features = ["rt", "macros"] }

Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden-vault/src/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use reqwest::Url;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;
// #[cfg(feature = "wasm")]
use tsify_next::Tsify;
// #[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::CipherListView;

Expand Down Expand Up @@ -38,6 +42,7 @@ pub enum TotpError {
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]

Check warning on line 45 in crates/bitwarden-vault/src/totp.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/totp.rs#L45

Added line #L45 was not covered by tests
pub struct TotpResponse {
/// Generated TOTP code
pub code: String,
Expand Down
2 changes: 2 additions & 0 deletions crates/bitwarden-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ argon2 = { version = ">=0.5.0, <0.6", features = [
"zeroize",
], default-features = false }
bitwarden = { workspace = true }
bitwarden-vault = { workspace = true, features = ["wasm"] }
bitwarden-json = { path = "../bitwarden-json", features = [
"secrets",
"internal",
Expand All @@ -33,6 +34,7 @@ js-sys = "0.3.68"
log = "0.4.20"
serde = { version = "1.0.196", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
tsify-next = { version = "0.5.4", features = ["js"], default-features = false }
wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.41"

Expand Down
41 changes: 23 additions & 18 deletions crates/bitwarden-wasm/src/vault/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::to_value;
use std::sync::Arc;
use tsify_next::Tsify;
use wasm_bindgen::prelude::*;

use bitwarden::{
Expand All @@ -10,23 +12,24 @@ use chrono::prelude::*;

use crate::error::Result;

#[wasm_bindgen(typescript_custom_section)]
const TOTP_RESPONSE: &'static str = r#"
export interface TotpResponse {
code: string;
period_foo: number;
}
// #[wasm_bindgen(typescript_custom_section)]
// const TOTP_RESPONSE: &'static str = r#"
// export interface TotpResponse {
// code: string;
// period_foo: number;
// }

export interface ClientVault {
generate_totp(key: string, time?: string): Promise<TotpResponse>;
}
"#;
// export interface ClientVault {
// generate_totp(key: string, time?: string): Promise<TotpResponse>;
// }
// "#;

#[wasm_bindgen]
pub struct JsTotpResult {
code: String,
period: u64,
}
// #[derive(Tsify, Serialize, Deserialize)]
// #[tsify(into_wasm_abi, from_wasm_abi)]
// pub struct JsTotpResult {
// code: String,
// period: u64,
// }

// #[derive(uniffi::Object)]
#[wasm_bindgen]

Check warning on line 35 in crates/bitwarden-wasm/src/vault/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/vault/mod.rs#L35

Added line #L35 was not covered by tests
Expand Down Expand Up @@ -66,8 +69,8 @@ impl ClientVault {
/// - A base32 encoded string
/// - OTP Auth URI
/// - Steam URI
#[wasm_bindgen(skip_typescript)]
pub async fn generate_totp(&self, key: String, time: Option<String>) -> JsValue {
#[wasm_bindgen]
pub async fn generate_totp(&self, key: String, time: Option<String>) -> TotpResponse {
// TODO: Fix time
// let time = time.map(|time| {
// // TODO: fix error
Expand All @@ -80,7 +83,9 @@ impl ClientVault {
.map_err(Error::Totp)
.unwrap();

to_value(&result).unwrap()
result

// to_value(&result).unwrap()

// JsTotpResult {
// code: result.code,
Expand Down

0 comments on commit 46e9463

Please sign in to comment.