@vasg/http-client
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

HTTP Client

HTTP Client is an advanced HTTP client library designed to streamline and simplify HTTP requests in various JavaScript environments. Leveraging the powerful Axios framework as its foundation, it offers a user-friendly interface and comprehensive functionality for making HTTP requests.

npm install @vasg/http-client

Create new instance of HTTP client. You can create multiple instances with different configuration

import { HttpClient } from "@vasg/http-client";

const httpClient = new HttpClient({
  baseURL: "https://YOUR_API_DOMAIN.com",
}); 

You can make GET request like this:

async function getUsers() {
  const response = await httpClient.get({
    route: {
      url: "users",
      method: "GET",
      isRequiredAuth: true,
    },
  });
}

Code beloved shows how you can do GET request with dynamic paths. URL will be parsed uses replacements object values

async function getPostsByUserId(userId: number) {
  return httpClient.get({
    route: {
      url: "users/:userId/posts",
      method: "GET",
      isRequiredAuth: true
    },
    replacements: {
      userId: userId
    }
  });
}

And PUT request example

async function updateUser(userId: number) {
  return httpClient.put({
    route: {
      url: "users/:userId",
      method: "PUT",
      isRequiredAuth: true
    },
    replacements: {
      userId: userId
    }
  });
}

Also I will suggest to have separate file where you can define and use all your routes.

You can find examples on src/examples.ts file

Readme

Keywords

none

Package Sidebar

Install

npm i @vasg/http-client

Weekly Downloads

12

Version

0.0.7

License

ISC

Unpacked Size

88.4 kB

Total Files

14

Last publish

Collaborators

  • grishadev