11/**
22 * Android specific http request implementation.
33 */
4- import * as imageSourceModule from "../../image-source" ;
5- import * as platformModule from "../../platform" ;
6- import * as fsModule from "../../file-system" ;
4+ import { fromNativeSource } from "../../image-source" ;
5+ import { screen } from "../../platform" ;
6+ import { File } from "../../file-system" ;
77import { getFilenameFromUrl } from "./http-request-common" ;
88
99// this is imported for definition purposes only
@@ -17,32 +17,18 @@ export enum HttpResponseEncoding {
1717}
1818
1919function parseJSON ( source : string ) : any {
20- var src = source . trim ( ) ;
20+ const src = source . trim ( ) ;
2121 if ( src . lastIndexOf ( ")" ) === src . length - 1 ) {
2222 return JSON . parse ( src . substring ( src . indexOf ( "(" ) + 1 , src . lastIndexOf ( ")" ) ) ) ;
2323 }
2424
2525 return JSON . parse ( src ) ;
2626}
2727
28- var requestIdCounter = 0 ;
29- var pendingRequests = { } ;
28+ let requestIdCounter = 0 ;
29+ const pendingRequests = { } ;
3030
31- var imageSource : typeof imageSourceModule ;
32- function ensureImageSource ( ) {
33- if ( ! imageSource ) {
34- imageSource = require ( "image-source" ) ;
35- }
36- }
37-
38- var platform : typeof platformModule ;
39- function ensurePlatform ( ) {
40- if ( ! platform ) {
41- platform = require ( "platform" ) ;
42- }
43- }
44-
45- var completeCallback : org . nativescript . widgets . Async . CompleteCallback ;
31+ let completeCallback : org . nativescript . widgets . Async . CompleteCallback ;
4632function ensureCompleteCallback ( ) {
4733 if ( completeCallback ) {
4834 return ;
@@ -60,7 +46,7 @@ function ensureCompleteCallback() {
6046}
6147
6248function onRequestComplete ( requestId : number , result : org . nativescript . widgets . Async . Http . RequestResult ) {
63- var callbacks = pendingRequests [ requestId ] ;
49+ const callbacks = pendingRequests [ requestId ] ;
6450 delete pendingRequests [ requestId ] ;
6551
6652 if ( result . error ) {
@@ -69,13 +55,12 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
6955 }
7056
7157 // read the headers
72- var headers : http . Headers = { } ;
58+ const headers : http . Headers = { } ;
7359 if ( result . headers ) {
74- var jHeaders = result . headers ;
75- var length = jHeaders . size ( ) ;
76- var i ;
77- var pair : org . nativescript . widgets . Async . Http . KeyValuePair ;
78- for ( i = 0 ; i < length ; i ++ ) {
60+ const jHeaders = result . headers ;
61+ const length = jHeaders . size ( ) ;
62+ let pair : org . nativescript . widgets . Async . Http . KeyValuePair ;
63+ for ( let i = 0 ; i < length ; i ++ ) {
7964 pair = jHeaders . get ( i ) ;
8065 addHeader ( headers , pair . key , pair . value ) ;
8166 }
@@ -112,29 +97,29 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
11297 return parseJSON ( str ) ;
11398 } ,
11499 toImage : ( ) => {
115- ensureImageSource ( ) ;
116-
117100 return new Promise < any > ( ( resolveImage , rejectImage ) => {
118101 if ( result . responseAsImage != null ) {
119- resolveImage ( imageSource . fromNativeSource ( result . responseAsImage ) ) ;
102+ resolveImage ( fromNativeSource ( result . responseAsImage ) ) ;
120103 }
121104 else {
122105 rejectImage ( new Error ( "Response content may not be converted to an Image" ) ) ;
123106 }
124107 } ) ;
125108 } ,
126109 toFile : ( destinationFilePath : string ) => {
127- var fs : typeof fsModule = require ( "file-system" ) ;
128-
129110 if ( ! destinationFilePath ) {
130111 destinationFilePath = getFilenameFromUrl ( callbacks . url ) ;
131112 }
132- var stream : java . io . FileOutputStream ;
113+ let stream : java . io . FileOutputStream ;
133114 try {
134- var javaFile = new java . io . File ( destinationFilePath ) ;
115+ // ensure destination path exists by creating any missing parent directories
116+ const file = File . fromPath ( destinationFilePath ) ;
117+
118+ const javaFile = new java . io . File ( destinationFilePath ) ;
135119 stream = new java . io . FileOutputStream ( javaFile ) ;
136120 stream . write ( result . raw . toByteArray ( ) ) ;
137- return fs . File . fromPath ( destinationFilePath ) ;
121+
122+ return file ;
138123 }
139124 catch ( exception ) {
140125 throw new Error ( `Cannot save file with path: ${ destinationFilePath } .` ) ;
@@ -152,7 +137,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
152137}
153138
154139function onRequestError ( error : string , requestId : number ) {
155- var callbacks = pendingRequests [ requestId ] ;
140+ const callbacks = pendingRequests [ requestId ] ;
156141 delete pendingRequests [ requestId ] ;
157142 if ( callbacks ) {
158143 callbacks . rejectCallback ( new Error ( error ) ) ;
@@ -164,7 +149,7 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
164149 throw new Error ( "Http request must provide a valid url." ) ;
165150 }
166151
167- var javaOptions = new org . nativescript . widgets . Async . Http . RequestOptions ( ) ;
152+ const javaOptions = new org . nativescript . widgets . Async . Http . RequestOptions ( ) ;
168153
169154 javaOptions . url = options . url ;
170155
@@ -182,22 +167,19 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
182167 }
183168
184169 if ( options . headers ) {
185- var arrayList = new java . util . ArrayList < org . nativescript . widgets . Async . Http . KeyValuePair > ( ) ;
186- var pair = org . nativescript . widgets . Async . Http . KeyValuePair ;
170+ const arrayList = new java . util . ArrayList < org . nativescript . widgets . Async . Http . KeyValuePair > ( ) ;
171+ const pair = org . nativescript . widgets . Async . Http . KeyValuePair ;
187172
188- for ( var key in options . headers ) {
173+ for ( let key in options . headers ) {
189174 arrayList . add ( new pair ( key , options . headers [ key ] + "" ) ) ;
190175 }
191176
192177 javaOptions . headers = arrayList ;
193178 }
194179
195- ensurePlatform ( ) ;
196-
197180 // pass the maximum available image size to the request options in case we need a bitmap conversion
198- var screen = platform . screen . mainScreen ;
199- javaOptions . screenWidth = screen . widthPixels ;
200- javaOptions . screenHeight = screen . heightPixels ;
181+ javaOptions . screenWidth = screen . mainScreen . widthPixels ;
182+ javaOptions . screenHeight = screen . mainScreen . heightPixels ;
201183
202184 return javaOptions ;
203185}
@@ -211,15 +193,15 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
211193 return new Promise < http . HttpResponse > ( ( resolve , reject ) => {
212194 try {
213195 // initialize the options
214- var javaOptions = buildJavaOptions ( options ) ;
196+ const javaOptions = buildJavaOptions ( options ) ;
215197
216198 // send request data to network debugger
217199 if ( global . __inspector && global . __inspector . isConnected ) {
218200 NetworkAgent . requestWillBeSent ( requestIdCounter , options ) ;
219201 }
220202
221203 // remember the callbacks so that we can use them when the CompleteCallback is called
222- var callbacks = {
204+ const callbacks = {
223205 url : options . url ,
224206 resolveCallback : resolve ,
225207 rejectCallback : reject
@@ -252,7 +234,7 @@ export function addHeader(headers: http.Headers, key: string, value: string): vo
252234 } else if ( Array . isArray ( headers [ key ] ) ) {
253235 ( < string [ ] > headers [ key ] ) . push ( value ) ;
254236 } else {
255- let values : string [ ] = [ < string > headers [ key ] ] ;
237+ const values : string [ ] = [ < string > headers [ key ] ] ;
256238 values . push ( value ) ;
257239 headers [ key ] = values ;
258240 }
0 commit comments