Skip to content

Simple, small and extendable http library for sending requests.

License

Notifications You must be signed in to change notification settings

ariesclark/http

Repository files navigation

@rubybb/http

Simple, small and extendable http library for sending requests.

Discord Maintenance GitHub issues npm bundle size (scoped) NPM

Install

Available on NPM: @rubybb/http

Ruby's recommended package manager:
pnpm: 📦🚀 Fast, disk space efficient package manager.

pnpm install @rubybb/http

Examples

import http, { HTTP } from "@rubybb/http";

http.get("https://reqres.in/api/users/3", {resultType: "json"}).then((json) => {
    console.log(json);
    /* {
        data: {
            id: 3,
            email: "emma.wong@reqres.in",
            first_name: "Emma",
            last_name: "Wong",
            avatar: "https://reqres.in/img/faces/3-image.jpg"
        }
    } */
});

const api = HTTP.create({
    baseURL: "https://reqres.in/api/",
    resultType: "json"
});

api.get("users/3").then((json) => {...});
api.get("users/:id", {id: 3}).then((json) => {...});

API

NOTE: RequestInit refers to all native fetch options, and BodyInit refers to a Blob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStream object.

export declare interface HTTPOptions extends RequestInit {
    resultType: ResultType;
    query: Record<string, any>;
    excludeDefaults: boolean;
    baseURL: string;

    /* logs loads of information to console. */
    debug: boolean;
    
    /* 
        disables features like path name mutation,
        request transforms, and other various things.
    */
    minimal: boolean;

    /* prevents requests from throwing on response errors. */
    nothrow: boolean;

    events?: {
        override?: boolean;
        pre?: (this: HTTP, path: string, options: PartialHTTPOptions) => Promise<boolean>;
        post?: (this: HTTP, result: any, path: string, options: PartialHTTPOptions) => Promise<typeof result>;
    }

    /* for path params */
    [key: string]: unknown
}

// equivalent of new HTTP(options, immutable)
HTTP.create (options: Partial<HTTPOptions> = {}, immutable? = false): HTTP;

// mutate the current HTTP instance options.
http.mutate (options: Partial<HTTPOptions>): HTTP;

// copy the current instance options onto a new instance, and apply addtional options.
http.clone (options: Partial<HTTPOptions>): HTTP;

// send a request with the GET method.
http.get (path: string, options: Partial<HTTPOptions> = {}): Promise<unknown>;

// send a request with the HEAD method.
http.head (path: string, options: Partial<HTTPOptions> = {}): Promise<unknown>;

// send a request with the POST method and body.
http.post (path: string, body: BodyInit, options: Partial<HTTPOptions> = {}): Promise<unknown>;

// send a request with the PATCH method and body.
http.patch (path: string, body: BodyInit, options: Partial<HTTPOptions> = {}): Promise<unknown>;

// send a request with the PUT method and body.
http.put (path: string, body: BodyInit, options: Partial<HTTPOptions> = {}): Promise<unknown>;

// send a request with the delete method and an optional body.
http.delete (path: string, body?: BodyInit, options: Partial<HTTPOptions> = {}): Promise<unknown>;

About

Simple, small and extendable http library for sending requests.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published