@gofreego/tsutils 0.1.13 → 0.1.14

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 CHANGED
@@ -604,64 +604,179 @@ declare class LocalStorage {
604
604
 
605
605
  declare const getHighlighter: () => Promise<Highlighter>;
606
606
 
607
- /**
608
- * Context State Interface
609
- */
610
- interface AuthContextType {
611
- isAuthenticated: boolean;
612
- isLoading: boolean;
613
- }
614
- interface AuthProviderProps {
615
- /** Name of the cookie containing the JWT token. Defaults to "authorization" */
616
- cookieName?: string;
617
- /** URL to redirect the user to if authentication fails */
618
- redirectUrl: string;
619
- /** Components to render if deeply authenticated */
620
- children: ReactNode;
621
- /** Custom fallback logic to fire upon failed auth (overrides redirectUrl behavior) */
622
- onAuthFail?: () => void;
623
- /** Custom callback fired strictly on successful auth */
624
- onAuthSuccess?: () => void;
625
- /** Optional loading element while checking cookies asynchronously */
626
- loadingElement?: ReactNode;
607
+ interface User {
608
+ id: string;
609
+ uuid: string;
610
+ username: string;
611
+ email?: string | undefined;
612
+ phone?: string | undefined;
613
+ /** Display name for the user */
614
+ name?: string | undefined;
615
+ /** URL to user's avatar image */
616
+ avatarUrl?: string | undefined;
617
+ emailVerified: boolean;
618
+ phoneVerified: boolean;
619
+ deactivated: boolean;
620
+ isActive: boolean;
621
+ isLocked: boolean;
622
+ failedLoginAttempts: number;
623
+ lastLoginAt?: string | undefined;
624
+ passwordChangedAt: string;
625
+ createdAt: string;
626
+ updatedAt: string;
627
+ }
628
+ interface SignInMetadata {
629
+ /** Unique device identifier */
630
+ deviceId?: string | undefined;
631
+ /** Human-readable device name */
632
+ deviceName?: string | undefined;
633
+ /** web, mobile, desktop, tablet */
634
+ deviceType?: string | undefined;
635
+ lat?: number | undefined;
636
+ long?: number | undefined;
637
+ }
638
+ /** SignInRequest for user authentication */
639
+ interface SignInRequest {
640
+ username: string;
641
+ password?: string | undefined;
642
+ otp?: string | undefined;
643
+ rememberMe?: boolean | undefined;
644
+ metadata?: SignInMetadata | undefined;
645
+ profiles?: boolean | undefined;
646
+ includePermissions?: boolean | undefined;
647
+ verificationId?: string | undefined;
648
+ }
649
+ /** SignInResponse with authentication tokens and user data */
650
+ interface SignInResponse {
651
+ accessToken: string;
652
+ refreshToken: string;
653
+ /** Access token expiration (Unix timestamp) */
654
+ expiresAt: string;
655
+ /** Refresh token expiration (Unix timestamp) */
656
+ refreshExpiresAt: string;
657
+ user: User | undefined;
658
+ /** Session UUID for tracking */
659
+ sessionId: string;
660
+ message: string;
661
+ }
662
+ /** RefreshTokenRequest to refresh access token */
663
+ interface RefreshTokenRequest {
664
+ refreshToken: string;
665
+ deviceId?: string | undefined;
666
+ profiles?: boolean | undefined;
667
+ includePermissions?: boolean | undefined;
668
+ }
669
+ /** RefreshTokenResponse with new tokens */
670
+ interface RefreshTokenResponse {
671
+ accessToken: string;
672
+ /** New refresh token (rotation) */
673
+ refreshToken: string;
674
+ expiresAt: string;
675
+ refreshExpiresAt: string;
676
+ message: string;
677
+ }
678
+ /** LogoutRequest to end user session */
679
+ interface LogoutRequest {
680
+ /** Specific session to logout */
681
+ sessionId?: string | undefined;
682
+ /** Logout from all devices */
683
+ allSessions?: boolean | undefined;
684
+ }
685
+ /** LogoutResponse */
686
+ interface LogoutResponse {
687
+ success: boolean;
688
+ message: string;
689
+ /** Number of sessions ended */
690
+ sessionsTerminated: number;
691
+ }
692
+ /** GenerateLoginTokenRequest - user is identified from Authorization header */
693
+ interface GenerateLoginTokenRequest {
694
+ /** Token TTL in seconds (default: 60, max: 300) */
695
+ ttlSeconds?: number | undefined;
696
+ }
697
+ /** SignInWithLoginTokenRequest - sign in using a single-use login token */
698
+ interface SignInWithLoginTokenRequest {
699
+ loginToken: string;
700
+ metadata?: SignInMetadata | undefined;
701
+ profiles?: boolean | undefined;
702
+ includePermissions?: boolean | undefined;
703
+ }
704
+ /** GenerateLoginTokenResponse - short-lived single-use token */
705
+ interface GenerateLoginTokenResponse {
706
+ /** Opaque single-use token (prefix: ltk_) */
707
+ loginToken: string;
708
+ /** Unix timestamp (seconds), TTL ~1 min */
709
+ expiresAt: string;
627
710
  }
628
- /**
629
- * AuthProvider verifies a JWT embedded in document.cookie.
630
- * Checks for existence, decodes the signature, and evaluates the `exp` claim.
631
- * Redirects heavily dependent or kicks custom callbacks if failed.
632
- */
633
- declare const AuthProvider: React.FC<AuthProviderProps>;
634
- /**
635
- * React Hook for easily accessing the authenticated JWT context
636
- */
637
- declare const useAuth: () => AuthContextType;
638
711
 
639
- /**
640
- * A utility class to manage user permissions using the browser's localStorage.
641
- * Employs an internal memory cache to prevent repeated synchronous parsing.
642
- */
643
- declare class PermissionManager {
644
- private static cachedPermissions;
645
- /**
646
- * Saves an array of permission strings to local storage.
647
- * @param permissions Array of permission strings.
648
- */
649
- static setPermissions(permissions: string[]): void;
650
- /**
651
- * Retrieves the currently stored permissions from local storage or memory cache.
652
- * @returns Array of permission strings, or an empty array if none exist.
653
- */
654
- static getPermissions(): string[];
655
- /**
656
- * Clears all stored permissions from memory and local storage.
657
- */
658
- static clearPermissions(): void;
659
- /**
660
- * Checks whether the given permission string exists in the currently stored permissions.
661
- * @param permission The permission string to check for.
662
- * @returns True if the permission exists, false otherwise.
663
- */
664
- static hasPermission(permission: string): boolean;
712
+ interface ISessionManager {
713
+ /** Persist session to localStorage and set the auth header. */
714
+ save(session: SignInResponse): void;
715
+ /** Partially update stored session fields (e.g. after token refresh). */
716
+ patch(updates: Partial<SignInResponse>): void;
717
+ /** Clear session from localStorage and remove the auth header. */
718
+ clear(): void;
719
+ /** Return the full stored session, or null if absent / unparseable. */
720
+ get(): SignInResponse | null;
721
+ /** Return the stored access token, or undefined. */
722
+ getAccessToken(): string | undefined;
723
+ /** Return the stored refresh token, or undefined. */
724
+ getRefreshToken(): string | undefined;
725
+ /** Return the session UUID, or undefined. */
726
+ getSessionId(): string | undefined;
727
+ /** True when a non-expired access token is stored. */
728
+ isAuthenticated(): boolean;
729
+ /** Called on app startup: restore the auth header or wipe an expired session. */
730
+ initialize(): void;
731
+ }
732
+ declare class SessionManager implements ISessionManager {
733
+ private static instance;
734
+ private readonly key;
735
+ private readonly client;
736
+ private cache;
737
+ private constructor();
738
+ static getInstance(client: HttpClient): SessionManager;
739
+ save(session: SignInResponse): void;
740
+ patch(updates: Partial<SignInResponse>): void;
741
+ clear(): void;
742
+ get(): SignInResponse | null;
743
+ getAccessToken(): string | undefined;
744
+ getRefreshToken(): string | undefined;
745
+ getSessionId(): string | undefined;
746
+ isAuthenticated(): boolean;
747
+ initialize(): void;
748
+ private setAuthToken;
749
+ private clearAuthToken;
750
+ }
751
+
752
+ interface IAuthService {
753
+ signIn(request: SignInRequest): Promise<SignInResponse>;
754
+ refreshToken(request?: Partial<RefreshTokenRequest>): Promise<RefreshTokenResponse>;
755
+ logout(request: LogoutRequest): Promise<LogoutResponse>;
756
+ generateLoginToken(request?: GenerateLoginTokenRequest): Promise<GenerateLoginTokenResponse>;
757
+ signInWithLoginToken(request: SignInWithLoginTokenRequest): Promise<SignInResponse>;
758
+ isAuthenticated(): boolean;
759
+ getAccessToken(): string | undefined;
760
+ getSessionDetails(): SignInResponse | null;
761
+ getSessionId(): string | undefined;
762
+ initializeAuth(): void;
763
+ }
764
+ declare class AuthService implements IAuthService {
765
+ private static instance;
766
+ private readonly client;
767
+ private readonly sessionManager;
768
+ private constructor();
769
+ static getInstance(client: HttpClient): AuthService;
770
+ signIn(request: SignInRequest): Promise<SignInResponse>;
771
+ refreshToken(request?: Partial<RefreshTokenRequest>): Promise<RefreshTokenResponse>;
772
+ logout(request: LogoutRequest): Promise<LogoutResponse>;
773
+ generateLoginToken(request?: GenerateLoginTokenRequest): Promise<GenerateLoginTokenResponse>;
774
+ signInWithLoginToken(request: SignInWithLoginTokenRequest): Promise<SignInResponse>;
775
+ isAuthenticated(): boolean;
776
+ getAccessToken(): string | undefined;
777
+ getSessionDetails(): SignInResponse | null;
778
+ getSessionId(): string | undefined;
779
+ initializeAuth(): void;
665
780
  }
666
781
 
667
- export { type AuthContextType, AuthProvider, type AuthProviderProps, ConfirmDialog, type ConfirmDialogProps, type ErrorData, HttpClient, type HttpClientConfig, type HttpError, type HttpResponse, LocalStorage, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, PermissionManager, ReadmeViewer, type RequestConfig, type ResolvedThemeMode, SidebarLayout, type SidebarLayoutProps, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, borderRadius, cn, darkTheme, debounce, elevation, extractErrorMessage, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useAuth, useNotification, useTheme, zIndex };
782
+ export { AuthService, ConfirmDialog, type ConfirmDialogProps, type ErrorData, type GenerateLoginTokenRequest, type GenerateLoginTokenResponse, HttpClient, type HttpClientConfig, type HttpError, type HttpResponse, type IAuthService, type ISessionManager, LocalStorage, type LogoutRequest, type LogoutResponse, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, ReadmeViewer, type RefreshTokenRequest, type RefreshTokenResponse, type RequestConfig, type ResolvedThemeMode, SessionManager, SidebarLayout, type SidebarLayoutProps, type SignInMetadata, type SignInRequest, type SignInResponse, type SignInWithLoginTokenRequest, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, type User, borderRadius, cn, darkTheme, debounce, elevation, extractErrorMessage, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useNotification, useTheme, zIndex };
package/dist/index.d.ts CHANGED
@@ -604,64 +604,179 @@ declare class LocalStorage {
604
604
 
605
605
  declare const getHighlighter: () => Promise<Highlighter>;
606
606
 
607
- /**
608
- * Context State Interface
609
- */
610
- interface AuthContextType {
611
- isAuthenticated: boolean;
612
- isLoading: boolean;
613
- }
614
- interface AuthProviderProps {
615
- /** Name of the cookie containing the JWT token. Defaults to "authorization" */
616
- cookieName?: string;
617
- /** URL to redirect the user to if authentication fails */
618
- redirectUrl: string;
619
- /** Components to render if deeply authenticated */
620
- children: ReactNode;
621
- /** Custom fallback logic to fire upon failed auth (overrides redirectUrl behavior) */
622
- onAuthFail?: () => void;
623
- /** Custom callback fired strictly on successful auth */
624
- onAuthSuccess?: () => void;
625
- /** Optional loading element while checking cookies asynchronously */
626
- loadingElement?: ReactNode;
607
+ interface User {
608
+ id: string;
609
+ uuid: string;
610
+ username: string;
611
+ email?: string | undefined;
612
+ phone?: string | undefined;
613
+ /** Display name for the user */
614
+ name?: string | undefined;
615
+ /** URL to user's avatar image */
616
+ avatarUrl?: string | undefined;
617
+ emailVerified: boolean;
618
+ phoneVerified: boolean;
619
+ deactivated: boolean;
620
+ isActive: boolean;
621
+ isLocked: boolean;
622
+ failedLoginAttempts: number;
623
+ lastLoginAt?: string | undefined;
624
+ passwordChangedAt: string;
625
+ createdAt: string;
626
+ updatedAt: string;
627
+ }
628
+ interface SignInMetadata {
629
+ /** Unique device identifier */
630
+ deviceId?: string | undefined;
631
+ /** Human-readable device name */
632
+ deviceName?: string | undefined;
633
+ /** web, mobile, desktop, tablet */
634
+ deviceType?: string | undefined;
635
+ lat?: number | undefined;
636
+ long?: number | undefined;
637
+ }
638
+ /** SignInRequest for user authentication */
639
+ interface SignInRequest {
640
+ username: string;
641
+ password?: string | undefined;
642
+ otp?: string | undefined;
643
+ rememberMe?: boolean | undefined;
644
+ metadata?: SignInMetadata | undefined;
645
+ profiles?: boolean | undefined;
646
+ includePermissions?: boolean | undefined;
647
+ verificationId?: string | undefined;
648
+ }
649
+ /** SignInResponse with authentication tokens and user data */
650
+ interface SignInResponse {
651
+ accessToken: string;
652
+ refreshToken: string;
653
+ /** Access token expiration (Unix timestamp) */
654
+ expiresAt: string;
655
+ /** Refresh token expiration (Unix timestamp) */
656
+ refreshExpiresAt: string;
657
+ user: User | undefined;
658
+ /** Session UUID for tracking */
659
+ sessionId: string;
660
+ message: string;
661
+ }
662
+ /** RefreshTokenRequest to refresh access token */
663
+ interface RefreshTokenRequest {
664
+ refreshToken: string;
665
+ deviceId?: string | undefined;
666
+ profiles?: boolean | undefined;
667
+ includePermissions?: boolean | undefined;
668
+ }
669
+ /** RefreshTokenResponse with new tokens */
670
+ interface RefreshTokenResponse {
671
+ accessToken: string;
672
+ /** New refresh token (rotation) */
673
+ refreshToken: string;
674
+ expiresAt: string;
675
+ refreshExpiresAt: string;
676
+ message: string;
677
+ }
678
+ /** LogoutRequest to end user session */
679
+ interface LogoutRequest {
680
+ /** Specific session to logout */
681
+ sessionId?: string | undefined;
682
+ /** Logout from all devices */
683
+ allSessions?: boolean | undefined;
684
+ }
685
+ /** LogoutResponse */
686
+ interface LogoutResponse {
687
+ success: boolean;
688
+ message: string;
689
+ /** Number of sessions ended */
690
+ sessionsTerminated: number;
691
+ }
692
+ /** GenerateLoginTokenRequest - user is identified from Authorization header */
693
+ interface GenerateLoginTokenRequest {
694
+ /** Token TTL in seconds (default: 60, max: 300) */
695
+ ttlSeconds?: number | undefined;
696
+ }
697
+ /** SignInWithLoginTokenRequest - sign in using a single-use login token */
698
+ interface SignInWithLoginTokenRequest {
699
+ loginToken: string;
700
+ metadata?: SignInMetadata | undefined;
701
+ profiles?: boolean | undefined;
702
+ includePermissions?: boolean | undefined;
703
+ }
704
+ /** GenerateLoginTokenResponse - short-lived single-use token */
705
+ interface GenerateLoginTokenResponse {
706
+ /** Opaque single-use token (prefix: ltk_) */
707
+ loginToken: string;
708
+ /** Unix timestamp (seconds), TTL ~1 min */
709
+ expiresAt: string;
627
710
  }
628
- /**
629
- * AuthProvider verifies a JWT embedded in document.cookie.
630
- * Checks for existence, decodes the signature, and evaluates the `exp` claim.
631
- * Redirects heavily dependent or kicks custom callbacks if failed.
632
- */
633
- declare const AuthProvider: React.FC<AuthProviderProps>;
634
- /**
635
- * React Hook for easily accessing the authenticated JWT context
636
- */
637
- declare const useAuth: () => AuthContextType;
638
711
 
639
- /**
640
- * A utility class to manage user permissions using the browser's localStorage.
641
- * Employs an internal memory cache to prevent repeated synchronous parsing.
642
- */
643
- declare class PermissionManager {
644
- private static cachedPermissions;
645
- /**
646
- * Saves an array of permission strings to local storage.
647
- * @param permissions Array of permission strings.
648
- */
649
- static setPermissions(permissions: string[]): void;
650
- /**
651
- * Retrieves the currently stored permissions from local storage or memory cache.
652
- * @returns Array of permission strings, or an empty array if none exist.
653
- */
654
- static getPermissions(): string[];
655
- /**
656
- * Clears all stored permissions from memory and local storage.
657
- */
658
- static clearPermissions(): void;
659
- /**
660
- * Checks whether the given permission string exists in the currently stored permissions.
661
- * @param permission The permission string to check for.
662
- * @returns True if the permission exists, false otherwise.
663
- */
664
- static hasPermission(permission: string): boolean;
712
+ interface ISessionManager {
713
+ /** Persist session to localStorage and set the auth header. */
714
+ save(session: SignInResponse): void;
715
+ /** Partially update stored session fields (e.g. after token refresh). */
716
+ patch(updates: Partial<SignInResponse>): void;
717
+ /** Clear session from localStorage and remove the auth header. */
718
+ clear(): void;
719
+ /** Return the full stored session, or null if absent / unparseable. */
720
+ get(): SignInResponse | null;
721
+ /** Return the stored access token, or undefined. */
722
+ getAccessToken(): string | undefined;
723
+ /** Return the stored refresh token, or undefined. */
724
+ getRefreshToken(): string | undefined;
725
+ /** Return the session UUID, or undefined. */
726
+ getSessionId(): string | undefined;
727
+ /** True when a non-expired access token is stored. */
728
+ isAuthenticated(): boolean;
729
+ /** Called on app startup: restore the auth header or wipe an expired session. */
730
+ initialize(): void;
731
+ }
732
+ declare class SessionManager implements ISessionManager {
733
+ private static instance;
734
+ private readonly key;
735
+ private readonly client;
736
+ private cache;
737
+ private constructor();
738
+ static getInstance(client: HttpClient): SessionManager;
739
+ save(session: SignInResponse): void;
740
+ patch(updates: Partial<SignInResponse>): void;
741
+ clear(): void;
742
+ get(): SignInResponse | null;
743
+ getAccessToken(): string | undefined;
744
+ getRefreshToken(): string | undefined;
745
+ getSessionId(): string | undefined;
746
+ isAuthenticated(): boolean;
747
+ initialize(): void;
748
+ private setAuthToken;
749
+ private clearAuthToken;
750
+ }
751
+
752
+ interface IAuthService {
753
+ signIn(request: SignInRequest): Promise<SignInResponse>;
754
+ refreshToken(request?: Partial<RefreshTokenRequest>): Promise<RefreshTokenResponse>;
755
+ logout(request: LogoutRequest): Promise<LogoutResponse>;
756
+ generateLoginToken(request?: GenerateLoginTokenRequest): Promise<GenerateLoginTokenResponse>;
757
+ signInWithLoginToken(request: SignInWithLoginTokenRequest): Promise<SignInResponse>;
758
+ isAuthenticated(): boolean;
759
+ getAccessToken(): string | undefined;
760
+ getSessionDetails(): SignInResponse | null;
761
+ getSessionId(): string | undefined;
762
+ initializeAuth(): void;
763
+ }
764
+ declare class AuthService implements IAuthService {
765
+ private static instance;
766
+ private readonly client;
767
+ private readonly sessionManager;
768
+ private constructor();
769
+ static getInstance(client: HttpClient): AuthService;
770
+ signIn(request: SignInRequest): Promise<SignInResponse>;
771
+ refreshToken(request?: Partial<RefreshTokenRequest>): Promise<RefreshTokenResponse>;
772
+ logout(request: LogoutRequest): Promise<LogoutResponse>;
773
+ generateLoginToken(request?: GenerateLoginTokenRequest): Promise<GenerateLoginTokenResponse>;
774
+ signInWithLoginToken(request: SignInWithLoginTokenRequest): Promise<SignInResponse>;
775
+ isAuthenticated(): boolean;
776
+ getAccessToken(): string | undefined;
777
+ getSessionDetails(): SignInResponse | null;
778
+ getSessionId(): string | undefined;
779
+ initializeAuth(): void;
665
780
  }
666
781
 
667
- export { type AuthContextType, AuthProvider, type AuthProviderProps, ConfirmDialog, type ConfirmDialogProps, type ErrorData, HttpClient, type HttpClientConfig, type HttpError, type HttpResponse, LocalStorage, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, PermissionManager, ReadmeViewer, type RequestConfig, type ResolvedThemeMode, SidebarLayout, type SidebarLayoutProps, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, borderRadius, cn, darkTheme, debounce, elevation, extractErrorMessage, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useAuth, useNotification, useTheme, zIndex };
782
+ export { AuthService, ConfirmDialog, type ConfirmDialogProps, type ErrorData, type GenerateLoginTokenRequest, type GenerateLoginTokenResponse, HttpClient, type HttpClientConfig, type HttpError, type HttpResponse, type IAuthService, type ISessionManager, LocalStorage, type LogoutRequest, type LogoutResponse, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, ReadmeViewer, type RefreshTokenRequest, type RefreshTokenResponse, type RequestConfig, type ResolvedThemeMode, SessionManager, SidebarLayout, type SidebarLayoutProps, type SignInMetadata, type SignInRequest, type SignInResponse, type SignInWithLoginTokenRequest, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, type User, borderRadius, cn, darkTheme, debounce, elevation, extractErrorMessage, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useNotification, useTheme, zIndex };