@symbo.ls/sdk 2.32.11 → 2.32.13

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.
Files changed (50) hide show
  1. package/README.md +141 -0
  2. package/dist/cjs/config/environment.js +18 -7
  3. package/dist/cjs/index.js +38 -12
  4. package/dist/cjs/services/BaseService.js +46 -0
  5. package/dist/cjs/services/DnsService.js +6 -5
  6. package/dist/cjs/services/TrackingService.js +661 -0
  7. package/dist/cjs/services/index.js +5 -5
  8. package/dist/cjs/utils/changePreprocessor.js +8 -1
  9. package/dist/cjs/utils/services.js +27 -3
  10. package/dist/esm/config/environment.js +18 -7
  11. package/dist/esm/index.js +20747 -5912
  12. package/dist/esm/services/AdminService.js +64 -7
  13. package/dist/esm/services/AuthService.js +64 -7
  14. package/dist/esm/services/BaseService.js +64 -7
  15. package/dist/esm/services/BranchService.js +64 -7
  16. package/dist/esm/services/CollabService.js +72 -8
  17. package/dist/esm/services/DnsService.js +70 -12
  18. package/dist/esm/services/FileService.js +64 -7
  19. package/dist/esm/services/PaymentService.js +64 -7
  20. package/dist/esm/services/PlanService.js +64 -7
  21. package/dist/esm/services/ProjectService.js +72 -8
  22. package/dist/esm/services/PullRequestService.js +64 -7
  23. package/dist/esm/services/ScreenshotService.js +64 -7
  24. package/dist/esm/services/SubscriptionService.js +64 -7
  25. package/dist/esm/services/TrackingService.js +18321 -0
  26. package/dist/esm/services/index.js +20667 -5882
  27. package/dist/esm/utils/CollabClient.js +18 -7
  28. package/dist/esm/utils/changePreprocessor.js +8 -1
  29. package/dist/esm/utils/services.js +27 -3
  30. package/dist/node/config/environment.js +18 -7
  31. package/dist/node/index.js +42 -16
  32. package/dist/node/services/BaseService.js +46 -0
  33. package/dist/node/services/DnsService.js +6 -5
  34. package/dist/node/services/TrackingService.js +632 -0
  35. package/dist/node/services/index.js +5 -5
  36. package/dist/node/utils/changePreprocessor.js +8 -1
  37. package/dist/node/utils/services.js +27 -3
  38. package/package.json +8 -6
  39. package/src/config/environment.js +19 -11
  40. package/src/index.js +44 -14
  41. package/src/services/BaseService.js +43 -0
  42. package/src/services/DnsService.js +5 -5
  43. package/src/services/TrackingService.js +853 -0
  44. package/src/services/index.js +6 -5
  45. package/src/utils/changePreprocessor.js +25 -1
  46. package/src/utils/services.js +28 -4
  47. package/dist/cjs/services/CoreService.js +0 -2818
  48. package/dist/esm/services/CoreService.js +0 -3513
  49. package/dist/node/services/CoreService.js +0 -2789
  50. package/src/services/CoreService.js +0 -3208
