@symbo.ls/sdk 3.2.3 → 3.2.7

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