From 31783642ed928c21680eece042ecf634d28f7a94 Mon Sep 17 00:00:00 2001 From: suyanhanx Date: Wed, 29 Mar 2023 19:36:46 +0800 Subject: [PATCH] feat(bindings/nodejs): Support PresignedRequest toJSON Signed-off-by: suyanhanx --- 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 b58267940121..740cb30071a4 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 03d6bf6e6f2b..bd094f425864 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 713f5ddbc09a..fd56c055a04c 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 +} export class Operator { constructor(scheme: string, options?: Record | 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 -} diff --git a/bindings/nodejs/src/lib.rs b/bindings/nodejs/src/lib.rs index be3f73930c2f..4ab5233daae7 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 +} -#[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 { - 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, + } } }