1
1
use js_sys:: { ArrayBuffer , Function , Object , Promise , Uint8Array } ;
2
2
use serde:: { de:: DeserializeOwned , Serialize } ;
3
3
use serde_json:: Value ;
4
+ use serde_wasm_bindgen:: Serializer ;
4
5
use wasm_bindgen:: { JsCast , JsValue } ;
5
6
use wasm_bindgen_futures:: JsFuture ;
6
7
7
8
use crate :: { KvError , ListResponse } ;
8
9
9
10
/// A builder to configure put requests.
10
- #[ derive( Debug , Clone , Serialize ) ]
11
+ #[ derive( Debug , Clone ) ]
11
12
#[ must_use = "PutOptionsBuilder does nothing until you 'execute' it" ]
12
13
pub struct PutOptionsBuilder {
13
- #[ serde( skip) ]
14
14
pub ( crate ) this : Object ,
15
- #[ serde( skip) ]
16
15
pub ( crate ) put_function : Function ,
17
- #[ serde( skip) ]
18
16
pub ( crate ) name : JsValue ,
19
- #[ serde( skip) ]
20
17
pub ( crate ) value : JsValue ,
21
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
22
18
pub ( crate ) expiration : Option < u64 > ,
19
+ pub ( crate ) expiration_ttl : Option < u64 > ,
20
+ pub ( crate ) metadata : Option < Value > ,
21
+ }
22
+
23
+ #[ derive( Serialize ) ]
24
+ struct PutOptions {
25
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
26
+ expiration : Option < u64 > ,
23
27
#[ serde( skip_serializing_if = "Option::is_none" ) ]
24
28
#[ serde( rename = "expirationTtl" ) ]
25
- pub ( crate ) expiration_ttl : Option < u64 > ,
29
+ expiration_ttl : Option < u64 > ,
26
30
#[ serde( skip_serializing_if = "Option::is_none" ) ]
27
- pub ( crate ) metadata : Option < Value > ,
31
+ metadata : Option < Value > ,
28
32
}
29
33
30
34
impl PutOptionsBuilder {
@@ -46,7 +50,15 @@ impl PutOptionsBuilder {
46
50
}
47
51
/// Puts the value in the kv store.
48
52
pub async fn execute ( self ) -> Result < ( ) , KvError > {
49
- let options_object = serde_wasm_bindgen:: to_value ( & self ) . map_err ( JsValue :: from) ?;
53
+ let ser = Serializer :: json_compatible ( ) ;
54
+ let options_object = PutOptions {
55
+ expiration : self . expiration ,
56
+ expiration_ttl : self . expiration_ttl ,
57
+ metadata : self . metadata ,
58
+ }
59
+ . serialize ( & ser)
60
+ . map_err ( JsValue :: from) ?;
61
+
50
62
let promise: Promise = self
51
63
. put_function
52
64
. call3 ( & self . this , & self . name , & self . value , & options_object) ?
@@ -59,13 +71,18 @@ impl PutOptionsBuilder {
59
71
}
60
72
61
73
/// A builder to configure list requests.
62
- #[ derive( Debug , Clone , Serialize ) ]
74
+ #[ derive( Debug , Clone ) ]
63
75
#[ must_use = "ListOptionsBuilder does nothing until you 'execute' it" ]
64
76
pub struct ListOptionsBuilder {
65
- #[ serde( skip) ]
66
77
pub ( crate ) this : Object ,
67
- #[ serde( skip) ]
68
78
pub ( crate ) list_function : Function ,
79
+ pub ( crate ) limit : Option < u64 > ,
80
+ pub ( crate ) cursor : Option < String > ,
81
+ pub ( crate ) prefix : Option < String > ,
82
+ }
83
+
84
+ #[ derive( Serialize ) ]
85
+ struct ListOptions {
69
86
#[ serde( skip_serializing_if = "Option::is_none" ) ]
70
87
pub ( crate ) limit : Option < u64 > ,
71
88
#[ serde( skip_serializing_if = "Option::is_none" ) ]
@@ -93,7 +110,15 @@ impl ListOptionsBuilder {
93
110
}
94
111
/// Lists the key value pairs in the kv store.
95
112
pub async fn execute ( self ) -> Result < ListResponse , KvError > {
96
- let options_object = serde_wasm_bindgen:: to_value ( & self ) . map_err ( JsValue :: from) ?;
113
+ let ser = Serializer :: json_compatible ( ) ;
114
+ let options_object = ListOptions {
115
+ limit : self . limit ,
116
+ cursor : self . cursor ,
117
+ prefix : self . prefix ,
118
+ }
119
+ . serialize ( & ser)
120
+ . map_err ( JsValue :: from) ?;
121
+
97
122
let promise: Promise = self
98
123
. list_function
99
124
. call1 ( & self . this , & options_object) ?
@@ -106,17 +131,19 @@ impl ListOptionsBuilder {
106
131
}
107
132
108
133
/// A builder to configure get requests.
109
- #[ derive( Debug , Clone , Serialize ) ]
134
+ #[ derive( Debug , Clone ) ]
110
135
#[ must_use = "GetOptionsBuilder does nothing until you 'get' it" ]
111
136
pub struct GetOptionsBuilder {
112
- #[ serde( skip) ]
113
137
pub ( crate ) this : Object ,
114
- #[ serde( skip) ]
115
138
pub ( crate ) get_function : Function ,
116
- #[ serde( skip) ]
117
139
pub ( crate ) get_with_meta_function : Function ,
118
- #[ serde( skip) ]
119
140
pub ( crate ) name : JsValue ,
141
+ pub ( crate ) cache_ttl : Option < u64 > ,
142
+ pub ( crate ) value_type : Option < GetValueType > ,
143
+ }
144
+
145
+ #[ derive( Serialize ) ]
146
+ struct GetOptions {
120
147
#[ serde( rename = "cacheTtl" , skip_serializing_if = "Option::is_none" ) ]
121
148
pub ( crate ) cache_ttl : Option < u64 > ,
122
149
#[ serde( rename = "type" , skip_serializing_if = "Option::is_none" ) ]
@@ -141,8 +168,19 @@ impl GetOptionsBuilder {
141
168
self
142
169
}
143
170
171
+ fn options ( & self ) -> Result < JsValue , KvError > {
172
+ let ser = Serializer :: json_compatible ( ) ;
173
+ Ok ( GetOptions {
174
+ cache_ttl : self . cache_ttl ,
175
+ value_type : self . value_type ,
176
+ }
177
+ . serialize ( & ser)
178
+ . map_err ( JsValue :: from) ?)
179
+ }
180
+
144
181
async fn get ( self ) -> Result < JsValue , KvError > {
145
- let options_object = serde_wasm_bindgen:: to_value ( & self ) . map_err ( JsValue :: from) ?;
182
+ let options_object = self . options ( ) ?;
183
+
146
184
let promise: Promise = self
147
185
. get_function
148
186
. call2 ( & self . this , & self . name , & options_object) ?
@@ -185,7 +223,8 @@ impl GetOptionsBuilder {
185
223
where
186
224
M : DeserializeOwned ,
187
225
{
188
- let options_object = serde_wasm_bindgen:: to_value ( & self ) . map_err ( JsValue :: from) ?;
226
+ let options_object = self . options ( ) ?;
227
+
189
228
let promise: Promise = self
190
229
. get_with_meta_function
191
230
. call2 ( & self . this , & self . name , & options_object) ?
@@ -257,7 +296,7 @@ impl GetOptionsBuilder {
257
296
}
258
297
}
259
298
260
- #[ derive( Debug , Clone , Serialize ) ]
299
+ #[ derive( Debug , Clone , Serialize , Copy ) ]
261
300
#[ serde( rename_all = "camelCase" ) ]
262
301
pub ( crate ) enum GetValueType {
263
302
Text ,
0 commit comments