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 - npm Package Compare versions

Comparing version
0.1.5
to
0.2.0
+109
README.md
# @fortify-ts/fallback
Fallback pattern for the Fortify-TS resilience library.
## Installation
```bash
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
```typescript
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
```typescript
const fallback = new Fallback<Response>({
fallback: (error) => {
console.log(`Falling back due to: ${error.message}`);
return cachedResponse;
},
});
```
### Custom Fallback Condition
```typescript
const fallback = new Fallback<Response>({
fallback: () => defaultResponse,
// Only fallback on network errors
shouldFallback: (error) => {
return error instanceof NetworkError;
},
});
```
### Configuration Options
```typescript
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
```typescript
const fallback = new Fallback<Data>({
fallback: async (error) => {
// Fallback to cache
return await cache.get('backup-data');
},
});
```
## Configuration Reference
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `fallback` | function | required | Fallback function |
| `shouldFallback` | function | - | Custom condition |
| `onFallback` | function | - | Fallback callback |
| `onSuccess` | function | - | Success callback |
| `logger` | FortifyLogger | - | Optional logger |
## License
MIT
+13
-2
import { FortifyLogger, Pattern, Operation } from '@fortify-ts/core';
import { z } from 'zod';
/**
* Zod schema for Fallback configuration.
* Note: The fallback function itself is validated separately since
* Zod can't express the generic type constraint.
*/
declare const fallbackConfigSchema: z.ZodObject<{
shouldFallback: z.ZodOptional<z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>>;
onFallback: z.ZodOptional<z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>>;
onSuccess: z.ZodOptional<z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>>;
}, z.core.$strip>;
/**
* Configuration for the Fallback pattern.

@@ -44,3 +55,3 @@ *

* @returns Validated configuration
* @throws {Error} When fallback function is not provided
* @throws {Error} When fallback function is not provided or is not a function
*/

@@ -98,2 +109,2 @@ declare function validateFallbackConfig<T>(config: FallbackConfig<T>): FallbackConfig<T>;

export { Fallback, type FallbackConfig, validateFallbackConfig };
export { Fallback, type FallbackConfig, fallbackConfigSchema, validateFallbackConfig };
import { FortifyLogger, Pattern, Operation } from '@fortify-ts/core';
import { z } from 'zod';
/**
* Zod schema for Fallback configuration.
* Note: The fallback function itself is validated separately since
* Zod can't express the generic type constraint.
*/
declare const fallbackConfigSchema: z.ZodObject<{
shouldFallback: z.ZodOptional<z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>>;
onFallback: z.ZodOptional<z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>>;
onSuccess: z.ZodOptional<z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>>;
}, z.core.$strip>;
/**
* Configuration for the Fallback pattern.

@@ -44,3 +55,3 @@ *

* @returns Validated configuration
* @throws {Error} When fallback function is not provided
* @throws {Error} When fallback function is not provided or is not a function
*/

@@ -98,2 +109,2 @@ declare function validateFallbackConfig<T>(config: FallbackConfig<T>): FallbackConfig<T>;

export { Fallback, type FallbackConfig, validateFallbackConfig };
export { Fallback, type FallbackConfig, fallbackConfigSchema, validateFallbackConfig };
+3
-2
{
"name": "@fortify-ts/fallback",
"version": "0.1.5",
"version": "0.2.0",
"description": "Fallback pattern for graceful degradation in @fortify-ts",

@@ -25,5 +25,6 @@ "type": "module",

"dependencies": {
"@fortify-ts/core": "0.2.0"
"@fortify-ts/core": "0.3.0"
},
"devDependencies": {
"fast-check": "^4.1.1",
"tsup": "^8.5.1",

@@ -30,0 +31,0 @@ "typescript": "^5.9.3",

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display