浏览代码

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

Lucas Fernandes Nogueira 3 年之前
父节点
当前提交
f3c5ca89e7
共有 4 个文件被更改,包括 40 次插入2 次删除
  1. 5 0
      .changes/fix-api-timeout-type.md
  2. 5 0
      .changes/http-timeout-serde-fix.md
  3. 22 0
      core/tauri/src/api/http.rs
  4. 8 2
      tooling/api/src/http.ts

+ 5 - 0
.changes/fix-api-timeout-type.md

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

+ 5 - 0
.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 - 0
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<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>,

+ 8 - 2
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<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,