Skip to content

Commit 22ac92f

Browse files
committed
feat: refactoring to allow N alias to replace core impl
1 parent 1cc655a commit 22ac92f

File tree

9 files changed

+185
-88
lines changed

9 files changed

+185
-88
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "Nativescript plugin for gestures",
55
"scripts": {
6-
"tsc": "cp src/https.d.ts plugin && tsc -skipLibCheck -d",
6+
"tsc": "cp src/index.d.ts src/request.d.ts plugin && tsc -skipLibCheck -d",
77
"clean": "rimraf ./plugin/**/*.d.ts ./plugin/**/*.js ./plugin/**/*.js.map plugin/node_modules plugin/package-lock.json",
88
"build.plugin": " cp README.md plugin/ && rm -f .tsbuildinfo && npm run tsc",
99
"build.android": "bash src-native/android/build.sh",

plugin/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "@nativescript-community/https",
33
"version": "3.3.13",
44
"description": "Nativescript plugin for gestures",
5-
"main": "https",
5+
"main": "index",
66
"sideEffects": false,
7-
"typings": "https.d.ts",
7+
"typings": "index.d.ts",
88
"nativescript": {
99
"platforms": {
1010
"android": "6.2.1",

src/https.d.ts

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

src/index.d.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* eslint-disable @typescript-eslint/unified-signatures */
2+
/* eslint-disable no-redeclare */
3+
import { ImageSource } from '@nativescript/core';
4+
import { HttpsRequestOptions } from './request';
5+
6+
export * from './request';
7+
8+
/**
9+
* Downloads the content from the specified URL as a string.
10+
* @param url The URL to request from.
11+
*/
12+
export function getString(url: string): Promise<string>;
13+
14+
/**
15+
* Downloads the content from the specified URL as a string.
16+
* @param options An object that specifies various request options.
17+
*/
18+
export function getString(options: HttpsRequestOptions): Promise<string>;
19+
20+
/**
21+
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
22+
* @param url The URL to request from.
23+
*/
24+
export function getJSON<T>(url: string): Promise<T>;
25+
26+
/**
27+
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
28+
* @param options An object that specifies various request options.
29+
*/
30+
export function getJSON<T>(options: HttpsRequestOptions): Promise<T>;
31+
32+
/**
33+
* Downloads the content from the specified URL and attempts to decode it as an image.
34+
* @param url The URL to request from.
35+
*/
36+
export function getImage(url: string): Promise<ImageSource>;
37+
38+
/**
39+
* Downloads the content from the specified URL and attempts to decode it as an image.
40+
* @param options An object that specifies various request options.
41+
*/
42+
export function getImage(options: HttpsRequestOptions): Promise<ImageSource>;
43+
44+
/**
45+
* Downloads the content from the specified URL and attempts to save it as file.
46+
* @param url The URL to request from.
47+
* @param destinationFilePath Optional. The downloaded file path.
48+
*/
49+
export function getFile(url: string, destinationFilePath?: string): Promise<File>;
50+
51+
/**
52+
* Downloads the content from the specified URL and attempts to save it as file.
53+
* @param options An object that specifies various request options.
54+
* @param destinationFilePath Optional. The downloaded file path.
55+
*/
56+
export function getFile(options: HttpsRequestOptions, destinationFilePath?: string): Promise<File>;
57+
58+
/**
59+
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
60+
* @param url The URL to request from.
61+
*/
62+
export function getBinary(url: string): Promise<ArrayBuffer>;
63+
64+
/**
65+
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
66+
* @param options An object that specifies various request options.
67+
*/
68+
export function getBinary(options: HttpsRequestOptions): Promise<ArrayBuffer>;

src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ImageSource } from '@nativescript/core';
2+
import { request } from './request';
3+
export * from './request';
4+
5+
export async function getString(arg: any): Promise<string> {
6+
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
7+
return r.content.toString();
8+
}
9+
10+
export async function getJSON<T>(arg: any): Promise<T> {
11+
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
12+
return r.content.toJSON();
13+
}
14+
15+
export async function getImage(arg: any): Promise<ImageSource> {
16+
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
17+
return r.content.toImage();
18+
}
19+
20+
export async function getFile(arg: any, destinationFilePath?: string): Promise<any> {
21+
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
22+
return r.content.toFile(destinationFilePath);
23+
}
24+
25+
export async function getBinary(arg: any): Promise<ArrayBuffer> {
26+
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
27+
return r.content.toArrayBuffer();
28+
}

src/https.android.ts renamed to src/request.android.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { File, HttpResponseEncoding, ImageSource, Utils } from '@nativescript/core';
2-
import * as Https from './https.common';
2+
import { CacheOptions, getFilenameFromUrl, HttpsFormDataParam, HttpsRequest, HttpsRequestOptions, HttpsResponseLegacy, HttpsSSLPinningOptions, interceptors, networkInterceptors, parseJSON } from '.';
33

4-
export { addNetworkInterceptor, addInterceptor } from './https.common';
4+
export { addNetworkInterceptor, addInterceptor } from './request.common';
55

66
interface Ipeer {
77
enabled: boolean;
@@ -21,7 +21,7 @@ const peer: Ipeer = {
2121

2222
let cache: okhttp3.Cache;
2323
let forceCache = false;
24-
export function setCache(options?: Https.CacheOptions) {
24+
export function setCache(options?: CacheOptions) {
2525
if (options) {
2626
forceCache = options.forceCache === true;
2727
cache = new okhttp3.Cache(new java.io.File(options.diskLocation), options.diskSize);
@@ -40,7 +40,7 @@ export function clearCache() {
4040

4141
let _timeout = 10;
4242

43-
class HttpsResponse implements Https.HttpsResponseLegacy {
43+
class HttpsResponseLegacyIOS implements HttpsResponseLegacy {
4444
private callback?: com.nativescript.https.OkHttpResponse.OkHttpResponseAsyncCallback;
4545
constructor(private response: com.nativescript.https.OkHttpResponse, private tag: string, private url: string) {}
4646

@@ -130,7 +130,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
130130
}
131131
// TODO: handle arraybuffer already stored
132132
this.stringResponse = this.stringResponse || this.response.asString();
133-
this.jsonResponse = this.stringResponse ? Https.parseJSON(this.stringResponse) : null;
133+
this.jsonResponse = this.stringResponse ? parseJSON(this.stringResponse) : null;
134134
return this.jsonResponse;
135135
} catch (err) {
136136
console.error('HttpsResponse.toJSON', err);
@@ -143,12 +143,12 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
143143
return this.jsonResponse;
144144
}
145145
if (this.stringResponse !== undefined) {
146-
this.jsonResponse = this.stringResponse ? Https.parseJSON(this.stringResponse) : null;
146+
this.jsonResponse = this.stringResponse ? parseJSON(this.stringResponse) : null;
147147
return this.jsonResponse;
148148
}
149149
// TODO: handle arraybuffer already stored
150150
const r = await this.toStringAsync();
151-
this.jsonResponse = r ? Https.parseJSON(r) : null;
151+
this.jsonResponse = r ? parseJSON(r) : null;
152152
return this.jsonResponse;
153153
}
154154

@@ -168,7 +168,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
168168
}
169169
// toFile(destinationFilePath: string): File {
170170
// if (!destinationFilePath) {
171-
// destinationFilePath = Https.getFilenameFromUrl(this.url);
171+
// destinationFilePath = getFilenameFromUrl(this.url);
172172
// }
173173
// const file = this.response.toFile(destinationFilePath);
174174
// return File.fromPath(destinationFilePath);
@@ -180,7 +180,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
180180
return Promise.resolve(this.file);
181181
}
182182
if (!destinationFilePath) {
183-
destinationFilePath = Https.getFilenameFromUrl(this.url);
183+
destinationFilePath = getFilenameFromUrl(this.url);
184184
}
185185
return new Promise((resolve, reject) => {
186186
this.getOrCreateCloseCallback();
@@ -192,7 +192,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
192192
}
193193
}
194194

195-
export function enableSSLPinning(options: Https.HttpsSSLPinningOptions) {
195+
export function enableSSLPinning(options: HttpsSSLPinningOptions) {
196196
if (!peer.host && !peer.certificate) {
197197
let certificate: string;
198198
let inputStream: java.io.FileInputStream;
@@ -268,8 +268,8 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt
268268
}
269269

270270
const builder = new okhttp3.OkHttpClient.Builder();
271-
Https.interceptors.forEach((interceptor) => builder.addInterceptor(interceptor));
272-
Https.networkInterceptors.forEach((interceptor) => builder.addNetworkInterceptor(interceptor));
271+
interceptors.forEach((interceptor) => builder.addInterceptor(interceptor));
272+
networkInterceptors.forEach((interceptor) => builder.addNetworkInterceptor(interceptor));
273273
if (peer.enabled === true) {
274274
if (peer.host || peer.certificate) {
275275
const spec = okhttp3.ConnectionSpec.MODERN_TLS;
@@ -389,7 +389,7 @@ const notClosedResponses: {
389389
const runningClients: { [k: string]: okhttp3.OkHttpClient } = {};
390390

391391
let OkHttpResponse: typeof com.nativescript.https.OkHttpResponse;
392-
export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolean = true): Https.HttpsRequest {
392+
export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = true): HttpsRequest {
393393
const client = getClient(false, opts.timeout);
394394

395395
const request = new okhttp3.Request.Builder();
@@ -434,7 +434,7 @@ export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolea
434434
const builder = new okhttp3.MultipartBody.Builder();
435435
builder.setType(MEDIA_TYPE);
436436

437-
(opts.body as Https.HttpsFormDataParam[]).forEach((param) => {
437+
(opts.body as HttpsFormDataParam[]).forEach((param) => {
438438
if (param.fileName && param.contentType) {
439439
const MEDIA_TYPE = okhttp3.MediaType.parse(param.contentType);
440440
builder.addFormDataPart(param.parameterName, param.fileName, okhttp3.RequestBody.create(MEDIA_TYPE, param.data));
@@ -519,7 +519,7 @@ export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolea
519519

520520
resolve({
521521
response,
522-
content: new HttpsResponse(nResponse, tag, opts.url),
522+
content: new HttpsResponseLegacyIOS(nResponse, tag, opts.url),
523523
statusCode,
524524
reason: message,
525525
get headers() {
@@ -548,7 +548,7 @@ export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolea
548548
};
549549
}
550550

551-
export function request(opts: Https.HttpsRequestOptions, useLegacy: boolean = true) {
551+
export function request(opts: HttpsRequestOptions, useLegacy: boolean = true) {
552552
return new Promise((resolve, reject) => {
553553
try {
554554
createRequest(opts, useLegacy).run(resolve, reject);

src/request.common.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { knownFolders, path } from '@nativescript/core';
2+
3+
export function getFilenameFromUrl(url: string) {
4+
const slashPos = url.lastIndexOf('/') + 1;
5+
const questionMarkPos = url.lastIndexOf('?');
6+
7+
let actualFileName: string;
8+
if (questionMarkPos !== -1) {
9+
actualFileName = url.substring(slashPos, questionMarkPos);
10+
} else {
11+
actualFileName = url.substring(slashPos);
12+
}
13+
14+
const result = path.join(knownFolders.documents().path, actualFileName);
15+
16+
return result;
17+
}
18+
export function parseJSON(source: string): any {
19+
const src = source.trim();
20+
if (src.lastIndexOf(')') === src.length - 1) {
21+
return JSON.parse(src.substring(src.indexOf('(') + 1, src.lastIndexOf(')')));
22+
}
23+
24+
return JSON.parse(src);
25+
}
26+
27+
export const interceptors = [];
28+
export function addInterceptor(interceptor) {
29+
interceptors.push(interceptor);
30+
}
31+
export const networkInterceptors = [];
32+
export function addNetworkInterceptor(interceptor) {
33+
networkInterceptors.push(interceptor);
34+
}

src/https.common.ts renamed to src/request.d.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { File, Headers, HttpRequestOptions, ImageSource, knownFolders, path } from '@nativescript/core';
1+
import { File, HttpRequestOptions, ImageSource } from '@nativescript/core';
2+
import * as Https from './request.common';
23

34
export interface HttpsSSLPinningOptions {
45
host: string;
@@ -84,35 +85,18 @@ export interface HttpsResponseLegacy<T = any> {
8485
// toFileAsync(destinationFilePath: string): Promise<File>;
8586
}
8687

87-
export function getFilenameFromUrl(url: string) {
88-
const slashPos = url.lastIndexOf('/') + 1;
89-
const questionMarkPos = url.lastIndexOf('?');
90-
91-
let actualFileName: string;
92-
if (questionMarkPos !== -1) {
93-
actualFileName = url.substring(slashPos, questionMarkPos);
94-
} else {
95-
actualFileName = url.substring(slashPos);
96-
}
97-
98-
const result = path.join(knownFolders.documents().path, actualFileName);
99-
100-
return result;
101-
}
102-
export function parseJSON(source: string): any {
103-
const src = source.trim();
104-
if (src.lastIndexOf(')') === src.length - 1) {
105-
return JSON.parse(src.substring(src.indexOf('(') + 1, src.lastIndexOf(')')));
106-
}
107-
108-
return JSON.parse(src);
109-
}
110-
111-
export const interceptors = [];
112-
export function addInterceptor(interceptor) {
113-
interceptors.push(interceptor);
114-
}
115-
export const networkInterceptors = [];
116-
export function addNetworkInterceptor(interceptor) {
117-
networkInterceptors.push(interceptor);
118-
}
88+
export function enableSSLPinning(options: HttpsSSLPinningOptions);
89+
90+
export function disableSSLPinning();
91+
92+
// export declare function request<T = any>(options: HttpsRequestOptions): Promise<HttpsResponse<HttpsResponseLegacy<T>>>;
93+
export declare function request<T = any, U extends boolean = true>(
94+
options: HttpsRequestOptions,
95+
useLegacy?: U
96+
): U extends true ? Promise<HttpsResponse<HttpsResponseLegacy<T>>> : Promise<HttpsResponse<T>>;
97+
export function setCache(options?: CacheOptions);
98+
export function clearCache();
99+
export function createRequest(opts: HttpsRequestOptions): HttpsRequest;
100+
export function cancelRequest(tag: string);
101+
export function addNetworkInterceptor(interceptor);
102+
export * from './request.common';

0 commit comments

Comments
 (0)