pulse-js-framework 1.7.32 → 1.7.37

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.
@@ -0,0 +1,426 @@
1
+ /**
2
+ * Pulse HTTP Client TypeScript Definitions
3
+ * @module pulse-js-framework/runtime/http
4
+ */
5
+
6
+ import { Pulse } from './pulse';
7
+ import { AsyncStatus, UseResourceReturn, ResourceOptions } from './async';
8
+
9
+ // ============================================================================
10
+ // Error Types
11
+ // ============================================================================
12
+
13
+ /**
14
+ * HTTP error codes
15
+ */
16
+ export type HttpErrorCode =
17
+ | 'TIMEOUT'
18
+ | 'NETWORK'
19
+ | 'ABORT'
20
+ | 'HTTP_ERROR'
21
+ | 'PARSE_ERROR';
22
+
23
+ /**
24
+ * HTTP Error with request/response context
25
+ */
26
+ export declare class HttpError extends Error {
27
+ readonly name: 'HttpError';
28
+ readonly code: HttpErrorCode;
29
+ readonly config: HttpConfig | null;
30
+ readonly request: Request | null;
31
+ readonly response: HttpResponse<unknown> | null;
32
+ readonly status: number | null;
33
+ readonly isHttpError: true;
34
+
35
+ constructor(message: string, options?: {
36
+ code?: HttpErrorCode;
37
+ config?: HttpConfig;
38
+ request?: Request;
39
+ response?: HttpResponse<unknown>;
40
+ context?: string;
41
+ suggestion?: string;
42
+ });
43
+
44
+ /**
45
+ * Check if an error is an HttpError
46
+ */
47
+ static isHttpError(error: unknown): error is HttpError;
48
+
49
+ /**
50
+ * Check if this is an abort/cancellation error
51
+ */
52
+ isAborted(): boolean;
53
+ }
54
+
55
+ // ============================================================================
56
+ // Configuration Types
57
+ // ============================================================================
58
+
59
+ /**
60
+ * HTTP response type
61
+ */
62
+ export type HttpResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData';
63
+
64
+ /**
65
+ * HTTP request method
66
+ */
67
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
68
+
69
+ /**
70
+ * HTTP client configuration
71
+ */
72
+ export interface HttpConfig {
73
+ /** Base URL prepended to relative request URLs */
74
+ baseURL?: string;
75
+ /** Request timeout in ms (default: 10000) */
76
+ timeout?: number;
77
+ /** Default request headers */
78
+ headers?: Record<string, string>;
79
+ /** Include credentials/cookies (default: false) */
80
+ withCredentials?: boolean;
81
+ /** Response parsing type (default: 'json') */
82
+ responseType?: HttpResponseType;
83
+ /** Function to validate response status (default: status >= 200 && status < 300) */
84
+ validateStatus?: (status: number) => boolean;
85
+ /** Number of retry attempts on failure (default: 0) */
86
+ retries?: number;
87
+ /** Delay between retries in ms (default: 1000) */
88
+ retryDelay?: number;
89
+ /** Custom retry condition function */
90
+ retryCondition?: ((error: HttpError) => boolean) | null;
91
+ /** URL query parameters */
92
+ params?: Record<string, string | number | boolean | null | undefined>;
93
+ /** Request body data */
94
+ data?: unknown;
95
+ /** Request URL */
96
+ url?: string;
97
+ /** HTTP method */
98
+ method?: HttpMethod | string;
99
+ /** AbortController signal for request cancellation */
100
+ signal?: AbortSignal;
101
+ }
102
+
103
+ // ============================================================================
104
+ // Response Types
105
+ // ============================================================================
106
+
107
+ /**
108
+ * HTTP response object
109
+ */
110
+ export interface HttpResponse<T = unknown> {
111
+ /** Parsed response body */
112
+ data: T;
113
+ /** HTTP status code */
114
+ status: number;
115
+ /** HTTP status text */
116
+ statusText: string;
117
+ /** Response headers */
118
+ headers: Headers;
119
+ /** Request configuration used */
120
+ config: HttpConfig;
121
+ }
122
+
123
+ // ============================================================================
124
+ // Interceptor Types
125
+ // ============================================================================
126
+
127
+ /**
128
+ * Request/response interceptor handler
129
+ */
130
+ export interface InterceptorHandler<T> {
131
+ fulfilled: ((value: T) => T | Promise<T>) | null;
132
+ rejected?: ((error: unknown) => unknown) | null;
133
+ }
134
+
135
+ /**
136
+ * Interceptor manager for request/response pipelines
137
+ */
138
+ export declare class InterceptorManager<T = unknown> {
139
+ /**
140
+ * Add an interceptor
141
+ * @param fulfilled Success handler
142
+ * @param rejected Error handler
143
+ * @returns Interceptor ID for removal
144
+ */
145
+ use(
146
+ fulfilled: (value: T) => T | Promise<T>,
147
+ rejected?: (error: unknown) => unknown
148
+ ): number;
149
+
150
+ /**
151
+ * Remove an interceptor by ID
152
+ */
153
+ eject(id: number): boolean;
154
+
155
+ /**
156
+ * Remove all interceptors
157
+ */
158
+ clear(): void;
159
+
160
+ /**
161
+ * Number of registered interceptors
162
+ */
163
+ readonly size: number;
164
+
165
+ /**
166
+ * Whether manager has no interceptors
167
+ */
168
+ readonly isEmpty: boolean;
169
+
170
+ /**
171
+ * Get all handler IDs
172
+ */
173
+ readonly ids: number[];
174
+
175
+ /**
176
+ * Run a value through all interceptors (async pipeline)
177
+ */
178
+ run(value: T): Promise<T>;
179
+
180
+ /**
181
+ * Run a value through all interceptors (sync pipeline)
182
+ */
183
+ runSync(value: T): T;
184
+
185
+ /**
186
+ * Get handlers as array
187
+ */
188
+ toArray(): InterceptorHandler<T>[];
189
+
190
+ [Symbol.iterator](): IterableIterator<InterceptorHandler<T>>;
191
+ }
192
+
193
+ // ============================================================================
194
+ // HTTP Client
195
+ // ============================================================================
196
+
197
+ /**
198
+ * HTTP client instance with interceptors and convenience methods
199
+ */
200
+ export declare class HttpClient {
201
+ /** Default configuration */
202
+ readonly defaults: HttpConfig;
203
+
204
+ /** Request and response interceptors */
205
+ readonly interceptors: {
206
+ request: InterceptorManager<HttpConfig>;
207
+ response: InterceptorManager<HttpResponse>;
208
+ };
209
+
210
+ constructor(config?: HttpConfig);
211
+
212
+ /**
213
+ * Make an HTTP request
214
+ * @param config Request configuration
215
+ * @returns HTTP response
216
+ */
217
+ request<T = unknown>(config: HttpConfig): Promise<HttpResponse<T>>;
218
+
219
+ /**
220
+ * GET request
221
+ * @param url Request URL
222
+ * @param options Additional request options
223
+ */
224
+ get<T = unknown>(url: string, options?: Omit<HttpConfig, 'url' | 'method'>): Promise<HttpResponse<T>>;
225
+
226
+ /**
227
+ * DELETE request
228
+ * @param url Request URL
229
+ * @param options Additional request options
230
+ */
231
+ delete<T = unknown>(url: string, options?: Omit<HttpConfig, 'url' | 'method'>): Promise<HttpResponse<T>>;
232
+
233
+ /**
234
+ * HEAD request
235
+ * @param url Request URL
236
+ * @param options Additional request options
237
+ */
238
+ head<T = unknown>(url: string, options?: Omit<HttpConfig, 'url' | 'method'>): Promise<HttpResponse<T>>;
239
+
240
+ /**
241
+ * OPTIONS request
242
+ * @param url Request URL
243
+ * @param options Additional request options
244
+ */
245
+ options<T = unknown>(url: string, options?: Omit<HttpConfig, 'url' | 'method'>): Promise<HttpResponse<T>>;
246
+
247
+ /**
248
+ * POST request
249
+ * @param url Request URL
250
+ * @param data Request body
251
+ * @param options Additional request options
252
+ */
253
+ post<T = unknown>(url: string, data?: unknown, options?: Omit<HttpConfig, 'url' | 'method' | 'data'>): Promise<HttpResponse<T>>;
254
+
255
+ /**
256
+ * PUT request
257
+ * @param url Request URL
258
+ * @param data Request body
259
+ * @param options Additional request options
260
+ */
261
+ put<T = unknown>(url: string, data?: unknown, options?: Omit<HttpConfig, 'url' | 'method' | 'data'>): Promise<HttpResponse<T>>;
262
+
263
+ /**
264
+ * PATCH request
265
+ * @param url Request URL
266
+ * @param data Request body
267
+ * @param options Additional request options
268
+ */
269
+ patch<T = unknown>(url: string, data?: unknown, options?: Omit<HttpConfig, 'url' | 'method' | 'data'>): Promise<HttpResponse<T>>;
270
+
271
+ /**
272
+ * Create a new HttpClient instance with merged configuration
273
+ * @param config Configuration to merge with current defaults
274
+ * @returns New client instance
275
+ */
276
+ create(config?: HttpConfig): HttpClient;
277
+
278
+ /**
279
+ * Get the full URI for a request configuration
280
+ * @param config Request configuration
281
+ * @returns Full URL string
282
+ */
283
+ getUri(config: HttpConfig): string;
284
+
285
+ /**
286
+ * Check if an error was caused by request cancellation
287
+ * @param error The error to check
288
+ * @returns True if the error is a cancellation
289
+ */
290
+ isCancel(error: unknown): boolean;
291
+ }
292
+
293
+ // ============================================================================
294
+ // useHttp Hook Types
295
+ // ============================================================================
296
+
297
+ /**
298
+ * useHttp hook options
299
+ */
300
+ export interface UseHttpOptions<T> {
301
+ /** Execute immediately on creation (default: true) */
302
+ immediate?: boolean;
303
+ /** Initial data value (default: null) */
304
+ initialData?: T | null;
305
+ /** Number of retry attempts (default: 0) */
306
+ retries?: number;
307
+ /** Delay between retries in ms (default: 1000) */
308
+ retryDelay?: number;
309
+ /** Callback invoked on success */
310
+ onSuccess?: (response: HttpResponse<T>) => void;
311
+ /** Callback invoked on error */
312
+ onError?: (error: HttpError) => void;
313
+ }
314
+
315
+ /**
316
+ * useHttp hook return type
317
+ */
318
+ export interface UseHttpReturn<T> {
319
+ /** Reactive response data (extracted from response.data) */
320
+ data: Pulse<T | null>;
321
+ /** Reactive full response object */
322
+ response: Pulse<HttpResponse<T> | null>;
323
+ /** Reactive loading state */
324
+ loading: Pulse<boolean>;
325
+ /** Reactive error state */
326
+ error: Pulse<HttpError | Error | null>;
327
+ /** Reactive status */
328
+ status: Pulse<AsyncStatus>;
329
+ /** Execute the request */
330
+ execute(...args: unknown[]): Promise<HttpResponse<T> | null>;
331
+ /** Reset state to initial values */
332
+ reset(): void;
333
+ /** Abort current request */
334
+ abort(): void;
335
+ }
336
+
337
+ // ============================================================================
338
+ // Factory Functions
339
+ // ============================================================================
340
+
341
+ /**
342
+ * Create a new HTTP client instance.
343
+ *
344
+ * @param config Default configuration for all requests
345
+ * @returns HTTP client instance
346
+ *
347
+ * @example
348
+ * const api = createHttp({
349
+ * baseURL: 'https://api.example.com',
350
+ * timeout: 5000,
351
+ * headers: { 'Authorization': 'Bearer token' }
352
+ * });
353
+ *
354
+ * const users = await api.get('/users');
355
+ * const user = await api.post('/users', { name: 'John' });
356
+ */
357
+ export declare function createHttp(config?: HttpConfig): HttpClient;
358
+
359
+ /**
360
+ * Reactive HTTP hook integrating with Pulse's useAsync.
361
+ *
362
+ * @param requestFn Function that returns a promise (from http.get, etc.)
363
+ * @param options Hook options
364
+ * @returns Reactive state and controls
365
+ *
366
+ * @example
367
+ * const { data, loading, error, execute } = useHttp(
368
+ * () => api.get('/users'),
369
+ * { immediate: true, retries: 3 }
370
+ * );
371
+ *
372
+ * effect(() => {
373
+ * if (data.get()) console.log('Users:', data.get());
374
+ * });
375
+ */
376
+ export declare function useHttp<T = unknown>(
377
+ requestFn: (...args: unknown[]) => Promise<HttpResponse<T>>,
378
+ options?: UseHttpOptions<T>
379
+ ): UseHttpReturn<T>;
380
+
381
+ /**
382
+ * HTTP resource with caching (SWR pattern).
383
+ * Integrates with useResource from async.js.
384
+ *
385
+ * @param key Cache key or function returning key
386
+ * @param requestFn Function that returns an HTTP response promise
387
+ * @param options Resource options
388
+ * @returns Resource state and controls
389
+ *
390
+ * @example
391
+ * const users = useHttpResource(
392
+ * 'users',
393
+ * () => api.get('/users'),
394
+ * { refreshInterval: 30000, refreshOnFocus: true }
395
+ * );
396
+ */
397
+ export declare function useHttpResource<T = unknown>(
398
+ key: string | (() => string),
399
+ requestFn: () => Promise<HttpResponse<T>>,
400
+ options?: ResourceOptions<T>
401
+ ): UseResourceReturn<T>;
402
+
403
+ // ============================================================================
404
+ // Default Instance
405
+ // ============================================================================
406
+
407
+ /**
408
+ * Pre-configured default HTTP client instance
409
+ */
410
+ export declare const http: HttpClient;
411
+
412
+ // ============================================================================
413
+ // Default Export
414
+ // ============================================================================
415
+
416
+ declare const _default: {
417
+ createHttp: typeof createHttp;
418
+ http: typeof http;
419
+ HttpClient: typeof HttpClient;
420
+ HttpError: typeof HttpError;
421
+ InterceptorManager: typeof InterceptorManager;
422
+ useHttp: typeof useHttp;
423
+ useHttpResource: typeof useHttpResource;
424
+ };
425
+
426
+ export default _default;
package/types/logger.d.ts CHANGED
@@ -93,6 +93,18 @@ export declare function getLogLevel(): LogLevelValue;
93
93
  */
94
94
  export declare function setFormatter(formatter: LogFormatter | null): void;
95
95
 
96
+ /**
97
+ * Configure logger options (production mode, etc.)
98
+ */
99
+ export declare function configureLogger(options?: {
100
+ production?: boolean;
101
+ }): void;
102
+
103
+ /**
104
+ * Check if logger is in production (noop) mode
105
+ */
106
+ export declare function isProductionMode(): boolean;
107
+
96
108
  /**
97
109
  * Create a logger instance with optional namespace
98
110
  */