@buildbase/sdk 0.0.9 → 0.0.11

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 CHANGED
@@ -212,7 +212,7 @@ const {
212
212
  isAuthenticated, // Boolean: true if user is authenticated
213
213
  isLoading, // Boolean: true when checking authentication status
214
214
  isRedirecting, // Boolean: true when redirecting for OAuth
215
- status, // String: 'idle' | 'loading' | 'authenticated' | 'error'
215
+ status, // AuthStatus: 'loading' | 'redirecting' | 'authenticating' | 'authenticated' | 'unauthenticated' (use AuthStatus enum for type-safe checks)
216
216
  signIn, // Function: initiates sign-in flow
217
217
  signOut, // Function: signs out the user
218
218
  openWorkspaceSettings, // Function: opens workspace settings dialog
@@ -529,6 +529,7 @@ function SettingsExample() {
529
529
  ### Enums
530
530
 
531
531
  - `ApiVersion` - API version enum (currently only `V1`)
532
+ - `AuthStatus` - Auth status enum: `loading` \| `redirecting` \| `authenticating` \| `authenticated` \| `unauthenticated`. Use with `useSaaSAuth().status`; `isLoading`, `isAuthenticated`, and `isRedirecting` are derived from it.
532
533
 
533
534
  ### Types
534
535
 
package/dist/index.d.ts CHANGED
@@ -21,6 +21,213 @@ interface IUser extends IDocument {
21
21
  currency: string;
22
22
  attributes?: Record<string, string | number | boolean>;
23
23
  }
24
+ interface ISubscription {
25
+ _id: string;
26
+ subscriptionStatus: 'active' | 'trialing' | 'canceled' | 'past_due';
27
+ cancelAtPeriodEnd: boolean;
28
+ createdAt: string;
29
+ updatedAt: string;
30
+ }
31
+ interface ISubscriptionItem {
32
+ _id: string;
33
+ type: 'feature' | 'limit' | 'quota';
34
+ name: string;
35
+ slug: string;
36
+ description?: string;
37
+ category: string;
38
+ }
39
+ interface IQuotaValue {
40
+ included: number;
41
+ overage?: number;
42
+ stripePriceId?: string;
43
+ }
44
+ interface IBasePricing {
45
+ monthly: number;
46
+ yearly: number;
47
+ quarterly: number;
48
+ }
49
+ interface IPlanVersion {
50
+ _id: string;
51
+ version: number;
52
+ name: string;
53
+ status: 'draft' | 'published';
54
+ features?: Record<string, boolean>;
55
+ limits?: Record<string, number>;
56
+ quotas?: Record<string, number | IQuotaValue>;
57
+ basePricing?: IBasePricing;
58
+ stripePrices?: {
59
+ monthlyPriceId?: string;
60
+ yearlyPriceId?: string;
61
+ quarterlyPriceId?: string;
62
+ monthly?: string;
63
+ yearly?: string;
64
+ };
65
+ subscriptionItems?: ISubscriptionItem[];
66
+ isCurrent?: boolean;
67
+ isLegacy?: boolean;
68
+ archived?: boolean;
69
+ deleted?: boolean;
70
+ createdAt?: string;
71
+ updatedAt?: string;
72
+ id?: string;
73
+ }
74
+ interface IPlan {
75
+ _id: string;
76
+ name: string;
77
+ slug: string;
78
+ description?: string;
79
+ latestVersion?: IPlanVersion;
80
+ archived?: boolean;
81
+ deleted?: boolean;
82
+ createdAt?: string;
83
+ updatedAt?: string;
84
+ id?: string;
85
+ }
86
+ interface IPlanGroupLatestVersion {
87
+ _id: string;
88
+ version: number;
89
+ planVersionIds: string[];
90
+ requiredItems: string[];
91
+ status: 'draft' | 'published';
92
+ isCurrent?: boolean;
93
+ isLegacy?: boolean;
94
+ archived?: boolean;
95
+ deleted?: boolean;
96
+ createdAt?: string;
97
+ updatedAt?: string;
98
+ description?: string;
99
+ }
100
+ interface IPlanGroup {
101
+ _id: string;
102
+ name: string;
103
+ slug: string;
104
+ description?: string;
105
+ latestVersion?: IPlanGroupLatestVersion;
106
+ archived?: boolean;
107
+ deleted?: boolean;
108
+ createdAt?: string;
109
+ updatedAt?: string;
110
+ id?: string;
111
+ }
112
+ interface IPlanGroupVersion {
113
+ _id: string;
114
+ version: number;
115
+ description?: string;
116
+ group?: {
117
+ _id: string;
118
+ name: string;
119
+ description?: string;
120
+ };
121
+ planVersionIds: IPlanVersionWithPlan[] | string[];
122
+ requiredItems?: string[];
123
+ status?: 'draft' | 'published';
124
+ isCurrent?: boolean;
125
+ isLegacy?: boolean;
126
+ archived?: boolean;
127
+ deleted?: boolean;
128
+ createdAt?: string;
129
+ updatedAt?: string;
130
+ id?: string;
131
+ }
132
+ interface ISubscriptionResponse {
133
+ subscription: ISubscription | null;
134
+ planVersion: IPlanVersion | null;
135
+ plan: IPlan | null;
136
+ group: IPlanGroup | null;
137
+ groupVersion: IPlanGroupVersion | null;
138
+ }
139
+ interface IPlanVersionWithPlan extends IPlanVersion {
140
+ plan: IPlan;
141
+ }
142
+ interface IPlanGroupVersionWithPlans {
143
+ _id: string;
144
+ version: number;
145
+ description?: string;
146
+ plans: IPlanVersionWithPlan[];
147
+ isCurrent?: boolean;
148
+ whatsNew?: {
149
+ newPlans: IPlanVersionWithPlan[];
150
+ updatedPlans: IPlanVersionWithPlan[];
151
+ removedPlans: IPlanVersionWithPlan[];
152
+ };
153
+ }
154
+ interface IPlanGroupResponse {
155
+ group: {
156
+ _id: string;
157
+ name: string;
158
+ slug: string;
159
+ };
160
+ currentVersion: IPlanGroupVersionWithPlans;
161
+ availableVersions: IPlanGroupVersionWithPlans[];
162
+ }
163
+ interface IPlanGroupVersionsResponse {
164
+ group: {
165
+ _id: string;
166
+ name: string;
167
+ slug: string;
168
+ };
169
+ currentVersion: IPlanGroupVersionWithPlans;
170
+ availableVersions: IPlanGroupVersionWithPlans[];
171
+ }
172
+ type BillingInterval = 'monthly' | 'yearly' | 'quarterly';
173
+ interface ICheckoutSessionRequest {
174
+ planVersionId: string;
175
+ billingInterval?: BillingInterval;
176
+ successUrl?: string;
177
+ cancelUrl?: string;
178
+ }
179
+ interface ICheckoutSessionResponse {
180
+ success: boolean;
181
+ checkoutUrl: string;
182
+ sessionId: string;
183
+ message: string;
184
+ planVersion?: {
185
+ _id: string;
186
+ version: number;
187
+ name: string;
188
+ };
189
+ }
190
+ interface ISubscriptionUpdateRequest {
191
+ planVersionId: string;
192
+ billingInterval?: BillingInterval;
193
+ successUrl?: string;
194
+ cancelUrl?: string;
195
+ }
196
+ interface ISubscriptionUpdateResponse {
197
+ _id: string;
198
+ subscriptionStatus: string;
199
+ workspace: string;
200
+ planVersion: string;
201
+ plan: string;
202
+ planGroup?: string;
203
+ planGroupVersion?: string;
204
+ cancelAtPeriodEnd: boolean;
205
+ createdAt: string;
206
+ updatedAt: string;
207
+ }
208
+ type InvoiceStatus = 'draft' | 'open' | 'paid' | 'uncollectible' | 'void';
209
+ interface IInvoice {
210
+ id: string;
211
+ amount_due: number;
212
+ amount_paid: number;
213
+ currency: string;
214
+ status: InvoiceStatus;
215
+ created: number;
216
+ due_date: number | null;
217
+ hosted_invoice_url: string;
218
+ invoice_pdf: string | null;
219
+ description: string | null;
220
+ subscription: string;
221
+ }
222
+ interface IInvoiceListResponse {
223
+ success: boolean;
224
+ invoices: IInvoice[];
225
+ has_more: boolean;
226
+ }
227
+ interface IInvoiceResponse {
228
+ success: boolean;
229
+ invoice: IInvoice;
230
+ }
24
231
 
25
232
  interface IWorkspace {
26
233
  _id: string;
@@ -112,6 +319,7 @@ interface IEventCallbacks {
112
319
 
113
320
  declare enum AuthStatus {
114
321
  loading = "loading",
322
+ redirecting = "redirecting",
115
323
  authenticated = "authenticated",
116
324
  unauthenticated = "unauthenticated",
117
325
  authenticating = "authenticating"
@@ -227,15 +435,15 @@ declare const WhenUserFeatureEnabled: (props: IProps) => react.ReactNode;
227
435
  declare const WhenUserFeatureDisabled: (props: IProps) => react.ReactNode;
228
436
 
229
437
  declare function useSaaSAuth(): {
230
- user: AuthUser | undefined;
231
- session: AuthSession | null;
232
- isLoading: boolean;
233
- isAuthenticated: boolean;
234
- isRedirecting: boolean;
235
- status: AuthStatus;
236
438
  signIn: () => Promise<void>;
237
439
  signOut: () => Promise<void>;
238
440
  openWorkspaceSettings: (section?: "profile" | "general" | "users" | "features" | "danger") => void;
441
+ isAuthenticated: boolean;
442
+ isLoading: boolean;
443
+ isRedirecting: boolean;
444
+ user: AuthUser | undefined;
445
+ session: AuthSession | null;
446
+ status: AuthStatus;
239
447
  };
240
448
 
241
449
  declare function useSaaSSettings(): {
@@ -309,6 +517,113 @@ declare const useSaaSWorkspaces: () => {
309
517
  }>;
310
518
  };
311
519
 
520
+ /**
521
+ * Hook to get and manage the current subscription for a workspace
522
+ * @param workspaceId - The workspace ID to get subscription for
523
+ * @returns Subscription data and loading/error states
524
+ */
525
+ declare const useSubscription: (workspaceId: string | null | undefined) => {
526
+ subscription: ISubscriptionResponse | null;
527
+ loading: boolean;
528
+ error: string | null;
529
+ refetch: () => Promise<void>;
530
+ };
531
+ /**
532
+ * Hook to get the plan group for a workspace
533
+ * Returns the plan group containing the current plan if subscription exists,
534
+ * otherwise returns the latest published group
535
+ * @param workspaceId - The workspace ID to get plan group for
536
+ * @param groupVersionId - Optional: specific group version ID to fetch
537
+ * @returns Plan group data and loading/error states
538
+ */
539
+ declare const usePlanGroup: (workspaceId: string | null | undefined, groupVersionId?: string | null) => {
540
+ planGroup: IPlanGroupResponse | null;
541
+ loading: boolean;
542
+ error: string | null;
543
+ refetch: () => Promise<void>;
544
+ };
545
+ /**
546
+ * Hook to get all available versions of a plan group for a workspace
547
+ * @param workspaceId - The workspace ID to get plan group versions for
548
+ * @returns Plan group versions data and loading/error states
549
+ */
550
+ declare const usePlanGroupVersions: (workspaceId: string | null | undefined) => {
551
+ versions: IPlanGroupVersionsResponse | null;
552
+ loading: boolean;
553
+ error: string | null;
554
+ refetch: () => Promise<void>;
555
+ };
556
+ /**
557
+ * Hook to create checkout session for new subscription
558
+ * @param workspaceId - The workspace ID to create checkout session for
559
+ * @returns Create checkout function and loading/error states
560
+ */
561
+ declare const useCreateCheckoutSession: (workspaceId: string | null | undefined) => {
562
+ createCheckoutSession: (request: ICheckoutSessionRequest) => Promise<ICheckoutSessionResponse>;
563
+ loading: boolean;
564
+ error: string | null;
565
+ };
566
+ /**
567
+ * Hook to update subscription (upgrade/downgrade)
568
+ * Returns checkout session if payment is required, otherwise returns subscription update response
569
+ * @param workspaceId - The workspace ID to update subscription for
570
+ * @returns Update function and loading/error states
571
+ */
572
+ declare const useUpdateSubscription: (workspaceId: string | null | undefined) => {
573
+ updateSubscription: (planVersionId: string, options?: {
574
+ billingInterval?: BillingInterval;
575
+ successUrl?: string;
576
+ cancelUrl?: string;
577
+ }) => Promise<ISubscriptionUpdateResponse | ICheckoutSessionResponse>;
578
+ loading: boolean;
579
+ error: string | null;
580
+ };
581
+ /**
582
+ * Combined hook that provides both subscription and plan group data
583
+ * Useful for subscription management pages
584
+ * @param workspaceId - The workspace ID
585
+ * @param groupVersionId - Optional: specific group version ID to fetch
586
+ * @returns Combined subscription and plan group data with loading/error states
587
+ */
588
+ declare const useSubscriptionManagement: (workspaceId: string | null | undefined, groupVersionId?: string | null) => {
589
+ subscription: ISubscriptionResponse | null;
590
+ planGroup: IPlanGroupResponse | null;
591
+ loading: boolean;
592
+ error: string | null;
593
+ updateSubscription: (planVersionId: string, options?: {
594
+ billingInterval?: BillingInterval;
595
+ successUrl?: string;
596
+ cancelUrl?: string;
597
+ }) => Promise<ISubscriptionUpdateResponse | ICheckoutSessionResponse>;
598
+ refetch: () => Promise<void>;
599
+ };
600
+ /**
601
+ * Hook to list invoices for a workspace subscription
602
+ * @param workspaceId - The workspace ID to get invoices for
603
+ * @param limit - Number of invoices to return (default: 10)
604
+ * @param startingAfter - Invoice ID to start after (for pagination)
605
+ * @returns Invoice list data and loading/error states
606
+ */
607
+ declare const useInvoices: (workspaceId: string | null | undefined, limit?: number, startingAfter?: string) => {
608
+ invoices: IInvoice[];
609
+ hasMore: boolean;
610
+ loading: boolean;
611
+ error: string | null;
612
+ refetch: () => Promise<void>;
613
+ };
614
+ /**
615
+ * Hook to get a single invoice by ID
616
+ * @param workspaceId - The workspace ID
617
+ * @param invoiceId - The invoice ID to fetch
618
+ * @returns Invoice data and loading/error states
619
+ */
620
+ declare const useInvoice: (workspaceId: string | null | undefined, invoiceId: string | null | undefined) => {
621
+ invoice: IInvoice | null;
622
+ loading: boolean;
623
+ error: string | null;
624
+ refetch: () => Promise<void>;
625
+ };
626
+
312
627
  /**
313
628
  * EventEmitter class to handle and trigger event callbacks
314
629
  * This class manages all event listeners and provides methods to trigger events
@@ -481,5 +796,5 @@ declare const errorHandler: ErrorHandler;
481
796
  declare function handleError(error: Error | unknown, context?: SDKErrorContext): void;
482
797
  declare function createSDKError(message: string, code?: string, context?: SDKErrorContext, originalError?: Error): SDKError;
483
798
 
484
- export { ApiVersion, BetaForm, SDKErrorBoundary as ErrorBoundary, SDKError, SDKErrorBoundary, SaaSOSProvider, WhenAuthenticated, WhenRoles, WhenUnauthenticated, WhenUserFeatureDisabled, WhenUserFeatureEnabled, WhenWorkspaceFeatureDisabled, WhenWorkspaceFeatureEnabled, WhenWorkspaceRoles, WorkspaceSwitcher, createSDKError, errorHandler, eventEmitter, handleError, useSaaSAuth, useSaaSSettings, useSaaSWorkspaces, useUserAttributes, useUserFeatures };
485
- export type { ErrorHandlerConfig, EventData, EventType, IEventCallbacks, SDKErrorContext, UserCreatedEventData, UserUpdatedEventData, WorkspaceChangedEventData, WorkspaceCreatedEventData, WorkspaceDeletedEventData, WorkspaceUpdatedEventData, WorkspaceUserAddedEventData, WorkspaceUserRemovedEventData, WorkspaceUserRoleChangedEventData };
799
+ export { ApiVersion, AuthStatus, BetaForm, SDKErrorBoundary as ErrorBoundary, SDKError, SDKErrorBoundary, SaaSOSProvider, WhenAuthenticated, WhenRoles, WhenUnauthenticated, WhenUserFeatureDisabled, WhenUserFeatureEnabled, WhenWorkspaceFeatureDisabled, WhenWorkspaceFeatureEnabled, WhenWorkspaceRoles, WorkspaceSwitcher, createSDKError, errorHandler, eventEmitter, handleError, useCreateCheckoutSession, useInvoice, useInvoices, usePlanGroup, usePlanGroupVersions, useSaaSAuth, useSaaSSettings, useSaaSWorkspaces, useSubscription, useSubscriptionManagement, useUpdateSubscription, useUserAttributes, useUserFeatures };
800
+ export type { BillingInterval, ErrorHandlerConfig, EventData, EventType, IBasePricing, ICheckoutSessionRequest, ICheckoutSessionResponse, IEventCallbacks, IInvoice, IInvoiceListResponse, IInvoiceResponse, IPlan, IPlanGroup, IPlanGroupLatestVersion, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionWithPlans, IPlanGroupVersionsResponse, IPlanVersion, IPlanVersionWithPlan, IQuotaValue, ISubscription, ISubscriptionItem, ISubscriptionResponse, ISubscriptionUpdateRequest, ISubscriptionUpdateResponse, InvoiceStatus, SDKErrorContext, UserCreatedEventData, UserUpdatedEventData, WorkspaceChangedEventData, WorkspaceCreatedEventData, WorkspaceDeletedEventData, WorkspaceUpdatedEventData, WorkspaceUserAddedEventData, WorkspaceUserRemovedEventData, WorkspaceUserRoleChangedEventData };