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
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:
Create a new class that extends
AxiosBaseClient
.Override the constructor to configure the base URL, default headers, and any additional settings specific to your use case.
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;
}
}
}