spaps-sdk 1.0.2 → 1.1.1

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.
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Permission checking utilities for SPAPS SDK
3
+ * Client-side role and permission management
4
+ */
5
+ interface User$1 {
6
+ id: string;
7
+ email?: string;
8
+ wallet_address?: string;
9
+ role?: string;
10
+ permissions?: string[];
11
+ tier?: string;
12
+ }
13
+ interface AdminConfig {
14
+ email: string;
15
+ wallets: {
16
+ ethereum: string;
17
+ solana: string;
18
+ };
19
+ }
20
+ interface PermissionCheckResult {
21
+ allowed: boolean;
22
+ reason?: string;
23
+ userRole: string;
24
+ requiredRole?: string;
25
+ }
26
+ declare const DEFAULT_ADMIN_ACCOUNTS: AdminConfig;
27
+ /**
28
+ * Check if an identifier (email/wallet) is an admin account
29
+ */
30
+ declare function isAdminAccount(identifier: string, customAdmins?: (string | AdminConfig)[]): boolean;
31
+ /**
32
+ * Get user role based on identifier
33
+ */
34
+ declare function getUserRole(identifier?: string, customAdmins?: (string | AdminConfig)[]): string;
35
+ /**
36
+ * Check if user has specific permissions
37
+ */
38
+ declare function hasPermission(user: User$1 | null, requiredPermissions: string | string[], customAdmins?: (string | AdminConfig)[]): boolean;
39
+ /**
40
+ * Check if user can access admin features
41
+ */
42
+ declare function canAccessAdmin(user: User$1 | null, customAdmins?: (string | AdminConfig)[]): PermissionCheckResult;
43
+ /**
44
+ * Get role-aware error message
45
+ */
46
+ declare function getRoleAwareErrorMessage(requiredRole: string, userRole: string, action?: string): string;
47
+ /**
48
+ * Get user display information with role indicators
49
+ */
50
+ declare function getUserDisplay(user: User$1 | null, customAdmins?: (string | AdminConfig)[]): {
51
+ displayName: string;
52
+ role: string;
53
+ badge: string | null;
54
+ isAdmin: boolean;
55
+ };
56
+ /**
57
+ * Permission checker class for easier usage
58
+ */
59
+ declare class PermissionChecker {
60
+ private customAdmins;
61
+ constructor(customAdmins?: (string | AdminConfig)[]);
62
+ isAdmin(identifier: string): boolean;
63
+ getRole(identifier?: string): string;
64
+ hasPermission(user: User$1 | null, permissions: string | string[]): boolean;
65
+ canAccessAdmin(user: User$1 | null): PermissionCheckResult;
66
+ getErrorMessage(requiredRole: string, userRole: string, action?: string): string;
67
+ getUserDisplay(user: User$1 | null): {
68
+ displayName: string;
69
+ role: string;
70
+ badge: string | null;
71
+ isAdmin: boolean;
72
+ };
73
+ requiresAuth(user: User$1 | null): boolean;
74
+ requiresAdmin(user: User$1 | null): boolean;
75
+ addCustomAdmin(admin: string | AdminConfig): void;
76
+ removeCustomAdmin(admin: string | AdminConfig): void;
77
+ }
78
+ /**
79
+ * Create a permission checker instance
80
+ */
81
+ declare function createPermissionChecker(customAdmins?: (string | AdminConfig)[]): PermissionChecker;
82
+ declare const defaultPermissionChecker: PermissionChecker;
83
+
84
+ /**
85
+ * @spaps/sdk - Sweet Potato Authentication & Payment Service SDK
86
+ * Zero-config client for SPAPS authentication and payments
87
+ */
88
+ interface SPAPSConfig {
89
+ apiUrl?: string;
90
+ apiKey?: string;
91
+ autoDetect?: boolean;
92
+ timeout?: number;
93
+ }
94
+ interface AuthResponse {
95
+ access_token: string;
96
+ refresh_token: string;
97
+ user: User;
98
+ }
99
+ interface User {
100
+ id: string;
101
+ email?: string;
102
+ wallet_address?: string;
103
+ chain_type?: string;
104
+ role: string;
105
+ created_at?: string;
106
+ }
107
+ interface CheckoutSession {
108
+ sessionId: string;
109
+ url: string;
110
+ }
111
+ interface Subscription {
112
+ id: string;
113
+ status: string;
114
+ plan: string;
115
+ current_period_end: string;
116
+ }
117
+ interface UsageBalance {
118
+ balance: number;
119
+ currency: string;
120
+ updated_at: string;
121
+ }
122
+ interface Product {
123
+ id: string;
124
+ name: string;
125
+ description?: string;
126
+ category: string;
127
+ active: boolean;
128
+ images?: string[];
129
+ metadata?: Record<string, string>;
130
+ statement_descriptor?: string;
131
+ unit_label?: string;
132
+ created?: number;
133
+ updated?: number;
134
+ }
135
+ interface Price {
136
+ id: string;
137
+ product: string;
138
+ unit_amount: number;
139
+ currency: string;
140
+ recurring?: {
141
+ interval: 'day' | 'week' | 'month' | 'year';
142
+ interval_count: number;
143
+ };
144
+ nickname?: string;
145
+ active: boolean;
146
+ }
147
+ interface CreateProductRequest {
148
+ name: string;
149
+ description?: string;
150
+ category: string;
151
+ images?: string[];
152
+ metadata?: Record<string, string>;
153
+ active?: boolean;
154
+ statement_descriptor?: string;
155
+ unit_label?: string;
156
+ }
157
+ interface UpdateProductRequest {
158
+ name?: string;
159
+ description?: string;
160
+ category?: string;
161
+ images?: string[];
162
+ metadata?: Record<string, string>;
163
+ active?: boolean;
164
+ statement_descriptor?: string;
165
+ unit_label?: string;
166
+ }
167
+ interface CreatePriceRequest {
168
+ product_id: string;
169
+ unit_amount: number;
170
+ currency: string;
171
+ interval?: 'day' | 'week' | 'month' | 'year';
172
+ interval_count?: number;
173
+ nickname?: string;
174
+ }
175
+ interface ProductSyncResult {
176
+ synced_count: number;
177
+ updated_count: number;
178
+ created_count: number;
179
+ errors: any[];
180
+ sync_time: string;
181
+ }
182
+ declare class SPAPSClient {
183
+ private client;
184
+ private apiKey?;
185
+ private accessToken?;
186
+ private refreshToken?;
187
+ private _isLocalMode;
188
+ admin: {
189
+ createProduct: (productData: CreateProductRequest) => Promise<{
190
+ data: Product;
191
+ }>;
192
+ updateProduct: (productId: string, updates: UpdateProductRequest) => Promise<{
193
+ data: Product;
194
+ }>;
195
+ deleteProduct: (productId: string) => Promise<{
196
+ data: {
197
+ id: string;
198
+ active: boolean;
199
+ archived: boolean;
200
+ };
201
+ }>;
202
+ createPrice: (priceData: CreatePriceRequest) => Promise<{
203
+ data: Price;
204
+ }>;
205
+ syncProducts: () => Promise<{
206
+ data: ProductSyncResult;
207
+ }>;
208
+ getProducts: () => Promise<{
209
+ data: {
210
+ products: Product[];
211
+ total: number;
212
+ adminMetadata?: any;
213
+ };
214
+ }>;
215
+ };
216
+ constructor(config?: SPAPSConfig);
217
+ login(email: string, password: string): Promise<{
218
+ data: AuthResponse;
219
+ }>;
220
+ register(email: string, password: string): Promise<{
221
+ data: AuthResponse;
222
+ }>;
223
+ walletSignIn(walletAddress: string, signature: string, message: string, chainType?: 'solana' | 'ethereum'): Promise<{
224
+ data: AuthResponse;
225
+ }>;
226
+ refresh(refreshToken?: string): Promise<{
227
+ data: AuthResponse;
228
+ }>;
229
+ logout(): Promise<void>;
230
+ getUser(): Promise<{
231
+ data: User;
232
+ }>;
233
+ createCheckoutSession(priceId: string, successUrl: string, cancelUrl?: string): Promise<{
234
+ data: CheckoutSession;
235
+ }>;
236
+ getSubscription(): Promise<{
237
+ data: Subscription;
238
+ }>;
239
+ cancelSubscription(): Promise<void>;
240
+ getUsageBalance(): Promise<{
241
+ data: UsageBalance;
242
+ }>;
243
+ recordUsage(feature: string, amount: number): Promise<void>;
244
+ /**
245
+ * Create a new Stripe product (Admin required)
246
+ */
247
+ createProduct(productData: CreateProductRequest): Promise<{
248
+ data: Product;
249
+ }>;
250
+ /**
251
+ * Update an existing Stripe product (Admin required)
252
+ */
253
+ updateProduct(productId: string, updates: UpdateProductRequest): Promise<{
254
+ data: Product;
255
+ }>;
256
+ /**
257
+ * Archive (soft delete) a Stripe product (Admin required)
258
+ */
259
+ deleteProduct(productId: string): Promise<{
260
+ data: {
261
+ id: string;
262
+ active: boolean;
263
+ archived: boolean;
264
+ };
265
+ }>;
266
+ /**
267
+ * Create a new price for a product (Admin required)
268
+ */
269
+ createPrice(priceData: CreatePriceRequest): Promise<{
270
+ data: Price;
271
+ }>;
272
+ /**
273
+ * Sync all products from Stripe to local database (Super Admin required)
274
+ */
275
+ syncProducts(): Promise<{
276
+ data: ProductSyncResult;
277
+ }>;
278
+ /**
279
+ * Get products with admin metadata (if user is admin)
280
+ */
281
+ getProducts(): Promise<{
282
+ data: {
283
+ products: Product[];
284
+ total: number;
285
+ adminMetadata?: any;
286
+ };
287
+ }>;
288
+ isAuthenticated(): boolean;
289
+ getAccessToken(): string | undefined;
290
+ setAccessToken(token: string): void;
291
+ isLocalMode(): boolean;
292
+ /**
293
+ * Check if current user has admin privileges
294
+ * Note: This requires the user object from authentication
295
+ */
296
+ isAdmin(user?: User): boolean;
297
+ health(): Promise<{
298
+ data: any;
299
+ }>;
300
+ }
301
+
302
+ export { type AdminConfig, type AuthResponse, type CheckoutSession, type CreatePriceRequest, type CreateProductRequest, DEFAULT_ADMIN_ACCOUNTS, type PermissionCheckResult, PermissionChecker, type Price, type Product, type ProductSyncResult, SPAPSClient as SPAPS, SPAPSClient, type SPAPSConfig, type Subscription, SPAPSClient as SweetPotatoSDK, type UpdateProductRequest, type UsageBalance, type User, canAccessAdmin, createPermissionChecker, SPAPSClient as default, defaultPermissionChecker, getRoleAwareErrorMessage, getUserDisplay, getUserRole, hasPermission, isAdminAccount };
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Permission checking utilities for SPAPS SDK
3
+ * Client-side role and permission management
4
+ */
5
+ interface User$1 {
6
+ id: string;
7
+ email?: string;
8
+ wallet_address?: string;
9
+ role?: string;
10
+ permissions?: string[];
11
+ tier?: string;
12
+ }
13
+ interface AdminConfig {
14
+ email: string;
15
+ wallets: {
16
+ ethereum: string;
17
+ solana: string;
18
+ };
19
+ }
20
+ interface PermissionCheckResult {
21
+ allowed: boolean;
22
+ reason?: string;
23
+ userRole: string;
24
+ requiredRole?: string;
25
+ }
26
+ declare const DEFAULT_ADMIN_ACCOUNTS: AdminConfig;
27
+ /**
28
+ * Check if an identifier (email/wallet) is an admin account
29
+ */
30
+ declare function isAdminAccount(identifier: string, customAdmins?: (string | AdminConfig)[]): boolean;
31
+ /**
32
+ * Get user role based on identifier
33
+ */
34
+ declare function getUserRole(identifier?: string, customAdmins?: (string | AdminConfig)[]): string;
35
+ /**
36
+ * Check if user has specific permissions
37
+ */
38
+ declare function hasPermission(user: User$1 | null, requiredPermissions: string | string[], customAdmins?: (string | AdminConfig)[]): boolean;
39
+ /**
40
+ * Check if user can access admin features
41
+ */
42
+ declare function canAccessAdmin(user: User$1 | null, customAdmins?: (string | AdminConfig)[]): PermissionCheckResult;
43
+ /**
44
+ * Get role-aware error message
45
+ */
46
+ declare function getRoleAwareErrorMessage(requiredRole: string, userRole: string, action?: string): string;
47
+ /**
48
+ * Get user display information with role indicators
49
+ */
50
+ declare function getUserDisplay(user: User$1 | null, customAdmins?: (string | AdminConfig)[]): {
51
+ displayName: string;
52
+ role: string;
53
+ badge: string | null;
54
+ isAdmin: boolean;
55
+ };
56
+ /**
57
+ * Permission checker class for easier usage
58
+ */
59
+ declare class PermissionChecker {
60
+ private customAdmins;
61
+ constructor(customAdmins?: (string | AdminConfig)[]);
62
+ isAdmin(identifier: string): boolean;
63
+ getRole(identifier?: string): string;
64
+ hasPermission(user: User$1 | null, permissions: string | string[]): boolean;
65
+ canAccessAdmin(user: User$1 | null): PermissionCheckResult;
66
+ getErrorMessage(requiredRole: string, userRole: string, action?: string): string;
67
+ getUserDisplay(user: User$1 | null): {
68
+ displayName: string;
69
+ role: string;
70
+ badge: string | null;
71
+ isAdmin: boolean;
72
+ };
73
+ requiresAuth(user: User$1 | null): boolean;
74
+ requiresAdmin(user: User$1 | null): boolean;
75
+ addCustomAdmin(admin: string | AdminConfig): void;
76
+ removeCustomAdmin(admin: string | AdminConfig): void;
77
+ }
78
+ /**
79
+ * Create a permission checker instance
80
+ */
81
+ declare function createPermissionChecker(customAdmins?: (string | AdminConfig)[]): PermissionChecker;
82
+ declare const defaultPermissionChecker: PermissionChecker;
83
+
84
+ /**
85
+ * @spaps/sdk - Sweet Potato Authentication & Payment Service SDK
86
+ * Zero-config client for SPAPS authentication and payments
87
+ */
88
+ interface SPAPSConfig {
89
+ apiUrl?: string;
90
+ apiKey?: string;
91
+ autoDetect?: boolean;
92
+ timeout?: number;
93
+ }
94
+ interface AuthResponse {
95
+ access_token: string;
96
+ refresh_token: string;
97
+ user: User;
98
+ }
99
+ interface User {
100
+ id: string;
101
+ email?: string;
102
+ wallet_address?: string;
103
+ chain_type?: string;
104
+ role: string;
105
+ created_at?: string;
106
+ }
107
+ interface CheckoutSession {
108
+ sessionId: string;
109
+ url: string;
110
+ }
111
+ interface Subscription {
112
+ id: string;
113
+ status: string;
114
+ plan: string;
115
+ current_period_end: string;
116
+ }
117
+ interface UsageBalance {
118
+ balance: number;
119
+ currency: string;
120
+ updated_at: string;
121
+ }
122
+ interface Product {
123
+ id: string;
124
+ name: string;
125
+ description?: string;
126
+ category: string;
127
+ active: boolean;
128
+ images?: string[];
129
+ metadata?: Record<string, string>;
130
+ statement_descriptor?: string;
131
+ unit_label?: string;
132
+ created?: number;
133
+ updated?: number;
134
+ }
135
+ interface Price {
136
+ id: string;
137
+ product: string;
138
+ unit_amount: number;
139
+ currency: string;
140
+ recurring?: {
141
+ interval: 'day' | 'week' | 'month' | 'year';
142
+ interval_count: number;
143
+ };
144
+ nickname?: string;
145
+ active: boolean;
146
+ }
147
+ interface CreateProductRequest {
148
+ name: string;
149
+ description?: string;
150
+ category: string;
151
+ images?: string[];
152
+ metadata?: Record<string, string>;
153
+ active?: boolean;
154
+ statement_descriptor?: string;
155
+ unit_label?: string;
156
+ }
157
+ interface UpdateProductRequest {
158
+ name?: string;
159
+ description?: string;
160
+ category?: string;
161
+ images?: string[];
162
+ metadata?: Record<string, string>;
163
+ active?: boolean;
164
+ statement_descriptor?: string;
165
+ unit_label?: string;
166
+ }
167
+ interface CreatePriceRequest {
168
+ product_id: string;
169
+ unit_amount: number;
170
+ currency: string;
171
+ interval?: 'day' | 'week' | 'month' | 'year';
172
+ interval_count?: number;
173
+ nickname?: string;
174
+ }
175
+ interface ProductSyncResult {
176
+ synced_count: number;
177
+ updated_count: number;
178
+ created_count: number;
179
+ errors: any[];
180
+ sync_time: string;
181
+ }
182
+ declare class SPAPSClient {
183
+ private client;
184
+ private apiKey?;
185
+ private accessToken?;
186
+ private refreshToken?;
187
+ private _isLocalMode;
188
+ admin: {
189
+ createProduct: (productData: CreateProductRequest) => Promise<{
190
+ data: Product;
191
+ }>;
192
+ updateProduct: (productId: string, updates: UpdateProductRequest) => Promise<{
193
+ data: Product;
194
+ }>;
195
+ deleteProduct: (productId: string) => Promise<{
196
+ data: {
197
+ id: string;
198
+ active: boolean;
199
+ archived: boolean;
200
+ };
201
+ }>;
202
+ createPrice: (priceData: CreatePriceRequest) => Promise<{
203
+ data: Price;
204
+ }>;
205
+ syncProducts: () => Promise<{
206
+ data: ProductSyncResult;
207
+ }>;
208
+ getProducts: () => Promise<{
209
+ data: {
210
+ products: Product[];
211
+ total: number;
212
+ adminMetadata?: any;
213
+ };
214
+ }>;
215
+ };
216
+ constructor(config?: SPAPSConfig);
217
+ login(email: string, password: string): Promise<{
218
+ data: AuthResponse;
219
+ }>;
220
+ register(email: string, password: string): Promise<{
221
+ data: AuthResponse;
222
+ }>;
223
+ walletSignIn(walletAddress: string, signature: string, message: string, chainType?: 'solana' | 'ethereum'): Promise<{
224
+ data: AuthResponse;
225
+ }>;
226
+ refresh(refreshToken?: string): Promise<{
227
+ data: AuthResponse;
228
+ }>;
229
+ logout(): Promise<void>;
230
+ getUser(): Promise<{
231
+ data: User;
232
+ }>;
233
+ createCheckoutSession(priceId: string, successUrl: string, cancelUrl?: string): Promise<{
234
+ data: CheckoutSession;
235
+ }>;
236
+ getSubscription(): Promise<{
237
+ data: Subscription;
238
+ }>;
239
+ cancelSubscription(): Promise<void>;
240
+ getUsageBalance(): Promise<{
241
+ data: UsageBalance;
242
+ }>;
243
+ recordUsage(feature: string, amount: number): Promise<void>;
244
+ /**
245
+ * Create a new Stripe product (Admin required)
246
+ */
247
+ createProduct(productData: CreateProductRequest): Promise<{
248
+ data: Product;
249
+ }>;
250
+ /**
251
+ * Update an existing Stripe product (Admin required)
252
+ */
253
+ updateProduct(productId: string, updates: UpdateProductRequest): Promise<{
254
+ data: Product;
255
+ }>;
256
+ /**
257
+ * Archive (soft delete) a Stripe product (Admin required)
258
+ */
259
+ deleteProduct(productId: string): Promise<{
260
+ data: {
261
+ id: string;
262
+ active: boolean;
263
+ archived: boolean;
264
+ };
265
+ }>;
266
+ /**
267
+ * Create a new price for a product (Admin required)
268
+ */
269
+ createPrice(priceData: CreatePriceRequest): Promise<{
270
+ data: Price;
271
+ }>;
272
+ /**
273
+ * Sync all products from Stripe to local database (Super Admin required)
274
+ */
275
+ syncProducts(): Promise<{
276
+ data: ProductSyncResult;
277
+ }>;
278
+ /**
279
+ * Get products with admin metadata (if user is admin)
280
+ */
281
+ getProducts(): Promise<{
282
+ data: {
283
+ products: Product[];
284
+ total: number;
285
+ adminMetadata?: any;
286
+ };
287
+ }>;
288
+ isAuthenticated(): boolean;
289
+ getAccessToken(): string | undefined;
290
+ setAccessToken(token: string): void;
291
+ isLocalMode(): boolean;
292
+ /**
293
+ * Check if current user has admin privileges
294
+ * Note: This requires the user object from authentication
295
+ */
296
+ isAdmin(user?: User): boolean;
297
+ health(): Promise<{
298
+ data: any;
299
+ }>;
300
+ }
301
+
302
+ export { type AdminConfig, type AuthResponse, type CheckoutSession, type CreatePriceRequest, type CreateProductRequest, DEFAULT_ADMIN_ACCOUNTS, type PermissionCheckResult, PermissionChecker, type Price, type Product, type ProductSyncResult, SPAPSClient as SPAPS, SPAPSClient, type SPAPSConfig, type Subscription, SPAPSClient as SweetPotatoSDK, type UpdateProductRequest, type UsageBalance, type User, canAccessAdmin, createPermissionChecker, SPAPSClient as default, defaultPermissionChecker, getRoleAwareErrorMessage, getUserDisplay, getUserRole, hasPermission, isAdminAccount };