chore(js): added glue user object

This commit is contained in:
frelodev 2024-09-27 23:34:07 +02:00
parent 512c2b30ea
commit 2b6d2037c7

View file

@ -1,6 +1,31 @@
use napi_derive::napi; use napi_derive::napi;
use crate::{Client, Workspace}; use crate::{Client, Workspace};
#[napi(object, js_name = "User")]
pub struct JsUser {
pub uuid: String,
pub name: String,
}
impl TryFrom<JsUser> for crate::api::User {
type Error = <uuid::Uuid as std::str::FromStr>::Err;
fn try_from(value: JsUser) -> Result<Self, Self::Error> {
Ok(Self {
id: value.uuid.parse()?,
name: value.name,
})
}
}
impl From<crate::api::User> for JsUser {
fn from(value: crate::api::User) -> Self {
Self {
uuid: value.id.to_string(),
name: value.name,
}
}
}
#[napi] #[napi]
/// connect to codemp servers and return a client session /// connect to codemp servers and return a client session
pub async fn connect(config: crate::api::Config) -> napi::Result<crate::Client>{ pub async fn connect(config: crate::api::Config) -> napi::Result<crate::Client>{
@ -51,10 +76,10 @@ impl Client {
self.get_workspace(&workspace) self.get_workspace(&workspace)
} }
#[napi(js_name = "user_id")] #[napi(js_name = "user")]
/// return current sessions's user id /// return current sessions's user id
pub fn js_user_id(&self) -> String { pub fn js_user(&self) -> JsUser {
self.user().id.to_string() self.user().clone().into()
} }
#[napi(js_name = "active_workspaces")] #[napi(js_name = "active_workspaces")]