@symbo.ls/sdk 3.1.2 → 3.2.6

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 (187) hide show
  1. package/README.md +143 -2
  2. package/dist/cjs/config/environment.js +98 -30
  3. package/dist/cjs/index.js +144 -24
  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 +743 -0
  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 +64 -16
  23. package/dist/cjs/state/RootStateManager.js +65 -0
  24. package/dist/cjs/state/rootEventBus.js +74 -0
  25. package/dist/cjs/utils/CollabClient.js +223 -0
  26. package/dist/cjs/utils/TokenManager.js +78 -30
  27. package/dist/cjs/utils/changePreprocessor.js +199 -0
  28. package/dist/cjs/utils/jsonDiff.js +145 -0
  29. package/dist/cjs/utils/ordering.js +309 -0
  30. package/dist/cjs/utils/services.js +301 -103
  31. package/dist/cjs/utils/validation.js +0 -3
  32. package/dist/esm/config/environment.js +98 -30
  33. package/dist/esm/index.js +49505 -8718
  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 +26895 -0
  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 +49062 -8569
  53. package/dist/esm/state/RootStateManager.js +90 -0
  54. package/dist/esm/state/rootEventBus.js +56 -0
  55. package/dist/esm/utils/CollabClient.js +18889 -0
  56. package/dist/esm/utils/TokenManager.js +78 -30
  57. package/dist/esm/utils/changePreprocessor.js +542 -0
  58. package/dist/esm/utils/jsonDiff.js +7011 -0
  59. package/dist/esm/utils/ordering.js +291 -0
  60. package/dist/esm/utils/services.js +301 -103
  61. package/dist/esm/utils/validation.js +116 -50
  62. package/dist/node/config/environment.js +98 -30
  63. package/dist/node/index.js +175 -32
  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 +724 -0
  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 +64 -16
  83. package/dist/node/state/RootStateManager.js +36 -0
  84. package/dist/node/state/rootEventBus.js +55 -0
  85. package/dist/node/utils/CollabClient.js +194 -0
  86. package/dist/node/utils/TokenManager.js +78 -30
  87. package/dist/node/utils/changePreprocessor.js +180 -0
  88. package/dist/node/utils/jsonDiff.js +116 -0
  89. package/dist/node/utils/ordering.js +290 -0
  90. package/dist/node/utils/services.js +301 -103
  91. package/dist/node/utils/validation.js +0 -3
  92. package/package.json +39 -21
  93. package/src/config/environment.js +99 -28
  94. package/src/index.js +181 -36
  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 +900 -0
  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 +80 -13
  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 +76 -0
  148. package/src/state/rootEventBus.js +67 -0
  149. package/src/utils/CollabClient.js +248 -0
  150. package/src/utils/TokenManager.js +88 -33
  151. package/src/utils/changePreprocessor.js +239 -0
  152. package/src/utils/jsonDiff.js +144 -0
  153. package/src/utils/ordering.js +271 -0
  154. package/src/utils/services.js +326 -107
  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 -1751
  159. package/dist/cjs/services/SocketIOService.js +0 -307
  160. package/dist/cjs/services/SocketService.js +0 -161
  161. package/dist/cjs/services/SymstoryService.js +0 -571
  162. package/dist/cjs/utils/basedQuerys.js +0 -181
  163. package/dist/cjs/utils/symstoryClient.js +0 -259
  164. package/dist/esm/services/AIService.js +0 -185
  165. package/dist/esm/services/BasedService.js +0 -5278
  166. package/dist/esm/services/CoreService.js +0 -2264
  167. package/dist/esm/services/SocketIOService.js +0 -470
  168. package/dist/esm/services/SocketService.js +0 -191
  169. package/dist/esm/services/SymstoryService.js +0 -7041
  170. package/dist/esm/utils/basedQuerys.js +0 -163
  171. package/dist/esm/utils/symstoryClient.js +0 -370
  172. package/dist/node/services/AIService.js +0 -136
  173. package/dist/node/services/BasedService.js +0 -1156
  174. package/dist/node/services/CoreService.js +0 -1722
  175. package/dist/node/services/SocketIOService.js +0 -278
  176. package/dist/node/services/SocketService.js +0 -142
  177. package/dist/node/services/SymstoryService.js +0 -542
  178. package/dist/node/utils/basedQuerys.js +0 -162
  179. package/dist/node/utils/symstoryClient.js +0 -230
  180. package/src/services/AIService.js +0 -150
  181. package/src/services/BasedService.js +0 -1301
  182. package/src/services/CoreService.js +0 -1943
  183. package/src/services/SocketIOService.js +0 -334
  184. package/src/services/SocketService.js +0 -168
  185. package/src/services/SymstoryService.js +0 -649
  186. package/src/utils/basedQuerys.js +0 -164
  187. package/src/utils/symstoryClient.js +0 -252
