@djangocfg/ext-support 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 (65) hide show
  1. package/README.md +233 -0
  2. package/dist/chunk-AZ4LWZB7.js +2630 -0
  3. package/dist/hooks.cjs +2716 -0
  4. package/dist/hooks.d.cts +255 -0
  5. package/dist/hooks.d.ts +255 -0
  6. package/dist/hooks.js +1 -0
  7. package/dist/index.cjs +2693 -0
  8. package/dist/index.d.cts +1392 -0
  9. package/dist/index.d.ts +1392 -0
  10. package/dist/index.js +1 -0
  11. package/package.json +80 -0
  12. package/src/api/generated/ext_support/_utils/fetchers/ext_support__support.ts +642 -0
  13. package/src/api/generated/ext_support/_utils/fetchers/index.ts +28 -0
  14. package/src/api/generated/ext_support/_utils/hooks/ext_support__support.ts +237 -0
  15. package/src/api/generated/ext_support/_utils/hooks/index.ts +28 -0
  16. package/src/api/generated/ext_support/_utils/schemas/Message.schema.ts +21 -0
  17. package/src/api/generated/ext_support/_utils/schemas/MessageCreate.schema.ts +15 -0
  18. package/src/api/generated/ext_support/_utils/schemas/MessageCreateRequest.schema.ts +15 -0
  19. package/src/api/generated/ext_support/_utils/schemas/MessageRequest.schema.ts +15 -0
  20. package/src/api/generated/ext_support/_utils/schemas/PaginatedMessageList.schema.ts +24 -0
  21. package/src/api/generated/ext_support/_utils/schemas/PaginatedTicketList.schema.ts +24 -0
  22. package/src/api/generated/ext_support/_utils/schemas/PatchedMessageRequest.schema.ts +15 -0
  23. package/src/api/generated/ext_support/_utils/schemas/PatchedTicketRequest.schema.ts +18 -0
  24. package/src/api/generated/ext_support/_utils/schemas/Sender.schema.ts +21 -0
  25. package/src/api/generated/ext_support/_utils/schemas/Ticket.schema.ts +21 -0
  26. package/src/api/generated/ext_support/_utils/schemas/TicketRequest.schema.ts +18 -0
  27. package/src/api/generated/ext_support/_utils/schemas/index.ts +29 -0
  28. package/src/api/generated/ext_support/api-instance.ts +131 -0
  29. package/src/api/generated/ext_support/client.ts +301 -0
  30. package/src/api/generated/ext_support/enums.ts +45 -0
  31. package/src/api/generated/ext_support/errors.ts +116 -0
  32. package/src/api/generated/ext_support/ext_support__support/client.ts +151 -0
  33. package/src/api/generated/ext_support/ext_support__support/index.ts +2 -0
  34. package/src/api/generated/ext_support/ext_support__support/models.ts +165 -0
  35. package/src/api/generated/ext_support/http.ts +103 -0
  36. package/src/api/generated/ext_support/index.ts +273 -0
  37. package/src/api/generated/ext_support/logger.ts +259 -0
  38. package/src/api/generated/ext_support/retry.ts +175 -0
  39. package/src/api/generated/ext_support/schema.json +1049 -0
  40. package/src/api/generated/ext_support/storage.ts +161 -0
  41. package/src/api/generated/ext_support/validation-events.ts +133 -0
  42. package/src/api/index.ts +9 -0
  43. package/src/config.ts +20 -0
  44. package/src/contexts/SupportContext.tsx +250 -0
  45. package/src/contexts/SupportExtensionProvider.tsx +38 -0
  46. package/src/contexts/types.ts +26 -0
  47. package/src/hooks/index.ts +33 -0
  48. package/src/index.ts +39 -0
  49. package/src/layouts/SupportLayout/README.md +91 -0
  50. package/src/layouts/SupportLayout/SupportLayout.tsx +179 -0
  51. package/src/layouts/SupportLayout/components/CreateTicketDialog.tsx +155 -0
  52. package/src/layouts/SupportLayout/components/MessageInput.tsx +92 -0
  53. package/src/layouts/SupportLayout/components/MessageList.tsx +312 -0
  54. package/src/layouts/SupportLayout/components/TicketCard.tsx +96 -0
  55. package/src/layouts/SupportLayout/components/TicketList.tsx +153 -0
  56. package/src/layouts/SupportLayout/components/index.ts +6 -0
  57. package/src/layouts/SupportLayout/context/SupportLayoutContext.tsx +258 -0
  58. package/src/layouts/SupportLayout/context/index.ts +2 -0
  59. package/src/layouts/SupportLayout/events.ts +33 -0
  60. package/src/layouts/SupportLayout/hooks/index.ts +2 -0
  61. package/src/layouts/SupportLayout/hooks/useInfiniteMessages.ts +115 -0
  62. package/src/layouts/SupportLayout/hooks/useInfiniteTickets.ts +88 -0
  63. package/src/layouts/SupportLayout/index.ts +6 -0
  64. package/src/layouts/SupportLayout/types.ts +21 -0
  65. package/src/utils/logger.ts +14 -0
