mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
No more ambient modules for tns-core-modules/* subpackages.
- Use path mappings in tsconfig.json to resolve module typings - Only use ambient mobules for global API's - Move single-file modules to a subdir with the same name so that we can provide a hand-written typing next to it (via package.json) - Delete all mentions of tns-core-modules.d.ts - Delete reference d.ts assembly build steps. Not needed anymore. - HACK! Use a <reference> for global typings in application.d.ts to avoid publishing a separate @types/tns-core-modules package. - Rename declarations.d.ts to tns-core-modules.d.ts to preserve JS project mappings in references.d.ts (the only place we use those)
This commit is contained in:
committed by
Hristo Deshev
parent
1af8c6ca8e
commit
b45cbe929b
235
tns-core-modules/http/http-request/http-request.android.ts
Normal file
235
tns-core-modules/http/http-request/http-request.android.ts
Normal file
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
* Android specific http request implementation.
|
||||
*/
|
||||
import * as imageSourceModule from "image-source";
|
||||
import * as platformModule from "platform";
|
||||
import * as fsModule from "file-system";
|
||||
|
||||
// this is imported for definition purposes only
|
||||
import * as http from "../../http";
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
|
||||
function parseJSON(source: string): any {
|
||||
var src = source.trim();
|
||||
if (src.lastIndexOf(")") === src.length - 1) {
|
||||
return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")")));
|
||||
}
|
||||
|
||||
return JSON.parse(src);
|
||||
}
|
||||
|
||||
var requestIdCounter = 0;
|
||||
var pendingRequests = {};
|
||||
|
||||
var imageSource: typeof imageSourceModule;
|
||||
function ensureImageSource() {
|
||||
if (!imageSource) {
|
||||
imageSource = require("image-source");
|
||||
}
|
||||
}
|
||||
|
||||
var platform: typeof platformModule;
|
||||
function ensurePlatform() {
|
||||
if (!platform) {
|
||||
platform = require("platform");
|
||||
}
|
||||
}
|
||||
|
||||
var 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function onRequestComplete(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult) {
|
||||
var callbacks = pendingRequests[requestId];
|
||||
delete pendingRequests[requestId];
|
||||
|
||||
if (result.error) {
|
||||
callbacks.rejectCallback(new Error(result.error.toString()));
|
||||
return;
|
||||
}
|
||||
|
||||
// read the headers
|
||||
var headers: http.Headers = {};
|
||||
if (result.headers) {
|
||||
var jHeaders = result.headers;
|
||||
var length = jHeaders.size();
|
||||
var i;
|
||||
var pair: org.nativescript.widgets.Async.Http.KeyValuePair;
|
||||
for (i = 0; i < length; i++) {
|
||||
pair = jHeaders.get(i);
|
||||
addHeader(headers, pair.key, pair.value);
|
||||
}
|
||||
}
|
||||
|
||||
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(imageSource.fromNativeSource(result.responseAsImage));
|
||||
}
|
||||
else {
|
||||
rejectImage(new Error("Response content may not be converted to an Image"));
|
||||
}
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath?: string) => {
|
||||
var fs: typeof fsModule = require("file-system");
|
||||
var fileName = callbacks.url;
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
|
||||
}
|
||||
var stream: java.io.FileOutputStream;
|
||||
try {
|
||||
var javaFile = new java.io.File(destinationFilePath);
|
||||
stream = new java.io.FileOutputStream(javaFile);
|
||||
stream.write(result.raw.toByteArray());
|
||||
return fs.File.fromPath(destinationFilePath);
|
||||
}
|
||||
catch (exception) {
|
||||
throw new Error(`Cannot save file with path: ${destinationFilePath}.`);
|
||||
}
|
||||
finally {
|
||||
if (stream) {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
statusCode: result.statusCode,
|
||||
headers: headers
|
||||
});
|
||||
}
|
||||
|
||||
function buildJavaOptions(options: http.HttpRequestOptions) {
|
||||
if (typeof options.url !== "string") {
|
||||
throw new Error("Http request must provide a valid url.");
|
||||
}
|
||||
|
||||
var 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) {
|
||||
var arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>();
|
||||
var pair = org.nativescript.widgets.Async.Http.KeyValuePair;
|
||||
|
||||
for (var 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
|
||||
var screen = platform.screen.mainScreen;
|
||||
javaOptions.screenWidth = screen.widthPixels;
|
||||
javaOptions.screenHeight = screen.heightPixels;
|
||||
|
||||
return javaOptions;
|
||||
}
|
||||
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
if (options === undefined || options === null) {
|
||||
// TODO: Shouldn't we throw an error here - defensive programming
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
try {
|
||||
// initialize the options
|
||||
var javaOptions = buildJavaOptions(options);
|
||||
|
||||
// remember the callbacks so that we can use them when the CompleteCallback is called
|
||||
var 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: http.Headers, key: string, value: string): void {
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
(<string[]>headers[key]).push(value);
|
||||
} else {
|
||||
let values: string[] = [<string>headers[key]];
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
}
|
||||
5
tns-core-modules/http/http-request/http-request.d.ts
vendored
Normal file
5
tns-core-modules/http/http-request/http-request.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
//@private
|
||||
|
||||
import * as http from "..";
|
||||
export var request: (options: http.HttpRequestOptions) => Promise<http.HttpResponse>;
|
||||
export function addHeader(headers: http.Headers, key: string, value: string): void;
|
||||
203
tns-core-modules/http/http-request/http-request.ios.ts
Normal file
203
tns-core-modules/http/http-request/http-request.ios.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
/**
|
||||
* iOS specific http request implementation.
|
||||
*/
|
||||
|
||||
import * as http from "../../http";
|
||||
import * as types from "utils/types";
|
||||
import * as imageSourceModule from "image-source";
|
||||
import * as fsModule from "file-system";
|
||||
|
||||
import * as utils from "utils/utils";
|
||||
import getter = utils.ios.getter;
|
||||
|
||||
import * as domainDebugger from "../../debugger/debugger";
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
|
||||
var device = utils.ios.getter(UIDevice, UIDevice.currentDevice).userInterfaceIdiom === UIUserInterfaceIdiom.Phone ? "Phone" : "Pad";
|
||||
|
||||
var GET = "GET";
|
||||
var USER_AGENT_HEADER = "User-Agent";
|
||||
var USER_AGENT = `Mozilla/5.0 (i${device}; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25`;
|
||||
var sessionConfig = getter(NSURLSessionConfiguration, NSURLSessionConfiguration.defaultSessionConfiguration);
|
||||
var queue = getter(NSOperationQueue, NSOperationQueue.mainQueue);
|
||||
|
||||
function parseJSON(source: string): any {
|
||||
var src = source.trim();
|
||||
if (src.lastIndexOf(")") === src.length - 1) {
|
||||
return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")")));
|
||||
}
|
||||
|
||||
return JSON.parse(src);
|
||||
}
|
||||
|
||||
class NSURLSessionTaskDelegateImpl extends NSObject implements NSURLSessionTaskDelegate {
|
||||
public static ObjCProtocols = [NSURLSessionTaskDelegate];
|
||||
public URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse, request: NSURLRequest, completionHandler: (p1: NSURLRequest) => void): void {
|
||||
completionHandler(null);
|
||||
}
|
||||
}
|
||||
var sessionTaskDelegateInstance: NSURLSessionTaskDelegateImpl = <NSURLSessionTaskDelegateImpl>NSURLSessionTaskDelegateImpl.new();
|
||||
|
||||
var defaultSession;
|
||||
function ensureDefaultSession() {
|
||||
if (!defaultSession) {
|
||||
defaultSession = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
|
||||
}
|
||||
}
|
||||
|
||||
var sessionNotFollowingRedirects;
|
||||
function ensureSessionNotFollowingRedirects() {
|
||||
if (!sessionNotFollowingRedirects) {
|
||||
sessionNotFollowingRedirects = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, sessionTaskDelegateInstance, queue);
|
||||
}
|
||||
}
|
||||
|
||||
var imageSource: typeof imageSourceModule;
|
||||
function ensureImageSource() {
|
||||
if (!imageSource) {
|
||||
imageSource = require("image-source");
|
||||
}
|
||||
}
|
||||
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
|
||||
try {
|
||||
var network = domainDebugger.getNetwork();
|
||||
var debugRequest = network && network.create();
|
||||
|
||||
var urlRequest = NSMutableURLRequest.requestWithURL(
|
||||
NSURL.URLWithString(options.url));
|
||||
|
||||
urlRequest.HTTPMethod = types.isDefined(options.method) ? options.method : GET;
|
||||
|
||||
urlRequest.setValueForHTTPHeaderField(USER_AGENT, USER_AGENT_HEADER);
|
||||
|
||||
if (options.headers) {
|
||||
for (var header in options.headers) {
|
||||
urlRequest.setValueForHTTPHeaderField(options.headers[header] + "", header);
|
||||
}
|
||||
}
|
||||
|
||||
if (types.isString(options.content) || options.content instanceof FormData) {
|
||||
urlRequest.HTTPBody = NSString.stringWithString(options.content.toString()).dataUsingEncoding(4);
|
||||
}
|
||||
|
||||
if (types.isNumber(options.timeout)) {
|
||||
urlRequest.timeoutInterval = options.timeout / 1000;
|
||||
}
|
||||
|
||||
var session;
|
||||
if (types.isBoolean(options.dontFollowRedirects) && options.dontFollowRedirects) {
|
||||
ensureSessionNotFollowingRedirects();
|
||||
session = sessionNotFollowingRedirects;
|
||||
} else {
|
||||
ensureDefaultSession();
|
||||
session = defaultSession;
|
||||
}
|
||||
|
||||
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
|
||||
function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
|
||||
if (error) {
|
||||
reject(new Error(error.localizedDescription));
|
||||
} else {
|
||||
var headers: http.Headers = {};
|
||||
if (response && response.allHeaderFields) {
|
||||
var headerFields = response.allHeaderFields;
|
||||
|
||||
headerFields.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
|
||||
addHeader(headers, key, value);
|
||||
});
|
||||
}
|
||||
|
||||
if (debugRequest) {
|
||||
debugRequest.mimeType = response.MIMEType;
|
||||
debugRequest.data = data;
|
||||
var debugResponse = {
|
||||
url: options.url,
|
||||
status: response.statusCode,
|
||||
statusText: NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode),
|
||||
headers: headers,
|
||||
mimeType: response.MIMEType,
|
||||
fromDiskCache: false
|
||||
}
|
||||
debugRequest.responseReceived(debugResponse);
|
||||
debugRequest.loadingFinished();
|
||||
}
|
||||
|
||||
resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toString: (encoding?: any) => NSDataToString(data, encoding),
|
||||
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
return new Promise((resolve, reject) => {
|
||||
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
|
||||
if (image) {
|
||||
resolve(imageSource.fromNativeSource(image))
|
||||
} else {
|
||||
reject(new Error("Response content may not be converted to an Image"));
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath?: string) => {
|
||||
var fs: typeof fsModule = require("file-system");
|
||||
var fileName = options.url;
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
|
||||
}
|
||||
if (data instanceof NSData) {
|
||||
data.writeToFileAtomically(destinationFilePath, true);
|
||||
return fs.File.fromPath(destinationFilePath);
|
||||
} else {
|
||||
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
|
||||
}
|
||||
}
|
||||
},
|
||||
statusCode: response.statusCode,
|
||||
headers: headers
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (options.url && debugRequest) {
|
||||
var request = {
|
||||
url: options.url,
|
||||
method: "GET",
|
||||
headers: options.headers
|
||||
};
|
||||
debugRequest.requestWillBeSent(request);
|
||||
}
|
||||
|
||||
dataTask.resume();
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
|
||||
let code = 4; //UTF8
|
||||
if (encoding === HttpResponseEncoding.GBK) {
|
||||
code = 1586;
|
||||
}
|
||||
return NSString.alloc().initWithDataEncoding(data, code).toString();
|
||||
}
|
||||
|
||||
export function addHeader(headers: http.Headers, key: string, value: string): void {
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
(<string[]>headers[key]).push(value);
|
||||
} else {
|
||||
let values: string[] = [<string>headers[key]];
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
}
|
||||
7
tns-core-modules/http/http-request/package.json
Normal file
7
tns-core-modules/http/http-request/package.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name" : "http-request",
|
||||
"main" : "http-request",
|
||||
"types": "http-request.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user