-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.d.ts
More file actions
135 lines (122 loc) · 4.01 KB
/
Copy pathindex.d.ts
File metadata and controls
135 lines (122 loc) · 4.01 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {IncomingMessage, ServerResponse} from 'http'
/**
* Options for configuring cache behavior.
*/
export interface CacheOptions {
/**
* Time-to-live for the cached entries, in milliseconds.
* Optional; defaults depend on the caching library used.
*/
ttl?: number
}
/**
* Interface for a cache implementation.
* A compatible cache must implement `get` and `set` methods.
*/
export interface Cache {
/**
* Retrieves the value associated with the given key from the cache.
*
* @param key - The key to look up in the cache.
* @returns A promise that resolves to the cached value, or `null` if the key is not found.
*/
get(key: string): Promise<any>
/**
* Stores a value in the cache with the specified key and options.
*
* @param key - The key to store the value under.
* @param value - The value to store in the cache.
* @param options - Additional options, such as TTL.
* @returns A promise that resolves once the value is stored.
*/
set(key: string, value: any, options?: CacheOptions): Promise<any>
}
/**
* Logger interface for custom logging implementations.
*/
export interface Logger {
/**
* Logs an error message or error object.
*
* @param args - The error message or objects to log.
*/
error(...args: any[]): void
}
/**
* Configuration options for the idempotency middleware.
*/
export interface IdempotencyMiddlewareOptions {
/**
* A cache instance that supports `get` and `set` methods.
* Compatible with libraries like `cache-manager`.
*/
cache: Cache
/**
* Default time-to-live for cached responses, in milliseconds.
* Must be between 1 and 86,400,000 (24 hours).
*/
ttl: number
/**
* A function to extract the idempotency key from the HTTP request.
* Defaults to extracting the `x-request-id` header.
*
* The returned key must be a non-empty string of at most 128 characters
* matching `^[a-zA-Z0-9_.~-]+$`. In multi-user environments the key should
* be scoped with a user/tenant identifier to prevent cross-user collisions.
*
* @param req - The incoming HTTP request.
* @returns The extracted idempotency key, or `undefined`/`null` to disable idempotency.
*/
idempotencyKeyExtractor?: (req: IncomingMessage) => string | undefined | null
/**
* An optional logger for error reporting.
* The logger must implement at least an `error` method.
* Defaults to using `console.error`.
*/
logger?: Logger
/**
* A prefix to prepend to cache keys to avoid collisions with other cached data.
* Defaults to `'idemp-key-'`.
*/
keyPrefix?: string
/**
* Maximum response body size (in bytes) that will be cached.
* Responses larger than this value are not cached, preventing cache memory exhaustion.
* Defaults to 1 MB (1,048,576 bytes).
*/
maxResponseSize?: number
/**
* Maximum time (in milliseconds) to wait for a cache read before giving up and
* calling the next handler. Defaults to 5,000 ms.
*/
cacheTimeout?: number
}
/**
* Creates an idempotency middleware function.
*
* The middleware ensures idempotent handling of requests by:
* - Checking if a response for a unique request key (idempotency key) is cached.
* - Returning the cached response if available, skipping reprocessing.
* - Caching successful responses (status codes 2xx) for future requests.
*
* The cache key is scoped by HTTP method, request URL, and the extracted idempotency
* key, and the middleware uses an in-flight lock to prevent concurrent duplicate
* processing of the same key.
*
* @param options - Configuration options for the middleware.
* @returns A middleware function compatible with Connect-like frameworks.
*/
export function idempotencyMiddleware(
options: IdempotencyMiddlewareOptions,
): (
req: IncomingMessage,
res: ServerResponse,
next: (err?: any) => void,
) => void
/**
* Generates a SHA-256 hash of the input string.
*
* @param data - The string to hash.
* @returns The hexadecimal SHA-256 hash of the input string.
*/
export function hashSha256(data: string): string