@@ -149,7 +149,7 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
149149 return Promise . resolve ( this . toString ( encoding ) ) ;
150150 }
151151 jsonResponse : any ;
152- toJSON ( encoding ?: any ) {
152+ toJSON < T > ( encoding ?: any ) {
153153 if ( ! this . data ) {
154154 return null ;
155155 }
@@ -165,48 +165,43 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
165165 this . jsonResponse = data ;
166166 return data ;
167167 }
168- try {
169- this . stringResponse = data ;
170- this . jsonResponse = parseJSON ( data ) ;
171- return this . jsonResponse ;
172- } catch ( err ) {
173- console . error ( 'HttpsResponse.toJSON' , err ) ;
174- return null ;
175- }
168+ this . stringResponse = data ;
169+ this . jsonResponse = parseJSON ( data ) ;
170+ return this . jsonResponse as T ;
171+
176172 }
177- toJSONAsync ( ) : Promise < any > {
178- return Promise . resolve ( this . toJSON ( ) ) ;
173+ toJSONAsync < T > ( ) {
174+ return Promise . resolve < T > ( this . toJSON ( ) ) ;
179175 }
180176 imageSource : ImageSource ;
181- toImage ( ) : Promise < ImageSource > {
177+ async toImage ( ) : Promise < ImageSource > {
182178 if ( ! this . data ) {
183179 return Promise . resolve ( null ) ;
184180 }
185181 if ( this . imageSource ) {
186182 return Promise . resolve ( this . imageSource ) ;
187183 }
188- return new Promise < ImageSource > ( ( resolve , reject ) => {
184+ const r = await new Promise < ImageSource > ( ( resolve , reject ) => {
189185 ( UIImage as any ) . tns_decodeImageWithDataCompletion ( this . data , ( image ) => {
190186 if ( image ) {
191187 resolve ( new ImageSource ( image ) ) ;
192188 } else {
193189 reject ( new Error ( 'Response content may not be converted to an Image' ) ) ;
194190 }
195191 } ) ;
196- } ) . then ( ( r ) => {
197- this . imageSource = r ;
198- return r ;
199192 } ) ;
193+ this . imageSource = r ;
194+ return r ;
200195 }
201196 file : File ;
202- toFile ( destinationFilePath ?: string ) : Promise < File > {
197+ async toFile ( destinationFilePath ?: string ) : Promise < File > {
203198 if ( ! this . data ) {
204199 return Promise . resolve ( null ) ;
205200 }
206201 if ( this . file ) {
207202 return Promise . resolve ( this . file ) ;
208203 }
209- return new Promise < File > ( ( resolve , reject ) => {
204+ const r = await new Promise < File > ( ( resolve , reject ) => {
210205 if ( ! destinationFilePath ) {
211206 destinationFilePath = getFilenameFromUrl ( this . url ) ;
212207 }
@@ -215,24 +210,23 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
215210 const file = File . fromPath ( destinationFilePath ) ;
216211
217212 const result = this . data . writeToFileAtomically ( destinationFilePath , true ) ;
218- if ( resolve ) {
213+ if ( result ) {
219214 resolve ( file ) ;
220215 } else {
221216 reject ( new Error ( `Cannot save file with path: ${ destinationFilePath } .` ) ) ;
222217 }
223218 } else {
224219 reject ( new Error ( `Cannot save file with path: ${ destinationFilePath } .` ) ) ;
225220 }
226- } ) . then ( ( f ) => {
227- this . file = f ;
228- return f ;
229- } ) ;
221+ } )
222+ this . file = r ;
223+ return r ;
230224 }
231225}
232226
233227function AFFailure ( resolve , reject , task : NSURLSessionDataTask , error : NSError , useLegacy : boolean , url ) {
234228 if ( error . code === - 999 ) {
235- return reject ( new Error ( error . localizedDescription ) ) ;
229+ return reject ( error ) ;
236230 }
237231 let getHeaders = ( ) => ( { } ) ;
238232 const sendi = {
@@ -265,6 +259,7 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError,
265259 return reject ( error . localizedDescription ) ;
266260 }
267261 const failure : any = {
262+ error,
268263 description : error . description ,
269264 reason : error . localizedDescription ,
270265 url : failingURL ? failingURL . description : url
@@ -277,6 +272,7 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError,
277272 resolve ( sendi ) ;
278273 } else {
279274 const response : any = {
275+ error,
280276 body : parsedData ,
281277 contentLength : sendi . contentLength ,
282278 description : error . description ,
0 commit comments