http module improved with more methods

This commit is contained in:
Vladimir Enchev
2014-05-15 17:14:04 +03:00
parent c946c24ae2
commit c6483cd32a
4 changed files with 33 additions and 16 deletions

View File

@ -159,7 +159,9 @@
<TypeScriptCompile Include="android17.d.ts" />
<TypeScriptCompile Include="promises\promises.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="globals\index.ts" />
<TypeScriptCompile Include="timer\index.ts" />
@ -189,6 +191,8 @@
</TypeScriptCompile>
<TypeScriptCompile Include="application\application-common.ts" />
<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\local-settings.d.ts" />
<TypeScriptCompile Include="local-settings\local-settings.android.ts">

View File

@ -43,13 +43,13 @@ export var test_getString_fail = function () {
var completed: boolean;
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;
result = e;
});
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 () {
@ -86,13 +86,13 @@ export var test_getJSON_fail = function () {
var completed: boolean;
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;
result = e;
});
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 () {
@ -129,13 +129,13 @@ export var test_getImage_fail = function () {
var completed: boolean;
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;
result = e;
});
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 () {
@ -153,7 +153,7 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function () {
});
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 () {

12
http/http.d.ts vendored Normal file
View 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>

View File

@ -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 http = require("http/http-request");
@ -11,10 +11,10 @@ export interface HttpContent extends http.HttpContent { };
/**
* 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>();
http.request({ url: url, method: "GET" })
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => d.resolve(r.content.toString()))
.fail(e => d.reject(e));
@ -24,10 +24,10 @@ export function getString(url: string): promises.Promise<string> {
/**
* 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>();
http.request({ url: url, method: "GET" })
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => d.resolve(r.content.toJSON()))
.fail(e => d.reject(e));
@ -37,10 +37,11 @@ export function getJSON<T>(url: string): promises.Promise<T> {
/**
* 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()))
.fail(e => d.reject(e));