mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactoring circular imports
This commit is contained in:
@@ -6,7 +6,12 @@ import * as platformModule from "platform";
|
||||
import * as fsModule from "file-system";
|
||||
|
||||
// this is imported for definition purposes only
|
||||
import * as http from "http";
|
||||
import { Headers, HttpRequestOptions, HttpResponse } from "http";
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
|
||||
function parseJSON(source: string): any {
|
||||
var src = source.trim();
|
||||
@@ -58,7 +63,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
}
|
||||
|
||||
// read the headers
|
||||
var headers: http.Headers = {};
|
||||
var headers: Headers = {};
|
||||
if (result.headers) {
|
||||
var jHeaders = result.headers;
|
||||
var length = jHeaders.size();
|
||||
@@ -66,15 +71,14 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
var pair: org.nativescript.widgets.Async.Http.KeyValuePair;
|
||||
for (i = 0; i < length; i++) {
|
||||
pair = jHeaders.get(i);
|
||||
|
||||
(<any>http).addHeader(headers, pair.key, pair.value);
|
||||
addHeader(headers, pair.key, pair.value);
|
||||
}
|
||||
}
|
||||
|
||||
callbacks.resolveCallback({
|
||||
content: {
|
||||
raw: result.raw,
|
||||
toString: (encoding?: http.HttpResponseEncoding) => {
|
||||
toString: (encoding?: HttpResponseEncoding) => {
|
||||
let str: string;
|
||||
if (encoding) {
|
||||
str = decodeResponse(result.raw, encoding);
|
||||
@@ -87,7 +91,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
throw new Error("Response content may not be converted to string");
|
||||
}
|
||||
},
|
||||
toJSON: (encoding?: http.HttpResponseEncoding) => {
|
||||
toJSON: (encoding?: HttpResponseEncoding) => {
|
||||
let str: string;
|
||||
if (encoding) {
|
||||
str = decodeResponse(result.raw, encoding);
|
||||
@@ -136,7 +140,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
});
|
||||
}
|
||||
|
||||
function buildJavaOptions(options: http.HttpRequestOptions) {
|
||||
function buildJavaOptions(options: HttpRequestOptions) {
|
||||
if (typeof options.url !== "string") {
|
||||
throw new Error("Http request must provide a valid url.");
|
||||
}
|
||||
@@ -179,13 +183,13 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
|
||||
return javaOptions;
|
||||
}
|
||||
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
|
||||
if (options === undefined || options === null) {
|
||||
// TODO: Shouldn't we throw an error here - defensive programming
|
||||
return;
|
||||
}
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
|
||||
return new Promise<HttpResponse>((resolve, reject) => {
|
||||
try {
|
||||
// initialize the options
|
||||
var javaOptions = buildJavaOptions(options);
|
||||
@@ -210,10 +214,22 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
});
|
||||
}
|
||||
|
||||
function decodeResponse(raw: any, encoding?: http.HttpResponseEncoding) {
|
||||
function decodeResponse(raw: any, encoding?: HttpResponseEncoding) {
|
||||
let charsetName = "UTF-8";
|
||||
if (encoding === http.HttpResponseEncoding.GBK) {
|
||||
if (encoding === HttpResponseEncoding.GBK) {
|
||||
charsetName = 'GBK';
|
||||
}
|
||||
return raw.toString(charsetName)
|
||||
}
|
||||
|
||||
export function addHeader(headers: 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.d.ts
vendored
5
tns-core-modules/http/http-request.d.ts
vendored
@@ -1,6 +1,7 @@
|
||||
//@private
|
||||
|
||||
declare module "http/http-request" {
|
||||
import * as http from "http";
|
||||
export var request: (options: http.HttpRequestOptions) => Promise<http.HttpResponse>;
|
||||
import { HttpResponse, HttpRequestOptions, Headers } from "http";
|
||||
export var request: (options: HttpRequestOptions) => Promise<HttpResponse>;
|
||||
export function addHeader(headers: Headers, key: string, value: string): void;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* iOS specific http request implementation.
|
||||
*/
|
||||
|
||||
import * as http from "http";
|
||||
import { HttpRequestOptions, HttpResponse, Headers } from "http";
|
||||
|
||||
import * as types from "utils/types";
|
||||
import * as imageSourceModule from "image-source";
|
||||
@@ -13,6 +13,11 @@ 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";
|
||||
@@ -59,8 +64,8 @@ function ensureImageSource() {
|
||||
}
|
||||
}
|
||||
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
|
||||
return new Promise<HttpResponse>((resolve, reject) => {
|
||||
|
||||
try {
|
||||
var network = domainDebugger.getNetwork();
|
||||
@@ -101,12 +106,12 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
if (error) {
|
||||
reject(new Error(error.localizedDescription));
|
||||
} else {
|
||||
var headers: http.Headers = {};
|
||||
var headers: Headers = {};
|
||||
if (response && response.allHeaderFields) {
|
||||
var headerFields = response.allHeaderFields;
|
||||
|
||||
headerFields.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
|
||||
(<any>http).addHeader(headers, key, value);
|
||||
addHeader(headers, key, value);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -128,10 +133,8 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toString: (encoding?: http.HttpResponseEncoding) => { return NSDataToString(data, encoding); },
|
||||
toJSON: (encoding?: http.HttpResponseEncoding) => {
|
||||
return parseJSON(NSDataToString(data, encoding));
|
||||
},
|
||||
toString: (encoding?: any) => NSDataToString(data, encoding),
|
||||
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -180,10 +183,22 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
});
|
||||
}
|
||||
|
||||
function NSDataToString(data: any, encoding?: http.HttpResponseEncoding): string {
|
||||
function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
|
||||
let code = 4; //UTF8
|
||||
if (encoding === http.HttpResponseEncoding.GBK) {
|
||||
if (encoding === HttpResponseEncoding.GBK) {
|
||||
code = 1586;
|
||||
}
|
||||
return NSString.alloc().initWithDataEncoding(data, code).toString();
|
||||
}
|
||||
|
||||
export function addHeader(headers: 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;
|
||||
}
|
||||
}
|
||||
1
tns-core-modules/http/http.d.ts
vendored
1
tns-core-modules/http/http.d.ts
vendored
@@ -122,6 +122,7 @@ declare module "http" {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates the content of an HttpResponse.
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import * as image from "image-source";
|
||||
import * as httpRequest from "http/http-request";
|
||||
import * as dts from "http";
|
||||
|
||||
global.moduleMerge(httpRequest, exports);
|
||||
|
||||
@@ -50,16 +49,4 @@ export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
|
||||
}
|
||||
}, e => reject(e));
|
||||
});
|
||||
}
|
||||
|
||||
export function addHeader(headers: dts.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user