This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new c7d975ef feat(bindings/nodejs): Make PresignedRequest serializable 
(#1797)
c7d975ef is described below

commit c7d975ef7ac38140e62d39fb4cb602b11f18c83d
Author: Suyan <[email protected]>
AuthorDate: Wed Mar 29 20:56:11 2023 +0800

    feat(bindings/nodejs): Make PresignedRequest serializable (#1797)
    
    feat(bindings/nodejs): Support PresignedRequest toJSON
    
    Signed-off-by: suyanhanx <[email protected]>
---
 bindings/nodejs/examples/presign.js |  2 +-
 bindings/nodejs/generated.js        |  3 +--
 bindings/nodejs/index.d.ts          | 20 +++++++---------
 bindings/nodejs/src/lib.rs          | 47 +++++++++++++++++--------------------
 4 files changed, 32 insertions(+), 40 deletions(-)

diff --git a/bindings/nodejs/examples/presign.js 
b/bindings/nodejs/examples/presign.js
index b5826794..740cb300 100644
--- a/bindings/nodejs/examples/presign.js
+++ b/bindings/nodejs/examples/presign.js
@@ -37,7 +37,7 @@ const server = http.createServer(async (req, res) => {
     const presignedRequest = op.presignRead(path, parseInt(expires))
 
     res.statusCode = 200
-    res.end(JSON.stringify({ url: presignedRequest.uri }))
+    res.end(JSON.stringify(presignedRequest))
   } else {
     res.statusCode = 404
     res.end('Not Found')
diff --git a/bindings/nodejs/generated.js b/bindings/nodejs/generated.js
index 03d6bf6e..bd094f42 100644
--- a/bindings/nodejs/generated.js
+++ b/bindings/nodejs/generated.js
@@ -271,11 +271,10 @@ if (!nativeBinding) {
   throw new Error(`Failed to load native binding`)
 }
 
-const { Operator, Entry, Metadata, Lister, BlockingLister, PresignedRequest } 
= nativeBinding
+const { Operator, Entry, Metadata, Lister, BlockingLister } = nativeBinding
 
 module.exports.Operator = Operator
 module.exports.Entry = Entry
 module.exports.Metadata = Metadata
 module.exports.Lister = Lister
 module.exports.BlockingLister = BlockingLister
-module.exports.PresignedRequest = PresignedRequest
diff --git a/bindings/nodejs/index.d.ts b/bindings/nodejs/index.d.ts
index 713f5ddb..fd56c055 100644
--- a/bindings/nodejs/index.d.ts
+++ b/bindings/nodejs/index.d.ts
@@ -22,6 +22,14 @@
 
 /* auto-generated by NAPI-RS */
 
+export interface PresignedRequest {
+  /** HTTP method of this request. */
+  method: string
+  /** URL of this request. */
+  url: string
+  /** HTTP headers of this request. */
+  headers: Record<string, string>
+}
 export class Operator {
   constructor(scheme: string, options?: Record<string, string> | undefined | 
null)
   /** Get current path's metadata **without cache** directly. */
@@ -119,15 +127,3 @@ export class Lister {
 export class BlockingLister {
   next(): Entry | null
 }
-export class PresignedRequest {
-  /** Returns the HTTP method of this request. */
-  get method(): string
-  /** Returns the URI of this request. */
-  get uri(): string
-  /**
-   * Returns the headers of this request.
-   *
-   * The key of the map is the header name, and the value is the header value.
-   */
-  headers(): Record<string, string>
-}
diff --git a/bindings/nodejs/src/lib.rs b/bindings/nodejs/src/lib.rs
index be3f7393..f3178906 100644
--- a/bindings/nodejs/src/lib.rs
+++ b/bindings/nodejs/src/lib.rs
@@ -217,7 +217,7 @@ impl Operator {
             .0
             .presign_read(&path, Duration::seconds(expires as i64))
             .map_err(format_napi_error)?;
-        Ok(PresignedRequest(res))
+        Ok(PresignedRequest::new(res))
     }
 
     /// Get a presigned request for write.
@@ -229,7 +229,7 @@ impl Operator {
             .0
             .presign_write(&path, Duration::seconds(expires as i64))
             .map_err(format_napi_error)?;
-        Ok(PresignedRequest(res))
+        Ok(PresignedRequest::new(res))
     }
 
     /// Get a presigned request for stat.
@@ -241,7 +241,7 @@ impl Operator {
             .0
             .presign_stat(&path, Duration::seconds(expires as i64))
             .map_err(format_napi_error)?;
-        Ok(PresignedRequest(res))
+        Ok(PresignedRequest::new(res))
     }
 }
 
@@ -355,29 +355,21 @@ impl BlockingLister {
     }
 }
 
-#[napi]
-pub struct PresignedRequest(opendal::raw::PresignedRequest);
+#[napi(object)]
+pub struct PresignedRequest {
+    /// HTTP method of this request.
+    pub method: String,
+    /// URL of this request.
+    pub url: String,
+    /// HTTP headers of this request.
+    pub headers: HashMap<String, String>,
+}
 
-#[napi]
 impl PresignedRequest {
-    /// Returns the HTTP method of this request.
-    #[napi(getter)]
-    pub fn method(&self) -> String {
-        self.0.method().to_string()
-    }
-
-    /// Returns the URI of this request.
-    #[napi(getter)]
-    pub fn uri(&self) -> String {
-        self.0.uri().to_string()
-    }
-
-    /// Returns the headers of this request.
-    ///
-    /// The key of the map is the header name, and the value is the header 
value.
-    #[napi]
-    pub fn headers(&self) -> HashMap<String, String> {
-        self.0
+    pub fn new(req: opendal::raw::PresignedRequest) -> Self {
+        let method = req.method().to_string();
+        let url = req.uri().to_string();
+        let headers = req
             .header()
             .iter()
             .map(|(k, v)| {
@@ -388,7 +380,12 @@ impl PresignedRequest {
                         .to_string(),
                 )
             })
-            .collect()
+            .collect();
+        Self {
+            method,
+            url,
+            headers,
+        }
     }
 }
 

Reply via email to