@djangocfg/ext-payments 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 (67) hide show
  1. package/README.md +206 -0
  2. package/dist/chunk-5KY6HVXF.js +2593 -0
  3. package/dist/hooks.cjs +2666 -0
  4. package/dist/hooks.d.cts +186 -0
  5. package/dist/hooks.d.ts +186 -0
  6. package/dist/hooks.js +1 -0
  7. package/dist/index.cjs +2590 -0
  8. package/dist/index.d.cts +1287 -0
  9. package/dist/index.d.ts +1287 -0
  10. package/dist/index.js +1 -0
  11. package/package.json +79 -0
  12. package/src/api/generated/ext_payments/_utils/fetchers/ext_payments__payments.ts +408 -0
  13. package/src/api/generated/ext_payments/_utils/fetchers/index.ts +28 -0
  14. package/src/api/generated/ext_payments/_utils/hooks/ext_payments__payments.ts +147 -0
  15. package/src/api/generated/ext_payments/_utils/hooks/index.ts +28 -0
  16. package/src/api/generated/ext_payments/_utils/schemas/Balance.schema.ts +23 -0
  17. package/src/api/generated/ext_payments/_utils/schemas/Currency.schema.ts +28 -0
  18. package/src/api/generated/ext_payments/_utils/schemas/PaginatedPaymentListList.schema.ts +24 -0
  19. package/src/api/generated/ext_payments/_utils/schemas/PaymentDetail.schema.ts +44 -0
  20. package/src/api/generated/ext_payments/_utils/schemas/PaymentList.schema.ts +28 -0
  21. package/src/api/generated/ext_payments/_utils/schemas/Transaction.schema.ts +28 -0
  22. package/src/api/generated/ext_payments/_utils/schemas/index.ts +24 -0
  23. package/src/api/generated/ext_payments/api-instance.ts +131 -0
  24. package/src/api/generated/ext_payments/client.ts +301 -0
  25. package/src/api/generated/ext_payments/enums.ts +64 -0
  26. package/src/api/generated/ext_payments/errors.ts +116 -0
  27. package/src/api/generated/ext_payments/ext_payments__payments/client.ts +118 -0
  28. package/src/api/generated/ext_payments/ext_payments__payments/index.ts +2 -0
  29. package/src/api/generated/ext_payments/ext_payments__payments/models.ts +135 -0
  30. package/src/api/generated/ext_payments/http.ts +103 -0
  31. package/src/api/generated/ext_payments/index.ts +273 -0
  32. package/src/api/generated/ext_payments/logger.ts +259 -0
  33. package/src/api/generated/ext_payments/retry.ts +175 -0
  34. package/src/api/generated/ext_payments/schema.json +850 -0
  35. package/src/api/generated/ext_payments/storage.ts +161 -0
  36. package/src/api/generated/ext_payments/validation-events.ts +133 -0
  37. package/src/api/index.ts +9 -0
  38. package/src/config.ts +20 -0
  39. package/src/contexts/BalancesContext.tsx +62 -0
  40. package/src/contexts/CurrenciesContext.tsx +63 -0
  41. package/src/contexts/OverviewContext.tsx +173 -0
  42. package/src/contexts/PaymentsContext.tsx +121 -0
  43. package/src/contexts/PaymentsExtensionProvider.tsx +55 -0
  44. package/src/contexts/README.md +201 -0
  45. package/src/contexts/RootPaymentsContext.tsx +65 -0
  46. package/src/contexts/index.ts +45 -0
  47. package/src/contexts/types.ts +40 -0
  48. package/src/hooks/index.ts +20 -0
  49. package/src/index.ts +36 -0
  50. package/src/layouts/PaymentsLayout/PaymentsLayout.tsx +92 -0
  51. package/src/layouts/PaymentsLayout/components/CreatePaymentDialog.tsx +291 -0
  52. package/src/layouts/PaymentsLayout/components/PaymentDetailsDialog.tsx +290 -0
  53. package/src/layouts/PaymentsLayout/components/index.ts +2 -0
  54. package/src/layouts/PaymentsLayout/events.ts +47 -0
  55. package/src/layouts/PaymentsLayout/index.ts +16 -0
  56. package/src/layouts/PaymentsLayout/types.ts +6 -0
  57. package/src/layouts/PaymentsLayout/views/overview/components/BalanceCard.tsx +128 -0
  58. package/src/layouts/PaymentsLayout/views/overview/components/RecentPayments.tsx +142 -0
  59. package/src/layouts/PaymentsLayout/views/overview/components/index.ts +2 -0
  60. package/src/layouts/PaymentsLayout/views/overview/index.tsx +20 -0
  61. package/src/layouts/PaymentsLayout/views/payments/components/PaymentsList.tsx +277 -0
  62. package/src/layouts/PaymentsLayout/views/payments/components/index.ts +1 -0
  63. package/src/layouts/PaymentsLayout/views/payments/index.tsx +17 -0
  64. package/src/layouts/PaymentsLayout/views/transactions/components/TransactionsList.tsx +273 -0
  65. package/src/layouts/PaymentsLayout/views/transactions/components/index.ts +1 -0
  66. package/src/layouts/PaymentsLayout/views/transactions/index.tsx +17 -0
  67. package/src/utils/logger.ts +9 -0
@@ -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 { ExtPaymentsPayments } from "./ext_payments__payments/client";
46
+ export * as ExtPaymentsPaymentsTypes from "./ext_payments__payments/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_payments_payments!: ExtPaymentsPayments;
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_payments_payments = this._client.ext_payments_payments;
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_payments_payments = this._client.ext_payments_payments;
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();
@@ -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
+ }