async-http
Asynchronous HTTP request API
Examples
import { Promise } from 'async-promise';
import { HttpClient } from 'async-http';
let client = new HttpClient("https://api.github.com/");
client.getAsJsonAsync("/repos/Microsoft/TypeScript/commits/a46a6106a8f01131ef208fa51fe69b3d06574507").then(response => {
console.log(response.commit.message);
});
API
declare module "async-http" {
import { Promise, CancellationToken } from 'async-promise';
export type JsonReplacer = any[] | ((key: string, value: any) => string);
export class Uri {
static SCHEME_HTTP: string;
static SCHEME_HTTPS: string;
protocol: string;
hostname: string;
port: number;
pathname: string;
search: string;
hash: string;
absolute: boolean;
constructor(uri: string);
constructor(baseUri: string | Uri, uri: string | Uri);
origin: string;
host: string;
scheme: string;
isSameOrigin(uri: string | Uri): boolean;
toString(format?: string): string;
static parse(uri: string): Uri;
static combine(baseUri: string | Uri, uri: string | Uri): Uri;
}
export module QueryString {
interface QueryStringMap {
[key: string]: string | number | boolean | (string | number | boolean)[];
}
function stringify(obj: any): string;
function parse(text: string): QueryStringMap;
}
export class HttpRequest {
private _headers;
content: HttpContent;
method: string;
url: Uri;
constructor(method?: string, url?: string | Uri);
headers: HttpHeaders;
}
export interface HttpHeaders {
"Content-Length"?: string;
"Content-Type"?: string;
"Accepts"?: string;
"User-Agent"?: string;
[header: string]: string;
}
export class HttpResponse {
private _headers;
request: HttpRequest;
statusCode: number;
statusText: string;
content: HttpContent;
constructor(statusCode?: number);
headers: HttpHeaders;
}
export class HttpClient {
private _headers;
private _cts;
private _closed;
baseUrl: Uri;
withCredentials: boolean;
timeout: number;
username: string;
password: string;
constructor(baseUrl?: string | Uri);
headers: HttpHeaders;
close(): void;
getStringAsync(url: string | Uri): Promise<string>;
getAsync(url: string | Uri, token?: CancellationToken): Promise<HttpResponse>;
postAsync(url: string | Uri, body: HttpContent, token?: CancellationToken): Promise<HttpResponse>;
postJsonAsync(url: string | Uri, value: any, jsonReplacer?: any[] | ((key: string, value: any) => string), token?: CancellationToken): Promise<HttpResponse>;
putAsync(url: string | Uri, content: HttpContent, token?: CancellationToken): Promise<HttpResponse>;
putJsonAsync(url: string | Uri, value: any, jsonReplacer?: any[] | ((key: string, value: any) => string), token?: CancellationToken): Promise<HttpResponse>;
deleteAsync(url: string | Uri, token?: CancellationToken): Promise<HttpResponse>;
sendAsync(request: HttpRequest, token?: CancellationToken): Promise<HttpResponse>;
}
export class HttpContentWriter {
private _buffer;
private _byteOffset;
private _limit;
private _capacity;
constructor(limit?: number, capacity?: number);
size: number;
write(buffer: ArrayBuffer, byteOffset: number, byteLength: number): void;
toArrayBuffer(): ArrayBuffer;
close(): void;
private ensureCapacity(capacity);
}
export class HttpContent {
private _content;
private _headers;
private _loadingPromise;
private _state;
constructor();
type: string;
headers: HttpHeaders;
loadAsync(maxBufferSize?: number): Promise<void>;
readAsStringAsync(): Promise<string>;
readAsArrayBufferAsync(): Promise<ArrayBuffer>;
readAsJsonAsync(reviver?: (key: any, value: any) => any): Promise<any>;
close(): void;
protected serialize(writer: HttpContentWriter): Promise<void> | void;
protected throwIfClosed(): void;
private createWriter(maxBufferSize);
}
export class ArrayBufferContent extends HttpContent {
private _buffer;
constructor(buffer: ArrayBuffer);
type: string;
close(): void;
readAsArrayBufferAsync(): Promise<ArrayBuffer>;
protected serialize(writer: HttpContentWriter): void;
}
export class StringContent extends HttpContent {
private _text;
constructor(text: string, encoding?: string, mediaType?: string);
type: string;
readAsStringAsync(): Promise<string>;
close(): void;
protected serialize(writer: HttpContentWriter): void;
}
export class JsonContent extends HttpContent {
private _value;
private _replacer;
private _space;
constructor(value: any, replacer?: JsonReplacer, space?: string, mediaType?: string);
type: string;
readAsJsonAsync(reviver?: (key: any, value: any) => any): Promise<any>;
close(): void;
protected serialize(writer: HttpContentWriter): void;
}
export interface HttpError extends Error {
httpClient: HttpClient;
response: HttpResponse;
}
}