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

http-guru

v1.0.0

Published

Build typed, fast and unified http apis for your frontend apps

Downloads

1

Readme

HTTP-Guru

Build typed, fast and unified http apis for your frontend apps.

Modules overview

Packaged includes only one api building entry HTTPRequestBuilder.

import {HTTPRequestBuilder} from 'http-guru';

Other imported entities are types. They are more useful than you can think from the first sight :3

Docs for each type is not represented in Readme, because it just a copy paste of js-docs, provided for each entity in the code. Import it to your code and jump to the definition, but type hinting and naming shoud make you fill easy to understand, what type is written for.

enum HTTPMethod;

interface HTTPHeadersMap;

type HTTPDefaultHeaders;

type HTTPHeaders<T extends Object = AnyObject>;

interface HTTPResponce<T>;

type HTTPRequestResult<T>;

interface HTTPRequestInit;

interface HTTPRequestBuildSettings<T extends Object = AnyObject>;

type ApiHandle<Q, S>;

type HTTPRequest<Q, S, M extends EnumKeys<typeof HTTPMethod>;

HTTPContentType;

// Supportive types, but may be useful as a Mocks (defaults) for generic types.
interface AnyObject;
type EnumKeys<T extends Record<string, any>>

Usage

Before, defining apis handlers on frontend was procedural and mostly was wrapped in other function, which just again and again assembled fetch request on each call without providing any types. Now, apis can be build with just one api builder, which is built around an api host and headers, which you don't need to specify again and again.

Default headers are static, and passed with each request. Build is generic, which provides an ability to specify headers, which are available to specify within this api in one point of the app.


// Just an example of the app specific http headers
interface ApiSpecificHeaders {
    "Authtype": "password" | "token";
}

// Api builder itself
const apiBuilder = new HTTPRequestBuilder<HTTPHeaders<ApiSpecificHeaders>>(
    'https://my-hostexample/api', {
        'Authtype': 'password',
        'Content-Type': 'application/json',
    },
);

Builder's methods:

Raw request

This method just makes a raw request, it does not defines anything and builds ready to use api handle, it's an improved version fetch

public async request<Q extends any, S extends any, H extends AnyObject = T>(endpoint: string, method: HTTPMethod, body?: Q, headers?: HTTPHeaders<H>, init: HTTPRequestInit = {}): HTTPRequestResult<S>

Example

interface ModelResponce {
    message: string,
}

interface ModelRequest {
    type: string,
    count: number,
}

apiBuilder.request<ModelRequest, ModelResponce>('/buy', HTTPMethod.POST, {
    count: 123,
    type: 'example',
})

Generics

S, responce type - responce body type, which leads to Response.json(): Promise<S> Q, request data type - request content type, which leads to api(data: Q) H, request headers - additional headers to be included to the request, it's data, required to be inclided to the Responce

Args

endpoint - host handle endpoint method - http request method body - content headers - additional headers to be included to the request init - limited {@link RequestInit}

Returns (request result, err)

Strict api handler

This method builds api handle, which you wound use most. It creates static, strictly defined api handler

build<S extends any, Q extends any | undefined = undefined, H extends AnyObject = T>(settings: HTTPRequestBuildSettings<H>): ApiHandle<Q, S>

Example

interface ModelResponce {
    message: string,
}

interface ModelAuth {
    password: string,
    email: string,
}

interface Service {
    /**
     * Get some info.
     */
    getInfo: ApiHandle<undefined, ModelResponce>

    /**
     * Authenticate
     */
    auth: ApiHandle<ModelAuth, ModelResponce>
}

const apiBuilder = new HTTPRequestBuilder<HTTPHeaders<ApiSpecificHeaders>>(
    'https://my-hostexample/api', {
        'Authtype': 'password',
        'Content-Type': 'application/json',
    },
);

export const service: Service = {
        
    getInfo: apiBuilder.build<ModelResponce, undefined>({
        method: HTTPMethod.GET,
        urlEndpoint: '/info',
    }),

    auth: apiBuilder.build<ModelResponce, ModelAuth>({
        method: HTTPMethod.POST,
        urlEndpoint: '/info',
        requestSpecificHeaders: {'Authtype': 'password'},
        requestInit: {
            credentials: 'include',
        }
    }),
};

Now your service apis are cleary defined and typed, request content and responce types are defined, which means that body.json() will be resolved as a, for example, ModelResponce by default. Default headers for this api is also specified from the time builder itself is created. Service or a group of handlers changed something? Now you don't need to deep-dive into the procedural code, just replace from one to two words, and that's it. You can create builders to specify groups of handlers, and not just one host.

Define request

This method defined request, but not fully builds it, so, event it's typed, it provides a range of values, that can be managed manually, like header and method

defineRequest<Q extends any, S extends any, M extends EnumKeys<typeof HTTPMethod> = HTTPMethod, H extends AnyObject|undefined = HTTPHeaders>(endpoint: string, init?: HTTPRequestInit): HTTPRequest<Q, S, M, H>

Example

apiBuilder.defineRequest<undefined, ModelResponce, "GET" | "POST">('/example');

Requests

Results of the request are implemented in golang-like style of res, err, so each request is handled like this:

const [res, err] = await apiCall(...data);
if (res == null) {
    ...handle err;
};