1- import * as Https from " nativescript-https" ;
2- import * as Observable from " @nativescript/core/data/observable" ;
3- import * as fs from " @nativescript/core/file-system" ;
4- import * as dialogs from " @nativescript/core/ui/dialogs" ;
5- import * as Page from " @nativescript/core/ui/page" ;
1+ import * as Https from '@ nativescript-community/ https' ;
2+ import * as Observable from ' @nativescript/core/data/observable' ;
3+ import * as fs from ' @nativescript/core/file-system' ;
4+ import * as dialogs from ' @nativescript/core/ui/dialogs' ;
5+ import * as Page from ' @nativescript/core/ui/page' ;
66
77let page ;
88let viewModel ;
@@ -16,21 +16,18 @@ export function pageLoaded(args: Page.NavigatedData) {
1616 page . bindingContext = viewModel ;
1717}
1818
19- function createRequest (
20- url : string ,
21- options ?: Partial < Https . HttpsRequestOptions >
22- ) {
19+ function createRequest ( url : string , options ?: Partial < Https . HttpsRequestOptions > ) {
2320 return Https . createRequest ( {
2421 useLegacy : true ,
2522 url,
26- method : " GET" ,
23+ method : ' GET' ,
2724 timeout : 1 ,
2825 ...options ,
2926 } ) ;
3027}
3128
3229function onError ( error ) {
33- console . error ( " Https.request error" , error , error . stack ) ;
30+ console . error ( ' Https.request error' , error , error . stack ) ;
3431 dialogs . alert ( error . toString ( ) ) ;
3532 page . bindingContext . currentRequest = null ;
3633 page . bindingContext . progress = 0 ;
@@ -40,59 +37,61 @@ function getRequest(url: string, options?: Partial<Https.HttpsRequestOptions>) {
4037 return Https . request ( {
4138 useLegacy : true ,
4239 url,
43- method : " GET" ,
40+ method : ' GET' ,
4441 timeout : 1 ,
4542 ...options ,
4643 } )
4744 . then ( ( response ) => {
4845 page . bindingContext . currentRequest = null ;
4946 page . bindingContext . progress = 0 ;
50- console . log ( " Https.request response" , response ) ;
47+ console . log ( ' Https.request response' , response ) ;
5148 return response ;
5249 } )
5350 . catch ( onError ) ;
5451}
5552
56- function postRequest (
57- url : string ,
58- options ?: Partial < Https . HttpsRequestOptions >
59- ) {
53+ function postRequest ( url : string , options ?: Partial < Https . HttpsRequestOptions > ) {
6054 return Https . request ( {
6155 useLegacy : true ,
6256 url,
63- method : " POST" ,
57+ method : ' POST' ,
6458 ...options ,
6559 } )
66- . then ( ( response ) => console . log ( " Https.request response" , response ) )
60+ . then ( ( response ) => console . log ( ' Https.request response' , response ) )
6761 . catch ( ( error ) => {
68- console . error ( " Https.request error" , error ) ;
62+ console . error ( ' Https.request error' , error ) ;
6963 dialogs . alert ( error ) ;
7064 } ) ;
7165}
7266
7367export function postHttpbin ( ) {
74- postRequest ( " https://httpbin.org/post" , {
75- body : { foo : " bar" , baz : undefined , plaz : null } ,
68+ postRequest ( ' https://httpbin.org/post' , {
69+ body : { foo : ' bar' , baz : undefined , plaz : null } ,
7670 } ) ;
7771}
7872
7973export function postHttpbinWithUTF8 ( ) {
8074 Https . request ( {
81- url : " https://httpbin.org/post" ,
82- method : " POST" ,
83- body : { foo : " bar" , baz : undefined , plaz : null } ,
75+ url : ' https://httpbin.org/post' ,
76+ method : ' POST' ,
77+ body : { foo : ' bar' , baz : undefined , plaz : null } ,
8478 headers : {
85- " Content-Type" : " application/json; charset=utf-8" ,
86- " X-testing" : "ok" ,
79+ ' Content-Type' : ' application/json; charset=utf-8' ,
80+ ' X-testing' : 'ok' ,
8781 } ,
8882 } ) . catch ( ( error ) => {
89- console . error ( " Https.request error" , error ) ;
83+ console . error ( ' Https.request error' , error ) ;
9084 dialogs . alert ( error ) ;
9185 } ) ;
9286}
9387
9488export function getHttpbin ( ) {
95- getRequest ( "https://httpbin.org/get" ) ;
89+ getRequest ( 'https://httpbin.org/get' , {
90+ headers : {
91+ // 'Cache-Control': 'public, only-if-cached, max-stale=' +(60 * 60 * 24 * 7),
92+ 'Cache-Control' : 'max-age=50000' ,
93+ }
94+ } ) ;
9695}
9796
9897export function cancelCurrentRequest ( ) {
@@ -101,65 +100,60 @@ export function cancelCurrentRequest() {
101100 }
102101}
103102export function getBigFile ( ) {
104- const request = createRequest (
105- "http://ipv4.download.thinkbroadband.com/200MB.zip" ,
106- {
107- onProgress : ( current , total ) => {
108- page . bindingContext . progress = ( current / total ) * 100 ;
109- } ,
110- }
111- ) ;
103+ const request = createRequest ( 'http://ipv4.download.thinkbroadband.com/200MB.zip' , {
104+ onProgress : ( current , total ) => {
105+ page . bindingContext . progress = ( current / total ) * 100 ;
106+ } ,
107+ } ) ;
112108 page . bindingContext . currentRequest = request ;
113109
114110 return new Promise < any > ( ( resolve , reject ) => {
115111 request . run ( resolve , reject ) ;
116112 } )
117113 . then ( ( response ) => {
118- console . log ( "did get response" ) ;
119- let dir = fs . knownFolders . temp ( ) . getFile ( "200MB.zip" ) ;
120- return ( response . content as Https . HttpsResponseLegacy ) . toFile (
121- dir . path
122- ) ;
114+ console . log ( 'did get response' ) ;
115+ const dir = fs . knownFolders . temp ( ) . getFile ( '200MB.zip' ) ;
116+ return ( response . content as Https . HttpsResponseLegacy ) . toFile ( dir . path ) ;
123117 // console.log("did get response done");
124118 } )
125119 . then ( ( ) => {
126120 page . bindingContext . currentPromise = null ;
127- console . log ( " did get response done" ) ;
121+ console . log ( ' did get response done' ) ;
128122 } )
129123 . catch ( onError ) ;
130124}
131125
132126export function getHttpbinLargeResponse ( ) {
133- getRequest ( " https://httpbin.org/bytes/100000" ) ;
127+ getRequest ( ' https://httpbin.org/bytes/100000' ) ;
134128}
135129
136130export function getMockbin ( ) {
137- getRequest ( " https://mockbin.com/request" ) ;
131+ getRequest ( ' https://mockbin.com/request' ) ;
138132}
139133
140134export function get404 ( ) {
141- getRequest ( " https://mockbin.com/reque2st" ) ;
135+ getRequest ( ' https://mockbin.com/reque2st' ) ;
142136}
143137
144138export function enableSSLPinning ( args : Observable . EventData ) {
145- let dir = fs . knownFolders . currentApp ( ) . getFolder ( " assets" ) ;
146- let certificate = dir . getFile ( " httpbin.org.cer" ) . path ;
139+ const dir = fs . knownFolders . currentApp ( ) . getFolder ( ' assets' ) ;
140+ const certificate = dir . getFile ( ' httpbin.org.cer' ) . path ;
147141 Https . enableSSLPinning ( {
148- host : " httpbin.org" ,
149- commonName : " httpbin.org" ,
142+ host : ' httpbin.org' ,
143+ commonName : ' httpbin.org' ,
150144 certificate,
151145 } ) ;
152- console . log ( " enabled" ) ;
146+ console . log ( ' enabled' ) ;
153147}
154148
155149export function enableSSLPinningExpired ( args : Observable . EventData ) {
156- let dir = fs . knownFolders . currentApp ( ) . getFolder ( " assets" ) ;
157- let certificate = dir . getFile ( " httpbin.org.expired.cer" ) . path ;
158- Https . enableSSLPinning ( { host : " httpbin.org" , certificate } ) ;
159- console . log ( " enabled" ) ;
150+ const dir = fs . knownFolders . currentApp ( ) . getFolder ( ' assets' ) ;
151+ const certificate = dir . getFile ( ' httpbin.org.expired.cer' ) . path ;
152+ Https . enableSSLPinning ( { host : ' httpbin.org' , certificate } ) ;
153+ console . log ( ' enabled' ) ;
160154}
161155
162156export function disableSSLPinning ( args : Observable . EventData ) {
163157 Https . disableSSLPinning ( ) ;
164- console . log ( " disabled" ) ;
158+ console . log ( ' disabled' ) ;
165159}
0 commit comments