@buildbase/sdk 0.0.19 → 0.0.20
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 +521 -2
- package/dist/index.d.ts +564 -13
- package/dist/index.esm.js +5 -5
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/types/api/types.d.ts +63 -0
- package/dist/types/components/features/index.d.ts +16 -4
- package/dist/types/components/quota/index.d.ts +121 -0
- package/dist/types/components/subscription/index.d.ts +12 -3
- package/dist/types/components/user/auth.d.ts +8 -2
- package/dist/types/components/user/role.d.ts +8 -2
- package/dist/types/contexts/QuotaUsageContext/QuotaUsageContext.d.ts +22 -0
- package/dist/types/contexts/QuotaUsageContext/index.d.ts +2 -0
- package/dist/types/contexts/QuotaUsageContext/quotaUsageInvalidation.d.ts +19 -0
- package/dist/types/contexts/QuotaUsageContext/types.d.ts +14 -0
- package/dist/types/contexts/SubscriptionContext/types.d.ts +2 -0
- package/dist/types/index.d.ts +7 -2
- package/dist/types/providers/workspace/api.d.ts +28 -1
- package/dist/types/providers/workspace/subscription-hooks.d.ts +179 -1
- package/package.json +1 -1
|
@@ -282,6 +282,7 @@ export interface IPlanGroupVersionsResponse {
|
|
|
282
282
|
export interface ICheckoutSessionRequest {
|
|
283
283
|
planVersionId: string;
|
|
284
284
|
billingInterval?: BillingInterval;
|
|
285
|
+
currency?: string;
|
|
285
286
|
successUrl?: string;
|
|
286
287
|
cancelUrl?: string;
|
|
287
288
|
}
|
|
@@ -375,3 +376,65 @@ export interface IInvoiceResponse {
|
|
|
375
376
|
success: boolean;
|
|
376
377
|
invoice: IInvoice;
|
|
377
378
|
}
|
|
379
|
+
/** Request body for POST .../workspaces/:id/subscription/usage */
|
|
380
|
+
export interface IRecordUsageRequest {
|
|
381
|
+
quotaSlug: string;
|
|
382
|
+
quantity: number;
|
|
383
|
+
metadata?: Record<string, unknown>;
|
|
384
|
+
source?: string;
|
|
385
|
+
idempotencyKey?: string;
|
|
386
|
+
}
|
|
387
|
+
/** Response from POST .../workspaces/:id/subscription/usage */
|
|
388
|
+
export interface IRecordUsageResponse {
|
|
389
|
+
used: number;
|
|
390
|
+
consumed: number;
|
|
391
|
+
included: number;
|
|
392
|
+
available: number;
|
|
393
|
+
overage: number;
|
|
394
|
+
billedAsync: boolean;
|
|
395
|
+
}
|
|
396
|
+
/** Single quota usage status (shared between status and all endpoints). */
|
|
397
|
+
export interface IQuotaUsageStatus {
|
|
398
|
+
consumed: number;
|
|
399
|
+
included: number;
|
|
400
|
+
available: number;
|
|
401
|
+
overage: number;
|
|
402
|
+
hasOverage: boolean;
|
|
403
|
+
}
|
|
404
|
+
/** Response from GET .../workspaces/:id/subscription/usage/status?quotaSlug=X */
|
|
405
|
+
export interface IQuotaUsageStatusResponse extends IQuotaUsageStatus {
|
|
406
|
+
quotaSlug: string;
|
|
407
|
+
}
|
|
408
|
+
/** Response from GET .../workspaces/:id/subscription/usage/all */
|
|
409
|
+
export interface IAllQuotaUsageResponse {
|
|
410
|
+
quotas: Record<string, IQuotaUsageStatus>;
|
|
411
|
+
}
|
|
412
|
+
/** Single usage log entry from GET .../usage/logs */
|
|
413
|
+
export interface IUsageLogEntry {
|
|
414
|
+
_id: string;
|
|
415
|
+
quotaSlug: string;
|
|
416
|
+
quantity: number;
|
|
417
|
+
source?: string;
|
|
418
|
+
workspace: string;
|
|
419
|
+
createdAt: string;
|
|
420
|
+
updatedAt: string;
|
|
421
|
+
}
|
|
422
|
+
/** Query parameters for GET .../workspaces/:id/subscription/usage/logs */
|
|
423
|
+
export interface IUsageLogsQuery {
|
|
424
|
+
quotaSlug?: string;
|
|
425
|
+
from?: string;
|
|
426
|
+
to?: string;
|
|
427
|
+
source?: string;
|
|
428
|
+
page?: number;
|
|
429
|
+
limit?: number;
|
|
430
|
+
}
|
|
431
|
+
/** Paginated response from GET .../workspaces/:id/subscription/usage/logs */
|
|
432
|
+
export interface IUsageLogsResponse {
|
|
433
|
+
docs: IUsageLogEntry[];
|
|
434
|
+
totalDocs: number;
|
|
435
|
+
limit: number;
|
|
436
|
+
page: number;
|
|
437
|
+
totalPages: number;
|
|
438
|
+
hasNextPage: boolean;
|
|
439
|
+
hasPrevPage: boolean;
|
|
440
|
+
}
|
|
@@ -38,7 +38,10 @@ interface IProps {
|
|
|
38
38
|
* }
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
export declare const WhenWorkspaceFeatureEnabled:
|
|
41
|
+
export declare const WhenWorkspaceFeatureEnabled: {
|
|
42
|
+
(props: IProps): import("react").ReactNode;
|
|
43
|
+
displayName: string;
|
|
44
|
+
};
|
|
42
45
|
/**
|
|
43
46
|
* Conditional component that renders children only when the specified workspace feature is disabled.
|
|
44
47
|
* Checks feature flags at the workspace level.
|
|
@@ -58,7 +61,10 @@ export declare const WhenWorkspaceFeatureEnabled: (props: IProps) => import("rea
|
|
|
58
61
|
* }
|
|
59
62
|
* ```
|
|
60
63
|
*/
|
|
61
|
-
export declare const WhenWorkspaceFeatureDisabled:
|
|
64
|
+
export declare const WhenWorkspaceFeatureDisabled: {
|
|
65
|
+
(props: IProps): import("react").ReactNode;
|
|
66
|
+
displayName: string;
|
|
67
|
+
};
|
|
62
68
|
/**
|
|
63
69
|
* Conditional component that renders children only when the specified user feature is enabled.
|
|
64
70
|
* Checks feature flags at the user level (from UserProvider).
|
|
@@ -94,7 +100,10 @@ export declare const WhenWorkspaceFeatureDisabled: (props: IProps) => import("re
|
|
|
94
100
|
* }
|
|
95
101
|
* ```
|
|
96
102
|
*/
|
|
97
|
-
export declare const WhenUserFeatureEnabled:
|
|
103
|
+
export declare const WhenUserFeatureEnabled: {
|
|
104
|
+
(props: IProps): import("react").ReactNode;
|
|
105
|
+
displayName: string;
|
|
106
|
+
};
|
|
98
107
|
/**
|
|
99
108
|
* Conditional component that renders children only when the specified user feature is disabled.
|
|
100
109
|
* Checks feature flags at the user level (from UserProvider).
|
|
@@ -114,5 +123,8 @@ export declare const WhenUserFeatureEnabled: (props: IProps) => import("react").
|
|
|
114
123
|
* }
|
|
115
124
|
* ```
|
|
116
125
|
*/
|
|
117
|
-
export declare const WhenUserFeatureDisabled:
|
|
126
|
+
export declare const WhenUserFeatureDisabled: {
|
|
127
|
+
(props: IProps): import("react").ReactNode;
|
|
128
|
+
displayName: string;
|
|
129
|
+
};
|
|
118
130
|
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
interface IWhenQuotaProps {
|
|
2
|
+
/** Quota slug to check (e.g. 'api_calls', 'emails', 'storage'). */
|
|
3
|
+
slug: string;
|
|
4
|
+
/** Content to render when the condition is met. */
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
/** Optional component/element to show while quota usage is loading (e.g. <Skeleton />). */
|
|
7
|
+
loadingComponent?: React.ReactNode;
|
|
8
|
+
/** Optional component/element to show when condition is not met (e.g. <UpgradePrompt />). */
|
|
9
|
+
fallbackComponent?: React.ReactNode;
|
|
10
|
+
}
|
|
11
|
+
interface IWhenQuotaThresholdProps extends IWhenQuotaProps {
|
|
12
|
+
/** Usage percentage threshold (0-100). Children render when usage >= this percentage. */
|
|
13
|
+
threshold: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Renders children only when the specified quota has remaining units (available > 0).
|
|
17
|
+
* Must be used within QuotaUsageContextProvider (included in SaaSOSProvider by default).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* <WhenQuotaAvailable slug="api_calls">
|
|
22
|
+
* <MakeApiCallButton />
|
|
23
|
+
* </WhenQuotaAvailable>
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* <WhenQuotaAvailable
|
|
29
|
+
* slug="emails"
|
|
30
|
+
* loadingComponent={<Skeleton />}
|
|
31
|
+
* fallbackComponent={<p>Email quota exhausted. <UpgradeLink /></p>}
|
|
32
|
+
* >
|
|
33
|
+
* <SendEmailButton />
|
|
34
|
+
* </WhenQuotaAvailable>
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const WhenQuotaAvailable: {
|
|
38
|
+
(props: IWhenQuotaProps): import("react").ReactNode;
|
|
39
|
+
displayName: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Renders children only when the specified quota is fully consumed (available <= 0).
|
|
43
|
+
* Must be used within QuotaUsageContextProvider (included in SaaSOSProvider by default).
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* <WhenQuotaExhausted slug="api_calls">
|
|
48
|
+
* <UpgradePrompt message="You've used all your API calls this month." />
|
|
49
|
+
* </WhenQuotaExhausted>
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* <WhenQuotaExhausted
|
|
55
|
+
* slug="storage"
|
|
56
|
+
* loadingComponent={<Spinner />}
|
|
57
|
+
* fallbackComponent={null}
|
|
58
|
+
* >
|
|
59
|
+
* <StorageFullBanner />
|
|
60
|
+
* </WhenQuotaExhausted>
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare const WhenQuotaExhausted: {
|
|
64
|
+
(props: IWhenQuotaProps): import("react").ReactNode;
|
|
65
|
+
displayName: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Renders children only when the specified quota is in overage (consumed > included).
|
|
69
|
+
* Must be used within QuotaUsageContextProvider (included in SaaSOSProvider by default).
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```tsx
|
|
73
|
+
* <WhenQuotaOverage slug="api_calls">
|
|
74
|
+
* <OverageBillingWarning />
|
|
75
|
+
* </WhenQuotaOverage>
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```tsx
|
|
80
|
+
* <WhenQuotaOverage
|
|
81
|
+
* slug="emails"
|
|
82
|
+
* loadingComponent={<Skeleton />}
|
|
83
|
+
* fallbackComponent={null}
|
|
84
|
+
* >
|
|
85
|
+
* <p>You are being billed for overage usage.</p>
|
|
86
|
+
* </WhenQuotaOverage>
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare const WhenQuotaOverage: {
|
|
90
|
+
(props: IWhenQuotaProps): import("react").ReactNode;
|
|
91
|
+
displayName: string;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Renders children when the specified quota's usage percentage reaches or exceeds the threshold.
|
|
95
|
+
* Usage percentage = (consumed / included) * 100. If included is 0 and consumed > 0, treats as 100%.
|
|
96
|
+
* Must be used within QuotaUsageContextProvider (included in SaaSOSProvider by default).
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* <WhenQuotaThreshold slug="api_calls" threshold={80}>
|
|
101
|
+
* <p>Warning: You've used over 80% of your API calls.</p>
|
|
102
|
+
* </WhenQuotaThreshold>
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```tsx
|
|
107
|
+
* <WhenQuotaThreshold
|
|
108
|
+
* slug="storage"
|
|
109
|
+
* threshold={90}
|
|
110
|
+
* loadingComponent={<Spinner />}
|
|
111
|
+
* fallbackComponent={null}
|
|
112
|
+
* >
|
|
113
|
+
* <StorageWarningBanner />
|
|
114
|
+
* </WhenQuotaThreshold>
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare const WhenQuotaThreshold: {
|
|
118
|
+
(props: IWhenQuotaThresholdProps): import("react").ReactNode;
|
|
119
|
+
displayName: string;
|
|
120
|
+
};
|
|
121
|
+
export {};
|
|
@@ -34,7 +34,10 @@ interface IWhenSubscriptionProps {
|
|
|
34
34
|
* </WhenSubscription>
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
export declare const WhenSubscription:
|
|
37
|
+
export declare const WhenSubscription: {
|
|
38
|
+
(props: IWhenSubscriptionProps): import("react").ReactNode;
|
|
39
|
+
displayName: string;
|
|
40
|
+
};
|
|
38
41
|
/**
|
|
39
42
|
* Renders children only when the current workspace has no subscription (or no current workspace).
|
|
40
43
|
* Optionally pass loadingComponent (while loading) or fallbackComponent (when already subscribed).
|
|
@@ -63,7 +66,10 @@ export declare const WhenSubscription: (props: IWhenSubscriptionProps) => import
|
|
|
63
66
|
* </WhenNoSubscription>
|
|
64
67
|
* ```
|
|
65
68
|
*/
|
|
66
|
-
export declare const WhenNoSubscription:
|
|
69
|
+
export declare const WhenNoSubscription: {
|
|
70
|
+
(props: IWhenSubscriptionProps): import("react").ReactNode;
|
|
71
|
+
displayName: string;
|
|
72
|
+
};
|
|
67
73
|
interface IWhenSubscriptionToPlansProps {
|
|
68
74
|
/** Plan slugs to match (e.g. ['pro', 'enterprise']). Matching is case-insensitive. */
|
|
69
75
|
plans: string[];
|
|
@@ -104,5 +110,8 @@ interface IWhenSubscriptionToPlansProps {
|
|
|
104
110
|
* </WhenSubscriptionToPlans>
|
|
105
111
|
* ```
|
|
106
112
|
*/
|
|
107
|
-
export declare const WhenSubscriptionToPlans:
|
|
113
|
+
export declare const WhenSubscriptionToPlans: {
|
|
114
|
+
(props: IWhenSubscriptionToPlansProps): import("react").ReactNode;
|
|
115
|
+
displayName: string;
|
|
116
|
+
};
|
|
108
117
|
export {};
|
|
@@ -24,7 +24,10 @@ interface IProps {
|
|
|
24
24
|
* }
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
export declare const WhenAuthenticated:
|
|
27
|
+
export declare const WhenAuthenticated: {
|
|
28
|
+
(props: IProps): import("react").ReactNode;
|
|
29
|
+
displayName: string;
|
|
30
|
+
};
|
|
28
31
|
/**
|
|
29
32
|
* Conditional component that renders children only when user is NOT authenticated.
|
|
30
33
|
* Returns null if user is authenticated.
|
|
@@ -66,5 +69,8 @@ export declare const WhenAuthenticated: (props: IProps) => import("react").React
|
|
|
66
69
|
* }
|
|
67
70
|
* ```
|
|
68
71
|
*/
|
|
69
|
-
export declare const WhenUnauthenticated:
|
|
72
|
+
export declare const WhenUnauthenticated: {
|
|
73
|
+
(props: IProps): import("react").ReactNode;
|
|
74
|
+
displayName: string;
|
|
75
|
+
};
|
|
70
76
|
export {};
|
|
@@ -38,7 +38,10 @@ interface IProps {
|
|
|
38
38
|
* }
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
export declare const WhenRoles:
|
|
41
|
+
export declare const WhenRoles: {
|
|
42
|
+
(props: IProps): import("react").ReactNode;
|
|
43
|
+
displayName: string;
|
|
44
|
+
};
|
|
42
45
|
/**
|
|
43
46
|
* Conditional component that renders children only when user has one of the specified roles
|
|
44
47
|
* in the current workspace. Checks workspace-specific role, not global role.
|
|
@@ -74,5 +77,8 @@ export declare const WhenRoles: (props: IProps) => import("react").ReactNode;
|
|
|
74
77
|
* }
|
|
75
78
|
* ```
|
|
76
79
|
*/
|
|
77
|
-
export declare const WhenWorkspaceRoles:
|
|
80
|
+
export declare const WhenWorkspaceRoles: {
|
|
81
|
+
(props: IProps): import("react").ReactNode;
|
|
82
|
+
displayName: string;
|
|
83
|
+
};
|
|
78
84
|
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React, { type ReactNode } from 'react';
|
|
2
|
+
import type { QuotaUsageContextValue } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Provides quota usage data for the current workspace to quota gate components.
|
|
5
|
+
* Fetches when workspace changes; refetches when quota usage is invalidated (e.g. after recording usage).
|
|
6
|
+
* Must wrap (or be ancestor of) any component that uses WhenQuotaAvailable, WhenQuotaExhausted,
|
|
7
|
+
* WhenQuotaOverage, WhenQuotaThreshold, or useQuotaUsageContext. Included in SaaSOSProvider by default.
|
|
8
|
+
*
|
|
9
|
+
* @param props - Component props
|
|
10
|
+
* @param props.children - React tree that may use quota gates or useQuotaUsageContext
|
|
11
|
+
* @returns Provider element that supplies quota usage context to descendants
|
|
12
|
+
*/
|
|
13
|
+
export declare const QuotaUsageContextProvider: React.FC<{
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* Returns quota usage data for the current workspace. Must be used within QuotaUsageContextProvider.
|
|
18
|
+
*
|
|
19
|
+
* @returns QuotaUsageContextValue - { quotas, loading, refetch }
|
|
20
|
+
* @throws Error if used outside QuotaUsageContextProvider
|
|
21
|
+
*/
|
|
22
|
+
export declare function useQuotaUsageContext(): QuotaUsageContextValue;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal notifier for quota usage data invalidation.
|
|
3
|
+
* When usage is recorded (via useRecordUsage), call invalidateQuotaUsage()
|
|
4
|
+
* so QuotaUsageContextProvider refetches and gates stay in sync.
|
|
5
|
+
*/
|
|
6
|
+
type Listener = () => void;
|
|
7
|
+
/**
|
|
8
|
+
* Subscribe a refetch callback to be called when quota usage is invalidated.
|
|
9
|
+
*
|
|
10
|
+
* @param fn - Callback (e.g. refetch) to run when invalidateQuotaUsage() is called
|
|
11
|
+
* @returns Unsubscribe function to remove the callback
|
|
12
|
+
*/
|
|
13
|
+
export declare function subscribeQuotaUsageInvalidate(fn: Listener): () => void;
|
|
14
|
+
/**
|
|
15
|
+
* Notify all subscribers to refetch quota usage (e.g. after recording usage).
|
|
16
|
+
* Called internally by useRecordUsage on success.
|
|
17
|
+
*/
|
|
18
|
+
export declare function invalidateQuotaUsage(): void;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IQuotaUsageStatus } from '../../api/types';
|
|
2
|
+
/**
|
|
3
|
+
* Value provided by QuotaUsageContext and returned by useQuotaUsageContext.
|
|
4
|
+
*/
|
|
5
|
+
export interface QuotaUsageContextValue {
|
|
6
|
+
/** Current quota usage statuses keyed by slug, or null if not loaded. */
|
|
7
|
+
quotas: Record<string, IQuotaUsageStatus> | null;
|
|
8
|
+
/** True while quota usage is being fetched. */
|
|
9
|
+
loading: boolean;
|
|
10
|
+
/** Error message if the last fetch failed, or null. */
|
|
11
|
+
error: string | null;
|
|
12
|
+
/** Refetch all quota usage for the current workspace. Call after recording usage or when usage was updated elsewhere. */
|
|
13
|
+
refetch: () => Promise<void>;
|
|
14
|
+
}
|
|
@@ -7,6 +7,8 @@ export interface SubscriptionContextValue {
|
|
|
7
7
|
response: ISubscriptionResponse | null;
|
|
8
8
|
/** True while subscription is being fetched. */
|
|
9
9
|
loading: boolean;
|
|
10
|
+
/** Error message if the last fetch failed, or null. */
|
|
11
|
+
error: string | null;
|
|
10
12
|
/** Refetch subscription for the current workspace. Call after plan change (e.g. upgrade) or when subscription was updated elsewhere. */
|
|
11
13
|
refetch: () => Promise<void>;
|
|
12
14
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -8,8 +8,13 @@ export { WhenAuthenticated, WhenUnauthenticated } from './components/user/auth';
|
|
|
8
8
|
export { WhenRoles, WhenWorkspaceRoles } from './components/user/role';
|
|
9
9
|
export { WhenUserFeatureDisabled, WhenUserFeatureEnabled, WhenWorkspaceFeatureDisabled, WhenWorkspaceFeatureEnabled, } from './components/features';
|
|
10
10
|
export { WhenNoSubscription, WhenSubscription, WhenSubscriptionToPlans, } from './components/subscription';
|
|
11
|
+
export { WhenQuotaAvailable, WhenQuotaExhausted, WhenQuotaOverage, WhenQuotaThreshold, } from './components/quota';
|
|
11
12
|
export { SubscriptionContextProvider, useSubscriptionContext, } from './contexts/SubscriptionContext';
|
|
12
13
|
export type { SubscriptionContextValue } from './contexts/SubscriptionContext';
|
|
14
|
+
export { invalidateSubscription } from './contexts/SubscriptionContext/subscriptionInvalidation';
|
|
15
|
+
export { QuotaUsageContextProvider, useQuotaUsageContext, } from './contexts/QuotaUsageContext';
|
|
16
|
+
export type { QuotaUsageContextValue } from './contexts/QuotaUsageContext';
|
|
17
|
+
export { invalidateQuotaUsage } from './contexts/QuotaUsageContext/quotaUsageInvalidation';
|
|
13
18
|
export { AuthStatus } from './providers/auth/types';
|
|
14
19
|
export type { OnWorkspaceChangeParams } from './providers/auth/types';
|
|
15
20
|
export { useSaaSAuth } from './providers/auth/hooks';
|
|
@@ -17,7 +22,7 @@ export { useSaaSOs, useSaaSSettings } from './providers/os/hooks';
|
|
|
17
22
|
export { useUserAttributes, useUserFeatures } from './providers/user/hooks';
|
|
18
23
|
export { useSaaSWorkspaces } from './providers/workspace/hooks';
|
|
19
24
|
export { WorkspaceSwitcher } from './providers/workspace/provider';
|
|
20
|
-
export { useCreateCheckoutSession, useInvoice, useInvoices, usePlanGroup, usePlanGroupVersions, usePublicPlanGroupVersion, usePublicPlans, useSubscription, useSubscriptionManagement, useUpdateSubscription, } from './providers/workspace/subscription-hooks';
|
|
25
|
+
export { useAllQuotaUsage, useCancelSubscription, useCreateCheckoutSession, useInvoice, useInvoices, usePlanGroup, usePlanGroupVersions, usePublicPlanGroupVersion, usePublicPlans, useQuotaUsageStatus, useRecordUsage, useResumeSubscription, useSubscription, useSubscriptionManagement, useUpdateSubscription, useUsageLogs, } from './providers/workspace/subscription-hooks';
|
|
21
26
|
export { eventEmitter } from './providers/events';
|
|
22
27
|
export type { EventData, EventType, IEventCallbacks, UserCreatedEventData, UserUpdatedEventData, WorkspaceChangedEventData, WorkspaceCreatedEventData, WorkspaceDeletedEventData, WorkspaceUpdatedEventData, WorkspaceUserAddedEventData, WorkspaceUserRemovedEventData, WorkspaceUserRoleChangedEventData, } from './providers/events/types';
|
|
23
28
|
export { BaseApi, SettingsApi, UserApi, WorkspaceApi } from './api';
|
|
@@ -27,4 +32,4 @@ export { formatQuotaWithPrice, getQuotaDisplayValue } from './api/quota-utils';
|
|
|
27
32
|
export type { FormatQuotaWithPriceOptions, QuotaDisplayValue } from './api/quota-utils';
|
|
28
33
|
export { getAvailableCurrenciesFromPlans, getBasePriceCents, getBillingIntervalAndCurrencyFromPriceId, getDisplayCurrency, getPricingVariant, getQuotaDisplayWithVariant, getQuotaOverageCents, getStripePriceIdForInterval, } from './api/pricing-variant-utils';
|
|
29
34
|
export type { PlanVersionWithPricingVariants, QuotaDisplayWithOverage, } from './api/pricing-variant-utils';
|
|
30
|
-
export type { BillingInterval, IBasePricing, ICheckoutSessionRequest, ICheckoutSessionResponse, IInvoice, IInvoiceListResponse, IInvoiceResponse, IPlan, IPlanGroup, IPlanGroupInfo, IPlanGroupLatestVersion, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionWithPlans, IPlanGroupVersionsResponse, IPlanVersion, IPlanVersionSummary, IPlanVersionWithPlan, IPricingVariant, IPublicPlanItem, IPublicPlanItemCategory, IPublicPlanVersion, IPublicPlansResponse, IQuotaByInterval, IQuotaIntervalValue, IQuotaOveragePriceIdsByInterval, IQuotaOveragesByInterval, IStripePricesByInterval, ISubscription, ISubscriptionItem, ISubscriptionResponse, ISubscriptionUpdateRequest, ISubscriptionUpdateResponse, InvoiceStatus, } from './api/types';
|
|
35
|
+
export type { BillingInterval, IAllQuotaUsageResponse, IBasePricing, ICheckoutSessionRequest, ICheckoutSessionResponse, IInvoice, IInvoiceListResponse, IInvoiceResponse, IPlan, IPlanGroup, IPlanGroupInfo, IPlanGroupLatestVersion, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionWithPlans, IPlanGroupVersionsResponse, IPlanVersion, IPlanVersionSummary, IPlanVersionWithPlan, IPricingVariant, IPublicPlanItem, IPublicPlanItemCategory, IPublicPlanVersion, IPublicPlansResponse, IQuotaByInterval, IQuotaIntervalValue, IQuotaOveragePriceIdsByInterval, IQuotaOveragesByInterval, IQuotaUsageStatus, IQuotaUsageStatusResponse, IRecordUsageRequest, IRecordUsageResponse, IStripePricesByInterval, ISubscription, ISubscriptionItem, ISubscriptionResponse, ISubscriptionUpdateRequest, ISubscriptionUpdateResponse, IUsageLogEntry, IUsageLogsQuery, IUsageLogsResponse, InvoiceStatus, } from './api/types';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ICheckoutSessionRequest, ICheckoutSessionResponse, IInvoiceListResponse, IInvoiceResponse, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionsResponse, IPublicPlansResponse, ISubscriptionResponse, ISubscriptionUpdateRequest, ISubscriptionUpdateResponse, IUser } from '../../api/types';
|
|
1
|
+
import { IAllQuotaUsageResponse, ICheckoutSessionRequest, ICheckoutSessionResponse, IInvoiceListResponse, IInvoiceResponse, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionsResponse, IPublicPlansResponse, IQuotaUsageStatusResponse, IRecordUsageRequest, IRecordUsageResponse, ISubscriptionResponse, ISubscriptionUpdateRequest, ISubscriptionUpdateResponse, IUsageLogsQuery, IUsageLogsResponse, IUser } from '../../api/types';
|
|
2
2
|
import { BaseApi } from '../../lib/api-base';
|
|
3
3
|
import { IOsConfig } from '../os/types';
|
|
4
4
|
import type { IWorkspace, IWorkspaceFeature, IWorkspaceUser } from './types';
|
|
@@ -125,4 +125,31 @@ export declare class WorkspaceApi extends BaseApi {
|
|
|
125
125
|
* @returns Updated subscription with cancelAtPeriodEnd set to false
|
|
126
126
|
*/
|
|
127
127
|
resumeSubscription(workspaceId: string): Promise<ISubscriptionResponse>;
|
|
128
|
+
/**
|
|
129
|
+
* Record quota usage for a workspace
|
|
130
|
+
* @param workspaceId - The workspace ID
|
|
131
|
+
* @param request - Usage request with quotaSlug, quantity, and optional metadata/source
|
|
132
|
+
* @returns Usage result with consumed/included/available/overage
|
|
133
|
+
*/
|
|
134
|
+
recordUsage(workspaceId: string, request: IRecordUsageRequest): Promise<IRecordUsageResponse>;
|
|
135
|
+
/**
|
|
136
|
+
* Get usage status for a single quota
|
|
137
|
+
* @param workspaceId - The workspace ID
|
|
138
|
+
* @param quotaSlug - The quota slug to check
|
|
139
|
+
* @returns Quota usage status with consumed/included/available/overage/hasOverage
|
|
140
|
+
*/
|
|
141
|
+
getQuotaUsageStatus(workspaceId: string, quotaSlug: string): Promise<IQuotaUsageStatusResponse>;
|
|
142
|
+
/**
|
|
143
|
+
* Get usage status for all quotas in the workspace's current plan
|
|
144
|
+
* @param workspaceId - The workspace ID
|
|
145
|
+
* @returns All quota usage statuses keyed by quota slug
|
|
146
|
+
*/
|
|
147
|
+
getAllQuotaUsage(workspaceId: string): Promise<IAllQuotaUsageResponse>;
|
|
148
|
+
/**
|
|
149
|
+
* Get paginated usage logs for a workspace
|
|
150
|
+
* @param workspaceId - The workspace ID
|
|
151
|
+
* @param query - Optional filters: quotaSlug, from, to, source, page, limit
|
|
152
|
+
* @returns Paginated usage log entries
|
|
153
|
+
*/
|
|
154
|
+
getUsageLogs(workspaceId: string, query?: IUsageLogsQuery): Promise<IUsageLogsResponse>;
|
|
128
155
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BillingInterval, ICheckoutSessionRequest, ICheckoutSessionResponse, IInvoice, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionsResponse, ISubscriptionResponse, ISubscriptionUpdateResponse } from '../../api/types';
|
|
1
|
+
import { BillingInterval, ICheckoutSessionRequest, ICheckoutSessionResponse, IInvoice, IPlanGroupResponse, IPlanGroupVersion, IPlanGroupVersionsResponse, IQuotaUsageStatus, IQuotaUsageStatusResponse, IRecordUsageRequest, IRecordUsageResponse, ISubscriptionResponse, ISubscriptionUpdateResponse, IUsageLogEntry } from '../../api/types';
|
|
2
2
|
/**
|
|
3
3
|
* Hook to get public plans by slug (no auth required).
|
|
4
4
|
* Returns items (features, limits, quotas) and plans (with pricing).
|
|
@@ -561,3 +561,181 @@ export declare const useResumeSubscription: (workspaceId: string | null | undefi
|
|
|
561
561
|
loading: boolean;
|
|
562
562
|
error: string | null;
|
|
563
563
|
};
|
|
564
|
+
/**
|
|
565
|
+
* Hook to record quota usage for a workspace.
|
|
566
|
+
* Returns a function to record usage (mutation pattern).
|
|
567
|
+
*
|
|
568
|
+
* @param workspaceId - The workspace ID. Can be null/undefined.
|
|
569
|
+
* @returns An object containing:
|
|
570
|
+
* - `recordUsage(request)`: Function to record usage (throws if workspaceId is null)
|
|
571
|
+
* - `loading`: Boolean indicating if recording is in progress
|
|
572
|
+
* - `error`: Error message string (null if no error)
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```tsx
|
|
576
|
+
* function RecordUsageButton() {
|
|
577
|
+
* const { currentWorkspace } = useSaaSWorkspaces();
|
|
578
|
+
* const { recordUsage, loading } = useRecordUsage(currentWorkspace?._id);
|
|
579
|
+
*
|
|
580
|
+
* const handleRecord = async () => {
|
|
581
|
+
* try {
|
|
582
|
+
* const result = await recordUsage({
|
|
583
|
+
* quotaSlug: 'api_calls',
|
|
584
|
+
* quantity: 1,
|
|
585
|
+
* source: 'web-app',
|
|
586
|
+
* });
|
|
587
|
+
* console.log(`Used: ${result.consumed}/${result.included}`);
|
|
588
|
+
* } catch (error) {
|
|
589
|
+
* console.error('Failed to record usage:', error);
|
|
590
|
+
* }
|
|
591
|
+
* };
|
|
592
|
+
*
|
|
593
|
+
* return (
|
|
594
|
+
* <button onClick={handleRecord} disabled={loading}>
|
|
595
|
+
* {loading ? 'Recording...' : 'Record Usage'}
|
|
596
|
+
* </button>
|
|
597
|
+
* );
|
|
598
|
+
* }
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
export declare const useRecordUsage: (workspaceId: string | null | undefined) => {
|
|
602
|
+
recordUsage: (request: IRecordUsageRequest) => Promise<IRecordUsageResponse>;
|
|
603
|
+
loading: boolean;
|
|
604
|
+
error: string | null;
|
|
605
|
+
};
|
|
606
|
+
/**
|
|
607
|
+
* Hook to get usage status for a single quota.
|
|
608
|
+
* Automatically fetches when workspaceId or quotaSlug changes.
|
|
609
|
+
*
|
|
610
|
+
* @param workspaceId - The workspace ID. Can be null/undefined to disable fetching.
|
|
611
|
+
* @param quotaSlug - The quota slug to check. Can be null/undefined to disable fetching.
|
|
612
|
+
* @returns An object containing:
|
|
613
|
+
* - `status`: Quota usage status (null if not loaded)
|
|
614
|
+
* - `loading`: Boolean indicating if status is being fetched
|
|
615
|
+
* - `error`: Error message string (null if no error)
|
|
616
|
+
* - `refetch()`: Function to manually refetch the status
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```tsx
|
|
620
|
+
* function QuotaStatusDisplay() {
|
|
621
|
+
* const { currentWorkspace } = useSaaSWorkspaces();
|
|
622
|
+
* const { status, loading } = useQuotaUsageStatus(currentWorkspace?._id, 'api_calls');
|
|
623
|
+
*
|
|
624
|
+
* if (loading) return <Loading />;
|
|
625
|
+
* if (!status) return null;
|
|
626
|
+
*
|
|
627
|
+
* return (
|
|
628
|
+
* <div>
|
|
629
|
+
* <p>Used: {status.consumed} / {status.included}</p>
|
|
630
|
+
* <p>Available: {status.available}</p>
|
|
631
|
+
* {status.hasOverage && <p>Overage: {status.overage}</p>}
|
|
632
|
+
* </div>
|
|
633
|
+
* );
|
|
634
|
+
* }
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
export declare const useQuotaUsageStatus: (workspaceId: string | null | undefined, quotaSlug: string | null | undefined) => {
|
|
638
|
+
status: IQuotaUsageStatusResponse | null;
|
|
639
|
+
loading: boolean;
|
|
640
|
+
error: string | null;
|
|
641
|
+
refetch: () => Promise<void>;
|
|
642
|
+
};
|
|
643
|
+
/**
|
|
644
|
+
* Hook to get usage status for all quotas in the workspace's current plan.
|
|
645
|
+
* Automatically fetches when workspaceId changes.
|
|
646
|
+
*
|
|
647
|
+
* @param workspaceId - The workspace ID. Can be null/undefined to disable fetching.
|
|
648
|
+
* @returns An object containing:
|
|
649
|
+
* - `quotas`: Record of quota usage statuses keyed by slug (null if not loaded)
|
|
650
|
+
* - `loading`: Boolean indicating if statuses are being fetched
|
|
651
|
+
* - `error`: Error message string (null if no error)
|
|
652
|
+
* - `refetch()`: Function to manually refetch all statuses
|
|
653
|
+
*
|
|
654
|
+
* @example
|
|
655
|
+
* ```tsx
|
|
656
|
+
* function AllQuotasDisplay() {
|
|
657
|
+
* const { currentWorkspace } = useSaaSWorkspaces();
|
|
658
|
+
* const { quotas, loading } = useAllQuotaUsage(currentWorkspace?._id);
|
|
659
|
+
*
|
|
660
|
+
* if (loading) return <Loading />;
|
|
661
|
+
* if (!quotas) return null;
|
|
662
|
+
*
|
|
663
|
+
* return (
|
|
664
|
+
* <div>
|
|
665
|
+
* {Object.entries(quotas).map(([slug, usage]) => (
|
|
666
|
+
* <div key={slug}>
|
|
667
|
+
* <p>{slug}: {usage.consumed}/{usage.included}</p>
|
|
668
|
+
* {usage.hasOverage && <p>Overage: {usage.overage}</p>}
|
|
669
|
+
* </div>
|
|
670
|
+
* ))}
|
|
671
|
+
* </div>
|
|
672
|
+
* );
|
|
673
|
+
* }
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
export declare const useAllQuotaUsage: (workspaceId: string | null | undefined) => {
|
|
677
|
+
quotas: Record<string, IQuotaUsageStatus> | null;
|
|
678
|
+
loading: boolean;
|
|
679
|
+
error: string | null;
|
|
680
|
+
refetch: () => Promise<void>;
|
|
681
|
+
};
|
|
682
|
+
/**
|
|
683
|
+
* Hook to get paginated usage logs for a workspace.
|
|
684
|
+
* Automatically fetches when workspaceId or filter params change.
|
|
685
|
+
*
|
|
686
|
+
* @param workspaceId - The workspace ID. Can be null/undefined to disable fetching.
|
|
687
|
+
* @param quotaSlug - Optional quota slug to filter logs by.
|
|
688
|
+
* @param options - Optional filters: from, to, source, page, limit
|
|
689
|
+
* @returns An object containing:
|
|
690
|
+
* - `logs`: Array of usage log entries
|
|
691
|
+
* - `totalDocs`: Total number of log entries matching the query
|
|
692
|
+
* - `totalPages`: Total number of pages
|
|
693
|
+
* - `page`: Current page number
|
|
694
|
+
* - `hasNextPage`: Whether there are more pages after the current one
|
|
695
|
+
* - `hasPrevPage`: Whether there are pages before the current one
|
|
696
|
+
* - `loading`: Boolean indicating if logs are being fetched
|
|
697
|
+
* - `error`: Error message string (null if no error)
|
|
698
|
+
* - `refetch()`: Function to manually refetch logs
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* ```tsx
|
|
702
|
+
* function UsageLogsTable() {
|
|
703
|
+
* const { currentWorkspace } = useSaaSWorkspaces();
|
|
704
|
+
* const { logs, totalPages, page, hasNextPage, loading } = useUsageLogs(
|
|
705
|
+
* currentWorkspace?._id,
|
|
706
|
+
* 'api_calls',
|
|
707
|
+
* { limit: 20 }
|
|
708
|
+
* );
|
|
709
|
+
*
|
|
710
|
+
* if (loading) return <Loading />;
|
|
711
|
+
*
|
|
712
|
+
* return (
|
|
713
|
+
* <div>
|
|
714
|
+
* {logs.map(log => (
|
|
715
|
+
* <div key={log._id}>
|
|
716
|
+
* {log.quotaSlug}: {log.quantity} ({log.createdAt})
|
|
717
|
+
* </div>
|
|
718
|
+
* ))}
|
|
719
|
+
* <p>Page {page} of {totalPages}</p>
|
|
720
|
+
* </div>
|
|
721
|
+
* );
|
|
722
|
+
* }
|
|
723
|
+
* ```
|
|
724
|
+
*/
|
|
725
|
+
export declare const useUsageLogs: (workspaceId: string | null | undefined, quotaSlug?: string, options?: {
|
|
726
|
+
from?: string;
|
|
727
|
+
to?: string;
|
|
728
|
+
source?: string;
|
|
729
|
+
page?: number;
|
|
730
|
+
limit?: number;
|
|
731
|
+
}) => {
|
|
732
|
+
logs: IUsageLogEntry[];
|
|
733
|
+
totalDocs: number;
|
|
734
|
+
totalPages: number;
|
|
735
|
+
page: number;
|
|
736
|
+
hasNextPage: boolean;
|
|
737
|
+
hasPrevPage: boolean;
|
|
738
|
+
loading: boolean;
|
|
739
|
+
error: string | null;
|
|
740
|
+
refetch: () => Promise<void>;
|
|
741
|
+
};
|