Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@fortify-ts/fallback

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fortify-ts/fallback

Fallback pattern for graceful degradation in @fortify-ts

Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
15
87.5%
Maintainers
1
Weekly downloads
 
Created
Source

@fortify-ts/fallback

Fallback pattern for the Fortify-TS resilience library.

Installation

npm install @fortify-ts/fallback
# or
pnpm add @fortify-ts/fallback

Features

  • Default Values: Return fallback on failure
  • Alternative Operations: Execute backup operation on failure
  • Custom Predicates: shouldFallback callback
  • Notifications: onFallback and onSuccess callbacks

Usage

Basic Usage

import { Fallback } from '@fortify-ts/fallback';

const fallback = new Fallback<User>({
  fallback: () => ({ id: 0, name: 'Guest' }),
});

// Returns Guest user if operation fails
const user = await fallback.execute(async (signal) => {
  return fetchUser(userId, { signal });
});

With Error Context

const fallback = new Fallback<Response>({
  fallback: (error) => {
    console.log(`Falling back due to: ${error.message}`);
    return cachedResponse;
  },
});

Custom Fallback Condition

const fallback = new Fallback<Response>({
  fallback: () => defaultResponse,

  // Only fallback on network errors
  shouldFallback: (error) => {
    return error instanceof NetworkError;
  },
});

Configuration Options

const fallback = new Fallback<Data>({
  // Fallback function (required)
  fallback: (error) => defaultData,

  // Custom fallback condition
  shouldFallback: (error) => error instanceof NetworkError,

  // Notification on fallback
  onFallback: (error) => {
    metrics.increment('fallback.activated');
  },

  // Notification on success
  onSuccess: (result) => {
    metrics.increment('primary.success');
  },

  // Optional logger
  logger: myLogger,
});

Async Fallback

const fallback = new Fallback<Data>({
  fallback: async (error) => {
    // Fallback to cache
    return await cache.get('backup-data');
  },
});

Configuration Reference

OptionTypeDefaultDescription
fallbackfunctionrequiredFallback function
shouldFallbackfunction-Custom condition
onFallbackfunction-Fallback callback
onSuccessfunction-Success callback
loggerFortifyLogger-Optional logger

License

MIT

Keywords

fallback

FAQs

Package last updated on 08 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts