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

@opensourceframework/next-circuit-breaker

v0.2.1

Published

Circuit breaker pattern implementation for Next.js API routes - Prevent cascading failures by failing fast when downstream services are unavailable

Downloads

306

Readme

@opensourceframework/next-circuit-breaker

npm version License: MIT

Circuit breaker pattern implementation for Next.js API routes. Prevent cascading failures by failing fast when downstream services are unavailable.

Features

  • 🔄 Circuit Breaker Pattern - Protect your services from cascading failures
  • Next.js Integration - Works seamlessly with Next.js API routes
  • 📊 State Management - CLOSED, OPEN, and HALF_OPEN states
  • 🔧 Configurable - Customizable failure thresholds and timeouts
  • 📈 Monitoring - Built-in state inspection and callbacks
  • 🪶 Lightweight - Zero dependencies, TypeScript native

Installation

npm install @opensourceframework/next-circuit-breaker
# or
yarn add @opensourceframework/next-circuit-breaker
# or
pnpm add @opensourceframework/next-circuit-breaker

Quick Start

Basic Usage

import { CircuitBreaker } from '@opensourceframework/next-circuit-breaker';

// Create a circuit breaker for an external API call
const fetchUserData = async (userId: string) => {
  const response = await fetch(`https://api.example.com/users/${userId}`);
  if (!response.ok) throw new Error('Failed to fetch user');
  return response.json();
};

const breaker = new CircuitBreaker(fetchUserData, {
  failureThreshold: 5,    // Open after 5 failures
  successThreshold: 2,    // Close after 2 consecutive successes
  timeout: 30000,         // Wait 30s before trying again
});

// Use the circuit breaker
try {
  const user = await breaker.fire('user-123');
  console.log(user);
} catch (error) {
  if (error.message === 'Circuit breaker is OPEN') {
    // Handle the circuit being open
    console.log('Service temporarily unavailable');
  }
}

Next.js API Route

import { withApiCircuitBreaker } from '@opensourceframework/next-circuit-breaker';
import type { NextApiRequest, NextApiResponse } from 'next';

async function handler(req: NextApiRequest, res: NextApiResponse) {
  // Your API logic that calls external services
  const data = await fetchExternalService();
  res.status(200).json(data);
}

// Wrap your handler with circuit breaker protection
export default withApiCircuitBreaker(handler, {
  failureThreshold: 5,
  timeout: 30000,
  onOpen: () => console.log('Circuit opened - external service may be down'),
  onClose: () => console.log('Circuit closed - service recovered'),
});

Function Wrapper

import { withCircuitBreaker } from '@opensourceframework/next-circuit-breaker';

// Wrap any async function
const protectedFetch = withCircuitBreaker(
  async (url: string) => {
    const response = await fetch(url);
    return response.json();
  },
  {
    failureThreshold: 3,
    timeout: 10000,
  }
);

// Use like a normal function
const data = await protectedFetch('/api/data');

API Reference

CircuitBreaker

The main circuit breaker class.

Constructor

new CircuitBreaker<TArgs, TResult>(
  request: AsyncFunction<TArgs, TResult>,
  options?: CircuitBreakerOptions
)

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | failureThreshold | number | 3 | Number of failures before opening the circuit | | successThreshold | number | 2 | Number of successes before closing the circuit | | timeout | number | 10000 | Time in ms before attempting to close | | onOpen | () => void | - | Callback when circuit opens | | onClose | () => void | - | Callback when circuit closes | | onHalfOpen | () => void | - | Callback when circuit enters half-open |

Methods

| Method | Description | |--------|-------------| | fire(...args) | Execute the wrapped function | | getState() | Get current state: 'CLOSED', 'OPEN', 'HALF_OPEN' | | getStats() | Get detailed state information | | isOpen() | Check if circuit is open | | isClosed() | Check if circuit is closed | | isHalfOpen() | Check if circuit is half-open | | trip() | Manually open the circuit | | reset() | Manually reset to closed state |

withCircuitBreaker

Convenience function to wrap an async function.

withCircuitBreaker<TArgs, TResult>(
  handler: AsyncFunction<TArgs, TResult>,
  options?: CircuitBreakerOptions
): (...args: TArgs) => Promise<TResult>

withApiCircuitBreaker

Alias for withCircuitBreaker - use in Next.js API routes.

How It Works

The circuit breaker has three states:

  1. CLOSED (Normal Operation)

    • All requests pass through to the wrapped function
    • Failures are counted
    • When failures exceed failureThreshold, circuit opens
  2. OPEN (Failing Fast)

    • All requests fail immediately without calling the wrapped function
    • After timeout milliseconds, circuit enters half-open state
  3. HALF_OPEN (Testing Recovery)

    • Limited requests are allowed through
    • If successThreshold consecutive requests succeed, circuit closes
    • If any request fails, circuit opens again

Best Practices

  1. Set appropriate thresholds based on your service's reliability requirements
  2. Use callbacks for logging and monitoring
  3. Provide fallback behavior when the circuit is open
  4. Monitor circuit state for alerting and debugging
const breaker = new CircuitBreaker(fetchData, {
  failureThreshold: 5,
  timeout: 30000,
  onOpen: () => {
    // Send alert to monitoring
    logger.error('Circuit opened for fetchData');
  },
  onClose: () => {
    logger.info('Circuit closed - service recovered');
  },
});

// With fallback
try {
  return await breaker.fire(id);
} catch (error) {
  if (breaker.isOpen()) {
    // Return cached data or default
    return getCachedData(id);
  }
  throw error;
}

Contributing

See Contributing Guide for details.

License

MIT © OpenSource Framework Contributors