@@ -58,15 +58,6 @@ mod ssr_http_provider {
58
58
. await ?;
59
59
60
60
Ok ( result)
61
-
62
- // if let Ok(response) = serde_json::from_value::<R>(result.clone())
63
- // { Ok(response)
64
- // } else {
65
- // match serde_json::from_value::<RpcError>(result) {
66
- // Ok(error) => Err(error.into()),
67
- // Err(error) => Err(ClientError::Other(error.to_string())),
68
- // }
69
- // }
70
61
}
71
62
}
72
63
@@ -107,7 +98,6 @@ mod ssr_http_provider {
107
98
108
99
#[ cfg( not( feature = "ssr" ) ) ]
109
100
mod wasm_http_provider {
110
- #![ allow( unsafe_code) ]
111
101
112
102
use std:: pin:: Pin ;
113
103
use std:: task:: Context ;
@@ -118,36 +108,20 @@ mod wasm_http_provider {
118
108
use pin_project:: pinned_drop;
119
109
use send_wrapper:: SendWrapper ;
120
110
use wasm_bindgen:: prelude:: * ;
121
- use wasm_bindgen_futures:: JsFuture ;
122
111
use web_sys:: AbortController ;
123
- use web_sys:: Headers ;
124
- use web_sys:: Request ;
125
- use web_sys:: RequestInit ;
126
- use web_sys:: Response ;
127
112
128
113
use super :: * ;
129
114
use crate :: ClientError ;
130
115
131
- #[ wasm_bindgen]
132
- extern "C" {
133
- // Create a separate binding for `fetch` as a global, rather than using the
134
- // existing Window/WorkerGlobalScope bindings defined by web_sys, for
135
- // greater efficiency.
136
- //
137
- // https://github.com/rustwasm/wasm-bindgen/discussions/3863
138
- #[ wasm_bindgen( js_name = "fetch" ) ]
139
- fn fetch_with_request ( request : & Request ) -> js_sys:: Promise ;
140
- }
141
-
142
116
#[ pin_project( PinnedDrop ) ]
143
- struct AbortableRequest < F : Future < Output = Result < JsValue , JsValue > > > {
117
+ struct AbortableRequest < F : Future < Output = Result < gloo_net :: http :: Response , gloo_net :: Error > > > {
144
118
#[ pin]
145
119
fut : F ,
146
120
controller : AbortController ,
147
121
pending : bool ,
148
122
}
149
123
150
- impl < F : Future < Output = Result < JsValue , JsValue > > > AbortableRequest < F > {
124
+ impl < F : Future < Output = Result < gloo_net :: http :: Response , gloo_net :: Error > > > AbortableRequest < F > {
151
125
fn new ( fut : F , controller : AbortController ) -> Self {
152
126
Self {
153
127
fut,
@@ -157,8 +131,10 @@ mod wasm_http_provider {
157
131
}
158
132
}
159
133
160
- impl < F : Future < Output = Result < JsValue , JsValue > > > Future for AbortableRequest < F > {
161
- type Output = Result < JsValue , JsValue > ;
134
+ impl < F : Future < Output = Result < gloo_net:: http:: Response , gloo_net:: Error > > > Future
135
+ for AbortableRequest < F >
136
+ {
137
+ type Output = Result < gloo_net:: http:: Response , gloo_net:: Error > ;
162
138
163
139
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
164
140
let mut this = self . project ( ) ;
@@ -175,7 +151,9 @@ mod wasm_http_provider {
175
151
}
176
152
177
153
#[ pinned_drop]
178
- impl < F : Future < Output = Result < JsValue , JsValue > > > PinnedDrop for AbortableRequest < F > {
154
+ impl < F : Future < Output = Result < gloo_net:: http:: Response , gloo_net:: Error > > > PinnedDrop
155
+ for AbortableRequest < F >
156
+ {
179
157
fn drop ( self : Pin < & mut Self > ) {
180
158
if self . pending {
181
159
// only abort the fetch if it is still pending.
@@ -203,23 +181,12 @@ mod wasm_http_provider {
203
181
let future = async move {
204
182
let controller = AbortController :: new ( ) . unwrap_throw ( ) ;
205
183
let signal = controller. signal ( ) ;
206
- let headers = Headers :: new ( ) ?;
207
- let options = RequestInit :: new ( ) ;
208
- let json = serde_json:: to_string ( & client_request) . unwrap_throw ( ) ;
209
-
210
- headers. set ( "Content-Type" , "application/json" ) ?;
211
- options. set_signal ( Some ( & signal) ) ;
212
- options. set_headers ( & headers. into ( ) ) ;
213
- options. set_body ( & json. into ( ) ) ;
214
- let request = Request :: new_with_str_and_init ( & self . 0 , & options) ?;
215
- let fetch_promise = fetch_with_request ( & request) ;
216
- let response_value =
217
- AbortableRequest :: new ( JsFuture :: from ( fetch_promise) , controller) . await ?;
218
- let response = response_value. dyn_into :: < Response > ( ) ?;
219
-
220
- let json_promise = response. json ( ) ?;
221
- let json_value = JsFuture :: from ( json_promise) . await ?;
222
- let value: Value = serde_wasm_bindgen:: from_value ( json_value) ?;
184
+ let request = gloo_net:: http:: Request :: post ( & self . 0 )
185
+ . abort_signal ( Some ( & signal) )
186
+ . json ( & client_request) ?;
187
+ let response = AbortableRequest :: new ( request. send ( ) , controller) . await ?;
188
+ let value = response. json ( ) . await ?;
189
+
223
190
Ok :: < Value , ClientError > ( value)
224
191
} ;
225
192
@@ -240,6 +207,11 @@ mod wasm_http_provider {
240
207
Self :: Other ( value. to_string ( ) )
241
208
}
242
209
}
210
+ impl From < gloo_net:: Error > for ClientError {
211
+ fn from ( value : gloo_net:: Error ) -> Self {
212
+ Self :: Other ( value. to_string ( ) )
213
+ }
214
+ }
243
215
244
216
impl From < JsValue > for ClientError {
245
217
fn from ( error : JsValue ) -> Self {
0 commit comments