Skip to content

orashus/http-client

Repository files navigation

Welcome to @orashus/http-client


current version author RashJrEdmund

A lightweight http-client built on top of the JavaScript Fetch API

table of content

Installation

To install run

  # npm
  npm install @orashus/http-client

  # pnpm
  pnpm install @orashus/http-client

  # yarn
  yarn add @orashus/http-client

How to use

  • There are 2 ways to get the http client class depending on how useful each is to you, for both ways, you have to create an instance of it to be able to use it.

    • First and common is to import the class from @orashus/http-client like so

        import { HttpClient } from "@orashus/http-client";
      
        const httpClient = new HttpClient({
          base_url: "http://localhost:8080",
          base_headers: {
            "x-some-api-key": "that api key value",
          },
          before_req_headers: () => {
            // returned object is added to the headers of each request using this class
            return {
             "Authorization": `Bearer ${localStorage.getItem("token")}`
           }
          }
        });
    • Or you use the http client provider, from which you could assign more options. This provider, returns a class that will inherit and use the default options you've passed to the provider... You can further give the returned class it's own options as well.

        import { HttpClientProvider } from "@orashus/http-client";
    
        const HttpClient = HttpClientProvider({
          api_base_url: "http://localhost:8080",
          api_base_headers: {
            "x-some-api-key": "that api key value",
          },
          base_before_req_headers: () => {
            // returned object is added to the headers of each request using this class
            return {
             "Authorization": `Bearer ${localStorage.getItem("token")}`
           }
          }
        });
    
        const authHC = new HttpClient({
          base_url: "/auth", // simply ["auth"]
        });
    
        // use like so
    
        authHC.POST("/login", {
          email: "test@email.com",
          password: "123456"
        }, {
          // optionally add any headers specific to this endpoint
        });
    
        authHC.GET("/current-user", {
          // second arg is for optional headers
        });

Options Object

It is also important to note that all url like options can be both of type string or an array of string. Example:

  • /users/${id} <-> ["users", id]

  • /auth/login <-> ["auth", "login"]

The following are all options arguments properties that the both the HttpClientProvider & the HttpClient class takes.

  • HttpClientProvider

      {
        api_base_url?: string | string[] = "",
        api_base_headers?:HeadersInit = {},
        base_before_req_headers?: () => HeadersInit,
      }
  • HttpClient

      {
        base_url?: string | string[] = "",
        base_headers?: HeadersInit = {},
        before_req_headers?: () => HeadersInit,
      }

Supported Request Methods

As of now @orashus/http-client supports the following request methods

  • GET
  • DELETE
  • POST
  • PUT
  • PATCH

Disclaimer/Flex

ChatGPT or any associated AI models have no hand in my code base! 🥷🏽


|

|

HAPPY CODING!!

About

A lightweight http-client built on top of the JavaScript Fetch API

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published