mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00
feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
This commit is contained in:

committed by
GitHub

parent
6c7139477e
commit
cc97a16800
268
nativescript-core/http/http-request/http-request.android.ts
Normal file
268
nativescript-core/http/http-request/http-request.android.ts
Normal file
@ -0,0 +1,268 @@
|
||||
// imported for definition purposes only
|
||||
import * as httpModule from "../../http";
|
||||
import * as imageSourceModule from "../../image-source";
|
||||
import * as platformModule from "../../platform";
|
||||
import * as fsModule from "../../file-system";
|
||||
|
||||
import { getFilenameFromUrl } from "./http-request-common";
|
||||
import { NetworkAgent } from "../../debugger/debugger";
|
||||
|
||||
export enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
|
||||
function parseJSON(source: string): any {
|
||||
const src = source.trim();
|
||||
if (src.lastIndexOf(")") === src.length - 1) {
|
||||
return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")")));
|
||||
}
|
||||
|
||||
return JSON.parse(src);
|
||||
}
|
||||
|
||||
let requestIdCounter = 0;
|
||||
const pendingRequests = {};
|
||||
|
||||
let imageSource: typeof imageSourceModule;
|
||||
function ensureImageSource() {
|
||||
if (!imageSource) {
|
||||
imageSource = require("../../image-source");
|
||||
}
|
||||
}
|
||||
|
||||
let platform: typeof platformModule;
|
||||
function ensurePlatform() {
|
||||
if (!platform) {
|
||||
platform = require("../../platform");
|
||||
}
|
||||
}
|
||||
|
||||
let fs: typeof fsModule;
|
||||
function ensureFileSystem() {
|
||||
if (!fs) {
|
||||
fs = require("../../file-system");
|
||||
}
|
||||
}
|
||||
|
||||
let completeCallback: org.nativescript.widgets.Async.CompleteCallback;
|
||||
function ensureCompleteCallback() {
|
||||
if (completeCallback) {
|
||||
return;
|
||||
}
|
||||
|
||||
completeCallback = new org.nativescript.widgets.Async.CompleteCallback({
|
||||
onComplete: function (result: any, context: any) {
|
||||
// as a context we will receive the id of the request
|
||||
onRequestComplete(context, result);
|
||||
},
|
||||
onError: function (error: string, context: any) {
|
||||
onRequestError(error, context);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function onRequestComplete(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult) {
|
||||
const callbacks = pendingRequests[requestId];
|
||||
delete pendingRequests[requestId];
|
||||
|
||||
if (result.error) {
|
||||
callbacks.rejectCallback(new Error(result.error.toString()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// read the headers
|
||||
const headers: httpModule.Headers = {};
|
||||
if (result.headers) {
|
||||
const jHeaders = result.headers;
|
||||
const length = jHeaders.size();
|
||||
let pair: org.nativescript.widgets.Async.Http.KeyValuePair;
|
||||
for (let i = 0; i < length; i++) {
|
||||
pair = jHeaders.get(i);
|
||||
addHeader(headers, pair.key, pair.value);
|
||||
}
|
||||
}
|
||||
|
||||
// send response data (for requestId) to network debugger
|
||||
if (global.__inspector && global.__inspector.isConnected) {
|
||||
NetworkAgent.responseReceived(requestId, result, headers);
|
||||
}
|
||||
|
||||
callbacks.resolveCallback({
|
||||
content: {
|
||||
raw: result.raw,
|
||||
toString: (encoding?: HttpResponseEncoding) => {
|
||||
let str: string;
|
||||
if (encoding) {
|
||||
str = decodeResponse(result.raw, encoding);
|
||||
} else {
|
||||
str = result.responseAsString;
|
||||
}
|
||||
if (typeof str === "string") {
|
||||
return str;
|
||||
} else {
|
||||
throw new Error("Response content may not be converted to string");
|
||||
}
|
||||
},
|
||||
toJSON: (encoding?: HttpResponseEncoding) => {
|
||||
let str: string;
|
||||
if (encoding) {
|
||||
str = decodeResponse(result.raw, encoding);
|
||||
} else {
|
||||
str = result.responseAsString;
|
||||
}
|
||||
|
||||
return parseJSON(str);
|
||||
},
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
|
||||
return new Promise<any>((resolveImage, rejectImage) => {
|
||||
if (result.responseAsImage != null) {
|
||||
resolveImage(new imageSource.ImageSource(result.responseAsImage));
|
||||
}
|
||||
else {
|
||||
rejectImage(new Error("Response content may not be converted to an Image"));
|
||||
}
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath: string) => {
|
||||
ensureFileSystem();
|
||||
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = getFilenameFromUrl(callbacks.url);
|
||||
}
|
||||
let stream: java.io.FileOutputStream;
|
||||
try {
|
||||
// ensure destination path exists by creating any missing parent directories
|
||||
const file = fs.File.fromPath(destinationFilePath);
|
||||
|
||||
const javaFile = new java.io.File(destinationFilePath);
|
||||
stream = new java.io.FileOutputStream(javaFile);
|
||||
stream.write(result.raw.toByteArray());
|
||||
|
||||
return file;
|
||||
}
|
||||
catch (exception) {
|
||||
throw new Error(`Cannot save file with path: ${destinationFilePath}.`);
|
||||
}
|
||||
finally {
|
||||
if (stream) {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
statusCode: result.statusCode,
|
||||
headers: headers
|
||||
});
|
||||
}
|
||||
|
||||
function onRequestError(error: string, requestId: number) {
|
||||
const callbacks = pendingRequests[requestId];
|
||||
delete pendingRequests[requestId];
|
||||
if (callbacks) {
|
||||
callbacks.rejectCallback(new Error(error));
|
||||
}
|
||||
}
|
||||
|
||||
function buildJavaOptions(options: httpModule.HttpRequestOptions) {
|
||||
if (typeof options.url !== "string") {
|
||||
throw new Error("Http request must provide a valid url.");
|
||||
}
|
||||
|
||||
const javaOptions = new org.nativescript.widgets.Async.Http.RequestOptions();
|
||||
|
||||
javaOptions.url = options.url;
|
||||
|
||||
if (typeof options.method === "string") {
|
||||
javaOptions.method = options.method;
|
||||
}
|
||||
if (typeof options.content === "string" || options.content instanceof FormData) {
|
||||
javaOptions.content = options.content.toString();
|
||||
}
|
||||
if (typeof options.timeout === "number") {
|
||||
javaOptions.timeout = options.timeout;
|
||||
}
|
||||
if (typeof options.dontFollowRedirects === "boolean") {
|
||||
javaOptions.dontFollowRedirects = options.dontFollowRedirects;
|
||||
}
|
||||
|
||||
if (options.headers) {
|
||||
const arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>();
|
||||
const pair = org.nativescript.widgets.Async.Http.KeyValuePair;
|
||||
|
||||
for (let key in options.headers) {
|
||||
arrayList.add(new pair(key, options.headers[key] + ""));
|
||||
}
|
||||
|
||||
javaOptions.headers = arrayList;
|
||||
}
|
||||
|
||||
ensurePlatform();
|
||||
|
||||
// pass the maximum available image size to the request options in case we need a bitmap conversion
|
||||
const screen = platform.screen.mainScreen;
|
||||
javaOptions.screenWidth = screen.widthPixels;
|
||||
javaOptions.screenHeight = screen.heightPixels;
|
||||
|
||||
return javaOptions;
|
||||
}
|
||||
|
||||
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
|
||||
if (options === undefined || options === null) {
|
||||
// TODO: Shouldn't we throw an error here - defensive programming
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
|
||||
try {
|
||||
// initialize the options
|
||||
const javaOptions = buildJavaOptions(options);
|
||||
|
||||
// send request data to network debugger
|
||||
if (global.__inspector && global.__inspector.isConnected) {
|
||||
NetworkAgent.requestWillBeSent(requestIdCounter, options);
|
||||
}
|
||||
|
||||
// remember the callbacks so that we can use them when the CompleteCallback is called
|
||||
const callbacks = {
|
||||
url: options.url,
|
||||
resolveCallback: resolve,
|
||||
rejectCallback: reject
|
||||
};
|
||||
pendingRequests[requestIdCounter] = callbacks;
|
||||
|
||||
ensureCompleteCallback();
|
||||
//make the actual async call
|
||||
org.nativescript.widgets.Async.Http.MakeRequest(javaOptions, completeCallback, new java.lang.Integer(requestIdCounter));
|
||||
|
||||
// increment the id counter
|
||||
requestIdCounter++;
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function decodeResponse(raw: any, encoding?: HttpResponseEncoding) {
|
||||
let charsetName = "UTF-8";
|
||||
if (encoding === HttpResponseEncoding.GBK) {
|
||||
charsetName = "GBK";
|
||||
}
|
||||
|
||||
return raw.toString(charsetName);
|
||||
}
|
||||
|
||||
export function addHeader(headers: httpModule.Headers, key: string, value: string): void {
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
(<string[]>headers[key]).push(value);
|
||||
} else {
|
||||
const values: string[] = [<string>headers[key]];
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user