This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add http & http_client wasm bindings (#130)
- Loading branch information
1 parent
5eed046
commit 40445ac
Showing
50 changed files
with
2,290 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod balances; | ||
pub mod exchanges; | ||
pub mod offerings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
} |
Oops, something went wrong.