@@ -11,9 +11,8 @@ export class APIManager {
1111 }
1212
1313 async request < T > ( path : string , options : APIRequestInit = { } ) {
14- const params = new URLSearchParams ( options . params ) . toString ( ) ;
14+ const { init , params } = this . parseOptions ( options ) ;
1515 const url = new URL ( `${ path } ?${ params } ` , this . baseUrl ) ;
16- const init = this . parseOptions ( options ) ;
1716
1817 const response = await fetch ( url , init ) ;
1918 const data : APIPayload < T > = await response . json ( ) ;
@@ -25,6 +24,15 @@ export class APIManager {
2524 }
2625
2726 private parseOptions ( options : APIRequestInit ) {
27+ const paramsObject =
28+ options . params &&
29+ Object . fromEntries (
30+ Object . entries ( options . params )
31+ . filter ( ( [ , value ] ) => Boolean ( value ) )
32+ . map ( ( [ key , value ] ) => [ key , String ( value ) ] ) ,
33+ ) ;
34+ const params = new URLSearchParams ( paramsObject ) . toString ( ) ;
35+
2836 const { method, headers, body, ...rest } = options ;
2937 const init : RequestInit = {
3038 method : method || "GET" ,
@@ -36,6 +44,6 @@ export class APIManager {
3644 init . body = body instanceof FormData ? body : JSON . stringify ( body ) ;
3745 }
3846
39- return init ;
47+ return { init, params } ;
4048 }
4149}
0 commit comments