From f3c5ca89e79d429183c4e15a9e7cebada2b493a0 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Fri, 29 Apr 2022 17:26:45 -0700 Subject: [PATCH] fix(core): http api `connect_timeout` deserialization, closes #4004 (#4006) --- .changes/fix-api-timeout-type.md | 5 +++++ .changes/http-timeout-serde-fix.md | 5 +++++ core/tauri/src/api/http.rs | 22 ++++++++++++++++++++++ tooling/api/src/http.ts | 10 ++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 .changes/fix-api-timeout-type.md create mode 100644 .changes/http-timeout-serde-fix.md diff --git a/.changes/fix-api-timeout-type.md b/.changes/fix-api-timeout-type.md new file mode 100644 index 000000000..97bc407f2 --- /dev/null +++ b/.changes/fix-api-timeout-type.md @@ -0,0 +1,5 @@ +--- +"api": patch +--- + +Fixes the type of `http > connectTimeout`. diff --git a/.changes/http-timeout-serde-fix.md b/.changes/http-timeout-serde-fix.md new file mode 100644 index 000000000..c6855c5d9 --- /dev/null +++ b/.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. diff --git a/core/tauri/src/api/http.rs b/core/tauri/src/api/http.rs index c74c3fe55..969a34b14 100644 --- a/core/tauri/src/api/http.rs +++ b/core/tauri/src/api/http.rs @@ -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, D::Error> { + if let Some(duration) = Option::::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, /// Connect timeout for the request. + #[serde(deserialize_with = "deserialize_duration")] pub connect_timeout: Option, } @@ -448,6 +469,7 @@ pub struct HttpRequestBuilder { /// The request body pub body: Option, /// Timeout for the whole request + #[serde(deserialize_with = "deserialize_duration")] pub timeout: Option, /// The response type (defaults to Json) pub response_type: Option, diff --git a/tooling/api/src/http.ts b/tooling/api/src/http.ts index a1d30ed5c..bbf12b3eb 100644 --- a/tooling/api/src/http.ts +++ b/tooling/api/src/http.ts @@ -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 query?: Record body?: Body - timeout?: number + timeout?: number | Duration responseType?: ResponseType } @@ -417,6 +422,7 @@ async function fetch( } export type { + Duration, ClientOptions, Part, HttpVerb,