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

pluto-http-client

v1.0.9

Published

HTTP client for NodeJS. Inspired in the Java JAX-RS spec so you can expect excellence, versatility and extensibility.

Downloads

2,270

Readme

Pluto HTTP Client

NPM version

HTTP client for NodeJS. Inspired in the Java JAX-RS spec, so you can expect excellence, versatility and extensibility.

Motivation

I have Node.js applications that currently rely on the well-known "request" library. However, as we're all aware, this library has been discontinued since 2020. To compound the issue, they've opted not to accept any community merge requests for updates or security patches. It's frustrating, resembling an admonishing parent, they consistently point to the discontinuation post, even though the library continues to see around 19,000 weekly downloads even after two years of being abandoned. It's worth noting that we're discussing an HTTP client here; it's not a complex, cutting-edge codebase.

Given this situation, I'm actively seeking a suitable replacement. While I experimented with alternatives like "axios" and others, they all emphasize front-end and back-end compatibility (not to mention ESM module compatibility), which isn't my primary concern. I'm in search of a library that provides a straightforward and robust API, harnessing the latest features of Node.js, without being encumbered by compatibility intricacies.

Features

  • HTTP, HTTPs, HTTP2
  • Filters / Observability
  • Streaming
  • Well-defined API
  • 0 dependencies

Examples

HTTP GET Request

 const client = new ClientBuilder()
    .withTimeout(30, TimeUnit.Seconds)
    .withFilter(new LoggingFilter(console.log))
    .build();

const target = client
    .target("https://run.mocky.io");

const response = await target
    .path("/v3/de314aa8-a521-47c4-8ff3-69b447dab89b")
    .request()
    .header(HttpHeaders.ACCEPT, MediaType.ANY_TEXT_TYPE.toString())
    .get();

if(response.getStatusInfo().getFamily().isSuccessful()) {
    const body = await response.readEntity(new StringEntity());
} else {
    console.log("Response status code:", response.getStatus());
}

HTTP GET Request and redirect to Writable Stream

 const client = new ClientBuilder()
    .withTimeout(30, TimeUnit.Seconds)
    .withFilter(new LoggingFilter(console.log))
    .build();

const target = client
    .target("https://run.mocky.io");

const response = await target
    .path("/v3/de314aa8-a521-47c4-8ff3-69b447dab89b")
    .request()
    .header(HttpHeaders.ACCEPT, MediaType.ANY_TEXT_TYPE.toString())
    .get();

response.readEntity(fs.createWriteStream('get_response.txt'));

HTTP POST JSON Request

 const client = new ClientBuilder()
    .withTimeout(30, TimeUnit.Seconds)
    .withFilter(new LoggingFilter(console.log))
    .build();

const target = client
    .target("https://run.mocky.io");

const response = await target
    .path("/v3/de314aa8-a521-47c4-8ff3-69b447dab89b")
    .request()
    .post(Entity.json({test: 1}));

const obj = await response.readEntity(new JsonEntity());

HTTP2

 const client = new ClientBuilder()
    .withTimeout(30, TimeUnit.Seconds)
    .withFilter(new LoggingFilter(console.log))
    .withHttp2()
    .build();

Implementing a Filter - Uppercase PostRequest Filter

class ToUppercaseFilter extends Transform implements Filter {
    
    filter(requestContext: RequestContext, responseContext?: ResponseContext): void {
        responseContext?.pipe(this);
    }

    order(): FilterOrder {
        return FilterOrder.PostRequest;
    }

    _transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback) {
        callback(null, String(chunk).toUpperCase());
    }

    equals(other: any): boolean {
        return other == this;
    }
}

Request Interface

interface Request {

    accept(mediaType: MediaType): RequestBuilder;

    acceptLanguage(locale: string): RequestBuilder;

    acceptEncoding(encoding: Encoding): RequestBuilder;

    cacheControl(cacheControl: CacheControl): RequestBuilder;

    cookie(cookie: Cookie): RequestBuilder;

    header(key: string, value: string): RequestBuilder;

    get(): Promise<Response>;

    put<T>(entity: Entity<T>): Promise<Response>

    post<T>(entity: Entity<T>): Promise<Response>

    delete<T>(entity: Entity<T>): Promise<Response>

    build<T>(method: Method, entity?: Entity<T>): Promise<Response>;
    
}

Response Interface

interface Response {

    getHeaders(): MultiValueMapType;

    getStatusInfo(): StatusType

    getStatus(): number;

    getMediaType(): MediaType | undefined;

    getEtag(): EntityTag | undefined;

    getDate(): Date | undefined;

    getLastModified(): Date | undefined;

    getHeaderString(key: string): string

    getCookies(): MultiValueMap<Cookie>;

    readEntity<T>(unmarshaller: Unmarshal<T>): Promise<T>

    readEntity(writable: Writable): Writable
    
}