Skip to content

Commit 8758a0a

Browse files
committed
Add support for typescript
Run `cargo test -p nym-vpn-lib-types -F typescript-bindings` to generate bindings under ./bindings
1 parent 5037f4d commit 8758a0a

File tree

18 files changed

+274
-37
lines changed

18 files changed

+274
-37
lines changed

nym-vpn-core/Cargo.lock

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nym-vpn-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ tonic = "0.14.2"
180180
tonic-prost = "0.14.2"
181181
tonic-prost-build = "0.14.2"
182182
typed-builder = "0.21"
183+
ts-rs = "11.0.1"
183184
tower = "0.5.2"
184185
tracing = "0.1"
185186
tracing-appender = "0.2.3"

nym-vpn-core/crates/nym-vpn-lib-types/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ nym-type-conversions = [
2626
"dep:nym-vpn-network-config",
2727
]
2828
uniffi-bindings = ["dep:uniffi", "ipnetwork"]
29+
typescript-bindings = ["dep:ts-rs", "serde"]
30+
serde = ["dep:serde"]
2931

3032
[dependencies]
31-
serde = { workspace = true, features = ["derive"] }
33+
time = { workspace = true, features = ["serde", "parsing", "formatting"] }
34+
serde = { workspace = true, features = ["derive"], optional = true }
3235
si-scale.workspace = true
3336
thiserror.workspace = true
34-
time.workspace = true
37+
ts-rs = { workspace = true, optional = true }
3538
zeroize.workspace = true
3639
ipnetwork = { workspace = true, optional = true }
3740
uniffi = { workspace = true, features = ["tokio"], optional = true }

nym-vpn-core/crates/nym-vpn-lib-types/src/account/controller_error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// Copyright 2025 - Nym Technologies SA <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only
33

