mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
http module improved with more methods
This commit is contained in:
@ -159,7 +159,9 @@
|
|||||||
<TypeScriptCompile Include="android17.d.ts" />
|
<TypeScriptCompile Include="android17.d.ts" />
|
||||||
<TypeScriptCompile Include="promises\promises.ts" />
|
<TypeScriptCompile Include="promises\promises.ts" />
|
||||||
<TypeScriptCompile Include="promises\index.ts" />
|
<TypeScriptCompile Include="promises\index.ts" />
|
||||||
<TypeScriptCompile Include="http\http.ts" />
|
<TypeScriptCompile Include="http\http.ts">
|
||||||
|
<DependentUpon>http.d.ts</DependentUpon>
|
||||||
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="ios7.d.ts" />
|
<TypeScriptCompile Include="ios7.d.ts" />
|
||||||
<TypeScriptCompile Include="globals\index.ts" />
|
<TypeScriptCompile Include="globals\index.ts" />
|
||||||
<TypeScriptCompile Include="timer\index.ts" />
|
<TypeScriptCompile Include="timer\index.ts" />
|
||||||
@ -189,6 +191,8 @@
|
|||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="application\application-common.ts" />
|
<TypeScriptCompile Include="application\application-common.ts" />
|
||||||
<Content Include="image-source\Readme.md" />
|
<Content Include="image-source\Readme.md" />
|
||||||
|
<TypeScriptCompile Include="http\http.d.ts" />
|
||||||
|
<Content Include="image\Readme.md" />
|
||||||
<TypeScriptCompile Include="local-settings\index.ts" />
|
<TypeScriptCompile Include="local-settings\index.ts" />
|
||||||
<TypeScriptCompile Include="local-settings\local-settings.d.ts" />
|
<TypeScriptCompile Include="local-settings\local-settings.d.ts" />
|
||||||
<TypeScriptCompile Include="local-settings\local-settings.android.ts">
|
<TypeScriptCompile Include="local-settings\local-settings.android.ts">
|
||||||
|
@ -43,13 +43,13 @@ export var test_getString_fail = function () {
|
|||||||
var completed: boolean;
|
var completed: boolean;
|
||||||
var isReady = function () { return completed; }
|
var isReady = function () { return completed; }
|
||||||
|
|
||||||
http.getString("hgfttp://httpbin.org/get").fail(function (e) {
|
http.getString({ url: "hgfttp://httpbin.org/get", method: "GET", timeout: 2000}).fail(function (e) {
|
||||||
completed = true;
|
completed = true;
|
||||||
result = e;
|
result = e;
|
||||||
});
|
});
|
||||||
|
|
||||||
TKUnit.waitUntilReady(isReady, 3);
|
TKUnit.waitUntilReady(isReady, 3);
|
||||||
TKUnit.assert(result instanceof Error, "Result from getString().fail() should be Error!");
|
TKUnit.assert(result instanceof Error, "Result from getString().fail() should be Error! Current type is " + typeof result);
|
||||||
};
|
};
|
||||||
|
|
||||||
export var test_getJSON_isDefined = function () {
|
export var test_getJSON_isDefined = function () {
|
||||||
@ -86,13 +86,13 @@ export var test_getJSON_fail = function () {
|
|||||||
var completed: boolean;
|
var completed: boolean;
|
||||||
var isReady = function () { return completed; }
|
var isReady = function () { return completed; }
|
||||||
|
|
||||||
http.getJSON("hgfttp://httpbin.org/get").fail(function (e) {
|
http.getJSON({ url: "hgfttp://httpbin.org/get", method: "GET", timeout: 2000 }).fail(function (e) {
|
||||||
completed = true;
|
completed = true;
|
||||||
result = e;
|
result = e;
|
||||||
});
|
});
|
||||||
|
|
||||||
TKUnit.waitUntilReady(isReady, 3);
|
TKUnit.waitUntilReady(isReady, 3);
|
||||||
TKUnit.assert(result instanceof Error, "Result from getJSON().fail() should be Error!");
|
TKUnit.assert(result instanceof Error, "Result from getJSON().fail() should be Error! Current type is " + typeof result);
|
||||||
};
|
};
|
||||||
|
|
||||||
export var test_getImage_isDefined = function () {
|
export var test_getImage_isDefined = function () {
|
||||||
@ -129,13 +129,13 @@ export var test_getImage_fail = function () {
|
|||||||
var completed: boolean;
|
var completed: boolean;
|
||||||
var isReady = function () { return completed; }
|
var isReady = function () { return completed; }
|
||||||
|
|
||||||
http.getImage("htadvtp://www.google.com/images/errors/logo_sm_2.pngm").fail(function (e) {
|
http.getImage({ url: "hgfttp://httpbin.org/get", method: "GET", timeout: 2000 }).fail(function (e) {
|
||||||
completed = true;
|
completed = true;
|
||||||
result = e;
|
result = e;
|
||||||
});
|
});
|
||||||
|
|
||||||
TKUnit.waitUntilReady(isReady, 3);
|
TKUnit.waitUntilReady(isReady, 3);
|
||||||
TKUnit.assert(result instanceof Error, "Result from getImage().fail() should be Error!");
|
TKUnit.assert(result instanceof Error, "Result from getImage().fail() should be Error! Current type is " + typeof result);
|
||||||
};
|
};
|
||||||
|
|
||||||
export var test_request_isDefined = function () {
|
export var test_request_isDefined = function () {
|
||||||
@ -153,7 +153,7 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
TKUnit.waitUntilReady(isReady, 3);
|
TKUnit.waitUntilReady(isReady, 3);
|
||||||
TKUnit.assert(result instanceof Error, "Result from request().fail() should be Error!");
|
TKUnit.assert(result instanceof Error, "Result from request().fail() should be Error! Current type is " + typeof result);
|
||||||
};
|
};
|
||||||
|
|
||||||
export var test_request_responseStatusCodeShouldBeDefined = function () {
|
export var test_request_responseStatusCodeShouldBeDefined = function () {
|
||||||
|
12
http/http.d.ts
vendored
Normal file
12
http/http.d.ts
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import image = require("image-source/image-source");
|
||||||
|
import promises = require("promises/promises");
|
||||||
|
import request = require("http/http-request");
|
||||||
|
|
||||||
|
export declare function getString(url: string): promises.Promise<string>
|
||||||
|
export declare function getString(options: request.HttpRequestOptions): promises.Promise<string>
|
||||||
|
|
||||||
|
export declare function getJSON<T>(url: string): promises.Promise<T>
|
||||||
|
export declare function getJSON<T>(options: request.HttpRequestOptions): promises.Promise<T>
|
||||||
|
|
||||||
|
export declare function getImage(url: string): promises.Promise<image.ImageSource>
|
||||||
|
export declare function getImage(options: request.HttpRequestOptions): promises.Promise<image.ImageSource>
|
17
http/http.ts
17
http/http.ts
@ -1,4 +1,4 @@
|
|||||||
import image_module = require("image-source/image-source");
|
import image = require("image-source/image-source");
|
||||||
import promises = require("promises/promises");
|
import promises = require("promises/promises");
|
||||||
import http = require("http/http-request");
|
import http = require("http/http-request");
|
||||||
|
|
||||||
@ -11,10 +11,10 @@ export interface HttpContent extends http.HttpContent { };
|
|||||||
/**
|
/**
|
||||||
* Gets string from url.
|
* Gets string from url.
|
||||||
*/
|
*/
|
||||||
export function getString(url: string): promises.Promise<string> {
|
export function getString(arg: any): promises.Promise<string> {
|
||||||
var d = promises.defer<string>();
|
var d = promises.defer<string>();
|
||||||
|
|
||||||
http.request({ url: url, method: "GET" })
|
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => d.resolve(r.content.toString()))
|
.then(r => d.resolve(r.content.toString()))
|
||||||
.fail(e => d.reject(e));
|
.fail(e => d.reject(e));
|
||||||
|
|
||||||
@ -24,10 +24,10 @@ export function getString(url: string): promises.Promise<string> {
|
|||||||
/**
|
/**
|
||||||
* Gets JSON from url.
|
* Gets JSON from url.
|
||||||
*/
|
*/
|
||||||
export function getJSON<T>(url: string): promises.Promise<T> {
|
export function getJSON<T>(arg: any): promises.Promise<T> {
|
||||||
var d = promises.defer<T>();
|
var d = promises.defer<T>();
|
||||||
|
|
||||||
http.request({ url: url, method: "GET" })
|
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => d.resolve(r.content.toJSON()))
|
.then(r => d.resolve(r.content.toJSON()))
|
||||||
.fail(e => d.reject(e));
|
.fail(e => d.reject(e));
|
||||||
|
|
||||||
@ -37,10 +37,11 @@ export function getJSON<T>(url: string): promises.Promise<T> {
|
|||||||
/**
|
/**
|
||||||
* Gets image from url.
|
* Gets image from url.
|
||||||
*/
|
*/
|
||||||
export function getImage(url: string): promises.Promise<image_module.ImageSource> {
|
|
||||||
var d = promises.defer<image_module.ImageSource>();
|
|
||||||
|
|
||||||
http.request({ url: url, method: "GET" })
|
export function getImage(arg: any): promises.Promise<image.ImageSource> {
|
||||||
|
var d = promises.defer<image.ImageSource>();
|
||||||
|
|
||||||
|
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
|
||||||
.then(r => d.resolve(r.content.toImage()))
|
.then(r => d.resolve(r.content.toImage()))
|
||||||
.fail(e => d.reject(e));
|
.fail(e => d.reject(e));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user