@tamasha/axios-connection
v1.0.0
Published
Cluster-based axios client manager with round-robin replicas and safe destroy
Readme
@tamasha/axios-connection
Single, reusable Axios instance with keep-alive agents and a safe destroy method.
- Initialize once with
AxiosConnection.init({...}) - Reuse via
AxiosConnection.get()across files destroy()closes keep-alive agents to prevent memory leaks
Install
npm install @tamasha/axios-connectionUsage
// http/client.ts
import AxiosConnection from "@tamasha/axios-connection";
AxiosConnection.init({
// baseURL is optional; callers can pass absolute URLs per request
timeout: 10000,
});
export const http = AxiosConnection.get();// features/user.ts
import { http } from "../http/client";
export async function getUser(id: string) {
// can use relative if baseURL is set, or absolute otherwise
const { data } = await http.get(`https://api.example.com/users/${id}`);
return data;
}// shutdown (e.g., on process exit)
import AxiosConnection from "@tamasha/axios-connection";
await AxiosConnection.destroy();Notes
- The instance uses
agentkeepaliveunder the hood with sensible defaults and supports overrides. - Call
destroy()during graceful shutdown to ensure sockets are closed and no resources are leaked.
