11import { File , ImageSource , Utils } from '@nativescript/core' ;
2- import { dispatchToMainThread } from '@nativescript/core/utils' ;
32import { CacheOptions , HttpsFormDataParam , HttpsRequest , HttpsRequestOptions , HttpsResponse , HttpsSSLPinningOptions , HttpsResponseLegacy as IHttpsResponseLegacy } from '.' ;
43import { getFilenameFromUrl , parseJSON } from './request.common' ;
54export { addInterceptor , addNetworkInterceptor } from './request.common' ;
@@ -19,6 +18,10 @@ export function clearCache() {
1918 NSURLCache . sharedURLCache . removeAllCachedResponses ( ) ;
2019}
2120
21+ export function removeCachedResponse ( url : string ) {
22+ NSURLCache . sharedURLCache . removeCachedResponseForRequest ( createNSRequest ( url ) ) ;
23+ }
24+
2225interface Ipolicies {
2326 def : AFSecurityPolicy ;
2427 secured : boolean ;
@@ -97,6 +100,10 @@ function getData(data) {
97100 return content ;
98101}
99102
103+ function createNSRequest ( url : string ) : NSMutableURLRequest {
104+ return NSMutableURLRequest . alloc ( ) . initWithURL ( NSURL . URLWithString ( url ) ) ;
105+ }
106+
100107class HttpsResponseLegacy implements IHttpsResponseLegacy {
101108 // private callback?: com.nativescript.https.OkhttpResponse.OkHttpResponseAsyncCallback;
102109 constructor ( private data : NSDictionary < string , any > & NSData & NSArray < any > , public contentLength , private url : string ) { }
@@ -168,7 +175,6 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
168175 this . stringResponse = data ;
169176 this . jsonResponse = parseJSON ( data ) ;
170177 return this . jsonResponse as T ;
171-
172178 }
173179 toJSONAsync < T > ( ) {
174180 return Promise . resolve < T > ( this . toJSON ( ) ) ;
@@ -370,10 +376,9 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
370376
371377 const progress = opts . onProgress
372378 ? ( progress : NSProgress ) => {
373- dispatchToMainThread ( ( ) => {
379+ Utils . dispatchToMainThread ( ( ) => {
374380 opts . onProgress ( progress . completedUnitCount , progress . totalUnitCount ) ;
375- } )
376-
381+ } ) ;
377382 }
378383 : null ;
379384 let task : NSURLSessionDataTask ;
@@ -463,13 +468,13 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
463468 }
464469 } else if ( opts . method === 'PUT' ) {
465470 if ( opts . body instanceof File ) {
466- const request = NSMutableURLRequest . alloc ( ) . initWithURL ( NSURL . URLWithString ( opts . url ) ) ;
471+ const request = createNSRequest ( opts . url ) ;
467472 request . HTTPMethod = opts . method ;
468- Object . keys ( heads ) . forEach ( k => {
473+ Object . keys ( heads ) . forEach ( k => {
469474 request . setValueForHTTPHeaderField ( heads [ k ] , k ) ;
470475 } ) ;
471476 task = manager . uploadTaskWithRequestFromFileProgressCompletionHandler ( request , NSURL . fileURLWithPath ( opts . body . path ) , progress , ( response : NSURLResponse , responseObject : any , error : NSError ) => {
472- if ( error ) {
477+ if ( error ) {
473478 failure ( task , error ) ;
474479 } else {
475480 success ( task , responseObject ) ;
@@ -486,13 +491,13 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
486491 } else {
487492 data = NSString . stringWithString ( JSON . stringify ( opts . body ) ) . dataUsingEncoding ( NSUTF8StringEncoding ) ;
488493 }
489- const request = NSMutableURLRequest . alloc ( ) . initWithURL ( NSURL . URLWithString ( opts . url ) ) ;
494+ const request = createNSRequest ( opts . url ) ;
490495 request . HTTPMethod = opts . method ;
491- Object . keys ( heads ) . forEach ( k => {
496+ Object . keys ( heads ) . forEach ( k => {
492497 request . setValueForHTTPHeaderField ( heads [ k ] , k ) ;
493498 } ) ;
494- task = manager . uploadTaskWithRequestFromDataProgressCompletionHandler ( request , data , progress , ( response : NSURLResponse , responseObject : any , error : NSError ) => {
495- if ( error ) {
499+ task = manager . uploadTaskWithRequestFromDataProgressCompletionHandler ( request , data , progress , ( response : NSURLResponse , responseObject : any , error : NSError ) => {
500+ if ( error ) {
496501 failure ( task , error ) ;
497502 } else {
498503 success ( task , responseObject ) ;
0 commit comments