summaryrefslogtreecommitdiff
path: root/op_crates/fetch/lib.rs
diff options
context:
space:
mode:
authorcrowlKats <13135287+crowlKats@users.noreply.github.com>2021-03-17 22:33:29 +0100
committerGitHub <noreply@github.com>2021-03-17 17:33:29 -0400
commitb3fe85163f303a1592335b23c25554dd0e39a4c4 (patch)
tree03cadab59638e8508227a91a00887ad21a7c633a /op_crates/fetch/lib.rs
parentbd961c3bc305e90e760394d2f424c5360a67543b (diff)
refactor: use serde ops more (#9817)
Diffstat (limited to 'op_crates/fetch/lib.rs')
-rw-r--r--op_crates/fetch/lib.rs93
1 files changed, 42 insertions, 51 deletions
diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs
index 456358b83..c37670852 100644
--- a/op_crates/fetch/lib.rs
+++ b/op_crates/fetch/lib.rs
@@ -9,7 +9,6 @@ use deno_core::error::AnyError;
use deno_core::futures::Future;
use deno_core::futures::Stream;
use deno_core::futures::StreamExt;
-use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::url::Url;
@@ -103,27 +102,25 @@ pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts")
}
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct FetchArgs {
+ method: Option<String>,
+ url: String,
+ base_url: Option<String>,
+ headers: Vec<(String, String)>,
+ client_rid: Option<u32>,
+ has_body: bool,
+}
+
pub fn op_fetch<FP>(
state: &mut OpState,
- args: Value,
+ args: FetchArgs,
data: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError>
where
FP: FetchPermissions + 'static,
{
- #[derive(Deserialize)]
- #[serde(rename_all = "camelCase")]
- struct FetchArgs {
- method: Option<String>,
- url: String,
- base_url: Option<String>,
- headers: Vec<(String, String)>,
- client_rid: Option<u32>,
- has_body: bool,
- }
-
- let args: FetchArgs = serde_json::from_value(args)?;
-
let client = if let Some(rid) = args.client_rid {
let r = state
.resource_table
@@ -203,19 +200,17 @@ where
}))
}
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct FetchSendArgs {
+ rid: u32,
+}
+
pub async fn op_fetch_send(
state: Rc<RefCell<OpState>>,
- args: Value,
+ args: FetchSendArgs,
_data: BufVec,
) -> Result<Value, AnyError> {
- #[derive(Deserialize)]
- #[serde(rename_all = "camelCase")]
- struct Args {
- rid: u32,
- }
-
- let args: Args = serde_json::from_value(args)?;
-
let request = state
.borrow_mut()
.resource_table
@@ -273,18 +268,17 @@ pub async fn op_fetch_send(
}))
}
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct FetchRequestWriteArgs {
+ rid: u32,
+}
+
pub async fn op_fetch_request_write(
state: Rc<RefCell<OpState>>,
- args: Value,
+ args: FetchRequestWriteArgs,
data: BufVec,
) -> Result<Value, AnyError> {
- #[derive(Deserialize)]
- #[serde(rename_all = "camelCase")]
- struct Args {
- rid: u32,
- }
-
- let args: Args = serde_json::from_value(args)?;
let rid = args.rid;
let buf = match data.len() {
@@ -304,18 +298,17 @@ pub async fn op_fetch_request_write(
Ok(json!({}))
}
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct FetchResponseReadArgs {
+ rid: u32,
+}
+
pub async fn op_fetch_response_read(
state: Rc<RefCell<OpState>>,
- args: Value,
+ args: FetchResponseReadArgs,
data: BufVec,
) -> Result<Value, AnyError> {
- #[derive(Deserialize)]
- #[serde(rename_all = "camelCase")]
- struct Args {
- rid: u32,
- }
-
- let args: Args = serde_json::from_value(args)?;
let rid = args.rid;
if data.len() != 1 {
@@ -385,24 +378,22 @@ impl HttpClientResource {
}
}
+#[derive(Deserialize, Default, Debug)]
+#[serde(rename_all = "camelCase")]
+#[serde(default)]
+pub struct CreateHttpClientOptions {
+ ca_file: Option<String>,
+ ca_data: Option<String>,
+}
+
pub fn op_create_http_client<FP>(
state: &mut OpState,
- args: Value,
+ args: CreateHttpClientOptions,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError>
where
FP: FetchPermissions + 'static,
{
- #[derive(Deserialize, Default, Debug)]
- #[serde(rename_all = "camelCase")]
- #[serde(default)]
- struct CreateHttpClientOptions {
- ca_file: Option<String>,
- ca_data: Option<String>,
- }
-
- let args: CreateHttpClientOptions = serde_json::from_value(args)?;
-
if let Some(ca_file) = args.ca_file.clone() {
let permissions = state.borrow::<FP>();
permissions.check_read(&PathBuf::from(ca_file))?;