@djangocfg/ext-knowbase 1.0.0

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.
Files changed (104) hide show
  1. package/README.md +177 -0
  2. package/dist/chunk-GNRC54ON.js +4308 -0
  3. package/dist/hooks.cjs +6003 -0
  4. package/dist/hooks.d.cts +610 -0
  5. package/dist/hooks.d.ts +610 -0
  6. package/dist/hooks.js +1486 -0
  7. package/dist/index.cjs +4450 -0
  8. package/dist/index.d.cts +4398 -0
  9. package/dist/index.d.ts +4398 -0
  10. package/dist/index.js +1 -0
  11. package/package.json +81 -0
  12. package/src/api/generated/ext_knowbase/_utils/fetchers/ext_knowbase__knowbase.ts +2983 -0
  13. package/src/api/generated/ext_knowbase/_utils/fetchers/index.ts +28 -0
  14. package/src/api/generated/ext_knowbase/_utils/hooks/ext_knowbase__knowbase.ts +999 -0
  15. package/src/api/generated/ext_knowbase/_utils/hooks/index.ts +28 -0
  16. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItem.schema.ts +33 -0
  17. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemChunk.schema.ts +29 -0
  18. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemChunkDetail.schema.ts +30 -0
  19. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemChunkRequest.schema.ts +22 -0
  20. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemDetail.schema.ts +35 -0
  21. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemRequest.schema.ts +22 -0
  22. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveProcessingResult.schema.ts +26 -0
  23. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveSearchRequestRequest.schema.ts +25 -0
  24. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveSearchResult.schema.ts +24 -0
  25. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveStatistics.schema.ts +28 -0
  26. package/src/api/generated/ext_knowbase/_utils/schemas/ChatHistory.schema.ts +22 -0
  27. package/src/api/generated/ext_knowbase/_utils/schemas/ChatMessage.schema.ts +27 -0
  28. package/src/api/generated/ext_knowbase/_utils/schemas/ChatQueryRequest.schema.ts +22 -0
  29. package/src/api/generated/ext_knowbase/_utils/schemas/ChatResponse.schema.ts +26 -0
  30. package/src/api/generated/ext_knowbase/_utils/schemas/ChatResponseRequest.schema.ts +26 -0
  31. package/src/api/generated/ext_knowbase/_utils/schemas/ChatSession.schema.ts +29 -0
  32. package/src/api/generated/ext_knowbase/_utils/schemas/ChatSessionCreateRequest.schema.ts +22 -0
  33. package/src/api/generated/ext_knowbase/_utils/schemas/ChatSessionRequest.schema.ts +25 -0
  34. package/src/api/generated/ext_knowbase/_utils/schemas/ChatSource.schema.ts +21 -0
  35. package/src/api/generated/ext_knowbase/_utils/schemas/ChatSourceRequest.schema.ts +21 -0
  36. package/src/api/generated/ext_knowbase/_utils/schemas/ChunkRevectorizationRequestRequest.schema.ts +20 -0
  37. package/src/api/generated/ext_knowbase/_utils/schemas/Document.schema.ts +32 -0
  38. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchive.schema.ts +44 -0
  39. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveDetail.schema.ts +48 -0
  40. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveList.schema.ts +35 -0
  41. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveRequest.schema.ts +21 -0
  42. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentCategory.schema.ts +23 -0
  43. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentCategoryRequest.schema.ts +21 -0
  44. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentCreateRequest.schema.ts +22 -0
  45. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentProcessingStatus.schema.ts +23 -0
  46. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentRequest.schema.ts +22 -0
  47. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentStats.schema.ts +25 -0
  48. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedArchiveItemChunkList.schema.ts +24 -0
  49. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedArchiveItemList.schema.ts +24 -0
  50. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedArchiveSearchResultList.schema.ts +24 -0
  51. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedChatResponseList.schema.ts +24 -0
  52. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedChatSessionList.schema.ts +24 -0
  53. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedDocumentArchiveListList.schema.ts +24 -0
  54. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedDocumentList.schema.ts +24 -0
  55. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedPublicCategoryList.schema.ts +24 -0
  56. package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedPublicDocumentListList.schema.ts +24 -0
  57. package/src/api/generated/ext_knowbase/_utils/schemas/PatchedArchiveItemChunkRequest.schema.ts +22 -0
  58. package/src/api/generated/ext_knowbase/_utils/schemas/PatchedArchiveItemRequest.schema.ts +22 -0
  59. package/src/api/generated/ext_knowbase/_utils/schemas/PatchedChatResponseRequest.schema.ts +26 -0
  60. package/src/api/generated/ext_knowbase/_utils/schemas/PatchedChatSessionRequest.schema.ts +25 -0
  61. package/src/api/generated/ext_knowbase/_utils/schemas/PatchedDocumentArchiveRequest.schema.ts +21 -0
  62. package/src/api/generated/ext_knowbase/_utils/schemas/PatchedDocumentRequest.schema.ts +22 -0
  63. package/src/api/generated/ext_knowbase/_utils/schemas/PublicCategory.schema.ts +21 -0
  64. package/src/api/generated/ext_knowbase/_utils/schemas/PublicDocument.schema.ts +25 -0
  65. package/src/api/generated/ext_knowbase/_utils/schemas/PublicDocumentList.schema.ts +24 -0
  66. package/src/api/generated/ext_knowbase/_utils/schemas/VectorizationResult.schema.ts +24 -0
  67. package/src/api/generated/ext_knowbase/_utils/schemas/VectorizationStatistics.schema.ts +26 -0
  68. package/src/api/generated/ext_knowbase/_utils/schemas/index.ts +70 -0
  69. package/src/api/generated/ext_knowbase/api-instance.ts +131 -0
  70. package/src/api/generated/ext_knowbase/client.ts +301 -0
  71. package/src/api/generated/ext_knowbase/enums.ts +241 -0
  72. package/src/api/generated/ext_knowbase/errors.ts +116 -0
  73. package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/client.ts +666 -0
  74. package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/index.ts +2 -0
  75. package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/models.ts +1120 -0
  76. package/src/api/generated/ext_knowbase/http.ts +103 -0
  77. package/src/api/generated/ext_knowbase/index.ts +273 -0
  78. package/src/api/generated/ext_knowbase/logger.ts +259 -0
  79. package/src/api/generated/ext_knowbase/retry.ts +175 -0
  80. package/src/api/generated/ext_knowbase/schema.json +5865 -0
  81. package/src/api/generated/ext_knowbase/storage.ts +161 -0
  82. package/src/api/generated/ext_knowbase/validation-events.ts +133 -0
  83. package/src/api/index.ts +9 -0
  84. package/src/components/Chat/ChatUIContext.tsx +110 -0
  85. package/src/components/Chat/ChatWidget.tsx +476 -0
  86. package/src/components/Chat/README.md +122 -0
  87. package/src/components/Chat/components/MessageInput.tsx +124 -0
  88. package/src/components/Chat/components/MessageList.tsx +169 -0
  89. package/src/components/Chat/components/SessionList.tsx +192 -0
  90. package/src/components/Chat/components/index.ts +9 -0
  91. package/src/components/Chat/hooks/index.ts +6 -0
  92. package/src/components/Chat/hooks/useInfiniteSessions.ts +81 -0
  93. package/src/components/Chat/index.tsx +45 -0
  94. package/src/components/Chat/types.ts +81 -0
  95. package/src/config.ts +20 -0
  96. package/src/contexts/knowbase/ChatContext.tsx +173 -0
  97. package/src/contexts/knowbase/DocumentsContext.tsx +306 -0
  98. package/src/contexts/knowbase/KnowbaseProvider.tsx +47 -0
  99. package/src/contexts/knowbase/SessionsContext.tsx +175 -0
  100. package/src/contexts/knowbase/index.ts +63 -0
  101. package/src/contexts/knowbase/types.ts +69 -0
  102. package/src/hooks/index.ts +28 -0
  103. package/src/index.ts +22 -0
  104. package/src/utils/logger.ts +10 -0
