1
1
use std:: { borrow:: Cow , collections:: HashMap , str:: from_utf8} ;
2
2
3
- use anyhow:: { anyhow, bail} ;
3
+ use anyhow:: { anyhow, bail, Context } ;
4
4
use extism_pdk:: extism:: load_input;
5
5
use extism_pdk:: * ;
6
6
use quickjs_wasm_rs:: { JSContextRef , JSError , JSValue , JSValueRef } ;
@@ -174,7 +174,7 @@ fn build_http_object(context: &JSContextRef) -> anyhow::Result<JSValueRef> {
174
174
175
175
let url = req
176
176
. get_property ( "url" )
177
- . expect ( "Http Request should have url property" ) ;
177
+ . context ( "Http Request should have url property" ) ? ;
178
178
179
179
let method = req. get_property ( "method" ) ;
180
180
let method_str = match method {
@@ -184,35 +184,35 @@ fn build_http_object(context: &JSContextRef) -> anyhow::Result<JSValueRef> {
184
184
185
185
let mut http_req = HttpRequest :: new ( url. as_str ( ) ?) . with_method ( method_str) ;
186
186
187
- let headers = req. get_property ( "headers" ) ;
188
- if let Ok ( headers) = headers {
187
+ let headers = req. get_property ( "headers" ) ? ;
188
+ if ! headers. is_null_or_undefined ( ) {
189
189
if !headers. is_object ( ) {
190
190
bail ! ( "Expected headers to be an object" ) ;
191
191
}
192
- let mut header_values = headers. properties ( ) ?;
193
- loop {
194
- let key = header_values. next_key ( ) ?;
195
- match key {
196
- None => break ,
197
- Some ( key) => {
198
- let key = key. as_str ( ) ?;
199
- let value = header_values. next_value ( ) ?;
200
- let value = value. as_str ( ) ?;
201
- http_req. headers . insert ( key. to_string ( ) , value. to_string ( ) ) ;
192
+ if !headers. is_object ( ) {
193
+ let mut header_values = headers. properties ( ) ?;
194
+ loop {
195
+ let key = header_values. next_key ( ) ?;
196
+ match key {
197
+ None => break ,
198
+ Some ( key) => {
199
+ let key = key. as_str ( ) ?;
200
+ let value = header_values. next_value ( ) ?;
201
+ let value = value. as_str ( ) ?;
202
+ http_req. headers . insert ( key. to_string ( ) , value. to_string ( ) ) ;
203
+ }
202
204
}
203
205
}
204
206
}
205
207
}
206
208
207
- let body_arg = args. get ( 1 ) . ok_or ( ctx . null_value ( ) ? ) . unwrap ( ) ;
209
+ let body_arg = args. get ( 1 ) ;
208
210
let mut http_body: Option < String > = None ;
209
- if !body_arg. is_null_or_undefined ( ) {
210
- let body_arg = body_arg. as_str ( ) ?;
211
- http_body = Some ( body_arg. to_string ( ) ) ;
211
+ if let Some ( body) = body_arg {
212
+ http_body = Some ( body. as_str ( ) ?. to_string ( ) ) ;
212
213
}
213
214
214
215
let resp = http:: request :: < String > ( & http_req, http_body) ?;
215
-
216
216
let body = resp. body ( ) ;
217
217
let body = from_utf8 ( & body) ?;
218
218
0 commit comments