@buildbase/sdk 0.0.19 → 0.0.21

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.
@@ -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
+ };
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { IWorkspace } from '../types';
3
- export type WorkspaceSettingsSection = 'profile' | 'general' | 'users' | 'subscription' | 'features' | 'danger';
3
+ export type WorkspaceSettingsSection = 'profile' | 'general' | 'users' | 'subscription' | 'usage' | 'features' | 'danger';
4
4
  export interface WorkspaceSettingsDialogProps {
5
5
  workspace: IWorkspace;
6
6
  onClose?: () => void;
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const WorkspaceSettingsUsage: React.FC;
3
+ export default WorkspaceSettingsUsage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buildbase/sdk",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "type": "module",
5
5
  "description": "A SDK for Buildbase",
6
6
  "main": "dist/index.js",