4+
#[cfg(feature = "serde")]
5+
use serde::{Deserialize, Serialize};
6+
#[cfg(feature = "typescript-bindings")]
7+
use ts_rs::TS;
8+
49
// todo: rename this type back to ErrorStateReason once support for renaming uniffi structs is released
510
// see: https://github.com/mozilla/uniffi-rs/issues/2212
611
#[derive(Debug, Clone, Eq, PartialEq)]
712
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Enum))]
13+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
14+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
815
pub enum AccountControllerErrorStateReason {
916
/// Error due to storage
1017
Storage { context: String },

nym-vpn-core/crates/nym-vpn-lib-types/src/account/controller_state.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33

44
use std::fmt;
55

6+
#[cfg(feature = "serde")]
7+
use serde::{Deserialize, Serialize};
8+
#[cfg(feature = "typescript-bindings")]
9+
use ts_rs::TS;
10+
611
use crate::AccountControllerErrorStateReason;
712

8-
// Public enum describing the tunnel state
913
#[derive(Debug, Clone, Eq, PartialEq)]
1014
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Enum))]
15+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
16+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1117
pub enum AccountControllerState {
1218
/// We don't have network
1319
Offline,

nym-vpn-core/crates/nym-vpn-lib-types/src/account/ticketbooks.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
// Copyright 2025 - Nym Technologies SA <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only
33

4+
#[cfg(feature = "serde")]
5+
use serde::{Deserialize, Serialize};
6+
#[cfg(feature = "typescript-bindings")]
7+
use ts_rs::TS;
8+
49
// todo: this type is not used anywhere in the codebase for some reason
510
#[derive(Clone, Debug)]
611
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
12+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
13+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
714
pub struct AvailableTickets {
815
pub mixnet_entry_tickets: u64,
916
pub mixnet_entry_data: u64,

nym-vpn-core/crates/nym-vpn-lib-types/src/connection_data.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ use std::{
66
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
77
};
88

9+
#[cfg(feature = "serde")]
10+
use serde::{Deserialize, Serialize};
911
use time::OffsetDateTime;
12+
#[cfg(feature = "typescript-bindings")]
13+
use ts_rs::TS;
1014

1115
use crate::TunnelType;
1216

1317
// Represents the identity of a gateway
1418
#[derive(Debug, Clone, Eq, PartialEq)]
1519
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
20+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
21+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1622
pub struct GatewayId {
1723
pub id: String,
1824
}
@@ -32,6 +38,8 @@ impl From<nym_gateway_directory::Gateway> for GatewayId {
3238

3339
#[derive(Clone, Debug, Eq, PartialEq)]
3440
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
41+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
42+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3543
pub struct EstablishConnectionData {
3644
/// Mixnet entry gateway.
3745
pub entry_gateway: GatewayId,
@@ -47,6 +55,8 @@ pub struct EstablishConnectionData {
4755
/// Describes the current state when establishing connection.
4856
#[derive(Clone, Debug, Eq, PartialEq)]
4957
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Enum))]
58+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
59+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5060
pub enum EstablishConnectionState {
5161
/// Resolving API IP addresses
5262
ResolvingApiAddresses,
@@ -82,6 +92,8 @@ impl fmt::Display for EstablishConnectionState {
8292

8393
#[derive(Clone, Debug, Eq, PartialEq)]
8494
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
95+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
96+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8597
pub struct ConnectionData {
8698
/// Mixnet entry gateway.
8799
pub entry_gateway: GatewayId,
@@ -90,6 +102,8 @@ pub struct ConnectionData {
90102
pub exit_gateway: GatewayId,
91103

92104
/// When the tunnel connection was last established.
105+
#[cfg_attr(feature = "typescript-bindings", ts(as = "String"))]
106+
#[cfg_attr(feature = "serde", serde(with = "time::serde::iso8601"))]
93107
pub connected_at: OffsetDateTime,
94108

95109
/// Tunnel connection data.
@@ -98,6 +112,8 @@ pub struct ConnectionData {
98112

99113
#[derive(Debug, Clone, Eq, PartialEq)]
100114
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Enum))]
115+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
116+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101117
pub enum TunnelConnectionData {
102118
Mixnet(MixnetConnectionData),
103119
Wireguard(WireguardConnectionData),
@@ -115,6 +131,8 @@ impl TunnelConnectionData {
115131
// Represents a nym-address of the form id.enc@gateway
116132
#[derive(Debug, Clone, Eq, PartialEq)]
117133
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
134+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
135+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
118136
pub struct NymAddress {
119137
pub nym_address: String,
120138
pub gateway_id: String,
@@ -155,6 +173,8 @@ impl From<nym_gateway_directory::IpPacketRouterAddress> for NymAddress {
155173

156174
#[derive(Debug, Clone, Eq, PartialEq)]
157175
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
176+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
177+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
158178
pub struct MixnetConnectionData {
159179
pub nym_address: NymAddress,
160180
pub exit_ipr: NymAddress,
@@ -166,6 +186,8 @@ pub struct MixnetConnectionData {
166186

167187
#[derive(Debug, Clone, Eq, PartialEq)]
168188
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
189+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
190+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
169191
pub struct WireguardConnectionData {
170192
pub entry_bridge_addr: Option<SocketAddr>,
171193
pub entry: WireguardNode,
@@ -174,6 +196,8 @@ pub struct WireguardConnectionData {
174196

175197
#[derive(Debug, Clone, Eq, PartialEq)]
176198
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
199+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
200+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
177201
pub struct WireguardNode {
178202
pub endpoint: SocketAddr,
179203
pub public_key: String,

nym-vpn-core/crates/nym-vpn-lib-types/src/device.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// Copyright 2024 - Nym Technologies SA <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only
33

4+
#[cfg(feature = "serde")]
5+
use serde::{Deserialize, Serialize};
6+
#[cfg(feature = "typescript-bindings")]
7+
use ts_rs::TS;
8+
49
#[derive(Clone, Debug, PartialEq, Eq)]
510
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
11+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
12+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
613
pub struct NymVpnDevice {
714
pub created_on_utc: String,
815
pub last_updated_utc: String,
@@ -12,6 +19,8 @@ pub struct NymVpnDevice {
1219

1320
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1421
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Enum))]
22+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
23+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1524
pub enum NymVpnDeviceStatus {
1625
Active,
1726
Inactive,
@@ -20,6 +29,8 @@ pub enum NymVpnDeviceStatus {
2029

2130
#[derive(Debug, Clone, PartialEq)]
2231
#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Record))]
32+
#[cfg_attr(feature = "typescript-bindings", derive(TS), ts(export))]
33+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2334
pub struct NymVpnUsage {
2435
pub created_on_utc: String,
2536
pub last_updated_utc: String,

0 commit comments

Comments
 (0)