opencloud-platform-sdk 2.0.0 → 3.0.0

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.mts CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- * opencloud-platform-sdk v2.0.0
2
+ * opencloud-platform-sdk v3.0.0
3
3
  * Official SDK for OpenCloud - AI App Marketplace
4
4
  *
5
- * New Model:
6
- * - Creators use their own AI/APIs
7
- * - SDK only tracks usage and charges end users
8
- * - Creators set price per use, platform takes 15% commission
5
+ * Standalone Mode:
6
+ * - Works directly on any domain (no iframe required)
7
+ * - Uses popup for authentication
8
+ * - Charges from user's OpenCloud wallet
9
9
  */
10
10
  interface OpenCloudConfig {
11
11
  appId: string;
@@ -21,8 +21,21 @@ interface TrackUsageResponse {
21
21
  userBalance: number;
22
22
  transactionId: string;
23
23
  }
24
+ interface ChargeResult {
25
+ success: boolean;
26
+ charged?: number;
27
+ balance?: number;
28
+ transactionId?: string;
29
+ error?: 'CANCELLED' | 'INSUFFICIENT_BALANCE' | 'NOT_AUTHENTICATED' | 'UNKNOWN';
30
+ }
24
31
  interface UserSession {
25
32
  token: string;
33
+ user?: {
34
+ id: string;
35
+ email: string;
36
+ username?: string;
37
+ balance?: number;
38
+ };
26
39
  }
27
40
  interface UserInfo {
28
41
  id: string;
@@ -32,11 +45,15 @@ interface UserInfo {
32
45
  }
33
46
  declare class OpenCloudSDK {
34
47
  private config;
35
- private heartbeatInterval?;
36
48
  private _isPreviewMode;
37
- private _cachedSession;
49
+ private _session;
50
+ private _authPopup;
38
51
  constructor();
39
52
  private detectPreviewMode;
53
+ private loadStoredSession;
54
+ private saveSession;
55
+ private clearStoredSession;
56
+ private setupMessageListener;
40
57
  /**
41
58
  * Check if running in preview/development mode
42
59
  */
@@ -46,85 +63,61 @@ declare class OpenCloudSDK {
46
63
  */
47
64
  init(options: OpenCloudConfig): void;
48
65
  /**
49
- * Track a usage event and charge the user
50
- * Call this BEFORE or AFTER your AI/API call
51
- *
52
- * Example:
53
- * ```
54
- * // Check if user can afford it first
55
- * const canUse = await opencloud.canAfford();
56
- * if (!canUse) {
57
- * opencloud.requestTopUp();
58
- * return;
59
- * }
60
- *
61
- * // Do your AI call
62
- * const result = await myAICall();
63
- *
64
- * // Track the usage (charges user)
65
- * await opencloud.trackUsage({ action: 'chat_message' });
66
- * ```
66
+ * Check if user is authenticated with OpenCloud
67
67
  */
68
- trackUsage(params?: TrackUsageParams): Promise<TrackUsageResponse>;
68
+ isAuthenticated(): boolean;
69
69
  /**
70
- * Check if the user can afford to use the app
71
- * Returns true if user has enough balance
70
+ * Open login popup for user authentication
71
+ * Returns a promise that resolves when login is complete
72
72
  */
73
- canAfford(): Promise<boolean>;
73
+ login(): Promise<UserSession | null>;
74
74
  /**
75
- * Get current user's balance
75
+ * Log out the current user
76
76
  */
77
- getBalance(): Promise<number>;
77
+ logout(): void;
78
78
  /**
79
79
  * Get current user info
80
80
  */
81
- getUserInfo(): Promise<UserInfo>;
81
+ getUser(): UserInfo | null;
82
82
  /**
83
- * Get the price per use for this app
83
+ * Charge the user for using a feature
84
+ * Will automatically prompt for login if not authenticated
85
+ *
86
+ * @param action - Optional action name for analytics
87
+ * @returns ChargeResult with success status
84
88
  */
85
- getAppPrice(): Promise<number>;
89
+ charge(action?: string, metadata?: Record<string, any>): Promise<ChargeResult>;
86
90
  /**
87
- * Request user to add more credits
88
- * Opens the top-up modal or redirects to payment page
91
+ * Check if user can afford to use the app
89
92
  */
90
- requestTopUp(): void;
93
+ canAfford(): Promise<boolean>;
91
94
  /**
92
- * Check if user is authenticated
95
+ * Get user's current balance
93
96
  */
94
- isAuthenticated(): Promise<boolean>;
97
+ getBalance(): Promise<number>;
95
98
  /**
96
- * Request user to log in
99
+ * Open top-up popup for user to add credits
97
100
  */
98
- requestLogin(): void;
99
- private startHeartbeat;
100
- private sendHeartbeat;
101
- private getUserSession;
101
+ openTopUp(): void;
102
102
  /**
103
- * Clear cached session (call on logout)
103
+ * Get price per use for this app
104
104
  */
105
- clearSession(): void;
105
+ getAppPrice(): Promise<number>;
106
106
  /**
107
- * Cleanup resources
107
+ * Execute a function and charge the user
108
+ * Handles authentication, balance check, and charging automatically
108
109
  */
110
+ withCharge<T>(fn: () => Promise<T>, options?: {
111
+ action?: string;
112
+ metadata?: Record<string, any>;
113
+ }): Promise<T>;
114
+ trackUsage(params?: TrackUsageParams): Promise<TrackUsageResponse>;
115
+ requestTopUp(): void;
116
+ requestLogin(): void;
117
+ getUserInfo(): Promise<UserInfo>;
118
+ clearSession(): void;
109
119
  destroy(): void;
110
- /**
111
- * Execute a function and automatically charge the user
112
- * This is a convenience method that combines canAfford + your code + trackUsage
113
- *
114
- * @param fn - The async function to execute (your AI call, etc.)
115
- * @param options - Optional tracking options
116
- * @returns The result of your function
117
- *
118
- * Example:
119
- * ```typescript
120
- * const result = await opencloud.withUsage(async () => {
121
- * const response = await fetch('https://api.openai.com/...', { ... });
122
- * return await response.json();
123
- * }, { action: 'chat_message' });
124
- * ```
125
- */
126
- withUsage<T>(fn: () => Promise<T>, options?: TrackUsageParams): Promise<T>;
127
120
  }
128
121
  declare const opencloud: OpenCloudSDK;
129
122
 
130
- export { type OpenCloudConfig, OpenCloudSDK, type TrackUsageParams, type TrackUsageResponse, type UserInfo, type UserSession, opencloud };
123
+ export { type ChargeResult, type OpenCloudConfig, OpenCloudSDK, type TrackUsageParams, type TrackUsageResponse, type UserInfo, type UserSession, opencloud };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- * opencloud-platform-sdk v2.0.0
2
+ * opencloud-platform-sdk v3.0.0
3
3
  * Official SDK for OpenCloud - AI App Marketplace
4
4
  *
5
- * New Model:
6
- * - Creators use their own AI/APIs
7
- * - SDK only tracks usage and charges end users
8
- * - Creators set price per use, platform takes 15% commission
5
+ * Standalone Mode:
6
+ * - Works directly on any domain (no iframe required)
7
+ * - Uses popup for authentication
8
+ * - Charges from user's OpenCloud wallet
9
9
  */
10
10
  interface OpenCloudConfig {
11
11
  appId: string;
@@ -21,8 +21,21 @@ interface TrackUsageResponse {
21
21
  userBalance: number;
22
22
  transactionId: string;
23
23
  }
24
+ interface ChargeResult {
25
+ success: boolean;
26
+ charged?: number;
27
+ balance?: number;
28
+ transactionId?: string;
29
+ error?: 'CANCELLED' | 'INSUFFICIENT_BALANCE' | 'NOT_AUTHENTICATED' | 'UNKNOWN';
30
+ }
24
31
  interface UserSession {
25
32
  token: string;
33
+ user?: {
34
+ id: string;
35
+ email: string;
36
+ username?: string;
37
+ balance?: number;
38
+ };
26
39
  }
27
40
  interface UserInfo {
28
41
  id: string;
@@ -32,11 +45,15 @@ interface UserInfo {
32
45
  }
33
46
  declare class OpenCloudSDK {
34
47
  private config;
35
- private heartbeatInterval?;
36
48
  private _isPreviewMode;
37
- private _cachedSession;
49
+ private _session;
50
+ private _authPopup;
38
51
  constructor();
39
52
  private detectPreviewMode;
53
+ private loadStoredSession;
54
+ private saveSession;
55
+ private clearStoredSession;
56
+ private setupMessageListener;
40
57
  /**
41
58
  * Check if running in preview/development mode
42
59
  */
@@ -46,85 +63,61 @@ declare class OpenCloudSDK {
46
63
  */
47
64
  init(options: OpenCloudConfig): void;
48
65
  /**
49
- * Track a usage event and charge the user
50
- * Call this BEFORE or AFTER your AI/API call
51
- *
52
- * Example:
53
- * ```
54
- * // Check if user can afford it first
55
- * const canUse = await opencloud.canAfford();
56
- * if (!canUse) {
57
- * opencloud.requestTopUp();
58
- * return;
59
- * }
60
- *
61
- * // Do your AI call
62
- * const result = await myAICall();
63
- *
64
- * // Track the usage (charges user)
65
- * await opencloud.trackUsage({ action: 'chat_message' });
66
- * ```
66
+ * Check if user is authenticated with OpenCloud
67
67
  */
68
- trackUsage(params?: TrackUsageParams): Promise<TrackUsageResponse>;
68
+ isAuthenticated(): boolean;
69
69
  /**
70
- * Check if the user can afford to use the app
71
- * Returns true if user has enough balance
70
+ * Open login popup for user authentication
71
+ * Returns a promise that resolves when login is complete
72
72
  */
73
- canAfford(): Promise<boolean>;
73
+ login(): Promise<UserSession | null>;
74
74
  /**
75
- * Get current user's balance
75
+ * Log out the current user
76
76
  */
77
- getBalance(): Promise<number>;
77
+ logout(): void;
78
78
  /**
79
79
  * Get current user info
80
80
  */
81
- getUserInfo(): Promise<UserInfo>;
81
+ getUser(): UserInfo | null;
82
82
  /**
83
- * Get the price per use for this app
83
+ * Charge the user for using a feature
84
+ * Will automatically prompt for login if not authenticated
85
+ *
86
+ * @param action - Optional action name for analytics
87
+ * @returns ChargeResult with success status
84
88
  */
85
- getAppPrice(): Promise<number>;
89
+ charge(action?: string, metadata?: Record<string, any>): Promise<ChargeResult>;
86
90
  /**
87
- * Request user to add more credits
88
- * Opens the top-up modal or redirects to payment page
91
+ * Check if user can afford to use the app
89
92
  */
90
- requestTopUp(): void;
93
+ canAfford(): Promise<boolean>;
91
94
  /**
92
- * Check if user is authenticated
95
+ * Get user's current balance
93
96
  */
94
- isAuthenticated(): Promise<boolean>;
97
+ getBalance(): Promise<number>;
95
98
  /**
96
- * Request user to log in
99
+ * Open top-up popup for user to add credits
97
100
  */
98
- requestLogin(): void;
99
- private startHeartbeat;
100
- private sendHeartbeat;
101
- private getUserSession;
101
+ openTopUp(): void;
102
102
  /**
103
- * Clear cached session (call on logout)
103
+ * Get price per use for this app
104
104
  */
105
- clearSession(): void;
105
+ getAppPrice(): Promise<number>;
106
106
  /**
107
- * Cleanup resources
107
+ * Execute a function and charge the user
108
+ * Handles authentication, balance check, and charging automatically
108
109
  */
110
+ withCharge<T>(fn: () => Promise<T>, options?: {
111
+ action?: string;
112
+ metadata?: Record<string, any>;
113
+ }): Promise<T>;
114
+ trackUsage(params?: TrackUsageParams): Promise<TrackUsageResponse>;
115
+ requestTopUp(): void;
116
+ requestLogin(): void;
117
+ getUserInfo(): Promise<UserInfo>;
118
+ clearSession(): void;
109
119
  destroy(): void;
110
- /**
111
- * Execute a function and automatically charge the user
112
- * This is a convenience method that combines canAfford + your code + trackUsage
113
- *
114
- * @param fn - The async function to execute (your AI call, etc.)
115
- * @param options - Optional tracking options
116
- * @returns The result of your function
117
- *
118
- * Example:
119
- * ```typescript
120
- * const result = await opencloud.withUsage(async () => {
121
- * const response = await fetch('https://api.openai.com/...', { ... });
122
- * return await response.json();
123
- * }, { action: 'chat_message' });
124
- * ```
125
- */
126
- withUsage<T>(fn: () => Promise<T>, options?: TrackUsageParams): Promise<T>;
127
120
  }
128
121
  declare const opencloud: OpenCloudSDK;
129
122
 
130
- export { type OpenCloudConfig, OpenCloudSDK, type TrackUsageParams, type TrackUsageResponse, type UserInfo, type UserSession, opencloud };
123
+ export { type ChargeResult, type OpenCloudConfig, OpenCloudSDK, type TrackUsageParams, type TrackUsageResponse, type UserInfo, type UserSession, opencloud };