fix(core): http api connect_timeout deserialization, closes #4004 (#4006)

This commit is contained in:
Lucas Fernandes Nogueira
2022-04-29 17:26:45 -07:00
committed by GitHub
parent ad1786178a
commit f3c5ca89e7
4 changed files with 40 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"api": patch
---
Fixes the type of `http > connectTimeout`.

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Deserialize numeric values (seconds) in the http API `ClientBuilder.connect_timeout` and `HttpRequestBuilder.timeout` fields.

View File

@@ -21,6 +21,26 @@ 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")]
@@ -28,6 +48,7 @@ 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>,
}
@@ -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>,

View File

@@ -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 {
@@ -177,7 +182,7 @@ interface HttpOptions {
headers?: Record<string, any>
query?: Record<string, any>
body?: Body
timeout?: number
timeout?: number | Duration
responseType?: ResponseType
}
@@ -417,6 +422,7 @@ async function fetch<T>(
}
export type {
Duration,
ClientOptions,
Part,
HttpVerb,