Skip to content

Commit 90c04b4

Browse files
committed
Added all http methods
- added all 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' methods - modified how typings are read through npm
1 parent 08d34ee commit 90c04b4

File tree

10 files changed

+160
-108
lines changed

10 files changed

+160
-108
lines changed

https.android.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

https.android.ts

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,68 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
171171
let request = new okhttp3.Request.Builder()
172172
request.url(opts.url)
173173

174-
let reqheads = opts.headers
175-
Object.keys(reqheads).forEach(function(key) {
176-
request.addHeader(key, reqheads[key] as any)
177-
})
178-
179-
if (opts.method == 'GET') {
180-
request.get()
181-
} else if (opts.method == 'POST') {
182-
let type = okhttp3.MediaType.parse('application/json')
183-
let body = okhttp3.RequestBody.create(type, opts.content as any)
184-
request.post(body)
174+
if (opts.headers) {
175+
Object.keys(opts.headers).forEach(function(key) {
176+
request.addHeader(key, opts.headers[key] as any)
177+
})
185178
}
179+
180+
let methods = {
181+
'GET': 'get',
182+
'HEAD': 'head',
183+
184+
'DELETE': 'delete',
185+
186+
'POST': 'post',
187+
'PUT': 'put',
188+
'PATCH': 'patch',
189+
}
190+
if (
191+
(['GET', 'HEAD'].indexOf(opts.method) != -1)
192+
||
193+
(opts.method == 'DELETE' && !isDefined(opts.body))
194+
) {
195+
request[methods[opts.method]]()
196+
} else {
197+
let type = <string>opts.headers['Content-Type'] || 'application/json'
198+
let body = <any>opts.body || {}
199+
try {
200+
body = JSON.stringify(body)
201+
} catch (e) { }
202+
request[methods[opts.method]](okhttp3.RequestBody.create(
203+
okhttp3.MediaType.parse(type),
204+
body
205+
))
206+
}
207+
186208
client.newCall(request.build()).enqueue(new okhttp3.Callback({
187209
onResponse: function(task, response) {
188-
let content: any
210+
// console.log('onResponse')
211+
// console.keys('response', response)
212+
// console.log('onResponse > response.isSuccessful()', response.isSuccessful())
213+
214+
// let body = response.body()//.bytes()
215+
// console.keys('body', body)
216+
// console.log('body.contentType()', body.contentType())
217+
// console.log('body.contentType().toString()', body.contentType().toString())
218+
// console.log('body.bytes()', body.bytes())
219+
// console.dump('wtf', wtf)
220+
// console.log('opts.url', opts.url)
221+
// console.log('body.string()', body.string())
222+
223+
// let content: any = response.body().string()
224+
// console.log('content', content)
225+
// try {
226+
// content = JSON.parse(response.body().string())
227+
// } catch (error) {
228+
// return reject(error)
229+
// }
230+
231+
let content = response.body().string()
189232
try {
190-
content = JSON.parse(response.body().string())
191-
} catch (error) {
192-
return reject(error)
193-
}
233+
content = JSON.parse(content)
234+
} catch (e) { }
235+
194236
let statusCode = response.code()
195237

196238
let headers = {}

https.common.d.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
1-
import { HttpRequestOptions, Headers } from 'http';
1+
//
2+
3+
import { HttpRequestOptions, Headers } from 'http'
4+
5+
6+
27
export interface HttpsSSLPinningOptions {
3-
host: string;
4-
certificate: string;
5-
allowInvalidCertificates?: boolean;
6-
validatesDomainName?: boolean;
8+
host: string
9+
certificate: string
10+
allowInvalidCertificates?: boolean
11+
validatesDomainName?: boolean
712
}
8-
export interface HttpsRequestOptions extends HttpRequestOptions {
9-
method: 'GET' | 'POST';
10-
headers?: Headers;
11-
content?: string;
13+
14+
export interface HttpsRequestObject {
15+
[key: string]: string | number
1216
}
17+
18+
export interface HttpsRequestOptions {
19+
url: string
20+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD'
21+
headers?: Headers
22+
params?: HttpsRequestObject
23+
body?: HttpsRequestObject
24+
}
25+
1326
export interface HttpsResponse {
14-
headers?: Headers;
15-
statusCode?: number;
16-
content?: any;
17-
reason?: string;
18-
reject?: boolean;
27+
headers?: Headers
28+
statusCode?: number
29+
content?: any
30+
reason?: string
31+
reject?: boolean
1932
}
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+

https.common.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

https.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
3+
import { HttpRequestOptions, Headers, HttpResponse } from 'http'
4+
import * as Https from './https.common'
5+
6+
7+
8+
export declare function enableSSLPinning(options: Https.HttpsSSLPinningOptions);
9+
export declare function disableSSLPinning();
10+
export declare function request(options: Https.HttpsRequestOptions): Promise<Https.HttpsResponse>;
11+
12+
13+
14+
export * from './https.common'

https.ios.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

https.ios.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
5757
// content[k] = v
5858
// })
5959
let serial = NSJSONSerialization.dataWithJSONObjectOptionsError(data, NSJSONWritingOptions.PrettyPrinted)
60-
let results = NSString.alloc().initWithDataEncoding(serial, NSUTF8StringEncoding)
61-
content = JSON.parse(results.toString())
60+
content = NSString.alloc().initWithDataEncoding(serial, NSUTF8StringEncoding).toString()
61+
// console.log('content', content)
6262
} else if (data.class().name == 'NSData') {
6363
content = NSString.alloc().initWithDataEncoding(data, NSASCIIStringEncoding).toString()
6464
// } else if (data.class().name == 'NSArray') {
@@ -80,9 +80,15 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
8080
} else {
8181
content = data
8282
}
83+
84+
try {
85+
content = JSON.parse(content)
86+
} catch (e) { }
87+
8388
} else {
8489
content = data
8590
}
91+
8692
resolve({ task, content })
8793
}
8894

@@ -92,9 +98,12 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError)
9298
// console.log('error.userInfo.description', error.userInfo.description)
9399
// console.log('error.localizedDescription', error.localizedDescription)
94100
let data: NSData = error.userInfo.valueForKey(AFNetworkingOperationFailingURLResponseDataErrorKey)
95-
let body = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding)
101+
let body = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding).toString()
102+
try {
103+
body = JSON.parse(body)
104+
} catch (e) { }
96105
let content: any = {
97-
body: body.description,
106+
body,
98107
description: error.description,
99108
reason: error.localizedDescription,
100109
url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
@@ -134,7 +143,7 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
134143

135144
let manager = AFHTTPSessionManager.manager()
136145

137-
if (opts.headers['Content-Type'] == 'application/json') {
146+
if (opts.headers && opts.headers['Content-Type'] == 'application/json') {
138147
manager.requestSerializer = AFJSONRequestSerializer.serializer()
139148
manager.responseSerializer = AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingOptions.AllowFragments)
140149
} else {
@@ -156,8 +165,8 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
156165
}
157166

158167
let dict: NSMutableDictionary<string, any> = null
159-
if (opts.content) {
160-
let cont = JSON.parse(opts.content as any)
168+
if (opts.body) {
169+
let cont = opts.body
161170
if (isObject(cont)) {
162171
dict = NSMutableDictionary.new<string, any>()
163172
Object.keys(cont).forEach(function(key) {
@@ -166,19 +175,33 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
166175
}
167176
}
168177

169-
if (opts.method == 'GET') {
170-
manager.GETParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
171-
AFSuccess(resolve, task, data)
172-
}, function failure(task, error) {
173-
AFFailure(resolve, reject, task, error)
174-
})
175-
} else if (opts.method == 'POST') {
176-
manager.POSTParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
177-
AFSuccess(resolve, task, data)
178-
}, function failure(task, error) {
179-
AFFailure(resolve, reject, task, error)
180-
})
178+
let methods = {
179+
'GET': 'GETParametersSuccessFailure',
180+
'POST': 'POSTParametersSuccessFailure',
181+
'PUT': 'PUTParametersSuccessFailure',
182+
'DELETE': 'DELETEParametersSuccessFailure',
183+
'PATCH': 'PATCHParametersSuccessFailure',
184+
'HEAD': 'HEADParametersSuccessFailure',
181185
}
186+
manager[methods[opts.method]](opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
187+
AFSuccess(resolve, task, data)
188+
}, function failure(task, error) {
189+
AFFailure(resolve, reject, task, error)
190+
})
191+
192+
// if (opts.method == 'GET') {
193+
// manager.GETParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
194+
// AFSuccess(resolve, task, data)
195+
// }, function failure(task, error) {
196+
// AFFailure(resolve, reject, task, error)
197+
// })
198+
// } else if (opts.method == 'POST') {
199+
// manager.POSTParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
200+
// AFSuccess(resolve, task, data)
201+
// }, function failure(task, error) {
202+
// AFFailure(resolve, reject, task, error)
203+
// })
204+
// }
182205

183206
} catch (error) {
184207
reject(error)

index.d.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
export * from './AFNetworking'
2-
export * from './com.squareup.okhttp3'
3-
/**
4-
* iOS and Android apis should match.
5-
* It doesn't matter if you export `.ios` or `.android`, either one but only one.
6-
*/
7-
export * from './https.ios';
1+
//
2+
// export * from './AFNetworking'
3+
// export * from './com.squareup.okhttp3'
84

9-
// Export any shared classes, constants, etc.
10-
export * from './https.common';
5+
export * from './https'
6+
// export * from './https.common'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.2",
44
"description": "Secure HTTP client with SSL pinning for Nativescript - iOS/Android.",
55
"main": "https",
6-
"typings": "index.d.ts",
6+
"typings": "https.d.ts",
77
"nativescript": {
88
"platforms": {
99
"android": "2.3.0",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"target": "es5",
44
"module": "commonjs",
5-
"declaration": true,
5+
"declaration": false,
66
"removeComments": true,
77
"noLib": false,
88
"skipLibCheck": true,

0 commit comments

Comments
 (0)