Skip to content

Commit dd1be42

Browse files
committed
fix(): guards and type checking
1 parent ef8e28b commit dd1be42

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

src/http/http.android.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class Http {
9292
}
9393

9494
buildJavaOptions(options: HttpRequestOptions) {
95-
if (typeof options.url !== 'string') {
95+
if (!types.isString(options.url)) {
9696
throw new Error('Http request must provide a valid url.');
9797
}
9898

@@ -101,18 +101,18 @@ export class Http {
101101
javaOptions.url = options.url;
102102

103103
let method;
104-
if (typeof options.method === 'string') {
104+
if (types.isString(typeof options.method)) {
105105
javaOptions.method = options.method;
106106
method = options.method.toLowerCase();
107107
}
108108
if ((method && method === 'post') || method === 'put') {
109109
if (
110-
typeof options.content === 'string'
110+
types.isString(options.content)
111111
) {
112112
javaOptions.content = new java.lang.String(options.content);
113113
} else if (options.content instanceof FormData) {
114114
javaOptions.content = new java.lang.String(options.content.toString());
115-
} else if (typeof options.content === 'object') {
115+
} else if (types.isObject(options.content)) {
116116
javaOptions.content = serialize(options.content);
117117
}
118118
}
@@ -182,7 +182,7 @@ export class Http {
182182
isString = true;
183183
} else {
184184
content = result.content;
185-
if (content instanceof java.lang.String || typeof content === 'string') {
185+
if (content instanceof java.lang.String || types.isString(content)) {
186186
try {
187187
responseText = JSON.stringify(content);
188188
} catch (err) {
@@ -208,7 +208,7 @@ export class Http {
208208

209209
let contentType = headers['Content-Type'];
210210
if (contentType == null) {
211-
contentType = headers['content-Type'];
211+
contentType = headers['content-type'];
212212
}
213213
let acceptHeader;
214214

@@ -219,7 +219,7 @@ export class Http {
219219
}
220220

221221
let returnType = 'text/plain';
222-
if (acceptHeader != null) {
222+
if (!types.isNullOrUndefined(acceptHeader) && types.isString(acceptHeader)) {
223223
let acceptValues = acceptHeader.split(',');
224224
let quality = [];
225225
let defaultQuality = [];

src/http/http.ios.ts

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class NSURLSessionTaskDelegateImpl extends NSObject
5252
private _loadingSent: boolean;
5353
private _debuggerRequest;
5454
private _response;
55+
5556
public static initWithDebuggerRequestResolveRejectCallbackHeadersLoadingListener(
5657
debuggerRequest,
5758
request,
@@ -99,20 +100,20 @@ class NSURLSessionTaskDelegateImpl extends NSObject
99100
) {
100101
// const method = this._request.HTTPMethod.toLowerCase();
101102
if (data) {
102-
this._data.appendData(data);
103-
104-
const lastProgress: any = this._lastProgress || {
105-
lengthComputable: false,
106-
total: 0
107-
};
108-
lastProgress.loaded = this._data.length;
109-
if (this._onLoading && !this._loadingSent) {
110-
this._onLoading(lastProgress);
111-
this._loadingSent = true;
112-
}
113-
if (this._onProgress) {
114-
this._onProgress(lastProgress);
115-
}
103+
this._data.appendData(data);
104+
105+
const lastProgress: any = this._lastProgress || {
106+
lengthComputable: false,
107+
total: 0
108+
};
109+
lastProgress.loaded = this._data.length;
110+
if (this._onLoading && !this._loadingSent) {
111+
this._onLoading(lastProgress);
112+
this._loadingSent = true;
113+
}
114+
if (this._onProgress) {
115+
this._onProgress(lastProgress);
116+
}
116117
}
117118
}
118119

@@ -123,20 +124,20 @@ class NSURLSessionTaskDelegateImpl extends NSObject
123124
totalBytesSent,
124125
totalBytesExpectedToSend
125126
) {
126-
if (this._onLoading || this._onProgress) {
127-
this._lastProgress = {
128-
lengthComputable: totalBytesExpectedToSend > -1,
129-
loaded: totalBytesSent,
130-
total: totalBytesExpectedToSend > -1 ? totalBytesExpectedToSend : 0
131-
};
132-
if (this._onLoading && !this._loadingSent) {
133-
this._onLoading(this._lastProgress);
134-
this._loadingSent = true;
135-
}
136-
if (this._onProgress) {
137-
this._onProgress(this._lastProgress);
127+
if (this._onLoading || this._onProgress) {
128+
this._lastProgress = {
129+
lengthComputable: totalBytesExpectedToSend > -1,
130+
loaded: totalBytesSent,
131+
total: totalBytesExpectedToSend > -1 ? totalBytesExpectedToSend : 0
132+
};
133+
if (this._onLoading && !this._loadingSent) {
134+
this._onLoading(this._lastProgress);
135+
this._loadingSent = true;
136+
}
137+
if (this._onProgress) {
138+
this._onProgress(this._lastProgress);
139+
}
138140
}
139-
}
140141
}
141142

142143
public URLSessionDataTaskDidReceiveResponseCompletionHandler(
@@ -223,7 +224,7 @@ class NSURLSessionTaskDelegateImpl extends NSObject
223224
const isTextContentType = (contentType: string): boolean => {
224225
let result = false;
225226
for (let i = 0; i < textTypes.length; i++) {
226-
if (contentType.toLowerCase().indexOf(textTypes[i]) >= 0) {
227+
if (types.isString(contentType) && contentType.toLowerCase().indexOf(textTypes[i]) >= 0) {
227228
result = true;
228229
break;
229230
}
@@ -257,7 +258,7 @@ class NSURLSessionTaskDelegateImpl extends NSObject
257258
}
258259

259260
let returnType = 'text/plain';
260-
if (acceptHeader != null) {
261+
if (!types.isNullOrUndefined(acceptHeader) && types.isString(acceptHeader)) {
261262
let acceptValues = acceptHeader.split(',');
262263
let quality = [];
263264
let defaultQuality = [];
@@ -284,19 +285,19 @@ class NSURLSessionTaskDelegateImpl extends NSObject
284285
if (isTextContentType(returnType)) {
285286
responseText = NSDataToString(this._data);
286287
content = responseText;
287-
} else if (returnType.indexOf('application/json') > -1) {
288+
} else if (types.isString(returnType) && returnType.indexOf('application/json') > -1) {
288289
// @ts-ignore
289290
try {
290-
responseText = NSDataToString(this._data);
291-
content = JSON.parse(responseText);
292-
// content = deserialize(NSJSONSerialization.JSONObjectWithDataOptionsError(this._data, NSJSONReadingOptions.AllowFragments, null));
291+
responseText = NSDataToString(this._data);
292+
content = JSON.parse(responseText);
293+
// content = deserialize(NSJSONSerialization.JSONObjectWithDataOptionsError(this._data, NSJSONReadingOptions.AllowFragments, null));
293294
} catch (err) {
294-
this._reject({
295-
type: HttpError.Error,
296-
ios: null,
297-
message: err
298-
});
299-
return;
295+
this._reject({
296+
type: HttpError.Error,
297+
ios: null,
298+
message: err
299+
});
300+
return;
300301
}
301302
} else {
302303
content = this._data;
@@ -379,9 +380,9 @@ export class Http {
379380
let domainDebugger;
380381
let debugRequest;
381382
if (TNSHttpDebugging.enabled) {
382-
domainDebugger = require('tns-core-modules/debugger');
383-
const network = domainDebugger.getNetwork();
384-
debugRequest = network && network.create();
383+
domainDebugger = require('tns-core-modules/debugger');
384+
const network = domainDebugger.getNetwork();
385+
debugRequest = network && network.create();
385386
}
386387

387388
const urlRequest = NSMutableURLRequest.requestWithURL(

0 commit comments

Comments
 (0)