@symbo.ls/sdk 2.32.11 → 2.32.13

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