@djangocfg/api 2.1.55 → 2.1.57

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 (93) hide show
  1. package/dist/auth.cjs +28 -15
  2. package/dist/auth.cjs.map +1 -1
  3. package/dist/auth.d.cts +6 -6
  4. package/dist/auth.d.ts +6 -6
  5. package/dist/auth.mjs +28 -15
  6. package/dist/auth.mjs.map +1 -1
  7. package/dist/clients.cjs +56 -17
  8. package/dist/clients.cjs.map +1 -1
  9. package/dist/clients.d.cts +17 -17
  10. package/dist/clients.d.ts +17 -17
  11. package/dist/clients.mjs +56 -17
  12. package/dist/clients.mjs.map +1 -1
  13. package/dist/hooks.cjs +763 -12
  14. package/dist/hooks.cjs.map +1 -1
  15. package/dist/hooks.d.cts +11 -11
  16. package/dist/hooks.d.ts +11 -11
  17. package/dist/hooks.mjs +763 -12
  18. package/dist/hooks.mjs.map +1 -1
  19. package/dist/index.cjs +893 -69
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +59 -41
  22. package/dist/index.d.ts +59 -41
  23. package/dist/index.mjs +893 -69
  24. package/dist/index.mjs.map +1 -1
  25. package/package.json +3 -3
  26. package/src/generated/cfg_accounts/_utils/schemas/CentrifugoToken.schema.ts +1 -1
  27. package/src/generated/cfg_accounts/_utils/schemas/OAuthAuthorizeRequestRequest.schema.ts +2 -2
  28. package/src/generated/cfg_accounts/_utils/schemas/OAuthAuthorizeResponse.schema.ts +1 -1
  29. package/src/generated/cfg_accounts/_utils/schemas/OAuthCallbackRequestRequest.schema.ts +1 -1
  30. package/src/generated/cfg_accounts/_utils/schemas/OAuthConnection.schema.ts +1 -1
  31. package/src/generated/cfg_accounts/_utils/schemas/OAuthProvidersResponse.schema.ts +1 -1
  32. package/src/generated/cfg_accounts/_utils/schemas/OAuthTokenResponse.schema.ts +1 -1
  33. package/src/generated/cfg_accounts/_utils/schemas/OTPRequestRequest.schema.ts +1 -1
  34. package/src/generated/cfg_accounts/_utils/schemas/OTPVerifyRequest.schema.ts +1 -1
  35. package/src/generated/cfg_accounts/_utils/schemas/User.schema.ts +1 -1
  36. package/src/generated/cfg_accounts/api-instance.ts +61 -13
  37. package/src/generated/cfg_centrifugo/api-instance.ts +61 -13
  38. package/src/generated/cfg_totp/CLAUDE.md +90 -0
  39. package/src/generated/cfg_totp/_utils/fetchers/index.ts +33 -0
  40. package/src/generated/cfg_totp/_utils/fetchers/totp.ts +49 -0
  41. package/src/generated/cfg_totp/_utils/fetchers/totp__2fa_management.ts +108 -0
  42. package/src/generated/cfg_totp/_utils/fetchers/totp__2fa_setup.ts +153 -0
  43. package/src/generated/cfg_totp/_utils/fetchers/totp__2fa_verification.ts +152 -0
  44. package/src/generated/cfg_totp/_utils/fetchers/totp__backup_codes.ts +152 -0
  45. package/src/generated/cfg_totp/_utils/hooks/index.ts +33 -0
  46. package/src/generated/cfg_totp/_utils/hooks/totp.ts +42 -0
  47. package/src/generated/cfg_totp/_utils/hooks/totp__2fa_management.ts +58 -0
  48. package/src/generated/cfg_totp/_utils/hooks/totp__2fa_setup.ts +63 -0
  49. package/src/generated/cfg_totp/_utils/hooks/totp__2fa_verification.ts +62 -0
  50. package/src/generated/cfg_totp/_utils/hooks/totp__backup_codes.ts +59 -0
  51. package/src/generated/cfg_totp/_utils/schemas/BackupCodesRegenerateRequest.schema.ts +19 -0
  52. package/src/generated/cfg_totp/_utils/schemas/BackupCodesRegenerateResponse.schema.ts +20 -0
  53. package/src/generated/cfg_totp/_utils/schemas/BackupCodesStatus.schema.ts +21 -0
  54. package/src/generated/cfg_totp/_utils/schemas/ConfirmSetupRequest.schema.ts +20 -0
  55. package/src/generated/cfg_totp/_utils/schemas/ConfirmSetupResponse.schema.ts +21 -0
  56. package/src/generated/cfg_totp/_utils/schemas/DeviceList.schema.ts +26 -0
  57. package/src/generated/cfg_totp/_utils/schemas/DisableRequest.schema.ts +19 -0
  58. package/src/generated/cfg_totp/_utils/schemas/PaginatedDeviceListList.schema.ts +24 -0
  59. package/src/generated/cfg_totp/_utils/schemas/SetupRequest.schema.ts +19 -0
  60. package/src/generated/cfg_totp/_utils/schemas/SetupResponse.schema.ts +23 -0
  61. package/src/generated/cfg_totp/_utils/schemas/VerifyBackupRequest.schema.ts +20 -0
  62. package/src/generated/cfg_totp/_utils/schemas/VerifyRequest.schema.ts +20 -0
  63. package/src/generated/cfg_totp/_utils/schemas/VerifyResponse.schema.ts +24 -0
  64. package/src/generated/cfg_totp/_utils/schemas/index.ts +32 -0
  65. package/src/generated/cfg_totp/api-instance.ts +180 -0
  66. package/src/generated/cfg_totp/client.ts +313 -0
  67. package/src/generated/cfg_totp/enums.ts +12 -0
  68. package/src/generated/cfg_totp/errors.ts +117 -0
  69. package/src/generated/cfg_totp/http.ts +104 -0
  70. package/src/generated/cfg_totp/index.ts +302 -0
  71. package/src/generated/cfg_totp/logger.ts +260 -0
  72. package/src/generated/cfg_totp/retry.ts +176 -0
  73. package/src/generated/cfg_totp/schema.json +859 -0
  74. package/src/generated/cfg_totp/storage.ts +162 -0
  75. package/src/generated/cfg_totp/totp/client.ts +23 -0
  76. package/src/generated/cfg_totp/totp/index.ts +3 -0
  77. package/src/generated/cfg_totp/totp/models.ts +1 -0
  78. package/src/generated/cfg_totp/totp__2fa_management/client.ts +41 -0
  79. package/src/generated/cfg_totp/totp__2fa_management/index.ts +3 -0
  80. package/src/generated/cfg_totp/totp__2fa_management/models.ts +60 -0
  81. package/src/generated/cfg_totp/totp__2fa_setup/client.ts +32 -0
  82. package/src/generated/cfg_totp/totp__2fa_setup/index.ts +3 -0
  83. package/src/generated/cfg_totp/totp__2fa_setup/models.ts +54 -0
  84. package/src/generated/cfg_totp/totp__2fa_verification/client.ts +32 -0
  85. package/src/generated/cfg_totp/totp__2fa_verification/index.ts +3 -0
  86. package/src/generated/cfg_totp/totp__2fa_verification/models.ts +44 -0
  87. package/src/generated/cfg_totp/totp__backup_codes/client.ts +31 -0
  88. package/src/generated/cfg_totp/totp__backup_codes/index.ts +3 -0
  89. package/src/generated/cfg_totp/totp__backup_codes/models.ts +37 -0
  90. package/src/generated/cfg_totp/validation-events.ts +134 -0
  91. package/src/generated/cfg_webpush/_utils/schemas/SendPushRequestRequest.schema.ts +2 -2
  92. package/src/generated/cfg_webpush/_utils/schemas/SubscribeRequestRequest.schema.ts +1 -1
  93. package/src/generated/cfg_webpush/api-instance.ts +61 -13