@@ -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();
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Retry Configuration and Utilities
3
+ *
4
+ * Provides automatic retry logic for failed HTTP requests using p-retry.
5
+ * Retries only on network errors and server errors (5xx), not client errors (4xx).
6
+ */
7
+
8
+ import pRetry, { AbortError } from 'p-retry';
9
+ import { APIError, NetworkError } from './errors';
10
+
11
+ /**
12
+ * Information about a failed retry attempt.
13
+ */
14
+ export interface FailedAttemptInfo {
15
+ /** The error that caused the failure */
16
+ error: Error;
17
+ /** The attempt number (1-indexed) */
18
+ attemptNumber: number;
19
+ /** Number of retries left */
20
+ retriesLeft: number;
21
+ }
22
+
23
+ /**
24
+ * Retry configuration options.
25
+ *
26
+ * Uses exponential backoff with jitter by default to avoid thundering herd.
27
+ */
28
+ export interface RetryConfig {
29
+ /**
30
+ * Maximum number of retry attempts.
31
+ * @default 3
32
+ */
33
+ retries?: number;
34
+
35
+ /**
36
+ * Exponential backoff factor.
37
+ * @default 2
38
+ */
39
+ factor?: number;
40
+
41
+ /**
42
+ * Minimum wait time between retries (ms).
43
+ * @default 1000
44
+ */
45
+ minTimeout?: number;
46
+
47
+ /**
48
+ * Maximum wait time between retries (ms).
49
+ * @default 60000
50
+ */
51
+ maxTimeout?: number;
52
+
53
+ /**
54
+ * Add randomness to wait times (jitter).
55
+ * Helps avoid thundering herd problem.
56
+ * @default true
57
+ */
58
+ randomize?: boolean;
59
+
60
+ /**
61
+ * Callback called on each failed attempt.
62
+ */
63
+ onFailedAttempt?: (info: FailedAttemptInfo) => void;
64
+ }
65
+
66
+ /**
67
+ * Default retry configuration.
68
+ */
69
+ export const DEFAULT_RETRY_CONFIG: Required<RetryConfig> = {
70
+ retries: 3,
71
+ factor: 2,
72
+ minTimeout: 1000,
73
+ maxTimeout: 60000,
74
+ randomize: true,
75
+ onFailedAttempt: () => {},
76
+ };
77
+
78
+ /**
79
+ * Determine if an error should trigger a retry.
80
+ *
81
+ * Retries on:
82
+ * - Network errors (connection refused, timeout, etc.)
83
+ * - Server errors (5xx status codes)
84
+ * - Rate limiting (429 status code)
85
+ *
86
+ * Does NOT retry on:
87
+ * - Client errors (4xx except 429)
88
+ * - Authentication errors (401, 403)
89
+ * - Not found (404)
90
+ *
91
+ * @param error - The error to check
92
+ * @returns true if should retry, false otherwise
93
+ */
94
+ export function shouldRetry(error: any): boolean {
95
+ // Always retry network errors
96
+ if (error instanceof NetworkError) {
97
+ return true;
98
+ }
99
+
100
+ // For API errors, check status code
101
+ if (error instanceof APIError) {
102
+ const status = error.statusCode;
103
+
104
+ // Retry on 5xx server errors
105
+ if (status >= 500 && status < 600) {
106
+ return true;
107
+ }
108
+
109
+ // Retry on 429 (rate limit)
110
+ if (status === 429) {
111
+ return true;
112
+ }
113
+
114
+ // Do NOT retry on 4xx client errors
115
+ return false;
116
+ }
117
+
118
+ // Retry on unknown errors (might be network issues)
119
+ return true;
120
+ }
121
+
122
+ /**
123
+ * Wrap a function with retry logic.
124
+ *
125
+ * @param fn - Async function to retry
126
+ * @param config - Retry configuration
127
+ * @returns Result of the function
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * const result = await withRetry(
132
+ * async () => fetch('https://api.example.com/users'),
133
+ * { retries: 5, minTimeout: 2000 }
134
+ * );
135
+ * ```
136
+ */
137
+ export async function withRetry<T>(
138
+ fn: () => Promise<T>,
139
+ config?: RetryConfig
140
+ ): Promise<T> {
141
+ const finalConfig = { ...DEFAULT_RETRY_CONFIG, ...config };
142
+
143
+ return pRetry(
144
+ async () => {
145
+ try {
146
+ return await fn();
147
+ } catch (error) {
148
+ // Check if we should retry this error
149
+ if (!shouldRetry(error)) {
150
+ // Abort retry immediately for non-retryable errors
151
+ throw new AbortError(error as Error);
152
+ }
153
+
154
+ // Re-throw error to trigger retry
155
+ throw error;
156
+ }
157
+ },
158
+ {
159
+ retries: finalConfig.retries,
160
+ factor: finalConfig.factor,
161
+ minTimeout: finalConfig.minTimeout,
162
+ maxTimeout: finalConfig.maxTimeout,
163
+ randomize: finalConfig.randomize,
164
+ onFailedAttempt: finalConfig.onFailedAttempt ? (error) => {
165
+ // Adapt p-retry's FailedAttemptError to our FailedAttemptInfo
166
+ const pRetryError = error as any; // p-retry's internal type
167
+ finalConfig.onFailedAttempt!({
168
+ error: pRetryError as Error,
169
+ attemptNumber: pRetryError.attemptNumber,
170
+ retriesLeft: pRetryError.retriesLeft,
171
+ });
172
+ } : undefined,
173
+ }
174
+ );
175
+ }