@symbo.ls/sdk 3.1.2 → 3.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +143 -2
  2. package/dist/cjs/config/environment.js +98 -30
  3. package/dist/cjs/index.js +144 -24
  4. package/dist/cjs/services/AdminService.js +351 -0
  5. package/dist/cjs/services/AuthService.js +738 -305
  6. package/dist/cjs/services/BaseService.js +158 -6
  7. package/dist/cjs/services/BranchService.js +484 -0
  8. package/dist/cjs/services/CollabService.js +743 -0
  9. package/dist/cjs/services/DnsService.js +340 -0
  10. package/dist/cjs/services/FeatureFlagService.js +175 -0
  11. package/dist/cjs/services/FileService.js +201 -0
  12. package/dist/cjs/services/IntegrationService.js +538 -0
  13. package/dist/cjs/services/MetricsService.js +62 -0
  14. package/dist/cjs/services/PaymentService.js +271 -0
  15. package/dist/cjs/services/PlanService.js +426 -0
  16. package/dist/cjs/services/ProjectService.js +1207 -0
  17. package/dist/cjs/services/PullRequestService.js +503 -0
  18. package/dist/cjs/services/ScreenshotService.js +304 -0
  19. package/dist/cjs/services/SubscriptionService.js +396 -0
  20. package/dist/cjs/services/TrackingService.js +661 -0
  21. package/dist/cjs/services/WaitlistService.js +148 -0
  22. package/dist/cjs/services/index.js +64 -16
  23. package/dist/cjs/state/RootStateManager.js +65 -0
  24. package/dist/cjs/state/rootEventBus.js +74 -0
  25. package/dist/cjs/utils/CollabClient.js +223 -0
  26. package/dist/cjs/utils/TokenManager.js +78 -30
  27. package/dist/cjs/utils/changePreprocessor.js +199 -0
  28. package/dist/cjs/utils/jsonDiff.js +145 -0
  29. package/dist/cjs/utils/ordering.js +309 -0
  30. package/dist/cjs/utils/services.js +301 -103
  31. package/dist/cjs/utils/validation.js +0 -3
  32. package/dist/esm/config/environment.js +98 -30
  33. package/dist/esm/index.js +49505 -8718
  34. package/dist/esm/services/AdminService.js +1132 -0
  35. package/dist/esm/services/AuthService.js +1493 -386
  36. package/dist/esm/services/BaseService.js +757 -6
  37. package/dist/esm/services/BranchService.js +1265 -0
  38. package/dist/esm/services/CollabService.js +26895 -0
  39. package/dist/esm/services/DnsService.js +1121 -0
  40. package/dist/esm/services/FeatureFlagService.js +956 -0
  41. package/dist/esm/services/FileService.js +982 -0
  42. package/dist/esm/services/IntegrationService.js +1319 -0
  43. package/dist/esm/services/MetricsService.js +843 -0
  44. package/dist/esm/services/PaymentService.js +1052 -0
  45. package/dist/esm/services/PlanService.js +1207 -0
  46. package/dist/esm/services/ProjectService.js +2526 -0
  47. package/dist/esm/services/PullRequestService.js +1284 -0
  48. package/dist/esm/services/ScreenshotService.js +1085 -0
  49. package/dist/esm/services/SubscriptionService.js +1177 -0
  50. package/dist/esm/services/TrackingService.js +18454 -0
  51. package/dist/esm/services/WaitlistService.js +929 -0
  52. package/dist/esm/services/index.js +49062 -8569
  53. package/dist/esm/state/RootStateManager.js +90 -0
  54. package/dist/esm/state/rootEventBus.js +56 -0
  55. package/dist/esm/utils/CollabClient.js +18889 -0
  56. package/dist/esm/utils/TokenManager.js +78 -30
  57. package/dist/esm/utils/changePreprocessor.js +542 -0
  58. package/dist/esm/utils/jsonDiff.js +7011 -0
  59. package/dist/esm/utils/ordering.js +291 -0
  60. package/dist/esm/utils/services.js +301 -103
  61. package/dist/esm/utils/validation.js +116 -50
  62. package/dist/node/config/environment.js +98 -30
  63. package/dist/node/index.js +175 -32
  64. package/dist/node/services/AdminService.js +332 -0
  65. package/dist/node/services/AuthService.js +742 -310
  66. package/dist/node/services/BaseService.js +148 -6
  67. package/dist/node/services/BranchService.js +465 -0
  68. package/dist/node/services/CollabService.js +724 -0
  69. package/dist/node/services/DnsService.js +321 -0
  70. package/dist/node/services/FeatureFlagService.js +156 -0
  71. package/dist/node/services/FileService.js +182 -0
  72. package/dist/node/services/IntegrationService.js +519 -0
  73. package/dist/node/services/MetricsService.js +43 -0
  74. package/dist/node/services/PaymentService.js +252 -0
  75. package/dist/node/services/PlanService.js +407 -0
  76. package/dist/node/services/ProjectService.js +1188 -0
  77. package/dist/node/services/PullRequestService.js +484 -0
  78. package/dist/node/services/ScreenshotService.js +285 -0
  79. package/dist/node/services/SubscriptionService.js +377 -0
  80. package/dist/node/services/TrackingService.js +632 -0
  81. package/dist/node/services/WaitlistService.js +129 -0
  82. package/dist/node/services/index.js +64 -16
  83. package/dist/node/state/RootStateManager.js +36 -0
  84. package/dist/node/state/rootEventBus.js +55 -0
  85. package/dist/node/utils/CollabClient.js +194 -0
  86. package/dist/node/utils/TokenManager.js +78 -30
  87. package/dist/node/utils/changePreprocessor.js +180 -0
  88. package/dist/node/utils/jsonDiff.js +116 -0
  89. package/dist/node/utils/ordering.js +290 -0
  90. package/dist/node/utils/services.js +301 -103
  91. package/dist/node/utils/validation.js +0 -3
  92. package/package.json +39 -21
  93. package/src/config/environment.js +99 -28
  94. package/src/index.js +181 -36
  95. package/src/services/AdminService.js +374 -0
  96. package/src/services/AuthService.js +874 -328
  97. package/src/services/BaseService.js +166 -6
  98. package/src/services/BranchService.js +536 -0
  99. package/src/services/CollabService.js +900 -0
  100. package/src/services/DnsService.js +366 -0
  101. package/src/services/FeatureFlagService.js +174 -0
  102. package/src/services/FileService.js +213 -0
  103. package/src/services/IntegrationService.js +548 -0
  104. package/src/services/MetricsService.js +40 -0
  105. package/src/services/PaymentService.js +287 -0
  106. package/src/services/PlanService.js +468 -0
  107. package/src/services/ProjectService.js +1366 -0
  108. package/src/services/PullRequestService.js +537 -0
  109. package/src/services/ScreenshotService.js +258 -0
  110. package/src/services/SubscriptionService.js +425 -0
  111. package/src/services/TrackingService.js +853 -0
  112. package/src/services/WaitlistService.js +130 -0
  113. package/src/services/index.js +80 -13
  114. package/src/services/tests/BranchService/createBranch.test.js +153 -0
  115. package/src/services/tests/BranchService/deleteBranch.test.js +173 -0
  116. package/src/services/tests/BranchService/getBranchChanges.test.js +146 -0
  117. package/src/services/tests/BranchService/listBranches.test.js +87 -0
  118. package/src/services/tests/BranchService/mergeBranch.test.js +210 -0
  119. package/src/services/tests/BranchService/publishVersion.test.js +183 -0
  120. package/src/services/tests/BranchService/renameBranch.test.js +240 -0
  121. package/src/services/tests/BranchService/resetBranch.test.js +152 -0
  122. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +67 -0
  123. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +75 -0
  124. package/src/services/tests/FileService/createFileFormData.test.js +74 -0
  125. package/src/services/tests/FileService/getFileUrl.test.js +69 -0
  126. package/src/services/tests/FileService/updateProjectIcon.test.js +109 -0
  127. package/src/services/tests/FileService/uploadDocument.test.js +36 -0
  128. package/src/services/tests/FileService/uploadFile.test.js +78 -0
  129. package/src/services/tests/FileService/uploadFileWithValidation.test.js +114 -0
  130. package/src/services/tests/FileService/uploadImage.test.js +36 -0
  131. package/src/services/tests/FileService/uploadMultipleFiles.test.js +111 -0
  132. package/src/services/tests/FileService/validateFile.test.js +63 -0
  133. package/src/services/tests/PlanService/createPlan.test.js +104 -0
  134. package/src/services/tests/PlanService/createPlanWithValidation.test.js +523 -0
  135. package/src/services/tests/PlanService/deletePlan.test.js +92 -0
  136. package/src/services/tests/PlanService/getActivePlans.test.js +123 -0
  137. package/src/services/tests/PlanService/getAdminPlans.test.js +84 -0
  138. package/src/services/tests/PlanService/getPlan.test.js +50 -0
  139. package/src/services/tests/PlanService/getPlanByKey.test.js +109 -0
  140. package/src/services/tests/PlanService/getPlanWithValidation.test.js +85 -0
  141. package/src/services/tests/PlanService/getPlans.test.js +53 -0
  142. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +109 -0
  143. package/src/services/tests/PlanService/getPlansWithValidation.test.js +48 -0
  144. package/src/services/tests/PlanService/initializePlans.test.js +75 -0
  145. package/src/services/tests/PlanService/updatePlan.test.js +111 -0
  146. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +556 -0
  147. package/src/state/RootStateManager.js +76 -0
  148. package/src/state/rootEventBus.js +67 -0
  149. package/src/utils/CollabClient.js +248 -0
  150. package/src/utils/TokenManager.js +88 -33
  151. package/src/utils/changePreprocessor.js +239 -0
  152. package/src/utils/jsonDiff.js +144 -0
  153. package/src/utils/ordering.js +271 -0
  154. package/src/utils/services.js +326 -107
  155. package/src/utils/validation.js +0 -3
  156. package/dist/cjs/services/AIService.js +0 -155
  157. package/dist/cjs/services/BasedService.js +0 -1185
  158. package/dist/cjs/services/CoreService.js +0 -1751
  159. package/dist/cjs/services/SocketIOService.js +0 -307
  160. package/dist/cjs/services/SocketService.js +0 -161
  161. package/dist/cjs/services/SymstoryService.js +0 -571
  162. package/dist/cjs/utils/basedQuerys.js +0 -181
  163. package/dist/cjs/utils/symstoryClient.js +0 -259
  164. package/dist/esm/services/AIService.js +0 -185
  165. package/dist/esm/services/BasedService.js +0 -5278
  166. package/dist/esm/services/CoreService.js +0 -2264
  167. package/dist/esm/services/SocketIOService.js +0 -470
  168. package/dist/esm/services/SocketService.js +0 -191
  169. package/dist/esm/services/SymstoryService.js +0 -7041
  170. package/dist/esm/utils/basedQuerys.js +0 -163
  171. package/dist/esm/utils/symstoryClient.js +0 -370
  172. package/dist/node/services/AIService.js +0 -136
  173. package/dist/node/services/BasedService.js +0 -1156
  174. package/dist/node/services/CoreService.js +0 -1722
  175. package/dist/node/services/SocketIOService.js +0 -278
  176. package/dist/node/services/SocketService.js +0 -142
  177. package/dist/node/services/SymstoryService.js +0 -542
  178. package/dist/node/utils/basedQuerys.js +0 -162
  179. package/dist/node/utils/symstoryClient.js +0 -230
  180. package/src/services/AIService.js +0 -150
  181. package/src/services/BasedService.js +0 -1301
  182. package/src/services/CoreService.js +0 -1943
  183. package/src/services/SocketIOService.js +0 -334
  184. package/src/services/SocketService.js +0 -168
  185. package/src/services/SymstoryService.js +0 -649
  186. package/src/utils/basedQuerys.js +0 -164
  187. package/src/utils/symstoryClient.js +0 -252
