-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
105 lines (93 loc) · 2.99 KB
/
types.ts
File metadata and controls
105 lines (93 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Type definitions for fetch-gate library
*/
import type { Logger } from "pino"
export interface ProxyOptions {
/** Base URL for all proxied requests */
base?: string
/** Request timeout in milliseconds (default: 30000) */
timeout?: number
/** Circuit breaker configuration */
circuitBreaker?: CircuitBreakerOptions
/** Number of URLs to cache (default: 100, set to 0 to disable) */
cacheURLs?: number
/** Custom headers to add to all requests */
headers?: Record<string, string>
/** Whether to follow redirects (default: false) */
followRedirects?: boolean
/** Maximum number of redirects to follow (default: 5) */
maxRedirects?: number
/** Logger instance for request/response logging */
logger?: Logger
}
export interface CircuitBreakerOptions {
/** Failure threshold to open circuit (default: 5) */
failureThreshold?: number
/** Reset timeout in milliseconds (default: 60000) */
resetTimeout?: number
/** Request timeout for circuit breaker (default: 5000) */
timeout?: number
/** Enable circuit breaker (default: true) */
enabled?: boolean
}
export interface ProxyRequestOptions {
/** Override base URL for this request */
base?: string
/** Override timeout for this request */
timeout?: number
/** Additional headers for this request */
headers?: Record<string, string>
/** Query string parameters to append */
queryString?: Record<string, any> | string
/** Custom request options */
request?: RequestInit
/** Logger instance to override the global logger for this request */
logger?: Logger
// Lifecycle hooks
/** Hook called before the request is sent to the target server */
beforeRequest?: BeforeRequestHook
/** Hook called after a successful response is received */
afterResponse?: AfterResponseHook
/** Hook called when an error occurs during the request */
onError?: ErrorHook
/** Hook called before the circuit breaker executes the request */
beforeCircuitBreakerExecution?: BeforeCircuitBreakerHook
/** Hook called after the circuit breaker completes (success or failure) */
afterCircuitBreakerExecution?: AfterCircuitBreakerHook
}
// Enhanced Hook Types
export type BeforeRequestHook = (
req: Request,
options: ProxyRequestOptions,
) => void | Promise<void>
export type AfterResponseHook = (
req: Request,
res: Response,
body?: ReadableStream | null,
) => void | Promise<void>
export type BeforeCircuitBreakerHook = (
req: Request,
options: ProxyRequestOptions,
) => void | Promise<void>
export type AfterCircuitBreakerHook = (
req: Request,
result: CircuitBreakerResult,
) => void | Promise<void>
export type ErrorHook = (
req: Request,
error: Error,
) => void | Promise<void> | Promise<Response>
// Circuit breaker result information
export interface CircuitBreakerResult {
success: boolean
error?: Error
state: CircuitState
failureCount: number
executionTimeMs: number
fallbackResponse?: Response | void
}
export enum CircuitState {
CLOSED = "CLOSED",
OPEN = "OPEN",
HALF_OPEN = "HALF_OPEN",
}