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 🙏

© 2026 – Pkg Stats / Ryan Hefner

poolswitch-node

v0.1.0

Published

Embedded and proxy-ready Node.js client for PoolSwitch.

Readme

poolswitch-node

poolswitch-node lets JavaScript and TypeScript apps use PoolSwitch in two ways:

  • directly inside your app with the embedded PoolSwitchClient
  • against a local or shared proxy with PoolSwitchProxyClient

Install

npm install poolswitch-node

Usage

Embedded client

import { PoolSwitchClient } from "poolswitch-node";

const client = new PoolSwitchClient({
  upstreamBaseUrl: "https://api.openai.com",
  keys: [
    { id: "openai-free-1", value: process.env.OPENAI_KEY_1! },
    { id: "openai-free-2", value: process.env.OPENAI_KEY_2! }
  ],
  strategy: "quota_failover"
});

async function main() {
  const response = await client.post("/v1/chat/completions", {
    json: {
      model: "gpt-4o-mini",
      messages: [{ role: "user", content: "Hello from the embedded client" }]
    }
  });

  console.log(response.choices[0].message.content);
  console.log(client.status().keys);
}

main().catch((error) => {
  console.error(error.reason, error.status, error.data);
});

Proxy client

const { PoolSwitchProxyClient } = require("poolswitch-node");

const client = new PoolSwitchProxyClient("http://localhost:8080", {
  timeout: 30000,
  headers: {
    "x-app-name": "demo"
  }
});

async function main() {
  const response = await client.post("/v1/chat/completions", {
    json: {
      model: "gpt-4.1-mini",
      messages: [{ role: "user", content: "Hello from the proxy" }]
    }
  });

  console.log(response.status);
  console.log(response.data);
}

main().catch((error) => {
  console.error(error.status, error.data);
});

API

new PoolSwitchClient(options)

  • options.upstreamBaseUrl: Upstream API base URL such as https://api.openai.com
  • options.keys: Array of API keys as strings or { id, value, monthlyQuota } objects
  • options.strategy: round_robin, least_used, random, or quota_failover
  • options.retryAttempts: Maximum attempts per request
  • options.cooldownSeconds: Cooldown for quota-exhausted keys
  • options.authHeaderName: Header name for key injection, default Authorization
  • options.authScheme: Auth scheme, default Bearer
  • options.headers: Default headers sent with every request
  • options.timeout: Request timeout in milliseconds
  • options.fetchImpl: Custom fetch implementation for tests or alternate runtimes

new PoolSwitchProxyClient(baseUrl, options?)

  • baseUrl: Proxy base URL such as http://localhost:8080
  • options.headers: Default headers sent with every request
  • options.timeout: Request timeout in milliseconds
  • options.fetchImpl: Custom fetch implementation for testing or alternate runtimes

Embedded client methods

  • client.get(path, options?)
  • client.post(path, options?)
  • client.put(path, options?)
  • client.patch(path, options?)
  • client.delete(path, options?)
  • client.status()

Embedded responses return parsed JSON for JSON APIs, or plain text for text responses.

Proxy client methods

client.request(method, path, options?)

Supported options:

  • headers: Per-request headers
  • json: JSON-serializable request body
  • body: Raw request body
  • query: Query string object
  • timeout: Per-request timeout override
  • signal: Optional AbortSignal

Returns:

  • status
  • ok
  • headers
  • data
  • text

Errors:

  • Throws PoolSwitchError for non-2xx responses or transport failures.
  • error.data includes parsed JSON when available.

Convenience methods

  • client.get(path, options?)
  • client.post(path, options?)
  • client.put(path, options?)
  • client.patch(path, options?)
  • client.delete(path, options?)

Notes

  • Requires Node.js 18 or newer because it uses the built-in fetch.
  • Supports both ESM import and CommonJS require.
  • JSON responses are parsed automatically when possible.
  • Non-JSON responses are returned as text.