@@ -0,0 +1,304 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var ScreenshotService_exports = {};
19
+ __export(ScreenshotService_exports, {
20
+ ScreenshotService: () => ScreenshotService,
21
+ default: () => ScreenshotService_default
22
+ });
23
+ module.exports = __toCommonJS(ScreenshotService_exports);
24
+ var import_BaseService = require("./BaseService.js");
25
+ class ScreenshotService extends import_BaseService.BaseService {
26
+ constructor(config) {
27
+ super(config);
28
+ this._debounceTimers = /* @__PURE__ */ new Map();
29
+ this._inflightRefreshes = /* @__PURE__ */ new Map();
30
+ }
31
+ // ==================== PROJECT-LEVEL OPERATIONS ====================
32
+ async createScreenshotProject(payload) {
33
+ this._requireReady("createScreenshotProject");
34
+ try {
35
+ const response = await this._request("/screenshots/projects", {
36
+ method: "POST",
37
+ body: JSON.stringify(payload),
38
+ methodName: "createScreenshotProject"
39
+ });
40
+ if (response.success) {
41
+ return response;
42
+ }
43
+ throw new Error(response.message);
44
+ } catch (error) {
45
+ throw new Error(`Failed to create screenshot project: ${error.message}`, { cause: error });
46
+ }
47
+ }
48
+ async getProjectScreenshots(projectKey, params = {}) {
49
+ this._requireReady("getProjectScreenshots");
50
+ if (!projectKey) {
51
+ throw new Error("projectKey is required");
52
+ }
53
+ const { type = "all", status, limit = 50, offset = 0 } = params;
54
+ const qs = new URLSearchParams();
55
+ if (type) {
56
+ qs.set("type", type);
57
+ }
58
+ if (status) {
59
+ qs.set("status", status);
60
+ }
61
+ if (limit != null) {
62
+ qs.set("limit", String(limit));
63
+ }
64
+ if (offset != null) {
65
+ qs.set("offset", String(offset));
66
+ }
67
+ try {
68
+ const response = await this._request(
69
+ `/screenshots/projects/${encodeURIComponent(projectKey)}${qs.toString() ? `?${qs.toString()}` : ""}`,
70
+ { method: "GET", methodName: "getProjectScreenshots" }
71
+ );
72
+ if (response.success) {
73
+ return response;
74
+ }
75
+ throw new Error(response.message);
76
+ } catch (error) {
77
+ throw new Error(`Failed to get project screenshots: ${error.message}`, { cause: error });
78
+ }
79
+ }
80
+ async reprocessProjectScreenshots(projectKey, body = {}) {
81
+ this._requireReady("reprocessProjectScreenshots");
82
+ if (!projectKey) {
83
+ throw new Error("projectKey is required");
84
+ }
85
+ try {
86
+ const response = await this._request(
87
+ `/screenshots/projects/${encodeURIComponent(projectKey)}/reprocess`,
88
+ { method: "POST", body: JSON.stringify(body), methodName: "reprocessProjectScreenshots" }
89
+ );
90
+ if (response.success) {
91
+ return response;
92
+ }
93
+ throw new Error(response.message);
94
+ } catch (error) {
95
+ throw new Error(`Failed to reprocess screenshots: ${error.message}`, { cause: error });
96
+ }
97
+ }
98
+ async recreateProjectScreenshots(projectKey, body = {}) {
99
+ this._requireReady("recreateProjectScreenshots");
100
+ if (!projectKey) {
101
+ throw new Error("projectKey is required");
102
+ }
103
+ try {
104
+ const response = await this._request(
105
+ `/screenshots/projects/${encodeURIComponent(projectKey)}/recreate`,
106
+ { method: "POST", body: JSON.stringify(body), methodName: "recreateProjectScreenshots" }
107
+ );
108
+ if (response.success) {
109
+ return response;
110
+ }
111
+ throw new Error(response.message);
112
+ } catch (error) {
113
+ throw new Error(`Failed to recreate screenshots: ${error.message}`, { cause: error });
114
+ }
115
+ }
116
+ async deleteProjectScreenshots(projectKey) {
117
+ this._requireReady("deleteProjectScreenshots");
118
+ if (!projectKey) {
119
+ throw new Error("projectKey is required");
120
+ }
121
+ try {
122
+ const response = await this._request(
123
+ `/screenshots/projects/${encodeURIComponent(projectKey)}`,
124
+ { method: "DELETE", methodName: "deleteProjectScreenshots" }
125
+ );
126
+ if (response.success) {
127
+ return response;
128
+ }
129
+ throw new Error(response.message);
130
+ } catch (error) {
131
+ throw new Error(`Failed to delete project screenshots: ${error.message}`, { cause: error });
132
+ }
133
+ }
134
+ // ==================== THUMBNAIL ====================
135
+ async getThumbnailCandidate(projectKey, options = {}) {
136
+ this._requireReady("getThumbnailCandidate");
137
+ if (!projectKey) {
138
+ throw new Error("projectKey is required");
139
+ }
140
+ const { includeData = false } = options;
141
+ const qs = new URLSearchParams();
142
+ if (includeData) {
143
+ qs.set("include_data", "true");
144
+ }
145
+ try {
146
+ const response = await this._request(
147
+ `/screenshots/projects/${encodeURIComponent(projectKey)}/thumbnail/candidate${qs.toString() ? `?${qs.toString()}` : ""}`,
148
+ { method: "GET", methodName: "getThumbnailCandidate" }
149
+ );
150
+ if (response.success) {
151
+ return response;
152
+ }
153
+ throw new Error(response.message);
154
+ } catch (error) {
155
+ throw new Error(`Failed to get thumbnail candidate: ${error.message}`, { cause: error });
156
+ }
157
+ }
158
+ async updateProjectThumbnail(projectKey, body = {}) {
159
+ this._requireReady("updateProjectThumbnail");
160
+ if (!projectKey) {
161
+ throw new Error("projectKey is required");
162
+ }
163
+ try {
164
+ const response = await this._request(
165
+ `/screenshots/projects/${encodeURIComponent(projectKey)}/thumbnail`,
166
+ { method: "POST", body: JSON.stringify(body), methodName: "updateProjectThumbnail" }
167
+ );
168
+ if (response.success) {
169
+ return response;
170
+ }
171
+ throw new Error(response.message);
172
+ } catch (error) {
173
+ throw new Error(`Failed to update project thumbnail: ${error.message}`, { cause: error });
174
+ }
175
+ }
176
+ // ==================== INDIVIDUAL SHOTS ====================
177
+ async getPageScreenshot(screenshotId, format = "json") {
178
+ this._requireReady("getPageScreenshot");
179
+ if (!screenshotId) {
180
+ throw new Error("screenshotId is required");
181
+ }
182
+ const qs = new URLSearchParams();
183
+ if (format) {
184
+ qs.set("format", format);
185
+ }
186
+ try {
187
+ return await this._request(
188
+ `/screenshots/pages/${encodeURIComponent(screenshotId)}${qs.toString() ? `?${qs.toString()}` : ""}`,
189
+ { method: "GET", methodName: "getPageScreenshot" }
190
+ );
191
+ } catch (error) {
192
+ throw new Error(`Failed to get page screenshot: ${error.message}`, { cause: error });
193
+ }
194
+ }
195
+ async getComponentScreenshot(screenshotId, format = "json") {
196
+ this._requireReady("getComponentScreenshot");
197
+ if (!screenshotId) {
198
+ throw new Error("screenshotId is required");
199
+ }
200
+ const qs = new URLSearchParams();
201
+ if (format) {
202
+ qs.set("format", format);
203
+ }
204
+ try {
205
+ return await this._request(
206
+ `/screenshots/components/${encodeURIComponent(screenshotId)}${qs.toString() ? `?${qs.toString()}` : ""}`,
207
+ { method: "GET", methodName: "getComponentScreenshot" }
208
+ );
209
+ } catch (error) {
210
+ throw new Error(`Failed to get component screenshot: ${error.message}`, { cause: error });
211
+ }
212
+ }
213
+ async getScreenshotByKey(projectKey, type, key, format = "json") {
214
+ this._requireReady("getScreenshotByKey");
215
+ if (!projectKey) {
216
+ throw new Error("projectKey is required");
217
+ }
218
+ if (!type || !["component", "page"].includes(String(type))) {
219
+ throw new Error("type must be 'component' or 'page'");
220
+ }
221
+ if (!key) {
222
+ throw new Error("key is required");
223
+ }
224
+ const qs = new URLSearchParams();
225
+ if (format) {
226
+ qs.set("format", format);
227
+ }
228
+ const sub = type === "component" ? "components" : "pages";
229
+ try {
230
+ return await this._request(
231
+ `/screenshots/projects/${encodeURIComponent(projectKey)}/${sub}/${encodeURIComponent(key)}${qs.toString() ? `?${qs.toString()}` : ""}`,
232
+ { method: "GET", methodName: "getScreenshotByKey" }
233
+ );
234
+ } catch (error) {
235
+ throw new Error(`Failed to get screenshot by key: ${error.message}`, { cause: error });
236
+ }
237
+ }
238
+ async getQueueStatistics() {
239
+ this._requireReady("getQueueStatistics");
240
+ try {
241
+ const response = await this._request("/screenshots/queue/stats", {
242
+ method: "GET",
243
+ methodName: "getQueueStatistics"
244
+ });
245
+ if (response.success) {
246
+ return response;
247
+ }
248
+ throw new Error(response.message);
249
+ } catch (error) {
250
+ throw new Error(`Failed to get queue statistics: ${error.message}`, { cause: error });
251
+ }
252
+ }
253
+ // ==================== COMBINATION/DEBOUNCED ====================
254
+ /**
255
+ * Debounced thumbnail refresh that recreates screenshots and then updates thumbnail.
256
+ * Subsequent calls within debounce window reset the timer.
257
+ */
258
+ async refreshThumbnail(projectKey, options = {}) {
259
+ this._requireReady("refreshThumbnail");
260
+ if (!projectKey) {
261
+ throw new Error("projectKey is required");
262
+ }
263
+ const {
264
+ debounceMs = 15e3,
265
+ waitAfterRecreateMs = 2e4,
266
+ recreate = {
267
+ process_pages: true,
268
+ process_components: false,
269
+ process_descriptions: false,
270
+ force: false,
271
+ priority: 5
272
+ },
273
+ thumbnail = {
274
+ strategy: "auto",
275
+ force: true
276
+ }
277
+ } = options;
278
+ const existingTimer = this._debounceTimers.get(projectKey);
279
+ if (existingTimer) {
280
+ clearTimeout(existingTimer);
281
+ }
282
+ const executionPromise = await new Promise((resolve) => {
283
+ const timer = setTimeout(async () => {
284
+ try {
285
+ await this.recreateProjectScreenshots(projectKey, recreate);
286
+ await new Promise((resolveDelay) => {
287
+ setTimeout(resolveDelay, waitAfterRecreateMs);
288
+ });
289
+ const result = await this.updateProjectThumbnail(projectKey, thumbnail);
290
+ resolve(result);
291
+ } catch (e) {
292
+ resolve({ success: false, error: (e == null ? void 0 : e.message) || String(e) });
293
+ } finally {
294
+ this._debounceTimers.delete(projectKey);
295
+ this._inflightRefreshes.delete(projectKey);
296
+ }
297
+ }, debounceMs);
298
+ this._debounceTimers.set(projectKey, timer);
299
+ });
300
+ this._inflightRefreshes.set(projectKey, executionPromise);
301
+ return executionPromise;
302
+ }
303
+ }
304
+ var ScreenshotService_default = ScreenshotService;
@@ -0,0 +1,396 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var SubscriptionService_exports = {};
19
+ __export(SubscriptionService_exports, {
20
+ SubscriptionService: () => SubscriptionService
21
+ });
22
+ module.exports = __toCommonJS(SubscriptionService_exports);
23
+ var import_BaseService = require("./BaseService.js");
24
+ class SubscriptionService extends import_BaseService.BaseService {
25
+ // ==================== SUBSCRIPTION METHODS ====================
26
+ /**
27
+ * Create a new subscription checkout session
28
+ */
29
+ async createSubscription(subscriptionData) {
30
+ this._requireReady("createSubscription");
31
+ if (!subscriptionData || typeof subscriptionData !== "object") {
32
+ throw new Error("Subscription data is required");
33
+ }
34
+ const { projectId, planId, pricingKey = "monthly", seats = 1, successUrl, cancelUrl } = subscriptionData;
35
+ if (!projectId) {
36
+ throw new Error("Project ID is required");
37
+ }
38
+ if (!planId) {
39
+ throw new Error("Plan ID is required");
40
+ }
41
+ try {
42
+ const response = await this._request("/subscriptions", {
43
+ method: "POST",
44
+ body: JSON.stringify({
45
+ projectId,
46
+ planId,
47
+ pricingKey,
48
+ seats,
49
+ successUrl,
50
+ cancelUrl
51
+ }),
52
+ methodName: "createSubscription"
53
+ });
54
+ if (response.success) {
55
+ return response.data;
56
+ }
57
+ throw new Error(response.message);
58
+ } catch (error) {
59
+ throw new Error(`Failed to create subscription: ${error.message}`, { cause: error });
60
+ }
61
+ }
62
+ /**
63
+ * Get subscription status and usage for a project
64
+ */
65
+ async getProjectStatus(projectId) {
66
+ this._requireReady("getProjectStatus");
67
+ if (!projectId) {
68
+ throw new Error("Project ID is required");
69
+ }
70
+ try {
71
+ const response = await this._request(`/subscriptions/project/${projectId}`, {
72
+ method: "GET",
73
+ methodName: "getProjectStatus"
74
+ });
75
+ if (response.success) {
76
+ return response.data;
77
+ }
78
+ throw new Error(response.message);
79
+ } catch (error) {
80
+ throw new Error(`Failed to get project subscription status: ${error.message}`, { cause: error });
81
+ }
82
+ }
83
+ /**
84
+ * Get usage data for a subscription
85
+ */
86
+ async getUsage(subscriptionId) {
87
+ this._requireReady("getUsage");
88
+ if (!subscriptionId) {
89
+ throw new Error("Subscription ID is required");
90
+ }
91
+ try {
92
+ const response = await this._request(`/subscriptions/${subscriptionId}/usage`, {
93
+ method: "GET",
94
+ methodName: "getUsage"
95
+ });
96
+ if (response.success) {
97
+ return response.data;
98
+ }
99
+ throw new Error(response.message);
100
+ } catch (error) {
101
+ throw new Error(`Failed to get subscription usage: ${error.message}`, { cause: error });
102
+ }
103
+ }
104
+ /**
105
+ * Cancel a subscription
106
+ */
107
+ async cancelSubscription(subscriptionId) {
108
+ this._requireReady("cancelSubscription");
109
+ if (!subscriptionId) {
110
+ throw new Error("Subscription ID is required");
111
+ }
112
+ try {
113
+ const response = await this._request(`/subscriptions/${subscriptionId}/cancel`, {
114
+ method: "POST",
115
+ methodName: "cancelSubscription"
116
+ });
117
+ if (response.success) {
118
+ return response.data;
119
+ }
120
+ throw new Error(response.message);
121
+ } catch (error) {
122
+ throw new Error(`Failed to cancel subscription: ${error.message}`, { cause: error });
123
+ }
124
+ }
125
+ /**
126
+ * List invoices for a subscription
127
+ */
128
+ async listInvoices(subscriptionId, options = {}) {
129
+ this._requireReady("listInvoices");
130
+ if (!subscriptionId) {
131
+ throw new Error("Subscription ID is required");
132
+ }
133
+ const { page = 1, limit = 20, status, startingAfter } = options;
134
+ try {
135
+ const queryParams = new URLSearchParams();
136
+ if (page) {
137
+ queryParams.append("page", page.toString());
138
+ }
139
+ if (limit) {
140
+ queryParams.append("limit", limit.toString());
141
+ }
142
+ if (status) {
143
+ queryParams.append("status", status);
144
+ }
145
+ if (startingAfter) {
146
+ queryParams.append("startingAfter", startingAfter);
147
+ }
148
+ const queryString = queryParams.toString();
149
+ const url = `/subscriptions/${subscriptionId}/invoices${queryString ? `?${queryString}` : ""}`;
150
+ const response = await this._request(url, {
151
+ method: "GET",
152
+ methodName: "listInvoices"
153
+ });
154
+ if (response.success) {
155
+ return response.data;
156
+ }
157
+ throw new Error(response.message);
158
+ } catch (error) {
159
+ throw new Error(`Failed to list invoices: ${error.message}`, { cause: error });
160
+ }
161
+ }
162
+ /**
163
+ * Get customer portal URL for a subscription
164
+ */
165
+ async getPortalUrl(subscriptionId, returnUrl) {
166
+ this._requireReady("getPortalUrl");
167
+ if (!subscriptionId) {
168
+ throw new Error("Subscription ID is required");
169
+ }
170
+ try {
171
+ const queryParams = new URLSearchParams();
172
+ if (returnUrl) {
173
+ queryParams.append("returnUrl", returnUrl);
174
+ }
175
+ const queryString = queryParams.toString();
176
+ const url = `/subscriptions/${subscriptionId}/portal${queryString ? `?${queryString}` : ""}`;
177
+ const response = await this._request(url, {
178
+ method: "GET",
179
+ methodName: "getPortalUrl"
180
+ });
181
+ if (response.success) {
182
+ return response.data;
183
+ }
184
+ throw new Error(response.message);
185
+ } catch (error) {
186
+ throw new Error(`Failed to get portal URL: ${error.message}`, { cause: error });
187
+ }
188
+ }
189
+ // ==================== SUBSCRIPTION HELPER METHODS ====================
190
+ /**
191
+ * Helper method to create subscription with validation
192
+ */
193
+ async createSubscriptionWithValidation(subscriptionData) {
194
+ if (!subscriptionData || typeof subscriptionData !== "object") {
195
+ throw new Error("Subscription data must be a valid object");
196
+ }
197
+ const requiredFields = ["projectId", "planId"];
198
+ for (const field of requiredFields) {
199
+ if (!subscriptionData[field]) {
200
+ throw new Error(`Required field '${field}' is missing`);
201
+ }
202
+ }
203
+ if (subscriptionData.seats != null) {
204
+ if (!Number.isInteger(subscriptionData.seats) || subscriptionData.seats < 1) {
205
+ throw new Error("Seats must be a positive integer");
206
+ }
207
+ }
208
+ if (subscriptionData.pricingKey && !["monthly", "yearly"].includes(subscriptionData.pricingKey)) {
209
+ throw new Error('Pricing key must be either "monthly" or "yearly"');
210
+ }
211
+ return await this.createSubscription(subscriptionData);
212
+ }
213
+ /**
214
+ * Helper method to check if project has active subscription
215
+ */
216
+ async hasActiveSubscription(projectId) {
217
+ try {
218
+ const status = await this.getProjectStatus(projectId);
219
+ return status.hasSubscription === true;
220
+ } catch (error) {
221
+ throw new Error(`Failed to check subscription status: ${error.message}`, { cause: error });
222
+ }
223
+ }
224
+ /**
225
+ * Helper method to get subscription details for a project
226
+ */
227
+ async getProjectSubscription(projectId) {
228
+ try {
229
+ const status = await this.getProjectStatus(projectId);
230
+ if (!status.hasSubscription) {
231
+ return null;
232
+ }
233
+ return status.subscription;
234
+ } catch (error) {
235
+ throw new Error(`Failed to get project subscription: ${error.message}`, { cause: error });
236
+ }
237
+ }
238
+ /**
239
+ * Helper method to get usage with limits for a project
240
+ */
241
+ async getProjectUsage(projectId) {
242
+ try {
243
+ const status = await this.getProjectStatus(projectId);
244
+ if (!status.hasSubscription) {
245
+ return null;
246
+ }
247
+ return status.usage;
248
+ } catch (error) {
249
+ throw new Error(`Failed to get project usage: ${error.message}`, { cause: error });
250
+ }
251
+ }
252
+ /**
253
+ * Helper method to get invoices with pagination
254
+ */
255
+ async getInvoicesWithPagination(subscriptionId, options = {}) {
256
+ try {
257
+ const result = await this.listInvoices(subscriptionId, options);
258
+ return {
259
+ invoices: result.data || [],
260
+ pagination: result.pagination || {}
261
+ };
262
+ } catch (error) {
263
+ throw new Error(`Failed to get invoices with pagination: ${error.message}`, { cause: error });
264
+ }
265
+ }
266
+ /**
267
+ * Helper method to check if subscription is active
268
+ */
269
+ async isSubscriptionActive(subscriptionId) {
270
+ try {
271
+ const usage = await this.getUsage(subscriptionId);
272
+ return usage && usage.subscription && usage.subscription.status === "active";
273
+ } catch (error) {
274
+ throw new Error(`Failed to check subscription status: ${error.message}`, { cause: error });
275
+ }
276
+ }
277
+ /**
278
+ * Helper method to get subscription limits
279
+ */
280
+ async getSubscriptionLimits(subscriptionId) {
281
+ try {
282
+ const usage = await this.getUsage(subscriptionId);
283
+ return usage.limits || {};
284
+ } catch (error) {
285
+ throw new Error(`Failed to get subscription limits: ${error.message}`, { cause: error });
286
+ }
287
+ }
288
+ /**
289
+ * Change subscription (unified endpoint for all pricing changes)
290
+ */
291
+ async changeSubscription(changeData) {
292
+ this._requireReady("changeSubscription");
293
+ if (!changeData || typeof changeData !== "object") {
294
+ throw new Error("Change data is required");
295
+ }
296
+ const { subscriptionId, planId, pricingKey, seats = 1, projectId, successUrl, cancelUrl } = changeData;
297
+ if (!subscriptionId) {
298
+ throw new Error("Subscription ID is required");
299
+ }
300
+ if (!pricingKey) {
301
+ throw new Error("Pricing key is required");
302
+ }
303
+ try {
304
+ const response = await this._request(`/subscriptions/${subscriptionId}/change`, {
305
+ method: "POST",
306
+ body: JSON.stringify({
307
+ planId,
308
+ pricingKey,
309
+ seats,
310
+ projectId,
311
+ successUrl,
312
+ cancelUrl
313
+ }),
314
+ methodName: "changeSubscription"
315
+ });
316
+ if (response.success) {
317
+ return response.data;
318
+ }
319
+ throw new Error(response.message);
320
+ } catch (error) {
321
+ throw new Error(`Failed to change subscription: ${error.message}`, { cause: error });
322
+ }
323
+ }
324
+ /**
325
+ * Downgrade subscription to free plan
326
+ */
327
+ async downgrade(downgradeData) {
328
+ this._requireReady("downgrade");
329
+ if (!downgradeData || typeof downgradeData !== "object") {
330
+ throw new Error("Downgrade data is required");
331
+ }
332
+ const { subscriptionId, reason } = downgradeData;
333
+ if (!subscriptionId) {
334
+ throw new Error("Subscription ID is required");
335
+ }
336
+ try {
337
+ const response = await this._request(`/subscriptions/${subscriptionId}/downgrade`, {
338
+ method: "POST",
339
+ body: JSON.stringify({
340
+ reason
341
+ }),
342
+ methodName: "downgrade"
343
+ });
344
+ if (response.success) {
345
+ return response.data;
346
+ }
347
+ throw new Error(response.message);
348
+ } catch (error) {
349
+ throw new Error(`Failed to downgrade subscription: ${error.message}`, { cause: error });
350
+ }
351
+ }
352
+ /**
353
+ * Helper method to change subscription with validation
354
+ */
355
+ async changeSubscriptionWithValidation(changeData) {
356
+ if (!changeData || typeof changeData !== "object") {
357
+ throw new Error("Change data must be a valid object");
358
+ }
359
+ const requiredFields = ["subscriptionId", "pricingKey"];
360
+ for (const field of requiredFields) {
361
+ if (!changeData[field]) {
362
+ throw new Error(`Required field '${field}' is missing`);
363
+ }
364
+ }
365
+ if (changeData.seats != null) {
366
+ if (!Number.isInteger(changeData.seats) || changeData.seats < 1) {
367
+ throw new Error("Seats must be a positive integer");
368
+ }
369
+ }
370
+ if (typeof changeData.subscriptionId !== "string") {
371
+ throw new Error("Subscription ID must be a valid string");
372
+ }
373
+ if (changeData.planId && typeof changeData.planId !== "string") {
374
+ throw new Error("Plan ID must be a valid string");
375
+ }
376
+ return await this.changeSubscription(changeData);
377
+ }
378
+ /**
379
+ * Helper method to downgrade subscription with validation
380
+ */
381
+ async downgradeWithValidation(downgradeData) {
382
+ if (!downgradeData || typeof downgradeData !== "object") {
383
+ throw new Error("Downgrade data must be a valid object");
384
+ }
385
+ if (!downgradeData.subscriptionId) {
386
+ throw new Error("Subscription ID is required");
387
+ }
388
+ if (typeof downgradeData.subscriptionId !== "string") {
389
+ throw new Error("Subscription ID must be a valid string");
390
+ }
391
+ if (downgradeData.reason && typeof downgradeData.reason !== "string") {
392
+ throw new Error("Reason must be a valid string");
393
+ }
394
+ return await this.downgrade(downgradeData);
395
+ }
396
+ }