Skip to content

Commit

Permalink
feat(js-poc): parse different http response formats
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Apr 26, 2024
1 parent 3362efb commit aae531b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/sdk/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum HttpMethod {

/// Response type used by a service to override the handshake http response fields when the
/// transport is HTTP
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct HttpResponse {
pub headers: Option<Vec<(String, Vec<String>)>>,
pub status: Option<u16>,
Expand Down
14 changes: 6 additions & 8 deletions services/js-poc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use deno_core::url::Url;
use deno_core::v8::IsolateHandle;
use deno_core::{serde_v8, v8, JsRuntime};
use fn_sdk::connection::Connection;
use fn_sdk::header::{HttpResponse, TransportDetail};
use fn_sdk::header::TransportDetail;
use fn_sdk::http_util::{
respond_to_client,
respond_to_client_with_http_response,
Expand Down Expand Up @@ -234,26 +234,24 @@ async fn handle_request(
respond_to_client(connection, string.as_bytes(), is_http).await?;
} else {
// todo() unest this
let value = serde_v8::from_v8::<serde_json::Value>(scope, local)
.context("failed to deserialize response")?
.clone();
if is_http {
// Try to deserialize into the HTTP response object incase that is what they
// returned
if let Ok(http_response) = serde_v8::from_v8::<HttpResponse>(scope, local) {

if let Ok(http_response) = response_parser::parse(&value) {
// Its an http response so lets send over the headers provided
respond_to_client_with_http_response(connection, http_response).await?;
} else {
// Otherwise, send the data as json
let value = serde_v8::from_v8::<serde_json::Value>(scope, local)
.context("failed to deserialize response")?
.clone();
let res =
serde_json::to_string(&value).context("failed to encode json response")?;
respond_to_client(connection, res.as_bytes(), is_http).await?;
}
} else {
// Otherwise, send the data as json
let value = serde_v8::from_v8::<serde_json::Value>(scope, local)
.context("failed to deserialize response")?
.clone();
let res =
serde_json::to_string(&value).context("failed to encode json response")?;
respond_to_client(connection, res.as_bytes(), is_http).await?;
Expand Down

0 comments on commit aae531b

Please sign in to comment.