Skip to content

Commit e808789

Browse files
authored
Fix wasm failing build due to getrandom in uuid library (#324)
* Fix wasm failing build due to getrandom in uuid library * Add wasm compatibility with request
1 parent 5943cb7 commit e808789

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ serde_json = "1.0"
2020
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing"] }
2121
jsonwebtoken = { version = "8", default-features = false }
2222
yaup = "0.2.0"
23-
uuid = { version = "1.1.2", features = ["v4"] }
2423

2524
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
2625
futures = "0.3"
2726
isahc = { version = "1.0", features = ["http2", "text-decoding"], default_features = false }
27+
uuid = { version = "1.1.2", features = ["v4"] }
2828

2929
[target.'cfg(target_arch = "wasm32")'.dependencies]
3030
js-sys = "0.3.47"

src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ impl Client {
810810
/// let client = client::Client::new(MEILISEARCH_HOST, token);
811811
/// # });
812812
/// ```
813+
#[cfg(not(target_arch = "wasm32"))]
813814
pub fn generate_tenant_token(
814815
&self,
815816
api_key_uid: String,

src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub enum Error {
3737
// The library formating the query parameters encountered an error.
3838
Yaup(yaup::Error),
3939
// The library validating the format of an uuid.
40+
#[cfg(not(target_arch = "wasm32"))]
4041
Uuid(uuid::Error),
4142
// Error thrown in case the version of the Uuid is not v4.
4243
InvalidUuid4Version,
@@ -79,6 +80,7 @@ impl From<yaup::Error> for Error {
7980
}
8081
}
8182

83+
#[cfg(not(target_arch = "wasm32"))]
8284
impl From<uuid::Error> for Error {
8385
fn from(error: uuid::Error) -> Error {
8486
Error::Uuid(error)
@@ -205,6 +207,7 @@ impl std::fmt::Display for Error {
205207
Error::TenantTokensExpiredSignature => write!(fmt, "The provided expires_at is already expired."),
206208
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e),
207209
Error::Yaup(e) => write!(fmt, "Internal Error: could not parse the query parameters: {}", e),
210+
#[cfg(not(target_arch = "wasm32"))]
208211
Error::Uuid(e) => write!(fmt, "The uid of the token has bit an uuid4 format: {}", e),
209212
Error::InvalidUuid4Version => write!(fmt, "The uid provided to the token is not of version uuidv4")
210213
}

src/request.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
112112
let user_agent = qualified_version();
113113

114114
// The 2 following unwraps should not be able to fail
115-
115+
let mut mut_url = url.clone().to_string();
116116
let headers = Headers::new().unwrap();
117117
headers.append("Authorization: Bearer", apikey).unwrap();
118118
headers.append("User-Agent", &user_agent).unwrap();
@@ -121,7 +121,15 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
121121
request.headers(&headers);
122122

123123
match &method {
124-
Method::Get => {
124+
Method::Get(query) => {
125+
let query = yaup::to_string(query)?;
126+
127+
mut_url = if query.is_empty() {
128+
mut_url.to_string()
129+
} else {
130+
format!("{}?{}", mut_url, query)
131+
};
132+
125133
request.method("GET");
126134
}
127135
Method::Delete => {
@@ -145,13 +153,14 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
145153
}
146154

147155
let window = web_sys::window().unwrap(); // TODO remove this unwrap
148-
let response = match JsFuture::from(window.fetch_with_str_and_init(url, &request)).await {
149-
Ok(response) => Response::from(response),
150-
Err(e) => {
151-
error!("Network error: {:?}", e);
152-
return Err(Error::UnreachableServer);
153-
}
154-
};
156+
let response =
157+
match JsFuture::from(window.fetch_with_str_and_init(mut_url.as_str(), &request)).await {
158+
Ok(response) => Response::from(response),
159+
Err(e) => {
160+
error!("Network error: {:?}", e);
161+
return Err(Error::UnreachableServer);
162+
}
163+
};
155164
let status = response.status() as u16;
156165
let text = match response.text() {
157166
Ok(text) => match JsFuture::from(text).await {

src/tenant_tokens.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use jsonwebtoken::{encode, EncodingKey, Header};
33
use serde::{Deserialize, Serialize};
44
use serde_json::Value;
55
use time::OffsetDateTime;
6+
#[cfg(not(target_arch = "wasm32"))]
67
use uuid::Uuid;
78

89
#[derive(Debug, Serialize, Deserialize)]
10+
#[cfg(not(target_arch = "wasm32"))]
911
#[serde(rename_all = "camelCase")]
1012
struct TenantTokenClaim {
1113
api_key_uid: String,
@@ -14,6 +16,7 @@ struct TenantTokenClaim {
1416
exp: Option<OffsetDateTime>,
1517
}
1618

19+
#[cfg(not(target_arch = "wasm32"))]
1720
pub fn generate_tenant_token(
1821
api_key_uid: String,
1922
search_rules: Value,

0 commit comments

Comments
 (0)