@@ -25,7 +25,7 @@ export const urlsession: Client<UrlsessionOptions> = {
25
25
description : "Foundation's URLSession request" ,
26
26
extname : '.swift' ,
27
27
} ,
28
- convert : ( { allHeaders, postData, fullUrl , method } , options ) => {
28
+ convert : ( { allHeaders, postData, uriObj , queryObj , method } , options ) => {
29
29
const opts = {
30
30
indent : ' ' ,
31
31
pretty : true ,
@@ -122,7 +122,36 @@ export const urlsession: Client<UrlsessionOptions> = {
122
122
123
123
blank ( ) ;
124
124
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
+
126
155
push ( `request.httpMethod = "${ method } "` ) ;
127
156
128
157
if ( req . hasHeaders ) {
@@ -136,7 +165,7 @@ export const urlsession: Client<UrlsessionOptions> = {
136
165
blank ( ) ;
137
166
// Retrieving the shared session will be less verbose than creating a new one.
138
167
139
- push ( 'let (data, response) = try await URLSession.shared.data(with : request)' ) ;
168
+ push ( 'let (data, response) = try await URLSession.shared.data(for : request)' ) ;
140
169
push ( 'print(String(decoding: data, as: UTF8.self))' ) ;
141
170
142
171
blank ( ) ;
0 commit comments