async-http
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

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);
    /**
     * A Uri
     */
    export class Uri {
        static SCHEME_HTTP: string;
        static SCHEME_HTTPS: string;
        /**
         * The protocol for the Uri (e.g. 'http:')
         */
        protocol: string;
        /**
         * The hostname for the Uri
         */
        hostname: string;
        /**
         * The port number for the Uri
         */
        port: number;
        /**
         * The path name for the Uri
         */
        pathname: string;
        /**
         * The search portion of the path, also known as the querystring
         */
        search: string;
        /**
         * The fragment portion of the path
         */
        hash: string;
        /**
         * A value indicating whether the Url is an absolute url
         */
        absolute: boolean;
        /**
         * Creates a new Uri by parsing a string
         * @param uri {String} The uri string to parse
         */
        constructor(uri: string);
        /**
         * Creates a new Uri by combining a base Uri and a relative Uri
         * @param baseUri The base uri
         * @param uri The relative uri
         */
        constructor(baseUri: string | Uri, uri: string | Uri);
        /**
         * Gets the origin of the Uri
         */
        origin: string;
        /**
         * Gets the host for the uri, including the hostname and port
         */
        host: string;
        /**
         * Gets the scheme for the uri (e.g. 'http://'')
         */
        scheme: string;
        /**
         * Tests whether the provided uri has the same origin as this uri
         * @param uri The uri to compare against
         * @returns True if the uri's have the same origin; otherwise, false
         */
        isSameOrigin(uri: string | Uri): boolean;
        /**
         * Gets the string representation of the Uri
         * @param format {String} A format specifier.
         * @returns {String} The string content of the Uri
         */
        toString(format?: string): string;
        /**
         * Parses the provided uri string
         * @param uri {String} The uri string to parse
         * @returns {Uri} The parsed uri
         */
        static parse(uri: string): Uri;
        /**
         * Combines two uris
         * @param baseUri The base uri
         * @param uri The relative uri
         * @returns The combined 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;
    }
    /**
     * An HTTP request for an HttpClient
     */
    export class HttpRequest {
        private _headers;
        /**
         * The body of the request
         */
        content: HttpContent;
        /**
         * The HTTP method for the request
         */
        method: string;
        /**
         * The url for the request
         */
        url: Uri;
        /**
         * Creates an HTTP request for an HttpClient
         * @param method The HTTP method for the request
         * @param url The url for the request
         */
        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;
    }
    /**
     * A response from an HttpClient
     */
    export class HttpResponse {
        private _headers;
        request: HttpRequest;
        statusCode: number;
        statusText: string;
        content: HttpContent;
        constructor(statusCode?: number);
        headers: HttpHeaders;
    }
    /**
     * A client for HTTP requests
     */
    export class HttpClient {
        private _headers;
        private _cts;
        private _closed;
        /**
         * The base url for the client
         */
        baseUrl: Uri;
        /**
         * A value indicating whether cookies should be sent to a cross-origin request
         */
        withCredentials: boolean;
        /**
         * The number of milliseconds to wait before the request should time out
         */
        timeout: number;
        /**
         * The username for the request
         */
        username: string;
        /**
         * The password for the request
         */
        password: string;
        /**
         * Creates a client for HTTP requests
         * @param baseUrl The base url for the client
         */
        constructor(baseUrl?: string | Uri);
        headers: HttpHeaders;
        /**
         * Closes the client and cancels all pending requests
         */
        close(): void;
        /**
         * Gets the response text from the requested url
         * @param url The url for the request
         * @returns A future result for the string
         */
        getStringAsync(url: string | Uri): Promise<string>;
        /**
         * Gets the response from issuing an HTTP GET to the requested url
         * @param url The url for the request
         * @param token A token that can be used to cancel the request
         * @returns A future result for the response
         */
        getAsync(url: string | Uri, token?: CancellationToken): Promise<HttpResponse>;
        /**
         * Gets the response from issuing an HTTP POST to the requested url
         * @param url The url for the request
         * @param body The body of the request
         * @param token A token that can be used to cancel the request
         * @returns A future result for the response
         */
        postAsync(url: string | Uri, body: HttpContent, token?: CancellationToken): Promise<HttpResponse>;
        /**
         * Gets the response from issuing an HTTP POST of a JSON serialized value to the requested url
         * @param url The url for the request
         * @param value The value to serialize
         * @param jsonReplacer An array or callback used to replace values during serialization
         * @param token A token that can be used to cancel the request
         * @returns A future result for the response
         */
        postJsonAsync(url: string | Uri, value: any, jsonReplacer?: any[] | ((key: string, value: any) => string), token?: CancellationToken): Promise<HttpResponse>;
        /**
         * Gets the response from issuing an HTTP PUT to the requested url
         * @param url The url for the request
         * @param body The body of the request
         * @param token A token that can be used to cancel the request
         * @returns A future result for the response
         */
        putAsync(url: string | Uri, content: HttpContent, token?: CancellationToken): Promise<HttpResponse>;
        /**
         * Gets the response from issuing an HTTP PUT of a JSON serialized value to the requested url
         * @param url The url for the request
         * @param value The value to serialize
         * @param jsonReplacer An array or callback used to replace values during serialization
         * @param token A token that can be used to cancel the request
         * @returns A future result for the response
         */
        putJsonAsync(url: string | Uri, value: any, jsonReplacer?: any[] | ((key: string, value: any) => string), token?: CancellationToken): Promise<HttpResponse>;
        /**
         * Gets the response from issuing an HTTP DELETE to the requested url
         * @param url The url for the request
         * @param token A token that can be used to cancel the request
         * @returns A future result for the response
         */
        deleteAsync(url: string | Uri, token?: CancellationToken): Promise<HttpResponse>;
        /**
         * Sends the provided request and returns the response
         * @param request {HttpRequest} An HTTP request to send
         * @param token {futures.CancellationToken} A token that can be used to cancel the request
         * @returns {futures.Promise<HttpResponse>} A future result for the response
         */
        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;
    }
    /**
     * An error raised during an http request
     */
    export interface HttpError extends Error {
        /**
         * The `HttpClient` that initiated the request
         */
        httpClient: HttpClient;
        /**
         * The `HttpResponse` for the error
         */
        response: HttpResponse;
    }
}

Package Sidebar

Install

npm i async-http

Weekly Downloads

5

Version

0.1.2

License

Apache-2.0

Last publish

Collaborators

  • rbuckton