npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-http

v0.1.2

Published

Asynchronous HTTP request API

Downloads

8

Readme

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;
    }
}