@fortify-ts/fallback
Advanced tools
+109
| # @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 }; |
+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 }; |
+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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
2553301
8117.37%9
12.5%25632
8222.08%0
-100%110
Infinity%4
33.33%1
Infinity%1
Infinity%+ Added
- Removed
Updated