@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/api",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.56",
|
|
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",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"check": "tsc --noEmit"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
|
-
"@djangocfg/ui-nextjs": "^2.1.
|
|
77
|
+
"@djangocfg/ui-nextjs": "^2.1.56",
|
|
78
78
|
"consola": "^3.4.2",
|
|
79
79
|
"next": "^14 || ^15",
|
|
80
80
|
"p-retry": "^7.0.0",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"devDependencies": {
|
|
86
86
|
"@types/node": "^24.7.2",
|
|
87
87
|
"@types/react": "^19.0.0",
|
|
88
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
88
|
+
"@djangocfg/typescript-config": "^2.1.56",
|
|
89
89
|
"next": "^15.0.0",
|
|
90
90
|
"react": "^19.0.0",
|
|
91
91
|
"tsup": "^8.5.0",
|
|
@@ -10,8 +10,8 @@ import { z } from 'zod'
|
|
|
10
10
|
* Request to start OAuth flow.
|
|
11
11
|
*/
|
|
12
12
|
export const OAuthAuthorizeRequestRequestSchema = z.object({
|
|
13
|
-
redirect_uri: z.url().optional(),
|
|
14
|
-
source_url: z.url().optional(),
|
|
13
|
+
redirect_uri: z.union([z.url(), z.literal('')]).optional(),
|
|
14
|
+
source_url: z.union([z.url(), z.literal('')]).optional(),
|
|
15
15
|
})
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -12,7 +12,7 @@ import { z } from 'zod'
|
|
|
12
12
|
export const OAuthCallbackRequestRequestSchema = z.object({
|
|
13
13
|
code: z.string().min(10).max(500),
|
|
14
14
|
state: z.string().min(20).max(100),
|
|
15
|
-
redirect_uri: z.url().optional(),
|
|
15
|
+
redirect_uri: z.union([z.url(), z.literal('')]).optional(),
|
|
16
16
|
})
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -16,7 +16,7 @@ export const OAuthConnectionSchema = z.object({
|
|
|
16
16
|
provider_display: z.string(),
|
|
17
17
|
provider_username: z.string(),
|
|
18
18
|
provider_email: z.email(),
|
|
19
|
-
provider_avatar_url: z.url(),
|
|
19
|
+
provider_avatar_url: z.union([z.url(), z.literal('')]),
|
|
20
20
|
connected_at: z.iso.datetime(),
|
|
21
21
|
last_login_at: z.iso.datetime(),
|
|
22
22
|
})
|
|
@@ -10,7 +10,7 @@ import { z } from 'zod'
|
|
|
10
10
|
* Response with available OAuth providers.
|
|
11
11
|
*/
|
|
12
12
|
export const OAuthProvidersResponseSchema = z.object({
|
|
13
|
-
providers: z.array(z.record(z.string(), z.
|
|
13
|
+
providers: z.array(z.record(z.string(), z.any())),
|
|
14
14
|
})
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -12,7 +12,7 @@ import { z } from 'zod'
|
|
|
12
12
|
export const OAuthTokenResponseSchema = z.object({
|
|
13
13
|
access: z.string(),
|
|
14
14
|
refresh: z.string(),
|
|
15
|
-
user: z.record(z.string(), z.
|
|
15
|
+
user: z.record(z.string(), z.any()),
|
|
16
16
|
is_new_user: z.boolean(),
|
|
17
17
|
is_new_connection: z.boolean(),
|
|
18
18
|
})
|
|
@@ -13,7 +13,7 @@ import * as Enums from '../../enums'
|
|
|
13
13
|
export const OTPRequestRequestSchema = z.object({
|
|
14
14
|
identifier: z.string().min(1),
|
|
15
15
|
channel: z.nativeEnum(Enums.OTPRequestRequestChannel).optional(),
|
|
16
|
-
source_url: z.url().optional(),
|
|
16
|
+
source_url: z.union([z.url(), z.literal('')]).optional(),
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -14,7 +14,7 @@ export const OTPVerifyRequestSchema = z.object({
|
|
|
14
14
|
identifier: z.string().min(1),
|
|
15
15
|
otp: z.string().min(6).max(6),
|
|
16
16
|
channel: z.nativeEnum(Enums.OTPVerifyRequestChannel).optional(),
|
|
17
|
-
source_url: z.url().optional(),
|
|
17
|
+
source_url: z.union([z.url(), z.literal('')]).optional(),
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -21,7 +21,7 @@ export const UserSchema = z.object({
|
|
|
21
21
|
company: z.string().max(100).optional(),
|
|
22
22
|
phone: z.string().max(20).optional(),
|
|
23
23
|
position: z.string().max(100).optional(),
|
|
24
|
-
avatar: z.url().nullable(),
|
|
24
|
+
avatar: z.union([z.url(), z.literal('')]).nullable(),
|
|
25
25
|
is_staff: z.boolean(),
|
|
26
26
|
is_superuser: z.boolean(),
|
|
27
27
|
date_joined: z.iso.datetime(),
|
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
2
|
/**
|
|
3
|
-
* Global API Instance - Singleton configuration
|
|
3
|
+
* Global API Instance - Singleton configuration with auto-configuration support
|
|
4
4
|
*
|
|
5
|
-
* This module provides a global API instance that
|
|
6
|
-
*
|
|
5
|
+
* This module provides a global API instance that auto-configures from
|
|
6
|
+
* environment variables or can be configured manually.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* AUTO-CONFIGURATION (recommended):
|
|
9
|
+
* Set one of these environment variables and the API will auto-configure:
|
|
10
|
+
* - NEXT_PUBLIC_API_URL (Next.js)
|
|
11
|
+
* - VITE_API_URL (Vite)
|
|
12
|
+
* - REACT_APP_API_URL (Create React App)
|
|
13
|
+
* - API_URL (generic)
|
|
14
|
+
*
|
|
15
|
+
* Then just use fetchers and hooks directly:
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { getUsers } from './_utils/fetchers'
|
|
18
|
+
* const users = await getUsers({ page: 1 })
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* MANUAL CONFIGURATION:
|
|
9
22
|
* ```typescript
|
|
10
|
-
* // Configure once (e.g., in your app entry point)
|
|
11
23
|
* import { configureAPI } from './api-instance'
|
|
12
24
|
*
|
|
13
25
|
* configureAPI({
|
|
14
26
|
* baseUrl: 'https://api.example.com',
|
|
15
27
|
* token: 'your-jwt-token'
|
|
16
28
|
* })
|
|
17
|
-
*
|
|
18
|
-
* // Then use fetchers and hooks anywhere without configuration
|
|
19
|
-
* import { getUsers } from './fetchers'
|
|
20
|
-
* const users = await getUsers({ page: 1 })
|
|
21
29
|
* ```
|
|
22
30
|
*
|
|
23
31
|
* For SSR or multiple instances:
|
|
24
32
|
* ```typescript
|
|
25
33
|
* import { API } from './index'
|
|
26
|
-
* import { getUsers } from './fetchers'
|
|
34
|
+
* import { getUsers } from './_utils/fetchers'
|
|
27
35
|
*
|
|
28
36
|
* const api = new API('https://api.example.com')
|
|
29
37
|
* const users = await getUsers({ page: 1 }, api)
|
|
@@ -33,27 +41,67 @@
|
|
|
33
41
|
import { API, type APIOptions } from './index'
|
|
34
42
|
|
|
35
43
|
let globalAPI: API | null = null
|
|
44
|
+
let autoConfigAttempted = false
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Auto-configure from environment variable if available (Next.js pattern)
|
|
48
|
+
* This allows hooks and fetchers to work without explicit configureAPI() call
|
|
49
|
+
*
|
|
50
|
+
* Supported environment variables:
|
|
51
|
+
* - NEXT_PUBLIC_API_URL (Next.js)
|
|
52
|
+
* - VITE_API_URL (Vite)
|
|
53
|
+
* - REACT_APP_API_URL (Create React App)
|
|
54
|
+
* - API_URL (generic)
|
|
55
|
+
*/
|
|
56
|
+
function tryAutoConfigureFromEnv(): void {
|
|
57
|
+
// Only attempt once
|
|
58
|
+
if (autoConfigAttempted) return
|
|
59
|
+
autoConfigAttempted = true
|
|
60
|
+
|
|
61
|
+
// Skip if already configured
|
|
62
|
+
if (globalAPI) return
|
|
63
|
+
|
|
64
|
+
// Skip if process is not available (pure browser without bundler)
|
|
65
|
+
if (typeof process === 'undefined' || !process.env) return
|
|
66
|
+
|
|
67
|
+
// Try different environment variable patterns
|
|
68
|
+
const baseUrl =
|
|
69
|
+
process.env.NEXT_PUBLIC_API_URL ||
|
|
70
|
+
process.env.VITE_API_URL ||
|
|
71
|
+
process.env.REACT_APP_API_URL ||
|
|
72
|
+
process.env.API_URL
|
|
73
|
+
|
|
74
|
+
if (baseUrl) {
|
|
75
|
+
globalAPI = new API(baseUrl)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
36
78
|
|
|
37
79
|
/**
|
|
38
80
|
* Get the global API instance
|
|
39
|
-
*
|
|
81
|
+
* Auto-configures from environment variables on first call if not manually configured.
|
|
82
|
+
* @throws Error if API is not configured and no env variable is set
|
|
40
83
|
*/
|
|
41
84
|
export function getAPIInstance(): API {
|
|
85
|
+
// Try auto-configuration on first access (lazy initialization)
|
|
86
|
+
tryAutoConfigureFromEnv()
|
|
87
|
+
|
|
42
88
|
if (!globalAPI) {
|
|
43
89
|
throw new Error(
|
|
44
90
|
'API not configured. Call configureAPI() with your base URL before using fetchers or hooks.\n\n' +
|
|
45
91
|
'Example:\n' +
|
|
46
92
|
' import { configureAPI } from "./api-instance"\n' +
|
|
47
|
-
' configureAPI({ baseUrl: "https://api.example.com" })'
|
|
93
|
+
' configureAPI({ baseUrl: "https://api.example.com" })\n\n' +
|
|
94
|
+
'Or set environment variable: NEXT_PUBLIC_API_URL, VITE_API_URL, or REACT_APP_API_URL'
|
|
48
95
|
)
|
|
49
96
|
}
|
|
50
97
|
return globalAPI
|
|
51
98
|
}
|
|
52
99
|
|
|
53
100
|
/**
|
|
54
|
-
* Check if API is configured
|
|
101
|
+
* Check if API is configured (or can be auto-configured)
|
|
55
102
|
*/
|
|
56
103
|
export function isAPIConfigured(): boolean {
|
|
104
|
+
tryAutoConfigureFromEnv()
|
|
57
105
|
return globalAPI !== null
|
|
58
106
|
}
|
|
59
107
|
|
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
2
|
/**
|
|
3
|
-
* Global API Instance - Singleton configuration
|
|
3
|
+
* Global API Instance - Singleton configuration with auto-configuration support
|
|
4
4
|
*
|
|
5
|
-
* This module provides a global API instance that
|
|
6
|
-
*
|
|
5
|
+
* This module provides a global API instance that auto-configures from
|
|
6
|
+
* environment variables or can be configured manually.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* AUTO-CONFIGURATION (recommended):
|
|
9
|
+
* Set one of these environment variables and the API will auto-configure:
|
|
10
|
+
* - NEXT_PUBLIC_API_URL (Next.js)
|
|
11
|
+
* - VITE_API_URL (Vite)
|
|
12
|
+
* - REACT_APP_API_URL (Create React App)
|
|
13
|
+
* - API_URL (generic)
|
|
14
|
+
*
|
|
15
|
+
* Then just use fetchers and hooks directly:
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { getUsers } from './_utils/fetchers'
|
|
18
|
+
* const users = await getUsers({ page: 1 })
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* MANUAL CONFIGURATION:
|
|
9
22
|
* ```typescript
|
|
10
|
-
* // Configure once (e.g., in your app entry point)
|
|
11
23
|
* import { configureAPI } from './api-instance'
|
|
12
24
|
*
|
|
13
25
|
* configureAPI({
|
|
14
26
|
* baseUrl: 'https://api.example.com',
|
|
15
27
|
* token: 'your-jwt-token'
|
|
16
28
|
* })
|
|
17
|
-
*
|
|
18
|
-
* // Then use fetchers and hooks anywhere without configuration
|
|
19
|
-
* import { getUsers } from './fetchers'
|
|
20
|
-
* const users = await getUsers({ page: 1 })
|
|
21
29
|
* ```
|
|
22
30
|
*
|
|
23
31
|
* For SSR or multiple instances:
|
|
24
32
|
* ```typescript
|
|
25
33
|
* import { API } from './index'
|
|
26
|
-
* import { getUsers } from './fetchers'
|
|
34
|
+
* import { getUsers } from './_utils/fetchers'
|
|
27
35
|
*
|
|
28
36
|
* const api = new API('https://api.example.com')
|
|
29
37
|
* const users = await getUsers({ page: 1 }, api)
|
|
@@ -33,27 +41,67 @@
|
|
|
33
41
|
import { API, type APIOptions } from './index'
|
|
34
42
|
|
|
35
43
|
let globalAPI: API | null = null
|
|
44
|
+
let autoConfigAttempted = false
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Auto-configure from environment variable if available (Next.js pattern)
|
|
48
|
+
* This allows hooks and fetchers to work without explicit configureAPI() call
|
|
49
|
+
*
|
|
50
|
+
* Supported environment variables:
|
|
51
|
+
* - NEXT_PUBLIC_API_URL (Next.js)
|
|
52
|
+
* - VITE_API_URL (Vite)
|
|
53
|
+
* - REACT_APP_API_URL (Create React App)
|
|
54
|
+
* - API_URL (generic)
|
|
55
|
+
*/
|
|
56
|
+
function tryAutoConfigureFromEnv(): void {
|
|
57
|
+
// Only attempt once
|
|
58
|
+
if (autoConfigAttempted) return
|
|
59
|
+
autoConfigAttempted = true
|
|
60
|
+
|
|
61
|
+
// Skip if already configured
|
|
62
|
+
if (globalAPI) return
|
|
63
|
+
|
|
64
|
+
// Skip if process is not available (pure browser without bundler)
|
|
65
|
+
if (typeof process === 'undefined' || !process.env) return
|
|
66
|
+
|
|
67
|
+
// Try different environment variable patterns
|
|
68
|
+
const baseUrl =
|
|
69
|
+
process.env.NEXT_PUBLIC_API_URL ||
|
|
70
|
+
process.env.VITE_API_URL ||
|
|
71
|
+
process.env.REACT_APP_API_URL ||
|
|
72
|
+
process.env.API_URL
|
|
73
|
+
|
|
74
|
+
if (baseUrl) {
|
|
75
|
+
globalAPI = new API(baseUrl)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
36
78
|
|
|
37
79
|
/**
|
|
38
80
|
* Get the global API instance
|
|
39
|
-
*
|
|
81
|
+
* Auto-configures from environment variables on first call if not manually configured.
|
|
82
|
+
* @throws Error if API is not configured and no env variable is set
|
|
40
83
|
*/
|
|
41
84
|
export function getAPIInstance(): API {
|
|
85
|
+
// Try auto-configuration on first access (lazy initialization)
|
|
86
|
+
tryAutoConfigureFromEnv()
|
|
87
|
+
|
|
42
88
|
if (!globalAPI) {
|
|
43
89
|
throw new Error(
|
|
44
90
|
'API not configured. Call configureAPI() with your base URL before using fetchers or hooks.\n\n' +
|
|
45
91
|
'Example:\n' +
|
|
46
92
|
' import { configureAPI } from "./api-instance"\n' +
|
|
47
|
-
' configureAPI({ baseUrl: "https://api.example.com" })'
|
|
93
|
+
' configureAPI({ baseUrl: "https://api.example.com" })\n\n' +
|
|
94
|
+
'Or set environment variable: NEXT_PUBLIC_API_URL, VITE_API_URL, or REACT_APP_API_URL'
|
|
48
95
|
)
|
|
49
96
|
}
|
|
50
97
|
return globalAPI
|
|
51
98
|
}
|
|
52
99
|
|
|
53
100
|
/**
|
|
54
|
-
* Check if API is configured
|
|
101
|
+
* Check if API is configured (or can be auto-configured)
|
|
55
102
|
*/
|
|
56
103
|
export function isAPIConfigured(): boolean {
|
|
104
|
+
tryAutoConfigureFromEnv()
|
|
57
105
|
return globalAPI !== null
|
|
58
106
|
}
|
|
59
107
|
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Django CFG API - Typescript Client
|
|
2
|
+
|
|
3
|
+
Auto-generated. **Do not edit manually.**
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
python manage.py generate_client --groups cfg_totp --typescript
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Stats
|
|
10
|
+
|
|
11
|
+
| | |
|
|
12
|
+
|---|---|
|
|
13
|
+
| Version | 3.0.3 |
|
|
14
|
+
| Operations | 9 |
|
|
15
|
+
| Schemas | 13 |
|
|
16
|
+
|
|
17
|
+
## Resources
|
|
18
|
+
|
|
19
|
+
- **2FA Management** (2 ops)
|
|
20
|
+
- **2FA Setup** (2 ops)
|
|
21
|
+
- **2FA Verification** (2 ops)
|
|
22
|
+
- **Backup Codes** (2 ops)
|
|
23
|
+
- **totp** (1 ops)
|
|
24
|
+
|
|
25
|
+
## Operations
|
|
26
|
+
|
|
27
|
+
**2FA Management:**
|
|
28
|
+
- `GET` /cfg/totp/devices/ → `cfg_totp_devices_list`
|
|
29
|
+
- `POST` /cfg/totp/disable/ → `cfg_totp_disable_create`
|
|
30
|
+
|
|
31
|
+
**2FA Setup:**
|
|
32
|
+
- `POST` /cfg/totp/setup/confirm/ → `cfg_totp_setup_confirm_create`
|
|
33
|
+
- `POST` /cfg/totp/setup/ → `cfg_totp_setup_create`
|
|
34
|
+
|
|
35
|
+
**2FA Verification:**
|
|
36
|
+
- `POST` /cfg/totp/verify/backup/ → `cfg_totp_verify_backup_create`
|
|
37
|
+
- `POST` /cfg/totp/verify/ → `cfg_totp_verify_create`
|
|
38
|
+
|
|
39
|
+
**Backup Codes:**
|
|
40
|
+
- `POST` /cfg/totp/backup-codes/regenerate/ → `cfg_totp_backup_codes_regenerate_create`
|
|
41
|
+
- `GET` /cfg/totp/backup-codes/ → `cfg_totp_backup_codes_retrieve`
|
|
42
|
+
|
|
43
|
+
**totp:**
|
|
44
|
+
- `DELETE` /cfg/totp/devices/{id}/ → `cfg_totp_devices_destroy`
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { APIClient } from './';
|
|
50
|
+
|
|
51
|
+
const client = new APIClient({ baseUrl, token });
|
|
52
|
+
|
|
53
|
+
await client.backup codes.retrieve({ id: 1 });
|
|
54
|
+
await client.backup codes.create({ ... });
|
|
55
|
+
await client.2fa management.list();
|
|
56
|
+
await client.2fa management.create({ ... });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**SWR Hooks:**
|
|
60
|
+
```typescript
|
|
61
|
+
import { useBackup codesList } from './hooks';
|
|
62
|
+
const { data, isLoading } = useBackup codesList();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## How It Works
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
DRF ViewSets → drf-spectacular → OpenAPI → IR Parser → Generator → This Client
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Configuration** (`api/config.py`):
|
|
72
|
+
```python
|
|
73
|
+
openapi_client = OpenAPIClientConfig(
|
|
74
|
+
enabled=True,
|
|
75
|
+
groups=[OpenAPIGroupConfig(name="cfg_totp", apps=["..."])],
|
|
76
|
+
generate_zod_schemas=True, # → schemas.ts
|
|
77
|
+
generate_fetchers=True, # → fetchers.ts
|
|
78
|
+
generate_swr_hooks=True, # → hooks.ts
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Copy to Next.js** (if `nextjs_admin` configured):
|
|
83
|
+
```python
|
|
84
|
+
nextjs_admin = NextJsAdminConfig(
|
|
85
|
+
project_path="../frontend/apps/...",
|
|
86
|
+
api_output_path="app/_lib/api/generated",
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
@see https://djangocfg.com/docs/features/api-generation
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
+
/**
|
|
3
|
+
* Typed Fetchers - Universal API functions
|
|
4
|
+
*
|
|
5
|
+
* Auto-generated from OpenAPI specification.
|
|
6
|
+
* These functions work in any JavaScript environment.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Runtime validation with Zod
|
|
10
|
+
* - Type-safe parameters and responses
|
|
11
|
+
* - Works with any data-fetching library (SWR, React Query, etc)
|
|
12
|
+
* - Server Component compatible
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import * as fetchers from './fetchers'
|
|
17
|
+
*
|
|
18
|
+
* // Direct usage
|
|
19
|
+
* const user = await fetchers.getUser(1)
|
|
20
|
+
*
|
|
21
|
+
* // With SWR
|
|
22
|
+
* const { data } = useSWR('user-1', () => fetchers.getUser(1))
|
|
23
|
+
*
|
|
24
|
+
* // With React Query
|
|
25
|
+
* const { data } = useQuery(['user', 1], () => fetchers.getUser(1))
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export * from './totp'
|
|
30
|
+
export * from './totp__2fa_management'
|
|
31
|
+
export * from './totp__2fa_setup'
|
|
32
|
+
export * from './totp__2fa_verification'
|
|
33
|
+
export * from './totp__backup_codes'
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
+
/**
|
|
3
|
+
* Typed fetchers for Totp
|
|
4
|
+
*
|
|
5
|
+
* Universal functions that work in any environment:
|
|
6
|
+
* - Next.js (App Router / Pages Router / Server Components)
|
|
7
|
+
* - React Native
|
|
8
|
+
* - Node.js backend
|
|
9
|
+
*
|
|
10
|
+
* These fetchers use Zod schemas for runtime validation.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Configure API once (in your app entry point)
|
|
15
|
+
* import { configureAPI } from '../../api-instance'
|
|
16
|
+
* configureAPI({ baseUrl: 'https://api.example.com' })
|
|
17
|
+
*
|
|
18
|
+
* // Then use fetchers anywhere
|
|
19
|
+
* const users = await getUsers({ page: 1 })
|
|
20
|
+
*
|
|
21
|
+
* // With SWR
|
|
22
|
+
* const { data } = useSWR(['users', params], () => getUsers(params))
|
|
23
|
+
*
|
|
24
|
+
* // With React Query
|
|
25
|
+
* const { data } = useQuery(['users', params], () => getUsers(params))
|
|
26
|
+
*
|
|
27
|
+
* // In Server Component or SSR (pass custom client)
|
|
28
|
+
* import { API } from '../../index'
|
|
29
|
+
* const api = new API('https://api.example.com')
|
|
30
|
+
* const users = await getUsers({ page: 1 }, api)
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
import { consola } from 'consola'
|
|
34
|
+
import { getAPIInstance } from '../../api-instance'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* API operation
|
|
38
|
+
*
|
|
39
|
+
* @method DELETE
|
|
40
|
+
* @path /cfg/totp/devices/{id}/
|
|
41
|
+
*/
|
|
42
|
+
export async function deleteTotpDevicesDestroy( id: string, client?: any
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
const api = client || getAPIInstance()
|
|
45
|
+
const response = await api.totp.devicesDestroy(id)
|
|
46
|
+
return response
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
+
/**
|
|
3
|
+
* Typed fetchers for 2fa Management
|
|
4
|
+
*
|
|
5
|
+
* Universal functions that work in any environment:
|
|
6
|
+
* - Next.js (App Router / Pages Router / Server Components)
|
|
7
|
+
* - React Native
|
|
8
|
+
* - Node.js backend
|
|
9
|
+
*
|
|
10
|
+
* These fetchers use Zod schemas for runtime validation.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Configure API once (in your app entry point)
|
|
15
|
+
* import { configureAPI } from '../../api-instance'
|
|
16
|
+
* configureAPI({ baseUrl: 'https://api.example.com' })
|
|
17
|
+
*
|
|
18
|
+
* // Then use fetchers anywhere
|
|
19
|
+
* const users = await getUsers({ page: 1 })
|
|
20
|
+
*
|
|
21
|
+
* // With SWR
|
|
22
|
+
* const { data } = useSWR(['users', params], () => getUsers(params))
|
|
23
|
+
*
|
|
24
|
+
* // With React Query
|
|
25
|
+
* const { data } = useQuery(['users', params], () => getUsers(params))
|
|
26
|
+
*
|
|
27
|
+
* // In Server Component or SSR (pass custom client)
|
|
28
|
+
* import { API } from '../../index'
|
|
29
|
+
* const api = new API('https://api.example.com')
|
|
30
|
+
* const users = await getUsers({ page: 1 }, api)
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
import { consola } from 'consola'
|
|
34
|
+
import { DisableRequestSchema, type DisableRequest } from '../schemas/DisableRequest.schema'
|
|
35
|
+
import { PaginatedDeviceListListSchema, type PaginatedDeviceListList } from '../schemas/PaginatedDeviceListList.schema'
|
|
36
|
+
import { getAPIInstance } from '../../api-instance'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* API operation
|
|
40
|
+
*
|
|
41
|
+
* @method GET
|
|
42
|
+
* @path /cfg/totp/devices/
|
|
43
|
+
*/
|
|
44
|
+
export async function getTotpDevicesList( params?: { page?: number; page_size?: number }, client?: any
|
|
45
|
+
): Promise<PaginatedDeviceListList> {
|
|
46
|
+
const api = client || getAPIInstance()
|
|
47
|
+
const response = await api.2fa_management.totpDevicesList(params?.page, params?.page_size)
|
|
48
|
+
try {
|
|
49
|
+
return PaginatedDeviceListListSchema.parse(response)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
// Zod validation error - log detailed information
|
|
52
|
+
consola.error('❌ Zod Validation Failed');
|
|
53
|
+
consola.box(`getTotpDevicesList\nPath: /cfg/totp/devices/\nMethod: GET`);
|
|
54
|
+
|
|
55
|
+
if (error instanceof Error && 'issues' in error && Array.isArray((error as any).issues)) {
|
|
56
|
+
consola.error('Validation Issues:');
|
|
57
|
+
(error as any).issues.forEach((issue: any, index: number) => {
|
|
58
|
+
consola.error(` ${index + 1}. ${issue.path.join('.') || 'root'}`);
|
|
59
|
+
consola.error(` ├─ Message: ${issue.message}`);
|
|
60
|
+
if (issue.expected) consola.error(` ├─ Expected: ${issue.expected}`);
|
|
61
|
+
if (issue.received) consola.error(` └─ Received: ${issue.received}`);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
consola.error('Response data:', response);
|
|
66
|
+
|
|
67
|
+
// Dispatch browser CustomEvent (only if window is defined)
|
|
68
|
+
if (typeof window !== 'undefined' && error instanceof Error && 'issues' in error) {
|
|
69
|
+
try {
|
|
70
|
+
const event = new CustomEvent('zod-validation-error', {
|
|
71
|
+
detail: {
|
|
72
|
+
operation: 'getTotpDevicesList',
|
|
73
|
+
path: '/cfg/totp/devices/',
|
|
74
|
+
method: 'GET',
|
|
75
|
+
error: error,
|
|
76
|
+
response: response,
|
|
77
|
+
timestamp: new Date(),
|
|
78
|
+
},
|
|
79
|
+
bubbles: true,
|
|
80
|
+
cancelable: false,
|
|
81
|
+
});
|
|
82
|
+
window.dispatchEvent(event);
|
|
83
|
+
} catch (eventError) {
|
|
84
|
+
// Silently fail - event dispatch should never crash the app
|
|
85
|
+
consola.warn('Failed to dispatch validation error event:', eventError);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Re-throw the error
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* API operation
|
|
97
|
+
*
|
|
98
|
+
* @method POST
|
|
99
|
+
* @path /cfg/totp/disable/
|
|
100
|
+
*/
|
|
101
|
+
export async function createTotpDisableCreate( data: DisableRequest, client?: any
|
|
102
|
+
): Promise<any> {
|
|
103
|
+
const api = client || getAPIInstance()
|
|
104
|
+
const response = await api.2fa_management.totpDisableCreate(data)
|
|
105
|
+
return response
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|