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