2024-03-10 12:42:56 +01:00
|
|
|
use napi_derive::napi;
|
|
|
|
use crate::ffi::js::JsCodempError;
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
/// main codemp client session
|
|
|
|
pub struct JsCodempClient(tokio::sync::RwLock<crate::Client>);
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
/// connect to codemp servers and return a client session
|
|
|
|
pub async fn connect(addr: Option<String>) -> napi::Result<JsCodempClient>{
|
|
|
|
let client = crate::Client::new(addr.as_deref().unwrap_or("http://codemp.alemi.dev:50053"))
|
2024-08-07 23:06:33 +02:00
|
|
|
.await?;
|
2024-03-10 12:42:56 +01:00
|
|
|
|
|
|
|
Ok(JsCodempClient(tokio::sync::RwLock::new(client)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl JsCodempClient {
|
|
|
|
#[napi]
|
|
|
|
/// login against AuthService with provided credentials, optionally requesting access to a workspace
|
|
|
|
pub async fn login(&self, username: String, password: String, workspace_id: Option<String>) -> napi::Result<()> {
|
2024-08-07 23:06:33 +02:00
|
|
|
self.0.read().await.login(username, password, workspace_id).await?;
|
2024-03-10 12:42:56 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
/// join workspace with given id (will start its cursor controller)
|
|
|
|
pub async fn join_workspace(&self, workspace: String) -> napi::Result<JsWorkspace> {
|
2024-08-07 23:06:33 +02:00
|
|
|
Ok(JsWorkspace::from(self.0.write().await.join_workspace(&workspace).await?))
|
2024-03-10 12:42:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
/// get workspace with given id, if it exists
|
|
|
|
pub async fn get_workspace(&self, workspace: String) -> Option<JsWorkspace> {
|
|
|
|
self.0.read().await.get_workspace(&workspace).map(|w| JsWorkspace::from(w))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
/// return current sessions's user id
|
|
|
|
pub async fn user_id(&self) -> String {
|
|
|
|
self.0.read().await.user_id().to_string()
|
|
|
|
}
|
|
|
|
}
|