@@ -0,0 +1,103 @@
1
+ /**
2
+ * HTTP Client Adapter Pattern
3
+ *
4
+ * Allows switching between fetch/axios/httpx without changing generated code.
5
+ * Provides unified interface for making HTTP requests.
6
+ */
7
+
8
+ export interface HttpRequest {
9
+ method: string;
10
+ url: string;
11
+ headers?: Record<string, string>;
12
+ body?: any;
13
+ params?: Record<string, any>;
14
+ /** FormData for file uploads (multipart/form-data) */
15
+ formData?: FormData;
16
+ }
17
+
18
+ export interface HttpResponse<T = any> {
19
+ data: T;
20
+ status: number;
21
+ statusText: string;
22
+ headers: Record<string, string>;
23
+ }
24
+
25
+ /**
26
+ * HTTP Client Adapter Interface.
27
+ * Implement this to use custom HTTP clients (axios, httpx, etc.)
28
+ */
29
+ export interface HttpClientAdapter {
30
+ request<T = any>(request: HttpRequest): Promise<HttpResponse<T>>;
31
+ }
32
+
33
+ /**
34
+ * Default Fetch API adapter.
35
+ * Uses native browser fetch() with proper error handling.
36
+ */
37
+ export class FetchAdapter implements HttpClientAdapter {
38
+ async request<T = any>(request: HttpRequest): Promise<HttpResponse<T>> {
39
+ const { method, url, headers, body, params, formData } = request;
40
+
41
+ // Build URL with query params
42
+ let finalUrl = url;
43
+ if (params) {
44
+ const searchParams = new URLSearchParams();
45
+ Object.entries(params).forEach(([key, value]) => {
46
+ if (value !== null && value !== undefined) {
47
+ searchParams.append(key, String(value));
48
+ }
49
+ });
50
+ const queryString = searchParams.toString();
51
+ if (queryString) {
52
+ finalUrl = url.includes('?') ? `${url}&${queryString}` : `${url}?${queryString}`;
53
+ }
54
+ }
55
+
56
+ // Build headers
57
+ const finalHeaders: Record<string, string> = { ...headers };
58
+
59
+ // Determine body and content-type
60
+ let requestBody: string | FormData | undefined;
61
+
62
+ if (formData) {
63
+ // For multipart/form-data, let browser set Content-Type with boundary
64
+ requestBody = formData;
65
+ // Don't set Content-Type - browser will set it with boundary
66
+ } else if (body) {
67
+ // JSON request
68
+ finalHeaders['Content-Type'] = 'application/json';
69
+ requestBody = JSON.stringify(body);
70
+ }
71
+
72
+ // Make request
73
+ const response = await fetch(finalUrl, {
74
+ method,
75
+ headers: finalHeaders,
76
+ body: requestBody,
77
+ credentials: 'include', // Include Django session cookies
78
+ });
79
+
80
+ // Parse response
81
+ let data: any = null;
82
+ const contentType = response.headers.get('content-type');
83
+
84
+ if (response.status !== 204 && contentType?.includes('application/json')) {
85
+ data = await response.json();
86
+ } else if (response.status !== 204) {
87
+ data = await response.text();
88
+ }
89
+
90
+ // Convert Headers to plain object
91
+ const responseHeaders: Record<string, string> = {};
92
+ response.headers.forEach((value, key) => {
93
+ responseHeaders[key] = value;
94
+ });
95
+
96
+ return {
97
+ data,
98
+ status: response.status,
99
+ statusText: response.statusText,
100
+ headers: responseHeaders,
101
+ };
102
+ }
103
+ }
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Django CFG API - API Client with JWT Management
3
+ *
4
+ * Usage:
5
+ * ```typescript
6
+ * import { API } from './api';
7
+ *
8
+ * const api = new API('https://api.example.com');
9
+ *
10
+ * // Set JWT token
11
+ * api.setToken('your-jwt-token', 'refresh-token');
12
+ *
13
+ * // Use API
14
+ * const posts = await api.posts.list();
15
+ * const user = await api.users.retrieve(1);
16
+ *
17
+ * // Check authentication
18
+ * if (api.isAuthenticated()) {
19
+ * // ...
20
+ * }
21
+ *
22
+ * // Custom storage with logging (for Electron/Node.js)
23
+ * import { MemoryStorageAdapter, APILogger } from './storage';
24
+ * const logger = new APILogger({ enabled: true, logLevel: 'debug' });
25
+ * const api = new API('https://api.example.com', {
26
+ * storage: new MemoryStorageAdapter(logger),
27
+ * loggerConfig: { enabled: true, logLevel: 'debug' }
28
+ * });
29
+ *
30
+ * // Get OpenAPI schema
31
+ * const schema = api.getSchema();
32
+ * ```
33
+ */
34
+
35
+ import { APIClient } from "./client";
36
+ import {
37
+ StorageAdapter,
38
+ LocalStorageAdapter,
39
+ CookieStorageAdapter,
40
+ MemoryStorageAdapter
41
+ } from "./storage";
42
+ import type { RetryConfig } from "./retry";
43
+ import type { LoggerConfig } from "./logger";
44
+ import { APILogger } from "./logger";
45
+ import { ExtKnowbaseKnowbase } from "./ext_knowbase__knowbase/client";
46
+ export * as ExtKnowbaseKnowbaseTypes from "./ext_knowbase__knowbase/models";
47
+ // Note: Direct exports (export * from) are removed to avoid duplicate type conflicts
48
+ // Use namespace exports like CfgAccountsTypes.User or import from specific modules
49
+ export * as Enums from "./enums";
50
+
51
+ // Re-export Zod schemas for runtime validation
52
+ export * as Schemas from "./_utils/schemas";
53
+ // Also export all schemas directly for convenience
54
+ export * from "./_utils/schemas";
55
+
56
+ // Re-export Zod validation events for browser integration
57
+ export type { ValidationErrorDetail, ValidationErrorEvent } from "./validation-events";
58
+ export { dispatchValidationError, onValidationError, formatZodError } from "./validation-events";
59
+
60
+ // Re-export typed fetchers for universal usage
61
+ export * as Fetchers from "./_utils/fetchers";
62
+ export * from "./_utils/fetchers";
63
+
64
+ // Re-export API instance configuration functions
65
+ export {
66
+ configureAPI,
67
+ getAPIInstance,
68
+ reconfigureAPI,
69
+ clearAPITokens,
70
+ resetAPI,
71
+ isAPIConfigured
72
+ } from "./api-instance";
73
+ // NOTE: SWR hooks are generated in ./_utils/hooks/ but NOT exported here to keep
74
+ // the main bundle server-safe. Import hooks directly from the hooks directory:
75
+ // import { useUsers } from './_utils/hooks';
76
+ // Or use a separate entry point like '@djangocfg/api/hooks' for client components.
77
+
78
+ // Re-export core client
79
+ export { APIClient };
80
+
81
+ // Re-export storage adapters for convenience
82
+ export type { StorageAdapter };
83
+ export { LocalStorageAdapter, CookieStorageAdapter, MemoryStorageAdapter };
84
+
85
+ // Re-export error classes for convenience
86
+ export { APIError, NetworkError } from "./errors";
87
+
88
+ // Re-export HTTP adapters for custom implementations
89
+ export type { HttpClientAdapter, HttpRequest, HttpResponse } from "./http";
90
+ export { FetchAdapter } from "./http";
91
+
92
+ // Re-export logger types and classes
93
+ export type { LoggerConfig, RequestLog, ResponseLog, ErrorLog } from "./logger";
94
+ export { APILogger } from "./logger";
95
+
96
+ // Re-export retry configuration and utilities
97
+ export type { RetryConfig, FailedAttemptInfo } from "./retry";
98
+ export { withRetry, shouldRetry, DEFAULT_RETRY_CONFIG } from "./retry";
99
+
100
+ export const TOKEN_KEY = "auth_token";
101
+ export const REFRESH_TOKEN_KEY = "refresh_token";
102
+
103
+ export interface APIOptions {
104
+ /** Custom storage adapter (defaults to LocalStorageAdapter) */
105
+ storage?: StorageAdapter;
106
+ /** Retry configuration for failed requests */
107
+ retryConfig?: RetryConfig;
108
+ /** Logger configuration */
109
+ loggerConfig?: Partial<LoggerConfig>;
110
+ }
111
+
112
+ export class API {
113
+ private baseUrl: string;
114
+ private _client: APIClient;
115
+ private _token: string | null = null;
116
+ private _refreshToken: string | null = null;
117
+ private storage: StorageAdapter;
118
+ private options?: APIOptions;
119
+
120
+ // Sub-clients
121
+ public ext_knowbase_knowbase!: ExtKnowbaseKnowbase;
122
+
123
+ constructor(baseUrl: string, options?: APIOptions) {
124
+ this.baseUrl = baseUrl;
125
+ this.options = options;
126
+
127
+ // Create logger if config provided
128
+ const logger = options?.loggerConfig ? new APILogger(options.loggerConfig) : undefined;
129
+
130
+ // Initialize storage with logger
131
+ this.storage = options?.storage || new LocalStorageAdapter(logger);
132
+
133
+ this._loadTokensFromStorage();
134
+
135
+ // Initialize APIClient
136
+ this._client = new APIClient(this.baseUrl, {
137
+ retryConfig: this.options?.retryConfig,
138
+ loggerConfig: this.options?.loggerConfig,
139
+ });
140
+
141
+ // Always inject auth header wrapper (reads token dynamically from storage)
142
+ this._injectAuthHeader();
143
+
144
+ // Initialize sub-clients from APIClient
145
+ this.ext_knowbase_knowbase = this._client.ext_knowbase_knowbase;
146
+ }
147
+
148
+ private _loadTokensFromStorage(): void {
149
+ this._token = this.storage.getItem(TOKEN_KEY);
150
+ this._refreshToken = this.storage.getItem(REFRESH_TOKEN_KEY);
151
+ }
152
+
153
+ private _reinitClients(): void {
154
+ this._client = new APIClient(this.baseUrl, {
155
+ retryConfig: this.options?.retryConfig,
156
+ loggerConfig: this.options?.loggerConfig,
157
+ });
158
+
159
+ // Always inject auth header wrapper (reads token dynamically from storage)
160
+ this._injectAuthHeader();
161
+
162
+ // Reinitialize sub-clients
163
+ this.ext_knowbase_knowbase = this._client.ext_knowbase_knowbase;
164
+ }
165
+
166
+ private _injectAuthHeader(): void {
167
+ // Override request method to inject auth header
168
+ const originalRequest = this._client.request.bind(this._client);
169
+ this._client.request = async <T>(
170
+ method: string,
171
+ path: string,
172
+ options?: { params?: Record<string, any>; body?: any; formData?: FormData; headers?: Record<string, string> }
173
+ ): Promise<T> => {
174
+ // Read token from storage dynamically (supports JWT injection after instantiation)
175
+ const token = this.getToken();
176
+ const mergedOptions = {
177
+ ...options,
178
+ headers: {
179
+ ...(options?.headers || {}),
180
+ ...(token ? { 'Authorization': `Bearer ${token}` } : {}),
181
+ },
182
+ };
183
+
184
+ return originalRequest(method, path, mergedOptions);
185
+ };
186
+ }
187
+
188
+ /**
189
+ * Get current JWT token
190
+ */
191
+ getToken(): string | null {
192
+ return this.storage.getItem(TOKEN_KEY);
193
+ }
194
+
195
+ /**
196
+ * Get current refresh token
197
+ */
198
+ getRefreshToken(): string | null {
199
+ return this.storage.getItem(REFRESH_TOKEN_KEY);
200
+ }
201
+
202
+ /**
203
+ * Set JWT token and refresh token
204
+ * @param token - JWT access token
205
+ * @param refreshToken - JWT refresh token (optional)
206
+ */
207
+ setToken(token: string, refreshToken?: string): void {
208
+ this._token = token;
209
+ this.storage.setItem(TOKEN_KEY, token);
210
+
211
+ if (refreshToken) {
212
+ this._refreshToken = refreshToken;
213
+ this.storage.setItem(REFRESH_TOKEN_KEY, refreshToken);
214
+ }
215
+
216
+ // Reinitialize clients with new token
217
+ this._reinitClients();
218
+ }
219
+
220
+ /**
221
+ * Clear all tokens
222
+ */
223
+ clearTokens(): void {
224
+ this._token = null;
225
+ this._refreshToken = null;
226
+ this.storage.removeItem(TOKEN_KEY);
227
+ this.storage.removeItem(REFRESH_TOKEN_KEY);
228
+
229
+ // Reinitialize clients without token
230
+ this._reinitClients();
231
+ }
232
+
233
+ /**
234
+ * Check if user is authenticated
235
+ */
236
+ isAuthenticated(): boolean {
237
+ return !!this.getToken();
238
+ }
239
+
240
+ /**
241
+ * Update base URL and reinitialize clients
242
+ * @param url - New base URL
243
+ */
244
+ setBaseUrl(url: string): void {
245
+ this.baseUrl = url;
246
+ this._reinitClients();
247
+ }
248
+
249
+ /**
250
+ * Get current base URL
251
+ */
252
+ getBaseUrl(): string {
253
+ return this.baseUrl;
254
+ }
255
+
256
+ /**
257
+ * Get OpenAPI schema path
258
+ * @returns Path to the OpenAPI schema JSON file
259
+ *
260
+ * Note: The OpenAPI schema is available in the schema.json file.
261
+ * You can load it dynamically using:
262
+ * ```typescript
263
+ * const schema = await fetch('./schema.json').then(r => r.json());
264
+ * // or using fs in Node.js:
265
+ * // const schema = JSON.parse(fs.readFileSync('./schema.json', 'utf-8'));
266
+ * ```
267
+ */
268
+ getSchemaPath(): string {
269
+ return './schema.json';
270
+ }
271
+ }
272
+
273
+ export default API;
@@ -0,0 +1,259 @@
1
+ /**
2
+ * API Logger with Consola
3
+ * Beautiful console logging for API requests and responses
4
+ *
5
+ * Installation:
6
+ * npm install consola
7
+ */
8
+
9
+ import { type ConsolaInstance, createConsola } from 'consola';
10
+
11
+ /**
12
+ * Request log data
13
+ */
14
+ export interface RequestLog {
15
+ method: string;
16
+ url: string;
17
+ headers?: Record<string, string>;
18
+ body?: any;
19
+ timestamp: number;
20
+ }
21
+
22
+ /**
23
+ * Response log data
24
+ */
25
+ export interface ResponseLog {
26
+ status: number;
27
+ statusText: string;
28
+ data?: any;
29
+ duration: number;
30
+ timestamp: number;
31
+ }
32
+
33
+ /**
34
+ * Error log data
35
+ */
36
+ export interface ErrorLog {
37
+ message: string;
38
+ statusCode?: number;
39
+ fieldErrors?: Record<string, string[]>;
40
+ duration: number;
41
+ timestamp: number;
42
+ }
43
+
44
+ /**
45
+ * Logger configuration
46
+ */
47
+ export interface LoggerConfig {
48
+ /** Enable logging */
49
+ enabled: boolean;
50
+ /** Log requests */
51
+ logRequests: boolean;
52
+ /** Log responses */
53
+ logResponses: boolean;
54
+ /** Log errors */
55
+ logErrors: boolean;
56
+ /** Log request/response bodies */
57
+ logBodies: boolean;
58
+ /** Log headers (excluding sensitive ones) */
59
+ logHeaders: boolean;
60
+ /** Custom consola instance */
61
+ consola?: ConsolaInstance;
62
+ }
63
+
64
+ /**
65
+ * Default logger configuration
66
+ */
67
+ const DEFAULT_CONFIG: LoggerConfig = {
68
+ enabled: process.env.NODE_ENV !== 'production',
69
+ logRequests: true,
70
+ logResponses: true,
71
+ logErrors: true,
72
+ logBodies: true,
73
+ logHeaders: false,
74
+ };
75
+
76
+ /**
77
+ * Sensitive header names to filter out
78
+ */
79
+ const SENSITIVE_HEADERS = [
80
+ 'authorization',
81
+ 'cookie',
82
+ 'set-cookie',
83
+ 'x-api-key',
84
+ 'x-csrf-token',
85
+ ];
86
+
87
+ /**
88
+ * API Logger class
89
+ */
90
+ export class APILogger {
91
+ private config: LoggerConfig;
92
+ private consola: ConsolaInstance;
93
+
94
+ constructor(config: Partial<LoggerConfig> = {}) {
95
+ this.config = { ...DEFAULT_CONFIG, ...config };
96
+ this.consola = config.consola || createConsola({
97
+ level: this.config.enabled ? 4 : 0,
98
+ });
99
+ }
100
+
101
+ /**
102
+ * Enable logging
103
+ */
104
+ enable(): void {
105
+ this.config.enabled = true;
106
+ }
107
+
108
+ /**
109
+ * Disable logging
110
+ */
111
+ disable(): void {
112
+ this.config.enabled = false;
113
+ }
114
+
115
+ /**
116
+ * Update configuration
117
+ */
118
+ setConfig(config: Partial<LoggerConfig>): void {
119
+ this.config = { ...this.config, ...config };
120
+ }
121
+
122
+ /**
123
+ * Filter sensitive headers
124
+ */
125
+ private filterHeaders(headers?: Record<string, string>): Record<string, string> {
126
+ if (!headers) return {};
127
+
128
+ const filtered: Record<string, string> = {};
129
+ Object.keys(headers).forEach((key) => {
130
+ const lowerKey = key.toLowerCase();
131
+ if (SENSITIVE_HEADERS.includes(lowerKey)) {
132
+ filtered[key] = '***';
133
+ } else {
134
+ filtered[key] = headers[key] || '';
135
+ }
136
+ });
137
+
138
+ return filtered;
139
+ }
140
+
141
+ /**
142
+ * Log request
143
+ */
144
+ logRequest(request: RequestLog): void {
145
+ if (!this.config.enabled || !this.config.logRequests) return;
146
+
147
+ const { method, url, headers, body } = request;
148
+
149
+ this.consola.start(`${method} ${url}`);
150
+
151
+ if (this.config.logHeaders && headers) {
152
+ this.consola.debug('Headers:', this.filterHeaders(headers));
153
+ }
154
+
155
+ if (this.config.logBodies && body) {
156
+ this.consola.debug('Body:', body);
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Log response
162
+ */
163
+ logResponse(request: RequestLog, response: ResponseLog): void {
164
+ if (!this.config.enabled || !this.config.logResponses) return;
165
+
166
+ const { method, url } = request;
167
+ const { status, statusText, data, duration } = response;
168
+
169
+ const statusColor = status >= 500 ? 'red'
170
+ : status >= 400 ? 'yellow'
171
+ : status >= 300 ? 'cyan'
172
+ : 'green';
173
+
174
+ this.consola.success(
175
+ `${method} ${url} ${status} ${statusText} (${duration}ms)`
176
+ );
177
+
178
+ if (this.config.logBodies && data) {
179
+ this.consola.debug('Response:', data);
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Log error
185
+ */
186
+ logError(request: RequestLog, error: ErrorLog): void {
187
+ if (!this.config.enabled || !this.config.logErrors) return;
188
+
189
+ const { method, url } = request;
190
+ const { message, statusCode, fieldErrors, duration } = error;
191
+
192
+ this.consola.error(
193
+ `${method} ${url} ${statusCode || 'Network'} Error (${duration}ms)`
194
+ );
195
+
196
+ this.consola.error('Message:', message);
197
+
198
+ if (fieldErrors && Object.keys(fieldErrors).length > 0) {
199
+ this.consola.error('Field Errors:');
200
+ Object.entries(fieldErrors).forEach(([field, errors]) => {
201
+ errors.forEach((err) => {
202
+ this.consola.error(` • ${field}: ${err}`);
203
+ });
204
+ });
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Log general info
210
+ */
211
+ info(message: string, ...args: any[]): void {
212
+ if (!this.config.enabled) return;
213
+ this.consola.info(message, ...args);
214
+ }
215
+
216
+ /**
217
+ * Log warning
218
+ */
219
+ warn(message: string, ...args: any[]): void {
220
+ if (!this.config.enabled) return;
221
+ this.consola.warn(message, ...args);
222
+ }
223
+
224
+ /**
225
+ * Log error
226
+ */
227
+ error(message: string, ...args: any[]): void {
228
+ if (!this.config.enabled) return;
229
+ this.consola.error(message, ...args);
230
+ }
231
+
232
+ /**
233
+ * Log debug
234
+ */
235
+ debug(message: string, ...args: any[]): void {
236
+ if (!this.config.enabled) return;
237
+ this.consola.debug(message, ...args);
238
+ }
239
+
240
+ /**
241
+ * Log success
242
+ */
243
+ success(message: string, ...args: any[]): void {
244
+ if (!this.config.enabled) return;
245
+ this.consola.success(message, ...args);
246
+ }
247
+
248
+ /**
249
+ * Create a sub-logger with prefix
250
+ */
251
+ withTag(tag: string): ConsolaInstance {
252
+ return this.consola.withTag(tag);
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Default logger instance
258
+ */
259
+ export const defaultLogger = new APILogger();