@djangocfg/api 2.1.356 → 2.1.358

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 (113) hide show
  1. package/README.md +53 -632
  2. package/dist/auth-server.cjs +717 -200
  3. package/dist/auth-server.cjs.map +1 -1
  4. package/dist/auth-server.mjs +717 -200
  5. package/dist/auth-server.mjs.map +1 -1
  6. package/dist/auth.cjs +843 -783
  7. package/dist/auth.cjs.map +1 -1
  8. package/dist/auth.d.cts +58 -37
  9. package/dist/auth.d.ts +58 -37
  10. package/dist/auth.mjs +846 -784
  11. package/dist/auth.mjs.map +1 -1
  12. package/dist/clients.cjs +1427 -0
  13. package/dist/clients.cjs.map +1 -1
  14. package/dist/clients.d.cts +1686 -0
  15. package/dist/clients.d.ts +1686 -0
  16. package/dist/clients.mjs +1427 -0
  17. package/dist/clients.mjs.map +1 -1
  18. package/dist/hooks.cjs +1277 -1924
  19. package/dist/hooks.cjs.map +1 -1
  20. package/dist/hooks.d.cts +18 -1206
  21. package/dist/hooks.d.ts +18 -1206
  22. package/dist/hooks.mjs +1253 -1900
  23. package/dist/hooks.mjs.map +1 -1
  24. package/dist/index.cjs +545 -1232
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +1389 -1455
  27. package/dist/index.d.ts +1389 -1455
  28. package/dist/index.mjs +545 -1232
  29. package/dist/index.mjs.map +1 -1
  30. package/package.json +7 -2
  31. package/src/_api/generated/_cfg_accounts/api.ts +11 -3
  32. package/src/_api/generated/_cfg_accounts/hooks/index.ts +3 -0
  33. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsApiKeyRegenerateCreate.ts +64 -0
  34. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsApiKeyRetrieve.ts +65 -0
  35. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsApiKeyTestCreate.ts +64 -0
  36. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOauthConnectionsList.ts +43 -3
  37. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOauthDisconnectCreate.ts +43 -3
  38. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOauthGithubAuthorizeCreate.ts +43 -3
  39. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOauthGithubCallbackCreate.ts +43 -3
  40. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOauthProvidersRetrieve.ts +43 -3
  41. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOtpRequestCreate.ts +43 -3
  42. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsOtpVerifyCreate.ts +43 -3
  43. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfileAvatarCreate.ts +43 -3
  44. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfileDeleteCreate.ts +43 -3
  45. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfilePartialPartialUpdate.ts +43 -3
  46. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfilePartialUpdate.ts +43 -3
  47. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfileRetrieve.ts +43 -3
  48. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfileUpdatePartialUpdate.ts +43 -3
  49. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsProfileUpdateUpdate.ts +43 -3
  50. package/src/_api/generated/_cfg_accounts/hooks/useCfgAccountsTokenRefreshCreate.ts +43 -3
  51. package/src/_api/generated/_cfg_accounts/index.ts +1 -1
  52. package/src/_api/generated/_cfg_accounts/schemas/APIKey.ts +13 -0
  53. package/src/_api/generated/_cfg_accounts/schemas/APIKeyRegenerate.ts +12 -0
  54. package/src/_api/generated/_cfg_accounts/schemas/APIKeyRequest.ts +13 -0
  55. package/src/_api/generated/{_cfg_totp/schemas/DeviceListStatusEnum.ts → _cfg_accounts/schemas/APIKeyTestRequest.ts} +4 -2
  56. package/src/_api/generated/_cfg_accounts/schemas/APIKeyTestResult.ts +12 -0
  57. package/src/_api/generated/_cfg_accounts/schemas/AccountDeleteResponse.ts +1 -1
  58. package/src/_api/generated/_cfg_accounts/schemas/CentrifugoToken.ts +2 -2
  59. package/src/_api/generated/_cfg_accounts/schemas/CfgUserUpdateRequest.ts +3 -2
  60. package/src/_api/generated/_cfg_accounts/schemas/OAuthCallbackRequestRequest.ts +1 -1
  61. package/src/_api/generated/_cfg_accounts/schemas/OAuthConnection.ts +6 -6
  62. package/src/_api/generated/_cfg_accounts/schemas/OAuthDisconnectRequestRequest.ts +2 -2
  63. package/src/_api/generated/_cfg_accounts/schemas/{ProviderEnum.ts → OAuthProviderEnum.ts} +2 -2
  64. package/src/_api/generated/_cfg_accounts/schemas/OAuthTokenResponse.ts +5 -5
  65. package/src/_api/generated/_cfg_accounts/schemas/OTPVerifyRequest.ts +1 -1
  66. package/src/_api/generated/_cfg_accounts/schemas/OTPVerifyResponse.ts +3 -3
  67. package/src/_api/generated/_cfg_accounts/schemas/PatchedCfgUserUpdateRequest.ts +3 -2
  68. package/src/_api/generated/_cfg_accounts/schemas/User.ts +12 -10
  69. package/src/_api/generated/_cfg_accounts/schemas/cfg_accounts_oauth_connections_response_200_AutoRef.ts +10 -0
  70. package/src/_api/generated/_cfg_accounts/schemas/cfg_accounts_oauth_disconnect_response_200_AutoRef.ts +11 -0
  71. package/src/_api/generated/_cfg_accounts/schemas/index.ts +8 -1
  72. package/src/_api/generated/_cfg_centrifugo/api.ts +3 -3
  73. package/src/_api/generated/_cfg_centrifugo/hooks/useCfgCentrifugoAuthTokenRetrieve.ts +43 -3
  74. package/src/_api/generated/_cfg_centrifugo/index.ts +1 -1
  75. package/src/_api/generated/_cfg_centrifugo/schemas/ConnectionTokenResponse.ts +2 -2
  76. package/src/_api/generated/_cfg_totp/api.ts +9 -3
  77. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpBackupCodesRegenerateCreate.ts +43 -3
  78. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpBackupCodesRetrieve.ts +43 -3
  79. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpDevicesDestroy.ts +7 -3
  80. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpDevicesRetrieve.ts +43 -3
  81. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpDisableCreate.ts +43 -3
  82. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpSetupConfirmCreate.ts +43 -3
  83. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpSetupCreate.ts +43 -3
  84. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpVerifyBackupCreate.ts +43 -3
  85. package/src/_api/generated/_cfg_totp/hooks/useCfgTotpVerifyCreate.ts +43 -3
  86. package/src/_api/generated/_cfg_totp/index.ts +1 -1
  87. package/src/_api/generated/_cfg_totp/schemas/ConfirmSetupRequest.ts +1 -1
  88. package/src/_api/generated/_cfg_totp/schemas/ConfirmSetupResponse.ts +1 -1
  89. package/src/_api/generated/_cfg_totp/schemas/DeviceList.ts +5 -5
  90. package/src/_api/generated/_cfg_totp/schemas/DeviceStatusEnum.ts +9 -0
  91. package/src/_api/generated/_cfg_totp/schemas/SetupResponse.ts +2 -2
  92. package/src/_api/generated/_cfg_totp/schemas/TotpVerifyUser.ts +11 -9
  93. package/src/_api/generated/_cfg_totp/schemas/VerifyBackupRequest.ts +1 -1
  94. package/src/_api/generated/_cfg_totp/schemas/VerifyRequest.ts +1 -1
  95. package/src/_api/generated/_cfg_totp/schemas/VerifyResponse.ts +2 -2
  96. package/src/_api/generated/_cfg_totp/schemas/cfg_totp_disable_response_200_AutoRef.ts +9 -0
  97. package/src/_api/generated/_cfg_totp/schemas/index.ts +2 -1
  98. package/src/_api/generated/index.ts +3 -1
  99. package/src/_api/generated/sdk.gen.ts +80 -813
  100. package/src/_api/generated/types.gen.ts +320 -159
  101. package/src/auth/context/AccountsContext.tsx +18 -6
  102. package/src/auth/context/AuthContext.tsx +11 -3
  103. package/src/auth/hooks/useDeleteAccount.ts +2 -2
  104. package/src/auth/hooks/useGithubAuth.ts +3 -3
  105. package/src/auth/hooks/useTokenRefresh.ts +2 -2
  106. package/src/auth/hooks/useTwoFactor.ts +3 -3
  107. package/src/auth/hooks/useTwoFactorSetup.ts +3 -3
  108. package/src/auth/hooks/useTwoFactorStatus.ts +3 -3
  109. package/src/auth/middlewares/tokenRefresh.ts +2 -2
  110. package/src/hooks/index.ts +1 -0
  111. package/src/hooks/useApiKey.ts +73 -0
  112. package/src/index.ts +12 -16
  113. package/src/lib/env.ts +10 -0
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Accounts Context
2
+ * CfgAccounts Context
3
3
  *
