@djangocfg/api 2.1.264 → 2.1.266

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djangocfg/api",
3
- "version": "2.1.264",
3
+ "version": "2.1.266",
4
4
  "description": "Auto-generated TypeScript API client with React hooks, SWR integration, and Zod validation for Django REST Framework backends",
5
5
  "keywords": [
6
6
  "django",
@@ -84,7 +84,7 @@
84
84
  "devDependencies": {
85
85
  "@types/node": "^24.7.2",
86
86
  "@types/react": "^19.1.0",
87
- "@djangocfg/typescript-config": "^2.1.264",
87
+ "@djangocfg/typescript-config": "^2.1.266",
88
88
  "next": "^16.0.10",
89
89
  "react": "^19.1.0",
90
90
  "tsup": "^8.5.0",
@@ -3,6 +3,7 @@
3
3
  import { useCallback, useState } from 'react';
4
4
 
5
5
  import { apiTotp } from '../../clients';
6
+ import { APIError } from '../../_api/generated/cfg_totp';
6
7
  import { authLogger } from '../utils/logger';
7
8
 
8
9
  export interface TwoFactorDevice {
@@ -48,6 +49,18 @@ export interface UseTwoFactorStatusReturn {
48
49
  * }
49
50
  * ```
50
51
  */
52
+ /** Extract a human-readable message from a thrown error. */
53
+ function extractErrorMessage(err: unknown, fallback: string): string {
54
+ if (err instanceof APIError) {
55
+ const body = err.response as Record<string, unknown> | null;
56
+ if (typeof body?.error === 'string') return body.error;
57
+ if (typeof body?.detail === 'string') return body.detail;
58
+ return err.message;
59
+ }
60
+ if (err instanceof Error) return err.message;
61
+ return fallback;
62
+ }
63
+
51
64
  export const useTwoFactorStatus = (): UseTwoFactorStatusReturn => {
52
65
  const [isLoading, setIsLoading] = useState(false);
53
66
  const [error, setError] = useState<string | null>(null);
@@ -85,7 +98,7 @@ export const useTwoFactorStatus = (): UseTwoFactorStatusReturn => {
85
98
  authLogger.info('2FA status:', response.has_2fa_enabled ? 'enabled' : 'disabled');
86
99
 
87
100
  } catch (err) {
88
- const errorMessage = err instanceof Error ? err.message : 'Failed to fetch 2FA status';
101
+ const errorMessage = extractErrorMessage(err, 'Failed to fetch 2FA status');
89
102
  authLogger.error('Failed to fetch 2FA status:', err);
90
103
  setError(errorMessage);
91
104
  } finally {
@@ -116,8 +129,8 @@ export const useTwoFactorStatus = (): UseTwoFactorStatusReturn => {
116
129
  authLogger.info('2FA disabled successfully');
117
130
  return true;
118
131
 
119
- } catch (err) {
120
- const errorMessage = err instanceof Error ? err.message : 'Invalid verification code';
132
+ } catch (err: unknown) {
133
+ const errorMessage = extractErrorMessage(err, 'Invalid verification code');
121
134
  authLogger.error('Failed to disable 2FA:', err);
122
135
  setError(errorMessage);
123
136
  return false;