Skip to content

Commit cd2fe88

Browse files
committed
chore: optimize types for JSR (Fast Check)
1 parent 3dd6c14 commit cd2fe88

5 files changed

Lines changed: 143 additions & 3 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"description": "Why do we fall, Bruce? Resilience patterns for async operations.",
55
"type": "module",
66
"main": "src/index.js",
7-
"types": "./index.d.ts",
7+
"types": "./src/index.d.ts",
88
"exports": {
99
".": {
10-
"types": "./index.d.ts",
10+
"types": "./src/index.d.ts",
1111
"default": "./src/index.js"
1212
},
1313
"./testing": {
14-
"types": "./index.d.ts",
14+
"types": "./src/testing.d.ts",
1515
"default": "./src/testing.js"
1616
}
1717
},
File renamed without changes.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Exports all public APIs for building resilient applications.
44
*/
55

6+
// @ts-self-types="./index.d.ts"
7+
68
// Error types
79
export {
810
RetryExhaustedError,

src/testing.d.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
export interface RetryOptions {
2+
retries?: number;
3+
delay?: number;
4+
maxDelay?: number;
5+
backoff?: 'constant' | 'linear' | 'exponential';
6+
jitter?: 'none' | 'full' | 'equal' | 'decorrelated';
7+
shouldRetry?: (error: Error) => boolean;
8+
onRetry?: (error: Error, attempt: number, delay: number) => void;
9+
telemetry?: TelemetrySink;
10+
clock?: any;
11+
}
12+
13+
export interface CircuitBreakerOptions {
14+
threshold: number;
15+
duration: number;
16+
successThreshold?: number;
17+
shouldTrip?: (error: Error) => boolean;
18+
onOpen?: () => void;
19+
onClose?: () => void;
20+
onHalfOpen?: () => void;
21+
telemetry?: TelemetrySink;
22+
clock?: any;
23+
}
24+
25+
export interface TimeoutOptions {
26+
onTimeout?: (elapsed: number) => void;
27+
telemetry?: TelemetrySink;
28+
}
29+
30+
export interface BulkheadOptions {
31+
limit: number;
32+
queueLimit?: number;
33+
telemetry?: TelemetrySink;
34+
clock?: any;
35+
}
36+
37+
export interface TelemetryEvent {
38+
type: string;
39+
timestamp: number;
40+
[key: string]: any;
41+
}
42+
43+
export interface TelemetrySink {
44+
emit(event: TelemetryEvent): void;
45+
}
46+
47+
export class InMemorySink implements TelemetrySink {
48+
events: TelemetryEvent[];
49+
emit(event: TelemetryEvent): void;
50+
clear(): void;
51+
}
52+
53+
export class ConsoleSink implements TelemetrySink {
54+
emit(event: TelemetryEvent): void;
55+
}
56+
57+
export class NoopSink implements TelemetrySink {
58+
emit(event: TelemetryEvent): void;
59+
}
60+
61+
export class MultiSink implements TelemetrySink {
62+
constructor(sinks: TelemetrySink[]);
63+
emit(event: TelemetryEvent): void;
64+
}
65+
66+
export class RetryExhaustedError extends Error {
67+
attempts: number;
68+
cause: Error;
69+
constructor(attempts: number, cause: Error);
70+
}
71+
72+
export class CircuitOpenError extends Error {
73+
openedAt: Date;
74+
failureCount: number;
75+
constructor(openedAt: Date, failureCount: number);
76+
}
77+
78+
export class TimeoutError extends Error {
79+
timeout: number;
80+
elapsed: number;
81+
constructor(timeout: number, elapsed: number);
82+
}
83+
84+
export class BulkheadRejectedError extends Error {
85+
limit: number;
86+
queueLimit: number;
87+
constructor(limit: number, queueLimit: number);
88+
}
89+
90+
export function retry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
91+
92+
export interface CircuitBreaker {
93+
execute<T>(fn: () => Promise<T>): Promise<T>;
94+
readonly state: 'CLOSED' | 'OPEN' | 'HALF_OPEN';
95+
}
96+
97+
export function circuitBreaker(options: CircuitBreakerOptions): CircuitBreaker;
98+
99+
export function timeout<T>(ms: number, fn: ((signal: AbortSignal) => Promise<T>) | (() => Promise<T>), options?: TimeoutOptions): Promise<T>;
100+
101+
export interface Bulkhead {
102+
execute<T>(fn: () => Promise<T>): Promise<T>;
103+
readonly stats: { active: number; pending: number; available: number };
104+
}
105+
106+
export function bulkhead(options: BulkheadOptions): Bulkhead;
107+
108+
export function compose(...policies: any[]): { execute<T>(fn: () => Promise<T>): Promise<T> };
109+
export function fallback(primary: any, secondary: any): { execute<T>(fn: () => Promise<T>): Promise<T> };
110+
export function race(primary: any, secondary: any): { execute<T>(fn: () => Promise<T>): Promise<T> };
111+
112+
export class Policy {
113+
constructor(executor: (fn: () => Promise<any>) => Promise<any>);
114+
static retry(options?: RetryOptions): Policy;
115+
static circuitBreaker(options: CircuitBreakerOptions): Policy;
116+
static timeout(ms: number, options?: TimeoutOptions): Policy;
117+
static bulkhead(options: BulkheadOptions): Policy;
118+
static noop(): Policy;
119+
120+
wrap(otherPolicy: Policy): Policy;
121+
or(otherPolicy: Policy): Policy;
122+
race(otherPolicy: Policy): Policy;
123+
execute<T>(fn: () => Promise<T>): Promise<T>;
124+
}
125+
126+
export class SystemClock {
127+
now(): number;
128+
sleep(ms: number): Promise<void>;
129+
}
130+
131+
export class TestClock {
132+
now(): number;
133+
sleep(ms: number): Promise<void>;
134+
tick(ms?: number): Promise<void>;
135+
advance(ms: number): Promise<void>;
136+
}

src/testing.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
* Provides tools for deterministic testing of resilience policies.
44
*/
55

6+
// @ts-self-types="./testing.d.ts"
7+
68
export { TestClock } from './utils/clock.js';

0 commit comments

Comments
 (0)