Skip to content

Commit 74f1afa

Browse files
committed
fix: useLegacy should be a request option
1 parent 29c9626 commit 74f1afa

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,24 @@ export interface HttpsSSLPinningOptions {
142142
allowInvalidCertificates?: boolean
143143
validatesDomainName?: boolean
144144
commonName?: string
145+
}
146+
import { HttpRequestOptions } from 'tns-core-modules/http';
147+
export interface HttpsRequestOptions extends HTTPOptions{
145148
useLegacy?: boolean
146149
cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache'
147150
onProgress?: (current: number, total: number) => void
148151
}
149152
```
150-
Option | Description
153+
SSLPinning Option | Description
151154
------------ | -------------
152155
`host: string` | This must be the request domain name eg `sales.company.org`.
153156
`commonName?: string` | Default: options.host, set if certificate CN is different from the host eg `*.company.org` (Android specific)
154157
`certificate: string` | The uri path to your `.cer` certificate file.
155158
`allowInvalidCertificates?: boolean` | Default: `false`. This should **always** be `false` if you are using SSL pinning. Set this to `true` if you're using a self-signed certificate.
156159
`validatesDomainName?: boolean` | Default: `true`. Determines if the domain name should be validated with your pinned certificate.
160+
161+
Requests Option | Description
162+
------------ | -------------
157163
`useLegacy?: boolean` | Default: `false`. [IOS only] set to true in order to get the response data (when status >= 300)in the `content` directly instead of `response.body.content`.
158164
`cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache'` | Set the cache policy to use with that request. This only works with GET requests for now.
159165
`onProgress?: (current: number, total: number) => void` | [IOS only] Set the progress callback.

src/https.common.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { Headers } from 'tns-core-modules/http';
1+
import { Headers, HttpRequestOptions } from 'tns-core-modules/http';
22
export interface HttpsSSLPinningOptions {
33
host: string;
44
certificate: string;
55
allowInvalidCertificates?: boolean;
66
validatesDomainName?: boolean;
77
commonName?: string;
8-
useLegacy?: boolean;
98
}
109
export interface CacheOptions {
1110
diskLocation: string;
@@ -22,7 +21,7 @@ export interface HttpsRequestObject {
2221
[key: string]: string | number | boolean | HttpsRequestObject | Array<any> | HttpsFormDataParam;
2322
}
2423
export declare type CachePolicy = 'noCache' | 'onlyCache' | 'ignoreCache';
25-
export interface HttpsRequestOptions {
24+
export interface HttpsRequestOptions extends HttpRequestOptions {
2625
url: string;
2726
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
2827
headers?: Headers;
@@ -33,6 +32,7 @@ export interface HttpsRequestOptions {
3332
timeout?: number;
3433
onProgress?: (current: number, total: number) => void;
3534
cachePolicy?: CachePolicy;
35+
useLegacy?: boolean;
3636
}
3737
export interface HttpsResponse {
3838
headers?: Headers;

src/https.common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Headers } from 'tns-core-modules/http';
1+
import { Headers, HttpRequestOptions } from 'tns-core-modules/http';
22

33
export interface HttpsSSLPinningOptions {
44
host: string;
55
certificate: string;
66
allowInvalidCertificates?: boolean;
77
validatesDomainName?: boolean;
88
commonName?: string;
9-
useLegacy?: boolean;
109
}
1110
export interface CacheOptions {
1211
diskLocation: string;
@@ -27,7 +26,7 @@ export interface HttpsRequestObject {
2726

2827

2928
export type CachePolicy = 'noCache' | 'onlyCache' | 'ignoreCache';
30-
export interface HttpsRequestOptions {
29+
export interface HttpsRequestOptions extends HttpRequestOptions {
3130
url: string;
3231
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
3332
headers?: Headers;
@@ -51,6 +50,7 @@ export interface HttpsRequestOptions {
5150
onProgress?: (current: number, total: number) => void
5251

5352
cachePolicy?:CachePolicy
53+
useLegacy?: boolean;
5454
}
5555

5656
export interface HttpsResponse {

src/https.ios.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
} from "tns-core-modules/utils/types";
66
import * as Https from "./https.common";
77

8-
let useLegacy: boolean = false;
9-
108
let cache: NSURLCache;
119

1210
export function setCache(options?: Https.CacheOptions) {
@@ -59,7 +57,6 @@ export function enableSSLPinning(options: Https.HttpsSSLPinningOptions) {
5957
let data = NSData.dataWithContentsOfFile(options.certificate);
6058
policies.secure.pinnedCertificates = NSSet.setWithObject(data);
6159
}
62-
useLegacy = isDefined(options.useLegacy) ? options.useLegacy : false;
6360
policies.secured = true;
6461
console.log("nativescript-https > Enabled SSL pinning");
6562
}
@@ -84,7 +81,8 @@ function AFFailure(
8481
resolve,
8582
reject,
8683
task: NSURLSessionDataTask,
87-
error: NSError
84+
error: NSError,
85+
useLegacy: boolean
8886
) {
8987
let data: NSDictionary<string, any> &
9088
NSData &
@@ -219,11 +217,14 @@ export function request(
219217
? opts.timeout
220218
: 10;
221219

220+
221+
const useLegacy = isDefined(opts.useLegacy) ? opts.useLegacy : false;
222+
222223
const success = function (task: NSURLSessionDataTask, data?: any) {
223224
AFSuccess(resolve, task, data);
224225
};
225226
const failure = function (task: NSURLSessionDataTask, error: any) {
226-
AFFailure(resolve, reject, task, error);
227+
AFFailure(resolve, reject, task, error, useLegacy);
227228
};
228229

229230
const progress = opts.onProgress ? (progress: NSProgress) => {

0 commit comments

Comments
 (0)