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

axios-base-client

v0.0.1

Published

The `AxiosBaseClient` class is a TypeScript-based client library that simplifies making HTTP requests using the Axios library. It provides a pre-configured Axios instance with request and response logging capabilities, making it easy to interact with REST

Downloads

3

Readme

axios-base-client

The AxiosBaseClient class is a TypeScript-based client library that simplifies making HTTP requests using the Axios library. It provides a pre-configured Axios instance with request and response logging capabilities, making it easy to interact with RESTful APIs while keeping track of the network traffic and responses. This readme will explain the purpose, features, and usage of the AxiosBaseClient class, and how you can extend it to create your own client for your specific needs.

Table of Contents

  1. Introduction
  2. Features
  3. Usage
  4. Extending AxiosBaseClient
  5. Example

1. Introduction

The AxiosBaseClient class is designed to streamline the process of making HTTP requests by encapsulating common configurations and providing request/response logging. It uses the Axios library as its HTTP client, which is widely used and highly flexible. This class can be extended to create specialized clients tailored to specific APIs or use cases.

2. Features

  • Pre-configured Axios instance: The AxiosBaseClient class creates an Axios instance with a base URL and default headers pre-set, simplifying the setup for API requests.

  • Request and Response Logging: The class integrates the Pino logger library to log both request and response details, including headers, status, and data. It logs data at different log levels for debugging and production purposes.

  • Customizable Logging Levels: The class allows custom log levels, including a 'production' level, for more fine-grained control over logging.

3. Usage

Creating an AxiosBaseClient instance

To use the AxiosBaseClient, you need to create an instance of the class. You can do this by providing the base URL and a client name as parameters, along with an optional log level. The AxiosBaseClient instance will be responsible for all HTTP requests to the specified base URL.

import { AxiosBaseClient } from 'axios-base-client';

const client = new AxiosBaseClient('https://api.example.com', 'MyApiClient');

Making HTTP Requests

With the AxiosBaseClient instance created, you can make HTTP requests using the Axios instance it encapsulates. For example, you can make a GET request as follows:

const response = await client.client.get('/resource');

Logging

The AxiosBaseClient logs requests and responses automatically. You can access the logs via the Pino logger instance associated with the client:

client.logger.debug('Debug log message');
client.logger.info('Info log message');
client.logger.production('Production log message');

4. Extending AxiosBaseClient

You can extend the AxiosBaseClient class to create your own client with specific configurations or additional features. To do this, follow these steps:

  1. Create a new class that extends AxiosBaseClient.

  2. Override the constructor to configure the base URL, default headers, and any additional settings specific to your use case.

  3. Add any custom methods or behavior as needed for your client.

Here's an example of how to extend AxiosBaseClient:

export class MyApiClient extends AxiosBaseClient {
  constructor() {
    super('https://api.example.com', 'MyApiClient', 'debug');

    // Additional client-specific configurations can be set here.
  }

  // Add custom methods here if needed.
}

5. Example

Let's create an extended client using the steps outlined above:

import { AxiosBaseClient } from 'axios-base-client';

export class MyApiClient extends AxiosBaseClient {
  constructor() {
    super('https://api.example.com', 'MyApiClient', 'debug');

    // Additional client-specific configurations can be set here.
  }

  // Add custom methods here if needed.
  async fetchData() {
    try {
      const response = await this.client.get('/data');
      return response.data;
    } catch (error) {
      throw error;
    }
  }
}