Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 60 additions & 21 deletions worker-kv/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use js_sys::{ArrayBuffer, Function, Object, Promise, Uint8Array};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
use serde_wasm_bindgen::Serializer;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;

use crate::{KvError, ListResponse};

/// A builder to configure put requests.
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone)]
#[must_use = "PutOptionsBuilder does nothing until you 'execute' it"]
pub struct PutOptionsBuilder {
#[serde(skip)]
pub(crate) this: Object,
#[serde(skip)]
pub(crate) put_function: Function,
#[serde(skip)]
pub(crate) name: JsValue,
#[serde(skip)]
pub(crate) value: JsValue,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) expiration: Option<u64>,
pub(crate) expiration_ttl: Option<u64>,
pub(crate) metadata: Option<Value>,
}

#[derive(Serialize)]
struct PutOptions {
#[serde(skip_serializing_if = "Option::is_none")]
expiration: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "expirationTtl")]
pub(crate) expiration_ttl: Option<u64>,
expiration_ttl: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) metadata: Option<Value>,
metadata: Option<Value>,
}

impl PutOptionsBuilder {
Expand All @@ -46,7 +50,15 @@ impl PutOptionsBuilder {
}
/// Puts the value in the kv store.
pub async fn execute(self) -> Result<(), KvError> {
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
let ser = Serializer::json_compatible();
let options_object = PutOptions {
expiration: self.expiration,
expiration_ttl: self.expiration_ttl,
metadata: self.metadata,
}
.serialize(&ser)
.map_err(JsValue::from)?;

let promise: Promise = self
.put_function
.call3(&self.this, &self.name, &self.value, &options_object)?
Expand All @@ -59,13 +71,18 @@ impl PutOptionsBuilder {
}

/// A builder to configure list requests.
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone)]
#[must_use = "ListOptionsBuilder does nothing until you 'execute' it"]
pub struct ListOptionsBuilder {
#[serde(skip)]
pub(crate) this: Object,
#[serde(skip)]
pub(crate) list_function: Function,
pub(crate) limit: Option<u64>,
pub(crate) cursor: Option<String>,
pub(crate) prefix: Option<String>,
}

#[derive(Serialize)]
struct ListOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) limit: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -93,7 +110,15 @@ impl ListOptionsBuilder {
}
/// Lists the key value pairs in the kv store.
pub async fn execute(self) -> Result<ListResponse, KvError> {
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
let ser = Serializer::json_compatible();
let options_object = ListOptions {
limit: self.limit,
cursor: self.cursor,
prefix: self.prefix,
}
.serialize(&ser)
.map_err(JsValue::from)?;

let promise: Promise = self
.list_function
.call1(&self.this, &options_object)?
Expand All @@ -106,17 +131,19 @@ impl ListOptionsBuilder {
}

/// A builder to configure get requests.
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone)]
#[must_use = "GetOptionsBuilder does nothing until you 'get' it"]
pub struct GetOptionsBuilder {
#[serde(skip)]
pub(crate) this: Object,
#[serde(skip)]
pub(crate) get_function: Function,
#[serde(skip)]
pub(crate) get_with_meta_function: Function,
#[serde(skip)]
pub(crate) name: JsValue,
pub(crate) cache_ttl: Option<u64>,
pub(crate) value_type: Option<GetValueType>,
}

#[derive(Serialize)]
struct GetOptions {
#[serde(rename = "cacheTtl", skip_serializing_if = "Option::is_none")]
pub(crate) cache_ttl: Option<u64>,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
Expand All @@ -141,8 +168,19 @@ impl GetOptionsBuilder {
self
}

fn options(&self) -> Result<JsValue, KvError> {
let ser = Serializer::json_compatible();
Ok(GetOptions {
cache_ttl: self.cache_ttl,
value_type: self.value_type,
}
.serialize(&ser)
.map_err(JsValue::from)?)
}

async fn get(self) -> Result<JsValue, KvError> {
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
let options_object = self.options()?;

let promise: Promise = self
.get_function
.call2(&self.this, &self.name, &options_object)?
Expand Down Expand Up @@ -185,7 +223,8 @@ impl GetOptionsBuilder {
where
M: DeserializeOwned,
{
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
let options_object = self.options()?;

let promise: Promise = self
.get_with_meta_function
.call2(&self.this, &self.name, &options_object)?
Expand Down Expand Up @@ -257,7 +296,7 @@ impl GetOptionsBuilder {
}
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Copy)]
#[serde(rename_all = "camelCase")]
pub(crate) enum GetValueType {
Text,
Expand Down
1 change: 0 additions & 1 deletion worker-kv/tests/.gitignore

This file was deleted.

97 changes: 0 additions & 97 deletions worker-kv/tests/integration.rs

This file was deleted.

Loading