Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Add http & http_client wasm bindings (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Oct 2, 2024
1 parent 5eed046 commit 40445ac
Show file tree
Hide file tree
Showing 50 changed files with 2,290 additions and 330 deletions.
6 changes: 3 additions & 3 deletions bindings/tbdex_wasm/src/foreign_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ impl WasmFetchOptions {
#[wasm_bindgen(getter)]
pub fn headers(&self) -> JsValue {
match &self.inner.headers {
Some(map) => serde_wasm_bindgen::to_value(map).unwrap_or(JsValue::undefined()),
None => JsValue::undefined(),
Some(map) => serde_wasm_bindgen::to_value(map).unwrap_or(JsValue::UNDEFINED),
None => JsValue::UNDEFINED,
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ impl WasmResponse {

#[wasm_bindgen(getter)]
pub fn headers(&self) -> JsValue {
serde_wasm_bindgen::to_value(&self.inner.headers).unwrap_or(JsValue::undefined())
serde_wasm_bindgen::to_value(&self.inner.headers).unwrap_or(JsValue::UNDEFINED)
}

// TODO body should be optional in web5-rs
Expand Down
46 changes: 46 additions & 0 deletions bindings/tbdex_wasm/src/http/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
errors::{map_err, Result},
resources::balance::WasmBalance,
};
use tbdex::{
http::balances::GetBalancesResponseBody,
json::{FromJson, ToJson},
};
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub struct WasmGetBalancesResponseBody {
inner: GetBalancesResponseBody,
}

#[wasm_bindgen]
impl WasmGetBalancesResponseBody {
#[wasm_bindgen(constructor)]
pub fn new(data: Vec<WasmBalance>) -> Self {
Self {
inner: GetBalancesResponseBody {
data: data.into_iter().map(|o| o.into()).collect(),
},
}
}

#[wasm_bindgen(getter)]
pub fn data(&self) -> Vec<WasmBalance> {
self.inner
.data
.clone()
.into_iter()
.map(|o| o.into())
.collect()
}

pub fn from_json_string(json: &str) -> Result<WasmGetBalancesResponseBody> {
Ok(Self {
inner: GetBalancesResponseBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}
230 changes: 230 additions & 0 deletions bindings/tbdex_wasm/src/http/exchanges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
use crate::{
errors::{map_err, Result},
messages::rfq::WasmRfq,
};
use tbdex::{
http::exchanges::{
CreateExchangeRequestBody, GetExchangeResponseBody, GetExchangesResponseBody,
ReplyToMessage, ReplyToRequestBody, UpdateExchangeRequestBody, WalletUpdateMessage,
},
json::{FromJson, ToJson},
messages::{Message, MessageKind},
};
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

#[wasm_bindgen]
pub struct WasmGetExchangeResponseBody {
inner: GetExchangeResponseBody,
}

#[wasm_bindgen]
impl WasmGetExchangeResponseBody {
#[wasm_bindgen(constructor)]
pub fn new(data: Vec<JsValue>) -> Result<WasmGetExchangeResponseBody> {
let messages: Vec<Message> = data
.into_iter()
.map(|m| serde_wasm_bindgen::from_value::<Message>(m).map_err(|e| e.into()))
.collect::<Result<Vec<Message>>>()?;

Ok(Self {
inner: GetExchangeResponseBody { data: messages },
})
}

#[wasm_bindgen(getter)]
pub fn data(&self) -> Result<Vec<WasmJsonSerializedMessage>> {
let mut data: Vec<WasmJsonSerializedMessage> = Vec::new();

for message in &self.inner.data {
let kind = match message {
Message::Rfq(_) => MessageKind::Rfq.to_string(),
Message::Quote(_) => MessageKind::Quote.to_string(),
Message::Order(_) => MessageKind::Order.to_string(),
Message::OrderInstructions(_) => MessageKind::OrderInstructions.to_string(),
Message::Cancel(_) => MessageKind::Cancel.to_string(),
Message::OrderStatus(_) => MessageKind::OrderStatus.to_string(),
Message::Close(_) => MessageKind::Close.to_string(),
};

data.push(WasmJsonSerializedMessage {
kind,
json: message.to_json_string().map_err(map_err)?,
});
}

Ok(data)
}

pub fn from_json_string(json: &str) -> Result<WasmGetExchangeResponseBody> {
Ok(Self {
inner: GetExchangeResponseBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}

#[wasm_bindgen]
pub struct WasmJsonSerializedMessage {
kind: String,
json: String,
}

#[wasm_bindgen]
impl WasmJsonSerializedMessage {
#[wasm_bindgen(getter)]
pub fn kind(&self) -> String {
self.kind.clone()
}

#[wasm_bindgen(getter)]
pub fn json(&self) -> String {
self.json.clone()
}
}

#[wasm_bindgen]
pub struct WasmGetExchangesResponseBody {
inner: GetExchangesResponseBody,
}

#[wasm_bindgen]
impl WasmGetExchangesResponseBody {
#[wasm_bindgen(constructor)]
pub fn new(data: Vec<String>) -> Self {
Self {
inner: GetExchangesResponseBody { data },
}
}

#[wasm_bindgen(getter)]
pub fn data(&self) -> Vec<String> {
self.inner.data.clone()
}

pub fn from_json_string(json: &str) -> Result<WasmGetExchangesResponseBody> {
Ok(Self {
inner: GetExchangesResponseBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}

#[wasm_bindgen]
pub struct WasmCreateExchangeRequestBody {
inner: CreateExchangeRequestBody,
}

#[wasm_bindgen]
impl WasmCreateExchangeRequestBody {
#[wasm_bindgen(constructor)]
pub fn new(message: WasmRfq, reply_to: Option<String>) -> Self {
Self {
inner: CreateExchangeRequestBody {
message: message.into(),
reply_to,
},
}
}

#[wasm_bindgen(getter)]
pub fn message(&self) -> WasmRfq {
self.inner.message.clone().into()
}

#[wasm_bindgen(getter)]
pub fn reply_to(&self) -> Option<String> {
self.inner.reply_to.clone()
}

pub fn from_json_string(json: &str) -> Result<WasmCreateExchangeRequestBody> {
Ok(Self {
inner: CreateExchangeRequestBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}

#[wasm_bindgen]
pub struct WasmUpdateExchangeRequestBody {
inner: UpdateExchangeRequestBody,
}

#[wasm_bindgen]
impl WasmUpdateExchangeRequestBody {
#[wasm_bindgen(constructor)]
pub fn new(message: JsValue) -> Result<WasmUpdateExchangeRequestBody> {
let message = serde_wasm_bindgen::from_value::<WalletUpdateMessage>(message)?;
Ok(Self {
inner: UpdateExchangeRequestBody { message },
})
}

#[wasm_bindgen(getter)]
pub fn data(&self) -> Result<WasmJsonSerializedMessage> {
let kind = match self.inner.message {
WalletUpdateMessage::Order(_) => MessageKind::Order.to_string(),
WalletUpdateMessage::Cancel(_) => MessageKind::Cancel.to_string(),
};
let json = self.inner.message.to_json_string().map_err(map_err)?;

Ok(WasmJsonSerializedMessage { kind, json })
}

pub fn from_json_string(json: &str) -> Result<WasmUpdateExchangeRequestBody> {
Ok(Self {
inner: UpdateExchangeRequestBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}

#[wasm_bindgen]
pub struct WasmReplyToRequestBody {
inner: ReplyToRequestBody,
}

#[wasm_bindgen]
impl WasmReplyToRequestBody {
#[wasm_bindgen(constructor)]
pub fn new(message: JsValue) -> Result<WasmReplyToRequestBody> {
let message = serde_wasm_bindgen::from_value::<ReplyToMessage>(message)?;
Ok(Self {
inner: ReplyToRequestBody { message },
})
}

#[wasm_bindgen(getter)]
pub fn data(&self) -> Result<WasmJsonSerializedMessage> {
let kind = match self.inner.message {
ReplyToMessage::Quote(_) => MessageKind::Quote.to_string(),
ReplyToMessage::OrderInstructions(_) => MessageKind::OrderInstructions.to_string(),
ReplyToMessage::OrderStatus(_) => MessageKind::OrderStatus.to_string(),
ReplyToMessage::Close(_) => MessageKind::Close.to_string(),
};
let json = self.inner.message.to_json_string().map_err(map_err)?;

Ok(WasmJsonSerializedMessage { kind, json })
}

pub fn from_json_string(json: &str) -> Result<WasmReplyToRequestBody> {
Ok(Self {
inner: ReplyToRequestBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}
3 changes: 3 additions & 0 deletions bindings/tbdex_wasm/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod balances;
pub mod exchanges;
pub mod offerings;
46 changes: 46 additions & 0 deletions bindings/tbdex_wasm/src/http/offerings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
errors::{map_err, Result},
resources::offering::WasmOffering,
};
use tbdex::{
http::offerings::GetOfferingsResponseBody,
json::{FromJson, ToJson},
};
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub struct WasmGetOfferingsResponseBody {
inner: GetOfferingsResponseBody,
}

#[wasm_bindgen]
impl WasmGetOfferingsResponseBody {
#[wasm_bindgen(constructor)]
pub fn new(data: Vec<WasmOffering>) -> Self {
Self {
inner: GetOfferingsResponseBody {
data: data.into_iter().map(|o| o.into()).collect(),
},
}
}

#[wasm_bindgen(getter)]
pub fn data(&self) -> Vec<WasmOffering> {
self.inner
.data
.clone()
.into_iter()
.map(|o| o.into())
.collect()
}

pub fn from_json_string(json: &str) -> Result<WasmGetOfferingsResponseBody> {
Ok(Self {
inner: GetOfferingsResponseBody::from_json_string(json).map_err(map_err)?,
})
}

pub fn to_json_string(&self) -> Result<String> {
self.inner.to_json_string().map_err(map_err)
}
}
21 changes: 21 additions & 0 deletions bindings/tbdex_wasm/src/http_client/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::{
errors::{map_err, Result},
resources::balance::WasmBalance,
web5::bearer_did::WasmBearerDid,
};
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub async fn get_balances(
pfi_did_uri: &str,
bearer_did: WasmBearerDid,
) -> Result<Vec<WasmBalance>> {
Ok(
tbdex::http_client::balances::get_balances(pfi_did_uri, &bearer_did.into())
.await
.map_err(map_err)?
.into_iter()
.map(|balance| balance.into())
.collect(),
)
}
Loading

0 comments on commit 40445ac

Please sign in to comment.