@@ -25,7 +25,7 @@ export const urlsession: Client<UrlsessionOptions> = {
2525 description : "Foundation's URLSession request" ,
2626 extname : '.swift' ,
2727 } ,
28- convert : ( { allHeaders, postData, fullUrl , method } , options ) => {
28+ convert : ( { allHeaders, postData, uriObj , queryObj , method } , options ) => {
2929 const opts = {
3030 indent : ' ' ,
3131 pretty : true ,
@@ -122,7 +122,36 @@ export const urlsession: Client<UrlsessionOptions> = {
122122
123123 blank ( ) ;
124124
125- push ( `var request = URLRequest(url: URL(string: "${ fullUrl } ")!)` ) ;
125+ push ( `let url = URL(string: "${ uriObj . href } ")!` ) ;
126+
127+ const queries = queryObj ? Object . entries ( queryObj ) : [ ] ;
128+ if ( queries . length < 1 ) {
129+ push ( 'var request = URLRequest(url: url)' ) ;
130+ } else {
131+ push ( 'var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!' ) ;
132+ push ( 'let queryItems: [URLQueryItem] = [' ) ;
133+
134+ queries . forEach ( query => {
135+ const key = query [ 0 ] ;
136+ const value = query [ 1 ] ;
137+ switch ( Object . prototype . toString . call ( value ) ) {
138+ case '[object String]' :
139+ push ( `${ opts . indent } URLQueryItem(name: "${ key } ", value: "${ value } "),` ) ;
140+ break ;
141+ case '[object Array]' :
142+ value . forEach ( val => {
143+ push ( `${ opts . indent } URLQueryItem(name: "${ key } ", value: "${ val } "),` ) ;
144+ } ) ;
145+ break ;
146+ }
147+ } ) ;
148+ push ( ']' ) ;
149+ push ( 'components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems' ) ;
150+
151+ blank ( ) ;
152+ push ( 'var request = URLRequest(url: components.url!)' ) ;
153+ }
154+
126155 push ( `request.httpMethod = "${ method } "` ) ;
127156
128157 if ( req . hasHeaders ) {
@@ -136,7 +165,7 @@ export const urlsession: Client<UrlsessionOptions> = {
136165 blank ( ) ;
137166 // Retrieving the shared session will be less verbose than creating a new one.
138167
139- push ( 'let (data, response) = try await URLSession.shared.data(with : request)' ) ;
168+ push ( 'let (data, response) = try await URLSession.shared.data(for : request)' ) ;
140169 push ( 'print(String(decoding: data, as: UTF8.self))' ) ;
141170
142171 blank ( ) ;
0 commit comments