@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,252 @@
1
+ import { BaseService } from "./BaseService.js";
2
+ class PaymentService extends BaseService {
3
+ // ==================== PAYMENT METHODS ====================
4
+ async checkout(options = {}) {
5
+ this._requireReady("checkout");
6
+ const {
7
+ projectId,
8
+ seats = 1,
9
+ price = "starter_monthly",
10
+ successUrl = `${window.location.origin}/success`,
11
+ cancelUrl = `${window.location.origin}/pricing`
12
+ } = options;
13
+ if (!projectId) {
14
+ throw new Error("Project ID is required for checkout");
15
+ }
16
+ try {
17
+ const response = await this._request("/payments/checkout", {
18
+ method: "POST",
19
+ body: JSON.stringify({
20
+ projectId,
21
+ seats,
22
+ price,
23
+ successUrl,
24
+ cancelUrl
25
+ }),
26
+ methodName: "checkout"
27
+ });
28
+ if (response.success) {
29
+ return response.data;
30
+ }
31
+ throw new Error(response.message);
32
+ } catch (error) {
33
+ throw new Error(`Failed to checkout: ${error.message}`, { cause: error });
34
+ }
35
+ }
36
+ async getSubscriptionStatus(projectId) {
37
+ this._requireReady("getSubscriptionStatus");
38
+ if (!projectId) {
39
+ throw new Error("Project ID is required");
40
+ }
41
+ try {
42
+ const response = await this._request(
43
+ `/payments/subscription/${projectId}`,
44
+ {
45
+ method: "GET",
46
+ methodName: "getSubscriptionStatus"
47
+ }
48
+ );
49
+ if (response.success) {
50
+ return response.data;
51
+ }
52
+ throw new Error(response.message);
53
+ } catch (error) {
54
+ throw new Error(`Failed to get subscription status: ${error.message}`, { cause: error });
55
+ }
56
+ }
57
+ // ==================== PAYMENT HELPER METHODS ====================
58
+ /**
59
+ * Helper method to create checkout with validation
60
+ */
61
+ async checkoutWithValidation(options = {}) {
62
+ const {
63
+ projectId,
64
+ seats,
65
+ price,
66
+ successUrl,
67
+ cancelUrl
68
+ } = options;
69
+ if (!projectId) {
70
+ throw new Error("Project ID is required");
71
+ }
72
+ if (seats && (typeof seats !== "number" || seats < 1)) {
73
+ throw new Error("Seats must be a positive number");
74
+ }
75
+ if (price && typeof price !== "string") {
76
+ throw new Error("Price must be a string");
77
+ }
78
+ if (successUrl && !this._isValidUrl(successUrl)) {
79
+ throw new Error("Success URL must be a valid URL");
80
+ }
81
+ if (cancelUrl && !this._isValidUrl(cancelUrl)) {
82
+ throw new Error("Cancel URL must be a valid URL");
83
+ }
84
+ return await this.checkout(options);
85
+ }
86
+ /**
87
+ * Helper method to get subscription status with validation
88
+ */
89
+ async getSubscriptionStatusWithValidation(projectId) {
90
+ if (!projectId || typeof projectId !== "string") {
91
+ throw new Error("Project ID must be a valid string");
92
+ }
93
+ return await this.getSubscriptionStatus(projectId);
94
+ }
95
+ /**
96
+ * Helper method to check if project has active subscription
97
+ */
98
+ async hasActiveSubscription(projectId) {
99
+ try {
100
+ const status = await this.getSubscriptionStatus(projectId);
101
+ return (status == null ? void 0 : status.active) === true;
102
+ } catch (error) {
103
+ console.warn("Failed to check subscription status:", error.message);
104
+ return false;
105
+ }
106
+ }
107
+ /**
108
+ * Helper method to get subscription details
109
+ */
110
+ async getSubscriptionDetails(projectId) {
111
+ try {
112
+ const status = await this.getSubscriptionStatus(projectId);
113
+ if (!status) {
114
+ return {
115
+ hasSubscription: false,
116
+ active: false,
117
+ message: "No subscription found"
118
+ };
119
+ }
120
+ return {
121
+ hasSubscription: true,
122
+ active: status.active || false,
123
+ plan: status.plan,
124
+ seats: status.seats,
125
+ nextBillingDate: status.nextBillingDate,
126
+ amount: status.amount,
127
+ currency: status.currency,
128
+ status: status.status
129
+ };
130
+ } catch (error) {
131
+ throw new Error(`Failed to get subscription details: ${error.message}`, { cause: error });
132
+ }
133
+ }
134
+ /**
135
+ * Helper method to create checkout for specific plan
136
+ */
137
+ async checkoutForPlan(projectId, planKey, options = {}) {
138
+ if (!projectId) {
139
+ throw new Error("Project ID is required");
140
+ }
141
+ if (!planKey) {
142
+ throw new Error("Plan key is required");
143
+ }
144
+ const checkoutOptions = {
145
+ projectId,
146
+ price: planKey,
147
+ ...options
148
+ };
149
+ return await this.checkoutWithValidation(checkoutOptions);
150
+ }
151
+ /**
152
+ * Helper method to create checkout for team plan
153
+ */
154
+ async checkoutForTeam(projectId, seats, options = {}) {
155
+ if (!projectId) {
156
+ throw new Error("Project ID is required");
157
+ }
158
+ if (!seats || seats < 1) {
159
+ throw new Error("Seats must be a positive number");
160
+ }
161
+ const checkoutOptions = {
162
+ projectId,
163
+ seats,
164
+ price: "team_monthly",
165
+ ...options
166
+ };
167
+ return await this.checkoutWithValidation(checkoutOptions);
168
+ }
169
+ /**
170
+ * Helper method to validate subscription status
171
+ */
172
+ validateSubscriptionStatus(status) {
173
+ if (!status || typeof status !== "object") {
174
+ return {
175
+ isValid: false,
176
+ error: "Invalid subscription status format"
177
+ };
178
+ }
179
+ const requiredFields = ["active", "plan", "seats"];
180
+ const missingFields = requiredFields.filter((field) => !(field in status));
181
+ if (missingFields.length > 0) {
182
+ return {
183
+ isValid: false,
184
+ error: `Missing required fields: ${missingFields.join(", ")}`
185
+ };
186
+ }
187
+ return {
188
+ isValid: true,
189
+ error: null
190
+ };
191
+ }
192
+ /**
193
+ * Helper method to format subscription amount
194
+ */
195
+ formatSubscriptionAmount(amount, currency = "USD") {
196
+ if (!amount || typeof amount !== "number") {
197
+ return "N/A";
198
+ }
199
+ return new Intl.NumberFormat("en-US", {
200
+ style: "currency",
201
+ currency
202
+ }).format(amount / 100);
203
+ }
204
+ /**
205
+ * Helper method to get subscription summary
206
+ */
207
+ async getSubscriptionSummary(projectId) {
208
+ try {
209
+ const details = await this.getSubscriptionDetails(projectId);
210
+ if (!details.hasSubscription) {
211
+ return {
212
+ status: "no_subscription",
213
+ message: "No active subscription",
214
+ action: "subscribe"
215
+ };
216
+ }
217
+ if (!details.active) {
218
+ return {
219
+ status: "inactive",
220
+ message: "Subscription is inactive",
221
+ action: "reactivate"
222
+ };
223
+ }
224
+ return {
225
+ status: "active",
226
+ message: `Active ${details.plan} plan with ${details.seats} seats`,
227
+ action: "manage",
228
+ details
229
+ };
230
+ } catch (error) {
231
+ return {
232
+ status: "error",
233
+ message: `Failed to get subscription: ${error.message}`,
234
+ action: "retry"
235
+ };
236
+ }
237
+ }
238
+ /**
239
+ * Private helper to validate URL
240
+ */
241
+ _isValidUrl(string) {
242
+ try {
243
+ const url = new URL(string);
244
+ return Boolean(url);
245
+ } catch {
246
+ return false;
247
+ }
248
+ }
249
+ }
250
+ export {
251
+ PaymentService
252
+ };
@@ -0,0 +1,407 @@
1
+ import { BaseService } from "./BaseService.js";
2
+ class PlanService extends BaseService {
3
+ // ==================== PLAN METHODS ====================
4
+ /**
5
+ * Get list of public plans (no authentication required)
6
+ */
7
+ async getPlans() {
8
+ try {
9
+ const response = await this._request("/plans", {
10
+ method: "GET",
11
+ methodName: "getPlans"
12
+ });
13
+ if (response.success) {
14
+ return response.data;
15
+ }
16
+ throw new Error(response.message);
17
+ } catch (error) {
18
+ throw new Error(`Failed to get plans: ${error.message}`, { cause: error });
19
+ }
20
+ }
21
+ /**
22
+ * Get list of public plans with enhanced pricing information (no authentication required)
23
+ */
24
+ async getPlansWithPricing() {
25
+ try {
26
+ const response = await this._request("/plans/pricing", {
27
+ method: "GET",
28
+ methodName: "getPlansWithPricing"
29
+ });
30
+ if (response.success) {
31
+ return response.data;
32
+ }
33
+ throw new Error(response.message);
34
+ } catch (error) {
35
+ throw new Error(`Failed to get plans with pricing: ${error.message}`, {
36
+ cause: error
37
+ });
38
+ }
39
+ }
40
+ /**
41
+ * Get a specific plan by ID (no authentication required)
42
+ */
43
+ async getPlan(planId) {
44
+ if (!planId) {
45
+ throw new Error("Plan ID is required");
46
+ }
47
+ try {
48
+ const response = await this._request(`/plans/${planId}`, {
49
+ method: "GET",
50
+ methodName: "getPlan"
51
+ });
52
+ if (response.success) {
53
+ return response.data;
54
+ }
55
+ throw new Error(response.message);
56
+ } catch (error) {
57
+ throw new Error(`Failed to get plan: ${error.message}`, { cause: error });
58
+ }
59
+ }
60
+ // ==================== ADMIN PLAN METHODS ====================
61
+ /**
62
+ * Get all plans including inactive ones (admin only)
63
+ */
64
+ async getAdminPlans() {
65
+ this._requireReady("getAdminPlans");
66
+ try {
67
+ const response = await this._request("/admin/plans", {
68
+ method: "GET",
69
+ methodName: "getAdminPlans"
70
+ });
71
+ if (response.success) {
72
+ return response.data;
73
+ }
74
+ throw new Error(response.message);
75
+ } catch (error) {
76
+ throw new Error(`Failed to get admin plans: ${error.message}`, {
77
+ cause: error
78
+ });
79
+ }
80
+ }
81
+ /**
82
+ * Create a new plan (admin only)
83
+ */
84
+ async createPlan(planData) {
85
+ this._requireReady("createPlan");
86
+ if (!planData || typeof planData !== "object") {
87
+ throw new Error("Plan data is required");
88
+ }
89
+ try {
90
+ const response = await this._request("/admin/plans", {
91
+ method: "POST",
92
+ body: JSON.stringify(planData),
93
+ methodName: "createPlan"
94
+ });
95
+ if (response.success) {
96
+ return response.data;
97
+ }
98
+ throw new Error(response.message);
99
+ } catch (error) {
100
+ throw new Error(`Failed to create plan: ${error.message}`, {
101
+ cause: error
102
+ });
103
+ }
104
+ }
105
+ /**
106
+ * Update an existing plan (admin only)
107
+ */
108
+ async updatePlan(planId, planData) {
109
+ this._requireReady("updatePlan");
110
+ if (!planId) {
111
+ throw new Error("Plan ID is required");
112
+ }
113
+ if (!planData || typeof planData !== "object") {
114
+ throw new Error("Plan data is required");
115
+ }
116
+ try {
117
+ const response = await this._request(`/admin/plans/${planId}`, {
118
+ method: "PATCH",
119
+ body: JSON.stringify(planData),
120
+ methodName: "updatePlan"
121
+ });
122
+ if (response.success) {
123
+ return response.data;
124
+ }
125
+ throw new Error(response.message);
126
+ } catch (error) {
127
+ throw new Error(`Failed to update plan: ${error.message}`, {
128
+ cause: error
129
+ });
130
+ }
131
+ }
132
+ /**
133
+ * Delete a plan (soft delete + archive Stripe product) (admin only)
134
+ */
135
+ async deletePlan(planId) {
136
+ this._requireReady("deletePlan");
137
+ if (!planId) {
138
+ throw new Error("Plan ID is required");
139
+ }
140
+ try {
141
+ const response = await this._request(`/admin/plans/${planId}`, {
142
+ method: "DELETE",
143
+ methodName: "deletePlan"
144
+ });
145
+ if (response.success) {
146
+ return response.data;
147
+ }
148
+ throw new Error(response.message);
149
+ } catch (error) {
150
+ throw new Error(`Failed to delete plan: ${error.message}`, {
151
+ cause: error
152
+ });
153
+ }
154
+ }
155
+ /**
156
+ * Initialize default plans (admin only)
157
+ */
158
+ async initializePlans() {
159
+ this._requireReady("initializePlans");
160
+ try {
161
+ const response = await this._request("/admin/plans/initialize", {
162
+ method: "POST",
163
+ methodName: "initializePlans"
164
+ });
165
+ if (response.success) {
166
+ return response;
167
+ }
168
+ throw new Error(response.message);
169
+ } catch (error) {
170
+ throw new Error(`Failed to initialize plans: ${error.message}`, {
171
+ cause: error
172
+ });
173
+ }
174
+ }
175
+ // ==================== PLAN HELPER METHODS ====================
176
+ /**
177
+ * Helper method to get plans with validation
178
+ */
179
+ async getPlansWithValidation() {
180
+ try {
181
+ const plans = await this.getPlans();
182
+ if (!Array.isArray(plans)) {
183
+ throw new Error("Invalid response format: plans should be an array");
184
+ }
185
+ return plans;
186
+ } catch (error) {
187
+ throw new Error(`Failed to get plans with validation: ${error.message}`, {
188
+ cause: error
189
+ });
190
+ }
191
+ }
192
+ /**
193
+ * Helper method to get a plan by ID with validation
194
+ */
195
+ async getPlanWithValidation(planId) {
196
+ if (!planId || typeof planId !== "string") {
197
+ throw new Error("Plan ID must be a valid string");
198
+ }
199
+ try {
200
+ const plan = await this.getPlan(planId);
201
+ if (!plan || typeof plan !== "object") {
202
+ throw new Error("Invalid plan data received");
203
+ }
204
+ return plan;
205
+ } catch (error) {
206
+ throw new Error(`Failed to get plan with validation: ${error.message}`, {
207
+ cause: error
208
+ });
209
+ }
210
+ }
211
+ /**
212
+ * Helper method to create a plan with validation (admin only)
213
+ */
214
+ async createPlanWithValidation(planData) {
215
+ if (!planData || typeof planData !== "object") {
216
+ throw new Error("Plan data must be a valid object");
217
+ }
218
+ const requiredFields = ["name", "description"];
219
+ for (const field of requiredFields) {
220
+ if (!planData[field]) {
221
+ throw new Error(`Required field '${field}' is missing`);
222
+ }
223
+ }
224
+ if (Object.hasOwn(planData, "price")) {
225
+ throw new Error(
226
+ 'Field "price" is no longer supported. Use unified "pricingOptions" with "amount" instead.'
227
+ );
228
+ }
229
+ if (planData.pricingOptions != null) {
230
+ if (!Array.isArray(planData.pricingOptions) || planData.pricingOptions.length === 0) {
231
+ throw new Error(
232
+ "pricingOptions must be a non-empty array when provided"
233
+ );
234
+ }
235
+ const allowedIntervals = /* @__PURE__ */ new Set(["month", "year", "week", "day", null]);
236
+ planData.pricingOptions.forEach((option, index) => {
237
+ if (!option || typeof option !== "object") {
238
+ throw new Error(`Pricing option at index ${index} must be an object`);
239
+ }
240
+ const { key, displayName, amount, interval, lookupKey } = option;
241
+ if (!key || typeof key !== "string") {
242
+ throw new Error(
243
+ `Pricing option at index ${index} is missing required field 'key'`
244
+ );
245
+ }
246
+ if (!/^[a-z0-9-]+$/u.test(key)) {
247
+ throw new Error(
248
+ `Pricing option key '${key}' must contain only lowercase letters, numbers, and hyphens`
249
+ );
250
+ }
251
+ if (!displayName || typeof displayName !== "string") {
252
+ throw new Error(
253
+ `Pricing option '${key}' is missing required field 'displayName'`
254
+ );
255
+ }
256
+ if (typeof amount !== "number" || amount < 0) {
257
+ throw new Error(
258
+ `Pricing option '${key}' must have a non-negative numeric 'amount'`
259
+ );
260
+ }
261
+ if (interval !== null && !allowedIntervals.has(interval)) {
262
+ throw new Error(
263
+ `Pricing option '${key}' has invalid interval '${interval}'. Allowed: month, year, week, day or null`
264
+ );
265
+ }
266
+ if (!lookupKey || typeof lookupKey !== "string") {
267
+ throw new Error(
268
+ `Pricing option '${key}' is missing required field 'lookupKey'`
269
+ );
270
+ }
271
+ });
272
+ }
273
+ if (Object.hasOwn(planData, "key") && planData.key == null) {
274
+ throw new Error("Plan key must be a valid string");
275
+ }
276
+ if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
277
+ throw new Error(
278
+ "Plan key must contain only lowercase letters, numbers, and hyphens"
279
+ );
280
+ }
281
+ return await this.createPlan(planData);
282
+ }
283
+ /**
284
+ * Helper method to update a plan with validation (admin only)
285
+ */
286
+ async updatePlanWithValidation(planId, planData) {
287
+ if (!planId || typeof planId !== "string") {
288
+ throw new Error("Plan ID must be a valid string");
289
+ }
290
+ if (!planData || typeof planData !== "object") {
291
+ throw new Error("Plan data must be a valid object");
292
+ }
293
+ if (Object.hasOwn(planData, "price")) {
294
+ throw new Error(
295
+ 'Field "price" is no longer supported. Use unified "pricingOptions" with "amount" instead.'
296
+ );
297
+ }
298
+ if (planData.pricingOptions != null) {
299
+ if (!Array.isArray(planData.pricingOptions) || planData.pricingOptions.length === 0) {
300
+ throw new Error(
301
+ "pricingOptions must be a non-empty array when provided"
302
+ );
303
+ }
304
+ const allowedIntervals = /* @__PURE__ */ new Set(["month", "year", "week", "day", null]);
305
+ planData.pricingOptions.forEach((option, index) => {
306
+ if (!option || typeof option !== "object") {
307
+ throw new Error(`Pricing option at index ${index} must be an object`);
308
+ }
309
+ const { key, displayName, amount, interval, lookupKey } = option;
310
+ if (!key || typeof key !== "string") {
311
+ throw new Error(
312
+ `Pricing option at index ${index} is missing required field 'key'`
313
+ );
314
+ }
315
+ if (!/^[a-z0-9-]+$/u.test(key)) {
316
+ throw new Error(
317
+ `Pricing option key '${key}' must contain only lowercase letters, numbers, and hyphens`
318
+ );
319
+ }
320
+ if (!displayName || typeof displayName !== "string") {
321
+ throw new Error(
322
+ `Pricing option '${key}' is missing required field 'displayName'`
323
+ );
324
+ }
325
+ if (typeof amount !== "number" || amount < 0) {
326
+ throw new Error(
327
+ `Pricing option '${key}' must have a non-negative numeric 'amount'`
328
+ );
329
+ }
330
+ if (interval !== null && !allowedIntervals.has(interval)) {
331
+ throw new Error(
332
+ `Pricing option '${key}' has invalid interval '${interval}'. Allowed: month, year, week, day or null`
333
+ );
334
+ }
335
+ if (!lookupKey || typeof lookupKey !== "string") {
336
+ throw new Error(
337
+ `Pricing option '${key}' is missing required field 'lookupKey'`
338
+ );
339
+ }
340
+ });
341
+ }
342
+ if (Object.hasOwn(planData, "key") && planData.key == null) {
343
+ throw new Error("Plan key must be a valid string");
344
+ }
345
+ if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
346
+ throw new Error(
347
+ "Plan key must contain only lowercase letters, numbers, and hyphens"
348
+ );
349
+ }
350
+ return await this.updatePlan(planId, planData);
351
+ }
352
+ /**
353
+ * Helper method to get active plans only
354
+ */
355
+ async getActivePlans() {
356
+ try {
357
+ const plans = await this.getPlans();
358
+ return plans.filter(
359
+ (plan) => plan.status === "active" && plan.isVisible !== false
360
+ );
361
+ } catch (error) {
362
+ throw new Error(`Failed to get active plans: ${error.message}`, {
363
+ cause: error
364
+ });
365
+ }
366
+ }
367
+ /**
368
+ * Helper method to get plans by price range
369
+ */
370
+ async getPlansByPriceRange(minPrice = 0, maxPrice = Infinity) {
371
+ try {
372
+ const plans = await this.getPlansWithPricing();
373
+ return plans.filter((plan) => {
374
+ var _a, _b;
375
+ const price = ((_b = (_a = plan == null ? void 0 : plan.pricing) == null ? void 0 : _a.bestPrice) == null ? void 0 : _b.amount) ?? 0;
376
+ return price >= minPrice && price <= maxPrice;
377
+ });
378
+ } catch (error) {
379
+ throw new Error(`Failed to get plans by price range: ${error.message}`, {
380
+ cause: error
381
+ });
382
+ }
383
+ }
384
+ /**
385
+ * Helper method to find plan by key
386
+ */
387
+ async getPlanByKey(key) {
388
+ if (!key) {
389
+ throw new Error("Plan key is required");
390
+ }
391
+ try {
392
+ const plans = await this.getPlans();
393
+ const plan = plans.find((p) => p.key === key);
394
+ if (!plan) {
395
+ throw new Error(`Plan with key '${key}' not found`);
396
+ }
397
+ return plan;
398
+ } catch (error) {
399
+ throw new Error(`Failed to get plan by key: ${error.message}`, {
400
+ cause: error
401
+ });
402
+ }
403
+ }
404
+ }
405
+ export {
406
+ PlanService
407
+ };