@djangocfg/api 2.1.54 → 2.1.56
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/auth.cjs +28 -15
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +6 -6
- package/dist/auth.d.ts +6 -6
- package/dist/auth.mjs +28 -15
- package/dist/auth.mjs.map +1 -1
- package/dist/clients.cjs +56 -17
- package/dist/clients.cjs.map +1 -1
- package/dist/clients.d.cts +17 -17
- package/dist/clients.d.ts +17 -17
- package/dist/clients.mjs +56 -17
- package/dist/clients.mjs.map +1 -1
- package/dist/hooks.cjs +763 -12
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +11 -11
- package/dist/hooks.d.ts +11 -11
- package/dist/hooks.mjs +763 -12
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.cjs +893 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +59 -41
- package/dist/index.d.ts +59 -41
- package/dist/index.mjs +893 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/generated/cfg_accounts/_utils/schemas/CentrifugoToken.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OAuthAuthorizeRequestRequest.schema.ts +2 -2
- package/src/generated/cfg_accounts/_utils/schemas/OAuthAuthorizeResponse.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OAuthCallbackRequestRequest.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OAuthConnection.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OAuthProvidersResponse.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OAuthTokenResponse.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OTPRequestRequest.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/OTPVerifyRequest.schema.ts +1 -1
- package/src/generated/cfg_accounts/_utils/schemas/User.schema.ts +1 -1
- package/src/generated/cfg_accounts/api-instance.ts +61 -13
- package/src/generated/cfg_centrifugo/api-instance.ts +61 -13
- package/src/generated/cfg_totp/CLAUDE.md +90 -0
- package/src/generated/cfg_totp/_utils/fetchers/index.ts +33 -0
- package/src/generated/cfg_totp/_utils/fetchers/totp.ts +49 -0
- package/src/generated/cfg_totp/_utils/fetchers/totp__2fa_management.ts +108 -0
- package/src/generated/cfg_totp/_utils/fetchers/totp__2fa_setup.ts +153 -0
- package/src/generated/cfg_totp/_utils/fetchers/totp__2fa_verification.ts +152 -0
- package/src/generated/cfg_totp/_utils/fetchers/totp__backup_codes.ts +152 -0
- package/src/generated/cfg_totp/_utils/hooks/index.ts +33 -0
- package/src/generated/cfg_totp/_utils/hooks/totp.ts +42 -0
- package/src/generated/cfg_totp/_utils/hooks/totp__2fa_management.ts +58 -0
- package/src/generated/cfg_totp/_utils/hooks/totp__2fa_setup.ts +63 -0
- package/src/generated/cfg_totp/_utils/hooks/totp__2fa_verification.ts +62 -0
- package/src/generated/cfg_totp/_utils/hooks/totp__backup_codes.ts +59 -0
- package/src/generated/cfg_totp/_utils/schemas/BackupCodesRegenerateRequest.schema.ts +19 -0
- package/src/generated/cfg_totp/_utils/schemas/BackupCodesRegenerateResponse.schema.ts +20 -0
- package/src/generated/cfg_totp/_utils/schemas/BackupCodesStatus.schema.ts +21 -0
- package/src/generated/cfg_totp/_utils/schemas/ConfirmSetupRequest.schema.ts +20 -0
- package/src/generated/cfg_totp/_utils/schemas/ConfirmSetupResponse.schema.ts +21 -0
- package/src/generated/cfg_totp/_utils/schemas/DeviceList.schema.ts +26 -0
- package/src/generated/cfg_totp/_utils/schemas/DisableRequest.schema.ts +19 -0
- package/src/generated/cfg_totp/_utils/schemas/PaginatedDeviceListList.schema.ts +24 -0
- package/src/generated/cfg_totp/_utils/schemas/SetupRequest.schema.ts +19 -0
- package/src/generated/cfg_totp/_utils/schemas/SetupResponse.schema.ts +23 -0
- package/src/generated/cfg_totp/_utils/schemas/VerifyBackupRequest.schema.ts +20 -0
- package/src/generated/cfg_totp/_utils/schemas/VerifyRequest.schema.ts +20 -0
- package/src/generated/cfg_totp/_utils/schemas/VerifyResponse.schema.ts +24 -0
- package/src/generated/cfg_totp/_utils/schemas/index.ts +32 -0
- package/src/generated/cfg_totp/api-instance.ts +180 -0
- package/src/generated/cfg_totp/client.ts +313 -0
- package/src/generated/cfg_totp/enums.ts +12 -0
- package/src/generated/cfg_totp/errors.ts +117 -0
- package/src/generated/cfg_totp/http.ts +104 -0
- package/src/generated/cfg_totp/index.ts +302 -0
- package/src/generated/cfg_totp/logger.ts +260 -0
- package/src/generated/cfg_totp/retry.ts +176 -0
- package/src/generated/cfg_totp/schema.json +859 -0
- package/src/generated/cfg_totp/storage.ts +162 -0
- package/src/generated/cfg_totp/totp/client.ts +23 -0
- package/src/generated/cfg_totp/totp/index.ts +3 -0
- package/src/generated/cfg_totp/totp/models.ts +1 -0
- package/src/generated/cfg_totp/totp__2fa_management/client.ts +41 -0
- package/src/generated/cfg_totp/totp__2fa_management/index.ts +3 -0
- package/src/generated/cfg_totp/totp__2fa_management/models.ts +60 -0
- package/src/generated/cfg_totp/totp__2fa_setup/client.ts +32 -0
- package/src/generated/cfg_totp/totp__2fa_setup/index.ts +3 -0
- package/src/generated/cfg_totp/totp__2fa_setup/models.ts +54 -0
- package/src/generated/cfg_totp/totp__2fa_verification/client.ts +32 -0
- package/src/generated/cfg_totp/totp__2fa_verification/index.ts +3 -0
- package/src/generated/cfg_totp/totp__2fa_verification/models.ts +44 -0
- package/src/generated/cfg_totp/totp__backup_codes/client.ts +31 -0
- package/src/generated/cfg_totp/totp__backup_codes/index.ts +3 -0
- package/src/generated/cfg_totp/totp__backup_codes/models.ts +37 -0
- package/src/generated/cfg_totp/validation-events.ts +134 -0
- package/src/generated/cfg_webpush/_utils/schemas/SendPushRequestRequest.schema.ts +2 -2
- package/src/generated/cfg_webpush/_utils/schemas/SubscribeRequestRequest.schema.ts +1 -1
- package/src/generated/cfg_webpush/api-instance.ts +61 -13
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { 2faManagement } from "./totp__2fa_management";
|
|
2
|
+
import { 2faSetup } from "./totp__2fa_setup";
|
|
3
|
+
import { 2faVerification } from "./totp__2fa_verification";
|
|
4
|
+
import { BackupCodes } from "./totp__backup_codes";
|
|
5
|
+
import { Totp } from "./totp";
|
|
6
|
+
import { HttpClientAdapter, FetchAdapter } from "./http";
|
|
7
|
+
import { APIError, NetworkError } from "./errors";
|
|
8
|
+
import { APILogger, type LoggerConfig } from "./logger";
|
|
9
|
+
import { withRetry, type RetryConfig } from "./retry";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Async API client for Django CFG API.
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const client = new APIClient('https://api.example.com');
|
|
18
|
+
* const users = await client.users.list();
|
|
19
|
+
* const post = await client.posts.create(newPost);
|
|
20
|
+
*
|
|
21
|
+
* // Custom HTTP adapter (e.g., Axios)
|
|
22
|
+
* const client = new APIClient('https://api.example.com', {
|
|
23
|
+
* httpClient: new AxiosAdapter()
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export class APIClient {
|
|
28
|
+
private baseUrl: string;
|
|
29
|
+
private httpClient: HttpClientAdapter;
|
|
30
|
+
private logger: APILogger | null = null;
|
|
31
|
+
private retryConfig: RetryConfig | null = null;
|
|
32
|
+
|
|
33
|
+
// Sub-clients
|
|
34
|
+
public 2fa_management: 2faManagement;
|
|
35
|
+
public 2fa_setup: 2faSetup;
|
|
36
|
+
public 2fa_verification: 2faVerification;
|
|
37
|
+
public backup_codes: BackupCodes;
|
|
38
|
+
public totp: Totp;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
baseUrl: string,
|
|
42
|
+
options?: {
|
|
43
|
+
httpClient?: HttpClientAdapter;
|
|
44
|
+
loggerConfig?: Partial<LoggerConfig>;
|
|
45
|
+
retryConfig?: RetryConfig;
|
|
46
|
+
}
|
|
47
|
+
) {
|
|
48
|
+
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
49
|
+
this.httpClient = options?.httpClient || new FetchAdapter();
|
|
50
|
+
|
|
51
|
+
// Initialize logger if config provided
|
|
52
|
+
if (options?.loggerConfig !== undefined) {
|
|
53
|
+
this.logger = new APILogger(options.loggerConfig);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Store retry configuration
|
|
57
|
+
if (options?.retryConfig !== undefined) {
|
|
58
|
+
this.retryConfig = options.retryConfig;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Initialize sub-clients
|
|
62
|
+
this.2fa_management = new 2faManagement(this);
|
|
63
|
+
this.2fa_setup = new 2faSetup(this);
|
|
64
|
+
this.2fa_verification = new 2faVerification(this);
|
|
65
|
+
this.backup_codes = new BackupCodes(this);
|
|
66
|
+
this.totp = new Totp(this);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get CSRF token from cookies (for SessionAuthentication).
|
|
71
|
+
*
|
|
72
|
+
* Returns null if cookie doesn't exist (JWT-only auth).
|
|
73
|
+
*/
|
|
74
|
+
getCsrfToken(): string | null {
|
|
75
|
+
const name = 'csrftoken';
|
|
76
|
+
const value = `; ${document.cookie}`;
|
|
77
|
+
const parts = value.split(`; ${name}=`);
|
|
78
|
+
if (parts.length === 2) {
|
|
79
|
+
return parts.pop()?.split(';').shift() || null;
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Make HTTP request with Django CSRF and session handling.
|
|
86
|
+
* Automatically retries on network errors and 5xx server errors.
|
|
87
|
+
*/
|
|
88
|
+
async request<T>(
|
|
89
|
+
method: string,
|
|
90
|
+
path: string,
|
|
91
|
+
options?: {
|
|
92
|
+
params?: Record<string, any>;
|
|
93
|
+
body?: any;
|
|
94
|
+
formData?: FormData;
|
|
95
|
+
headers?: Record<string, string>;
|
|
96
|
+
}
|
|
97
|
+
): Promise<T> {
|
|
98
|
+
// Wrap request in retry logic if configured
|
|
99
|
+
if (this.retryConfig) {
|
|
100
|
+
return withRetry(() => this._makeRequest<T>(method, path, options), {
|
|
101
|
+
...this.retryConfig,
|
|
102
|
+
onFailedAttempt: (info) => {
|
|
103
|
+
// Log retry attempts
|
|
104
|
+
if (this.logger) {
|
|
105
|
+
this.logger.warn(
|
|
106
|
+
`Retry attempt ${info.attemptNumber}/${info.retriesLeft + info.attemptNumber} ` +
|
|
107
|
+
`for ${method} ${path}: ${info.error.message}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
// Call user's onFailedAttempt if provided
|
|
111
|
+
this.retryConfig?.onFailedAttempt?.(info);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// No retry configured, make request directly
|
|
117
|
+
return this._makeRequest<T>(method, path, options);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Internal request method (without retry wrapper).
|
|
122
|
+
* Used by request() method with optional retry logic.
|
|
123
|
+
*/
|
|
124
|
+
private async _makeRequest<T>(
|
|
125
|
+
method: string,
|
|
126
|
+
path: string,
|
|
127
|
+
options?: {
|
|
128
|
+
params?: Record<string, any>;
|
|
129
|
+
body?: any;
|
|
130
|
+
formData?: FormData;
|
|
131
|
+
headers?: Record<string, string>;
|
|
132
|
+
}
|
|
133
|
+
): Promise<T> {
|
|
134
|
+
// Build URL - handle both absolute and relative paths
|
|
135
|
+
// When baseUrl is empty (static builds), path is used as-is (relative to current origin)
|
|
136
|
+
const url = this.baseUrl ? `${this.baseUrl}${path}` : path;
|
|
137
|
+
const startTime = Date.now();
|
|
138
|
+
|
|
139
|
+
// Build headers - start with custom headers from options
|
|
140
|
+
const headers: Record<string, string> = {
|
|
141
|
+
...(options?.headers || {})
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Don't set Content-Type for FormData (browser will set it with boundary)
|
|
145
|
+
if (!options?.formData && !headers['Content-Type']) {
|
|
146
|
+
headers['Content-Type'] = 'application/json';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// CSRF not needed - SessionAuthentication not enabled in DRF config
|
|
150
|
+
// Your API uses JWT/Token authentication (no CSRF required)
|
|
151
|
+
|
|
152
|
+
// Log request
|
|
153
|
+
if (this.logger) {
|
|
154
|
+
this.logger.logRequest({
|
|
155
|
+
method,
|
|
156
|
+
url: url,
|
|
157
|
+
headers,
|
|
158
|
+
body: options?.formData || options?.body,
|
|
159
|
+
timestamp: startTime,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
// Make request via HTTP adapter
|
|
165
|
+
const response = await this.httpClient.request<T>({
|
|
166
|
+
method,
|
|
167
|
+
url: url,
|
|
168
|
+
headers,
|
|
169
|
+
params: options?.params,
|
|
170
|
+
body: options?.body,
|
|
171
|
+
formData: options?.formData,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const duration = Date.now() - startTime;
|
|
175
|
+
|
|
176
|
+
// Check for HTTP errors
|
|
177
|
+
if (response.status >= 400) {
|
|
178
|
+
const error = new APIError(
|
|
179
|
+
response.status,
|
|
180
|
+
response.statusText,
|
|
181
|
+
response.data,
|
|
182
|
+
url
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
// Log error
|
|
186
|
+
if (this.logger) {
|
|
187
|
+
this.logger.logError(
|
|
188
|
+
{
|
|
189
|
+
method,
|
|
190
|
+
url: url,
|
|
191
|
+
headers,
|
|
192
|
+
body: options?.formData || options?.body,
|
|
193
|
+
timestamp: startTime,
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
message: error.message,
|
|
197
|
+
statusCode: response.status,
|
|
198
|
+
duration,
|
|
199
|
+
timestamp: Date.now(),
|
|
200
|
+
}
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Log successful response
|
|
208
|
+
if (this.logger) {
|
|
209
|
+
this.logger.logResponse(
|
|
210
|
+
{
|
|
211
|
+
method,
|
|
212
|
+
url: url,
|
|
213
|
+
headers,
|
|
214
|
+
body: options?.formData || options?.body,
|
|
215
|
+
timestamp: startTime,
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
status: response.status,
|
|
219
|
+
statusText: response.statusText,
|
|
220
|
+
data: response.data,
|
|
221
|
+
duration,
|
|
222
|
+
timestamp: Date.now(),
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return response.data as T;
|
|
228
|
+
} catch (error) {
|
|
229
|
+
const duration = Date.now() - startTime;
|
|
230
|
+
|
|
231
|
+
// Re-throw APIError as-is
|
|
232
|
+
if (error instanceof APIError) {
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Detect CORS errors and dispatch event
|
|
237
|
+
const isCORSError = error instanceof TypeError &&
|
|
238
|
+
(error.message.toLowerCase().includes('cors') ||
|
|
239
|
+
error.message.toLowerCase().includes('failed to fetch') ||
|
|
240
|
+
error.message.toLowerCase().includes('network request failed'));
|
|
241
|
+
|
|
242
|
+
// Log specific error type first
|
|
243
|
+
if (this.logger) {
|
|
244
|
+
if (isCORSError) {
|
|
245
|
+
this.logger.error(`🚫 CORS Error: ${method} ${url}`);
|
|
246
|
+
this.logger.error(` → ${error instanceof Error ? error.message : String(error)}`);
|
|
247
|
+
this.logger.error(` → Configure security_domains parameter on the server`);
|
|
248
|
+
} else {
|
|
249
|
+
this.logger.error(`⚠️ Network Error: ${method} ${url}`);
|
|
250
|
+
this.logger.error(` → ${error instanceof Error ? error.message : String(error)}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Dispatch browser events
|
|
255
|
+
if (typeof window !== 'undefined') {
|
|
256
|
+
try {
|
|
257
|
+
if (isCORSError) {
|
|
258
|
+
// Dispatch CORS-specific error event
|
|
259
|
+
window.dispatchEvent(new CustomEvent('cors-error', {
|
|
260
|
+
detail: {
|
|
261
|
+
url: url,
|
|
262
|
+
method: method,
|
|
263
|
+
error: error instanceof Error ? error.message : String(error),
|
|
264
|
+
timestamp: new Date(),
|
|
265
|
+
},
|
|
266
|
+
bubbles: true,
|
|
267
|
+
cancelable: false,
|
|
268
|
+
}));
|
|
269
|
+
} else {
|
|
270
|
+
// Dispatch generic network error event
|
|
271
|
+
window.dispatchEvent(new CustomEvent('network-error', {
|
|
272
|
+
detail: {
|
|
273
|
+
url: url,
|
|
274
|
+
method: method,
|
|
275
|
+
error: error instanceof Error ? error.message : String(error),
|
|
276
|
+
timestamp: new Date(),
|
|
277
|
+
},
|
|
278
|
+
bubbles: true,
|
|
279
|
+
cancelable: false,
|
|
280
|
+
}));
|
|
281
|
+
}
|
|
282
|
+
} catch (eventError) {
|
|
283
|
+
// Silently fail - event dispatch should never crash the app
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Wrap other errors as NetworkError
|
|
288
|
+
const networkError = error instanceof Error
|
|
289
|
+
? new NetworkError(error.message, url, error)
|
|
290
|
+
: new NetworkError('Unknown error', url);
|
|
291
|
+
|
|
292
|
+
// Detailed logging via logger.logError
|
|
293
|
+
if (this.logger) {
|
|
294
|
+
this.logger.logError(
|
|
295
|
+
{
|
|
296
|
+
method,
|
|
297
|
+
url: url,
|
|
298
|
+
headers,
|
|
299
|
+
body: options?.formData || options?.body,
|
|
300
|
+
timestamp: startTime,
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
message: networkError.message,
|
|
304
|
+
duration,
|
|
305
|
+
timestamp: Date.now(),
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
throw networkError;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
+
/**
|
|
3
|
+
* * `pending` - Pending Confirmation
|
|
4
|
+
* * `active` - Active
|
|
5
|
+
* * `disabled` - Disabled
|
|
6
|
+
*/
|
|
7
|
+
export enum DeviceListStatus {
|
|
8
|
+
PENDING = "pending",
|
|
9
|
+
ACTIVE = "active",
|
|
10
|
+
DISABLED = "disabled",
|
|
11
|
+
}
|
|
12
|
+
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
+
/**
|
|
3
|
+
* API Error Classes
|
|
4
|
+
*
|
|
5
|
+
* Typed error classes with Django REST Framework support.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* HTTP API Error with DRF field-specific validation errors.
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* try {
|
|
14
|
+
* await api.users.create(userData);
|
|
15
|
+
* } catch (error) {
|
|
16
|
+
* if (error instanceof APIError) {
|
|
17
|
+
* if (error.isValidationError) {
|
|
18
|
+
* console.log('Field errors:', error.fieldErrors);
|
|
19
|
+
* // { "email": ["Email already exists"], "username": ["Required"] }
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class APIError extends Error {
|
|
26
|
+
constructor(
|
|
27
|
+
public statusCode: number,
|
|
28
|
+
public statusText: string,
|
|
29
|
+
public response: any,
|
|
30
|
+
public url: string,
|
|
31
|
+
message?: string
|
|
32
|
+
) {
|
|
33
|
+
super(message || `HTTP ${statusCode}: ${statusText}`);
|
|
34
|
+
this.name = 'APIError';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get error details from response.
|
|
39
|
+
* DRF typically returns: { "detail": "Error message" } or { "field": ["error1", "error2"] }
|
|
40
|
+
*/
|
|
41
|
+
get details(): Record<string, any> | null {
|
|
42
|
+
if (typeof this.response === 'object' && this.response !== null) {
|
|
43
|
+
return this.response;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get field-specific validation errors from DRF.
|
|
50
|
+
* Returns: { "field_name": ["error1", "error2"], ... }
|
|
51
|
+
*/
|
|
52
|
+
get fieldErrors(): Record<string, string[]> | null {
|
|
53
|
+
const details = this.details;
|
|
54
|
+
if (!details) return null;
|
|
55
|
+
|
|
56
|
+
// DRF typically returns: { "field": ["error1", "error2"] }
|
|
57
|
+
const fieldErrors: Record<string, string[]> = {};
|
|
58
|
+
for (const [key, value] of Object.entries(details)) {
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
fieldErrors[key] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return Object.keys(fieldErrors).length > 0 ? fieldErrors : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get single error message from DRF.
|
|
69
|
+
* Checks for "detail", "message", or first field error.
|
|
70
|
+
*/
|
|
71
|
+
get errorMessage(): string {
|
|
72
|
+
const details = this.details;
|
|
73
|
+
if (!details) return this.message;
|
|
74
|
+
|
|
75
|
+
// Check for "detail" field (common in DRF)
|
|
76
|
+
if (details.detail) {
|
|
77
|
+
return Array.isArray(details.detail) ? details.detail.join(', ') : String(details.detail);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Check for "message" field
|
|
81
|
+
if (details.message) {
|
|
82
|
+
return String(details.message);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Return first field error
|
|
86
|
+
const fieldErrors = this.fieldErrors;
|
|
87
|
+
if (fieldErrors) {
|
|
88
|
+
const firstField = Object.keys(fieldErrors)[0];
|
|
89
|
+
if (firstField) {
|
|
90
|
+
return `${firstField}: ${fieldErrors[firstField]?.join(', ')}`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return this.message;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Helper methods for common HTTP status codes
|
|
98
|
+
get isValidationError(): boolean { return this.statusCode === 400; }
|
|
99
|
+
get isAuthError(): boolean { return this.statusCode === 401; }
|
|
100
|
+
get isPermissionError(): boolean { return this.statusCode === 403; }
|
|
101
|
+
get isNotFoundError(): boolean { return this.statusCode === 404; }
|
|
102
|
+
get isServerError(): boolean { return this.statusCode >= 500 && this.statusCode < 600; }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Network Error (connection failed, timeout, etc.)
|
|
107
|
+
*/
|
|
108
|
+
export class NetworkError extends Error {
|
|
109
|
+
constructor(
|
|
110
|
+
message: string,
|
|
111
|
+
public url: string,
|
|
112
|
+
public originalError?: Error
|
|
113
|
+
) {
|
|
114
|
+
super(message);
|
|
115
|
+
this.name = 'NetworkError';
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
+
/**
|
|
3
|
+
* HTTP Client Adapter Pattern
|
|
4
|
+
*
|
|
5
|
+
* Allows switching between fetch/axios/httpx without changing generated code.
|
|
6
|
+
* Provides unified interface for making HTTP requests.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface HttpRequest {
|
|
10
|
+
method: string;
|
|
11
|
+
url: string;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
body?: any;
|
|
14
|
+
params?: Record<string, any>;
|
|
15
|
+
/** FormData for file uploads (multipart/form-data) */
|
|
16
|
+
formData?: FormData;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface HttpResponse<T = any> {
|
|
20
|
+
data: T;
|
|
21
|
+
status: number;
|
|
22
|
+
statusText: string;
|
|
23
|
+
headers: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* HTTP Client Adapter Interface.
|
|
28
|
+
* Implement this to use custom HTTP clients (axios, httpx, etc.)
|
|
29
|
+
*/
|
|
30
|
+
export interface HttpClientAdapter {
|
|
31
|
+
request<T = any>(request: HttpRequest): Promise<HttpResponse<T>>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Default Fetch API adapter.
|
|
36
|
+
* Uses native browser fetch() with proper error handling.
|
|
37
|
+
*/
|
|
38
|
+
export class FetchAdapter implements HttpClientAdapter {
|
|
39
|
+
async request<T = any>(request: HttpRequest): Promise<HttpResponse<T>> {
|
|
40
|
+
const { method, url, headers, body, params, formData } = request;
|
|
41
|
+
|
|
42
|
+
// Build URL with query params
|
|
43
|
+
let finalUrl = url;
|
|
44
|
+
if (params) {
|
|
45
|
+
const searchParams = new URLSearchParams();
|
|
46
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
47
|
+
if (value !== null && value !== undefined) {
|
|
48
|
+
searchParams.append(key, String(value));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const queryString = searchParams.toString();
|
|
52
|
+
if (queryString) {
|
|
53
|
+
finalUrl = url.includes('?') ? `${url}&${queryString}` : `${url}?${queryString}`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Build headers
|
|
58
|
+
const finalHeaders: Record<string, string> = { ...headers };
|
|
59
|
+
|
|
60
|
+
// Determine body and content-type
|
|
61
|
+
let requestBody: string | FormData | undefined;
|
|
62
|
+
|
|
63
|
+
if (formData) {
|
|
64
|
+
// For multipart/form-data, let browser set Content-Type with boundary
|
|
65
|
+
requestBody = formData;
|
|
66
|
+
// Don't set Content-Type - browser will set it with boundary
|
|
67
|
+
} else if (body) {
|
|
68
|
+
// JSON request
|
|
69
|
+
finalHeaders['Content-Type'] = 'application/json';
|
|
70
|
+
requestBody = JSON.stringify(body);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Make request
|
|
74
|
+
const response = await fetch(finalUrl, {
|
|
75
|
+
method,
|
|
76
|
+
headers: finalHeaders,
|
|
77
|
+
body: requestBody,
|
|
78
|
+
credentials: 'include', // Include Django session cookies
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Parse response
|
|
82
|
+
let data: any = null;
|
|
83
|
+
const contentType = response.headers.get('content-type');
|
|
84
|
+
|
|
85
|
+
if (response.status !== 204 && contentType?.includes('application/json')) {
|
|
86
|
+
data = await response.json();
|
|
87
|
+
} else if (response.status !== 204) {
|
|
88
|
+
data = await response.text();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Convert Headers to plain object
|
|
92
|
+
const responseHeaders: Record<string, string> = {};
|
|
93
|
+
response.headers.forEach((value, key) => {
|
|
94
|
+
responseHeaders[key] = value;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
data,
|
|
99
|
+
status: response.status,
|
|
100
|
+
statusText: response.statusText,
|
|
101
|
+
headers: responseHeaders,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|