@anythink-cloud/sdk 0.2.0 → 0.3.0
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/index.d.mts +142 -1
- package/dist/index.d.ts +142 -1
- package/dist/index.js +265 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +264 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as zustand_middleware from 'zustand/middleware';
|
|
2
2
|
import * as zustand from 'zustand';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
3
5
|
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
4
6
|
|
|
5
7
|
/**
|
|
@@ -62,6 +64,11 @@ interface AuthConfig {
|
|
|
62
64
|
instanceUrl: string;
|
|
63
65
|
orgId: number;
|
|
64
66
|
cookieStorage?: CookieStorageConfig;
|
|
67
|
+
tokenEndpoint?: string;
|
|
68
|
+
refreshEndpoint?: string;
|
|
69
|
+
registerEndpoint?: string;
|
|
70
|
+
changePasswordEndpoint?: string;
|
|
71
|
+
logoutEndpoint?: string;
|
|
65
72
|
}
|
|
66
73
|
/**
|
|
67
74
|
* Sign in response
|
|
@@ -115,9 +122,11 @@ interface CookieStorageOptions {
|
|
|
115
122
|
|
|
116
123
|
interface AuthState {
|
|
117
124
|
session: Session | null;
|
|
125
|
+
user: User | null;
|
|
118
126
|
isLoading: boolean;
|
|
119
127
|
error: Error | null;
|
|
120
128
|
setSession: (session: Session | null) => void;
|
|
129
|
+
setUser: (user: User | null) => void;
|
|
121
130
|
signOut: () => void;
|
|
122
131
|
clearError: () => void;
|
|
123
132
|
setLoading: (isLoading: boolean) => void;
|
|
@@ -226,12 +235,144 @@ declare class AuthClient {
|
|
|
226
235
|
* Check if user is authenticated
|
|
227
236
|
*/
|
|
228
237
|
isAuthenticated(): boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Fetch user information from the API
|
|
240
|
+
* @returns User object or null if failed
|
|
241
|
+
*/
|
|
242
|
+
fetchUserInfo(): Promise<{
|
|
243
|
+
data: {
|
|
244
|
+
user: User | null;
|
|
245
|
+
};
|
|
246
|
+
error: Error | null;
|
|
247
|
+
}>;
|
|
248
|
+
/**
|
|
249
|
+
* Get the current user
|
|
250
|
+
* @returns User object or null if not available
|
|
251
|
+
*/
|
|
252
|
+
getUser(): User | null;
|
|
229
253
|
/**
|
|
230
254
|
* Get the Zustand store (for React hooks)
|
|
231
255
|
*/
|
|
232
256
|
getStore(): ReturnType<typeof createAuthStore>;
|
|
233
257
|
}
|
|
234
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Callback functions for auth events
|
|
261
|
+
*/
|
|
262
|
+
interface AuthCallbacks {
|
|
263
|
+
/**
|
|
264
|
+
* Called after successful sign in
|
|
265
|
+
*/
|
|
266
|
+
onSignIn?: (session: Session) => void | Promise<void>;
|
|
267
|
+
/**
|
|
268
|
+
* Called after sign out
|
|
269
|
+
*/
|
|
270
|
+
onSignOut?: () => void | Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* Called when token refresh fails and user is signed out
|
|
273
|
+
*/
|
|
274
|
+
onTokenRefreshFailed?: () => void | Promise<void>;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Auth context value
|
|
278
|
+
*/
|
|
279
|
+
interface AuthContextValue {
|
|
280
|
+
/**
|
|
281
|
+
* Current session
|
|
282
|
+
*/
|
|
283
|
+
session: Session | null;
|
|
284
|
+
/**
|
|
285
|
+
* Current user information
|
|
286
|
+
*/
|
|
287
|
+
user: User | null;
|
|
288
|
+
/**
|
|
289
|
+
* Whether auth operations are in progress
|
|
290
|
+
*/
|
|
291
|
+
isLoading: boolean;
|
|
292
|
+
/**
|
|
293
|
+
* Current error, if any
|
|
294
|
+
*/
|
|
295
|
+
error: Error | null;
|
|
296
|
+
/**
|
|
297
|
+
* Whether user is authenticated
|
|
298
|
+
*/
|
|
299
|
+
isAuthenticated: boolean;
|
|
300
|
+
/**
|
|
301
|
+
* Sign in with email and password
|
|
302
|
+
*/
|
|
303
|
+
signIn: (email: string, password: string, orgId?: number) => Promise<SignInResponse>;
|
|
304
|
+
/**
|
|
305
|
+
* Sign out and clear session
|
|
306
|
+
*/
|
|
307
|
+
signOut: () => Promise<{
|
|
308
|
+
error: Error | null;
|
|
309
|
+
}>;
|
|
310
|
+
/**
|
|
311
|
+
* Register a new user
|
|
312
|
+
*/
|
|
313
|
+
register: (firstName: string, lastName: string, email: string, password: string) => Promise<{
|
|
314
|
+
error: Error | null;
|
|
315
|
+
}>;
|
|
316
|
+
/**
|
|
317
|
+
* Change password
|
|
318
|
+
*/
|
|
319
|
+
changePassword: (currentPassword: string, newPassword: string) => Promise<{
|
|
320
|
+
error: Error | null;
|
|
321
|
+
}>;
|
|
322
|
+
/**
|
|
323
|
+
* Refresh the session token
|
|
324
|
+
*/
|
|
325
|
+
refreshSession: () => Promise<RefreshResponse>;
|
|
326
|
+
/**
|
|
327
|
+
* Get the current access token
|
|
328
|
+
*/
|
|
329
|
+
getAccessToken: () => string | null;
|
|
330
|
+
/**
|
|
331
|
+
* Get the current refresh token
|
|
332
|
+
*/
|
|
333
|
+
getRefreshToken: () => string | null;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Props for AuthProvider
|
|
337
|
+
*/
|
|
338
|
+
interface AuthProviderProps {
|
|
339
|
+
/**
|
|
340
|
+
* AuthClient instance
|
|
341
|
+
*/
|
|
342
|
+
authClient: AuthClient;
|
|
343
|
+
/**
|
|
344
|
+
* Optional callbacks for auth events
|
|
345
|
+
*/
|
|
346
|
+
callbacks?: AuthCallbacks;
|
|
347
|
+
/**
|
|
348
|
+
* Whether to automatically refresh tokens before they expire
|
|
349
|
+
* @default true
|
|
350
|
+
*/
|
|
351
|
+
autoRefresh?: boolean;
|
|
352
|
+
/**
|
|
353
|
+
* How many seconds before expiration to refresh the token
|
|
354
|
+
* @default 60
|
|
355
|
+
*/
|
|
356
|
+
refreshThreshold?: number;
|
|
357
|
+
/**
|
|
358
|
+
* URL to redirect to when user is not authenticated or token refresh fails
|
|
359
|
+
*/
|
|
360
|
+
loginUrl?: string;
|
|
361
|
+
/**
|
|
362
|
+
* Child components
|
|
363
|
+
*/
|
|
364
|
+
children: ReactNode;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* AuthProvider component that wraps your app and provides auth state
|
|
368
|
+
*/
|
|
369
|
+
declare function AuthProvider({ authClient, callbacks, autoRefresh, refreshThreshold, loginUrl, children, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
|
|
370
|
+
/**
|
|
371
|
+
* Hook to access auth context
|
|
372
|
+
* @throws Error if used outside AuthProvider
|
|
373
|
+
*/
|
|
374
|
+
declare function useAuth(): AuthContextValue;
|
|
375
|
+
|
|
235
376
|
/**
|
|
236
377
|
* Base service class with automatic token injection and refresh handling
|
|
237
378
|
*/
|
|
@@ -274,4 +415,4 @@ declare class AuthenticatedBaseService {
|
|
|
274
415
|
getClient(): AxiosInstance;
|
|
275
416
|
}
|
|
276
417
|
|
|
277
|
-
export { AuthClient, type AuthConfig, AuthenticatedBaseService, type CookieStorageConfig, type RefreshResponse, type Session, type SignInResponse, type TokenPair, type User, createAuthStore };
|
|
418
|
+
export { type AuthCallbacks, AuthClient, type AuthConfig, type AuthContextValue, AuthProvider, type AuthProviderProps, AuthenticatedBaseService, type CookieStorageConfig, type RefreshResponse, type Session, type SignInResponse, type TokenPair, type User, createAuthStore, useAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as zustand_middleware from 'zustand/middleware';
|
|
2
2
|
import * as zustand from 'zustand';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
3
5
|
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
4
6
|
|
|
5
7
|
/**
|
|
@@ -62,6 +64,11 @@ interface AuthConfig {
|
|
|
62
64
|
instanceUrl: string;
|
|
63
65
|
orgId: number;
|
|
64
66
|
cookieStorage?: CookieStorageConfig;
|
|
67
|
+
tokenEndpoint?: string;
|
|
68
|
+
refreshEndpoint?: string;
|
|
69
|
+
registerEndpoint?: string;
|
|
70
|
+
changePasswordEndpoint?: string;
|
|
71
|
+
logoutEndpoint?: string;
|
|
65
72
|
}
|
|
66
73
|
/**
|
|
67
74
|
* Sign in response
|
|
@@ -115,9 +122,11 @@ interface CookieStorageOptions {
|
|
|
115
122
|
|
|
116
123
|
interface AuthState {
|
|
117
124
|
session: Session | null;
|
|
125
|
+
user: User | null;
|
|
118
126
|
isLoading: boolean;
|
|
119
127
|
error: Error | null;
|
|
120
128
|
setSession: (session: Session | null) => void;
|
|
129
|
+
setUser: (user: User | null) => void;
|
|
121
130
|
signOut: () => void;
|
|
122
131
|
clearError: () => void;
|
|
123
132
|
setLoading: (isLoading: boolean) => void;
|
|
@@ -226,12 +235,144 @@ declare class AuthClient {
|
|
|
226
235
|
* Check if user is authenticated
|
|
227
236
|
*/
|
|
228
237
|
isAuthenticated(): boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Fetch user information from the API
|
|
240
|
+
* @returns User object or null if failed
|
|
241
|
+
*/
|
|
242
|
+
fetchUserInfo(): Promise<{
|
|
243
|
+
data: {
|
|
244
|
+
user: User | null;
|
|
245
|
+
};
|
|
246
|
+
error: Error | null;
|
|
247
|
+
}>;
|
|
248
|
+
/**
|
|
249
|
+
* Get the current user
|
|
250
|
+
* @returns User object or null if not available
|
|
251
|
+
*/
|
|
252
|
+
getUser(): User | null;
|
|
229
253
|
/**
|
|
230
254
|
* Get the Zustand store (for React hooks)
|
|
231
255
|
*/
|
|
232
256
|
getStore(): ReturnType<typeof createAuthStore>;
|
|
233
257
|
}
|
|
234
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Callback functions for auth events
|
|
261
|
+
*/
|
|
262
|
+
interface AuthCallbacks {
|
|
263
|
+
/**
|
|
264
|
+
* Called after successful sign in
|
|
265
|
+
*/
|
|
266
|
+
onSignIn?: (session: Session) => void | Promise<void>;
|
|
267
|
+
/**
|
|
268
|
+
* Called after sign out
|
|
269
|
+
*/
|
|
270
|
+
onSignOut?: () => void | Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* Called when token refresh fails and user is signed out
|
|
273
|
+
*/
|
|
274
|
+
onTokenRefreshFailed?: () => void | Promise<void>;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Auth context value
|
|
278
|
+
*/
|
|
279
|
+
interface AuthContextValue {
|
|
280
|
+
/**
|
|
281
|
+
* Current session
|
|
282
|
+
*/
|
|
283
|
+
session: Session | null;
|
|
284
|
+
/**
|
|
285
|
+
* Current user information
|
|
286
|
+
*/
|
|
287
|
+
user: User | null;
|
|
288
|
+
/**
|
|
289
|
+
* Whether auth operations are in progress
|
|
290
|
+
*/
|
|
291
|
+
isLoading: boolean;
|
|
292
|
+
/**
|
|
293
|
+
* Current error, if any
|
|
294
|
+
*/
|
|
295
|
+
error: Error | null;
|
|
296
|
+
/**
|
|
297
|
+
* Whether user is authenticated
|
|
298
|
+
*/
|
|
299
|
+
isAuthenticated: boolean;
|
|
300
|
+
/**
|
|
301
|
+
* Sign in with email and password
|
|
302
|
+
*/
|
|
303
|
+
signIn: (email: string, password: string, orgId?: number) => Promise<SignInResponse>;
|
|
304
|
+
/**
|
|
305
|
+
* Sign out and clear session
|
|
306
|
+
*/
|
|
307
|
+
signOut: () => Promise<{
|
|
308
|
+
error: Error | null;
|
|
309
|
+
}>;
|
|
310
|
+
/**
|
|
311
|
+
* Register a new user
|
|
312
|
+
*/
|
|
313
|
+
register: (firstName: string, lastName: string, email: string, password: string) => Promise<{
|
|
314
|
+
error: Error | null;
|
|
315
|
+
}>;
|
|
316
|
+
/**
|
|
317
|
+
* Change password
|
|
318
|
+
*/
|
|
319
|
+
changePassword: (currentPassword: string, newPassword: string) => Promise<{
|
|
320
|
+
error: Error | null;
|
|
321
|
+
}>;
|
|
322
|
+
/**
|
|
323
|
+
* Refresh the session token
|
|
324
|
+
*/
|
|
325
|
+
refreshSession: () => Promise<RefreshResponse>;
|
|
326
|
+
/**
|
|
327
|
+
* Get the current access token
|
|
328
|
+
*/
|
|
329
|
+
getAccessToken: () => string | null;
|
|
330
|
+
/**
|
|
331
|
+
* Get the current refresh token
|
|
332
|
+
*/
|
|
333
|
+
getRefreshToken: () => string | null;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Props for AuthProvider
|
|
337
|
+
*/
|
|
338
|
+
interface AuthProviderProps {
|
|
339
|
+
/**
|
|
340
|
+
* AuthClient instance
|
|
341
|
+
*/
|
|
342
|
+
authClient: AuthClient;
|
|
343
|
+
/**
|
|
344
|
+
* Optional callbacks for auth events
|
|
345
|
+
*/
|
|
346
|
+
callbacks?: AuthCallbacks;
|
|
347
|
+
/**
|
|
348
|
+
* Whether to automatically refresh tokens before they expire
|
|
349
|
+
* @default true
|
|
350
|
+
*/
|
|
351
|
+
autoRefresh?: boolean;
|
|
352
|
+
/**
|
|
353
|
+
* How many seconds before expiration to refresh the token
|
|
354
|
+
* @default 60
|
|
355
|
+
*/
|
|
356
|
+
refreshThreshold?: number;
|
|
357
|
+
/**
|
|
358
|
+
* URL to redirect to when user is not authenticated or token refresh fails
|
|
359
|
+
*/
|
|
360
|
+
loginUrl?: string;
|
|
361
|
+
/**
|
|
362
|
+
* Child components
|
|
363
|
+
*/
|
|
364
|
+
children: ReactNode;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* AuthProvider component that wraps your app and provides auth state
|
|
368
|
+
*/
|
|
369
|
+
declare function AuthProvider({ authClient, callbacks, autoRefresh, refreshThreshold, loginUrl, children, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
|
|
370
|
+
/**
|
|
371
|
+
* Hook to access auth context
|
|
372
|
+
* @throws Error if used outside AuthProvider
|
|
373
|
+
*/
|
|
374
|
+
declare function useAuth(): AuthContextValue;
|
|
375
|
+
|
|
235
376
|
/**
|
|
236
377
|
* Base service class with automatic token injection and refresh handling
|
|
237
378
|
*/
|
|
@@ -274,4 +415,4 @@ declare class AuthenticatedBaseService {
|
|
|
274
415
|
getClient(): AxiosInstance;
|
|
275
416
|
}
|
|
276
417
|
|
|
277
|
-
export { AuthClient, type AuthConfig, AuthenticatedBaseService, type CookieStorageConfig, type RefreshResponse, type Session, type SignInResponse, type TokenPair, type User, createAuthStore };
|
|
418
|
+
export { type AuthCallbacks, AuthClient, type AuthConfig, type AuthContextValue, AuthProvider, type AuthProviderProps, AuthenticatedBaseService, type CookieStorageConfig, type RefreshResponse, type Session, type SignInResponse, type TokenPair, type User, createAuthStore, useAuth };
|