@@ -11,6 +11,7 @@ import {
11
11
type RequestParamMetadata ,
12
12
} from "./decorators" ;
13
13
import { HttpClient } from "./types/http-client.interface" ;
14
+ import { URLBuilder } from "./utils/url-builder" ;
14
15
15
16
@Injectable ( )
16
17
export class NodeFetchInjector implements OnModuleInit {
@@ -44,53 +45,29 @@ export class NodeFetchInjector implements OnModuleInit {
44
45
const pathMetadata = getMetadata < PathVariableMetadata > (
45
46
PATH_VARIABLE_METADATA
46
47
) ;
47
-
48
48
const requestParamMetadata = getMetadata < RequestParamMetadata > (
49
49
REQUEST_PARAM_METADATA
50
50
) ;
51
51
52
52
wrapper . instance [ methodName ] = async ( ...args : any [ ] ) => {
53
- const url = [ ...( pathMetadata ?. entries ( ) ?? [ ] ) ] . reduce (
54
- ( url , [ index , value ] ) =>
55
- url . replace ( new RegExp ( `{${ value } }` , "g" ) , args [ index ] ) ,
56
- `${ baseUrl } ${ httpExchangeMetadata . url } `
57
- ) ;
58
- const searchParams = new URLSearchParams ( ) ;
59
- requestParamMetadata ?. forEach ( ( queryParamKey , paramIndex ) => {
60
- if ( typeof queryParamKey === "undefined" ) {
61
- this . toArray ( args [ paramIndex ] ) . forEach ( ( [ key , value ] ) => {
62
- searchParams . set ( key , `${ value ?. toString ( ) ?? "" } ` ) ;
63
- } ) ;
64
-
65
- return ;
53
+ const urlBuilder = new URLBuilder (
54
+ baseUrl ,
55
+ httpExchangeMetadata . url ,
56
+ args ,
57
+ {
58
+ pathParam : pathMetadata ,
59
+ queryParam : requestParamMetadata ,
66
60
}
67
- searchParams . set ( queryParamKey , args [ paramIndex ] ) ;
68
- } ) ;
69
-
70
- const request = new Request (
71
- [ url , searchParams . toString ( ) ] . filter ( Boolean ) . join ( "?" )
72
61
) ;
73
62
74
63
return await this . httpClient
75
- . request ( request )
64
+ . request ( new Request ( urlBuilder . build ( ) ) )
76
65
. then ( async ( response ) => await response . json ( ) ) ;
77
66
} ;
78
67
} ) ;
79
68
} ) ;
80
69
}
81
70
82
- private toArray < T > ( value : T ) : Array < [ string , unknown ] > {
83
- if ( value instanceof Map ) {
84
- return [ ...value . values ( ) ] ;
85
- }
86
-
87
- if ( typeof value === "object" && value !== null ) {
88
- return Object . entries ( value ) ;
89
- }
90
-
91
- return [ ] ;
92
- }
93
-
94
71
private getHttpProviders ( ) : InstanceWrapper [ ] {
95
72
return this . discoveryService . getProviders ( ) . filter ( ( wrapper ) => {
96
73
const metadata = Reflect . getMetadata (
0 commit comments