@@ -1,1943 +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._baseUrl = 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._baseUrl = environment.apiUrl || environment.baseUrl
20
-
21
- if (!this._baseUrl) {
22
- throw new Error('Core service base URL not configured')
23
- }
24
-
25
- // Initialize token manager
26
- this._tokenManager = getTokenManager({
27
- apiUrl: this._baseUrl,
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 we have an authToken, set it in the token manager
42
- if (authToken) {
43
- this._tokenManager.setTokens({ access_token: authToken })
44
- }
45
-
46
- // Store masked configuration info
47
- this._info = {
48
- config: {
49
- baseUrl: this._baseUrl,
50
- appKey: appKey ? `${appKey.substr(0, 4)}...${appKey.substr(-4)}` : null,
51
- hasToken: Boolean(authToken)
52
- }
53
- }
54
-
55
- this._initialized = true
56
- this._setReady()
57
- } catch (error) {
58
- this._setError(error)
59
- throw error
60
- }
61
- }
62
-
63
- // Helper to check if method requires initialization
64
- _requiresInit (methodName) {
65
- const noInitMethods = new Set([
66
- 'register',
67
- 'login',
68
- 'googleAuth',
69
- 'githubAuth',
70
- 'requestPasswordReset',
71
- 'confirmPasswordReset',
72
- 'confirmRegistration',
73
- 'verifyEmail'
74
- ])
75
- return !noInitMethods.has(methodName)
76
- }
77
-
78
- // Override _requireReady to be more flexible
79
- _requireReady (methodName) {
80
- if (this._requiresInit(methodName) && !this._initialized) {
81
- throw new Error('Core service not initialized')
82
- }
83
- }
84
-
85
- // Debug method to check token status
86
- getTokenDebugInfo () {
87
- if (!this._tokenManager) {
88
- return {
89
- tokenManagerExists: false,
90
- error: 'TokenManager not initialized'
91
- }
92
- }
93
-
94
- const tokenStatus = this._tokenManager.getTokenStatus()
95
- const {tokens} = this._tokenManager
96
-
97
- return {
98
- tokenManagerExists: true,
99
- tokenStatus,
100
- hasAccessToken: Boolean(tokens.accessToken),
101
- hasRefreshToken: Boolean(tokens.refreshToken),
102
- accessTokenPreview: tokens.accessToken ? `${tokens.accessToken.substring(0, 20)}...` : null,
103
- expiresAt: tokens.expiresAt,
104
- timeToExpiry: tokenStatus.timeToExpiry,
105
- authHeader: this._tokenManager.getAuthHeader()
106
- }
107
- }
108
-
109
- // Helper method to make HTTP requests
110
- async _request (endpoint, options = {}) {
111
- const url = `${this._baseUrl}/core${endpoint}`
112
-
113
- const defaultHeaders = {
114
- 'Content-Type': 'application/json'
115
- }
116
-
117
- // Use TokenManager for automatic token management
118
- if (this._requiresInit(options.methodName) && this._tokenManager) {
119
- try {
120
- // Ensure we have a valid token (will refresh if needed)
121
- const validToken = await this._tokenManager.ensureValidToken()
122
- console.log(`[CoreService] Token check for ${options.methodName}:`, {
123
- hasValidToken: Boolean(validToken),
124
- tokenPreview: validToken ? `${validToken.substring(0, 20)}...` : null
125
- })
126
-
127
- if (validToken) {
128
- const authHeader = this._tokenManager.getAuthHeader()
129
- if (authHeader) {
130
- defaultHeaders.Authorization = authHeader
131
- console.log(`[CoreService] Added auth header for ${options.methodName}`)
132
- }
133
- }
134
- } catch (error) {
135
- console.warn('Token management failed, proceeding without authentication:', error)
136
- }
137
- } else if (this._requiresInit(options.methodName)) {
138
- // Fallback to context token if TokenManager not available
139
- const { authToken } = this._context
140
- if (authToken) {
141
- defaultHeaders.Authorization = `Bearer ${authToken}`
142
- console.log(`[CoreService] Using context token for ${options.methodName}`)
143
- }
144
- }
145
-
146
- try {
147
- const response = await fetch(url, {
148
- ...options,
149
- headers: {
150
- ...defaultHeaders,
151
- ...options.headers
152
- }
153
- })
154
-
155
- if (!response.ok) {
156
- let error = { message: `HTTP ${response.status}: ${response.statusText}` }
157
- try {
158
- error = await response.json()
159
- } catch {
160
- // Use default error message
161
- }
162
- throw new Error(error.message || error.error || 'Request failed')
163
- }
164
-
165
- return response.status === 204 ? null : response.json()
166
- } catch (error) {
167
- throw new Error(`Request failed: ${error.message}`)
168
- }
169
- }
170
-
171
- // ==================== AUTH METHODS ====================
172
-
173
- async register (userData) {
174
- try {
175
- return await this._request('/auth/register', {
176
- method: 'POST',
177
- body: JSON.stringify(userData),
178
- methodName: 'register'
179
- })
180
- } catch (error) {
181
- throw new Error(`Registration failed: ${error.message}`)
182
- }
183
- }
184
-
185
- async login (email, password) {
186
- try {
187
- const response = await this._request('/auth/login', {
188
- method: 'POST',
189
- body: JSON.stringify({ email, password }),
190
- methodName: 'login'
191
- })
192
-
193
- // Handle new response format: response.data.tokens
194
- if (response.success && response.data && response.data.tokens) {
195
- const { tokens } = response.data
196
- const tokenData = {
197
- access_token: tokens.accessToken,
198
- refresh_token: tokens.refreshToken,
199
- expires_in: tokens.accessTokenExp?.expiresIn,
200
- token_type: 'Bearer'
201
- }
202
-
203
- // Set tokens in TokenManager (will handle persistence and refresh scheduling)
204
- if (this._tokenManager) {
205
- this._tokenManager.setTokens(tokenData)
206
- }
207
-
208
- // Update context for backward compatibility
209
- this.updateContext({ authToken: tokens.accessToken })
210
- }
211
-
212
- return response
213
- } catch (error) {
214
- throw new Error(`Login failed: ${error.message}`)
215
- }
216
- }
217
-
218
- async logout () {
219
- this._requireReady('logout')
220
- try {
221
- // Call the logout API endpoint
222
- await this._request('/auth/logout', {
223
- method: 'POST',
224
- methodName: 'logout'
225
- })
226
-
227
- // Clear tokens from TokenManager and context
228
- if (this._tokenManager) {
229
- this._tokenManager.clearTokens()
230
- }
231
- this.updateContext({ authToken: null })
232
- } catch (error) {
233
- // Even if the API call fails, clear local tokens
234
- if (this._tokenManager) {
235
- this._tokenManager.clearTokens()
236
- }
237
- this.updateContext({ authToken: null })
238
-
239
- throw new Error(`Logout failed: ${error.message}`)
240
- }
241
- }
242
-
243
- async refreshToken (refreshToken) {
244
- try {
245
- return await this._request('/auth/refresh', {
246
- method: 'POST',
247
- body: JSON.stringify({ refreshToken }),
248
- methodName: 'refreshToken'
249
- })
250
- } catch (error) {
251
- throw new Error(`Token refresh failed: ${error.message}`)
252
- }
253
- }
254
-
255
- async googleAuth (idToken) {
256
- try {
257
- const response = await this._request('/auth/google', {
258
- method: 'POST',
259
- body: JSON.stringify({ idToken }),
260
- methodName: 'googleAuth'
261
- })
262
-
263
- // Handle new response format: response.data.tokens
264
- if (response.success && response.data && response.data.tokens) {
265
- const { tokens } = response.data
266
- const tokenData = {
267
- access_token: tokens.accessToken,
268
- refresh_token: tokens.refreshToken,
269
- expires_in: tokens.accessTokenExp?.expiresIn,
270
- token_type: 'Bearer'
271
- }
272
-
273
- // Set tokens in TokenManager
274
- if (this._tokenManager) {
275
- this._tokenManager.setTokens(tokenData)
276
- }
277
-
278
- // Update context for backward compatibility
279
- this.updateContext({ authToken: tokens.accessToken })
280
- }
281
-
282
- return response
283
- } catch (error) {
284
- throw new Error(`Google auth failed: ${error.message}`)
285
- }
286
- }
287
-
288
- async githubAuth (code) {
289
- try {
290
- const response = await this._request('/auth/github', {
291
- method: 'POST',
292
- body: JSON.stringify({ code }),
293
- methodName: 'githubAuth'
294
- })
295
-
296
- // Handle new response format: response.data.tokens
297
- if (response.success && response.data && response.data.tokens) {
298
- const { tokens } = response.data
299
- const tokenData = {
300
- access_token: tokens.accessToken,
301
- refresh_token: tokens.refreshToken,
302
- expires_in: tokens.accessTokenExp?.expiresIn,
303
- token_type: 'Bearer'
304
- }
305
-
306
- // Set tokens in TokenManager
307
- if (this._tokenManager) {
308
- this._tokenManager.setTokens(tokenData)
309
- }
310
-
311
- // Update context for backward compatibility
312
- this.updateContext({ authToken: tokens.accessToken })
313
- }
314
-
315
- return response
316
- } catch (error) {
317
- throw new Error(`GitHub auth failed: ${error.message}`)
318
- }
319
- }
320
-
321
- async requestPasswordReset (email) {
322
- try {
323
- return await this._request('/auth/request-password-reset', {
324
- method: 'POST',
325
- body: JSON.stringify({ email }),
326
- methodName: 'requestPasswordReset'
327
- })
328
- } catch (error) {
329
- throw new Error(`Password reset request failed: ${error.message}`)
330
- }
331
- }
332
-
333
- async confirmPasswordReset (token, password) {
334
- try {
335
- return await this._request('/auth/reset-password-confirm', {
336
- method: 'POST',
337
- body: JSON.stringify({ token, password }),
338
- methodName: 'confirmPasswordReset'
339
- })
340
- } catch (error) {
341
- throw new Error(`Password reset confirmation failed: ${error.message}`)
342
- }
343
- }
344
-
345
- async confirmRegistration (token) {
346
- try {
347
- return await this._request('/auth/register-confirmation', {
348
- method: 'POST',
349
- body: JSON.stringify({ token }),
350
- methodName: 'confirmRegistration'
351
- })
352
- } catch (error) {
353
- throw new Error(`Registration confirmation failed: ${error.message}`)
354
- }
355
- }
356
-
357
- async requestPasswordChange () {
358
- this._requireReady('requestPasswordChange')
359
- try {
360
- return await this._request('/auth/request-password-change', {
361
- method: 'POST',
362
- methodName: 'requestPasswordChange'
363
- })
364
- } catch (error) {
365
- throw new Error(`Password change request failed: ${error.message}`)
366
- }
367
- }
368
-
369
- async confirmPasswordChange (currentPassword, newPassword, code) {
370
- this._requireReady('confirmPasswordChange')
371
- try {
372
- return await this._request('/auth/confirm-password-change', {
373
- method: 'POST',
374
- body: JSON.stringify({ currentPassword, newPassword, code }),
375
- methodName: 'confirmPasswordChange'
376
- })
377
- } catch (error) {
378
- throw new Error(`Password change confirmation failed: ${error.message}`)
379
- }
380
- }
381
-
382
- async getMe () {
383
- this._requireReady('getMe')
384
- try {
385
- return await this._request('/auth/me', {
386
- method: 'GET',
387
- methodName: 'getMe'
388
- })
389
- } catch (error) {
390
- throw new Error(`Failed to get user profile: ${error.message}`)
391
- }
392
- }
393
-
394
- /**
395
- * Get stored authentication state (backward compatibility method)
396
- * Replaces AuthService.getStoredAuthState()
397
- */
398
- async getStoredAuthState () {
399
- try {
400
- if (!this._tokenManager) {
401
- return {
402
- userId: false,
403
- authToken: false
404
- }
405
- }
406
-
407
- const tokenStatus = this._tokenManager.getTokenStatus()
408
-
409
- if (!tokenStatus.hasTokens) {
410
- return {
411
- userId: false,
412
- authToken: false
413
- }
414
- }
415
-
416
- // If tokens exist but are invalid, try to refresh
417
- if (!tokenStatus.isValid && tokenStatus.hasRefreshToken) {
418
- try {
419
- await this._tokenManager.ensureValidToken()
420
- } catch (error) {
421
- // If refresh fails, clear tokens and return empty state
422
- this._tokenManager.clearTokens()
423
- return {
424
- userId: false,
425
- authToken: false,
426
- error: `Token refresh failed: ${error.message}`
427
- }
428
- }
429
- }
430
-
431
- // Get current user data if we have valid tokens
432
- try {
433
- const currentUser = await this.getMe()
434
- const userProjects = await this.getUserProjects()
435
-
436
- return {
437
- userId: currentUser.data.id,
438
- authToken: this._tokenManager.getAccessToken(),
439
- user: {
440
- ...currentUser.data,
441
- projects: userProjects?.data || []
442
- },
443
- error: null
444
- }
445
- } catch (error) {
446
- // If API calls fail, clear invalid tokens
447
- this._tokenManager.clearTokens()
448
- return {
449
- userId: false,
450
- authToken: false,
451
- error: `Failed to get user data: ${error.message}`
452
- }
453
- }
454
- } catch (error) {
455
- return {
456
- userId: false,
457
- authToken: false,
458
- error: `Failed to get stored auth state: ${error.message}`
459
- }
460
- }
461
- }
462
-
463
- // ==================== USER METHODS ====================
464
-
465
- async getUserProfile () {
466
- this._requireReady('getUserProfile')
467
- try {
468
- return await this._request('/users/profile', {
469
- method: 'GET',
470
- methodName: 'getUserProfile'
471
- })
472
- } catch (error) {
473
- throw new Error(`Failed to get user profile: ${error.message}`)
474
- }
475
- }
476
-
477
- async updateUserProfile (profileData) {
478
- this._requireReady('updateUserProfile')
479
- try {
480
- return await this._request('/users/profile', {
481
- method: 'PATCH',
482
- body: JSON.stringify(profileData),
483
- methodName: 'updateUserProfile'
484
- })
485
- } catch (error) {
486
- throw new Error(`Failed to update user profile: ${error.message}`)
487
- }
488
- }
489
-
490
- async getUserProjects () {
491
- this._requireReady('getUserProjects')
492
- try {
493
- return await this._request('/users/projects', {
494
- method: 'GET',
495
- methodName: 'getUserProjects'
496
- })
497
- } catch (error) {
498
- throw new Error(`Failed to get user projects: ${error.message}`)
499
- }
500
- }
501
-
502
- async getUser (userId) {
503
- this._requireReady('getUser')
504
- if (!userId) {
505
- throw new Error('User ID is required')
506
- }
507
- try {
508
- return await this._request(`/users/${userId}`, {
509
- method: 'GET',
510
- methodName: 'getUser'
511
- })
512
- } catch (error) {
513
- throw new Error(`Failed to get user: ${error.message}`)
514
- }
515
- }
516
-
517
- async getUserByEmail (email) {
518
- this._requireReady('getUserByEmail')
519
- if (!email) {
520
- throw new Error('Email is required')
521
- }
522
- try {
523
- return await this._request('/auth/user', {
524
- method: 'GET',
525
- headers: {
526
- 'X-User-Email': email
527
- },
528
- methodName: 'getUserByEmail'
529
- })
530
- } catch (error) {
531
- throw new Error(`Failed to get user by email: ${error.message}`)
532
- }
533
- }
534
-
535
- // ==================== PROJECT METHODS ====================
536
-
537
- async createProject (projectData) {
538
- this._requireReady('createProject')
539
- try {
540
- return await this._request('/projects', {
541
- method: 'POST',
542
- body: JSON.stringify(projectData),
543
- methodName: 'createProject'
544
- })
545
- } catch (error) {
546
- throw new Error(`Failed to create project: ${error.message}`)
547
- }
548
- }
549
-
550
- async getProjects () {
551
- this._requireReady('getProjects')
552
- try {
553
- return await this._request('/projects', {
554
- method: 'GET',
555
- methodName: 'getProjects'
556
- })
557
- } catch (error) {
558
- throw new Error(`Failed to get projects: ${error.message}`)
559
- }
560
- }
561
-
562
- async getProject (projectId) {
563
- this._requireReady('getProject')
564
- if (!projectId) {
565
- throw new Error('Project ID is required')
566
- }
567
- try {
568
- return await this._request(`/projects/${projectId}`, {
569
- method: 'GET',
570
- methodName: 'getProject'
571
- })
572
- } catch (error) {
573
- throw new Error(`Failed to get project: ${error.message}`)
574
- }
575
- }
576
-
577
- async getProjectByKey (key) {
578
- this._requireReady('getProjectByKey')
579
- if (!key) {
580
- throw new Error('Project key is required')
581
- }
582
- try {
583
- return await this._request(`/projects/check-key/${key}`, {
584
- method: 'GET',
585
- methodName: 'getProjectByKey'
586
- })
587
- } catch (error) {
588
- throw new Error(`Failed to get project by key: ${error.message}`)
589
- }
590
- }
591
-
592
- async updateProject (projectId, data) {
593
- this._requireReady('updateProject')
594
- if (!projectId) {
595
- throw new Error('Project ID is required')
596
- }
597
- try {
598
- return await this._request(`/projects/${projectId}`, {
599
- method: 'PATCH',
600
- body: JSON.stringify(data),
601
- methodName: 'updateProject'
602
- })
603
- } catch (error) {
604
- throw new Error(`Failed to update project: ${error.message}`)
605
- }
606
- }
607
-
608
- async updateProjectComponents (projectId, components) {
609
- this._requireReady('updateProjectComponents')
610
- if (!projectId) {
611
- throw new Error('Project ID is required')
612
- }
613
- try {
614
- return await this._request(`/projects/${projectId}/components`, {
615
- method: 'PATCH',
616
- body: JSON.stringify({ components }),
617
- methodName: 'updateProjectComponents'
618
- })
619
- } catch (error) {
620
- throw new Error(`Failed to update project components: ${error.message}`)
621
- }
622
- }
623
-
624
- async updateProjectSettings (projectId, settings) {
625
- this._requireReady('updateProjectSettings')
626
- if (!projectId) {
627
- throw new Error('Project ID is required')
628
- }
629
- try {
630
- return await this._request(`/projects/${projectId}/settings`, {
631
- method: 'PATCH',
632
- body: JSON.stringify({ settings }),
633
- methodName: 'updateProjectSettings'
634
- })
635
- } catch (error) {
636
- throw new Error(`Failed to update project settings: ${error.message}`)
637
- }
638
- }
639
-
640
- async updateProjectName (projectId, name) {
641
- this._requireReady('updateProjectName')
642
- if (!projectId) {
643
- throw new Error('Project ID is required')
644
- }
645
- try {
646
- return await this._request(`/projects/${projectId}`, {
647
- method: 'PATCH',
648
- body: JSON.stringify({ name }),
649
- methodName: 'updateProjectName'
650
- })
651
- } catch (error) {
652
- throw new Error(`Failed to update project name: ${error.message}`)
653
- }
654
- }
655
-
656
- async updateProjectPackage (projectId, pkg) {
657
- this._requireReady('updateProjectPackage')
658
- if (!projectId) {
659
- throw new Error('Project ID is required')
660
- }
661
- try {
662
- return await this._request(`/projects/${projectId}/tier`, {
663
- method: 'PATCH',
664
- body: JSON.stringify({ tier: pkg }),
665
- methodName: 'updateProjectPackage'
666
- })
667
- } catch (error) {
668
- throw new Error(`Failed to update project package: ${error.message}`)
669
- }
670
- }
671
-
672
- async duplicateProject (projectId, newName, newKey) {
673
- this._requireReady('duplicateProject')
674
- if (!projectId) {
675
- throw new Error('Project ID is required')
676
- }
677
- try {
678
- return await this._request(`/projects/${projectId}/duplicate`, {
679
- method: 'POST',
680
- body: JSON.stringify({ name: newName, key: newKey }),
681
- methodName: 'duplicateProject'
682
- })
683
- } catch (error) {
684
- throw new Error(`Failed to duplicate project: ${error.message}`)
685
- }
686
- }
687
-
688
- async removeProject (projectId) {
689
- this._requireReady('removeProject')
690
- if (!projectId) {
691
- throw new Error('Project ID is required')
692
- }
693
- try {
694
- return await this._request(`/projects/${projectId}`, {
695
- method: 'DELETE',
696
- methodName: 'removeProject'
697
- })
698
- } catch (error) {
699
- throw new Error(`Failed to remove project: ${error.message}`)
700
- }
701
- }
702
-
703
- async checkProjectKeyAvailability (key) {
704
- this._requireReady('checkProjectKeyAvailability')
705
- if (!key) {
706
- throw new Error('Project key is required')
707
- }
708
- try {
709
- return await this._request(`/projects/check-key/${key}`, {
710
- method: 'GET',
711
- methodName: 'checkProjectKeyAvailability'
712
- })
713
- } catch (error) {
714
- throw new Error(`Failed to check project key availability: ${error.message}`)
715
- }
716
- }
717
-
718
- // ==================== PROJECT MEMBER METHODS ====================
719
-
720
- async getProjectMembers (projectId) {
721
- this._requireReady('getProjectMembers')
722
- if (!projectId) {
723
- throw new Error('Project ID is required')
724
- }
725
- try {
726
- return await this._request(`/projects/${projectId}/members`, {
727
- method: 'GET',
728
- methodName: 'getProjectMembers'
729
- })
730
- } catch (error) {
731
- throw new Error(`Failed to get project members: ${error.message}`)
732
- }
733
- }
734
-
735
- async inviteMember (projectId, email, message, role = 'guest') {
736
- this._requireReady('inviteMember')
737
- if (!projectId || !email) {
738
- throw new Error('Project ID and email are required')
739
- }
740
- try {
741
- return await this._request(`/projects/${projectId}/invite`, {
742
- method: 'POST',
743
- body: JSON.stringify({ email, role, message }),
744
- methodName: 'inviteMember'
745
- })
746
- } catch (error) {
747
- throw new Error(`Failed to invite member: ${error.message}`)
748
- }
749
- }
750
-
751
- async acceptInvite (projectId, token) {
752
- this._requireReady('acceptInvite')
753
- if (!projectId || !token) {
754
- throw new Error('Project ID and token are required')
755
- }
756
- try {
757
- return await this._request(`/projects/${projectId}/accept-invite`, {
758
- method: 'POST',
759
- body: JSON.stringify({ token }),
760
- methodName: 'acceptInvite'
761
- })
762
- } catch (error) {
763
- throw new Error(`Failed to accept invite: ${error.message}`)
764
- }
765
- }
766
-
767
- async updateMemberRole (projectId, memberId, role) {
768
- this._requireReady('updateMemberRole')
769
- if (!projectId || !memberId || !role) {
770
- throw new Error('Project ID, member ID, and role are required')
771
- }
772
- try {
773
- return await this._request(`/projects/${projectId}/members/${memberId}`, {
774
- method: 'PATCH',
775
- body: JSON.stringify({ role }),
776
- methodName: 'updateMemberRole'
777
- })
778
- } catch (error) {
779
- throw new Error(`Failed to update member role: ${error.message}`)
780
- }
781
- }
782
-
783
- async removeMember (projectId, memberId) {
784
- this._requireReady('removeMember')
785
- if (!projectId || !memberId) {
786
- throw new Error('Project ID and member ID are required')
787
- }
788
- try {
789
- return await this._request(`/projects/${projectId}/members/${memberId}`, {
790
- method: 'DELETE',
791
- methodName: 'removeMember'
792
- })
793
- } catch (error) {
794
- throw new Error(`Failed to remove member: ${error.message}`)
795
- }
796
- }
797
-
798
- // ==================== PROJECT LIBRARY METHODS ====================
799
-
800
- async getAvailableLibraries (params = {}) {
801
- this._requireReady('getAvailableLibraries')
802
- const queryParams = new URLSearchParams(params).toString()
803
- try {
804
- return await this._request(`/projects/libraries/available?${queryParams}`, {
805
- method: 'GET',
806
- methodName: 'getAvailableLibraries'
807
- })
808
- } catch (error) {
809
- throw new Error(`Failed to get available libraries: ${error.message}`)
810
- }
811
- }
812
-
813
- async getProjectLibraries (projectId) {
814
- this._requireReady('getProjectLibraries')
815
- if (!projectId) {
816
- throw new Error('Project ID is required')
817
- }
818
- try {
819
- return await this._request(`/projects/${projectId}/libraries`, {
820
- method: 'GET',
821
- methodName: 'getProjectLibraries'
822
- })
823
- } catch (error) {
824
- throw new Error(`Failed to get project libraries: ${error.message}`)
825
- }
826
- }
827
-
828
- async addProjectLibraries (projectId, libraryIds) {
829
- this._requireReady('addProjectLibraries')
830
- if (!projectId || !libraryIds) {
831
- throw new Error('Project ID and library IDs are required')
832
- }
833
- try {
834
- return await this._request(`/projects/${projectId}/libraries`, {
835
- method: 'POST',
836
- body: JSON.stringify({ libraryIds }),
837
- methodName: 'addProjectLibraries'
838
- })
839
- } catch (error) {
840
- throw new Error(`Failed to add project libraries: ${error.message}`)
841
- }
842
- }
843
-
844
- async removeProjectLibraries (projectId, libraryIds) {
845
- this._requireReady('removeProjectLibraries')
846
- if (!projectId || !libraryIds) {
847
- throw new Error('Project ID and library IDs are required')
848
- }
849
- try {
850
- return await this._request(`/projects/${projectId}/libraries`, {
851
- method: 'DELETE',
852
- body: JSON.stringify({ libraryIds }),
853
- methodName: 'removeProjectLibraries'
854
- })
855
- } catch (error) {
856
- throw new Error(`Failed to remove project libraries: ${error.message}`)
857
- }
858
- }
859
-
860
- // ==================== FILE METHODS ====================
861
-
862
- async uploadFile (file, options = {}) {
863
- this._requireReady('uploadFile')
864
- if (!file) {
865
- throw new Error('File is required for upload')
866
- }
867
-
868
- const formData = new FormData()
869
- formData.append('file', file)
870
-
871
- // Add optional parameters
872
- if (options.projectId) {formData.append('projectId', options.projectId)}
873
- if (options.tags) {formData.append('tags', JSON.stringify(options.tags))}
874
- if (options.visibility) {formData.append('visibility', options.visibility)}
875
- if (options.metadata) {formData.append('metadata', JSON.stringify(options.metadata))}
876
-
877
- try {
878
- return await this._request('/files/upload', {
879
- method: 'POST',
880
- body: formData,
881
- headers: {}, // Let browser set Content-Type for FormData
882
- methodName: 'uploadFile'
883
- })
884
- } catch (error) {
885
- throw new Error(`File upload failed: ${error.message}`)
886
- }
887
- }
888
-
889
- async updateProjectIcon (projectId, iconFile) {
890
- this._requireReady('updateProjectIcon')
891
- if (!projectId || !iconFile) {
892
- throw new Error('Project ID and icon file are required')
893
- }
894
-
895
- const formData = new FormData()
896
- formData.append('icon', iconFile)
897
- formData.append('projectId', projectId)
898
-
899
- try {
900
- return await this._request('/files/upload-project-icon', {
901
- method: 'POST',
902
- body: formData,
903
- headers: {}, // Let browser set Content-Type for FormData
904
- methodName: 'updateProjectIcon'
905
- })
906
- } catch (error) {
907
- throw new Error(`Failed to update project icon: ${error.message}`)
908
- }
909
- }
910
-
911
- // ==================== PAYMENT METHODS ====================
912
-
913
- async checkout (options = {}) {
914
- this._requireReady('checkout')
915
- const {
916
- projectId,
917
- seats = 1,
918
- price = 'starter_monthly',
919
- successUrl = `${window.location.origin}/success`,
920
- cancelUrl = `${window.location.origin}/pricing`
921
- } = options
922
-
923
- if (!projectId) {
924
- throw new Error('Project ID is required for checkout')
925
- }
926
-
927
- try {
928
- return await this._request('/payments/checkout', {
929
- method: 'POST',
930
- body: JSON.stringify({
931
- projectId,
932
- seats,
933
- price,
934
- successUrl,
935
- cancelUrl
936
- }),
937
- methodName: 'checkout'
938
- })
939
- } catch (error) {
940
- throw new Error(`Failed to checkout: ${error.message}`)
941
- }
942
- }
943
-
944
- async getSubscriptionStatus (projectId) {
945
- this._requireReady('getSubscriptionStatus')
946
- if (!projectId) {
947
- throw new Error('Project ID is required')
948
- }
949
- try {
950
- return await this._request(`/payments/subscription/${projectId}`, {
951
- method: 'GET',
952
- methodName: 'getSubscriptionStatus'
953
- })
954
- } catch (error) {
955
- throw new Error(`Failed to get subscription status: ${error.message}`)
956
- }
957
- }
958
-
959
- // ==================== DNS METHODS ====================
960
-
961
- async createDnsRecord (domain, options = {}) {
962
- this._requireReady('createDnsRecord')
963
- if (!domain) {
964
- throw new Error('Domain is required')
965
- }
966
- try {
967
- return await this._request('/dns/records', {
968
- method: 'POST',
969
- body: JSON.stringify({ domain, ...options }),
970
- methodName: 'createDnsRecord'
971
- })
972
- } catch (error) {
973
- throw new Error(`Failed to create DNS record: ${error.message}`)
974
- }
975
- }
976
-
977
- async getDnsRecord (domain) {
978
- this._requireReady('getDnsRecord')
979
- if (!domain) {
980
- throw new Error('Domain is required')
981
- }
982
- try {
983
- return await this._request(`/dns/records/${domain}`, {
984
- method: 'GET',
985
- methodName: 'getDnsRecord'
986
- })
987
- } catch (error) {
988
- throw new Error(`Failed to get DNS record: ${error.message}`)
989
- }
990
- }
991
-
992
- async removeDnsRecord (domain) {
993
- this._requireReady('removeDnsRecord')
994
- if (!domain) {
995
- throw new Error('Domain is required')
996
- }
997
- try {
998
- return await this._request(`/dns/records/${domain}`, {
999
- method: 'DELETE',
1000
- methodName: 'removeDnsRecord'
1001
- })
1002
- } catch (error) {
1003
- throw new Error(`Failed to remove DNS record: ${error.message}`)
1004
- }
1005
- }
1006
-
1007
- async setProjectDomains (projectKey, customDomain, hasCustomDomainAccess = false) {
1008
- this._requireReady('setProjectDomains')
1009
- if (!projectKey) {
1010
- throw new Error('Project key is required')
1011
- }
1012
- try {
1013
- return await this._request('/dns/project-domains', {
1014
- method: 'POST',
1015
- body: JSON.stringify({
1016
- projectKey,
1017
- customDomain,
1018
- hasCustomDomainAccess
1019
- }),
1020
- methodName: 'setProjectDomains'
1021
- })
1022
- } catch (error) {
1023
- throw new Error(`Failed to set project domains: ${error.message}`)
1024
- }
1025
- }
1026
-
1027
- // ==================== UTILITY METHODS ====================
1028
-
1029
- async getHealthStatus () {
1030
- try {
1031
- return await this._request('/health', {
1032
- method: 'GET',
1033
- methodName: 'getHealthStatus'
1034
- })
1035
- } catch (error) {
1036
- throw new Error(`Failed to get health status: ${error.message}`)
1037
- }
1038
- }
1039
-
1040
- // ==================== PROJECT DATA METHODS (SYMSTORY REPLACEMENT) ====================
1041
-
1042
- /**
1043
- * Apply changes to a project, creating a new version
1044
- * Replaces: SymstoryService.updateData()
1045
- */
1046
- async applyProjectChanges (projectId, changes, options = {}) {
1047
- this._requireReady('applyProjectChanges')
1048
- if (!projectId) {
1049
- throw new Error('Project ID is required')
1050
- }
1051
- if (!Array.isArray(changes)) {
1052
- throw new Error('Changes must be an array')
1053
- }
1054
-
1055
- const {
1056
- message,
1057
- branch = 'main',
1058
- type = 'patch'
1059
- } = options
1060
-
1061
- try {
1062
- const response = await this._request(`/projects/${projectId}/changes`, {
1063
- method: 'POST',
1064
- body: JSON.stringify({
1065
- changes,
1066
- message,
1067
- branch,
1068
- type
1069
- }),
1070
- methodName: 'applyProjectChanges'
1071
- })
1072
-
1073
- return response
1074
- } catch (error) {
1075
- throw new Error(`Failed to apply project changes: ${error.message}`)
1076
- }
1077
- }
1078
-
1079
- /**
1080
- * Get current project data for a specific branch
1081
- * Replaces: SymstoryService.getData()
1082
- */
1083
- async getProjectData (projectId, options = {}) {
1084
- this._requireReady('getProjectData')
1085
- if (!projectId) {
1086
- throw new Error('Project ID is required')
1087
- }
1088
-
1089
- const {
1090
- branch = 'main',
1091
- includeHistory = false
1092
- } = options
1093
-
1094
- const queryParams = new URLSearchParams({
1095
- branch,
1096
- includeHistory: includeHistory.toString()
1097
- }).toString()
1098
-
1099
- try {
1100
- return await this._request(`/projects/${projectId}/data?${queryParams}`, {
1101
- method: 'GET',
1102
- methodName: 'getProjectData'
1103
- })
1104
- } catch (error) {
1105
- throw new Error(`Failed to get project data: ${error.message}`)
1106
- }
1107
- }
1108
-
1109
- /**
1110
- * Get project versions with pagination
1111
- */
1112
- async getProjectVersions (projectId, options = {}) {
1113
- this._requireReady('getProjectVersions')
1114
- if (!projectId) {
1115
- throw new Error('Project ID is required')
1116
- }
1117
-
1118
- const {
1119
- branch = 'main',
1120
- page = 1,
1121
- limit = 50
1122
- } = options
1123
-
1124
- const queryParams = new URLSearchParams({
1125
- branch,
1126
- page: page.toString(),
1127
- limit: limit.toString()
1128
- }).toString()
1129
-
1130
- try {
1131
- return await this._request(`/projects/${projectId}/versions?${queryParams}`, {
1132
- method: 'GET',
1133
- methodName: 'getProjectVersions'
1134
- })
1135
- } catch (error) {
1136
- throw new Error(`Failed to get project versions: ${error.message}`)
1137
- }
1138
- }
1139
-
1140
- /**
1141
- * Restore project to a previous version
1142
- * Replaces: SymstoryService.restoreVersion()
1143
- */
1144
- async restoreProjectVersion (projectId, version, options = {}) {
1145
- this._requireReady('restoreProjectVersion')
1146
- if (!projectId) {
1147
- throw new Error('Project ID is required')
1148
- }
1149
- if (!version) {
1150
- throw new Error('Version is required')
1151
- }
1152
-
1153
- const {
1154
- message,
1155
- branch = 'main',
1156
- type = 'patch'
1157
- } = options
1158
-
1159
- try {
1160
- return await this._request(`/projects/${projectId}/restore`, {
1161
- method: 'POST',
1162
- body: JSON.stringify({
1163
- version,
1164
- message,
1165
- branch,
1166
- type
1167
- }),
1168
- methodName: 'restoreProjectVersion'
1169
- })
1170
- } catch (error) {
1171
- throw new Error(`Failed to restore project version: ${error.message}`)
1172
- }
1173
- }
1174
-
1175
- /**
1176
- * Helper method to update a single item in the project
1177
- * Convenience wrapper around applyProjectChanges
1178
- */
1179
- async updateProjectItem (projectId, path, value, options = {}) {
1180
- const changes = [['update', path, value]]
1181
- const message = options.message || `Updated ${Array.isArray(path) ? path.join('.') : path}`
1182
-
1183
- return await this.applyProjectChanges(projectId, changes, {
1184
- ...options,
1185
- message
1186
- })
1187
- }
1188
-
1189
- /**
1190
- * Helper method to delete an item from the project
1191
- * Convenience wrapper around applyProjectChanges
1192
- */
1193
- async deleteProjectItem (projectId, path, options = {}) {
1194
- const changes = [['delete', path]]
1195
- const message = options.message || `Deleted ${Array.isArray(path) ? path.join('.') : path}`
1196
-
1197
- return await this.applyProjectChanges(projectId, changes, {
1198
- ...options,
1199
- message
1200
- })
1201
- }
1202
-
1203
- /**
1204
- * Helper method to set a value in the project (alias for update)
1205
- * Convenience wrapper around applyProjectChanges
1206
- */
1207
- async setProjectValue (projectId, path, value, options = {}) {
1208
- const changes = [['set', path, value]]
1209
- const message = options.message || `Set ${Array.isArray(path) ? path.join('.') : path}`
1210
-
1211
- return await this.applyProjectChanges(projectId, changes, {
1212
- ...options,
1213
- message
1214
- })
1215
- }
1216
-
1217
- /**
1218
- * Helper method to add multiple items to the project
1219
- * Convenience wrapper around applyProjectChanges
1220
- */
1221
- async addProjectItems (projectId, items, options = {}) {
1222
- const changes = items.map(item => {
1223
- const [type, data] = item
1224
- const { value, ...schema } = data
1225
- return [
1226
- ['update', [type, data.key], value],
1227
- ['update', ['schema', type, data.key], schema]
1228
- ]
1229
- }).flat()
1230
-
1231
- const message = options.message || `Added ${items.length} items`
1232
-
1233
- return await this.applyProjectChanges(projectId, changes, {
1234
- ...options,
1235
- message
1236
- })
1237
- }
1238
-
1239
- /**
1240
- * Helper method to get specific data from project by path
1241
- * Convenience wrapper that gets project data and extracts specific path
1242
- */
1243
- async getProjectItemByPath (projectId, path, options = {}) {
1244
- const projectData = await this.getProjectData(projectId, options)
1245
-
1246
- if (!projectData?.data) {
1247
- return null
1248
- }
1249
-
1250
- // Navigate to the specific path in the data
1251
- let current = projectData.data
1252
- const pathArray = Array.isArray(path) ? path : [path]
1253
-
1254
- for (const segment of pathArray) {
1255
- if (current && typeof current === 'object' && segment in current) {
1256
- current = current[segment]
1257
- } else {
1258
- return null
1259
- }
1260
- }
1261
-
1262
- return current
1263
- }
1264
-
1265
- // ==================== PULL REQUEST METHODS ====================
1266
-
1267
- /**
1268
- * Create a new pull request
1269
- */
1270
- async createPullRequest (projectId, pullRequestData) {
1271
- this._requireReady('createPullRequest')
1272
- if (!projectId) {
1273
- throw new Error('Project ID is required')
1274
- }
1275
- if (!pullRequestData.source || !pullRequestData.target || !pullRequestData.title) {
1276
- throw new Error('Source branch, target branch, and title are required')
1277
- }
1278
-
1279
- try {
1280
- return await this._request(`/projects/${projectId}/pull-requests`, {
1281
- method: 'POST',
1282
- body: JSON.stringify(pullRequestData),
1283
- methodName: 'createPullRequest'
1284
- })
1285
- } catch (error) {
1286
- throw new Error(`Failed to create pull request: ${error.message}`)
1287
- }
1288
- }
1289
-
1290
- /**
1291
- * List pull requests for a project with filtering options
1292
- */
1293
- async listPullRequests (projectId, options = {}) {
1294
- this._requireReady('listPullRequests')
1295
- if (!projectId) {
1296
- throw new Error('Project ID is required')
1297
- }
1298
-
1299
- const {
1300
- status = 'open',
1301
- source,
1302
- target,
1303
- page = 1,
1304
- limit = 20
1305
- } = options
1306
-
1307
- const queryParams = new URLSearchParams({
1308
- status,
1309
- page: page.toString(),
1310
- limit: limit.toString()
1311
- })
1312
-
1313
- if (source) {queryParams.append('source', source)}
1314
- if (target) {queryParams.append('target', target)}
1315
-
1316
- try {
1317
- return await this._request(`/projects/${projectId}/pull-requests?${queryParams.toString()}`, {
1318
- method: 'GET',
1319
- methodName: 'listPullRequests'
1320
- })
1321
- } catch (error) {
1322
- throw new Error(`Failed to list pull requests: ${error.message}`)
1323
- }
1324
- }
1325
-
1326
- /**
1327
- * Get detailed information about a specific pull request
1328
- */
1329
- async getPullRequest (projectId, prId) {
1330
- this._requireReady('getPullRequest')
1331
- if (!projectId) {
1332
- throw new Error('Project ID is required')
1333
- }
1334
- if (!prId) {
1335
- throw new Error('Pull request ID is required')
1336
- }
1337
-
1338
- try {
1339
- return await this._request(`/projects/${projectId}/pull-requests/${prId}`, {
1340
- method: 'GET',
1341
- methodName: 'getPullRequest'
1342
- })
1343
- } catch (error) {
1344
- throw new Error(`Failed to get pull request: ${error.message}`)
1345
- }
1346
- }
1347
-
1348
- /**
1349
- * Submit a review for a pull request
1350
- */
1351
- async reviewPullRequest (projectId, prId, reviewData) {
1352
- this._requireReady('reviewPullRequest')
1353
- if (!projectId) {
1354
- throw new Error('Project ID is required')
1355
- }
1356
- if (!prId) {
1357
- throw new Error('Pull request ID is required')
1358
- }
1359
-
1360
- const validStatuses = ['approved', 'requested_changes', 'feedback']
1361
- if (reviewData.status && !validStatuses.includes(reviewData.status)) {
1362
- throw new Error(`Invalid review status. Must be one of: ${validStatuses.join(', ')}`)
1363
- }
1364
-
1365
- try {
1366
- return await this._request(`/projects/${projectId}/pull-requests/${prId}/review`, {
1367
- method: 'POST',
1368
- body: JSON.stringify(reviewData),
1369
- methodName: 'reviewPullRequest'
1370
- })
1371
- } catch (error) {
1372
- throw new Error(`Failed to review pull request: ${error.message}`)
1373
- }
1374
- }
1375
-
1376
- /**
1377
- * Add a comment to an existing review thread
1378
- */
1379
- async addPullRequestComment (projectId, prId, commentData) {
1380
- this._requireReady('addPullRequestComment')
1381
- if (!projectId) {
1382
- throw new Error('Project ID is required')
1383
- }
1384
- if (!prId) {
1385
- throw new Error('Pull request ID is required')
1386
- }
1387
- if (!commentData.value) {
1388
- throw new Error('Comment value is required')
1389
- }
1390
-
1391
- try {
1392
- return await this._request(`/projects/${projectId}/pull-requests/${prId}/comment`, {
1393
- method: 'POST',
1394
- body: JSON.stringify(commentData),
1395
- methodName: 'addPullRequestComment'
1396
- })
1397
- } catch (error) {
1398
- throw new Error(`Failed to add pull request comment: ${error.message}`)
1399
- }
1400
- }
1401
-
1402
- /**
1403
- * Merge an approved pull request
1404
- */
1405
- async mergePullRequest (projectId, prId) {
1406
- this._requireReady('mergePullRequest')
1407
- if (!projectId) {
1408
- throw new Error('Project ID is required')
1409
- }
1410
- if (!prId) {
1411
- throw new Error('Pull request ID is required')
1412
- }
1413
-
1414
- try {
1415
- const response = await this._request(`/projects/${projectId}/pull-requests/${prId}/merge`, {
1416
- method: 'POST',
1417
- methodName: 'mergePullRequest'
1418
- })
1419
-
1420
- return response
1421
- } catch (error) {
1422
- // Handle specific merge conflict errors
1423
- if (error.message.includes('conflicts') || error.message.includes('409')) {
1424
- throw new Error(`Pull request has merge conflicts: ${error.message}`)
1425
- }
1426
- throw new Error(`Failed to merge pull request: ${error.message}`)
1427
- }
1428
- }
1429
-
1430
- /**
1431
- * Get the diff/changes for a pull request
1432
- */
1433
- async getPullRequestDiff (projectId, prId) {
1434
- this._requireReady('getPullRequestDiff')
1435
- if (!projectId) {
1436
- throw new Error('Project ID is required')
1437
- }
1438
- if (!prId) {
1439
- throw new Error('Pull request ID is required')
1440
- }
1441
-
1442
- try {
1443
- return await this._request(`/projects/${projectId}/pull-requests/${prId}/diff`, {
1444
- method: 'GET',
1445
- methodName: 'getPullRequestDiff'
1446
- })
1447
- } catch (error) {
1448
- throw new Error(`Failed to get pull request diff: ${error.message}`)
1449
- }
1450
- }
1451
-
1452
- /**
1453
- * Helper method to create a pull request with validation
1454
- */
1455
- async createPullRequestWithValidation (projectId, data) {
1456
- const { source, target, title, description, changes } = data
1457
-
1458
- // Basic validation
1459
- if (source === target) {
1460
- throw new Error('Source and target branches cannot be the same')
1461
- }
1462
-
1463
- if (!title || title.trim().length === 0) {
1464
- throw new Error('Pull request title cannot be empty')
1465
- }
1466
-
1467
- if (title.length > 200) {
1468
- throw new Error('Pull request title cannot exceed 200 characters')
1469
- }
1470
-
1471
- const pullRequestData = {
1472
- source: source.trim(),
1473
- target: target.trim(),
1474
- title: title.trim(),
1475
- ...(description && { description: description.trim() }),
1476
- ...(changes && { changes })
1477
- }
1478
-
1479
- return await this.createPullRequest(projectId, pullRequestData)
1480
- }
1481
-
1482
- /**
1483
- * Helper method to approve a pull request
1484
- */
1485
- async approvePullRequest (projectId, prId, comment = '') {
1486
- const reviewData = {
1487
- status: 'approved',
1488
- ...(comment && {
1489
- threads: [{
1490
- comment,
1491
- type: 'praise'
1492
- }]
1493
- })
1494
- }
1495
-
1496
- return await this.reviewPullRequest(projectId, prId, reviewData)
1497
- }
1498
-
1499
- /**
1500
- * Helper method to request changes on a pull request
1501
- */
1502
- async requestPullRequestChanges (projectId, prId, threads = []) {
1503
- if (!threads || threads.length === 0) {
1504
- throw new Error('Must provide specific feedback when requesting changes')
1505
- }
1506
-
1507
- const reviewData = {
1508
- status: 'requested_changes',
1509
- threads
1510
- }
1511
-
1512
- return await this.reviewPullRequest(projectId, prId, reviewData)
1513
- }
1514
-
1515
- /**
1516
- * Helper method to get pull requests by status
1517
- */
1518
- async getOpenPullRequests (projectId, options = {}) {
1519
- return await this.listPullRequests(projectId, { ...options, status: 'open' })
1520
- }
1521
-
1522
- async getClosedPullRequests (projectId, options = {}) {
1523
- return await this.listPullRequests(projectId, { ...options, status: 'closed' })
1524
- }
1525
-
1526
- async getMergedPullRequests (projectId, options = {}) {
1527
- return await this.listPullRequests(projectId, { ...options, status: 'merged' })
1528
- }
1529
-
1530
- /**
1531
- * Helper method to check if a pull request is canMerge
1532
- */
1533
- async isPullRequestMergeable (projectId, prId) {
1534
- try {
1535
- const prData = await this.getPullRequest(projectId, prId)
1536
- return prData?.data?.canMerge || false
1537
- } catch (error) {
1538
- throw new Error(`Failed to check pull request mergeability: ${error.message}`)
1539
- }
1540
- }
1541
-
1542
- /**
1543
- * Helper method to get pull request status summary
1544
- */
1545
- async getPullRequestStatusSummary (projectId, prId) {
1546
- try {
1547
- const prData = await this.getPullRequest(projectId, prId)
1548
- const pr = prData?.data
1549
-
1550
- if (!pr) {
1551
- throw new Error('Pull request not found')
1552
- }
1553
-
1554
- return {
1555
- status: pr.status,
1556
- reviewStatus: pr.reviewStatus,
1557
- canMerge: pr.canMerge,
1558
- hasConflicts: !pr.canMerge,
1559
- reviewCount: pr.reviews?.length || 0,
1560
- approvedReviews: pr.reviews?.filter(r => r.status === 'approved').length || 0,
1561
- changesRequested: pr.reviews?.filter(r => r.status === 'requested_changes').length || 0,
1562
- }
1563
- } catch (error) {
1564
- throw new Error(`Failed to get pull request status summary: ${error.message}`)
1565
- }
1566
- }
1567
-
1568
- // ==================== BRANCH MANAGEMENT METHODS ====================
1569
-
1570
- /**
1571
- * Get all branches for a project
1572
- */
1573
- async listBranches (projectId) {
1574
- this._requireReady('listBranches')
1575
- if (!projectId) {
1576
- throw new Error('Project ID is required')
1577
- }
1578
-
1579
- try {
1580
- return await this._request(`/projects/${projectId}/branches`, {
1581
- method: 'GET',
1582
- methodName: 'listBranches'
1583
- })
1584
- } catch (error) {
1585
- throw new Error(`Failed to list branches: ${error.message}`)
1586
- }
1587
- }
1588
-
1589
- /**
1590
- * Create a new branch from an existing branch
1591
- */
1592
- async createBranch (projectId, branchData) {
1593
- this._requireReady('createBranch')
1594
- if (!projectId) {
1595
- throw new Error('Project ID is required')
1596
- }
1597
- if (!branchData.name) {
1598
- throw new Error('Branch name is required')
1599
- }
1600
-
1601
- const { name, source = 'main' } = branchData
1602
-
1603
- try {
1604
- return await this._request(`/projects/${projectId}/branches`, {
1605
- method: 'POST',
1606
- body: JSON.stringify({ name, source }),
1607
- methodName: 'createBranch'
1608
- })
1609
- } catch (error) {
1610
- throw new Error(`Failed to create branch: ${error.message}`)
1611
- }
1612
- }
1613
-
1614
- /**
1615
- * Delete a branch (cannot delete main branch)
1616
- */
1617
- async deleteBranch (projectId, branchName) {
1618
- this._requireReady('deleteBranch')
1619
- if (!projectId) {
1620
- throw new Error('Project ID is required')
1621
- }
1622
- if (!branchName) {
1623
- throw new Error('Branch name is required')
1624
- }
1625
- if (branchName === 'main') {
1626
- throw new Error('Cannot delete main branch')
1627
- }
1628
-
1629
- try {
1630
- return await this._request(`/projects/${projectId}/branches/${encodeURIComponent(branchName)}`, {
1631
- method: 'DELETE',
1632
- methodName: 'deleteBranch'
1633
- })
1634
- } catch (error) {
1635
- throw new Error(`Failed to delete branch: ${error.message}`)
1636
- }
1637
- }
1638
-
1639
- /**
1640
- * Rename a branch (cannot rename main branch)
1641
- */
1642
- async renameBranch (projectId, branchName, newName) {
1643
- this._requireReady('renameBranch')
1644
- if (!projectId) {
1645
- throw new Error('Project ID is required')
1646
- }
1647
- if (!branchName) {
1648
- throw new Error('Current branch name is required')
1649
- }
1650
- if (!newName) {
1651
- throw new Error('New branch name is required')
1652
- }
1653
- if (branchName === 'main') {
1654
- throw new Error('Cannot rename main branch')
1655
- }
1656
-
1657
- try {
1658
- return await this._request(`/projects/${projectId}/branches/${encodeURIComponent(branchName)}/rename`, {
1659
- method: 'POST',
1660
- body: JSON.stringify({ newName }),
1661
- methodName: 'renameBranch'
1662
- })
1663
- } catch (error) {
1664
- throw new Error(`Failed to rename branch: ${error.message}`)
1665
- }
1666
- }
1667
-
1668
- /**
1669
- * Get changes/diff for a branch compared to another version
1670
- */
1671
- async getBranchChanges (projectId, branchName, options = {}) {
1672
- this._requireReady('getBranchChanges')
1673
- if (!projectId) {
1674
- throw new Error('Project ID is required')
1675
- }
1676
- if (!branchName) {
1677
- throw new Error('Branch name is required')
1678
- }
1679
-
1680
- const { versionId, versionValue, target } = options
1681
- const queryParams = new URLSearchParams()
1682
-
1683
- if (versionId) {queryParams.append('versionId', versionId)}
1684
- if (versionValue) {queryParams.append('versionValue', versionValue)}
1685
- if (target) {queryParams.append('target', target)}
1686
-
1687
- const queryString = queryParams.toString()
1688
- const url = `/projects/${projectId}/branches/${encodeURIComponent(branchName)}/changes${queryString ? `?${queryString}` : ''}`
1689
-
1690
- try {
1691
- return await this._request(url, {
1692
- method: 'GET',
1693
- methodName: 'getBranchChanges'
1694
- })
1695
- } catch (error) {
1696
- throw new Error(`Failed to get branch changes: ${error.message}`)
1697
- }
1698
- }
1699
-
1700
- /**
1701
- * Merge changes between branches (preview or commit)
1702
- */
1703
- async mergeBranch (projectId, branchName, mergeData = {}) {
1704
- this._requireReady('mergeBranch')
1705
- if (!projectId) {
1706
- throw new Error('Project ID is required')
1707
- }
1708
- if (!branchName) {
1709
- throw new Error('Source branch name is required')
1710
- }
1711
-
1712
- const {
1713
- target = 'main',
1714
- message,
1715
- type = 'patch',
1716
- commit = false,
1717
- changes
1718
- } = mergeData
1719
-
1720
- const requestBody = {
1721
- target,
1722
- type,
1723
- commit,
1724
- ...(message && { message }),
1725
- ...(changes && { changes })
1726
- }
1727
-
1728
- try {
1729
- return await this._request(`/projects/${projectId}/branches/${encodeURIComponent(branchName)}/merge`, {
1730
- method: 'POST',
1731
- body: JSON.stringify(requestBody),
1732
- methodName: 'mergeBranch'
1733
- })
1734
- } catch (error) {
1735
- // Handle merge conflicts specifically
1736
- if (error.message.includes('conflicts') || error.message.includes('409')) {
1737
- throw new Error(`Merge conflicts detected: ${error.message}`)
1738
- }
1739
- throw new Error(`Failed to merge branch: ${error.message}`)
1740
- }
1741
- }
1742
-
1743
- /**
1744
- * Reset a branch to a clean state
1745
- */
1746
- async resetBranch (projectId, branchName) {
1747
- this._requireReady('resetBranch')
1748
- if (!projectId) {
1749
- throw new Error('Project ID is required')
1750
- }
1751
- if (!branchName) {
1752
- throw new Error('Branch name is required')
1753
- }
1754
-
1755
- try {
1756
- return await this._request(`/projects/${projectId}/branches/${encodeURIComponent(branchName)}/reset`, {
1757
- method: 'POST',
1758
- methodName: 'resetBranch'
1759
- })
1760
- } catch (error) {
1761
- throw new Error(`Failed to reset branch: ${error.message}`)
1762
- }
1763
- }
1764
-
1765
- /**
1766
- * Publish a specific version as the live version
1767
- */
1768
- async publishVersion (projectId, publishData) {
1769
- this._requireReady('publishVersion')
1770
- if (!projectId) {
1771
- throw new Error('Project ID is required')
1772
- }
1773
- if (!publishData.version) {
1774
- throw new Error('Version is required')
1775
- }
1776
-
1777
- const { version, branch = 'main' } = publishData
1778
-
1779
- try {
1780
- return await this._request(`/projects/${projectId}/publish`, {
1781
- method: 'POST',
1782
- body: JSON.stringify({ version, branch }),
1783
- methodName: 'publishVersion'
1784
- })
1785
- } catch (error) {
1786
- throw new Error(`Failed to publish version: ${error.message}`)
1787
- }
1788
- }
1789
-
1790
- // ==================== BRANCH HELPER METHODS ====================
1791
-
1792
- /**
1793
- * Helper method to create a branch with validation
1794
- */
1795
- async createBranchWithValidation (projectId, name, source = 'main') {
1796
- // Basic validation
1797
- if (!name || name.trim().length === 0) {
1798
- throw new Error('Branch name cannot be empty')
1799
- }
1800
-
1801
- if (name.includes(' ')) {
1802
- throw new Error('Branch name cannot contain spaces')
1803
- }
1804
-
1805
- if (name === 'main') {
1806
- throw new Error('Cannot create a branch named "main"')
1807
- }
1808
-
1809
- const sanitizedName = name.trim().toLowerCase().replace(/[^a-z0-9-_]/gu, '-')
1810
-
1811
- return await this.createBranch(projectId, {
1812
- name: sanitizedName,
1813
- source
1814
- })
1815
- }
1816
-
1817
- /**
1818
- * Helper method to check if a branch exists
1819
- */
1820
- async branchExists (projectId, branchName) {
1821
- try {
1822
- const branches = await this.listBranches(projectId)
1823
- return branches?.data?.includes(branchName) || false
1824
- } catch (error) {
1825
- throw new Error(`Failed to check if branch exists: ${error.message}`)
1826
- }
1827
- }
1828
-
1829
- /**
1830
- * Helper method to preview merge without committing
1831
- */
1832
- async previewMerge (projectId, sourceBranch, targetBranch = 'main') {
1833
- return await this.mergeBranch(projectId, sourceBranch, {
1834
- target: targetBranch,
1835
- commit: false
1836
- })
1837
- }
1838
-
1839
- /**
1840
- * Helper method to commit merge after preview
1841
- */
1842
- async commitMerge (projectId, sourceBranch, options = {}) {
1843
- const {
1844
- target = 'main',
1845
- message = `Merge ${sourceBranch} into ${target}`,
1846
- type = 'patch',
1847
- changes
1848
- } = options
1849
-
1850
- return await this.mergeBranch(projectId, sourceBranch, {
1851
- target,
1852
- message,
1853
- type,
1854
- commit: true,
1855
- changes
1856
- })
1857
- }
1858
-
1859
- /**
1860
- * Helper method to create a feature branch from main
1861
- */
1862
- async createFeatureBranch (projectId, featureName) {
1863
- const branchName = `feature/${featureName.toLowerCase().replace(/[^a-z0-9-]/gu, '-')}`
1864
-
1865
- return await this.createBranch(projectId, {
1866
- name: branchName,
1867
- source: 'main'
1868
- })
1869
- }
1870
-
1871
- /**
1872
- * Helper method to create a hotfix branch from main
1873
- */
1874
- async createHotfixBranch (projectId, hotfixName) {
1875
- const branchName = `hotfix/${hotfixName.toLowerCase().replace(/[^a-z0-9-]/gu, '-')}`
1876
-
1877
- return await this.createBranch(projectId, {
1878
- name: branchName,
1879
- source: 'main'
1880
- })
1881
- }
1882
-
1883
- /**
1884
- * Helper method to get branch status summary
1885
- */
1886
- async getBranchStatus (projectId, branchName) {
1887
- try {
1888
- const [branches, changes] = await Promise.all([
1889
- this.listBranches(projectId),
1890
- this.getBranchChanges(projectId, branchName).catch(() => null)
1891
- ])
1892
-
1893
- const exists = branches?.data?.includes(branchName) || false
1894
- const hasChanges = changes?.data?.length > 0
1895
-
1896
- return {
1897
- exists,
1898
- hasChanges,
1899
- changeCount: changes?.data?.length || 0,
1900
- canDelete: exists && branchName !== 'main',
1901
- canRename: exists && branchName !== 'main'
1902
- }
1903
- } catch (error) {
1904
- throw new Error(`Failed to get branch status: ${error.message}`)
1905
- }
1906
- }
1907
-
1908
- /**
1909
- * Helper method to safely delete a branch with confirmation
1910
- */
1911
- async deleteBranchSafely (projectId, branchName, options = {}) {
1912
- const { force = false } = options
1913
-
1914
- if (!force) {
1915
- const status = await this.getBranchStatus(projectId, branchName)
1916
-
1917
- if (!status.exists) {
1918
- throw new Error(`Branch '${branchName}' does not exist`)
1919
- }
1920
-
1921
- if (!status.canDelete) {
1922
- throw new Error(`Branch '${branchName}' cannot be deleted`)
1923
- }
1924
-
1925
- if (status.hasChanges) {
1926
- throw new Error(`Branch '${branchName}' has uncommitted changes. Use force option to delete anyway.`)
1927
- }
1928
- }
1929
-
1930
- return await this.deleteBranch(projectId, branchName)
1931
- }
1932
-
1933
- // Cleanup
1934
- destroy () {
1935
- if (this._tokenManager) {
1936
- this._tokenManager.destroy()
1937
- this._tokenManager = null
1938
- }
1939
- this._client = null
1940
- this._initialized = false
1941
- this._setReady(false)
1942
- }
1943
- }