4
4
  * Manages user authentication and profile operations using generated SWR hooks.
5
5
  *
@@ -14,11 +14,11 @@
14
14
  "use client";
15
15
 
16
16
  import {
17
- createContext, ReactNode, useCallback, useContext, useEffect, useRef, useState
17
+ createContext, memo, ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState
18
18
  } from 'react';
19
19
 
20
20
  import { CfgAccountsApi } from '../../';
21
- import { Accounts, Auth, UserProfile } from '../../_api/generated/_cfg_accounts/sdk.gen';
21
+ import { CfgAccountsProfile } from '../../_api/generated/_cfg_accounts/sdk.gen';
22
22
  import {
23
23
  useCfgAccountsOtpRequestCreate,
24
24
  useCfgAccountsOtpVerifyCreate,
@@ -111,7 +111,7 @@ export function AccountsProvider({ children }: AccountsProviderProps) {
111
111
  setProfileError(null);
112
112
  try {
113
113
  authLogger.debug(`Fetching profile from API (caller: ${callerId}, force: ${force})`);
114
- const res = await UserProfile.cfgAccountsProfileRetrieve({ throwOnError: true });
114
+ const res = await CfgAccountsProfile.cfgAccountsProfileRetrieve({ throwOnError: true });
115
115
  const result = res.data;
116
116
  setProfile(result);
117
117
  profileRef.current = result;
@@ -190,7 +190,7 @@ export function AccountsProvider({ children }: AccountsProviderProps) {
190
190
  clearProfileCache();
191
191
  }, []);
192
192
 
193
- const value: AccountsContextValue = {
193
+ const value: AccountsContextValue = useMemo(() => ({
194
194
  profile,
195
195
  isLoadingProfile,
196
196
  profileError,
@@ -202,7 +202,19 @@ export function AccountsProvider({ children }: AccountsProviderProps) {
202
202
  verifyOTP,
203
203
  refreshToken,
204
204
  logout,
205
- };
205
+ }), [
206
+ profile,
207
+ isLoadingProfile,
208
+ profileError,
209
+ updateProfile,
210
+ partialUpdateProfile,
211
+ uploadAvatar,
212
+ refreshProfile,
213
+ requestOTP,
214
+ verifyOTP,
215
+ refreshToken,
216
+ logout,
217
+ ]);
206
218
 
207
219
  return (
208
220
  <AccountsContext.Provider value={value}>
@@ -4,7 +4,7 @@
4
4
  import { isDev, isBrowser } from "../utils/env";
5
5
  import { usePathname } from 'next/navigation';
6
6
  import React, {
7
- createContext, ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState
7
+ createContext, memo, ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState
8
8
  } from 'react';
9
9
  import { SWRConfig } from 'swr';
10
10
 
@@ -629,7 +629,13 @@ const AuthProviderInternal: React.FC<AuthProviderProps> = ({ children, config })
629
629
  };
630
630
 
631
631
  // Wrapper that provides AccountsContext
632
- export const AuthProvider: React.FC<AuthProviderProps> = ({ children, config }) => {
632
+ /**
633
+ * AuthProvider — wraps AccountsProvider + AuthProviderInternal.
634
+ *
635
+ * Memoised: re-renders only when `config` reference or `children` change.
636
+ * Internal auth state is isolated inside AuthProviderInternal.
637
+ */
638
+ function AuthProviderRaw({ children, config }: AuthProviderProps) {
633
639
  return (
634
640
  <AccountsProvider>
635
641
  <AuthProviderInternal config={config}>
@@ -637,7 +643,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children, config })
637
643
  </AuthProviderInternal>
638
644
  </AccountsProvider>
639
645
  );
640
- };
646
+ }
647
+
648
+ export const AuthProvider = memo(AuthProviderRaw);
641
649
 
642
650
  /**
643
651
  * Default auth state for SSR/outside provider
@@ -3,7 +3,7 @@
3
3
  import { useCallback, useState } from 'react';
4
4
 
5
5
  import { apiAccounts } from '../../clients';
6
- import { UserProfile } from '../../_api/generated/sdk.gen';
6
+ import { CfgAccountsProfile } from '../../_api/generated/sdk.gen';
7
7
  import { useAuth } from '../context';
8
8
  import { authLogger } from '../utils/logger';
9
9
 
@@ -61,7 +61,7 @@ export const useDeleteAccount = (): UseDeleteAccountReturn => {
61
61
  try {
62
62
  authLogger.info('Deleting account...');
63
63
 
64
- const result = await UserProfile.cfgAccountsProfileDeleteCreate({ throwOnError: true });
64
+ const result = await CfgAccountsProfile.cfgAccountsProfileDeleteCreate({ throwOnError: true });
65
65
  const response = result.data;
66
66
 
67
67
  if (!response.success) {
@@ -3,7 +3,7 @@
3
3
  import { useCallback, useState } from 'react';
4
4
 
5
5
  import { apiAccounts } from '../../clients';
6
- import { OAuth } from '../../_api/generated/sdk.gen';
6
+ import { CfgAccountsOauth } from '../../_api/generated/sdk.gen';
7
7
  import { Analytics, AnalyticsCategory, AnalyticsEvent } from '../utils/analytics';
8
8
  import { authLogger } from '../utils/logger';
9
9
  import { useCfgRouter } from './useCfgRouter';
@@ -70,7 +70,7 @@ export const useGithubAuth = (options: UseGithubAuthOptions = {}): UseGithubAuth
70
70
 
71
71
  // Call API to get authorization URL
72
72
  // The API will auto-generate redirect_uri from config if not provided
73
- const result = await OAuth.cfgAccountsOauthGithubAuthorizeCreate({
73
+ const result = await CfgAccountsOauth.cfgAccountsOauthGithubAuthorizeCreate({
74
74
  body: {
75
75
  source_url: sourceUrl || (typeof window !== 'undefined' ? window.location.href : ''),
76
76
  },
@@ -131,7 +131,7 @@ export const useGithubAuth = (options: UseGithubAuthOptions = {}): UseGithubAuth
131
131
  sessionStorage.removeItem('oauth_provider');
132
132
  }
133
133
 
134
- const result = await OAuth.cfgAccountsOauthGithubCallbackCreate({
134
+ const result = await CfgAccountsOauth.cfgAccountsOauthGithubCallbackCreate({
135
135
  body: { code, state },
136
136
  throwOnError: true,
137
137
  });
@@ -12,7 +12,7 @@
12
12
  import { useCallback, useEffect, useRef } from 'react';
13
13
 
14
14
  import { api as apiAccounts } from '../../';
15
- import { Auth } from '../../_api/generated/sdk.gen';
15
+ import { CfgAccountsAuth } from '../../_api/generated/sdk.gen';
16
16
  import { authLogger } from '../utils/logger';
17
17
 
18
18
  // Configuration
@@ -78,7 +78,7 @@ export function useTokenRefresh(options: UseTokenRefreshOptions = {}) {
78
78
 
79
79
  try {
80
80
  // Use generated API client with correct URL (/cfg/accounts/token/refresh/)
81
- const result = await Auth.cfgAccountsTokenRefreshCreate({
81
+ const result = await CfgAccountsAuth.cfgAccountsTokenRefreshCreate({
82
82
  body: { refresh: refreshTokenValue },
83
83
  throwOnError: true,
84
84
  });
@@ -4,7 +4,7 @@ import { useCallback, useState } from 'react';
4
4
 
5
5
  import { apiAccounts, apiTotp } from '../../clients';
6
6
  import { APIError } from '../../_api/generated/helpers';
7
- import { TotpVerification } from '../../_api/generated/sdk.gen';
7
+ import { CfgTotp, CfgTotpVerify } from '../../_api/generated/sdk.gen';
8
8
  import { Analytics, AnalyticsCategory, AnalyticsEvent } from '../utils/analytics';
9
9
  import { authLogger } from '../utils/logger';
10
10
  import { useCfgRouter } from './useCfgRouter';
@@ -148,7 +148,7 @@ export const useTwoFactor = (options: UseTwoFactorOptions = {}): UseTwoFactorRet
148
148
  try {
149
149
  authLogger.info('Verifying TOTP code...');
150
150
 
151
- const result = await TotpVerification.cfgTotpVerifyCreate({
151
+ const result = await CfgTotpVerify.cfgTotpVerifyCreate({
152
152
  body: { session_id: sessionId, code },
153
153
  throwOnError: true,
154
154
  });
@@ -208,7 +208,7 @@ export const useTwoFactor = (options: UseTwoFactorOptions = {}): UseTwoFactorRet
208
208
  try {
209
209
  authLogger.info('Verifying backup code...');
210
210
 
211
- const result = await TotpVerification.cfgTotpVerifyBackupCreate({
211
+ const result = await CfgTotpVerify.cfgTotpVerifyBackupCreate({
212
212
  body: {
213
213
  session_id: sessionId,
214
214
  backup_code: backupCode.replace(/\s+/g, ''),
@@ -3,7 +3,7 @@
3
3
  import { useCallback, useState } from 'react';
4
4
 
5
5
  import { apiTotp } from '../../clients';
6
- import { TotpSetup } from '../../_api/generated/sdk.gen';
6
+ import { CfgTotpSetup } from '../../_api/generated/sdk.gen';
7
7
  import { authLogger } from '../utils/logger';
8
8
 
9
9
  export interface TwoFactorSetupData {
@@ -116,7 +116,7 @@ export const useTwoFactorSetup = (options: UseTwoFactorSetupOptions = {}): UseTw
116
116
  try {
117
117
  authLogger.info('Starting 2FA setup...');
118
118
 
119
- const result = await TotpSetup.cfgTotpSetupCreate({
119
+ const result = await CfgTotpSetup.cfgTotpSetupCreate({
120
120
  body: { device_name: deviceName },
121
121
  throwOnError: true,
122
122
  });
@@ -172,7 +172,7 @@ export const useTwoFactorSetup = (options: UseTwoFactorSetupOptions = {}): UseTw
172
172
  try {
173
173
  authLogger.info('Confirming 2FA setup...');
174
174
 
175
- const result = await TotpSetup.cfgTotpSetupConfirmCreate({
175
+ const result = await CfgTotpSetup.cfgTotpSetupConfirmCreate({
176
176
  body: { device_id: setupData.deviceId, code },
177
177
  throwOnError: true,
178
178
  });
@@ -4,7 +4,7 @@ import { useCallback, useState } from 'react';
4
4
 
5
5
  import { apiTotp } from '../../clients';
6
6
  import { APIError } from '../../_api/generated/helpers';
7
- import { TotpManagement } from '../../_api/generated/sdk.gen';
7
+ import { CfgTotp } from '../../_api/generated/sdk.gen';
8
8
  import { authLogger } from '../utils/logger';
9
9
 
10
10
  export interface TwoFactorDevice {
@@ -83,7 +83,7 @@ export const useTwoFactorStatus = (): UseTwoFactorStatusReturn => {
83
83
  authLogger.info('Fetching 2FA status...');
84
84
 
85
85
  // API returns { devices: [...], has_2fa_enabled: bool }
86
- const result = await TotpManagement.cfgTotpDevicesRetrieve({ throwOnError: true });
86
+ const result = await CfgTotp.cfgTotpDevicesRetrieve({ throwOnError: true });
87
87
  const response = result.data;
88
88
 
89
89
  const mappedDevices: TwoFactorDevice[] = response.devices.map((device) => ({
@@ -123,7 +123,7 @@ export const useTwoFactorStatus = (): UseTwoFactorStatusReturn => {
123
123
  try {
124
124
  authLogger.info('Disabling 2FA...');
125
125
 
126
- await TotpManagement.cfgTotpDisableCreate({ body: { code }, throwOnError: true });
126
+ await CfgTotp.cfgTotpDisableCreate({ body: { code }, throwOnError: true });
127
127
 
128
128
  setHas2FAEnabled(false);
129
129
  setDevices([]);
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import { api as apiAccounts } from '../../';
9
- import { Auth } from '../../_api/generated/sdk.gen';
9
+ import { CfgAccountsAuth } from '../../_api/generated/sdk.gen';
10
10
  import { authLogger } from '../utils/logger';
11
11
 
12
12
  // Refresh state management
@@ -52,7 +52,7 @@ export async function refreshAccessToken(): Promise<string | null> {
52
52
  }
53
53
 
54
54
  // Use generated API client with correct URL (/cfg/accounts/token/refresh/)
55
- const result = await Auth.cfgAccountsTokenRefreshCreate({
55
+ const result = await CfgAccountsAuth.cfgAccountsTokenRefreshCreate({
56
56
  body: { refresh: refreshToken },
57
57
  throwOnError: true,
58
58
  });
@@ -0,0 +1 @@
1
+ export { useApiKey, type UseApiKeyReturn } from './useApiKey';
@@ -0,0 +1,73 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useState } from 'react';
4
+
5
+ import { useCfgAccountsApiKeyRegenerateCreate } from '../_api/generated/_cfg_accounts/hooks/useCfgAccountsApiKeyRegenerateCreate';
6
+ import { useCfgAccountsApiKeyRetrieve } from '../_api/generated/_cfg_accounts/hooks/useCfgAccountsApiKeyRetrieve';
7
+ import { useCfgAccountsApiKeyTestCreate } from '../_api/generated/_cfg_accounts/hooks/useCfgAccountsApiKeyTestCreate';
8
+
9
+ export interface ApiKeyTestResult {
10
+ valid: boolean;
11
+ userId: string | null;
12
+ }
13
+
14
+ export interface UseApiKeyReturn {
15
+ apiKey: string | null;
16
+ reissuedAt: string | null;
17
+ createdAt: string | null;
18
+ isLoading: boolean;
19
+ isRegenerating: boolean;
20
+ isTesting: boolean;
21
+ regenerate: () => Promise<void>;
22
+ testKey: (key: string) => Promise<ApiKeyTestResult>;
23
+ refresh: () => void;
24
+ }
25
+
26
+ export function useApiKey(): UseApiKeyReturn {
27
+ const { data, isLoading, mutate } = useCfgAccountsApiKeyRetrieve();
28
+ const { trigger: regenerateTrigger, isMutating: isRegenerating } = useCfgAccountsApiKeyRegenerateCreate();
29
+ const { trigger: testTrigger, isMutating: isTesting } = useCfgAccountsApiKeyTestCreate();
30
+
31
+ // After regenerate, the POST response contains the FULL key (shown once).
32
+ // We store it separately so the user can copy it before we refetch.
33
+ const [freshKey, setFreshKey] = useState<string | null>(null);
34
+
35
+ const apiKey = freshKey ?? (data?.key ?? null);
36
+ const reissuedAt = (data as Record<string, unknown> | undefined)?.reissued_at as string | null ?? null;
37
+ const createdAt = (data as Record<string, unknown> | undefined)?.created_at as string | null ?? null;
38
+
39
+ const regenerate = useCallback(async () => {
40
+ const result = await regenerateTrigger({} as never);
41
+ // The POST response has the full key — surface it immediately.
42
+ const newKey = (result as Record<string, unknown> | undefined)?.key as string | undefined;
43
+ if (newKey) {
44
+ setFreshKey(newKey);
45
+ }
46
+ }, [regenerateTrigger]);
47
+
48
+ const testKey = useCallback(async (key: string): Promise<ApiKeyTestResult> => {
49
+ const result = await testTrigger({ body: { key } } as never);
50
+ const record = result as Record<string, unknown> | undefined;
51
+ return {
52
+ valid: Boolean(record?.valid),
53
+ userId: (record?.user_id as string | null) ?? null,
54
+ };
55
+ }, [testTrigger]);
56
+
57
+ const refresh = useCallback(() => {
58
+ setFreshKey(null);
59
+ mutate();
60
+ }, [mutate]);
61
+
62
+ return {
63
+ apiKey,
64
+ reissuedAt,
65
+ createdAt,
66
+ isLoading,
67
+ isRegenerating,
68
+ isTesting,
69
+ regenerate,
70
+ testKey,
71
+ refresh,
72
+ };
73
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * @djangocfg/api — package entry point.
3
3
  *
4
- * Per-group API singletons (`cfgAccountsApi`, `cfgTotpApi`, `cfgCentrifugoApi`)
5
- * + API classes + shared utilities (LocalStorageAdapter / APIError / ...).
4
+ * Per-group API singletons + API classes + shared utilities.
6
5
  *
7
6
  * Usage:
8
7
  * import { cfgAccountsApi, AccountsTypes } from '@djangocfg/api';
@@ -11,21 +10,18 @@
11
10
 
12
11
  export * from './_api/generated';
13
12
 
14
- // Hey API SDK classes (one per OpenAPI tag) — direct callable static methods,
15
- // e.g. `CentrifugoAuth.cfgCentrifugoAuthTokenRetrieve(...)`.
13
+ // Hey API SDK classes (one per OpenAPI tag) — direct callable static methods.
16
14
  export {
17
- Cfg,
18
- Accounts,
19
- OAuth,
20
- UserProfile,
21
- Auth,
22
- Centrifugo,
23
- CentrifugoAuth,
24
- Totp,
25
- BackupCodes,
26
- TotpManagement,
27
- TotpSetup,
28
- TotpVerification,
15
+ CfgAccountsApiKey,
16
+ CfgAccountsOauth,
17
+ CfgAccounts,
18
+ CfgAccountsProfile,
19
+ CfgAccountsAuth,
20
+ CfgCentrifugo,
21
+ CfgTotpBackupCodes,
22
+ CfgTotp,
23
+ CfgTotpSetup,
24
+ CfgTotpVerify,
29
25
  } from './_api/generated/sdk.gen';
30
26
 
31
27
  // Type namespaces (one per group) for code that needs `AccountsTypes.User`.
package/src/lib/env.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Environment constants — shared across all @djangocfg packages.
3
+ */
4
+
5
+ export const nodeEnv = process.env.NODE_ENV || 'development';
6
+ export const isDev = nodeEnv === 'development';
7
+ export const isProd = !isDev;
8
+ export const isTest = nodeEnv === 'test';
9
+ export const isBrowser = typeof window !== 'undefined';
10
+ export const isServer = typeof window === 'undefined';