@buildbase/sdk 0.0.11 → 0.0.12
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.ts +960 -32
- package/dist/index.esm.js +6 -6
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/saas-os.css +1 -1
- package/dist/types/components/ErrorBoundary.d.ts +49 -2
- package/dist/types/components/features/index.d.ts +109 -0
- package/dist/types/components/user/auth.d.ts +64 -0
- package/dist/types/components/user/role.d.ts +70 -0
- package/dist/types/lib/api-utils.d.ts +164 -0
- package/dist/types/lib/error-handler.d.ts +40 -0
- package/dist/types/providers/auth/hooks.d.ts +61 -0
- package/dist/types/providers/os/hooks.d.ts +40 -1
- package/dist/types/providers/user/hooks.d.ts +71 -0
- package/dist/types/providers/workspace/hooks.d.ts +105 -0
- package/dist/types/providers/workspace/subscription-hooks.d.ts +351 -29
- package/package.json +14 -12
|
@@ -55,6 +55,46 @@ declare class ErrorHandler {
|
|
|
55
55
|
wrapSync<T extends (...args: any[]) => any>(fn: T, context: SDKErrorContext): T;
|
|
56
56
|
}
|
|
57
57
|
export declare const errorHandler: ErrorHandler;
|
|
58
|
+
/**
|
|
59
|
+
* Convenience function to handle an error with context.
|
|
60
|
+
* Uses the global error handler instance.
|
|
61
|
+
*
|
|
62
|
+
* @param error - The error to handle (Error instance, string, or unknown)
|
|
63
|
+
* @param context - Optional context about where the error occurred
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```tsx
|
|
67
|
+
* try {
|
|
68
|
+
* await someOperation();
|
|
69
|
+
* } catch (error) {
|
|
70
|
+
* handleError(error, {
|
|
71
|
+
* component: 'MyComponent',
|
|
72
|
+
* action: 'someOperation',
|
|
73
|
+
* metadata: { userId: '123' },
|
|
74
|
+
* });
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
58
78
|
export declare function handleError(error: Error | unknown, context?: SDKErrorContext): void;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new SDKError instance with optional code and context.
|
|
81
|
+
* Useful for creating standardized errors throughout the SDK.
|
|
82
|
+
*
|
|
83
|
+
* @param message - Error message
|
|
84
|
+
* @param code - Optional error code (e.g., 'AUTH_FAILED', 'NETWORK_ERROR')
|
|
85
|
+
* @param context - Optional context about where the error occurred
|
|
86
|
+
* @param originalError - Optional original error that caused this error
|
|
87
|
+
* @returns A new SDKError instance
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```tsx
|
|
91
|
+
* throw createSDKError(
|
|
92
|
+
* 'Failed to authenticate user',
|
|
93
|
+
* 'AUTH_FAILED',
|
|
94
|
+
* { component: 'AuthProvider', action: 'signIn' },
|
|
95
|
+
* originalError
|
|
96
|
+
* );
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
59
99
|
export declare function createSDKError(message: string, code?: string, context?: SDKErrorContext, originalError?: Error): SDKError;
|
|
60
100
|
export {};
|
|
@@ -1,3 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main authentication hook for the SDK.
|
|
3
|
+
* Provides authentication state, user session, and auth actions.
|
|
4
|
+
*
|
|
5
|
+
* @returns An object containing:
|
|
6
|
+
* - `user`: Current authenticated user object (null if not authenticated)
|
|
7
|
+
* - `session`: Full session object with user and token (null if not authenticated)
|
|
8
|
+
* - `status`: Current authentication status (loading, redirecting, authenticating, authenticated, unauthenticated)
|
|
9
|
+
* - `isLoading`: Boolean indicating if auth state is being determined
|
|
10
|
+
* - `isAuthenticated`: Boolean indicating if user is authenticated
|
|
11
|
+
* - `isRedirecting`: Boolean indicating if redirecting to OAuth provider
|
|
12
|
+
* - `signIn()`: Function to initiate OAuth sign-in flow
|
|
13
|
+
* - `signOut()`: Function to sign out the current user
|
|
14
|
+
* - `openWorkspaceSettings(section?)`: Function to open workspace settings dialog
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* function MyComponent() {
|
|
19
|
+
* const { user, isAuthenticated, signIn, signOut } = useSaaSAuth();
|
|
20
|
+
*
|
|
21
|
+
* if (!isAuthenticated) {
|
|
22
|
+
* return <button onClick={signIn}>Sign In</button>;
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <div>
|
|
27
|
+
* <p>Welcome, {user?.name}</p>
|
|
28
|
+
* <button onClick={signOut}>Sign Out</button>
|
|
29
|
+
* </div>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* // Handle loading state
|
|
37
|
+
* function App() {
|
|
38
|
+
* const { status, isLoading } = useSaaSAuth();
|
|
39
|
+
*
|
|
40
|
+
* if (isLoading) {
|
|
41
|
+
* return <LoadingSpinner />;
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* return <MainContent />;
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* // Open workspace settings
|
|
51
|
+
* function SettingsButton() {
|
|
52
|
+
* const { openWorkspaceSettings } = useSaaSAuth();
|
|
53
|
+
*
|
|
54
|
+
* return (
|
|
55
|
+
* <button onClick={() => openWorkspaceSettings('general')}>
|
|
56
|
+
* Open Settings
|
|
57
|
+
* </button>
|
|
58
|
+
* );
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
1
62
|
export declare function useSaaSAuth(): {
|
|
2
63
|
signIn: () => Promise<void>;
|
|
3
64
|
signOut: () => Promise<void>;
|
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
import type { ISettings } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to access organization settings from the OS context.
|
|
4
|
+
* Automatically fetches settings when OS config is ready.
|
|
5
|
+
*
|
|
6
|
+
* @returns An object containing:
|
|
7
|
+
* - `settings`: Organization settings object (null if not loaded)
|
|
8
|
+
* - `getSettings(signal?)`: Function to manually fetch settings (supports AbortSignal)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* function SettingsDisplay() {
|
|
13
|
+
* const { settings } = useSaaSSettings();
|
|
14
|
+
*
|
|
15
|
+
* if (!settings) return <Loading />;
|
|
16
|
+
*
|
|
17
|
+
* return (
|
|
18
|
+
* <div>
|
|
19
|
+
* <p>Organization: {settings.name}</p>
|
|
20
|
+
* <p>Theme: {settings.theme}</p>
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* // Manual fetch with abort signal
|
|
29
|
+
* function SettingsLoader() {
|
|
30
|
+
* const { getSettings } = useSaaSSettings();
|
|
31
|
+
*
|
|
32
|
+
* useEffect(() => {
|
|
33
|
+
* const controller = new AbortController();
|
|
34
|
+
* getSettings(controller.signal);
|
|
35
|
+
*
|
|
36
|
+
* return () => controller.abort();
|
|
37
|
+
* }, [getSettings]);
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
2
41
|
export declare function useSaaSSettings(): {
|
|
3
42
|
settings: ISettings | null | undefined;
|
|
4
|
-
getSettings: () => Promise<ISettings | null>;
|
|
43
|
+
getSettings: (signal?: AbortSignal) => Promise<ISettings | null>;
|
|
5
44
|
};
|
|
@@ -1,4 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to access user attributes from the UserProvider.
|
|
3
|
+
* Must be used within a UserProvider component.
|
|
4
|
+
*
|
|
5
|
+
* @returns User context object containing:
|
|
6
|
+
* - `attributes`: Record of user attribute key-value pairs
|
|
7
|
+
* - `isLoading`: Boolean indicating if attributes are being loaded
|
|
8
|
+
* - `error`: Error message string (null if no error)
|
|
9
|
+
* - `refreshAttributes()`: Function to manually refresh attributes
|
|
10
|
+
*
|
|
11
|
+
* @throws {Error} If used outside of UserProvider
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* function UserProfile() {
|
|
16
|
+
* const { attributes, isLoading } = useUserAttributes();
|
|
17
|
+
*
|
|
18
|
+
* if (isLoading) return <Loading />;
|
|
19
|
+
*
|
|
20
|
+
* return (
|
|
21
|
+
* <div>
|
|
22
|
+
* <p>Plan: {attributes?.plan}</p>
|
|
23
|
+
* <p>Company: {attributes?.company}</p>
|
|
24
|
+
* </div>
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
1
29
|
export declare function useUserAttributes(): import("./provider").UserContextValue;
|
|
30
|
+
/**
|
|
31
|
+
* Hook to access user feature flags from the UserProvider.
|
|
32
|
+
* Must be used within a UserProvider component.
|
|
33
|
+
*
|
|
34
|
+
* @returns An object containing:
|
|
35
|
+
* - `features`: Record of feature flag key-value pairs (boolean values)
|
|
36
|
+
* - `isLoading`: Boolean indicating if features are being loaded
|
|
37
|
+
* - `error`: Error message string (null if no error)
|
|
38
|
+
* - `refreshFeatures()`: Function to manually refresh features
|
|
39
|
+
* - `isFeatureEnabled(featureId)`: Function to check if a specific feature is enabled
|
|
40
|
+
*
|
|
41
|
+
* @throws {Error} If used outside of UserProvider
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* function FeatureContent() {
|
|
46
|
+
* const { isFeatureEnabled, isLoading } = useUserFeatures();
|
|
47
|
+
*
|
|
48
|
+
* if (isLoading) return <Loading />;
|
|
49
|
+
*
|
|
50
|
+
* if (isFeatureEnabled('premium-feature')) {
|
|
51
|
+
* return <PremiumFeature />;
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* return <BasicFeature />;
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* // Check multiple features
|
|
61
|
+
* function Dashboard() {
|
|
62
|
+
* const { isFeatureEnabled } = useUserFeatures();
|
|
63
|
+
*
|
|
64
|
+
* return (
|
|
65
|
+
* <div>
|
|
66
|
+
* {isFeatureEnabled('analytics') && <Analytics />}
|
|
67
|
+
* {isFeatureEnabled('reports') && <Reports />}
|
|
68
|
+
* </div>
|
|
69
|
+
* );
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
2
73
|
export declare function useUserFeatures(): {
|
|
3
74
|
features: Record<string, boolean>;
|
|
4
75
|
isLoading: boolean;
|
|
@@ -1,6 +1,111 @@
|
|
|
1
1
|
import { IUser } from '../../api/types';
|
|
2
2
|
import { WorkspaceSwitcher } from './provider';
|
|
3
3
|
import { IWorkspace, IWorkspaceUser } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Main workspace management hook for the SDK.
|
|
6
|
+
* Provides workspace state, CRUD operations, and user management.
|
|
7
|
+
*
|
|
8
|
+
* @returns An object containing:
|
|
9
|
+
* - `workspaces`: Array of all workspaces the user has access to
|
|
10
|
+
* - `currentWorkspace`: Currently selected workspace (null if none selected)
|
|
11
|
+
* - `loading`: Boolean indicating if workspaces are being fetched
|
|
12
|
+
* - `error`: Error message string (null if no error)
|
|
13
|
+
* - `refreshing`: Boolean indicating if workspaces are being refreshed in background
|
|
14
|
+
* - `switching`: Boolean indicating if workspace is being switched
|
|
15
|
+
* - `WorkspaceSwitcher`: Component for switching between workspaces
|
|
16
|
+
* - `fetchWorkspaces()`: Function to fetch all workspaces
|
|
17
|
+
* - `refreshWorkspaces()`: Function to refresh workspaces in background (non-blocking)
|
|
18
|
+
* - `setCurrentWorkspace(workspace)`: Function to set the current workspace
|
|
19
|
+
* - `resetCurrentWorkspace()`: Function to clear the current workspace
|
|
20
|
+
* - `createWorkspace(name, image?)`: Function to create a new workspace
|
|
21
|
+
* - `updateWorkspace(workspace, data)`: Function to update a workspace
|
|
22
|
+
* - `deleteWorkspace(workspaceId)`: Function to delete a workspace (owner only)
|
|
23
|
+
* - `getWorkspace(workspaceId)`: Function to fetch a specific workspace
|
|
24
|
+
* - `getUsers(workspaceId)`: Function to fetch users in a workspace
|
|
25
|
+
* - `addUser(workspaceId, email, role)`: Function to add a user to a workspace
|
|
26
|
+
* - `removeUser(workspaceId, userId)`: Function to remove a user from a workspace
|
|
27
|
+
* - `updateUser(workspaceId, userId, config)`: Function to update a user in a workspace
|
|
28
|
+
* - `getProfile()`: Function to fetch current user profile
|
|
29
|
+
* - `updateUserProfile(config)`: Function to update current user profile
|
|
30
|
+
* - `allFeatures`: Array of all available feature flags
|
|
31
|
+
* - `getFeatures()`: Function to fetch all available features
|
|
32
|
+
* - `updateFeature(workspaceId, key, value)`: Function to update a feature flag
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* function WorkspaceList() {
|
|
37
|
+
* const { workspaces, loading, fetchWorkspaces } = useSaaSWorkspaces();
|
|
38
|
+
*
|
|
39
|
+
* useEffect(() => {
|
|
40
|
+
* fetchWorkspaces();
|
|
41
|
+
* }, [fetchWorkspaces]);
|
|
42
|
+
*
|
|
43
|
+
* if (loading) return <Loading />;
|
|
44
|
+
*
|
|
45
|
+
* return (
|
|
46
|
+
* <ul>
|
|
47
|
+
* {workspaces.map(ws => (
|
|
48
|
+
* <li key={ws._id}>{ws.name}</li>
|
|
49
|
+
* ))}
|
|
50
|
+
* </ul>
|
|
51
|
+
* );
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* // Create a new workspace
|
|
58
|
+
* function CreateWorkspace() {
|
|
59
|
+
* const { createWorkspace } = useSaaSWorkspaces();
|
|
60
|
+
*
|
|
61
|
+
* const handleCreate = async () => {
|
|
62
|
+
* try {
|
|
63
|
+
* await createWorkspace('My Workspace', 'https://example.com/logo.png');
|
|
64
|
+
* } catch (error) {
|
|
65
|
+
* console.error('Failed to create workspace:', error);
|
|
66
|
+
* }
|
|
67
|
+
* };
|
|
68
|
+
*
|
|
69
|
+
* return <button onClick={handleCreate}>Create Workspace</button>;
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* // Delete workspace (owner only)
|
|
76
|
+
* function DeleteWorkspaceButton({ workspaceId }) {
|
|
77
|
+
* const { deleteWorkspace } = useSaaSWorkspaces();
|
|
78
|
+
*
|
|
79
|
+
* const handleDelete = async () => {
|
|
80
|
+
* if (!confirm('Are you sure?')) return;
|
|
81
|
+
* try {
|
|
82
|
+
* await deleteWorkspace(workspaceId);
|
|
83
|
+
* } catch (error) {
|
|
84
|
+
* // Error: "Only the workspace creator can delete the workspace"
|
|
85
|
+
* alert(error.message);
|
|
86
|
+
* }
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* return <button onClick={handleDelete}>Delete</button>;
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```tsx
|
|
95
|
+
* // Edge case: Workspace removed from user's access
|
|
96
|
+
* function WorkspaceContent() {
|
|
97
|
+
* const { currentWorkspace, workspaces } = useSaaSWorkspaces();
|
|
98
|
+
*
|
|
99
|
+
* // If current workspace is not in the list, it was removed
|
|
100
|
+
* // The hook automatically switches to first available workspace
|
|
101
|
+
* if (!currentWorkspace) {
|
|
102
|
+
* return <p>No workspace selected</p>;
|
|
103
|
+
* }
|
|
104
|
+
*
|
|
105
|
+
* return <div>{currentWorkspace.name}</div>;
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
4
109
|
export declare const useSaaSWorkspaces: () => {
|
|
5
110
|
workspaces: IWorkspace[];
|
|
6
111
|
loading: boolean;
|