@buildbase/sdk 0.0.11 → 0.0.13

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 (37) hide show
  1. package/README.md +34 -13
  2. package/dist/index.d.ts +896 -134
  3. package/dist/index.esm.js +5 -11
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.js +5 -11
  6. package/dist/index.js.map +1 -1
  7. package/dist/saas-os.css +1 -1
  8. package/dist/types/components/ErrorBoundary.d.ts +49 -2
  9. package/dist/types/components/dropdowns/country/selectCountry.d.ts +2 -2
  10. package/dist/types/components/dropdowns/currency/selectCurrency.d.ts +2 -2
  11. package/dist/types/components/dropdowns/language/selectLanguage.d.ts +2 -2
  12. package/dist/types/components/dropdowns/timezone/selectTimeZone.d.ts +2 -2
  13. package/dist/types/components/features/index.d.ts +109 -0
  14. package/dist/types/components/ui/command-select.d.ts +13 -0
  15. package/dist/types/components/ui/command.d.ts +2 -2
  16. package/dist/types/components/user/auth.d.ts +64 -0
  17. package/dist/types/components/user/role.d.ts +70 -0
  18. package/dist/types/contexts/WorkspaceContext/actions.d.ts +1 -1
  19. package/dist/types/contexts/WorkspaceContext/types.d.ts +3 -3
  20. package/dist/types/contexts/shared/useSelectWithEquality.d.ts +10 -0
  21. package/dist/types/index.d.ts +1 -3
  22. package/dist/types/lib/api-utils.d.ts +182 -0
  23. package/dist/types/lib/error-handler.d.ts +58 -0
  24. package/dist/types/lib/logger.d.ts +27 -0
  25. package/dist/types/lib/useAsyncEffect.d.ts +29 -0
  26. package/dist/types/lib/utils.d.ts +5 -0
  27. package/dist/types/providers/auth/hooks.d.ts +61 -0
  28. package/dist/types/providers/auth/types.d.ts +12 -0
  29. package/dist/types/providers/auth/utils.d.ts +7 -2
  30. package/dist/types/providers/constants.d.ts +1 -0
  31. package/dist/types/providers/os/hooks.d.ts +40 -1
  32. package/dist/types/providers/user/hooks.d.ts +71 -0
  33. package/dist/types/providers/workspace/hooks.d.ts +112 -4
  34. package/dist/types/providers/workspace/provider.d.ts +0 -1
  35. package/dist/types/providers/workspace/subscription-hooks.d.ts +351 -29
  36. package/dist/types/providers/workspace/types.d.ts +1 -1
  37. package/package.json +14 -12
package/README.md CHANGED
@@ -95,6 +95,11 @@ export default function SaaSProvider(props: { children: React.ReactNode }) {
95
95
  // Handle SDK events (user created, workspace changed, etc.)
96
96
  console.log('SDK Event:', eventType, data);
97
97
  },
98
+ onWorkspaceChange: async ({ workspace, user, role }) => {
99
+ // Called before switching workspace (e.g. generate token). Used on "Switch to" and page refresh/restore.
100
+ // Switch proceeds only when this resolves; reject to abort.
101
+ console.log('Switching to workspace:', workspace.name, 'as', role);
102
+ },
98
103
  },
99
104
  }}
100
105
  >
@@ -124,7 +129,7 @@ export default App;
124
129
 
125
130
  ### 4. Workspace Management
126
131
 
127
- The WorkspaceSwitcher component uses a render prop pattern, giving you full control over the UI:
132
+ The WorkspaceSwitcher component uses a render prop pattern, giving you full control over the UI. Configure `onWorkspaceChange` in `auth.callbacks` (SaaSOSProvider) to handle workspace switches—used when clicking "Switch to" and when restoring from storage on page refresh. The callback receives `{ workspace, user, role }` so you don't need to look up the user's role:
128
133
 
