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

@oleksii-pavlov/http

v1.3.1

Published

Tiny http client based on Fetch API

Downloads

138

Readme

HTTPClient Class Documentation

Overview

The HTTPClient class is a flexible and configurable HTTP client designed for making various types of HTTP requests (GET, POST, PUT, PATCH, DELETE, and custom methods) in a standardized manner. It allows users to customize the configuration at both the client level and per-request level. This class supports JSON requests and responses, automatically handling JSON parsing if specified. Additionally, it leverages TypeScript's generics to provide strong typing for response data, ensuring that you receive the expected data shape from your API requests.

Constructor

HTTPClient(config: ClientConfig = {})

  • Parameters:

    • config: An optional ClientConfig object that sets the default configuration for the client instance. Reference
  • Example:

    const client = new HTTPClient({ base: 'https://api.example.com' })

Methods

get<Result>(path: string, init: RequestOptions = {}, config: ClientConfig = {})

  • Description: Sends a GET request to the specified path.

  • Parameters:

    • path: The endpoint path (appended to the base URL defined by ClientConfig in constructor).
    • init: Optional RequestOptions to override the default request options. Reference
    • config: Optional ClientConfig to override the client instance's default configuration for this request. Reference
  • Returns: A Promise that resolves to a Result, which is the expected response type.

  • Generics:

    • Result: The expected type of the response data. This can be any shape depending on your API response.

post<Body, Result>(path: string, body: Body, init: RequestOptions = {}, config: ClientConfig = {})

  • Description: Sends a POST request to the specified path with the provided body.

  • Parameters:

    • path: The endpoint path.
    • body: The request body, which will be stringified if json is true. Reference
    • init: Optional RequestOptions. Reference
    • config: Optional ClientConfig. Reference
  • Returns: A Promise that resolves to a Result, which is the expected response type.

  • Generics:

    • Body: The type of the request body.
    • Result: The expected type of the response data.

put<Body, Result>(path: string, body: Body, init: RequestOptions = {}, config: ClientConfig = {})

  • Description: Sends a PUT request to the specified path with the provided body.

  • Parameters: Same as post().

  • Returns: A Promise that resolves to a Result, which is the expected response type.

  • Generics:

    • Body: The type of the request body.
    • Result: The expected type of the response data.

patch<Body, Result>(path: string, body: Body, init: RequestOptions = {}, config: ClientConfig = {})

  • Description: Sends a PATCH request to the specified path with the provided body.

  • Parameters: Same as post().

  • Returns: A Promise that resolves to a Result, which is the expected response type.

  • Generics:

    • Body: The type of the request body.
    • Result: The expected type of the response data.

delete<Result>(path: string, init: RequestOptions = {}, config: ClientConfig = {})

  • Description: Sends a DELETE request to the specified path.

  • Parameters:

    • path: The endpoint path.
    • init: Optional RequestOptions. Reference
    • config: Optional ClientConfig. Reference
  • Returns: A Promise that resolves to a Result, which is the expected response type.

  • Generics:

    • Result: The expected type of the response data.

other<Result>(method: string, path: string, init: RequestInit = {}, config: ClientConfig = {})

  • Description: Sends a request with a custom HTTP method.

  • Parameters:

    • method: The HTTP method (e.g., HEAD, OPTIONS).
    • path: The endpoint path.
    • init: Optional RequestInit (Fetch API).
    • config: Optional ClientConfig. Reference
  • Returns: A Promise that resolves to a Result, which is the expected response type.

  • Generics:

    • Result: The expected type of the response data.

Interfaces and Types

ClientConfig

The ClientConfig interface defines the configuration options available for the HTTPClient.

  • Properties:
    • base?: string (default: ""): Optional base URL for all requests made with the client.
    • json?: boolean (default: true): If true, the client automatically adds Content-Type: application/json to headers and stringifies request bodies.
    • parse?: boolean (default: true if config.json is true): If true, the client automatically parses JSON responses.
    • headers?: () => HeadersInit (default: () => ({})): preloaded headers getter, can be used for authorization, server special etc.
    • middleware?: (response: Response) => Response (default: res => res): contains custom logic to throw exceptions, log responses and so on

RequestOptions

The RequestOptions type is a simplified version of the RequestInit interface (Fetch API), excluding the method and body properties. It allows you to specify options for a request, such as headers, credentials, and more.

type RequestOptions = Omit<RequestInit, 'method' | 'body'>

Example Usage

const client = new HTTPClient({
  base: 'https://api.example.com',
})

interface User {
  id: number
  name: string
}

interface UserDTO {
  name: string
}

// GET request
client.get<User[]>('/users')
  .then(response => console.log(response))
  .catch(error => console.error(error))

// POST request with a JSON body
client.post<UserDTO, User>('/users', { name: 'John Doe' })
  .then(response => console.log(response))
  .catch(error => console.error(error))