Skip to content

Commit

Permalink
feat(bindings/nodejs): Support PresignedRequest toJSON
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <[email protected]>
  • Loading branch information
suyanhanx committed Mar 29, 2023
1 parent 2d174fc commit 3178364
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/presign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 8 additions & 12 deletions bindings/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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>
}
47 changes: 22 additions & 25 deletions bindings/nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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)| {
Expand All @@ -388,7 +380,12 @@ impl PresignedRequest {
.to_string(),
)
})
.collect()
.collect();
Self {
method,
url,
headers,
}
}
}

Expand Down

0 comments on commit 3178364

Please sign in to comment.