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

@n7e/http-client

v0.4.1

Published

Configurable HTTP client library.

Downloads

129

Readme

HTTP HttpClient

Configurable HTTP client library.

For further insights, read on.

Installation

To install this library use your favorite package manager. No additional steps are required to start using the library.

npm install @n7e/http-client

This library is implemented in TypeScript but can be used with JavaScript without any additional steps.

HTTP Client

An HTTP client has the ability to send HTTP requests with a very simple API. HTTP clients can be configured (through an HTTP client factory) to process requests/responses using a middleware pipeline.

const response = await client.send(request);

HttpClient is just an interface describing the functionality of an HTTP client. To create an HTTP client instance use an HTTP client factory.

HTTP Client Factory

To create an HTTP client instance you should use a HttpClientFactory.

import type { HttpClientFactory } from "@n7e/http-client";

function someFunction(clientFactory: HttpClientFactory): void {
    const client = clientFactory.createClient();
    
    // ...
}

The createClient() method takes an optional client name as an argument. This enables us to configure named clients individually. If no name is provided the assumed client name is "default".

To configure a named client use the configure() method. The return value is a client definition instance which can be used to configure a named client.

clientFactory.configure("name")
    .useDefaultRequestTimeout(500)
    .addMiddleware(someMiddleware);

Any client created with the "name" name will have the appropriate configuration.

const client = clientFactory.createClient("name");

To configure the default client use the name "default".

clientFactory.configure("default").useDefaultRequestTimeout(500);

HttpClientFactory is just an interface describing the functionality of an HTTP client factory. To create a client factory instance you need to reference a specific implementation.

Default Implementation

This library provides a client factory implementation. It produces clients that use the Fetch API internally to send HTTP requests.

The implementation depends on a ResponseFactory and a StreamFactory. Since the @n7e/http library provides default implementations of those factories here's an example of how to create a client factory:

import { DefaultHttpClientFactory } from "@n7e/http-client";
import { DefaultResponseFactory, DefaultStreamFactory } from "@n7e/http";

const streamFactory = new DefaultStreamFactory();
const responseFactory = new DefaultResponseFactory(streamFactory);
const clientFactory = new DefaultHttpClientFactory(responseFactory, streamFactory);

This implementation allows you to provide a custom fetch method implementation that will be used instead of the global method. This is very useful for test scenarios.

clientFactory.useFetchImplementation(customFetchImplementation);