hyperttp 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Hyperttp/Core/HttpClientImproved.d.ts +437 -109
- package/dist/Hyperttp/Core/HttpClientImproved.d.ts.map +1 -1
- package/dist/Hyperttp/Core/HttpClientImproved.js +631 -215
- package/dist/Hyperttp/Core/HttpClientImproved.js.map +1 -1
- package/dist/Hyperttp/Core/RateLimiter.d.ts +21 -24
- package/dist/Hyperttp/Core/RateLimiter.d.ts.map +1 -1
- package/dist/Hyperttp/Core/RateLimiter.js +48 -33
- package/dist/Hyperttp/Core/RateLimiter.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,66 +1,67 @@
|
|
|
1
|
-
import { CookieJar } from "tough-cookie";
|
|
2
1
|
import { type RateLimiterConfig } from "./RateLimiter";
|
|
3
2
|
import { ResponseType } from "../../Types";
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
4
|
+
* Base error class for HTTP client operations.
|
|
5
|
+
* Contains additional context about the failed request including status code, URL, and method.
|
|
6
6
|
*/
|
|
7
7
|
export declare class HttpClientError extends Error {
|
|
8
8
|
statusCode?: number | undefined;
|
|
9
9
|
originalError?: Error | undefined;
|
|
10
10
|
url?: string | undefined;
|
|
11
11
|
method?: string | undefined;
|
|
12
|
-
/**
|
|
13
|
-
* Creates a new HttpClientError instance.
|
|
14
|
-
* @param message The error message
|
|
15
|
-
* @param statusCode Optional HTTP status code
|
|
16
|
-
* @param originalError Optional original error that caused this error
|
|
17
|
-
* @param url Optional request URL
|
|
18
|
-
* @param method Optional HTTP method
|
|
19
|
-
*/
|
|
20
12
|
constructor(message: string, statusCode?: number | undefined, originalError?: Error | undefined, url?: string | undefined, method?: string | undefined);
|
|
21
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Error thrown when an HTTP request exceeds the configured timeout duration.
|
|
16
|
+
* Contains information about the URL and timeout value that caused the failure.
|
|
17
|
+
*/
|
|
22
18
|
export declare class TimeoutError extends HttpClientError {
|
|
23
|
-
/**
|
|
24
|
-
* Creates a new TimeoutError instance.
|
|
25
|
-
* @param url The request URL that timed out
|
|
26
|
-
* @param timeout The timeout duration in milliseconds
|
|
27
|
-
*/
|
|
28
19
|
constructor(url: string, timeout: number);
|
|
29
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Error thrown when an HTTP request is rate limited by the server.
|
|
23
|
+
* Contains information about the URL and optional retry-after duration.
|
|
24
|
+
*/
|
|
30
25
|
export declare class RateLimitError extends HttpClientError {
|
|
31
26
|
retryAfter?: number | undefined;
|
|
32
|
-
/**
|
|
33
|
-
* Creates a new RateLimitError instance.
|
|
34
|
-
* @param url The request URL that was rate limited
|
|
35
|
-
* @param retryAfter Optional retry after duration in milliseconds
|
|
36
|
-
*/
|
|
37
27
|
constructor(url: string, retryAfter?: number | undefined);
|
|
38
28
|
}
|
|
39
29
|
/**
|
|
40
|
-
* Log
|
|
30
|
+
* Log level for HTTP client logging.
|
|
31
|
+
* Defines the verbosity of log output from the client.
|
|
41
32
|
*/
|
|
42
33
|
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
43
34
|
/**
|
|
44
|
-
*
|
|
35
|
+
* Function type for logging HTTP client events.
|
|
36
|
+
* Used to customize how the client logs various events like requests, responses, and errors.
|
|
37
|
+
*
|
|
38
|
+
* @param level - The severity level of the log message
|
|
39
|
+
* @param message - The log message content
|
|
40
|
+
* @param meta - Optional additional context or metadata
|
|
45
41
|
*/
|
|
46
42
|
export type LoggerFunction = (level: LogLevel, message: string, meta?: any) => void;
|
|
47
43
|
/**
|
|
48
|
-
* Configuration options for retry behavior
|
|
44
|
+
* Configuration options for HTTP request retry behavior.
|
|
45
|
+
* Defines how the client should handle failed requests and when to retry them.
|
|
49
46
|
*/
|
|
50
47
|
export interface RetryOptions {
|
|
51
|
-
/** Maximum number of retry attempts
|
|
48
|
+
/** Maximum number of retry attempts before giving up */
|
|
52
49
|
maxRetries: number;
|
|
53
|
-
/** Base delay in milliseconds
|
|
50
|
+
/** Base delay in milliseconds between retry attempts (exponential backoff) */
|
|
54
51
|
baseDelay: number;
|
|
55
|
-
/** Maximum delay in milliseconds between
|
|
52
|
+
/** Maximum delay in milliseconds between retry attempts */
|
|
56
53
|
maxDelay: number;
|
|
57
54
|
/** HTTP status codes that should trigger a retry */
|
|
58
55
|
retryStatusCodes: number[];
|
|
59
|
-
/** Whether to add random jitter to retry delays
|
|
56
|
+
/** Whether to add random jitter to retry delays to prevent thundering herd */
|
|
60
57
|
jitter: boolean;
|
|
61
58
|
}
|
|
62
59
|
/**
|
|
63
|
-
*
|
|
60
|
+
* Function type for intercepting and modifying HTTP requests before they are sent.
|
|
61
|
+
* Request interceptors can modify the URL, method, headers, or body of outgoing requests.
|
|
62
|
+
*
|
|
63
|
+
* @param config - The request configuration object
|
|
64
|
+
* @returns A promise that resolves to the modified request configuration
|
|
64
65
|
*/
|
|
65
66
|
export type RequestInterceptor = (config: {
|
|
66
67
|
url: string;
|
|
@@ -74,7 +75,11 @@ export type RequestInterceptor = (config: {
|
|
|
74
75
|
body?: string | Buffer;
|
|
75
76
|
}>;
|
|
76
77
|
/**
|
|
77
|
-
*
|
|
78
|
+
* Function type for intercepting and modifying HTTP responses before they are processed.
|
|
79
|
+
* Response interceptors can modify the status, headers, body, or URL of incoming responses.
|
|
80
|
+
*
|
|
81
|
+
* @param response - The response object containing status, headers, body, and URL
|
|
82
|
+
* @returns A promise that resolves to the modified response object
|
|
78
83
|
*/
|
|
79
84
|
export type ResponseInterceptor = (response: {
|
|
80
85
|
status: number;
|
|
@@ -88,72 +93,207 @@ export type ResponseInterceptor = (response: {
|
|
|
88
93
|
url: string;
|
|
89
94
|
}>;
|
|
90
95
|
/**
|
|
91
|
-
* Configuration options for the
|
|
96
|
+
* Configuration options for the HttpClientImproved instance.
|
|
97
|
+
* Defines all the behavior settings for HTTP requests including timeouts, retries, caching, and more.
|
|
92
98
|
*/
|
|
93
99
|
export interface HttpClientOptions {
|
|
94
100
|
/** Request timeout in milliseconds (default: 15000) */
|
|
95
101
|
timeout?: number;
|
|
96
102
|
/** Maximum number of concurrent requests (default: 50) */
|
|
97
103
|
maxConcurrent?: number;
|
|
98
|
-
/** Maximum number of retry attempts (default: 3) */
|
|
104
|
+
/** Maximum number of retry attempts for failed requests (default: 3) */
|
|
99
105
|
maxRetries?: number;
|
|
100
106
|
/** Cache time-to-live in milliseconds (default: 300000) */
|
|
101
107
|
cacheTTL?: number;
|
|
102
|
-
/** Maximum
|
|
108
|
+
/** Maximum number of cached entries (default: 500) */
|
|
103
109
|
cacheMaxSize?: number;
|
|
104
|
-
/** Rate limiting configuration */
|
|
110
|
+
/** Rate limiting configuration to prevent overwhelming servers */
|
|
105
111
|
rateLimit?: RateLimiterConfig;
|
|
106
|
-
/**
|
|
112
|
+
/** User-Agent string for HTTP requests (default: "Hyperttp/0.1.0 Node.js") */
|
|
107
113
|
userAgent?: string;
|
|
108
|
-
/**
|
|
114
|
+
/** Custom logger function for HTTP client events */
|
|
109
115
|
logger?: LoggerFunction;
|
|
110
|
-
/**
|
|
116
|
+
/** Retry behavior configuration */
|
|
111
117
|
retryOptions?: Partial<RetryOptions>;
|
|
112
|
-
/**
|
|
118
|
+
/** Whether to automatically follow HTTP redirects (default: true) */
|
|
113
119
|
followRedirects?: boolean;
|
|
114
|
-
/** Maximum redirects to follow (default: 5) */
|
|
120
|
+
/** Maximum number of redirects to follow (default: 5) */
|
|
115
121
|
maxRedirects?: number;
|
|
116
|
-
/**
|
|
122
|
+
/** Maximum response size in bytes (default: 1MB) */
|
|
117
123
|
maxResponseBytes?: number;
|
|
124
|
+
/** Function to validate HTTP status codes (default: 200-299) */
|
|
125
|
+
validateStatus?: (status: number) => boolean;
|
|
126
|
+
/** HTTP methods that should be cached (default: ["GET", "HEAD"]) */
|
|
127
|
+
cacheMethods?: string[];
|
|
128
|
+
/** Maximum number of request metrics to store (default: 10000) */
|
|
129
|
+
maxMetricsSize?: number;
|
|
130
|
+
/** Whether to enable verbose logging (default: false) */
|
|
131
|
+
verbose?: boolean;
|
|
118
132
|
}
|
|
119
133
|
/**
|
|
120
|
-
* Interface for request
|
|
134
|
+
* Interface for defining HTTP request parameters.
|
|
135
|
+
* Used to encapsulate URL, headers, and body data for HTTP requests.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* class ApiRequest implements RequestInterface {
|
|
140
|
+
* constructor(
|
|
141
|
+
* private url: string,
|
|
142
|
+
* private headers: Record<string, string> = {},
|
|
143
|
+
* private body?: any
|
|
144
|
+
* ) {}
|
|
145
|
+
*
|
|
146
|
+
* getURL(): string { return this.url; }
|
|
147
|
+
* getHeaders(): Record<string, string> { return this.headers; }
|
|
148
|
+
* getBodyData(): any { return this.body; }
|
|
149
|
+
* }
|
|
150
|
+
* ```
|
|
121
151
|
*/
|
|
122
152
|
export interface RequestInterface {
|
|
153
|
+
/** Returns the full URL for the HTTP request */
|
|
123
154
|
getURL(): string;
|
|
155
|
+
/** Returns the request body data (string, Buffer, or any serializable object) */
|
|
124
156
|
getBodyData(): any;
|
|
157
|
+
/** Returns the HTTP headers for the request */
|
|
125
158
|
getHeaders(): Record<string, string>;
|
|
126
159
|
}
|
|
127
160
|
/**
|
|
128
|
-
* Interface for HTTP client implementations
|
|
161
|
+
* Interface defining the contract for HTTP client implementations.
|
|
162
|
+
* Provides methods for making HTTP requests with various HTTP methods.
|
|
129
163
|
*/
|
|
130
164
|
export interface HttpClientInterface {
|
|
165
|
+
/**
|
|
166
|
+
* Makes an HTTP GET request
|
|
167
|
+
* @param req - Request configuration or URL string
|
|
168
|
+
* @param responseType - Expected response type (default: "json")
|
|
169
|
+
* @returns Promise resolving to the response data
|
|
170
|
+
*/
|
|
131
171
|
get<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
132
|
-
|
|
133
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Makes an HTTP POST request
|
|
174
|
+
* @param req - Request configuration or URL string
|
|
175
|
+
* @param body - Request body data
|
|
176
|
+
* @param responseType - Expected response type (default: "json")
|
|
177
|
+
* @returns Promise resolving to the response data
|
|
178
|
+
*/
|
|
179
|
+
post<T = any>(req: RequestInterface, body?: any, responseType?: ResponseType): Promise<T>;
|
|
180
|
+
/**
|
|
181
|
+
* Makes an HTTP PUT request
|
|
182
|
+
* @param req - Request configuration or URL string
|
|
183
|
+
* @param body - Request body data
|
|
184
|
+
* @param responseType - Expected response type (default: "json")
|
|
185
|
+
* @returns Promise resolving to the response data
|
|
186
|
+
*/
|
|
187
|
+
put<T = any>(req: RequestInterface, body?: any, responseType?: ResponseType): Promise<T>;
|
|
188
|
+
/**
|
|
189
|
+
* Makes an HTTP DELETE request
|
|
190
|
+
* @param req - Request configuration or URL string
|
|
191
|
+
* @param responseType - Expected response type (default: "json")
|
|
192
|
+
* @returns Promise resolving to the response data
|
|
193
|
+
*/
|
|
134
194
|
delete<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
195
|
+
/**
|
|
196
|
+
* Makes an HTTP PATCH request
|
|
197
|
+
* @param req - Request configuration
|
|
198
|
+
* @param responseType - Expected response type (default: "json")
|
|
199
|
+
* @returns Promise resolving to the response data
|
|
200
|
+
*/
|
|
135
201
|
patch<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
136
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Makes an HTTP HEAD request
|
|
204
|
+
* @param req - Request configuration or URL string
|
|
205
|
+
* @returns Promise resolving to status and headers
|
|
206
|
+
*/
|
|
207
|
+
head(req: RequestInterface): Promise<{
|
|
208
|
+
status: number;
|
|
209
|
+
headers: Record<string, any>;
|
|
210
|
+
}>;
|
|
211
|
+
/**
|
|
212
|
+
* Clears the internal cache of the HTTP client
|
|
213
|
+
*/
|
|
137
214
|
clearCache(): void;
|
|
138
215
|
}
|
|
139
216
|
/**
|
|
140
|
-
*
|
|
217
|
+
* Metrics collected for HTTP requests to monitor performance and behavior.
|
|
218
|
+
* Contains timing information, response details, and caching information.
|
|
141
219
|
*/
|
|
142
220
|
export interface RequestMetrics {
|
|
221
|
+
/** Timestamp when the request started */
|
|
143
222
|
startTime: number;
|
|
223
|
+
/** Timestamp when the request completed */
|
|
144
224
|
endTime: number;
|
|
225
|
+
/** Total duration of the request in milliseconds */
|
|
145
226
|
duration: number;
|
|
227
|
+
/** HTTP status code of the response (if available) */
|
|
146
228
|
statusCode?: number;
|
|
229
|
+
/** Number of bytes received in the response */
|
|
147
230
|
bytesReceived: number;
|
|
231
|
+
/** Number of bytes sent in the request body */
|
|
148
232
|
bytesSent: number;
|
|
233
|
+
/** Number of retry attempts made for this request */
|
|
149
234
|
retries: number;
|
|
235
|
+
/** Whether the response was served from cache */
|
|
150
236
|
cached: boolean;
|
|
237
|
+
/** URL of the request */
|
|
151
238
|
url: string;
|
|
239
|
+
/** HTTP method used (GET, POST, etc.) */
|
|
152
240
|
method: string;
|
|
241
|
+
/** Hash of the request body for cache key generation */
|
|
242
|
+
bodyHash?: string;
|
|
153
243
|
}
|
|
154
244
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
245
|
+
* @ru
|
|
246
|
+
* Улучшенный HTTP-клиент с кэшированием, ограничением скорости, логикой повторных попыток и расширенными функциями.
|
|
247
|
+
* Предоставляет надежный интерфейс для выполнения HTTP-запросов с автоматической обработкой
|
|
248
|
+
* распространенных паттернов, таких как повторные попытки, кэширование и перехват запросов/ответов.
|
|
249
|
+
* @en
|
|
250
|
+
* Enhanced HTTP client with caching, rate limiting, retry logic, and advanced features.
|
|
251
|
+
* Provides a robust interface for making HTTP requests with automatic handling of
|
|
252
|
+
* common patterns like retries, caching, and request/response interception.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const client = new HttpClientImproved({
|
|
257
|
+
* timeout: 10000,
|
|
258
|
+
* maxRetries: 3,
|
|
259
|
+
* cacheTTL: 300000,
|
|
260
|
+
* rateLimit: { maxRequests: 100, windowMs: 60000 }
|
|
261
|
+
* });
|
|
262
|
+
*
|
|
263
|
+
* const response = await client.get('https://api.example.com/data');
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* // Using the fluent request builder
|
|
269
|
+
* const client = new HttpClientImproved();
|
|
270
|
+
* const response = await client.request('https://api.example.com/data')
|
|
271
|
+
* .headers({ 'Authorization': 'Bearer token' })
|
|
272
|
+
* .json()
|
|
273
|
+
* .send();
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* // Using RequestInterface for complex requests
|
|
279
|
+
* import { RequestInterface } from './src';
|
|
280
|
+
*
|
|
281
|
+
* class ApiRequest implements RequestInterface {
|
|
282
|
+
* constructor(
|
|
283
|
+
* private url: string,
|
|
284
|
+
* private headers: Record<string, string> = {},
|
|
285
|
+
* private body?: any
|
|
286
|
+
* ) {}
|
|
287
|
+
*
|
|
288
|
+
* getURL(): string { return this.url; }
|
|
289
|
+
* getHeaders(): Record<string, string> { return this.headers; }
|
|
290
|
+
* getBodyData(): any { return this.body; }
|
|
291
|
+
* }
|
|
292
|
+
*
|
|
293
|
+
* const client = new HttpClientImproved();
|
|
294
|
+
* const request = new ApiRequest('https://api.example.com/data');
|
|
295
|
+
* const response = await client.get(request);
|
|
296
|
+
* ```
|
|
157
297
|
*/
|
|
158
298
|
export default class HttpClientImproved implements HttpClientInterface {
|
|
159
299
|
private cookieJar;
|
|
@@ -168,106 +308,201 @@ export default class HttpClientImproved implements HttpClientInterface {
|
|
|
168
308
|
private requestInterceptors;
|
|
169
309
|
private responseInterceptors;
|
|
170
310
|
private requestMetrics;
|
|
171
|
-
/**
|
|
172
|
-
* Creates a new instance of HttpClientImproved.
|
|
173
|
-
* @param options Optional configuration options for the HTTP client
|
|
174
|
-
*/
|
|
175
311
|
constructor(options?: HttpClientOptions);
|
|
312
|
+
private log;
|
|
176
313
|
/**
|
|
177
|
-
*
|
|
178
|
-
* @param
|
|
314
|
+
* Creates a hash of the request body for cache key generation.
|
|
315
|
+
* @param body - Request body (string or Buffer)
|
|
316
|
+
* @returns SHA1 hash of the body, truncated to 8 characters
|
|
179
317
|
*/
|
|
180
|
-
|
|
318
|
+
private hashBody;
|
|
181
319
|
/**
|
|
182
|
-
*
|
|
183
|
-
* @
|
|
320
|
+
* Calculates the delay for retry attempts using exponential backoff.
|
|
321
|
+
* @param attempt - Current retry attempt number (0-based)
|
|
322
|
+
* @returns Delay in milliseconds
|
|
184
323
|
*/
|
|
185
|
-
|
|
324
|
+
private calcDelay;
|
|
186
325
|
/**
|
|
187
|
-
*
|
|
188
|
-
* @param
|
|
326
|
+
* Creates a promise that resolves after the specified delay.
|
|
327
|
+
* @param ms - Delay in milliseconds
|
|
328
|
+
* @returns Promise that resolves after the delay
|
|
189
329
|
*/
|
|
190
|
-
|
|
330
|
+
private sleep;
|
|
191
331
|
/**
|
|
192
|
-
*
|
|
193
|
-
*
|
|
332
|
+
* Applies all registered request interceptors to modify the request configuration.
|
|
333
|
+
* Interceptors are executed in sequence, with each one receiving the output of the previous.
|
|
334
|
+
* @param config - Original request configuration
|
|
335
|
+
* @returns Modified request configuration
|
|
194
336
|
*/
|
|
195
|
-
addResponseInterceptor(interceptor: ResponseInterceptor): void;
|
|
196
|
-
/** Closes the HTTP agent to properly terminate keep-alive connections. */
|
|
197
|
-
close(): void;
|
|
198
|
-
private log;
|
|
199
|
-
private decompress;
|
|
200
|
-
private calcDelay;
|
|
201
|
-
private sleep;
|
|
202
337
|
private applyRequestInterceptors;
|
|
338
|
+
/**
|
|
339
|
+
* Applies all registered response interceptors to modify the response data.
|
|
340
|
+
* Interceptors are executed in sequence, with each one receiving the output of the previous.
|
|
341
|
+
* @param response - Original response data
|
|
342
|
+
* @returns Modified response data
|
|
343
|
+
*/
|
|
203
344
|
private applyResponseInterceptors;
|
|
345
|
+
/**
|
|
346
|
+
* Resolves a redirect location relative to the base URL.
|
|
347
|
+
* Handles both absolute and relative redirect URLs.
|
|
348
|
+
* @param location - The redirect location from the response
|
|
349
|
+
* @param baseUrl - The original request URL
|
|
350
|
+
* @returns The resolved absolute URL
|
|
351
|
+
*/
|
|
204
352
|
private resolveRedirect;
|
|
353
|
+
/**
|
|
354
|
+
* Parses the Retry-After header to determine when to retry a request.
|
|
355
|
+
* Supports both seconds and HTTP date formats.
|
|
356
|
+
* @param retryAfterHeader - The Retry-After header value
|
|
357
|
+
* @returns Delay in milliseconds, or undefined if not parseable
|
|
358
|
+
*/
|
|
205
359
|
private parseRetryAfterMs;
|
|
360
|
+
/**
|
|
361
|
+
* Reads response body with size limit enforcement.
|
|
362
|
+
* Collects chunks until the response is complete or the limit is exceeded.
|
|
363
|
+
* @param body - Async iterable of response chunks
|
|
364
|
+
* @returns Complete response body as a Buffer
|
|
365
|
+
*/
|
|
206
366
|
private readBodyWithLimit;
|
|
367
|
+
/**
|
|
368
|
+
* Removes old metrics entries to prevent memory leaks.
|
|
369
|
+
* Keeps only metrics from the last 24 hours.
|
|
370
|
+
*/
|
|
371
|
+
private trimMetrics;
|
|
372
|
+
/**
|
|
373
|
+
* Sends an HTTP request with retry logic and rate limiting.
|
|
374
|
+
* Handles timeouts, redirects, and various retry scenarios.
|
|
375
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
376
|
+
* @param url - Target URL
|
|
377
|
+
* @param headers - HTTP headers
|
|
378
|
+
* @param body - Request body (optional)
|
|
379
|
+
* @param metrics - Optional metrics object to track request details
|
|
380
|
+
* @param redirects - Number of redirects followed so far
|
|
381
|
+
* @returns Promise resolving to the response data
|
|
382
|
+
*/
|
|
207
383
|
private sendWithRetry;
|
|
384
|
+
/**
|
|
385
|
+
* Parses the Content-Type header to extract MIME type and character encoding.
|
|
386
|
+
* @param contentType - Content-Type header value
|
|
387
|
+
* @returns Object containing type and charset information
|
|
388
|
+
*/
|
|
208
389
|
private parseContentType;
|
|
390
|
+
/**
|
|
391
|
+
* Parses the HTTP response body based on content type and requested response type.
|
|
392
|
+
* Handles JSON, XML, text, and buffer responses with fallback parsing.
|
|
393
|
+
* @param res - HTTP response object
|
|
394
|
+
* @param responseType - Desired response type
|
|
395
|
+
* @returns Parsed response data
|
|
396
|
+
*/
|
|
209
397
|
private parseResponse;
|
|
398
|
+
/**
|
|
399
|
+
* Makes an HTTP request without using the cache.
|
|
400
|
+
* Used for methods that shouldn't be cached or when caching is disabled.
|
|
401
|
+
* @param method - HTTP method
|
|
402
|
+
* @param req - Request configuration
|
|
403
|
+
* @param responseType - Expected response type
|
|
404
|
+
* @returns Promise resolving to the response data
|
|
405
|
+
*/
|
|
406
|
+
private requestInternalWithoutCache;
|
|
407
|
+
/**
|
|
408
|
+
* Makes an HTTP request with caching support.
|
|
409
|
+
* Handles cache lookups, request deduplication, and automatic cache storage.
|
|
410
|
+
* @param method - HTTP method
|
|
411
|
+
* @param req - Request configuration
|
|
412
|
+
* @param useCache - Whether to use caching (default: true)
|
|
413
|
+
* @param responseType - Expected response type
|
|
414
|
+
* @returns Promise resolving to the response data
|
|
415
|
+
*/
|
|
210
416
|
private requestInternal;
|
|
211
417
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @
|
|
216
|
-
* @
|
|
418
|
+
* Makes an HTTP GET request.
|
|
419
|
+
* Supports both RequestInterface objects and direct URL strings.
|
|
420
|
+
* GET requests are cached by default unless caching is disabled.
|
|
421
|
+
* @param req - Request configuration or URL string
|
|
422
|
+
* @param responseType - Expected response type (default: "json")
|
|
423
|
+
* @returns Promise resolving to the response data
|
|
217
424
|
*/
|
|
218
|
-
get<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
425
|
+
get<T = any>(req: RequestInterface | string, responseType?: ResponseType): Promise<T>;
|
|
219
426
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
* @
|
|
224
|
-
* @
|
|
427
|
+
* Makes an HTTP POST request.
|
|
428
|
+
* Supports both RequestInterface objects and direct URL strings with body data.
|
|
429
|
+
* POST requests are not cached by default due to their side effects.
|
|
430
|
+
* @param req - Request configuration or URL string
|
|
431
|
+
* @param body - Request body data (optional)
|
|
432
|
+
* @param responseType - Expected response type (default: "json")
|
|
433
|
+
* @returns Promise resolving to the response data
|
|
225
434
|
*/
|
|
226
|
-
post<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
435
|
+
post<T = any>(req: RequestInterface | string, body?: any, responseType?: ResponseType): Promise<T>;
|
|
227
436
|
/**
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* @
|
|
232
|
-
* @
|
|
437
|
+
* Makes an HTTP PUT request.
|
|
438
|
+
* Supports both RequestInterface objects and direct URL strings with body data.
|
|
439
|
+
* PUT requests are not cached by default due to their side effects.
|
|
440
|
+
* @param req - Request configuration or URL string
|
|
441
|
+
* @param body - Request body data (optional)
|
|
442
|
+
* @param responseType - Expected response type (default: "json")
|
|
443
|
+
* @returns Promise resolving to the response data
|
|
233
444
|
*/
|
|
234
|
-
put<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
445
|
+
put<T = any>(req: RequestInterface | string, body?: any, responseType?: ResponseType): Promise<T>;
|
|
235
446
|
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
* @
|
|
240
|
-
* @
|
|
447
|
+
* Makes an HTTP DELETE request.
|
|
448
|
+
* Supports both RequestInterface objects and direct URL strings.
|
|
449
|
+
* DELETE requests are not cached by default due to their side effects.
|
|
450
|
+
* @param req - Request configuration or URL string
|
|
451
|
+
* @param responseType - Expected response type (default: "json")
|
|
452
|
+
* @returns Promise resolving to the response data
|
|
241
453
|
*/
|
|
242
|
-
delete<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
454
|
+
delete<T = any>(req: RequestInterface | string, responseType?: ResponseType): Promise<T>;
|
|
243
455
|
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* @param
|
|
247
|
-
* @
|
|
248
|
-
* @
|
|
456
|
+
* Makes an HTTP PATCH request.
|
|
457
|
+
* PATCH requests are not cached by default due to their side effects.
|
|
458
|
+
* @param req - Request configuration
|
|
459
|
+
* @param responseType - Expected response type (default: "json")
|
|
460
|
+
* @returns Promise resolving to the response data
|
|
249
461
|
*/
|
|
250
462
|
patch<T = any>(req: RequestInterface, responseType?: ResponseType): Promise<T>;
|
|
251
463
|
/**
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
464
|
+
* Makes an HTTP HEAD request.
|
|
465
|
+
* Returns only the status code and headers without the response body.
|
|
466
|
+
* HEAD requests are not cached by default.
|
|
467
|
+
* @param req - Request configuration or URL string
|
|
468
|
+
* @returns Promise resolving to status and headers
|
|
255
469
|
*/
|
|
256
|
-
head(req: RequestInterface): Promise<
|
|
470
|
+
head(req: RequestInterface | string): Promise<{
|
|
471
|
+
status: number;
|
|
472
|
+
headers: Record<string, any>;
|
|
473
|
+
}>;
|
|
257
474
|
/**
|
|
258
|
-
* Clears the
|
|
475
|
+
* Clears the internal cache of the HTTP client.
|
|
476
|
+
* Removes all cached responses and resets the cache state.
|
|
259
477
|
*/
|
|
260
478
|
clearCache(): void;
|
|
261
479
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
|
|
265
|
-
|
|
480
|
+
* Clears all collected request metrics.
|
|
481
|
+
* Removes performance and timing data from memory.
|
|
482
|
+
*/
|
|
483
|
+
clearMetrics(): void;
|
|
484
|
+
/**
|
|
485
|
+
* Retrieves metrics for a specific request by its URL.
|
|
486
|
+
* @param key - The URL or cache key to retrieve metrics for
|
|
487
|
+
* @returns Metrics object if found, undefined otherwise
|
|
266
488
|
*/
|
|
267
|
-
getMetrics(
|
|
489
|
+
getMetrics(key: string): RequestMetrics | undefined;
|
|
490
|
+
/**
|
|
491
|
+
* Retrieves all collected request metrics.
|
|
492
|
+
* @returns Array of all metrics objects
|
|
493
|
+
*/
|
|
494
|
+
getAllMetrics(): RequestMetrics[];
|
|
495
|
+
/**
|
|
496
|
+
* Creates a fluent request builder for making HTTP requests.
|
|
497
|
+
* Provides a chainable API for building and sending requests.
|
|
498
|
+
* @param url - The target URL for the request
|
|
499
|
+
* @returns RequestBuilder instance for chaining
|
|
500
|
+
*/
|
|
501
|
+
request<T = any>(url: string): RequestBuilder<T>;
|
|
268
502
|
/**
|
|
269
503
|
* Returns current statistics about the HTTP client's state.
|
|
270
|
-
*
|
|
504
|
+
* Useful for monitoring and debugging performance.
|
|
505
|
+
* @returns Object containing various client statistics
|
|
271
506
|
*/
|
|
272
507
|
getStats(): {
|
|
273
508
|
cacheSize: number;
|
|
@@ -275,6 +510,99 @@ export default class HttpClientImproved implements HttpClientInterface {
|
|
|
275
510
|
queuedRequests: number;
|
|
276
511
|
activeRequests: number;
|
|
277
512
|
currentRateLimit: number;
|
|
513
|
+
metricsSize: number;
|
|
278
514
|
};
|
|
279
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Fluent request builder for making HTTP requests with a chainable API.
|
|
518
|
+
* Provides a convenient way to build and send HTTP requests with various options.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```ts
|
|
522
|
+
* const client = new HttpClientImproved();
|
|
523
|
+
* const response = await client.request('https://api.example.com/data')
|
|
524
|
+
* .headers({ 'Authorization': 'Bearer token' })
|
|
525
|
+
* .query({ limit: 10, offset: 0 })
|
|
526
|
+
* .json()
|
|
527
|
+
* .send();
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
declare class RequestBuilder<T = any> {
|
|
531
|
+
private _url;
|
|
532
|
+
private _method;
|
|
533
|
+
private _headers;
|
|
534
|
+
private _body?;
|
|
535
|
+
private _responseType;
|
|
536
|
+
/**
|
|
537
|
+
* Creates a new request builder for the specified URL.
|
|
538
|
+
* @param url - The target URL for the request
|
|
539
|
+
*/
|
|
540
|
+
constructor(url: string);
|
|
541
|
+
/**
|
|
542
|
+
* Sets HTTP headers for the request.
|
|
543
|
+
* @param headers - Object containing header key-value pairs
|
|
544
|
+
* @returns The builder instance for chaining
|
|
545
|
+
*/
|
|
546
|
+
headers(headers: Record<string, string>): this;
|
|
547
|
+
/**
|
|
548
|
+
* Sets the request body data.
|
|
549
|
+
* @param bodyData - The body data to send with the request
|
|
550
|
+
* @returns The builder instance for chaining
|
|
551
|
+
*/
|
|
552
|
+
body(bodyData: any): this;
|
|
553
|
+
/**
|
|
554
|
+
* Sets the response type to JSON.
|
|
555
|
+
* @returns The builder instance for chaining
|
|
556
|
+
*/
|
|
557
|
+
json(): this;
|
|
558
|
+
/**
|
|
559
|
+
* Sets the response type to plain text.
|
|
560
|
+
* @returns The builder instance for chaining
|
|
561
|
+
*/
|
|
562
|
+
text(): this;
|
|
563
|
+
/**
|
|
564
|
+
* Sets the response type to XML.
|
|
565
|
+
* @returns The builder instance for chaining
|
|
566
|
+
*/
|
|
567
|
+
xml(): this;
|
|
568
|
+
/**
|
|
569
|
+
* Sets the HTTP method to POST.
|
|
570
|
+
* @returns The builder instance for chaining
|
|
571
|
+
*/
|
|
572
|
+
post(): this;
|
|
573
|
+
/**
|
|
574
|
+
* Sets the HTTP method to PUT.
|
|
575
|
+
* @returns The builder instance for chaining
|
|
576
|
+
*/
|
|
577
|
+
put(): this;
|
|
578
|
+
/**
|
|
579
|
+
* Sets the HTTP method to PATCH.
|
|
580
|
+
* @returns The builder instance for chaining
|
|
581
|
+
*/
|
|
582
|
+
patch(): this;
|
|
583
|
+
/**
|
|
584
|
+
* Sets the HTTP method to DELETE.
|
|
585
|
+
* @returns The builder instance for chaining
|
|
586
|
+
*/
|
|
587
|
+
delete(): this;
|
|
588
|
+
/**
|
|
589
|
+
* Adds query parameters to the URL.
|
|
590
|
+
* @param params - Object containing query parameter key-value pairs
|
|
591
|
+
* @returns The builder instance for chaining
|
|
592
|
+
*/
|
|
593
|
+
query(params: Record<string, string | number | boolean>): this;
|
|
594
|
+
/**
|
|
595
|
+
* Sets a JSON body for the request.
|
|
596
|
+
* Automatically sets the Content-Type header to application/json.
|
|
597
|
+
* @param body - The JSON body data
|
|
598
|
+
* @returns The builder instance for chaining
|
|
599
|
+
*/
|
|
600
|
+
jsonBody<T>(body: T): this;
|
|
601
|
+
/**
|
|
602
|
+
* Sends the HTTP request and returns the response.
|
|
603
|
+
* @returns Promise resolving to the response data
|
|
604
|
+
*/
|
|
605
|
+
send(): Promise<T>;
|
|
606
|
+
}
|
|
607
|
+
export {};
|
|
280
608
|
//# sourceMappingURL=HttpClientImproved.d.ts.map
|