@ram_28/kf-ai-sdk 1.0.2 → 1.0.4
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/README.md +3 -27
- package/dist/auth/AuthProvider.d.ts.map +1 -1
- package/dist/auth/authClient.d.ts +19 -3
- package/dist/auth/authClient.d.ts.map +1 -1
- package/dist/auth/authConfig.d.ts +1 -1
- package/dist/auth/authConfig.d.ts.map +1 -1
- package/dist/auth/index.d.ts +2 -3
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/types.d.ts +4 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/auth/useAuth.d.ts.map +1 -1
- package/dist/index.cjs +11 -11
- package/dist/index.mjs +896 -880
- package/package.json +1 -1
- package/sdk/auth/AuthProvider.tsx +8 -2
- package/sdk/auth/authClient.ts +71 -23
- package/sdk/auth/authConfig.ts +5 -3
- package/sdk/auth/index.ts +2 -21
- package/sdk/auth/types.ts +6 -0
- package/sdk/auth/useAuth.ts +33 -31
package/package.json
CHANGED
|
@@ -89,7 +89,8 @@ export function AuthProvider({
|
|
|
89
89
|
retryDelay: authConfig.retry.delay,
|
|
90
90
|
staleTime: authConfig.staleTime,
|
|
91
91
|
gcTime: authConfig.staleTime * 2,
|
|
92
|
-
refetchOnWindowFocus: true,
|
|
92
|
+
refetchOnWindowFocus: authConfig.refetchOnWindowFocus ?? true,
|
|
93
|
+
refetchOnReconnect: authConfig.refetchOnReconnect ?? true,
|
|
93
94
|
refetchInterval: authConfig.sessionCheckInterval || false,
|
|
94
95
|
});
|
|
95
96
|
|
|
@@ -167,6 +168,11 @@ export function AuthProvider({
|
|
|
167
168
|
);
|
|
168
169
|
|
|
169
170
|
const refreshSession = useCallback(async (): Promise<SessionResponse | null> => {
|
|
171
|
+
// Prevent concurrent refreshes - return existing data if already fetching
|
|
172
|
+
if (isFetching) {
|
|
173
|
+
return sessionData || null;
|
|
174
|
+
}
|
|
175
|
+
|
|
170
176
|
try {
|
|
171
177
|
const result = await refetch();
|
|
172
178
|
return result.data || null;
|
|
@@ -174,7 +180,7 @@ export function AuthProvider({
|
|
|
174
180
|
setError(err as Error);
|
|
175
181
|
return null;
|
|
176
182
|
}
|
|
177
|
-
}, [refetch]);
|
|
183
|
+
}, [refetch, isFetching, sessionData]);
|
|
178
184
|
|
|
179
185
|
const hasRole = useCallback(
|
|
180
186
|
(role: string): boolean => {
|
package/sdk/auth/authClient.ts
CHANGED
|
@@ -61,38 +61,86 @@ export async function fetchSession(): Promise<SessionResponse> {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
64
|
+
* Initiates OAuth login flow by redirecting to the auth provider.
|
|
65
|
+
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* This function redirects the browser and never resolves.
|
|
68
|
+
* Any code after calling this function will not execute.
|
|
69
|
+
*
|
|
70
|
+
* @param provider - OAuth provider to use (defaults to config.defaultProvider)
|
|
71
|
+
* @param options - Login options including callback URL and custom params
|
|
72
|
+
* @returns Promise that never resolves (browser redirects away)
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // Correct usage - no code after login()
|
|
77
|
+
* function handleLoginClick() {
|
|
78
|
+
* login('google');
|
|
79
|
+
* // Don't put code here - it won't run
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
66
82
|
*/
|
|
67
83
|
export function initiateLogin(
|
|
68
84
|
provider?: AuthProviderName,
|
|
69
85
|
options?: LoginOptions
|
|
70
|
-
):
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
86
|
+
): Promise<never> {
|
|
87
|
+
return new Promise(() => {
|
|
88
|
+
const config = getAuthConfig();
|
|
89
|
+
const baseUrl = getAuthBaseUrl();
|
|
90
|
+
|
|
91
|
+
// Validate base URL
|
|
92
|
+
if (!baseUrl) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
'Auth base URL is not configured. Call setApiBaseUrl("https://...") or configureAuth({ baseUrl: "https://..." }) first.'
|
|
95
|
+
);
|
|
96
|
+
}
|
|
74
97
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
throw new Error(`Auth provider "${selectedProvider}" is not configured`);
|
|
78
|
-
}
|
|
98
|
+
const selectedProvider = provider || config.defaultProvider;
|
|
99
|
+
const providerConfig = getProviderConfig(selectedProvider);
|
|
79
100
|
|
|
80
|
-
|
|
101
|
+
// Validate provider config
|
|
102
|
+
if (!providerConfig) {
|
|
103
|
+
const availableProviders = Object.keys(config.providers || {}).join(", ") || "none";
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Auth provider "${selectedProvider}" is not configured. Available providers: ${availableProviders}`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
81
108
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
109
|
+
// Validate login path
|
|
110
|
+
if (!providerConfig.loginPath) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
`Login path not configured for provider "${selectedProvider}". ` +
|
|
113
|
+
`Configure it with: configureAuth({ providers: { ${selectedProvider}: { loginPath: '/api/auth/...' } } })`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
88
116
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
117
|
+
// Validate URL construction
|
|
118
|
+
let loginUrl: URL;
|
|
119
|
+
try {
|
|
120
|
+
loginUrl = new URL(`${baseUrl}${providerConfig.loginPath}`);
|
|
121
|
+
} catch {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`Failed to construct login URL. Base URL: "${baseUrl}", Login path: "${providerConfig.loginPath}". ` +
|
|
124
|
+
`Ensure baseUrl is a valid URL (e.g., "https://example.com").`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (options?.callbackUrl || config.callbackUrl) {
|
|
129
|
+
loginUrl.searchParams.set(
|
|
130
|
+
"callbackUrl",
|
|
131
|
+
options?.callbackUrl || config.callbackUrl || window.location.href
|
|
132
|
+
);
|
|
133
|
+
}
|
|
94
134
|
|
|
95
|
-
|
|
135
|
+
if (options?.params) {
|
|
136
|
+
Object.entries(options.params).forEach(([key, value]) => {
|
|
137
|
+
loginUrl.searchParams.set(key, value);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
window.location.href = loginUrl.toString();
|
|
142
|
+
// Promise never resolves - browser navigates away
|
|
143
|
+
});
|
|
96
144
|
}
|
|
97
145
|
|
|
98
146
|
/**
|
package/sdk/auth/authConfig.ts
CHANGED
|
@@ -18,13 +18,15 @@ const defaultAuthConfig: AuthConfig = {
|
|
|
18
18
|
},
|
|
19
19
|
},
|
|
20
20
|
defaultProvider: "google",
|
|
21
|
-
autoRedirect:
|
|
21
|
+
autoRedirect: false,
|
|
22
22
|
sessionCheckInterval: 0,
|
|
23
23
|
retry: {
|
|
24
24
|
count: 3,
|
|
25
25
|
delay: 1000,
|
|
26
26
|
},
|
|
27
27
|
staleTime: 5 * 60 * 1000,
|
|
28
|
+
refetchOnWindowFocus: false,
|
|
29
|
+
refetchOnReconnect: true,
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -80,10 +82,10 @@ export function getAuthConfig(): Readonly<AuthConfig> {
|
|
|
80
82
|
|
|
81
83
|
/**
|
|
82
84
|
* Get the base URL for auth endpoints
|
|
83
|
-
* Falls back to API base URL
|
|
85
|
+
* Falls back to API base URL, then window.location.origin
|
|
84
86
|
*/
|
|
85
87
|
export function getAuthBaseUrl(): string {
|
|
86
|
-
return authConfig.baseUrl || getApiBaseUrl();
|
|
88
|
+
return authConfig.baseUrl || getApiBaseUrl() || (typeof window !== 'undefined' ? window.location.origin : '');
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
/**
|
package/sdk/auth/index.ts
CHANGED
|
@@ -8,33 +8,14 @@ export { AuthProvider } from "./AuthProvider";
|
|
|
8
8
|
// Main hook
|
|
9
9
|
export { useAuth } from "./useAuth";
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
export {
|
|
13
|
-
configureAuth,
|
|
14
|
-
setAuthProvider,
|
|
15
|
-
getAuthConfig,
|
|
16
|
-
getAuthBaseUrl,
|
|
17
|
-
resetAuthConfig,
|
|
18
|
-
} from "./authConfig";
|
|
19
|
-
|
|
20
|
-
// API client functions (for advanced use cases)
|
|
21
|
-
export {
|
|
22
|
-
fetchSession,
|
|
23
|
-
initiateLogin,
|
|
24
|
-
performLogout,
|
|
25
|
-
AuthenticationError,
|
|
26
|
-
} from "./authClient";
|
|
11
|
+
// Error class (for error handling)
|
|
12
|
+
export { AuthenticationError } from "./authClient";
|
|
27
13
|
|
|
28
14
|
// Type exports
|
|
29
15
|
export type {
|
|
30
16
|
UserDetails,
|
|
31
17
|
SessionResponse,
|
|
32
18
|
AuthStatus,
|
|
33
|
-
AuthConfig,
|
|
34
|
-
AuthProviderName,
|
|
35
|
-
AuthEndpointConfig,
|
|
36
19
|
AuthProviderProps,
|
|
37
20
|
UseAuthReturn,
|
|
38
|
-
LoginOptions,
|
|
39
|
-
LogoutOptions,
|
|
40
21
|
} from "./types";
|
package/sdk/auth/types.ts
CHANGED
|
@@ -79,6 +79,12 @@ export interface AuthConfig {
|
|
|
79
79
|
|
|
80
80
|
/** React Query stale time for session data */
|
|
81
81
|
staleTime: number;
|
|
82
|
+
|
|
83
|
+
/** Refetch session when window regains focus (default: true) */
|
|
84
|
+
refetchOnWindowFocus?: boolean;
|
|
85
|
+
|
|
86
|
+
/** Refetch session when network reconnects (default: true) */
|
|
87
|
+
refetchOnReconnect?: boolean;
|
|
82
88
|
}
|
|
83
89
|
|
|
84
90
|
/**
|
package/sdk/auth/useAuth.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// ============================================================
|
|
4
4
|
// Main hook for consuming authentication state
|
|
5
5
|
|
|
6
|
+
import { useMemo } from "react";
|
|
6
7
|
import type { UseAuthReturn } from "./types";
|
|
7
8
|
import { useAuthContext } from "./AuthProvider";
|
|
8
9
|
|
|
@@ -33,35 +34,36 @@ import { useAuthContext } from "./AuthProvider";
|
|
|
33
34
|
export function useAuth(): UseAuthReturn {
|
|
34
35
|
const context = useAuthContext();
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
37
|
+
return useMemo(
|
|
38
|
+
() => ({
|
|
39
|
+
user: context.user,
|
|
40
|
+
staticBaseUrl: context.staticBaseUrl,
|
|
41
|
+
buildId: context.buildId,
|
|
42
|
+
status: context.status,
|
|
43
|
+
isAuthenticated: context.isAuthenticated,
|
|
44
|
+
isLoading: context.isLoading,
|
|
45
|
+
login: context.login,
|
|
46
|
+
logout: context.logout,
|
|
47
|
+
refreshSession: context.refreshSession,
|
|
48
|
+
hasRole: context.hasRole,
|
|
49
|
+
hasAnyRole: context.hasAnyRole,
|
|
50
|
+
error: context.error,
|
|
51
|
+
clearError: context.clearError,
|
|
52
|
+
}),
|
|
53
|
+
[
|
|
54
|
+
context.user,
|
|
55
|
+
context.staticBaseUrl,
|
|
56
|
+
context.buildId,
|
|
57
|
+
context.status,
|
|
58
|
+
context.isAuthenticated,
|
|
59
|
+
context.isLoading,
|
|
60
|
+
context.login,
|
|
61
|
+
context.logout,
|
|
62
|
+
context.refreshSession,
|
|
63
|
+
context.hasRole,
|
|
64
|
+
context.hasAnyRole,
|
|
65
|
+
context.error,
|
|
66
|
+
context.clearError,
|
|
67
|
+
]
|
|
68
|
+
);
|
|
67
69
|
}
|