@@ -0,0 +1,62 @@
1
+ // Auto-generated by DjangoCFG - see CLAUDE.md
2
+ 'use client';
3
+
4
+ /**
5
+ * SWR Hooks for 2fa Verification
6
+ *
7
+ * React hooks powered by SWR for data fetching with automatic caching,
8
+ * revalidation, and optimistic updates.
9
+ *
10
+ * Usage:
11
+ * ```typescript
12
+ * // Query hooks (GET)
13
+ * const { data, error, isLoading } = useUsers({ page: 1 })
14
+ *
15
+ * // Mutation hooks (POST/PUT/PATCH/DELETE)
16
+ * const createUser = useCreateUser()
17
+ * await createUser({ name: 'John', email: 'john@example.com' })
18
+ * ```
19
+ */
20
+ import { useSWRConfig } from 'swr'
21
+ import * as Fetchers from '../fetchers/totp__2fa_verification'
22
+ import type { API } from '../../index'
23
+ import type { VerifyBackupRequest } from '../schemas/VerifyBackupRequest.schema'
24
+ import type { VerifyRequest } from '../schemas/VerifyRequest.schema'
25
+ import type { VerifyResponse } from '../schemas/VerifyResponse.schema'
26
+
27
+ /**
28
+ * API operation
29
+ *
30
+ * @method POST
31
+ * @path /cfg/totp/verify/
32
+ */
33
+ export function useCreateTotpVerifyCreate() {
34
+ const { mutate } = useSWRConfig()
35
+
36
+ return async (data: VerifyRequest, client?: API): Promise<VerifyResponse> => {
37
+ const result = await Fetchers.createTotpVerifyCreate(data, client)
38
+ // Revalidate related queries
39
+ mutate('cfg-totp-verify')
40
+ return result
41
+ }
42
+ }
43
+
44
+
45
+ /**
46
+ * API operation
47
+ *
48
+ * @method POST
49
+ * @path /cfg/totp/verify/backup/
50
+ */
51
+ export function useCreateTotpVerifyBackupCreate() {
52
+ const { mutate } = useSWRConfig()
53
+
54
+ return async (data: VerifyBackupRequest, client?: API): Promise<VerifyResponse> => {
55
+ const result = await Fetchers.createTotpVerifyBackupCreate(data, client)
56
+ // Revalidate related queries
57
+ mutate('cfg-totp-verify-backup')
58
+ return result
59
+ }
60
+ }
61
+
62
+
@@ -0,0 +1,59 @@
1
+ // Auto-generated by DjangoCFG - see CLAUDE.md
2
+ 'use client';
3
+
4
+ /**
5
+ * SWR Hooks for Backup Codes
6
+ *
7
+ * React hooks powered by SWR for data fetching with automatic caching,
8
+ * revalidation, and optimistic updates.
9
+ *
10
+ * Usage:
11
+ * ```typescript
12
+ * // Query hooks (GET)
13
+ * const { data, error, isLoading } = useUsers({ page: 1 })
14
+ *
15
+ * // Mutation hooks (POST/PUT/PATCH/DELETE)
16
+ * const createUser = useCreateUser()
17
+ * await createUser({ name: 'John', email: 'john@example.com' })
18
+ * ```
19
+ */
20
+ import useSWR from 'swr'
21
+ import { useSWRConfig } from 'swr'
22
+ import * as Fetchers from '../fetchers/totp__backup_codes'
23
+ import type { API } from '../../index'
24
+ import type { BackupCodesRegenerateRequest } from '../schemas/BackupCodesRegenerateRequest.schema'
25
+ import type { BackupCodesRegenerateResponse } from '../schemas/BackupCodesRegenerateResponse.schema'
26
+ import type { BackupCodesStatus } from '../schemas/BackupCodesStatus.schema'
27
+
28
+ /**
29
+ * API operation
30
+ *
31
+ * @method GET
32
+ * @path /cfg/totp/backup-codes/
33
+ */
34
+ export function useTotpBackupCodesRetrieve(client?: API): ReturnType<typeof useSWR<BackupCodesStatus>> {
35
+ return useSWR<BackupCodesStatus>(
36
+ 'cfg-totp-backup-code',
37
+ () => Fetchers.getTotpBackupCodesRetrieve(client)
38
+ )
39
+ }
40
+
41
+
42
+ /**
43
+ * API operation
44
+ *
45
+ * @method POST
46
+ * @path /cfg/totp/backup-codes/regenerate/
47
+ */
48
+ export function useCreateTotpBackupCodesRegenerateCreate() {
49
+ const { mutate } = useSWRConfig()
50
+
51
+ return async (data: BackupCodesRegenerateRequest, client?: API): Promise<BackupCodesRegenerateResponse> => {
52
+ const result = await Fetchers.createTotpBackupCodesRegenerateCreate(data, client)
53
+ // Revalidate related queries
54
+ mutate('cfg-totp-backup-codes-regenerate')
55
+ return result
56
+ }
57
+ }
58
+
59
+
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Zod schema for BackupCodesRegenerateRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for regenerating backup codes.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for regenerating backup codes.
11
+ */
12
+ export const BackupCodesRegenerateRequestSchema = z.object({
13
+ code: z.string().min(6).max(6),
14
+ })
15
+
16
+ /**
17
+ * Infer TypeScript type from Zod schema
18
+ */
19
+ export type BackupCodesRegenerateRequest = z.infer<typeof BackupCodesRegenerateRequestSchema>
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Zod schema for BackupCodesRegenerateResponse
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Response serializer for backup codes regeneration.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Response serializer for backup codes regeneration.
11
+ */
12
+ export const BackupCodesRegenerateResponseSchema = z.object({
13
+ backup_codes: z.array(z.string()),
14
+ warning: z.string(),
15
+ })
16
+
17
+ /**
18
+ * Infer TypeScript type from Zod schema
19
+ */
20
+ export type BackupCodesRegenerateResponse = z.infer<typeof BackupCodesRegenerateResponseSchema>
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Zod schema for BackupCodesStatus
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for backup codes status.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for backup codes status.
11
+ */
12
+ export const BackupCodesStatusSchema = z.object({
13
+ remaining_count: z.int(),
14
+ total_generated: z.int(),
15
+ warning: z.string().nullable().optional(),
16
+ })
17
+
18
+ /**
19
+ * Infer TypeScript type from Zod schema
20
+ */
21
+ export type BackupCodesStatus = z.infer<typeof BackupCodesStatusSchema>
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Zod schema for ConfirmSetupRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for confirming 2FA setup with first code.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for confirming 2FA setup with first code.
11
+ */
12
+ export const ConfirmSetupRequestSchema = z.object({
13
+ device_id: z.string().regex(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i),
14
+ code: z.string().min(6).max(6),
15
+ })
16
+
17
+ /**
18
+ * Infer TypeScript type from Zod schema
19
+ */
20
+ export type ConfirmSetupRequest = z.infer<typeof ConfirmSetupRequestSchema>
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Zod schema for ConfirmSetupResponse
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Response serializer for setup confirmation.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Response serializer for setup confirmation.
11
+ */
12
+ export const ConfirmSetupResponseSchema = z.object({
13
+ message: z.string(),
14
+ backup_codes: z.array(z.string()),
15
+ backup_codes_warning: z.string(),
16
+ })
17
+
18
+ /**
19
+ * Infer TypeScript type from Zod schema
20
+ */
21
+ export type ConfirmSetupResponse = z.infer<typeof ConfirmSetupResponseSchema>
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Zod schema for DeviceList
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for listing TOTP devices.
6
+ * */
7
+ import { z } from 'zod'
8
+ import * as Enums from '../../enums'
9
+
10
+ /**
11
+ * Serializer for listing TOTP devices.
12
+ */
13
+ export const DeviceListSchema = z.object({
14
+ id: z.int(),
15
+ name: z.string(),
16
+ is_primary: z.boolean(),
17
+ status: z.nativeEnum(Enums.DeviceListStatus),
18
+ created_at: z.iso.datetime(),
19
+ confirmed_at: z.iso.datetime().nullable(),
20
+ last_used_at: z.iso.datetime().nullable(),
21
+ })
22
+
23
+ /**
24
+ * Infer TypeScript type from Zod schema
25
+ */
26
+ export type DeviceList = z.infer<typeof DeviceListSchema>
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Zod schema for DisableRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for completely disabling 2FA.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for completely disabling 2FA.
11
+ */
12
+ export const DisableRequestSchema = z.object({
13
+ code: z.string().min(6).max(6),
14
+ })
15
+
16
+ /**
17
+ * Infer TypeScript type from Zod schema
18
+ */
19
+ export type DisableRequest = z.infer<typeof DisableRequestSchema>
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Zod schema for PaginatedDeviceListList
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * */
6
+ import { z } from 'zod'
7
+ import { DeviceListSchema } from './DeviceList.schema'
8
+
9
+ export const PaginatedDeviceListListSchema = z.object({
10
+ count: z.int(),
11
+ page: z.int(),
12
+ pages: z.int(),
13
+ page_size: z.int(),
14
+ has_next: z.boolean(),
15
+ has_previous: z.boolean(),
16
+ next_page: z.int().nullable().optional(),
17
+ previous_page: z.int().nullable().optional(),
18
+ results: z.array(DeviceListSchema),
19
+ })
20
+
21
+ /**
22
+ * Infer TypeScript type from Zod schema
23
+ */
24
+ export type PaginatedDeviceListList = z.infer<typeof PaginatedDeviceListListSchema>
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Zod schema for SetupRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for starting 2FA setup.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for starting 2FA setup.
11
+ */
12
+ export const SetupRequestSchema = z.object({
13
+ device_name: z.string().min(1).max(100).optional(),
14
+ })
15
+
16
+ /**
17
+ * Infer TypeScript type from Zod schema
18
+ */
19
+ export type SetupRequest = z.infer<typeof SetupRequestSchema>
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Zod schema for SetupResponse
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Response serializer for setup initiation.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Response serializer for setup initiation.
11
+ */
12
+ export const SetupResponseSchema = z.object({
13
+ device_id: z.string().regex(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i),
14
+ secret: z.string(),
15
+ provisioning_uri: z.string(),
16
+ qr_code_base64: z.string(),
17
+ expires_in: z.int(),
18
+ })
19
+
20
+ /**
21
+ * Infer TypeScript type from Zod schema
22
+ */
23
+ export type SetupResponse = z.infer<typeof SetupResponseSchema>
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Zod schema for VerifyBackupRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for backup code verification during login.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for backup code verification during login.
11
+ */
12
+ export const VerifyBackupRequestSchema = z.object({
13
+ session_id: z.string().regex(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i),
14
+ backup_code: z.string().min(8).max(8),
15
+ })
16
+
17
+ /**
18
+ * Infer TypeScript type from Zod schema
19
+ */
20
+ export type VerifyBackupRequest = z.infer<typeof VerifyBackupRequestSchema>
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Zod schema for VerifyRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Serializer for TOTP code verification during login.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Serializer for TOTP code verification during login.
11
+ */
12
+ export const VerifyRequestSchema = z.object({
13
+ session_id: z.string().regex(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i),
14
+ code: z.string().min(6).max(6),
15
+ })
16
+
17
+ /**
18
+ * Infer TypeScript type from Zod schema
19
+ */
20
+ export type VerifyRequest = z.infer<typeof VerifyRequestSchema>
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Zod schema for VerifyResponse
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * * Response serializer for successful 2FA verification.
6
+ * */
7
+ import { z } from 'zod'
8
+
9
+ /**
10
+ * Response serializer for successful 2FA verification.
11
+ */
12
+ export const VerifyResponseSchema = z.object({
13
+ message: z.string(),
14
+ access_token: z.string(),
15
+ refresh_token: z.string(),
16
+ user: z.record(z.string(), z.any()),
17
+ remaining_backup_codes: z.int().optional(),
18
+ warning: z.string().optional(),
19
+ })
20
+
21
+ /**
22
+ * Infer TypeScript type from Zod schema
23
+ */
24
+ export type VerifyResponse = z.infer<typeof VerifyResponseSchema>
@@ -0,0 +1,32 @@
1
+ // Auto-generated by DjangoCFG - see CLAUDE.md
2
+ /**
3
+ * Zod Schemas - Runtime validation and type inference
4
+ *
5
+ * Auto-generated from OpenAPI specification.
6
+ * Provides runtime validation for API requests and responses.
7
+ *
8
+ * Usage:
9
+ * ```typescript
10
+ * import { UserSchema } from './schemas'
11
+ *
12
+ * // Validate data
13
+ * const user = UserSchema.parse(data)
14
+ *
15
+ * // Type inference
16
+ * type User = z.infer<typeof UserSchema>
17
+ * ```
18
+ */
19
+
20
+ export * from './BackupCodesRegenerateRequest.schema'
21
+ export * from './BackupCodesRegenerateResponse.schema'
22
+ export * from './BackupCodesStatus.schema'
23
+ export * from './ConfirmSetupRequest.schema'
24
+ export * from './ConfirmSetupResponse.schema'
25
+ export * from './DeviceList.schema'
26
+ export * from './DisableRequest.schema'
27
+ export * from './PaginatedDeviceListList.schema'
28
+ export * from './SetupRequest.schema'
29
+ export * from './SetupResponse.schema'
30
+ export * from './VerifyBackupRequest.schema'
31
+ export * from './VerifyRequest.schema'
32
+ export * from './VerifyResponse.schema'
@@ -0,0 +1,180 @@
1
+ // Auto-generated by DjangoCFG - see CLAUDE.md
2
+ /**
3
+ * Global API Instance - Singleton configuration with auto-configuration support
4
+ *
5
+ * This module provides a global API instance that auto-configures from
6
+ * environment variables or can be configured manually.
7
+ *
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:
22
+ * ```typescript
23
+ * import { configureAPI } from './api-instance'
24
+ *
25
+ * configureAPI({
26
+ * baseUrl: 'https://api.example.com',
27
+ * token: 'your-jwt-token'
28
+ * })
29
+ * ```
30
+ *
31
+ * For SSR or multiple instances:
32
+ * ```typescript
33
+ * import { API } from './index'
34
+ * import { getUsers } from './_utils/fetchers'
35
+ *
36
+ * const api = new API('https://api.example.com')
37
+ * const users = await getUsers({ page: 1 }, api)
38
+ * ```
39
+ */
40
+
41
+ import { API, type APIOptions } from './index'
42
+
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
+ }
78
+
79
+ /**
80
+ * Get the global API instance
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
83
+ */
84
+ export function getAPIInstance(): API {
85
+ // Try auto-configuration on first access (lazy initialization)
86
+ tryAutoConfigureFromEnv()
87
+
88
+ if (!globalAPI) {
89
+ throw new Error(
90
+ 'API not configured. Call configureAPI() with your base URL before using fetchers or hooks.\n\n' +
91
+ 'Example:\n' +
92
+ ' import { configureAPI } from "./api-instance"\n' +
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'
95
+ )
96
+ }
97
+ return globalAPI
98
+ }
99
+
100
+ /**
101
+ * Check if API is configured (or can be auto-configured)
102
+ */
103
+ export function isAPIConfigured(): boolean {
104
+ tryAutoConfigureFromEnv()
105
+ return globalAPI !== null
106
+ }
107
+
108
+ /**
109
+ * Configure the global API instance
110
+ *
111
+ * @param baseUrl - Base URL for the API
112
+ * @param options - Optional configuration (storage, retry, logger)
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * configureAPI({
117
+ * baseUrl: 'https://api.example.com',
118
+ * token: 'jwt-token',
119
+ * options: {
120
+ * retryConfig: { maxRetries: 3 },
121
+ * loggerConfig: { enabled: true }
122
+ * }
123
+ * })
124
+ * ```
125
+ */
126
+ export function configureAPI(config: {
127
+ baseUrl: string
128
+ token?: string
129
+ refreshToken?: string
130
+ options?: APIOptions
131
+ }): API {
132
+ globalAPI = new API(config.baseUrl, config.options)
133
+
134
+ if (config.token) {
135
+ globalAPI.setToken(config.token, config.refreshToken)
136
+ }
137
+
138
+ return globalAPI
139
+ }
140
+
141
+ /**
142
+ * Reconfigure the global API instance with new settings
143
+ * Useful for updating tokens or base URL
144
+ */
145
+ export function reconfigureAPI(updates: {
146
+ baseUrl?: string
147
+ token?: string
148
+ refreshToken?: string
149
+ }): API {
150
+ const instance = getAPIInstance()
151
+
152
+ if (updates.baseUrl) {
153
+ instance.setBaseUrl(updates.baseUrl)
154
+ }
155
+
156
+ if (updates.token) {
157
+ instance.setToken(updates.token, updates.refreshToken)
158
+ }
159
+
160
+ return instance
161
+ }
162
+
163
+ /**
164
+ * Clear tokens from the global API instance
165
+ */
166
+ export function clearAPITokens(): void {
167
+ const instance = getAPIInstance()
168
+ instance.clearTokens()
169
+ }
170
+
171
+ /**
172
+ * Reset the global API instance
173
+ * Useful for testing or logout scenarios
174
+ */
175
+ export function resetAPI(): void {
176
+ if (globalAPI) {
177
+ globalAPI.clearTokens()
178
+ }
179
+ globalAPI = null
180
+ }