129
134
  ```tsx
130
135
  import React from 'react';
@@ -160,10 +165,6 @@ function WorkspaceExample() {
160
165
  </div>
161
166
  );
162
167
  }}
163
- onWorkspaceChange={async workspace => {
164
- // Handle workspace change
165
- console.log('Workspace changed to:', workspace);
166
- }}
167
168
  />
168
169
  );
169
170
  }
@@ -372,10 +373,12 @@ function WorkspaceManager() {
372
373
  currentWorkspace, // Currently selected workspace
373
374
  loading, // Loading state
374
375
  refreshing, // Refreshing state
376
+ switching, // True when a workspace switch is in progress
375
377
  error, // Error message
376
378
  fetchWorkspaces, // Fetch all workspaces
377
379
  refreshWorkspaces, // Background refresh
378
- setCurrentWorkspace, // Switch workspace
380
+ setCurrentWorkspace, // Direct workspace set (bypasses onWorkspaceChange)
381
+ switchToWorkspace, // Full switch flow: onWorkspaceChange first, then set workspace
379
382
  createWorkspace, // Create new workspace
380
383
  updateWorkspace, // Update workspace
381
384
  deleteWorkspace, // Delete workspace
@@ -460,7 +463,7 @@ import { SaaSOSProvider, eventEmitter } from '@buildbase/sdk';
460
463
 
461
464
  - `user:created` - User account created
462
465
  - `user:updated` - User profile updated
463
- - `workspace:changed` - Workspace switched
466
+ - `workspace:changed` - Workspace switched (fires after switch completes; use `onWorkspaceChange` for prep before switch)
464
467
  - `workspace:created` - New workspace created
465
468
  - `workspace:updated` - Workspace updated
466
469
  - `workspace:deleted` - Workspace deleted
@@ -557,8 +560,15 @@ interface IAuthConfig {
557
560
  handleAuthentication: (code: string) => Promise<{ sessionId: string }>;
558
561
  onSignOut?: () => Promise<void>;
559
562
  handleEvent?: (eventType: EventType, data: EventData) => void | Promise<void>;
563
+ onWorkspaceChange?: (params: OnWorkspaceChangeParams) => Promise<void>;
560
564
  };
561
565
  }
566
+
567
+ interface OnWorkspaceChangeParams {
568
+ workspace: IWorkspace;
569
+ user: AuthUser | null;
570
+ role: string | null; // User's role in this workspace
571
+ }
562
572
  ```
563
573
 
564
574
  ### Validation Requirements
@@ -683,7 +693,7 @@ import { useSaaSWorkspaces } from '@buildbase/sdk';
683
693
  import { useEffect } from 'react';
684
694
 
685
695
  function App() {
686
- const { currentWorkspace, setCurrentWorkspace } = useSaaSWorkspaces();
696
+ const { currentWorkspace, switching } = useSaaSWorkspaces();
687
697
 
688
698
  useEffect(() => {
689
699
  if (currentWorkspace) {
@@ -693,10 +703,15 @@ function App() {
693
703
  }
694
704
  }, [currentWorkspace]);
695
705
 
706
+ // Show loading during switch (switchingToId !== null)
707
+ if (switching) return <LoadingOverlay />;
708
+
696
709
  return <YourApp />;
697
710
  }
698
711
  ```
699
712
 
713
+ For token generation or prep before switching, configure `onWorkspaceChange` in auth callbacks (see Quick Start)—it receives `{ workspace, user, role }`.
714
+
700
715
  ### Pattern 6: Error Boundary with Custom Fallback
701
716
 
702
717
  ```tsx
@@ -863,7 +878,7 @@ A: Yes, as long as you're using React 19+.
863
878
  A: Use the `trigger` render prop to fully customize the UI.
864
879
 
865
880
  **Q: Can I use multiple workspaces simultaneously?**
866
- A: No, the SDK manages one current workspace at a time. Switch between workspaces using `setCurrentWorkspace()`.
881
+ A: No, the SDK manages one current workspace at a time. Use `switchToWorkspace()` (runs `onWorkspaceChange` first) or `setCurrentWorkspace()` (direct set, bypasses callback).
867
882
 
868
883
  **Q: How do I handle offline scenarios?**
869
884
  A: The SDK stores session data in localStorage. Handle offline scenarios in your `handleAuthentication` callback.
@@ -933,9 +948,13 @@ try {
933
948
 
934
949
  ```tsx
935
950
  // ✅ Good
936
- const { currentWorkspace, setCurrentWorkspace } = useSaaSWorkspaces();
951
+ const { currentWorkspace, switchToWorkspace, switching } = useSaaSWorkspaces();
952
+ // switchToWorkspace: runs onWorkspaceChange first (token gen, etc.)
953
+ // switching: true when switch is in progress
937
954
  ```
938
955
 
956
+ ✅ **Do**: Configure `onWorkspaceChange` in auth callbacks for token generation—receives `{ workspace, user, role }`.
957
+
939
958
  ❌ **Don't**: Manually manage workspace state.
940
959
 
941
960
  ```tsx
@@ -987,17 +1006,19 @@ const { signIn, status } = useSaaSAuth();
987
1006
 
988
1007
  ### 6. Event Handling
989
1008
 
990
- ✅ **Do**: Handle events in your provider configuration.
1009
+ ✅ **Do**: Handle events in your provider configuration. Use `onWorkspaceChange` for prep before switch (e.g. generate token), and `handleEvent` for post-switch notifications.
991
1010
 
992
1011
  ```tsx
993
1012
  // ✅ Good
994
1013
  <SaaSOSProvider
995
1014
  auth={{
996
1015
  callbacks: {
1016
+ onWorkspaceChange: async ({ workspace, user, role }) => {
1017
+ await generateTokenForWorkspace(workspace._id, user?.id, role);
1018
+ },
997
1019
  handleEvent: async (eventType, data) => {
998
- // Handle events
999
1020
  if (eventType === 'workspace:changed') {
1000
- // Update your app state
1021
+ // Workspace already switched; update app state
1001
1022
  }
1002
1023
  },
1003
1024
  },