@@ -10,6 +10,7 @@ import {
1010 parseStringifiedDate ,
1111 parseStringifiedValue
1212} from '../utils/parsingUtils'
13+ import { ThrowHttpErrors } from '../types'
1314
1415const method = [
1516 'get' ,
@@ -23,6 +24,14 @@ const method = [
2324 'subscribe'
2425] as const
2526
27+ const shouldThrow = (
28+ error : EdenFetchError < number , unknown > ,
29+ throwHttpErrors ?: ThrowHttpErrors
30+ ) : boolean => {
31+ if ( typeof throwHttpErrors === 'function' ) return throwHttpErrors ( error )
32+ return throwHttpErrors === true
33+ }
34+
2635const locals = [ 'localhost' , '127.0.0.1' , '0.0.0.0' ]
2736
2837const isServer = typeof FileList === 'undefined'
@@ -213,12 +222,13 @@ const createProxy = (
213222) : any =>
214223 new Proxy ( ( ) => { } , {
215224 get ( _ , param : string ) : any {
216- return createProxy (
217- domain ,
218- config ,
219- param === 'index' ? paths : [ ...paths , param ] ,
220- elysia
225+ if (
226+ paths . length === 0 &&
227+ ( param === 'then' || param === 'catch' || param === 'finally' )
221228 )
229+ return undefined
230+
231+ return createProxy ( domain , config , [ ...paths , param ] , elysia )
222232 } ,
223233 apply ( _ , __ , [ body , options ] ) {
224234 if (
@@ -251,29 +261,30 @@ const createProxy = (
251261
252262 let q = ''
253263 if ( query ) {
254- const append = ( key : string , value : string ) => {
255- q +=
264+ const append = ( key : string , value : unknown ) => {
265+ // Explicitly exclude null and undefined values from url encoding
266+ // to prevent parsing string "null" / string "undefined"
267+ if ( value === undefined || value === null ) return
268+
269+ if ( value instanceof Date ) value = value . toISOString ( )
270+
271+ q +=
256272 ( q ? '&' : '?' ) +
257273 `${ encodeURIComponent ( key ) } =${ encodeURIComponent (
258- value
274+ typeof value === 'object'
275+ ? JSON . stringify ( value )
276+ : value + ''
259277 ) } `
260278 }
261279
262280 for ( const [ key , value ] of Object . entries ( query ) ) {
263281 if ( Array . isArray ( value ) ) {
264282 for ( const v of value ) append ( key , v )
265- continue
266- }
267283
268- // Explicitly exclude null and undefined values from url encoding
269- // to prevent parsing string "null" / string "undefined"
270- if ( value === undefined || value === null ) continue
271-
272- if ( typeof value === 'object' ) {
273- append ( key , JSON . stringify ( value ) )
274284 continue
275285 }
276- append ( key , `${ value } ` )
286+
287+ append ( key , value )
277288 }
278289 }
279290
@@ -322,6 +333,14 @@ const createProxy = (
322333 ? body . fetch
323334 : options ?. fetch
324335
336+ // Per-request throwHttpErrors overrides config
337+ const requestThrowHttpErrors =
338+ isGetOrHead && typeof body === 'object'
339+ ? body . throwHttpErrors
340+ : options ?. throwHttpErrors
341+ const resolvedThrowHttpErrors =
342+ requestThrowHttpErrors ?? config . throwHttpErrors
343+
325344 fetchInit = {
326345 ...fetchInit ,
327346 ...fetchOpts
@@ -498,7 +517,8 @@ const createProxy = (
498517 }
499518 }
500519
501- if ( options ?. headers ?. [ 'content-type' ] )
520+ if ( options ?. headers ?. [ 'content-type' ] )
521+ // @ts -ignore
502522 fetchInit . headers [ 'content-type' ] =
503523 options ?. headers [ 'content-type' ]
504524
@@ -511,9 +531,12 @@ const createProxy = (
511531 new Request ( url , fetchInit )
512532 ) ?? fetcher ! ( url , fetchInit ) )
513533 } catch ( err ) {
534+ const error = new EdenFetchError ( 503 , err )
535+ if ( shouldThrow ( error , resolvedThrowHttpErrors ) )
536+ throw error
514537 return {
515538 data : null ,
516- error : new EdenFetchError ( 503 , err ) ,
539+ error,
517540 response : undefined ,
518541 status : 503 ,
519542 headers : undefined
@@ -574,7 +597,8 @@ const createProxy = (
574597
575598 return v
576599 } )
577- break
600+ break
601+
578602 case 'application/octet-stream' :
579603 data = await response . arrayBuffer ( )
580604 break
@@ -600,6 +624,8 @@ const createProxy = (
600624
601625 if ( response . status >= 300 || response . status < 200 ) {
602626 error = new EdenFetchError ( response . status , data )
627+ if ( shouldThrow ( error , resolvedThrowHttpErrors ) )
628+ throw error
603629 data = null
604630 }
605631
@@ -626,11 +652,12 @@ const createProxy = (
626652 } ) as any
627653
628654export const treaty = <
629- const App extends Elysia < any , any , any , any , any , any , any >
655+ const App extends Elysia < any , any , any , any , any , any , any > ,
656+ const Head extends { } = { }
630657> (
631658 domain : string | App ,
632- config : Treaty . Config = { }
633- ) : Treaty . Create < App > => {
659+ config : Treaty . Config < Head > = { }
660+ ) : Treaty . Create < App , Head > => {
634661 if ( typeof domain === 'string' ) {
635662 if ( ! config . keepDomain ) {
636663 if ( ! domain . includes ( '://' ) )
0 commit comments