@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,468 @@
1
+ import { BaseService } from './BaseService.js'
2
+
3
+ export class PlanService extends BaseService {
4
+ // ==================== PLAN METHODS ====================
5
+
6
+ /**
7
+ * Get list of public plans (no authentication required)
8
+ */
9
+ async getPlans () {
10
+ try {
11
+ const response = await this._request('/plans', {
12
+ method: 'GET',
13
+ methodName: 'getPlans'
14
+ })
15
+ if (response.success) {
16
+ return response.data
17
+ }
18
+ throw new Error(response.message)
19
+ } catch (error) {
20
+ throw new Error(`Failed to get plans: ${error.message}`, { cause: error })
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Get list of public plans with enhanced pricing information (no authentication required)
26
+ */
27
+ async getPlansWithPricing () {
28
+ try {
29
+ const response = await this._request('/plans/pricing', {
30
+ method: 'GET',
31
+ methodName: 'getPlansWithPricing'
32
+ })
33
+ if (response.success) {
34
+ return response.data
35
+ }
36
+ throw new Error(response.message)
37
+ } catch (error) {
38
+ throw new Error(`Failed to get plans with pricing: ${error.message}`, {
39
+ cause: error
40
+ })
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Get a specific plan by ID (no authentication required)
46
+ */
47
+ async getPlan (planId) {
48
+ if (!planId) {
49
+ throw new Error('Plan ID is required')
50
+ }
51
+ try {
52
+ const response = await this._request(`/plans/${planId}`, {
53
+ method: 'GET',
54
+ methodName: 'getPlan'
55
+ })
56
+ if (response.success) {
57
+ return response.data
58
+ }
59
+ throw new Error(response.message)
60
+ } catch (error) {
61
+ throw new Error(`Failed to get plan: ${error.message}`, { cause: error })
62
+ }
63
+ }
64
+
65
+ // ==================== ADMIN PLAN METHODS ====================
66
+
67
+ /**
68
+ * Get all plans including inactive ones (admin only)
69
+ */
70
+ async getAdminPlans () {
71
+ this._requireReady('getAdminPlans')
72
+ try {
73
+ const response = await this._request('/admin/plans', {
74
+ method: 'GET',
75
+ methodName: 'getAdminPlans'
76
+ })
77
+ if (response.success) {
78
+ return response.data
79
+ }
80
+ throw new Error(response.message)
81
+ } catch (error) {
82
+ throw new Error(`Failed to get admin plans: ${error.message}`, {
83
+ cause: error
84
+ })
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Create a new plan (admin only)
90
+ */
91
+ async createPlan (planData) {
92
+ this._requireReady('createPlan')
93
+ if (!planData || typeof planData !== 'object') {
94
+ throw new Error('Plan data is required')
95
+ }
96
+ try {
97
+ const response = await this._request('/admin/plans', {
98
+ method: 'POST',
99
+ body: JSON.stringify(planData),
100
+ methodName: 'createPlan'
101
+ })
102
+ if (response.success) {
103
+ return response.data
104
+ }
105
+ throw new Error(response.message)
106
+ } catch (error) {
107
+ throw new Error(`Failed to create plan: ${error.message}`, {
108
+ cause: error
109
+ })
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Update an existing plan (admin only)
115
+ */
116
+ async updatePlan (planId, planData) {
117
+ this._requireReady('updatePlan')
118
+ if (!planId) {
119
+ throw new Error('Plan ID is required')
120
+ }
121
+ if (!planData || typeof planData !== 'object') {
122
+ throw new Error('Plan data is required')
123
+ }
124
+ try {
125
+ const response = await this._request(`/admin/plans/${planId}`, {
126
+ method: 'PATCH',
127
+ body: JSON.stringify(planData),
128
+ methodName: 'updatePlan'
129
+ })
130
+ if (response.success) {
131
+ return response.data
132
+ }
133
+ throw new Error(response.message)
134
+ } catch (error) {
135
+ throw new Error(`Failed to update plan: ${error.message}`, {
136
+ cause: error
137
+ })
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Delete a plan (soft delete + archive Stripe product) (admin only)
143
+ */
144
+ async deletePlan (planId) {
145
+ this._requireReady('deletePlan')
146
+ if (!planId) {
147
+ throw new Error('Plan ID is required')
148
+ }
149
+ try {
150
+ const response = await this._request(`/admin/plans/${planId}`, {
151
+ method: 'DELETE',
152
+ methodName: 'deletePlan'
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 delete plan: ${error.message}`, {
160
+ cause: error
161
+ })
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Initialize default plans (admin only)
167
+ */
168
+ async initializePlans () {
169
+ this._requireReady('initializePlans')
170
+ try {
171
+ const response = await this._request('/admin/plans/initialize', {
172
+ method: 'POST',
173
+ methodName: 'initializePlans'
174
+ })
175
+ if (response.success) {
176
+ return response
177
+ }
178
+ throw new Error(response.message)
179
+ } catch (error) {
180
+ throw new Error(`Failed to initialize plans: ${error.message}`, {
181
+ cause: error
182
+ })
183
+ }
184
+ }
185
+
186
+ // ==================== PLAN HELPER METHODS ====================
187
+
188
+ /**
189
+ * Helper method to get plans with validation
190
+ */
191
+ async getPlansWithValidation () {
192
+ try {
193
+ const plans = await this.getPlans()
194
+ if (!Array.isArray(plans)) {
195
+ throw new Error('Invalid response format: plans should be an array')
196
+ }
197
+ return plans
198
+ } catch (error) {
199
+ throw new Error(`Failed to get plans with validation: ${error.message}`, {
200
+ cause: error
201
+ })
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Helper method to get a plan by ID with validation
207
+ */
208
+ async getPlanWithValidation (planId) {
209
+ if (!planId || typeof planId !== 'string') {
210
+ throw new Error('Plan ID must be a valid string')
211
+ }
212
+
213
+ try {
214
+ const plan = await this.getPlan(planId)
215
+ if (!plan || typeof plan !== 'object') {
216
+ throw new Error('Invalid plan data received')
217
+ }
218
+ return plan
219
+ } catch (error) {
220
+ throw new Error(`Failed to get plan with validation: ${error.message}`, {
221
+ cause: error
222
+ })
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Helper method to create a plan with validation (admin only)
228
+ */
229
+ async createPlanWithValidation (planData) {
230
+ if (!planData || typeof planData !== 'object') {
231
+ throw new Error('Plan data must be a valid object')
232
+ }
233
+
234
+ // Basic validation for required fields to match server model
235
+ const requiredFields = ['name', 'description']
236
+ for (const field of requiredFields) {
237
+ if (!planData[field]) {
238
+ throw new Error(`Required field '${field}' is missing`)
239
+ }
240
+ }
241
+
242
+ // Legacy field guard: bare "price" is no longer supported on the server
243
+ if (Object.hasOwn(planData, 'price')) {
244
+ throw new Error(
245
+ 'Field "price" is no longer supported. Use unified "pricingOptions" with "amount" instead.'
246
+ )
247
+ }
248
+
249
+ // Validate unified pricingOptions structure if provided
250
+ if (planData.pricingOptions != null) {
251
+ if (
252
+ !Array.isArray(planData.pricingOptions) ||
253
+ planData.pricingOptions.length === 0
254
+ ) {
255
+ throw new Error(
256
+ 'pricingOptions must be a non-empty array when provided'
257
+ )
258
+ }
259
+
260
+ const allowedIntervals = new Set(['month', 'year', 'week', 'day', null])
261
+ planData.pricingOptions.forEach((option, index) => {
262
+ if (!option || typeof option !== 'object') {
263
+ throw new Error(`Pricing option at index ${index} must be an object`)
264
+ }
265
+
266
+ const { key, displayName, amount, interval, lookupKey } = option
267
+
268
+ if (!key || typeof key !== 'string') {
269
+ throw new Error(
270
+ `Pricing option at index ${index} is missing required field 'key'`
271
+ )
272
+ }
273
+
274
+ // Validate key format (alphanumeric and hyphens only)
275
+ if (!/^[a-z0-9-]+$/u.test(key)) {
276
+ throw new Error(
277
+ `Pricing option key '${key}' must contain only lowercase letters, numbers, and hyphens`
278
+ )
279
+ }
280
+
281
+ if (!displayName || typeof displayName !== 'string') {
282
+ throw new Error(
283
+ `Pricing option '${key}' is missing required field 'displayName'`
284
+ )
285
+ }
286
+
287
+ if (typeof amount !== 'number' || amount < 0) {
288
+ throw new Error(
289
+ `Pricing option '${key}' must have a non-negative numeric 'amount'`
290
+ )
291
+ }
292
+
293
+ if (interval !== null && !allowedIntervals.has(interval)) {
294
+ throw new Error(
295
+ `Pricing option '${key}' has invalid interval '${interval}'. Allowed: month, year, week, day or null`
296
+ )
297
+ }
298
+
299
+ if (!lookupKey || typeof lookupKey !== 'string') {
300
+ throw new Error(
301
+ `Pricing option '${key}' is missing required field 'lookupKey'`
302
+ )
303
+ }
304
+ })
305
+ }
306
+
307
+ // Optional: validate top-level key if provided (legacy support)
308
+ if (Object.hasOwn(planData, 'key') && planData.key == null) {
309
+ throw new Error('Plan key must be a valid string')
310
+ }
311
+ if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
312
+ throw new Error(
313
+ 'Plan key must contain only lowercase letters, numbers, and hyphens'
314
+ )
315
+ }
316
+
317
+ return await this.createPlan(planData)
318
+ }
319
+
320
+ /**
321
+ * Helper method to update a plan with validation (admin only)
322
+ */
323
+ async updatePlanWithValidation (planId, planData) {
324
+ if (!planId || typeof planId !== 'string') {
325
+ throw new Error('Plan ID must be a valid string')
326
+ }
327
+ if (!planData || typeof planData !== 'object') {
328
+ throw new Error('Plan data must be a valid object')
329
+ }
330
+
331
+ // Legacy field guard: bare "price" is no longer supported on the server
332
+ if (Object.hasOwn(planData, 'price')) {
333
+ throw new Error(
334
+ 'Field "price" is no longer supported. Use unified "pricingOptions" with "amount" instead.'
335
+ )
336
+ }
337
+
338
+ // Validate unified pricingOptions structure if provided
339
+ if (planData.pricingOptions != null) {
340
+ if (
341
+ !Array.isArray(planData.pricingOptions) ||
342
+ planData.pricingOptions.length === 0
343
+ ) {
344
+ throw new Error(
345
+ 'pricingOptions must be a non-empty array when provided'
346
+ )
347
+ }
348
+
349
+ const allowedIntervals = new Set(['month', 'year', 'week', 'day', null])
350
+ planData.pricingOptions.forEach((option, index) => {
351
+ if (!option || typeof option !== 'object') {
352
+ throw new Error(`Pricing option at index ${index} must be an object`)
353
+ }
354
+
355
+ const { key, displayName, amount, interval, lookupKey } = option
356
+
357
+ if (!key || typeof key !== 'string') {
358
+ throw new Error(
359
+ `Pricing option at index ${index} is missing required field 'key'`
360
+ )
361
+ }
362
+
363
+ // Validate key format (alphanumeric and hyphens only)
364
+ if (!/^[a-z0-9-]+$/u.test(key)) {
365
+ throw new Error(
366
+ `Pricing option key '${key}' must contain only lowercase letters, numbers, and hyphens`
367
+ )
368
+ }
369
+
370
+ if (!displayName || typeof displayName !== 'string') {
371
+ throw new Error(
372
+ `Pricing option '${key}' is missing required field 'displayName'`
373
+ )
374
+ }
375
+
376
+ if (typeof amount !== 'number' || amount < 0) {
377
+ throw new Error(
378
+ `Pricing option '${key}' must have a non-negative numeric 'amount'`
379
+ )
380
+ }
381
+
382
+ if (interval !== null && !allowedIntervals.has(interval)) {
383
+ throw new Error(
384
+ `Pricing option '${key}' has invalid interval '${interval}'. Allowed: month, year, week, day or null`
385
+ )
386
+ }
387
+
388
+ if (!lookupKey || typeof lookupKey !== 'string') {
389
+ throw new Error(
390
+ `Pricing option '${key}' is missing required field 'lookupKey'`
391
+ )
392
+ }
393
+ })
394
+ }
395
+
396
+ // Validate key format if provided
397
+ if (Object.hasOwn(planData, 'key') && planData.key == null) {
398
+ throw new Error('Plan key must be a valid string')
399
+ }
400
+ if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
401
+ throw new Error(
402
+ 'Plan key must contain only lowercase letters, numbers, and hyphens'
403
+ )
404
+ }
405
+
406
+ return await this.updatePlan(planId, planData)
407
+ }
408
+
409
+ /**
410
+ * Helper method to get active plans only
411
+ */
412
+ async getActivePlans () {
413
+ try {
414
+ const plans = await this.getPlans()
415
+ // Server already returns only active & visible plans for /plans,
416
+ // but we keep this helper aligned with the model fields.
417
+ return plans.filter(
418
+ plan => plan.status === 'active' && plan.isVisible !== false
419
+ )
420
+ } catch (error) {
421
+ throw new Error(`Failed to get active plans: ${error.message}`, {
422
+ cause: error
423
+ })
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Helper method to get plans by price range
429
+ */
430
+ async getPlansByPriceRange (minPrice = 0, maxPrice = Infinity) {
431
+ try {
432
+ // Use enhanced pricing information from /plans/pricing
433
+ const plans = await this.getPlansWithPricing()
434
+ return plans.filter(plan => {
435
+ const price = plan?.pricing?.bestPrice?.amount ?? 0
436
+ return price >= minPrice && price <= maxPrice
437
+ })
438
+ } catch (error) {
439
+ throw new Error(`Failed to get plans by price range: ${error.message}`, {
440
+ cause: error
441
+ })
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Helper method to find plan by key
447
+ */
448
+ async getPlanByKey (key) {
449
+ if (!key) {
450
+ throw new Error('Plan key is required')
451
+ }
452
+
453
+ try {
454
+ const plans = await this.getPlans()
455
+ const plan = plans.find(p => p.key === key)
456
+
457
+ if (!plan) {
458
+ throw new Error(`Plan with key '${key}' not found`)
459
+ }
460
+
461
+ return plan
462
+ } catch (error) {
463
+ throw new Error(`Failed to get plan by key: ${error.message}`, {
464
+ cause: error
465
+ })
466
+ }
467
+ }
468
+ }