Skip to content

Commit f3c5ca8

Browse files
authored
fix(core): http api connect_timeout deserialization, closes #4004 (#4006)
1 parent ad17861 commit f3c5ca8

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

.changes/fix-api-timeout-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Fixes the type of `http > connectTimeout`.

.changes/http-timeout-serde-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Deserialize numeric values (seconds) in the http API `ClientBuilder.connect_timeout` and `HttpRequestBuilder.timeout` fields.

core/tauri/src/api/http.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,34 @@ pub use attohttpc::header;
2121

2222
use header::{HeaderName, HeaderValue};
2323

24+
#[derive(Deserialize)]
25+
#[serde(untagged)]
26+
enum SerdeDuration {
27+
Seconds(u64),
28+
Duration(Duration),
29+
}
30+
31+
fn deserialize_duration<'de, D: Deserializer<'de>>(
32+
deserializer: D,
33+
) -> Result<Option<Duration>, D::Error> {
34+
if let Some(duration) = Option::<SerdeDuration>::deserialize(deserializer)? {
35+
Ok(Some(match duration {
36+
SerdeDuration::Seconds(s) => Duration::from_secs(s),
37+
SerdeDuration::Duration(d) => d,
38+
}))
39+
} else {
40+
Ok(None)
41+
}
42+
}
43+
2444
/// The builder of [`Client`].
2545
#[derive(Debug, Clone, Default, Deserialize)]
2646
#[serde(rename_all = "camelCase")]
2747
pub struct ClientBuilder {
2848
/// Max number of redirections to follow.
2949
pub max_redirections: Option<usize>,
3050
/// Connect timeout for the request.
51+
#[serde(deserialize_with = "deserialize_duration")]
3152
pub connect_timeout: Option<Duration>,
3253
}
3354

@@ -448,6 +469,7 @@ pub struct HttpRequestBuilder {
448469
/// The request body
449470
pub body: Option<Body>,
450471
/// Timeout for the whole request
472+
#[serde(deserialize_with = "deserialize_duration")]
451473
pub timeout: Option<Duration>,
452474
/// The response type (defaults to Json)
453475
pub response_type: Option<ResponseType>,

tooling/api/src/http.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@
4545

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

48+
interface Duration {
49+
secs: number
50+
nanos: number
51+
}
52+
4853
interface ClientOptions {
4954
maxRedirections: number
50-
connectTimeout: number
55+
connectTimeout: number | Duration
5156
}
5257

5358
enum ResponseType {
@@ -177,7 +182,7 @@ interface HttpOptions {
177182
headers?: Record<string, any>
178183
query?: Record<string, any>
179184
body?: Body
180-
timeout?: number
185+
timeout?: number | Duration
181186
responseType?: ResponseType
182187
}
183188

@@ -417,6 +422,7 @@ async function fetch<T>(
417422
}
418423

419424
export type {
425+
Duration,
420426
ClientOptions,
421427
Part,
422428
HttpVerb,

0 commit comments

Comments
 (0)