@@ -1,3208 +0,0 @@
1
- import { BaseService } from './BaseService.js'
2
- import environment from '../config/environment.js'
3
- import { getTokenManager } from '../utils/TokenManager.js'
4
-
5
- export class CoreService extends BaseService {
6
- constructor (config) {
7
- super(config)
8
- this._client = null
9
- this._initialized = false
10
- this._apiUrl = null
11
- this._tokenManager = null
12
- }
13
-
14
- init ({ context }) {
15
- try {
16
- const { appKey, authToken } = context || this._context
17
-
18
- // Get base URL from environment config
19
- this._apiUrl = environment.apiUrl
20
-
21
- if (!this._apiUrl) {
22
- throw new Error('Core service base URL not configured')
23
- }
24
-
25
- // Initialize token manager
26
- this._tokenManager = getTokenManager({
27
- apiUrl: this._apiUrl,
28
- onTokenRefresh: tokens => {
29
- // Update context with new token
30
- this.updateContext({ authToken: tokens.accessToken })
31
- },
32
- onTokenExpired: () => {
33
- // Clear context token
34
- this.updateContext({ authToken: null })
35
- },
36
- onTokenError: error => {
37
- console.error('Token management error:', error)
38
- }
39
- })
40
-
41
- if (authToken && !this._tokenManager.hasTokens()) {
42
- this._tokenManager.setTokens({ access_token: authToken })
43
- }
44
-
45
- // Store masked configuration info
46
- this._info = {
47
- config: {
48
- apiUrl: this._apiUrl,
49
- appKey: appKey
50
- ? `${appKey.substr(0, 4)}...${appKey.substr(-4)}`
51
- : null,
52
- hasToken: Boolean(authToken)
53
- }
54
- }
55
-
56
- this._initialized = true
57
- this._setReady()
58
- } catch (error) {
59
- this._setError(error)
60
- throw error
61
- }
62
- }
63
-
64
- // Helper to check if method requires initialization
65
- _requiresInit (methodName) {
66
- const noInitMethods = new Set([
67
- 'register',
68
- 'login',
69
- 'googleAuth',
70
- 'googleAuthCallback',
71
- 'githubAuth',
72
- 'requestPasswordReset',
73
- 'confirmPasswordReset',
74
- 'confirmRegistration',
75
- 'verifyEmail',
76
- 'listPublicProjects',
77
- 'getPublicProject',
78
- 'getHealthStatus'
79
- ])
80
- return !noInitMethods.has(methodName)
81
- }
82
-
83
- // Override _requireReady to be more flexible
84
- _requireReady (methodName) {
85
- if (this._requiresInit(methodName) && !this._initialized) {
86
- throw new Error('Core service not initialized')
87
- }
88
- }
89
-
90
- // Debug method to check token status
91
- getTokenDebugInfo () {
92
- if (!this._tokenManager) {
93
- return {
94
- tokenManagerExists: false,
95
- error: 'TokenManager not initialized'
96
- }
97
- }
98
-
99
- const tokenStatus = this._tokenManager.getTokenStatus()
100
- const { tokens } = this._tokenManager
101
-
102
- return {
103
- tokenManagerExists: true,
104
- tokenStatus,
105
- hasAccessToken: Boolean(tokens.accessToken),
106
- hasRefreshToken: Boolean(tokens.refreshToken),
107
- accessTokenPreview: tokens.accessToken
108
- ? `${tokens.accessToken.substring(0, 20)}...`
109
- : null,
110
- expiresAt: tokens.expiresAt,
111
- timeToExpiry: tokenStatus.timeToExpiry,
112
- authHeader: this._tokenManager.getAuthHeader()
113
- }
114
- }
115
-
116
- // Helper method to check if user is authenticated
117
- isAuthenticated () {
118
- if (!this._tokenManager) {
119
- return false
120
- }
121
- return this._tokenManager.hasTokens()
122
- }
123
-
124
- // Helper method to check if user has valid tokens
125
- hasValidTokens () {
126
- if (!this._tokenManager) {
127
- return false
128
- }
129
- return (
130
- this._tokenManager.hasTokens() && this._tokenManager.isAccessTokenValid()
131
- )
132
- }
133
-
134
- // Helper method to make HTTP requests
135
- async _request (endpoint, options = {}) {
136
- const url = `${this._apiUrl}/core${endpoint}`
137
-
138
- const defaultHeaders = {}
139
-
140
- // Only set Content-Type for JSON requests, not for FormData
141
- if (!(options.body instanceof FormData)) {
142
- defaultHeaders['Content-Type'] = 'application/json'
143
- }
144
-
145
- // Use TokenManager for automatic token management
146
- if (this._requiresInit(options.methodName) && this._tokenManager) {
147
- try {
148
- // Ensure we have a valid token (will refresh if needed)
149
- const validToken = await this._tokenManager.ensureValidToken()
150
-
151
- if (validToken) {
152
- const authHeader = this._tokenManager.getAuthHeader()
153
- if (authHeader) {
154
- defaultHeaders.Authorization = authHeader
155
- }
156
- }
157
- } catch (error) {
158
- console.warn(
159
- 'Token management failed, proceeding without authentication:',
160
- error
161
- )
162
- }
163
- } else if (this._requiresInit(options.methodName)) {
164
- // Fallback to context token if TokenManager not available
165
- const { authToken } = this._context
166
- if (authToken) {
167
- defaultHeaders.Authorization = `Bearer ${authToken}`
168
- }
169
- }
170
-
171
- try {
172
- const response = await fetch(url, {
173
- ...options,
174
- headers: {
175
- ...defaultHeaders,
176
- ...options.headers
177
- }
178
- })
179
-
180
- if (!response.ok) {
181
- let error = {
182
- message: `HTTP ${response.status}: ${response.statusText}`
183
- }
184
- try {
185
- error = await response.json()
186
- } catch {
187
- // Use default error message
188
- }
189
- throw new Error(error.message || error.error || 'Request failed', { cause: error })
190
- }
191
-
192
- return response.status === 204 ? null : response.json()
193
- } catch (error) {
194
- throw new Error(`Request failed: ${error.message}`, { cause: error })
195
- }
196
- }
197
-
198
- // ==================== AUTH METHODS ====================
199
-
200
- async register (userData) {
201
- try {
202
- const response = await this._request('/auth/register', {
203
- method: 'POST',
204
- body: JSON.stringify(userData),
205
- methodName: 'register'
206
- })
207
- if (response.success) {
208
- return response.data
209
- }
210
- throw new Error(response.message)
211
- } catch (error) {
212
- throw new Error(`Registration failed: ${error.message}`, { cause: error })
213
- }
214
- }
215
-
216
- async login (email, password) {
217
- try {
218
- const response = await this._request('/auth/login', {
219
- method: 'POST',
220
- body: JSON.stringify({ email, password }),
221
- methodName: 'login'
222
- })
223
-
224
- // Handle new response format: response.data.tokens
225
- if (response.success && response.data && response.data.tokens) {
226
- const { tokens } = response.data
227
- const tokenData = {
228
- access_token: tokens.accessToken,
229
- refresh_token: tokens.refreshToken,
230
- expires_in: tokens.accessTokenExp?.expiresIn,
231
- token_type: 'Bearer'
232
- }
233
-
234
- // Set tokens in TokenManager (will handle persistence and refresh scheduling)
235
- if (this._tokenManager) {
236
- this._tokenManager.setTokens(tokenData)
237
- }
238
-
239
- // Update context for backward compatibility
240
- this.updateContext({ authToken: tokens.accessToken })
241
- }
242
-
243
- if (response.success) {
244
- return response.data
245
- }
246
- throw new Error(response.message)
247
- } catch (error) {
248
- throw new Error(`Login failed: ${error.message}`, { cause: error })
249
- }
250
- }
251
-
252
- async logout () {
253
- this._requireReady('logout')
254
- try {
255
- // Call the logout API endpoint
256
- await this._request('/auth/logout', {
257
- method: 'POST',
258
- methodName: 'logout'
259
- })
260
-
261
- // Clear tokens from TokenManager and context
262
- if (this._tokenManager) {
263
- this._tokenManager.clearTokens()
264
- }
265
- this.updateContext({ authToken: null })
266
- } catch (error) {
267
- // Even if the API call fails, clear local tokens
268
- if (this._tokenManager) {
269
- this._tokenManager.clearTokens()
270
- }
271
- this.updateContext({ authToken: null })
272
-
273
- throw new Error(`Logout failed: ${error.message}`, { cause: error })
274
- }
275
- }
276
-
277
- async refreshToken (refreshToken) {
278
- try {
279
- const response = await this._request('/auth/refresh', {
280
- method: 'POST',
281
- body: JSON.stringify({ refreshToken }),
282
- methodName: 'refreshToken'
283
- })
284
- if (response.success) {
285
- return response.data
286
- }
287
- throw new Error(response.message)
288
- } catch (error) {
289
- throw new Error(`Token refresh failed: ${error.message}`, { cause: error })
290
- }
291
- }
292
-
293
- async googleAuth (idToken, inviteToken = null) {
294
- try {
295
- const payload = { idToken }
296
- if (inviteToken) {payload.inviteToken = inviteToken}
297
-
298
- const response = await this._request('/auth/google', {
299
- method: 'POST',
300
- body: JSON.stringify(payload),
301
- methodName: 'googleAuth'
302
- })
303
-
304
- // Handle new response format: response.data.tokens
305
- if (response.success && response.data && response.data.tokens) {
306
- const { tokens } = response.data
307
- const tokenData = {
308
- access_token: tokens.accessToken,
309
- refresh_token: tokens.refreshToken,
310
- expires_in: tokens.accessTokenExp?.expiresIn,
311
- token_type: 'Bearer'
312
- }
313
-
314
- // Set tokens in TokenManager
315
- if (this._tokenManager) {
316
- this._tokenManager.setTokens(tokenData)
317
- }
318
-
319
- // Update context for backward compatibility
320
- this.updateContext({ authToken: tokens.accessToken })
321
- }
322
-
323
- if (response.success) {
324
- return response.data
325
- }
326
- throw new Error(response.message)
327
- } catch (error) {
328
- throw new Error(`Google auth failed: ${error.message}`, { cause: error })
329
- }
330
- }
331
-
332
- async githubAuth (code, inviteToken = null) {
333
- try {
334
- const payload = { code }
335
- if (inviteToken) {payload.inviteToken = inviteToken}
336
-
337
- const response = await this._request('/auth/github', {
338
- method: 'POST',
339
- body: JSON.stringify(payload),
340
- methodName: 'githubAuth'
341
- })
342
-
343
- // Handle new response format: response.data.tokens
344
- if (response.success && response.data && response.data.tokens) {
345
- const { tokens } = response.data
346
- const tokenData = {
347
- access_token: tokens.accessToken,
348
- refresh_token: tokens.refreshToken,
349
- expires_in: tokens.accessTokenExp?.expiresIn,
350
- token_type: 'Bearer'
351
- }
352
-
353
- // Set tokens in TokenManager
354
- if (this._tokenManager) {
355
- this._tokenManager.setTokens(tokenData)
356
- }
357
-
358
- // Update context for backward compatibility
359
- this.updateContext({ authToken: tokens.accessToken })
360
- }
361
-
362
- if (response.success) {
363
- return response.data
364
- }
365
- throw new Error(response.message)
366
- } catch (error) {
367
- throw new Error(`GitHub auth failed: ${error.message}`, { cause: error })
368
- }
369
- }
370
-
371
- async googleAuthCallback (code, redirectUri, inviteToken = null) {
372
- try {
373
- const body = { code, redirectUri }
374
- if (inviteToken) {
375
- body.inviteToken = inviteToken
376
- }
377
-
378
- const response = await this._request('/auth/google/callback', {
379
- method: 'POST',
380
- body: JSON.stringify(body),
381
- methodName: 'googleAuthCallback'
382
- })
383
-
384
- // Handle new response format: response.data.tokens
385
- if (response.success && response.data && response.data.tokens) {
386
- const { tokens } = response.data
387
- const tokenData = {
388
- access_token: tokens.accessToken,
389
- refresh_token: tokens.refreshToken,
390
- expires_in: tokens.accessTokenExp?.expiresIn,
391
- token_type: 'Bearer'
392
- }
393
-
394
- // Set tokens in TokenManager
395
- if (this._tokenManager) {
396
- this._tokenManager.setTokens(tokenData)
397
- }
398
-
399
- // Update context for backward compatibility
400
- this.updateContext({ authToken: tokens.accessToken })
401
- }
402
-
403
- if (response.success) {
404
- return response.data
405
- }
406
- throw new Error(response.message)
407
- } catch (error) {
408
- throw new Error(`Google auth callback failed: ${error.message}`, { cause: error })
409
- }
410
- }
411
-
412
- async requestPasswordReset (email) {
413
- try {
414
- const response = await this._request('/auth/request-password-reset', {
415
- method: 'POST',
416
- body: JSON.stringify({ email }),
417
- methodName: 'requestPasswordReset'
418
- })
419
- if (response.success) {
420
- return response.data
421
- }
422
- throw new Error(response.message)
423
- } catch (error) {
424
- throw new Error(`Password reset request failed: ${error.message}`, { cause: error })
425
- }
426
- }
427
-
428
- async confirmPasswordReset (token, password) {
429
- try {
430
- const response = await this._request('/auth/reset-password-confirm', {
431
- method: 'POST',
432
- body: JSON.stringify({ token, password }),
433
- methodName: 'confirmPasswordReset'
434
- })
435
- if (response.success) {
436
- return response.data
437
- }
438
- throw new Error(response.message)
439
- } catch (error) {
440
- throw new Error(`Password reset confirmation failed: ${error.message}`, { cause: error })
441
- }
442
- }
443
-
444
- async confirmRegistration (token) {
445
- try {
446
- const response = await this._request('/auth/register-confirmation', {
447
- method: 'POST',
448
- body: JSON.stringify({ token }),
449
- methodName: 'confirmRegistration'
450
- })
451
- if (response.success) {
452
- return response.data
453
- }
454
- throw new Error(response.message)
455
- } catch (error) {
456
- throw new Error(`Registration confirmation failed: ${error.message}`, { cause: error })
457
- }
458
- }
459
-
460
- async requestPasswordChange () {
461
- this._requireReady('requestPasswordChange')
462
- try {
463
- const response = await this._request('/auth/request-password-change', {
464
- method: 'POST',
465
- methodName: 'requestPasswordChange'
466
- })
467
- if (response.success) {
468
- return response.data
469
- }
470
- throw new Error(response.message)
471
- } catch (error) {
472
- throw new Error(`Password change request failed: ${error.message}`, { cause: error })
473
- }
474
- }
475
-
476
- async confirmPasswordChange (currentPassword, newPassword, code) {
477
- this._requireReady('confirmPasswordChange')
478
- try {
479
- const response = await this._request('/auth/confirm-password-change', {
480
- method: 'POST',
481
- body: JSON.stringify({ currentPassword, newPassword, code }),
482
- methodName: 'confirmPasswordChange'
483
- })
484
- if (response.success) {
485
- return response.data
486
- }
487
- throw new Error(response.message)
488
- } catch (error) {
489
- throw new Error(`Password change confirmation failed: ${error.message}`, { cause: error })
490
- }
491
- }
492
-
493
- async getMe () {
494
- this._requireReady('getMe')
495
- try {
496
- const response = await this._request('/auth/me', {
497
- method: 'GET',
498
- methodName: 'getMe'
499
- })
500
- if (response.success) {
501
- return response.data
502
- }
503
- throw new Error(response.message)
504
- } catch (error) {
505
- throw new Error(`Failed to get user profile: ${error.message}`, { cause: error })
506
- }
507
- }
508
-
509
- /**
510
- * Get stored authentication state (backward compatibility method)
511
- * Replaces AuthService.getStoredAuthState()
512
- */
513
- async getStoredAuthState () {
514
- try {
515
- if (!this._tokenManager) {
516
- return {
517
- userId: false,
518
- authToken: false
519
- }
520
- }
521
-
522
- const tokenStatus = this._tokenManager.getTokenStatus()
523
-
524
- if (!tokenStatus.hasTokens) {
525
- return {
526
- userId: false,
527
- authToken: false
528
- }
529
- }
530
-
531
- // If tokens exist but are invalid, try to refresh
532
- if (!tokenStatus.isValid && tokenStatus.hasRefreshToken) {
533
- try {
534
- await this._tokenManager.ensureValidToken()
535
- } catch (error) {
536
- console.warn('[CoreService] Token refresh failed:', error.message)
537
- // Only clear tokens if it's definitely an auth error, not a network error
538
- if (
539
- error.message.includes('401') ||
540
- error.message.includes('403') ||
541
- error.message.includes('invalid') ||
542
- error.message.includes('expired')
543
- ) {
544
- this._tokenManager.clearTokens()
545
- return {
546
- userId: false,
547
- authToken: false,
548
- error: `Authentication failed: ${error.message}`
549
- }
550
- }
551
- // For network errors, keep tokens and return what we have
552
- return {
553
- userId: false,
554
- authToken: this._tokenManager.getAccessToken(),
555
- error: `Network error during token refresh: ${error.message}`,
556
- hasTokens: true
557
- }
558
- }
559
- }
560
-
561
- // Check if we have a valid token now
562
- const currentAccessToken = this._tokenManager.getAccessToken()
563
- if (!currentAccessToken) {
564
- return {
565
- userId: false,
566
- authToken: false
567
- }
568
- }
569
-
570
- // Get current user data if we have valid tokens
571
- // Be more lenient with API failures - don't immediately clear tokens
572
- try {
573
- const currentUser = await this.getMe()
574
-
575
- return {
576
- userId: currentUser.user.id,
577
- authToken: currentAccessToken,
578
- ...currentUser,
579
- error: null
580
- }
581
- } catch (error) {
582
- console.warn('[CoreService] Failed to get user data:', error.message)
583
-
584
- // Only clear tokens if it's an auth error (401, 403), not network errors
585
- if (error.message.includes('401') || error.message.includes('403')) {
586
- this._tokenManager.clearTokens()
587
- return {
588
- userId: false,
589
- authToken: false,
590
- error: `Authentication failed: ${error.message}`
591
- }
592
- }
593
-
594
- // For other errors (network, 500, etc.), keep tokens but return minimal state
595
- return {
596
- userId: false,
597
- authToken: currentAccessToken,
598
- error: `Failed to get user data: ${error.message}`,
599
- hasTokens: true
600
- }
601
- }
602
- } catch (error) {
603
- console.error(
604
- '[CoreService] Unexpected error in getStoredAuthState:',
605
- error
606
- )
607
- return {
608
- userId: false,
609
- authToken: false,
610
- error: `Failed to get stored auth state: ${error.message}`
611
- }
612
- }
613
- }
614
-
615
- // ==================== USER METHODS ====================
616
-
617
- async getUserProfile () {
618
- this._requireReady('getUserProfile')
619
- try {
620
- const response = await this._request('/users/profile', {
621
- method: 'GET',
622
- methodName: 'getUserProfile'
623
- })
624
- if (response.success) {
625
- return response.data
626
- }
627
- throw new Error(response.message)
628
- } catch (error) {
629
- throw new Error(`Failed to get user profile: ${error.message}`)
630
- }
631
- }
632
-
633
- async updateUserProfile (profileData) {
634
- this._requireReady('updateUserProfile')
635
- try {
636
- const response = await this._request('/users/profile', {
637
- method: 'PATCH',
638
- body: JSON.stringify(profileData),
639
- methodName: 'updateUserProfile'
640
- })
641
- if (response.success) {
642
- return response.data
643
- }
644
- throw new Error(response.message)
645
- } catch (error) {
646
- throw new Error(`Failed to update user profile: ${error.message}`, { cause: error })
647
- }
648
- }
649
-
650
- async getUserProjects () {
651
- this._requireReady('getUserProjects')
652
- try {
653
- const response = await this._request('/users/projects', {
654
- method: 'GET',
655
- methodName: 'getUserProjects'
656
- })
657
- if (response.success) {
658
- return response.data.map(project => ({
659
- ...project,
660
- ...(project.icon && {
661
- icon: {
662
- src: `${this._apiUrl}/core/files/public/${project.icon.id}/download`,
663
- ...project.icon
664
- }
665
- })
666
- }))
667
- }
668
- throw new Error(response.message)
669
- } catch (error) {
670
- throw new Error(`Failed to get user projects: ${error.message}`, { cause: error })
671
- }
672
- }
673
-
674
- async getUser (userId) {
675
- this._requireReady('getUser')
676
- if (!userId) {
677
- throw new Error('User ID is required')
678
- }
679
- try {
680
- const response = await this._request(`/users/${userId}`, {
681
- method: 'GET',
682
- methodName: 'getUser'
683
- })
684
- if (response.success) {
685
- return response.data
686
- }
687
- throw new Error(response.message)
688
- } catch (error) {
689
- throw new Error(`Failed to get user: ${error.message}`, { cause: error })
690
- }
691
- }
692
-
693
- async getUserByEmail (email) {
694
- this._requireReady('getUserByEmail')
695
- if (!email) {
696
- throw new Error('Email is required')
697
- }
698
- try {
699
- const response = await this._request(`/auth/user?email=${email}`, {
700
- method: 'GET',
701
- methodName: 'getUserByEmail'
702
- })
703
- if (response.success) {
704
- return response.data.user
705
- }
706
- throw new Error(response.message)
707
- } catch (error) {
708
- throw new Error(`Failed to get user by email: ${error.message}`, { cause: error })
709
- }
710
- }
711
-
712
- // ==================== PROJECT METHODS ====================
713
-
714
- async createProject (projectData) {
715
- this._requireReady('createProject')
716
- try {
717
- const response = await this._request('/projects', {
718
- method: 'POST',
719
- body: JSON.stringify(projectData),
720
- methodName: 'createProject'
721
- })
722
- if (response.success) {
723
- return response.data
724
- }
725
- throw new Error(response.message)
726
- } catch (error) {
727
- throw new Error(`Failed to create project: ${error.message}`)
728
- }
729
- }
730
-
731
- async getProjects (params = {}) {
732
- this._requireReady('getProjects')
733
- try {
734
- const queryParams = new URLSearchParams()
735
-
736
- // Add query parameters
737
- Object.keys(params).forEach(key => {
738
- if (params[key] != null) {
739
- queryParams.append(key, params[key])
740
- }
741
- })
742
-
743
- const queryString = queryParams.toString()
744
- const url = `/projects${queryString ? `?${queryString}` : ''}`
745
-
746
- const response = await this._request(url, {
747
- method: 'GET',
748
- methodName: 'getProjects'
749
- })
750
- if (response.success) {
751
- return response
752
- }
753
- throw new Error(response.message)
754
- } catch (error) {
755
- throw new Error(`Failed to get projects: ${error.message}`)
756
- }
757
- }
758
-
759
- /**
760
- * Alias for getProjects for consistency with API naming
761
- */
762
- async listProjects (params = {}) {
763
- return await this.getProjects(params)
764
- }
765
-
766
- /**
767
- * List only public projects (no authentication required)
768
- */
769
- async listPublicProjects (params = {}) {
770
- try {
771
- const queryParams = new URLSearchParams()
772
-
773
- // Add query parameters
774
- Object.keys(params).forEach(key => {
775
- if (params[key] != null) {
776
- queryParams.append(key, params[key])
777
- }
778
- })
779
-
780
- const queryString = queryParams.toString()
781
- const url = `/projects/public${queryString ? `?${queryString}` : ''}`
782
-
783
- const response = await this._request(url, {
784
- method: 'GET',
785
- methodName: 'listPublicProjects'
786
- })
787
- if (response.success) {
788
- return response.data
789
- }
790
- throw new Error(response.message)
791
- } catch (error) {
792
- throw new Error(`Failed to list public projects: ${error.message}`)
793
- }
794
- }
795
-
796
- async getProject (projectId) {
797
- this._requireReady('getProject')
798
- if (!projectId) {
799
- throw new Error('Project ID is required')
800
- }
801
- try {
802
- const response = await this._request(`/projects/${projectId}`, {
803
- method: 'GET',
804
- methodName: 'getProject'
805
- })
806
- if (response.success) {
807
- const iconSrc = response.data.icon
808
- ? `${this._apiUrl}/core/files/public/${response.data.icon.id}/download`
809
- : null
810
- return {
811
- ...response.data,
812
- icon: { src: iconSrc, ...response.data.icon }
813
- }
814
- }
815
- throw new Error(response.message)
816
- } catch (error) {
817
- throw new Error(`Failed to get project: ${error.message}`)
818
- }
819
- }
820
-
821
- /**
822
- * Get a public project by ID (no authentication required)
823
- * Corresponds to router.get('/public/:projectId', ProjectController.getPublicProject)
824
- */
825
- async getPublicProject (projectId) {
826
- if (!projectId) {
827
- throw new Error('Project ID is required')
828
- }
829
- try {
830
- const response = await this._request(`/projects/public/${projectId}`, {
831
- method: 'GET',
832
- methodName: 'getPublicProject'
833
- })
834
- if (response.success) {
835
- const iconSrc = response.data.icon
836
- ? `${this._apiUrl}/core/files/public/${response.data.icon.id}/download`
837
- : null
838
- return {
839
- ...response.data,
840
- icon: { src: iconSrc, ...response.data.icon }
841
- }
842
- }
843
- throw new Error(response.message)
844
- } catch (error) {
845
- throw new Error(`Failed to get public project: ${error.message}`)
846
- }
847
- }
848
-
849
- async getProjectByKey (key) {
850
- this._requireReady('getProjectByKey')
851
- if (!key) {
852
- throw new Error('Project key is required')
853
- }
854
- try {
855
- // Fetch project by key using new backend route
856
- const response = await this._request(`/projects/key/${key}`, {
857
- method: 'GET',
858
- methodName: 'getProjectByKey'
859
- })
860
- if (response.success) {
861
- const iconSrc = response.data.icon
862
- ? `${this._apiUrl}/core/files/public/${response.data.icon.id}/download`
863
- : null
864
-
865
- return {
866
- ...response.data,
867
- icon: { src: iconSrc, ...response.data.icon }
868
- }
869
- }
870
-
871
- throw new Error(response.message)
872
- } catch (error) {
873
- throw new Error(`Failed to get project by key: ${error.message}`)
874
- }
875
- }
876
-
877
- /**
878
- * Get current project data by key (no project ID required)
879
- */
880
- async getProjectDataByKey (key, options = {}) {
881
- this._requireReady('getProjectDataByKey')
882
- if (!key) {
883
- throw new Error('Project key is required')
884
- }
885
-
886
- const {
887
- branch = 'main',
888
- version = 'latest',
889
- includeHistory = false
890
- } = options
891
-
892
- const queryParams = new URLSearchParams({
893
- branch,
894
- version,
895
- includeHistory: includeHistory.toString()
896
- }).toString()
897
-
898
- try {
899
- const response = await this._request(
900
- `/projects/key/${key}/data?${queryParams}`,
901
- {
902
- method: 'GET',
903
- methodName: 'getProjectDataByKey'
904
- }
905
- )
906
-
907
- if (response.success) {
908
- return response.data
909
- }
910
-
911
- throw new Error(response.message)
912
- } catch (error) {
913
- throw new Error(`Failed to get project data by key: ${error.message}`)
914
- }
915
- }
916
-
917
- async updateProject (projectId, data) {
918
- this._requireReady('updateProject')
919
- if (!projectId) {
920
- throw new Error('Project ID is required')
921
- }
922
- try {
923
- const response = await this._request(`/projects/${projectId}`, {
924
- method: 'PATCH',
925
- body: JSON.stringify(data),
926
- methodName: 'updateProject'
927
- })
928
- if (response.success) {
929
- return response.data
930
- }
931
- throw new Error(response.message)
932
- } catch (error) {
933
- throw new Error(`Failed to update project: ${error.message}`)
934
- }
935
- }
936
-
937
- async updateProjectComponents (projectId, components) {
938
- this._requireReady('updateProjectComponents')
939
- if (!projectId) {
940
- throw new Error('Project ID is required')
941
- }
942
- try {
943
- const response = await this._request(
944
- `/projects/${projectId}/components`,
945
- {
946
- method: 'PATCH',
947
- body: JSON.stringify({ components }),
948
- methodName: 'updateProjectComponents'
949
- }
950
- )
951
- if (response.success) {
952
- return response.data
953
- }
954
- throw new Error(response.message)
955
- } catch (error) {
956
- throw new Error(`Failed to update project components: ${error.message}`)
957
- }
958
- }
959
-
960
- async updateProjectSettings (projectId, settings) {
961
- this._requireReady('updateProjectSettings')
962
- if (!projectId) {
963
- throw new Error('Project ID is required')
964
- }
965
- try {
966
- const response = await this._request(`/projects/${projectId}/settings`, {
967
- method: 'PATCH',
968
- body: JSON.stringify({ settings }),
969
- methodName: 'updateProjectSettings'
970
- })
971
- if (response.success) {
972
- return response.data
973
- }
974
- throw new Error(response.message)
975
- } catch (error) {
976
- throw new Error(`Failed to update project settings: ${error.message}`)
977
- }
978
- }
979
-
980
- async updateProjectName (projectId, name) {
981
- this._requireReady('updateProjectName')
982
- if (!projectId) {
983
- throw new Error('Project ID is required')
984
- }
985
- try {
986
- const response = await this._request(`/projects/${projectId}`, {
987
- method: 'PATCH',
988
- body: JSON.stringify({ name }),
989
- methodName: 'updateProjectName'
990
- })
991
- if (response.success) {
992
- return response.data
993
- }
994
- throw new Error(response.message)
995
- } catch (error) {
996
- throw new Error(`Failed to update project name: ${error.message}`)
997
- }
998
- }
999
-
1000
- async updateProjectPackage (projectId, pkg) {
1001
- this._requireReady('updateProjectPackage')
1002
- if (!projectId) {
1003
- throw new Error('Project ID is required')
1004
- }
1005
- try {
1006
- // Updated endpoint and payload to align with new API (PATCH /projects/:projectId/package)
1007
- const response = await this._request(`/projects/${projectId}/package`, {
1008
- method: 'PATCH',
1009
- body: JSON.stringify({ package: pkg }),
1010
- methodName: 'updateProjectPackage'
1011
- })
1012
- if (response.success) {
1013
- return response.data
1014
- }
1015
- throw new Error(response.message)
1016
- } catch (error) {
1017
- throw new Error(`Failed to update project package: ${error.message}`)
1018
- }
1019
- }
1020
-
1021
- async duplicateProject (projectId, newName, newKey, targetUserId) {
1022
- this._requireReady('duplicateProject')
1023
- if (!projectId) {
1024
- throw new Error('Project ID is required')
1025
- }
1026
- try {
1027
- const response = await this._request(`/projects/${projectId}/duplicate`, {
1028
- method: 'POST',
1029
- body: JSON.stringify({ name: newName, key: newKey, targetUserId }),
1030
- methodName: 'duplicateProject'
1031
- })
1032
- if (response.success) {
1033
- return response.data
1034
- }
1035
- throw new Error(response.message)
1036
- } catch (error) {
1037
- throw new Error(`Failed to duplicate project: ${error.message}`)
1038
- }
1039
- }
1040
-
1041
- async removeProject (projectId) {
1042
- this._requireReady('removeProject')
1043
- if (!projectId) {
1044
- throw new Error('Project ID is required')
1045
- }
1046
- try {
1047
- const response = await this._request(`/projects/${projectId}`, {
1048
- method: 'DELETE',
1049
- methodName: 'removeProject'
1050
- })
1051
- if (response.success) {
1052
- return response
1053
- }
1054
- throw new Error(response.message)
1055
- } catch (error) {
1056
- throw new Error(`Failed to remove project: ${error.message}`)
1057
- }
1058
- }
1059
-
1060
- async checkProjectKeyAvailability (key) {
1061
- this._requireReady('checkProjectKeyAvailability')
1062
- if (!key) {
1063
- throw new Error('Project key is required')
1064
- }
1065
- try {
1066
- const response = await this._request(`/projects/check-key/${key}`, {
1067
- method: 'GET',
1068
- methodName: 'checkProjectKeyAvailability'
1069
- })
1070
- if (response.success) {
1071
- return response.data
1072
- }
1073
- throw new Error(response.message)
1074
- } catch (error) {
1075
- throw new Error(
1076
- `Failed to check project key availability: ${error.message}`
1077
- )
1078
- }
1079
- }
1080
-
1081
- // ==================== PROJECT MEMBER METHODS ====================
1082
-
1083
- async getProjectMembers (projectId) {
1084
- this._requireReady('getProjectMembers')
1085
- if (!projectId) {
1086
- throw new Error('Project ID is required')
1087
- }
1088
- try {
1089
- const response = await this._request(`/projects/${projectId}/members`, {
1090
- method: 'GET',
1091
- methodName: 'getProjectMembers'
1092
- })
1093
- if (response.success) {
1094
- return response.data
1095
- }
1096
- throw new Error(response.message)
1097
- } catch (error) {
1098
- throw new Error(`Failed to get project members: ${error.message}`)
1099
- }
1100
- }
1101
-
1102
- async inviteMember (projectId, email, role = 'guest', options = {}) {
1103
- this._requireReady('inviteMember')
1104
- if (!projectId || !email || !role) {
1105
- throw new Error('Project ID, email, and role are required')
1106
- }
1107
-
1108
- const { name, callbackUrl } = options
1109
-
1110
- // Default callbackUrl if not provided
1111
- const defaultCallbackUrl =
1112
- typeof window === 'undefined'
1113
- ? 'https://app.symbols.com/accept-invite'
1114
- : `${window.location.origin}/accept-invite`
1115
-
1116
- try {
1117
- const requestBody = {
1118
- email,
1119
- role,
1120
- callbackUrl: callbackUrl || defaultCallbackUrl
1121
- }
1122
-
1123
- // Add optional name if provided
1124
- if (name) {
1125
- requestBody.name = name
1126
- }
1127
-
1128
- const response = await this._request(`/projects/${projectId}/invite`, {
1129
- method: 'POST',
1130
- body: JSON.stringify(requestBody),
1131
- methodName: 'inviteMember'
1132
- })
1133
- if (response.success) {
1134
- return response.data
1135
- }
1136
- throw new Error(response.message)
1137
- } catch (error) {
1138
- throw new Error(`Failed to invite member: ${error.message}`)
1139
- }
1140
- }
1141
-
1142
- async acceptInvite (token) {
1143
- this._requireReady('acceptInvite')
1144
- if (!token) {
1145
- throw new Error('Invitation token is required')
1146
- }
1147
- try {
1148
- const response = await this._request('/projects/accept-invite', {
1149
- method: 'POST',
1150
- body: JSON.stringify({ token }),
1151
- methodName: 'acceptInvite'
1152
- })
1153
- if (response.success) {
1154
- return response.data
1155
- }
1156
- throw new Error(response.message)
1157
- } catch (error) {
1158
- throw new Error(`Failed to accept invite: ${error.message}`)
1159
- }
1160
- }
1161
-
1162
- async updateMemberRole (projectId, memberId, role) {
1163
- this._requireReady('updateMemberRole')
1164
- if (!projectId || !memberId || !role) {
1165
- throw new Error('Project ID, member ID, and role are required')
1166
- }
1167
- try {
1168
- const response = await this._request(
1169
- `/projects/${projectId}/members/${memberId}`,
1170
- {
1171
- method: 'PATCH',
1172
- body: JSON.stringify({ role }),
1173
- methodName: 'updateMemberRole'
1174
- }
1175
- )
1176
- if (response.success) {
1177
- return response.data
1178
- }
1179
- throw new Error(response.message)
1180
- } catch (error) {
1181
- throw new Error(`Failed to update member role: ${error.message}`)
1182
- }
1183
- }
1184
-
1185
- async removeMember (projectId, memberId) {
1186
- this._requireReady('removeMember')
1187
- if (!projectId || !memberId) {
1188
- throw new Error('Project ID and member ID are required')
1189
- }
1190
- try {
1191
- const response = await this._request(
1192
- `/projects/${projectId}/members/${memberId}`,
1193
- {
1194
- method: 'DELETE',
1195
- methodName: 'removeMember'
1196
- }
1197
- )
1198
- if (response.success) {
1199
- return response.data
1200
- }
1201
- throw new Error(response.message)
1202
- } catch (error) {
1203
- throw new Error(`Failed to remove member: ${error.message}`)
1204
- }
1205
- }
1206
-
1207
- // ==================== PROJECT LIBRARY METHODS ====================
1208
-
1209
- async getAvailableLibraries (params = {}) {
1210
- this._requireReady('getAvailableLibraries')
1211
- const queryParams = new URLSearchParams(params).toString()
1212
- try {
1213
- const response = await this._request(
1214
- `/projects/libraries/available?${queryParams}`,
1215
- {
1216
- method: 'GET',
1217
- methodName: 'getAvailableLibraries'
1218
- }
1219
- )
1220
- if (response.success) {
1221
- return response.data
1222
- }
1223
- throw new Error(response.message)
1224
- } catch (error) {
1225
- throw new Error(`Failed to get available libraries: ${error.message}`)
1226
- }
1227
- }
1228
-
1229
- async getProjectLibraries (projectId) {
1230
- this._requireReady('getProjectLibraries')
1231
- if (!projectId) {
1232
- throw new Error('Project ID is required')
1233
- }
1234
- try {
1235
- const response = await this._request(`/projects/${projectId}/libraries`, {
1236
- method: 'GET',
1237
- methodName: 'getProjectLibraries'
1238
- })
1239
- if (response.success) {
1240
- return response.data
1241
- }
1242
- throw new Error(response.message)
1243
- } catch (error) {
1244
- throw new Error(`Failed to get project libraries: ${error.message}`)
1245
- }
1246
- }
1247
-
1248
- async addProjectLibraries (projectId, libraryIds) {
1249
- this._requireReady('addProjectLibraries')
1250
- if (!projectId || !libraryIds) {
1251
- throw new Error('Project ID and library IDs are required')
1252
- }
1253
- try {
1254
- const response = await this._request(`/projects/${projectId}/libraries`, {
1255
- method: 'POST',
1256
- body: JSON.stringify({ libraryIds }),
1257
- methodName: 'addProjectLibraries'
1258
- })
1259
- if (response.success) {
1260
- return response.data
1261
- }
1262
- throw new Error(response.message)
1263
- } catch (error) {
1264
- throw new Error(`Failed to add project libraries: ${error.message}`)
1265
- }
1266
- }
1267
-
1268
- async removeProjectLibraries (projectId, libraryIds) {
1269
- this._requireReady('removeProjectLibraries')
1270
- if (!projectId || !libraryIds) {
1271
- throw new Error('Project ID and library IDs are required')
1272
- }
1273
- try {
1274
- const response = await this._request(`/projects/${projectId}/libraries`, {
1275
- method: 'DELETE',
1276
- body: JSON.stringify({ libraryIds }),
1277
- methodName: 'removeProjectLibraries'
1278
- })
1279
- if (response.success) {
1280
- return response
1281
- }
1282
- throw new Error(response.message)
1283
- } catch (error) {
1284
- throw new Error(`Failed to remove project libraries: ${error.message}`)
1285
- }
1286
- }
1287
-
1288
- // ==================== FILE METHODS ====================
1289
-
1290
- async uploadFile (file, options = {}) {
1291
- this._requireReady('uploadFile')
1292
- if (!file) {
1293
- throw new Error('File is required for upload')
1294
- }
1295
-
1296
- const formData = new FormData()
1297
- formData.append('file', file)
1298
-
1299
- // Add optional parameters only if they exist
1300
- if (options.projectId) {
1301
- formData.append('projectId', options.projectId)
1302
- }
1303
- if (options.tags) {
1304
- formData.append('tags', JSON.stringify(options.tags))
1305
- }
1306
- if (options.visibility) {
1307
- formData.append('visibility', options.visibility || 'public')
1308
- }
1309
- if (options.metadata) {
1310
- formData.append('metadata', JSON.stringify(options.metadata))
1311
- }
1312
-
1313
- try {
1314
- const response = await this._request('/files/upload', {
1315
- method: 'POST',
1316
- body: formData,
1317
- headers: {}, // Let browser set Content-Type for FormData
1318
- methodName: 'uploadFile'
1319
- })
1320
-
1321
- if (!response.success) {
1322
- throw new Error(response.message)
1323
- }
1324
-
1325
- return {
1326
- id: response.data.id,
1327
- src: `${this._apiUrl}/core/files/public/${response.data.id}/download`,
1328
- success: true,
1329
- message: response.message
1330
- }
1331
- } catch (error) {
1332
- throw new Error(`File upload failed: ${error.message}`)
1333
- }
1334
- }
1335
-
1336
- async updateProjectIcon (projectId, iconFile) {
1337
- this._requireReady('updateProjectIcon')
1338
- if (!projectId || !iconFile) {
1339
- throw new Error('Project ID and icon file are required')
1340
- }
1341
-
1342
- const formData = new FormData()
1343
- formData.append('icon', iconFile)
1344
- formData.append('projectId', projectId)
1345
-
1346
- try {
1347
- const response = await this._request('/files/upload-project-icon', {
1348
- method: 'POST',
1349
- body: formData,
1350
- headers: {}, // Let browser set Content-Type for FormData
1351
- methodName: 'updateProjectIcon'
1352
- })
1353
- if (response.success) {
1354
- return response.data
1355
- }
1356
- throw new Error(response.message)
1357
- } catch (error) {
1358
- throw new Error(`Failed to update project icon: ${error.message}`)
1359
- }
1360
- }
1361
-
1362
- // ==================== PAYMENT METHODS ====================
1363
-
1364
- async checkout (options = {}) {
1365
- this._requireReady('checkout')
1366
- const {
1367
- projectId,
1368
- seats = 1,
1369
- price = 'starter_monthly',
1370
- successUrl = `${window.location.origin}/success`,
1371
- cancelUrl = `${window.location.origin}/pricing`
1372
- } = options
1373
-
1374
- if (!projectId) {
1375
- throw new Error('Project ID is required for checkout')
1376
- }
1377
-
1378
- try {
1379
- const response = await this._request('/payments/checkout', {
1380
- method: 'POST',
1381
- body: JSON.stringify({
1382
- projectId,
1383
- seats,
1384
- price,
1385
- successUrl,
1386
- cancelUrl
1387
- }),
1388
- methodName: 'checkout'
1389
- })
1390
- if (response.success) {
1391
- return response.data
1392
- }
1393
- throw new Error(response.message)
1394
- } catch (error) {
1395
- throw new Error(`Failed to checkout: ${error.message}`)
1396
- }
1397
- }
1398
-
1399
- async getSubscriptionStatus (projectId) {
1400
- this._requireReady('getSubscriptionStatus')
1401
- if (!projectId) {
1402
- throw new Error('Project ID is required')
1403
- }
1404
- try {
1405
- const response = await this._request(
1406
- `/payments/subscription/${projectId}`,
1407
- {
1408
- method: 'GET',
1409
- methodName: 'getSubscriptionStatus'
1410
- }
1411
- )
1412
- if (response.success) {
1413
- return response.data
1414
- }
1415
- throw new Error(response.message)
1416
- } catch (error) {
1417
- throw new Error(`Failed to get subscription status: ${error.message}`)
1418
- }
1419
- }
1420
-
1421
- // ==================== DNS METHODS ====================
1422
-
1423
- async createDnsRecord (domain, options = {}) {
1424
- this._requireReady('createDnsRecord')
1425
- if (!domain) {
1426
- throw new Error('Domain is required')
1427
- }
1428
- try {
1429
- const response = await this._request('/dns/records', {
1430
- method: 'POST',
1431
- body: JSON.stringify({ domain, ...options }),
1432
- methodName: 'createDnsRecord'
1433
- })
1434
- if (response.success) {
1435
- return response.data
1436
- }
1437
- throw new Error(response.message)
1438
- } catch (error) {
1439
- throw new Error(`Failed to create DNS record: ${error.message}`)
1440
- }
1441
- }
1442
-
1443
- async getDnsRecord (domain) {
1444
- this._requireReady('getDnsRecord')
1445
- if (!domain) {
1446
- throw new Error('Domain is required')
1447
- }
1448
- try {
1449
- const response = await this._request(`/dns/records/${domain}`, {
1450
- method: 'GET',
1451
- methodName: 'getDnsRecord'
1452
- })
1453
- if (response.success) {
1454
- return response.data
1455
- }
1456
- throw new Error(response.message)
1457
- } catch (error) {
1458
- throw new Error(`Failed to get DNS record: ${error.message}`)
1459
- }
1460
- }
1461
-
1462
- async removeDnsRecord (domain) {
1463
- this._requireReady('removeDnsRecord')
1464
- if (!domain) {
1465
- throw new Error('Domain is required')
1466
- }
1467
- try {
1468
- const response = await this._request(`/dns/records/${domain}`, {
1469
- method: 'DELETE',
1470
- methodName: 'removeDnsRecord'
1471
- })
1472
- if (response.success) {
1473
- return response.data
1474
- }
1475
- throw new Error(response.message)
1476
- } catch (error) {
1477
- throw new Error(`Failed to remove DNS record: ${error.message}`)
1478
- }
1479
- }
1480
-
1481
- async setProjectDomains (
1482
- projectKey,
1483
- customDomain,
1484
- hasCustomDomainAccess = false
1485
- ) {
1486
- this._requireReady('setProjectDomains')
1487
- if (!projectKey) {
1488
- throw new Error('Project key is required')
1489
- }
1490
- try {
1491
- const response = await this._request('/dns/project-domains', {
1492
- method: 'POST',
1493
- body: JSON.stringify({
1494
- projectKey,
1495
- customDomain,
1496
- hasCustomDomainAccess
1497
- }),
1498
- methodName: 'setProjectDomains'
1499
- })
1500
- if (response.success) {
1501
- return response.data
1502
- }
1503
- throw new Error(response.message)
1504
- } catch (error) {
1505
- throw new Error(`Failed to set project domains: ${error.message}`)
1506
- }
1507
- }
1508
-
1509
- async addProjectCustomDomains (projectId, customDomains) {
1510
- this._requireReady('addProjectCustomDomains')
1511
- if (!projectId) {
1512
- throw new Error('Project ID is required')
1513
- }
1514
- if (
1515
- !customDomains ||
1516
- (Array.isArray(customDomains) && !customDomains.length)
1517
- ) {
1518
- throw new Error(
1519
- 'customDomains is required and must be a non-empty string or array'
1520
- )
1521
- }
1522
-
1523
- try {
1524
- const response = await this._request(`/projects/${projectId}/domains`, {
1525
- method: 'PATCH',
1526
- body: JSON.stringify({ customDomains }),
1527
- methodName: 'addProjectCustomDomains'
1528
- })
1529
- if (response.success) {
1530
- return response.data
1531
- }
1532
- throw new Error(response.message)
1533
- } catch (error) {
1534
- throw new Error(
1535
- `Failed to update project custom domains: ${error.message}`
1536
- )
1537
- }
1538
- }
1539
-
1540
- // ==================== UTILITY METHODS ====================
1541
-
1542
- async getHealthStatus () {
1543
- try {
1544
- const response = await this._request('/health', {
1545
- method: 'GET',
1546
- methodName: 'getHealthStatus'
1547
- })
1548
- if (response.success) {
1549
- return response.data
1550
- }
1551
- throw new Error(response.message)
1552
- } catch (error) {
1553
- throw new Error(`Failed to get health status: ${error.message}`)
1554
- }
1555
- }
1556
-
1557
- // ==================== PROJECT DATA METHODS (SYMSTORY REPLACEMENT) ====================
1558
-
1559
- /**
1560
- * Apply changes to a project, creating a new version
1561
- * Replaces: SymstoryService.updateData()
1562
- */
1563
- async applyProjectChanges (projectId, changes, options = {}) {
1564
- this._requireReady('applyProjectChanges')
1565
- if (!projectId) {
1566
- throw new Error('Project ID is required')
1567
- }
1568
- if (!Array.isArray(changes)) {
1569
- throw new Error('Changes must be an array')
1570
- }
1571
-
1572
- const { message, branch = 'main', type = 'patch' } = options
1573
-
1574
- try {
1575
- const response = await this._request(`/projects/${projectId}/changes`, {
1576
- method: 'POST',
1577
- body: JSON.stringify({
1578
- changes,
1579
- message,
1580
- branch,
1581
- type
1582
- }),
1583
- methodName: 'applyProjectChanges'
1584
- })
1585
-
1586
- if (response.success) {
1587
- return response.data
1588
- }
1589
- throw new Error(response.message)
1590
- } catch (error) {
1591
- throw new Error(`Failed to apply project changes: ${error.message}`)
1592
- }
1593
- }
1594
-
1595
- /**
1596
- * Get current project data for a specific branch
1597
- * Replaces: SymstoryService.getData()
1598
- */
1599
- async getProjectData (projectId, options = {}) {
1600
- this._requireReady('getProjectData')
1601
- if (!projectId) {
1602
- throw new Error('Project ID is required')
1603
- }
1604
-
1605
- const {
1606
- branch = 'main',
1607
- version = 'latest',
1608
- includeHistory = false
1609
- } = options
1610
-
1611
- const queryParams = new URLSearchParams({
1612
- branch,
1613
- version,
1614
- includeHistory: includeHistory.toString()
1615
- }).toString()
1616
-
1617
- try {
1618
- const response = await this._request(
1619
- `/projects/${projectId}/data?${queryParams}`,
1620
- {
1621
- method: 'GET',
1622
- methodName: 'getProjectData'
1623
- }
1624
- )
1625
- if (response.success) {
1626
- return response.data
1627
- }
1628
- throw new Error(response.message)
1629
- } catch (error) {
1630
- throw new Error(`Failed to get project data: ${error.message}`)
1631
- }
1632
- }
1633
-
1634
- /**
1635
- * Get project versions with pagination
1636
- */
1637
- async getProjectVersions (projectId, options = {}) {
1638
- this._requireReady('getProjectVersions')
1639
- if (!projectId) {
1640
- throw new Error('Project ID is required')
1641
- }
1642
-
1643
- const { branch = 'main', page = 1, limit = 50 } = options
1644
-
1645
- const queryParams = new URLSearchParams({
1646
- branch,
1647
- page: page.toString(),
1648
- limit: limit.toString()
1649
- }).toString()
1650
-
1651
- try {
1652
- const response = await this._request(
1653
- `/projects/${projectId}/versions?${queryParams}`,
1654
- {
1655
- method: 'GET',
1656
- methodName: 'getProjectVersions'
1657
- }
1658
- )
1659
- if (response.success) {
1660
- return response.data
1661
- }
1662
- throw new Error(response.message)
1663
- } catch (error) {
1664
- throw new Error(`Failed to get project versions: ${error.message}`)
1665
- }
1666
- }
1667
-
1668
- /**
1669
- * Restore project to a previous version
1670
- * Replaces: SymstoryService.restoreVersion()
1671
- */
1672
- async restoreProjectVersion (projectId, version, options = {}) {
1673
- this._requireReady('restoreProjectVersion')
1674
- if (!projectId) {
1675
- throw new Error('Project ID is required')
1676
- }
1677
- if (!version) {
1678
- throw new Error('Version is required')
1679
- }
1680
-
1681
- const { message, branch = 'main', type = 'patch' } = options
1682
-
1683
- try {
1684
- const response = await this._request(`/projects/${projectId}/restore`, {
1685
- method: 'POST',
1686
- body: JSON.stringify({
1687
- version,
1688
- message,
1689
- branch,
1690
- type
1691
- }),
1692
- methodName: 'restoreProjectVersion'
1693
- })
1694
- if (response.success) {
1695
- return response.data
1696
- }
1697
- throw new Error(response.message)
1698
- } catch (error) {
1699
- throw new Error(`Failed to restore project version: ${error.message}`)
1700
- }
1701
- }
1702
-
1703
- /**
1704
- * Helper method to update a single item in the project
1705
- * Convenience wrapper around applyProjectChanges
1706
- */
1707
- async updateProjectItem (projectId, path, value, options = {}) {
1708
- const changes = [['update', path, value]]
1709
- const message =
1710
- options.message ||
1711
- `Updated ${Array.isArray(path) ? path.join('.') : path}`
1712
-
1713
- return await this.applyProjectChanges(projectId, changes, {
1714
- ...options,
1715
- message
1716
- })
1717
- }
1718
-
1719
- /**
1720
- * Helper method to delete an item from the project
1721
- * Convenience wrapper around applyProjectChanges
1722
- */
1723
- async deleteProjectItem (projectId, path, options = {}) {
1724
- const changes = [['delete', path]]
1725
- const message =
1726
- options.message ||
1727
- `Deleted ${Array.isArray(path) ? path.join('.') : path}`
1728
-
1729
- return await this.applyProjectChanges(projectId, changes, {
1730
- ...options,
1731
- message
1732
- })
1733
- }
1734
-
1735
- /**
1736
- * Helper method to set a value in the project (alias for update)
1737
- * Convenience wrapper around applyProjectChanges
1738
- */
1739
- async setProjectValue (projectId, path, value, options = {}) {
1740
- const changes = [['set', path, value]]
1741
- const message =
1742
- options.message || `Set ${Array.isArray(path) ? path.join('.') : path}`
1743
-
1744
- return await this.applyProjectChanges(projectId, changes, {
1745
- ...options,
1746
- message
1747
- })
1748
- }
1749
-
1750
- /**
1751
- * Helper method to add multiple items to the project
1752
- * Convenience wrapper around applyProjectChanges
1753
- */
1754
- async addProjectItems (projectId, items, options = {}) {
1755
- const changes = items
1756
- .map(item => {
1757
- const [type, data] = item
1758
- const { value, ...schema } = data
1759
- return [
1760
- ['update', [type, data.key], value],
1761
- ['update', ['schema', type, data.key], schema]
1762
- ]
1763
- })
1764
- .flat()
1765
-
1766
- const message = options.message || `Added ${items.length} items`
1767
-
1768
- return await this.applyProjectChanges(projectId, changes, {
1769
- ...options,
1770
- message
1771
- })
1772
- }
1773
-
1774
- /**
1775
- * Helper method to get specific data from project by path
1776
- * Convenience wrapper that gets project data and extracts specific path
1777
- */
1778
- async getProjectItemByPath (projectId, path, options = {}) {
1779
- const projectData = await this.getProjectData(projectId, options)
1780
-
1781
- if (!projectData?.data) {
1782
- return null
1783
- }
1784
-
1785
- // Navigate to the specific path in the data
1786
- let current = projectData.data
1787
- const pathArray = Array.isArray(path) ? path : [path]
1788
-
1789
- for (const segment of pathArray) {
1790
- if (current && typeof current === 'object' && segment in current) {
1791
- current = current[segment]
1792
- } else {
1793
- return null
1794
- }
1795
- }
1796
-
1797
- return current
1798
- }
1799
-
1800
- // ==================== PULL REQUEST METHODS ====================
1801
-
1802
- /**
1803
- * Create a new pull request
1804
- */
1805
- async createPullRequest (projectId, pullRequestData) {
1806
- this._requireReady('createPullRequest')
1807
- if (!projectId) {
1808
- throw new Error('Project ID is required')
1809
- }
1810
- if (
1811
- !pullRequestData.source ||
1812
- !pullRequestData.target ||
1813
- !pullRequestData.title
1814
- ) {
1815
- throw new Error('Source branch, target branch, and title are required')
1816
- }
1817
-
1818
- try {
1819
- const response = await this._request(
1820
- `/projects/${projectId}/pull-requests`,
1821
- {
1822
- method: 'POST',
1823
- body: JSON.stringify(pullRequestData),
1824
- methodName: 'createPullRequest'
1825
- }
1826
- )
1827
- if (response.success) {
1828
- return response.data
1829
- }
1830
- throw new Error(response.message)
1831
- } catch (error) {
1832
- throw new Error(`Failed to create pull request: ${error.message}`)
1833
- }
1834
- }
1835
-
1836
- /**
1837
- * List pull requests for a project with filtering options
1838
- */
1839
- async listPullRequests (projectId, options = {}) {
1840
- this._requireReady('listPullRequests')
1841
- if (!projectId) {
1842
- throw new Error('Project ID is required')
1843
- }
1844
-
1845
- const { status = 'open', source, target, page = 1, limit = 20 } = options
1846
-
1847
- const queryParams = new URLSearchParams({
1848
- status,
1849
- page: page.toString(),
1850
- limit: limit.toString()
1851
- })
1852
-
1853
- if (source) {
1854
- queryParams.append('source', source)
1855
- }
1856
- if (target) {
1857
- queryParams.append('target', target)
1858
- }
1859
-
1860
- try {
1861
- const response = await this._request(
1862
- `/projects/${projectId}/pull-requests?${queryParams.toString()}`,
1863
- {
1864
- method: 'GET',
1865
- methodName: 'listPullRequests'
1866
- }
1867
- )
1868
- if (response.success) {
1869
- return response.data
1870
- }
1871
- throw new Error(response.message)
1872
- } catch (error) {
1873
- throw new Error(`Failed to list pull requests: ${error.message}`)
1874
- }
1875
- }
1876
-
1877
- /**
1878
- * Get detailed information about a specific pull request
1879
- */
1880
- async getPullRequest (projectId, prId) {
1881
- this._requireReady('getPullRequest')
1882
- if (!projectId) {
1883
- throw new Error('Project ID is required')
1884
- }
1885
- if (!prId) {
1886
- throw new Error('Pull request ID is required')
1887
- }
1888
-
1889
- try {
1890
- const response = await this._request(
1891
- `/projects/${projectId}/pull-requests/${prId}`,
1892
- {
1893
- method: 'GET',
1894
- methodName: 'getPullRequest'
1895
- }
1896
- )
1897
- if (response.success) {
1898
- return response.data
1899
- }
1900
- throw new Error(response.message)
1901
- } catch (error) {
1902
- throw new Error(`Failed to get pull request: ${error.message}`)
1903
- }
1904
- }
1905
-
1906
- /**
1907
- * Submit a review for a pull request
1908
- */
1909
- async reviewPullRequest (projectId, prId, reviewData) {
1910
- this._requireReady('reviewPullRequest')
1911
- if (!projectId) {
1912
- throw new Error('Project ID is required')
1913
- }
1914
- if (!prId) {
1915
- throw new Error('Pull request ID is required')
1916
- }
1917
-
1918
- const validStatuses = ['approved', 'requested_changes', 'feedback']
1919
- if (reviewData.status && !validStatuses.includes(reviewData.status)) {
1920
- throw new Error(
1921
- `Invalid review status. Must be one of: ${validStatuses.join(', ')}`
1922
- )
1923
- }
1924
-
1925
- try {
1926
- const response = await this._request(
1927
- `/projects/${projectId}/pull-requests/${prId}/review`,
1928
- {
1929
- method: 'POST',
1930
- body: JSON.stringify(reviewData),
1931
- methodName: 'reviewPullRequest'
1932
- }
1933
- )
1934
- if (response.success) {
1935
- return response.data
1936
- }
1937
- throw new Error(response.message)
1938
- } catch (error) {
1939
- throw new Error(`Failed to review pull request: ${error.message}`)
1940
- }
1941
- }
1942
-
1943
- /**
1944
- * Add a comment to an existing review thread
1945
- */
1946
- async addPullRequestComment (projectId, prId, commentData) {
1947
- this._requireReady('addPullRequestComment')
1948
- if (!projectId) {
1949
- throw new Error('Project ID is required')
1950
- }
1951
- if (!prId) {
1952
- throw new Error('Pull request ID is required')
1953
- }
1954
- if (!commentData.value) {
1955
- throw new Error('Comment value is required')
1956
- }
1957
-
1958
- try {
1959
- const response = await this._request(
1960
- `/projects/${projectId}/pull-requests/${prId}/comment`,
1961
- {
1962
- method: 'POST',
1963
- body: JSON.stringify(commentData),
1964
- methodName: 'addPullRequestComment'
1965
- }
1966
- )
1967
- if (response.success) {
1968
- return response.data
1969
- }
1970
- throw new Error(response.message)
1971
- } catch (error) {
1972
- throw new Error(`Failed to add pull request comment: ${error.message}`)
1973
- }
1974
- }
1975
-
1976
- /**
1977
- * Merge an approved pull request
1978
- */
1979
- async mergePullRequest (projectId, prId) {
1980
- this._requireReady('mergePullRequest')
1981
- if (!projectId) {
1982
- throw new Error('Project ID is required')
1983
- }
1984
- if (!prId) {
1985
- throw new Error('Pull request ID is required')
1986
- }
1987
-
1988
- try {
1989
- const response = await this._request(
1990
- `/projects/${projectId}/pull-requests/${prId}/merge`,
1991
- {
1992
- method: 'POST',
1993
- methodName: 'mergePullRequest'
1994
- }
1995
- )
1996
-
1997
- if (response.success) {
1998
- return response.data
1999
- }
2000
- throw new Error(response.message)
2001
- } catch (error) {
2002
- // Handle specific merge conflict errors
2003
- if (
2004
- error.message.includes('conflicts') ||
2005
- error.message.includes('409')
2006
- ) {
2007
- throw new Error(`Pull request has merge conflicts: ${error.message}`)
2008
- }
2009
- throw new Error(`Failed to merge pull request: ${error.message}`)
2010
- }
2011
- }
2012
-
2013
- /**
2014
- * Get the diff/changes for a pull request
2015
- */
2016
- async getPullRequestDiff (projectId, prId) {
2017
- this._requireReady('getPullRequestDiff')
2018
- if (!projectId) {
2019
- throw new Error('Project ID is required')
2020
- }
2021
- if (!prId) {
2022
- throw new Error('Pull request ID is required')
2023
- }
2024
-
2025
- try {
2026
- const response = await this._request(
2027
- `/projects/${projectId}/pull-requests/${prId}/diff`,
2028
- {
2029
- method: 'GET',
2030
- methodName: 'getPullRequestDiff'
2031
- }
2032
- )
2033
- if (response.success) {
2034
- return response.data
2035
- }
2036
- throw new Error(response.message)
2037
- } catch (error) {
2038
- throw new Error(`Failed to get pull request diff: ${error.message}`)
2039
- }
2040
- }
2041
-
2042
- /**
2043
- * Helper method to create a pull request with validation
2044
- */
2045
- async createPullRequestWithValidation (projectId, data) {
2046
- const { source, target, title, description, changes } = data
2047
-
2048
- // Basic validation
2049
- if (source === target) {
2050
- throw new Error('Source and target branches cannot be the same')
2051
- }
2052
-
2053
- if (!title || title.trim().length === 0) {
2054
- throw new Error('Pull request title cannot be empty')
2055
- }
2056
-
2057
- if (title.length > 200) {
2058
- throw new Error('Pull request title cannot exceed 200 characters')
2059
- }
2060
-
2061
- const pullRequestData = {
2062
- source: source.trim(),
2063
- target: target.trim(),
2064
- title: title.trim(),
2065
- ...(description && { description: description.trim() }),
2066
- ...(changes && { changes })
2067
- }
2068
-
2069
- return await this.createPullRequest(projectId, pullRequestData)
2070
- }
2071
-
2072
- /**
2073
- * Helper method to approve a pull request
2074
- */
2075
- async approvePullRequest (projectId, prId, comment = '') {
2076
- const reviewData = {
2077
- status: 'approved',
2078
- ...(comment && {
2079
- threads: [
2080
- {
2081
- comment,
2082
- type: 'praise'
2083
- }
2084
- ]
2085
- })
2086
- }
2087
-
2088
- return await this.reviewPullRequest(projectId, prId, reviewData)
2089
- }
2090
-
2091
- /**
2092
- * Helper method to request changes on a pull request
2093
- */
2094
- async requestPullRequestChanges (projectId, prId, threads = []) {
2095
- if (!threads || threads.length === 0) {
2096
- throw new Error('Must provide specific feedback when requesting changes')
2097
- }
2098
-
2099
- const reviewData = {
2100
- status: 'requested_changes',
2101
- threads
2102
- }
2103
-
2104
- return await this.reviewPullRequest(projectId, prId, reviewData)
2105
- }
2106
-
2107
- /**
2108
- * Helper method to get pull requests by status
2109
- */
2110
- async getOpenPullRequests (projectId, options = {}) {
2111
- return await this.listPullRequests(projectId, {
2112
- ...options,
2113
- status: 'open'
2114
- })
2115
- }
2116
-
2117
- async getClosedPullRequests (projectId, options = {}) {
2118
- return await this.listPullRequests(projectId, {
2119
- ...options,
2120
- status: 'closed'
2121
- })
2122
- }
2123
-
2124
- async getMergedPullRequests (projectId, options = {}) {
2125
- return await this.listPullRequests(projectId, {
2126
- ...options,
2127
- status: 'merged'
2128
- })
2129
- }
2130
-
2131
- /**
2132
- * Helper method to check if a pull request is canMerge
2133
- */
2134
- async isPullRequestMergeable (projectId, prId) {
2135
- try {
2136
- const prData = await this.getPullRequest(projectId, prId)
2137
- return prData?.data?.canMerge || false
2138
- } catch (error) {
2139
- throw new Error(
2140
- `Failed to check pull request mergeability: ${error.message}`
2141
- )
2142
- }
2143
- }
2144
-
2145
- /**
2146
- * Helper method to get pull request status summary
2147
- */
2148
- async getPullRequestStatusSummary (projectId, prId) {
2149
- try {
2150
- const prData = await this.getPullRequest(projectId, prId)
2151
- const pr = prData?.data
2152
-
2153
- if (!pr) {
2154
- throw new Error('Pull request not found')
2155
- }
2156
-
2157
- return {
2158
- status: pr.status,
2159
- reviewStatus: pr.reviewStatus,
2160
- canMerge: pr.canMerge,
2161
- hasConflicts: !pr.canMerge,
2162
- reviewCount: pr.reviews?.length || 0,
2163
- approvedReviews:
2164
- pr.reviews?.filter(r => r.status === 'approved').length || 0,
2165
- changesRequested:
2166
- pr.reviews?.filter(r => r.status === 'requested_changes').length || 0
2167
- }
2168
- } catch (error) {
2169
- throw new Error(
2170
- `Failed to get pull request status summary: ${error.message}`
2171
- )
2172
- }
2173
- }
2174
-
2175
- // ==================== BRANCH MANAGEMENT METHODS ====================
2176
-
2177
- /**
2178
- * Get all branches for a project
2179
- */
2180
- async listBranches (projectId) {
2181
- this._requireReady('listBranches')
2182
- if (!projectId) {
2183
- throw new Error('Project ID is required')
2184
- }
2185
-
2186
- try {
2187
- const response = await this._request(`/projects/${projectId}/branches`, {
2188
- method: 'GET',
2189
- methodName: 'listBranches'
2190
- })
2191
- if (response.success) {
2192
- return response.data
2193
- }
2194
- throw new Error(response.message)
2195
- } catch (error) {
2196
- throw new Error(`Failed to list branches: ${error.message}`)
2197
- }
2198
- }
2199
-
2200
- /**
2201
- * Create a new branch from an existing branch
2202
- */
2203
- async createBranch (projectId, branchData) {
2204
- this._requireReady('createBranch')
2205
- if (!projectId) {
2206
- throw new Error('Project ID is required')
2207
- }
2208
- if (!branchData.name) {
2209
- throw new Error('Branch name is required')
2210
- }
2211
-
2212
- const { name, source = 'main' } = branchData
2213
-
2214
- try {
2215
- const response = await this._request(`/projects/${projectId}/branches`, {
2216
- method: 'POST',
2217
- body: JSON.stringify({ name, source }),
2218
- methodName: 'createBranch'
2219
- })
2220
- if (response.success) {
2221
- return response.data
2222
- }
2223
- throw new Error(response.message)
2224
- } catch (error) {
2225
- throw new Error(`Failed to create branch: ${error.message}`)
2226
- }
2227
- }
2228
-
2229
- /**
2230
- * Delete a branch (cannot delete main branch)
2231
- */
2232
- async deleteBranch (projectId, branchName) {
2233
- this._requireReady('deleteBranch')
2234
- if (!projectId) {
2235
- throw new Error('Project ID is required')
2236
- }
2237
- if (!branchName) {
2238
- throw new Error('Branch name is required')
2239
- }
2240
- if (branchName === 'main') {
2241
- throw new Error('Cannot delete main branch')
2242
- }
2243
-
2244
- try {
2245
- const response = await this._request(
2246
- `/projects/${projectId}/branches/${encodeURIComponent(branchName)}`,
2247
- {
2248
- method: 'DELETE',
2249
- methodName: 'deleteBranch'
2250
- }
2251
- )
2252
- if (response.success) {
2253
- return response
2254
- }
2255
- throw new Error(response.message)
2256
- } catch (error) {
2257
- throw new Error(`Failed to delete branch: ${error.message}`)
2258
- }
2259
- }
2260
-
2261
- /**
2262
- * Rename a branch (cannot rename main branch)
2263
- */
2264
- async renameBranch (projectId, branchName, newName) {
2265
- this._requireReady('renameBranch')
2266
- if (!projectId) {
2267
- throw new Error('Project ID is required')
2268
- }
2269
- if (!branchName) {
2270
- throw new Error('Current branch name is required')
2271
- }
2272
- if (!newName) {
2273
- throw new Error('New branch name is required')
2274
- }
2275
- if (branchName === 'main') {
2276
- throw new Error('Cannot rename main branch')
2277
- }
2278
-
2279
- try {
2280
- const response = await this._request(
2281
- `/projects/${projectId}/branches/${encodeURIComponent(
2282
- branchName
2283
- )}/rename`,
2284
- {
2285
- method: 'POST',
2286
- body: JSON.stringify({ newName }),
2287
- methodName: 'renameBranch'
2288
- }
2289
- )
2290
- if (response.success) {
2291
- return response
2292
- }
2293
- throw new Error(response.message)
2294
- } catch (error) {
2295
- throw new Error(`Failed to rename branch: ${error.message}`)
2296
- }
2297
- }
2298
-
2299
- /**
2300
- * Get changes/diff for a branch compared to another version
2301
- */
2302
- async getBranchChanges (projectId, branchName = 'main', options = {}) {
2303
- this._requireReady('getBranchChanges')
2304
- if (!projectId) {
2305
- throw new Error('Project ID is required')
2306
- }
2307
- if (!branchName) {
2308
- throw new Error('Branch name is required')
2309
- }
2310
-
2311
- const { versionId, versionValue, target } = options
2312
- const queryParams = new URLSearchParams()
2313
-
2314
- if (versionId) {
2315
- queryParams.append('versionId', versionId)
2316
- }
2317
- if (versionValue) {
2318
- queryParams.append('versionValue', versionValue)
2319
- }
2320
- if (target) {
2321
- queryParams.append('target', target)
2322
- }
2323
-
2324
- const queryString = queryParams.toString()
2325
- const url = `/projects/${projectId}/branches/${encodeURIComponent(
2326
- branchName
2327
- )}/changes${queryString ? `?${queryString}` : ''}`
2328
-
2329
- try {
2330
- const response = await this._request(url, {
2331
- method: 'GET',
2332
- methodName: 'getBranchChanges'
2333
- })
2334
- if (response.success) {
2335
- return response.data
2336
- }
2337
- throw new Error(response.message)
2338
- } catch (error) {
2339
- throw new Error(`Failed to get branch changes: ${error.message}`)
2340
- }
2341
- }
2342
-
2343
- /**
2344
- * Merge changes between branches (preview or commit)
2345
- */
2346
- async mergeBranch (projectId, branchName, mergeData = {}) {
2347
- this._requireReady('mergeBranch')
2348
- if (!projectId) {
2349
- throw new Error('Project ID is required')
2350
- }
2351
- if (!branchName) {
2352
- throw new Error('Source branch name is required')
2353
- }
2354
-
2355
- const {
2356
- target = 'main',
2357
- message,
2358
- type = 'patch',
2359
- commit = false,
2360
- changes
2361
- } = mergeData
2362
-
2363
- const requestBody = {
2364
- target,
2365
- type,
2366
- commit,
2367
- ...(message && { message }),
2368
- ...(changes && { changes })
2369
- }
2370
-
2371
- try {
2372
- const response = await this._request(
2373
- `/projects/${projectId}/branches/${encodeURIComponent(
2374
- branchName
2375
- )}/merge`,
2376
- {
2377
- method: 'POST',
2378
- body: JSON.stringify(requestBody),
2379
- methodName: 'mergeBranch'
2380
- }
2381
- )
2382
- if (response.success) {
2383
- return response.data
2384
- }
2385
- throw new Error(response.message)
2386
- } catch (error) {
2387
- // Handle merge conflicts specifically
2388
- if (
2389
- error.message.includes('conflicts') ||
2390
- error.message.includes('409')
2391
- ) {
2392
- throw new Error(`Merge conflicts detected: ${error.message}`)
2393
- }
2394
- throw new Error(`Failed to merge branch: ${error.message}`)
2395
- }
2396
- }
2397
-
2398
- /**
2399
- * Reset a branch to a clean state
2400
- */
2401
- async resetBranch (projectId, branchName) {
2402
- this._requireReady('resetBranch')
2403
- if (!projectId) {
2404
- throw new Error('Project ID is required')
2405
- }
2406
- if (!branchName) {
2407
- throw new Error('Branch name is required')
2408
- }
2409
-
2410
- try {
2411
- const response = await this._request(
2412
- `/projects/${projectId}/branches/${encodeURIComponent(
2413
- branchName
2414
- )}/reset`,
2415
- {
2416
- method: 'POST',
2417
- methodName: 'resetBranch'
2418
- }
2419
- )
2420
- if (response.success) {
2421
- return response.data
2422
- }
2423
- throw new Error(response.message)
2424
- } catch (error) {
2425
- throw new Error(`Failed to reset branch: ${error.message}`)
2426
- }
2427
- }
2428
-
2429
- /**
2430
- * Publish a specific version as the live version
2431
- */
2432
- async publishVersion (projectId, publishData) {
2433
- this._requireReady('publishVersion')
2434
- if (!projectId) {
2435
- throw new Error('Project ID is required')
2436
- }
2437
- if (!publishData.version) {
2438
- throw new Error('Version is required')
2439
- }
2440
-
2441
- const { version, branch = 'main' } = publishData
2442
-
2443
- try {
2444
- const response = await this._request(`/projects/${projectId}/publish`, {
2445
- method: 'POST',
2446
- body: JSON.stringify({ version, branch }),
2447
- methodName: 'publishVersion'
2448
- })
2449
- if (response.success) {
2450
- return response.data
2451
- }
2452
- throw new Error(response.message)
2453
- } catch (error) {
2454
- throw new Error(`Failed to publish version: ${error.message}`)
2455
- }
2456
- }
2457
-
2458
- // ==================== BRANCH HELPER METHODS ====================
2459
-
2460
- /**
2461
- * Helper method to create a branch with validation
2462
- */
2463
- async createBranchWithValidation (projectId, name, source = 'main') {
2464
- // Basic validation
2465
- if (!name || name.trim().length === 0) {
2466
- throw new Error('Branch name cannot be empty')
2467
- }
2468
-
2469
- if (name.includes(' ')) {
2470
- throw new Error('Branch name cannot contain spaces')
2471
- }
2472
-
2473
- if (name === 'main') {
2474
- throw new Error('Cannot create a branch named "main"')
2475
- }
2476
-
2477
- const sanitizedName = name
2478
- .trim()
2479
- .toLowerCase()
2480
- .replace(/[^a-z0-9-_]/gu, '-')
2481
-
2482
- return await this.createBranch(projectId, {
2483
- name: sanitizedName,
2484
- source
2485
- })
2486
- }
2487
-
2488
- /**
2489
- * Helper method to check if a branch exists
2490
- */
2491
- async branchExists (projectId, branchName) {
2492
- try {
2493
- const branches = await this.listBranches(projectId)
2494
- return branches?.data?.includes(branchName) || false
2495
- } catch (error) {
2496
- throw new Error(`Failed to check if branch exists: ${error.message}`)
2497
- }
2498
- }
2499
-
2500
- /**
2501
- * Helper method to preview merge without committing
2502
- */
2503
- async previewMerge (projectId, sourceBranch, targetBranch = 'main') {
2504
- return await this.mergeBranch(projectId, sourceBranch, {
2505
- target: targetBranch,
2506
- commit: false
2507
- })
2508
- }
2509
-
2510
- /**
2511
- * Helper method to commit merge after preview
2512
- */
2513
- async commitMerge (projectId, sourceBranch, options = {}) {
2514
- const {
2515
- target = 'main',
2516
- message = `Merge ${sourceBranch} into ${target}`,
2517
- type = 'patch',
2518
- changes
2519
- } = options
2520
-
2521
- return await this.mergeBranch(projectId, sourceBranch, {
2522
- target,
2523
- message,
2524
- type,
2525
- commit: true,
2526
- changes
2527
- })
2528
- }
2529
-
2530
- /**
2531
- * Helper method to create a feature branch from main
2532
- */
2533
- async createFeatureBranch (projectId, featureName) {
2534
- const branchName = `feature/${featureName
2535
- .toLowerCase()
2536
- .replace(/[^a-z0-9-]/gu, '-')}`
2537
-
2538
- return await this.createBranch(projectId, {
2539
- name: branchName,
2540
- source: 'main'
2541
- })
2542
- }
2543
-
2544
- /**
2545
- * Helper method to create a hotfix branch from main
2546
- */
2547
- async createHotfixBranch (projectId, hotfixName) {
2548
- const branchName = `hotfix/${hotfixName
2549
- .toLowerCase()
2550
- .replace(/[^a-z0-9-]/gu, '-')}`
2551
-
2552
- return await this.createBranch(projectId, {
2553
- name: branchName,
2554
- source: 'main'
2555
- })
2556
- }
2557
-
2558
- /**
2559
- * Helper method to get branch status summary
2560
- */
2561
- async getBranchStatus (projectId, branchName) {
2562
- try {
2563
- const [branches, changes] = await Promise.all([
2564
- this.listBranches(projectId),
2565
- this.getBranchChanges(projectId, branchName).catch(() => null)
2566
- ])
2567
-
2568
- const exists = branches?.data?.includes(branchName) || false
2569
- const hasChanges = changes?.data?.length > 0
2570
-
2571
- return {
2572
- exists,
2573
- hasChanges,
2574
- changeCount: changes?.data?.length || 0,
2575
- canDelete: exists && branchName !== 'main',
2576
- canRename: exists && branchName !== 'main'
2577
- }
2578
- } catch (error) {
2579
- throw new Error(`Failed to get branch status: ${error.message}`)
2580
- }
2581
- }
2582
-
2583
- /**
2584
- * Helper method to safely delete a branch with confirmation
2585
- */
2586
- async deleteBranchSafely (projectId, branchName, options = {}) {
2587
- const { force = false } = options
2588
-
2589
- if (!force) {
2590
- const status = await this.getBranchStatus(projectId, branchName)
2591
-
2592
- if (!status.exists) {
2593
- throw new Error(`Branch '${branchName}' does not exist`)
2594
- }
2595
-
2596
- if (!status.canDelete) {
2597
- throw new Error(`Branch '${branchName}' cannot be deleted`)
2598
- }
2599
-
2600
- if (status.hasChanges) {
2601
- throw new Error(
2602
- `Branch '${branchName}' has uncommitted changes. Use force option to delete anyway.`
2603
- )
2604
- }
2605
- }
2606
-
2607
- return await this.deleteBranch(projectId, branchName)
2608
- }
2609
-
2610
- // ==================== ADMIN METHODS ====================
2611
-
2612
- /**
2613
- * Get admin users list with comprehensive filtering and search capabilities
2614
- * Requires admin or super_admin global role
2615
- */
2616
- async getAdminUsers (params = {}) {
2617
- this._requireReady('getAdminUsers')
2618
-
2619
- const {
2620
- emails,
2621
- ids,
2622
- query,
2623
- status,
2624
- page = 1,
2625
- limit = 50,
2626
- sort = { field: 'createdAt', order: 'desc' }
2627
- } = params
2628
-
2629
- const queryParams = new URLSearchParams()
2630
-
2631
- // Add query parameters
2632
- if (emails) {
2633
- queryParams.append('emails', emails)
2634
- }
2635
- if (ids) {
2636
- queryParams.append('ids', ids)
2637
- }
2638
- if (query) {
2639
- queryParams.append('query', query)
2640
- }
2641
- if (status) {
2642
- queryParams.append('status', status)
2643
- }
2644
- if (page) {
2645
- queryParams.append('page', page.toString())
2646
- }
2647
- if (limit) {
2648
- queryParams.append('limit', limit.toString())
2649
- }
2650
- if (sort && sort.field) {
2651
- queryParams.append('sort[field]', sort.field)
2652
- queryParams.append('sort[order]', sort.order || 'desc')
2653
- }
2654
-
2655
- const queryString = queryParams.toString()
2656
- const url = `/users/admin/users${queryString ? `?${queryString}` : ''}`
2657
-
2658
- try {
2659
- const response = await this._request(url, {
2660
- method: 'GET',
2661
- methodName: 'getAdminUsers'
2662
- })
2663
- if (response.success) {
2664
- return response.data
2665
- }
2666
- throw new Error(response.message)
2667
- } catch (error) {
2668
- throw new Error(`Failed to get admin users: ${error.message}`)
2669
- }
2670
- }
2671
-
2672
- /**
2673
- * Assign projects to a specific user
2674
- * Requires admin or super_admin global role
2675
- */
2676
- async assignProjectsToUser (userId, options = {}) {
2677
- this._requireReady('assignProjectsToUser')
2678
-
2679
- if (!userId) {
2680
- throw new Error('User ID is required')
2681
- }
2682
-
2683
- const { projectIds, role = 'guest' } = options
2684
-
2685
- const requestBody = {
2686
- userId,
2687
- role
2688
- }
2689
-
2690
- // Only include projectIds if provided (otherwise assigns all projects)
2691
- if (projectIds && Array.isArray(projectIds)) {
2692
- requestBody.projectIds = projectIds
2693
- }
2694
-
2695
- try {
2696
- const response = await this._request('/assign-projects', {
2697
- method: 'POST',
2698
- body: JSON.stringify(requestBody),
2699
- methodName: 'assignProjectsToUser'
2700
- })
2701
- if (response.success) {
2702
- return response.data
2703
- }
2704
- throw new Error(response.message)
2705
- } catch (error) {
2706
- throw new Error(`Failed to assign projects to user: ${error.message}`)
2707
- }
2708
- }
2709
-
2710
- /**
2711
- * Helper method for admin users search
2712
- */
2713
- async searchAdminUsers (searchQuery, options = {}) {
2714
- return await this.getAdminUsers({
2715
- query: searchQuery,
2716
- ...options
2717
- })
2718
- }
2719
-
2720
- /**
2721
- * Helper method to get admin users by email list
2722
- */
2723
- async getAdminUsersByEmails (emails, options = {}) {
2724
- const emailList = Array.isArray(emails) ? emails.join(',') : emails
2725
- return await this.getAdminUsers({
2726
- emails: emailList,
2727
- ...options
2728
- })
2729
- }
2730
-
2731
- /**
2732
- * Helper method to get admin users by ID list
2733
- */
2734
- async getAdminUsersByIds (ids, options = {}) {
2735
- const idList = Array.isArray(ids) ? ids.join(',') : ids
2736
- return await this.getAdminUsers({
2737
- ids: idList,
2738
- ...options
2739
- })
2740
- }
2741
-
2742
- /**
2743
- * Helper method to assign specific projects to a user with a specific role
2744
- */
2745
- async assignSpecificProjectsToUser (userId, projectIds, role = 'guest') {
2746
- if (!Array.isArray(projectIds) || projectIds.length === 0) {
2747
- throw new Error('Project IDs must be a non-empty array')
2748
- }
2749
-
2750
- return await this.assignProjectsToUser(userId, {
2751
- projectIds,
2752
- role
2753
- })
2754
- }
2755
-
2756
- /**
2757
- * Helper method to assign all projects to a user with a specific role
2758
- */
2759
- async assignAllProjectsToUser (userId, role = 'guest') {
2760
- return await this.assignProjectsToUser(userId, {
2761
- role
2762
- })
2763
- }
2764
-
2765
- async updateUser (userId, userData) {
2766
- this._requireReady('updateUser')
2767
- if (!userId) {
2768
- throw new Error('User ID is required')
2769
- }
2770
- if (
2771
- !userData ||
2772
- typeof userData !== 'object' ||
2773
- Object.keys(userData).length === 0
2774
- ) {
2775
- throw new Error('userData must be a non-empty object')
2776
- }
2777
-
2778
- try {
2779
- const response = await this._request(`/users/${userId}`, {
2780
- method: 'PATCH',
2781
- body: JSON.stringify(userData),
2782
- methodName: 'updateUser'
2783
- })
2784
- if (response.success) {
2785
- return response.data
2786
- }
2787
- throw new Error(response.message)
2788
- } catch (error) {
2789
- // Surface duplicate username conflict nicely
2790
- if (error.message?.includes('Duplicate')) {
2791
- throw new Error('Username already exists')
2792
- }
2793
- throw new Error(`Failed to update user: ${error.message}`)
2794
- }
2795
- }
2796
-
2797
- // Cleanup
2798
- destroy () {
2799
- if (this._tokenManager) {
2800
- this._tokenManager.destroy()
2801
- this._tokenManager = null
2802
- }
2803
- this._client = null
2804
- this._initialized = false
2805
- this._setReady(false)
2806
- }
2807
-
2808
- // ==================== FAVORITE PROJECT METHODS ====================
2809
-
2810
- async getFavoriteProjects () {
2811
- this._requireReady('getFavoriteProjects')
2812
- try {
2813
- const response = await this._request('/users/favorites', {
2814
- method: 'GET',
2815
- methodName: 'getFavoriteProjects'
2816
- })
2817
-
2818
- if (response.success) {
2819
- // Ensure each project has proper icon src like other project lists
2820
- return (response.data || []).map(project => ({
2821
- isFavorite: true,
2822
- ...project,
2823
- ...(project.icon && {
2824
- icon: {
2825
- src: `${this._apiUrl}/core/files/public/${project.icon.id}/download`,
2826
- ...project.icon
2827
- }
2828
- })
2829
- }))
2830
- }
2831
-
2832
- throw new Error(response.message)
2833
- } catch (error) {
2834
- throw new Error(`Failed to get favorite projects: ${error.message}`)
2835
- }
2836
- }
2837
-
2838
- async addFavoriteProject (projectId) {
2839
- this._requireReady('addFavoriteProject')
2840
- if (!projectId) {
2841
- throw new Error('Project ID is required')
2842
- }
2843
- try {
2844
- const response = await this._request(`/users/favorites/${projectId}`, {
2845
- method: 'POST',
2846
- methodName: 'addFavoriteProject'
2847
- })
2848
-
2849
- if (response.success) {
2850
- return response.data
2851
- }
2852
-
2853
- throw new Error(response.message)
2854
- } catch (error) {
2855
- throw new Error(`Failed to add favorite project: ${error.message}`)
2856
- }
2857
- }
2858
-
2859
- async removeFavoriteProject (projectId) {
2860
- this._requireReady('removeFavoriteProject')
2861
- if (!projectId) {
2862
- throw new Error('Project ID is required')
2863
- }
2864
- try {
2865
- const response = await this._request(`/users/favorites/${projectId}`, {
2866
- method: 'DELETE',
2867
- methodName: 'removeFavoriteProject'
2868
- })
2869
-
2870
- if (response.success) {
2871
- return response.message || 'Project removed from favorites'
2872
- }
2873
-
2874
- throw new Error(response.message)
2875
- } catch (error) {
2876
- throw new Error(`Failed to remove favorite project: ${error.message}`)
2877
- }
2878
- }
2879
-
2880
- // ==================== RECENT PROJECT METHODS ====================
2881
-
2882
- async getRecentProjects (options = {}) {
2883
- this._requireReady('getRecentProjects')
2884
-
2885
- const { limit = 20 } = options
2886
- const queryString = new URLSearchParams({
2887
- limit: limit.toString()
2888
- }).toString()
2889
- const url = `/users/projects/recent${queryString ? `?${queryString}` : ''}`
2890
-
2891
- try {
2892
- const response = await this._request(url, {
2893
- method: 'GET',
2894
- methodName: 'getRecentProjects'
2895
- })
2896
-
2897
- if (response.success) {
2898
- // Map icon src similar to other project lists
2899
- return (response.data || []).map(item => ({
2900
- ...item.project,
2901
- ...(item.project &&
2902
- item.project.icon && {
2903
- icon: {
2904
- src: `${this._apiUrl}/core/files/public/${item.project.icon.id}/download`,
2905
- ...item.project.icon
2906
- }
2907
- })
2908
- }))
2909
- }
2910
-
2911
- throw new Error(response.message)
2912
- } catch (error) {
2913
- throw new Error(`Failed to get recent projects: ${error.message}`)
2914
- }
2915
- }
2916
-
2917
- // ==================== PLAN METHODS ====================
2918
-
2919
- /**
2920
- * Get list of public plans (no authentication required)
2921
- */
2922
- async getPlans () {
2923
- try {
2924
- const response = await this._request('/plans', {
2925
- method: 'GET',
2926
- methodName: 'getPlans'
2927
- })
2928
- if (response.success) {
2929
- return response.data
2930
- }
2931
- throw new Error(response.message)
2932
- } catch (error) {
2933
- throw new Error(`Failed to get plans: ${error.message}`)
2934
- }
2935
- }
2936
-
2937
- /**
2938
- * Get a specific plan by ID (no authentication required)
2939
- */
2940
- async getPlan (planId) {
2941
- if (!planId) {
2942
- throw new Error('Plan ID is required')
2943
- }
2944
- try {
2945
- const response = await this._request(`/plans/${planId}`, {
2946
- method: 'GET',
2947
- methodName: 'getPlan'
2948
- })
2949
- if (response.success) {
2950
- return response.data
2951
- }
2952
- throw new Error(response.message)
2953
- } catch (error) {
2954
- throw new Error(`Failed to get plan: ${error.message}`)
2955
- }
2956
- }
2957
-
2958
- // ==================== ADMIN PLAN METHODS ====================
2959
-
2960
- /**
2961
- * Get all plans including inactive ones (admin only)
2962
- */
2963
- async getAdminPlans () {
2964
- this._requireReady('getAdminPlans')
2965
- try {
2966
- const response = await this._request('/admin/plans', {
2967
- method: 'GET',
2968
- methodName: 'getAdminPlans'
2969
- })
2970
- if (response.success) {
2971
- return response.data
2972
- }
2973
- throw new Error(response.message)
2974
- } catch (error) {
2975
- throw new Error(`Failed to get admin plans: ${error.message}`)
2976
- }
2977
- }
2978
-
2979
- /**
2980
- * Create a new plan (admin only)
2981
- */
2982
- async createPlan (planData) {
2983
- this._requireReady('createPlan')
2984
- if (!planData || typeof planData !== 'object') {
2985
- throw new Error('Plan data is required')
2986
- }
2987
- try {
2988
- const response = await this._request('/admin/plans', {
2989
- method: 'POST',
2990
- body: JSON.stringify(planData),
2991
- methodName: 'createPlan'
2992
- })
2993
- if (response.success) {
2994
- return response.data
2995
- }
2996
- throw new Error(response.message)
2997
- } catch (error) {
2998
- throw new Error(`Failed to create plan: ${error.message}`)
2999
- }
3000
- }
3001
-
3002
- /**
3003
- * Update an existing plan (admin only)
3004
- */
3005
- async updatePlan (planId, planData) {
3006
- this._requireReady('updatePlan')
3007
- if (!planId) {
3008
- throw new Error('Plan ID is required')
3009
- }
3010
- if (!planData || typeof planData !== 'object') {
3011
- throw new Error('Plan data is required')
3012
- }
3013
- try {
3014
- const response = await this._request(`/admin/plans/${planId}`, {
3015
- method: 'PATCH',
3016
- body: JSON.stringify(planData),
3017
- methodName: 'updatePlan'
3018
- })
3019
- if (response.success) {
3020
- return response.data
3021
- }
3022
- throw new Error(response.message)
3023
- } catch (error) {
3024
- throw new Error(`Failed to update plan: ${error.message}`)
3025
- }
3026
- }
3027
-
3028
- /**
3029
- * Delete a plan (soft delete + archive Stripe product) (admin only)
3030
- */
3031
- async deletePlan (planId) {
3032
- this._requireReady('deletePlan')
3033
- if (!planId) {
3034
- throw new Error('Plan ID is required')
3035
- }
3036
- try {
3037
- const response = await this._request(`/admin/plans/${planId}`, {
3038
- method: 'DELETE',
3039
- methodName: 'deletePlan'
3040
- })
3041
- if (response.success) {
3042
- return response.data
3043
- }
3044
- throw new Error(response.message)
3045
- } catch (error) {
3046
- throw new Error(`Failed to delete plan: ${error.message}`)
3047
- }
3048
- }
3049
-
3050
- /**
3051
- * Initialize default plans (admin only)
3052
- */
3053
- async initializePlans () {
3054
- this._requireReady('initializePlans')
3055
- try {
3056
- const response = await this._request('/admin/plans/initialize', {
3057
- method: 'POST',
3058
- methodName: 'initializePlans'
3059
- })
3060
- if (response.success) {
3061
- return response
3062
- }
3063
- throw new Error(response.message)
3064
- } catch (error) {
3065
- throw new Error(`Failed to initialize plans: ${error.message}`)
3066
- }
3067
- }
3068
-
3069
- // ==================== PLAN HELPER METHODS ====================
3070
-
3071
- /**
3072
- * Helper method to get plans with validation
3073
- */
3074
- async getPlansWithValidation () {
3075
- try {
3076
- const plans = await this.getPlans()
3077
- if (!Array.isArray(plans)) {
3078
- throw new Error('Invalid response format: plans should be an array')
3079
- }
3080
- return plans
3081
- } catch (error) {
3082
- throw new Error(`Failed to get plans with validation: ${error.message}`)
3083
- }
3084
- }
3085
-
3086
- /**
3087
- * Helper method to get a plan by ID with validation
3088
- */
3089
- async getPlanWithValidation (planId) {
3090
- if (!planId || typeof planId !== 'string') {
3091
- throw new Error('Plan ID must be a valid string')
3092
- }
3093
-
3094
- try {
3095
- const plan = await this.getPlan(planId)
3096
- if (!plan || typeof plan !== 'object') {
3097
- throw new Error('Invalid plan data received')
3098
- }
3099
- return plan
3100
- } catch (error) {
3101
- throw new Error(`Failed to get plan with validation: ${error.message}`)
3102
- }
3103
- }
3104
-
3105
- /**
3106
- * Helper method to create a plan with validation (admin only)
3107
- */
3108
- async createPlanWithValidation (planData) {
3109
- if (!planData || typeof planData !== 'object') {
3110
- throw new Error('Plan data must be a valid object')
3111
- }
3112
-
3113
- // Basic validation for required fields
3114
- const requiredFields = ['name', 'key', 'price']
3115
- for (const field of requiredFields) {
3116
- if (!planData[field]) {
3117
- throw new Error(`Required field '${field}' is missing`)
3118
- }
3119
- }
3120
-
3121
- // Validate price is a positive number
3122
- if (typeof planData.price !== 'number' || planData.price < 0) {
3123
- throw new Error('Price must be a positive number')
3124
- }
3125
-
3126
- // Validate key format (alphanumeric and hyphens only)
3127
- if (!/^[a-z0-9-]+$/u.test(planData.key)) {
3128
- throw new Error('Plan key must contain only lowercase letters, numbers, and hyphens')
3129
- }
3130
-
3131
- return await this.createPlan(planData)
3132
- }
3133
-
3134
- /**
3135
- * Helper method to update a plan with validation (admin only)
3136
- */
3137
- async updatePlanWithValidation (planId, planData) {
3138
- if (!planId || typeof planId !== 'string') {
3139
- throw new Error('Plan ID must be a valid string')
3140
- }
3141
- if (!planData || typeof planData !== 'object') {
3142
- throw new Error('Plan data must be a valid object')
3143
- }
3144
-
3145
- // Validate price if provided
3146
- if (planData.price != null) {
3147
- if (typeof planData.price !== 'number' || planData.price < 0) {
3148
- throw new Error('Price must be a positive number')
3149
- }
3150
- }
3151
-
3152
- // Validate key format if provided
3153
- if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
3154
- throw new Error('Plan key must contain only lowercase letters, numbers, and hyphens')
3155
- }
3156
-
3157
- return await this.updatePlan(planId, planData)
3158
- }
3159
-
3160
- /**
3161
- * Helper method to get active plans only
3162
- */
3163
- async getActivePlans () {
3164
- try {
3165
- const plans = await this.getPlans()
3166
- return plans.filter(plan => plan.active !== false)
3167
- } catch (error) {
3168
- throw new Error(`Failed to get active plans: ${error.message}`)
3169
- }
3170
- }
3171
-
3172
- /**
3173
- * Helper method to get plans by price range
3174
- */
3175
- async getPlansByPriceRange (minPrice = 0, maxPrice = Infinity) {
3176
- try {
3177
- const plans = await this.getPlans()
3178
- return plans.filter(plan => {
3179
- const price = plan.price || 0
3180
- return price >= minPrice && price <= maxPrice
3181
- })
3182
- } catch (error) {
3183
- throw new Error(`Failed to get plans by price range: ${error.message}`)
3184
- }
3185
- }
3186
-
3187
- /**
3188
- * Helper method to find plan by key
3189
- */
3190
- async getPlanByKey (key) {
3191
- if (!key) {
3192
- throw new Error('Plan key is required')
3193
- }
3194
-
3195
- try {
3196
- const plans = await this.getPlans()
3197
- const plan = plans.find(p => p.key === key)
3198
-
3199
- if (!plan) {
3200
- throw new Error(`Plan with key '${key}' not found`)
3201
- }
3202
-
3203
- return plan
3204
- } catch (error) {
3205
- throw new Error(`Failed to get plan by key: ${error.message}`)
3206
- }
3207
- }
3208
- }