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-cpool

v1.2.31

Published

This package provides a simple and efficient way to manage HTTP requests using Axios. It includes two main components: the Client and the ClientPool. The Client is a wrapper around the Axios library to manage configurations and instances. The ClientPool m

Downloads

14

Readme

AXIOS CPOOL

Simple a Client & Pool using AXIOS

npm version Build Status install size npm downloads

Introduction

This package simplifies and streamlines HTTP request management using Axios. It offers two core components:

Client: A streamlined wrapper around Axios that efficiently manages configurations and instances.

ClientPool: Designed for high-load scenarios, this component manages multiple Client instances, enabling reuse and optimizing the handling of numerous requests. The poolSize parameter acts as a maximum limit on the number of client instances that can be simultaneously held within the pool.

This streamlined approach simplifies the integration of Axios into your projects, especially in situations demanding high performance and efficient resource utilization.

Key Functions of ClientPool:
  • Resource Control: By setting a poolSize, you prevent the excessive consumption of resources by ensuring that the number of active client instances remains within defined bounds.

  • Client Lifecycle Management: When the pool reaches its capacity (poolSize), adding a new client automatically triggers the removal of the oldest client from the pool. This mechanism efficiently manages the lifecycle of clients, prioritizing newer instances and maintaining optimal performance.

This streamlined approach simplifies the integration of Axios into your projects, especially in situations demanding high performance and efficient resource utilization.

Key Features:
  • Easy to Use: Simple and intuitive API for making HTTP requests.

  • Client Pool: Manage multiple API clients with ease using the client pool.

  • Configurable: Easily configure base URLs and request timeouts.

  • TypeScript Support: Fully written in TypeScript for type safety and improved development experience.

  • Error Handling: Built-in error handling with Axios interceptors.


Installing

Axios is a required package that must be installed.

Using npm:

$ npm i axios axios-cpool

using yarn:

$ yarn add axios axios-cpool

Once the package is installed, you can import the library using import or require approach:

import { Client, ClientPool } from 'axios-cpool';

If you use require for importing, only default export is available:

const { Client, ClientPool } = require("axios-cpool");

Example

import { Client } from 'axios-cpool';

// Create an instance of Client for your API server
const apiClient = new Client('https://api.example.com');

// Set headers for the clients: Authorization, Api-key, etc...
apiClient.setHeaders({
  Authorization: 'Bearer token1',
  'Custom-Header': 'customValue1'
});

// Set handle errors (error: AxiosError)
apiClient.handleErrors((error) => {
    // Custom error handling logic here
    console.error('Error occurred:', error);

    return Promise.reject(error);
})

// Example usage
(async () => {
  try {
    // Make a GET request
    const getResponse = await apiClient.get('/data');
    console.log('GET Response:', getResponse.data);

    // Make a POST request
    const postData = { name: 'John', age: 30 };
    const postResponse = await apiClient.post('/users', postData);
    console.log('POST Response:', postResponse.data);

    // Make a DELETE request
    const deleteResponse = await apiClient.delete('/users/123');
    console.log('DELETE Response:', deleteResponse.data);

    // Make an UPDATE request
    const updateData = { name: 'Updated Name', age: 35 };
    const updateResponse = await apiClient.put('/users/123', updateData);
    console.log('UPDATE Response:', updateResponse.data);
  } catch (error) {
    console.error('Error:', error);
  }
})();

Assuming you have multiple API servers:

1. https://jsonplaceholder.typicode.com
2. https://jsonplaceholder.typicode.com
3. https://jsonplaceholder.typicode.com
import { Client, ClientPool } from "axios-cpool";

// Create instances of Client for each API server
const client1 = new Client('https://jsonplaceholder.typicode.com');
const client2 = new Client('https://jsonplaceholder.typicode.com');
const client3 = new Client('https://jsonplaceholder.typicode.com'); // additional client for testing pool size


// Set headers for the clients: Authorization, Api-key, etc...
client1.setHeaders({
  Authorization: 'Bearer token1',
  'Custom-Header': 'customValue1'
});

// Set handle errors (error: AxiosError)
client1.handleErrors((error) => {
    // Custom error handling logic here
    console.error('Error occurred:', error);
    return Promise.reject(error);
})

// Create client pool with a size limit
const poolSize = 2;

// Create a ClientPool and add clients to it
const cpool = new ClientPool(poolSize);
cpool.addClient('server1', client1);
cpool.addClient('server2', client2);
cpool.addClient('server3', client3);

console.log('Current clients in pool:', cpool.listClients());

async function fetchData() {
  try {
    const server2Data = await cpool.request('server2', 'get', '/posts/1');
    console.log('Server2 Data:', server2Data);

    const server3Data = await cpool.request('server3', 'get', '/posts/1');
    console.log('Server3 Data:', server3Data);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

Credits

Special thanks to the axios library for providing a robust and versatile HTTP client that served as the foundation for this package.

Support

If you have enjoyed this package and would like to buy me a coffee ☕️

"Buy Me A Coffee"