Skip to content

Commit

Permalink
fix(core): http api connect_timeout deserialization, closes #4004 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 30, 2022
1 parent ad17861 commit f3c5ca8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-api-timeout-type.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Fixes the type of `http > connectTimeout`.
5 changes: 5 additions & 0 deletions .changes/http-timeout-serde-fix.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Deserialize numeric values (seconds) in the http API `ClientBuilder.connect_timeout` and `HttpRequestBuilder.timeout` fields.
22 changes: 22 additions & 0 deletions core/tauri/src/api/http.rs
Expand Up @@ -21,13 +21,34 @@ pub use attohttpc::header;

use header::{HeaderName, HeaderValue};

#[derive(Deserialize)]
#[serde(untagged)]
enum SerdeDuration {
Seconds(u64),
Duration(Duration),
}

fn deserialize_duration<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Duration>, D::Error> {
if let Some(duration) = Option::<SerdeDuration>::deserialize(deserializer)? {
Ok(Some(match duration {
SerdeDuration::Seconds(s) => Duration::from_secs(s),
SerdeDuration::Duration(d) => d,
}))
} else {
Ok(None)
}
}

/// The builder of [`Client`].
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientBuilder {
/// Max number of redirections to follow.
pub max_redirections: Option<usize>,
/// Connect timeout for the request.
#[serde(deserialize_with = "deserialize_duration")]
pub connect_timeout: Option<Duration>,
}

Expand Down Expand Up @@ -448,6 +469,7 @@ pub struct HttpRequestBuilder {
/// The request body
pub body: Option<Body>,
/// Timeout for the whole request
#[serde(deserialize_with = "deserialize_duration")]
pub timeout: Option<Duration>,
/// The response type (defaults to Json)
pub response_type: Option<ResponseType>,
Expand Down
10 changes: 8 additions & 2 deletions tooling/api/src/http.ts
Expand Up @@ -45,9 +45,14 @@

import { invokeTauriCommand } from './helpers/tauri'

interface Duration {
secs: number
nanos: number
}

interface ClientOptions {
maxRedirections: number
connectTimeout: number
connectTimeout: number | Duration
}

enum ResponseType {
Expand Down Expand Up @@ -177,7 +182,7 @@ interface HttpOptions {
headers?: Record<string, any>
query?: Record<string, any>
body?: Body
timeout?: number
timeout?: number | Duration
responseType?: ResponseType
}

Expand Down Expand Up @@ -417,6 +422,7 @@ async function fetch<T>(
}

export type {
Duration,
ClientOptions,
Part,
HttpVerb,
Expand Down

0 comments on commit f3c5ca8

Please sign in to comment.