@symbo.ls/sdk 2.34.34 → 3.1.1

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 +13 -315
  2. package/dist/cjs/config/environment.js +50 -124
  3. package/dist/cjs/index.js +22 -169
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +277 -751
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +679 -0
  8. package/dist/cjs/services/SocketIOService.js +309 -0
  9. package/dist/cjs/services/SocketService.js +161 -0
  10. package/dist/cjs/services/SymstoryService.js +485 -0
  11. package/dist/cjs/services/index.js +16 -64
  12. package/dist/cjs/utils/basedQuerys.js +61 -0
  13. package/dist/cjs/utils/permission.js +4 -4
  14. package/dist/cjs/utils/services.js +80 -301
  15. package/dist/cjs/utils/symstoryClient.js +228 -0
  16. package/dist/cjs/utils/validation.js +3 -0
  17. package/dist/esm/config/environment.js +50 -124
  18. package/dist/esm/index.js +11282 -49789
  19. package/dist/esm/services/AIService.js +185 -0
  20. package/dist/esm/services/AuthService.js +358 -1506
  21. package/dist/esm/services/BaseService.js +6 -757
  22. package/dist/esm/services/BasedService.js +4651 -0
  23. package/dist/esm/services/SocketIOService.js +467 -0
  24. package/dist/esm/services/SocketService.js +191 -0
  25. package/dist/esm/services/SymstoryService.js +6849 -0
  26. package/dist/esm/services/index.js +11188 -49352
  27. package/dist/esm/utils/basedQuerys.js +43 -0
  28. package/dist/esm/utils/permission.js +4 -4
  29. package/dist/esm/utils/services.js +80 -301
  30. package/dist/esm/utils/symstoryClient.js +334 -0
  31. package/dist/esm/utils/validation.js +26 -93
  32. package/dist/node/config/environment.js +50 -124
  33. package/dist/node/index.js +26 -202
  34. package/dist/node/services/AIService.js +136 -0
  35. package/dist/node/services/AuthService.js +278 -751
  36. package/dist/node/services/BaseService.js +6 -148
  37. package/dist/node/services/BasedService.js +650 -0
  38. package/dist/node/services/SocketIOService.js +280 -0
  39. package/dist/node/services/SocketService.js +142 -0
  40. package/dist/node/services/SymstoryService.js +456 -0
  41. package/dist/node/services/index.js +16 -64
  42. package/dist/node/utils/basedQuerys.js +42 -0
  43. package/dist/node/utils/permission.js +4 -4
  44. package/dist/node/utils/services.js +80 -301
  45. package/dist/node/utils/symstoryClient.js +199 -0
  46. package/dist/node/utils/validation.js +3 -0
  47. package/package.json +21 -43
  48. package/src/config/environment.js +50 -126
  49. package/src/index.js +24 -208
  50. package/src/services/AIService.js +150 -0
  51. package/src/services/AuthService.js +298 -893
  52. package/src/services/BaseService.js +6 -166
  53. package/src/services/BasedService.js +722 -0
  54. package/src/services/SocketIOService.js +356 -0
  55. package/src/services/SocketService.js +168 -0
  56. package/src/services/SymstoryService.js +563 -0
  57. package/src/services/index.js +13 -80
  58. package/src/utils/basedQuerys.js +41 -0
  59. package/src/utils/permission.js +4 -4
  60. package/src/utils/services.js +83 -325
  61. package/src/utils/symstoryClient.js +218 -0
  62. package/src/utils/validation.js +3 -0
  63. package/dist/cjs/services/AdminService.js +0 -351
  64. package/dist/cjs/services/BranchService.js +0 -484
  65. package/dist/cjs/services/CollabService.js +0 -743
  66. package/dist/cjs/services/DnsService.js +0 -340
  67. package/dist/cjs/services/FeatureFlagService.js +0 -175
  68. package/dist/cjs/services/FileService.js +0 -201
  69. package/dist/cjs/services/IntegrationService.js +0 -538
  70. package/dist/cjs/services/MetricsService.js +0 -62
  71. package/dist/cjs/services/PaymentService.js +0 -271
  72. package/dist/cjs/services/PlanService.js +0 -426
  73. package/dist/cjs/services/ProjectService.js +0 -1207
  74. package/dist/cjs/services/PullRequestService.js +0 -503
  75. package/dist/cjs/services/ScreenshotService.js +0 -304
  76. package/dist/cjs/services/SubscriptionService.js +0 -396
  77. package/dist/cjs/services/TrackingService.js +0 -661
  78. package/dist/cjs/services/WaitlistService.js +0 -148
  79. package/dist/cjs/state/RootStateManager.js +0 -65
  80. package/dist/cjs/state/rootEventBus.js +0 -74
  81. package/dist/cjs/utils/CollabClient.js +0 -223
  82. package/dist/cjs/utils/TokenManager.js +0 -422
  83. package/dist/cjs/utils/changePreprocessor.js +0 -199
  84. package/dist/cjs/utils/jsonDiff.js +0 -145
  85. package/dist/cjs/utils/ordering.js +0 -309
  86. package/dist/esm/services/AdminService.js +0 -1132
  87. package/dist/esm/services/BranchService.js +0 -1265
  88. package/dist/esm/services/CollabService.js +0 -26838
  89. package/dist/esm/services/DnsService.js +0 -1121
  90. package/dist/esm/services/FeatureFlagService.js +0 -956
  91. package/dist/esm/services/FileService.js +0 -982
  92. package/dist/esm/services/IntegrationService.js +0 -1319
  93. package/dist/esm/services/MetricsService.js +0 -843
  94. package/dist/esm/services/PaymentService.js +0 -1052
  95. package/dist/esm/services/PlanService.js +0 -1207
  96. package/dist/esm/services/ProjectService.js +0 -2526
  97. package/dist/esm/services/PullRequestService.js +0 -1284
  98. package/dist/esm/services/ScreenshotService.js +0 -1085
  99. package/dist/esm/services/SubscriptionService.js +0 -1177
  100. package/dist/esm/services/TrackingService.js +0 -18343
  101. package/dist/esm/services/WaitlistService.js +0 -929
  102. package/dist/esm/state/RootStateManager.js +0 -90
  103. package/dist/esm/state/rootEventBus.js +0 -56
  104. package/dist/esm/utils/CollabClient.js +0 -18901
  105. package/dist/esm/utils/TokenManager.js +0 -408
  106. package/dist/esm/utils/changePreprocessor.js +0 -542
  107. package/dist/esm/utils/jsonDiff.js +0 -7011
  108. package/dist/esm/utils/ordering.js +0 -291
  109. package/dist/node/services/AdminService.js +0 -332
  110. package/dist/node/services/BranchService.js +0 -465
  111. package/dist/node/services/CollabService.js +0 -724
  112. package/dist/node/services/DnsService.js +0 -321
  113. package/dist/node/services/FeatureFlagService.js +0 -156
  114. package/dist/node/services/FileService.js +0 -182
  115. package/dist/node/services/IntegrationService.js +0 -519
  116. package/dist/node/services/MetricsService.js +0 -43
  117. package/dist/node/services/PaymentService.js +0 -252
  118. package/dist/node/services/PlanService.js +0 -407
  119. package/dist/node/services/ProjectService.js +0 -1188
  120. package/dist/node/services/PullRequestService.js +0 -484
  121. package/dist/node/services/ScreenshotService.js +0 -285
  122. package/dist/node/services/SubscriptionService.js +0 -377
  123. package/dist/node/services/TrackingService.js +0 -632
  124. package/dist/node/services/WaitlistService.js +0 -129
  125. package/dist/node/state/RootStateManager.js +0 -36
  126. package/dist/node/state/rootEventBus.js +0 -55
  127. package/dist/node/utils/CollabClient.js +0 -194
  128. package/dist/node/utils/TokenManager.js +0 -403
  129. package/dist/node/utils/changePreprocessor.js +0 -180
  130. package/dist/node/utils/jsonDiff.js +0 -116
  131. package/dist/node/utils/ordering.js +0 -290
  132. package/src/services/AdminService.js +0 -374
  133. package/src/services/BranchService.js +0 -536
  134. package/src/services/CollabService.js +0 -900
  135. package/src/services/DnsService.js +0 -366
  136. package/src/services/FeatureFlagService.js +0 -174
  137. package/src/services/FileService.js +0 -213
  138. package/src/services/IntegrationService.js +0 -548
  139. package/src/services/MetricsService.js +0 -40
  140. package/src/services/PaymentService.js +0 -287
  141. package/src/services/PlanService.js +0 -468
  142. package/src/services/ProjectService.js +0 -1366
  143. package/src/services/PullRequestService.js +0 -537
  144. package/src/services/ScreenshotService.js +0 -258
  145. package/src/services/SubscriptionService.js +0 -425
  146. package/src/services/TrackingService.js +0 -853
  147. package/src/services/WaitlistService.js +0 -130
  148. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  149. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  150. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  151. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  152. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  153. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  154. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  155. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  156. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  157. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  158. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  159. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  160. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  161. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  162. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  163. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  164. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  165. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  166. package/src/services/tests/FileService/validateFile.test.js +0 -63
  167. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  168. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  169. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  170. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  171. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  172. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  173. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  174. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  175. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  176. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  177. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  178. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  179. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  180. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  181. package/src/state/RootStateManager.js +0 -76
  182. package/src/state/rootEventBus.js +0 -67
  183. package/src/utils/CollabClient.js +0 -248
  184. package/src/utils/TokenManager.js +0 -479
  185. package/src/utils/changePreprocessor.js +0 -239
  186. package/src/utils/jsonDiff.js +0 -144
  187. package/src/utils/ordering.js +0 -271
@@ -0,0 +1,679 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var BasedService_exports = {};
29
+ __export(BasedService_exports, {
30
+ BasedService: () => BasedService
31
+ });
32
+ module.exports = __toCommonJS(BasedService_exports);
33
+ var import_BaseService = require("./BaseService.js");
34
+ var import_basedQuerys = require("../utils/basedQuerys.js");
35
+ var import_client = __toESM(require("@based/client"), 1);
36
+ var import_utils = require("@domql/utils");
37
+ var import_environment = __toESM(require("../config/environment.js"), 1);
38
+ class BasedService extends import_BaseService.BaseService {
39
+ constructor(config) {
40
+ super(config);
41
+ this._client = null;
42
+ this._subscriptions = /* @__PURE__ */ new Map();
43
+ }
44
+ init({ context }) {
45
+ try {
46
+ const { env, org, project } = context.based || {
47
+ env: import_environment.default.basedEnv,
48
+ org: import_environment.default.basedOrg,
49
+ project: import_environment.default.basedProject
50
+ };
51
+ if (!env || !org || !project) {
52
+ throw new Error("Based configuration missing required parameters");
53
+ }
54
+ this._client = new import_client.default({
55
+ env,
56
+ org,
57
+ project
58
+ });
59
+ this._info = {
60
+ config: {
61
+ env,
62
+ org: this._maskString(org),
63
+ project: this._maskString(project)
64
+ }
65
+ };
66
+ this._setReady();
67
+ } catch (error) {
68
+ this._setError(error);
69
+ throw error;
70
+ }
71
+ }
72
+ // Helper method to mask sensitive strings
73
+ _maskString(str) {
74
+ if (!str) {
75
+ return "";
76
+ }
77
+ return `${str.substr(0, 4)}...${str.substr(-4)}`;
78
+ }
79
+ updateContext(context) {
80
+ var _a;
81
+ this._context = { ...this._context, ...context };
82
+ if (this._context.authToken && ((_a = this._context.user) == null ? void 0 : _a.id)) {
83
+ this.setAuthState(this._context.authToken, this._context.user.id);
84
+ }
85
+ }
86
+ async setAuthState(authState) {
87
+ this._requireReady();
88
+ const newAuthState = {
89
+ ...authState,
90
+ persistent: true
91
+ };
92
+ try {
93
+ return await this._client.setAuthState(newAuthState);
94
+ } catch (error) {
95
+ throw new Error(`Failed to set auth state: ${error.message}`);
96
+ }
97
+ }
98
+ async setBucket(bucketId, callback) {
99
+ this._requireReady();
100
+ if (!(0, import_utils.isString)(bucketId)) {
101
+ throw new Error("Invalid type of bucket ID", bucketId);
102
+ }
103
+ if (!this._context.project) {
104
+ throw new Error("Project is undefined");
105
+ }
106
+ try {
107
+ const obj = {
108
+ $id: this._context.project.id,
109
+ // TODO: change to getProjectId
110
+ bucket: bucketId
111
+ };
112
+ if ((0, import_utils.isFunction)(callback)) {
113
+ const data = await this._client.call("db:set", obj);
114
+ return callback(data);
115
+ }
116
+ return await this._client.call("db:set", obj);
117
+ } catch (error) {
118
+ throw new Error(`Failed to set bucket: ${error.message}`);
119
+ }
120
+ }
121
+ async setUserForced(userId, obj) {
122
+ this._requireReady();
123
+ try {
124
+ await this._client.query("db", {
125
+ $id: obj.projectId,
126
+ members: {
127
+ $list: true,
128
+ user: {
129
+ id: true
130
+ }
131
+ }
132
+ }).get();
133
+ const { id: membershipId } = await this._client.call("db:set", {
134
+ type: "projectMember",
135
+ user: userId,
136
+ project: obj.projectId,
137
+ role: obj.role,
138
+ joinedAt: Date.now()
139
+ });
140
+ await Promise.all([
141
+ this._client.call("db:set", {
142
+ $id: userId,
143
+ memberProjects: { $add: membershipId }
144
+ }),
145
+ this._client.call("db:set", {
146
+ $id: obj.projectId,
147
+ members: { $add: membershipId }
148
+ })
149
+ ]);
150
+ return await this._client.call("db:set", obj);
151
+ } catch (error) {
152
+ throw new Error(`Failed to set bucket: ${error.message}`);
153
+ }
154
+ }
155
+ async query(collection, query, options = {}) {
156
+ this._requireReady();
157
+ try {
158
+ return await this._client.query(collection, query, {
159
+ ...options,
160
+ context: this._context
161
+ }).get();
162
+ } catch (error) {
163
+ throw new Error(`Query failed: ${error.message}`);
164
+ }
165
+ }
166
+ async setProject(params) {
167
+ this._requireReady();
168
+ try {
169
+ return await this._client.call("db:set", {
170
+ type: "project",
171
+ ...params
172
+ });
173
+ } catch (error) {
174
+ throw new Error(`Query failed: ${error.message}`);
175
+ }
176
+ }
177
+ async setUser(params) {
178
+ this._requireReady();
179
+ try {
180
+ return await this._client.call("db:set", {
181
+ type: "user",
182
+ ...params
183
+ });
184
+ } catch (error) {
185
+ throw new Error(`Query failed: ${error.message}`);
186
+ }
187
+ }
188
+ async fetchUser(userId) {
189
+ if (!userId) {
190
+ throw new Error("User Id is required");
191
+ }
192
+ return await this.query("db", (0, import_basedQuerys.buildUserQuery)(userId));
193
+ }
194
+ async getUser(userId) {
195
+ this._requireReady();
196
+ if (!userId) {
197
+ throw new Error("User Id is required");
198
+ }
199
+ try {
200
+ return await this.call("users:get", { userId });
201
+ } catch (error) {
202
+ throw new Error(`Failed to get user: ${error.message}`);
203
+ }
204
+ }
205
+ async getUserByEmail(email) {
206
+ this._requireReady();
207
+ if (!email) {
208
+ throw new Error("Email is required");
209
+ }
210
+ try {
211
+ return await this.call("users:get-by", { email });
212
+ } catch (error) {
213
+ throw new Error(`Failed to get user: ${error.message}`);
214
+ }
215
+ }
216
+ async setProjectDomains(projectId, domains) {
217
+ this._requireReady();
218
+ if (!projectId) {
219
+ throw new Error("Project ID is required");
220
+ }
221
+ try {
222
+ return await this.call("projects:update-domains", { projectId, domains });
223
+ } catch (error) {
224
+ throw new Error(`Failed to set project domains: ${error.message}`);
225
+ }
226
+ }
227
+ async checkProjectKeyAvailability(key) {
228
+ this._requireReady();
229
+ try {
230
+ return await this.call("projects:check-key", { key });
231
+ } catch (error) {
232
+ throw new Error(
233
+ `Failed to check project key availability: ${error.message}`
234
+ );
235
+ }
236
+ }
237
+ async removeProject(projectId) {
238
+ this._requireReady();
239
+ if (!projectId) {
240
+ throw new Error("Project ID is required");
241
+ }
242
+ try {
243
+ return await this.call("projects:remove", { projectId });
244
+ } catch (error) {
245
+ throw new Error(`Failed to remove project: ${error.message}`);
246
+ }
247
+ }
248
+ async getAvailableLibraries(params) {
249
+ this._requireReady();
250
+ const defaultParams = {
251
+ page: 1,
252
+ limit: 20,
253
+ search: "",
254
+ framework: "",
255
+ language: ""
256
+ };
257
+ try {
258
+ return await this.call("projects:get-available-libraries", {
259
+ ...defaultParams,
260
+ ...params
261
+ });
262
+ } catch (error) {
263
+ throw new Error(`Failed to get available libraries: ${error.message}`);
264
+ }
265
+ }
266
+ async addProjectLibraries(projectId, libraryIds) {
267
+ this._requireReady();
268
+ if (!projectId) {
269
+ throw new Error("Project ID is required");
270
+ }
271
+ try {
272
+ return await this.call("projects:add-libraries", {
273
+ projectId,
274
+ libraryIds
275
+ });
276
+ } catch (error) {
277
+ throw new Error(`Failed to add project libraries: ${error.message}`);
278
+ }
279
+ }
280
+ async removeProjectLibraries(projectId, libraryIds) {
281
+ this._requireReady();
282
+ if (!projectId) {
283
+ throw new Error("Project ID is required");
284
+ }
285
+ try {
286
+ return await this.call("projects:remove-libraries", {
287
+ projectId,
288
+ libraryIds
289
+ });
290
+ } catch (error) {
291
+ throw new Error(`Failed to remove project libraries: ${error.message}`);
292
+ }
293
+ }
294
+ async getProjectLibraries(projectId) {
295
+ this._requireReady();
296
+ if (!projectId) {
297
+ throw new Error("Project ID is required");
298
+ }
299
+ try {
300
+ return await this.call("projects:get-libraries", { projectId });
301
+ } catch (error) {
302
+ throw new Error(`Failed to get project libraries: ${error.message}`);
303
+ }
304
+ }
305
+ subscribe(collection, query, callback, options = {}) {
306
+ this._requireReady();
307
+ const subscriptionKey = JSON.stringify({ collection, query });
308
+ if (this._subscriptions.has(subscriptionKey)) {
309
+ return this._subscriptions.get(subscriptionKey);
310
+ }
311
+ try {
312
+ const unsubscribe = this._client.query(collection, query, {
313
+ ...options,
314
+ context: this._context
315
+ }).subscribe(callback);
316
+ this._subscriptions.set(subscriptionKey, unsubscribe);
317
+ return () => {
318
+ unsubscribe();
319
+ this._subscriptions.delete(subscriptionKey);
320
+ };
321
+ } catch (error) {
322
+ throw new Error(`Subscription failed: ${error.message}`);
323
+ }
324
+ }
325
+ subscribeChannel(name, params, callback) {
326
+ this._requireReady();
327
+ try {
328
+ return this._client.channel(name, params).subscribe(callback);
329
+ } catch (error) {
330
+ throw new Error(`Channel subscription failed: ${error.message}`);
331
+ }
332
+ }
333
+ publishToChannel(name, params, data) {
334
+ this._requireReady();
335
+ try {
336
+ return this._client.channel(name, params).publish(data);
337
+ } catch (error) {
338
+ throw new Error(`Channel publish failed: ${error.message}`);
339
+ }
340
+ }
341
+ async call(functionName, params = {}) {
342
+ this._requireReady();
343
+ try {
344
+ return await this._client.call(functionName, params);
345
+ } catch (error) {
346
+ throw new Error(`Function call failed: ${error.message}`);
347
+ }
348
+ }
349
+ async updateSchema(schema) {
350
+ this._requireReady();
351
+ try {
352
+ return await this._client.call("db:update-schema", schema);
353
+ } catch (error) {
354
+ throw new Error(`Schema update failed: ${error.message}`);
355
+ }
356
+ }
357
+ async createProject(projectData) {
358
+ this._requireReady();
359
+ try {
360
+ return await this.call("projects:create", projectData);
361
+ } catch (error) {
362
+ throw new Error(`Failed to create project: ${error.message}`);
363
+ }
364
+ }
365
+ async getProject(projectId, fields = { $all: true }) {
366
+ this._requireReady();
367
+ if (!projectId) {
368
+ throw new Error("Project ID is required");
369
+ }
370
+ try {
371
+ return await this.call("projects:get", {
372
+ projectId,
373
+ fields: { ...fields }
374
+ });
375
+ } catch (error) {
376
+ throw new Error(`Failed to get project: ${error.message}`);
377
+ }
378
+ }
379
+ async getProjectByKey(key, fields = { $all: true }) {
380
+ this._requireReady();
381
+ try {
382
+ return await this.call("projects:get", {
383
+ key,
384
+ fields: { ...fields }
385
+ });
386
+ } catch (error) {
387
+ throw new Error(`Failed to get project by key: ${error.message}`);
388
+ }
389
+ }
390
+ async fetchProject(projectId) {
391
+ this._requireReady();
392
+ if (!projectId) {
393
+ throw new Error("Project ID is required");
394
+ }
395
+ return await this.query("db", (0, import_basedQuerys.buildProjectQuery)(projectId));
396
+ }
397
+ async chooseProject({
398
+ username = ((_a) => (_a = this._context.user) == null ? void 0 : _a.username)(),
399
+ projectId,
400
+ activeProject
401
+ }) {
402
+ this._requireReady();
403
+ try {
404
+ await this.call("fetchProject", {
405
+ username,
406
+ projectId,
407
+ activeProject
408
+ });
409
+ await this._client.call("setCookie", "activeProject", activeProject);
410
+ } catch (error) {
411
+ throw new Error(`Failed to choose project: ${error.message}`);
412
+ }
413
+ }
414
+ destroy() {
415
+ for (const unsubscribe of this._subscriptions.values()) {
416
+ unsubscribe();
417
+ }
418
+ this._subscriptions.clear();
419
+ this._client = null;
420
+ this._setReady(false);
421
+ }
422
+ // New helper methods for state management
423
+ async updateState(changes) {
424
+ if (!changes || Object.keys(changes).length === 0) {
425
+ return;
426
+ }
427
+ try {
428
+ await this._client.call("state:update", changes);
429
+ } catch (error) {
430
+ throw new Error(`Failed to update state: ${error.message}`);
431
+ }
432
+ }
433
+ async getState() {
434
+ try {
435
+ return await this._client.call("state:get");
436
+ } catch (error) {
437
+ throw new Error(`Failed to get state: ${error.message}`);
438
+ }
439
+ }
440
+ /**
441
+ * Upload a file to the database
442
+ * @param {File} file - The file to upload
443
+ * @param {Object} options - The options for the upload
444
+ * @returns {Promise<string>} The source of the uploaded file
445
+ * @example
446
+ * const fileInput = document.querySelector('input[type="file"]')
447
+ * const file = fileInput.files[0]
448
+ * const { id, src } = await basedService.uploadFile(file)
449
+ */
450
+ async uploadFile(file, options = {}) {
451
+ this._requireReady();
452
+ if (!file) {
453
+ throw new Error("File is required for upload");
454
+ }
455
+ try {
456
+ const { id, src } = await this._client.stream("db:file-upload", {
457
+ contents: file,
458
+ ...options
459
+ });
460
+ return { id, src };
461
+ } catch (error) {
462
+ throw new Error(`File upload failed: ${error.message}`);
463
+ }
464
+ }
465
+ async checkout({
466
+ projectId,
467
+ pkg = 2,
468
+ seats = 1,
469
+ interval = "monthly",
470
+ plan = "startup"
471
+ }) {
472
+ this._requireReady();
473
+ const prices = {
474
+ 999: { startup: "unlimited_startup", agency: "unlimited_agency" },
475
+ 2: { monthly: "starter_monthly", yearly: "starter_yearly" },
476
+ 3: { monthly: "growth_monthly", yearly: "growth_yearly" }
477
+ };
478
+ if (!projectId) {
479
+ throw new Error("Project ID is required for checkout");
480
+ }
481
+ if (!prices[pkg]) {
482
+ throw new Error(`Invalid package type: ${pkg}`);
483
+ }
484
+ try {
485
+ const price = prices[pkg][interval] || prices[pkg][plan];
486
+ if (!price) {
487
+ throw new Error(`Invalid interval/plan combination for package ${pkg}`);
488
+ }
489
+ return await this.call("checkout", {
490
+ price,
491
+ seats,
492
+ projectId
493
+ });
494
+ } catch (error) {
495
+ throw new Error(`Failed to checkout: ${error.message}`);
496
+ }
497
+ }
498
+ async updateProject(projectId, data) {
499
+ this._requireReady();
500
+ try {
501
+ return await this.call("projects:update", { projectId, data });
502
+ } catch (error) {
503
+ throw new Error(`Failed to update project: ${error.message}`);
504
+ }
505
+ }
506
+ async updateProjectComponents(projectId, components) {
507
+ this._requireReady();
508
+ try {
509
+ return await this.call("projects:update-components", {
510
+ projectId,
511
+ components
512
+ });
513
+ } catch (error) {
514
+ throw new Error(`Failed to update project components: ${error.message}`);
515
+ }
516
+ }
517
+ async updateProjectSettings(projectId, settings) {
518
+ this._requireReady();
519
+ try {
520
+ return await this.call("projects:update-settings", {
521
+ projectId,
522
+ settings
523
+ });
524
+ } catch (error) {
525
+ throw new Error(`Failed to update project settings: ${error.message}`);
526
+ }
527
+ }
528
+ async updateProjectName(projectId, name) {
529
+ this._requireReady();
530
+ try {
531
+ return await this.call("projects:update", { projectId, name });
532
+ } catch (error) {
533
+ throw new Error(`Failed to update project name: ${error.message}`);
534
+ }
535
+ }
536
+ async updateProjectPackage(projectId, pkg) {
537
+ this._requireReady();
538
+ try {
539
+ return await this.call("projects:update", {
540
+ projectId,
541
+ data: { package: pkg }
542
+ });
543
+ } catch (error) {
544
+ throw new Error(`Failed to update project package: ${error.message}`);
545
+ }
546
+ }
547
+ /**
548
+ * Update the icon of a project
549
+ * @param {string} projectId - The ID of the project to update
550
+ * @param {id, src} icon - The icon to update the project with
551
+ * @returns {Promise<Object>} The updated project
552
+ */
553
+ async updateProjectIcon(projectId, icon) {
554
+ this._requireReady();
555
+ try {
556
+ return await this.call("projects:update", {
557
+ projectId,
558
+ data: {
559
+ icon: {
560
+ $id: icon.id,
561
+ src: icon.src
562
+ }
563
+ }
564
+ });
565
+ } catch (error) {
566
+ throw new Error(`Failed to update project icon: ${error.message}`);
567
+ }
568
+ }
569
+ async createDnsRecord(domain) {
570
+ this._requireReady();
571
+ try {
572
+ return await this.call("dns:create-record", { domain });
573
+ } catch (error) {
574
+ throw new Error(`Failed to create DNS record: ${error.message}`);
575
+ }
576
+ }
577
+ async getDnsRecord(domain) {
578
+ this._requireReady();
579
+ try {
580
+ return await this.call("dns:get-record", { domain });
581
+ } catch (error) {
582
+ throw new Error(`Failed to get DNS records: ${error.message}`);
583
+ }
584
+ }
585
+ async removeDnsRecord(domain) {
586
+ this._requireReady();
587
+ try {
588
+ return await this.call("dns:remove-record", { domain });
589
+ } catch (error) {
590
+ throw new Error(`Failed to delete DNS record: ${error.message}`);
591
+ }
592
+ }
593
+ async createStorageBucket(key) {
594
+ this._requireReady();
595
+ try {
596
+ const randomString = Math.random().toString(36).slice(2, 15);
597
+ const bucket = `symbols-bucket-${key}-${randomString}`;
598
+ return await this.call("storage:create-bucket", {
599
+ bucketName: bucket,
600
+ clientName: key.split(".")[0]
601
+ });
602
+ } catch (error) {
603
+ throw new Error(`Failed to create storage bucket: ${error.message}`);
604
+ }
605
+ }
606
+ async getStorageBucket(bucketName) {
607
+ this._requireReady();
608
+ try {
609
+ return await this.call("storage:get-bucket", { bucketName });
610
+ } catch (error) {
611
+ throw new Error(`Failed to get storage bucket: ${error.message}`);
612
+ }
613
+ }
614
+ async removeStorageBucket(bucketName) {
615
+ this._requireReady();
616
+ try {
617
+ return await this.call("storage:remove-bucket", { bucketName });
618
+ } catch (error) {
619
+ throw new Error(`Failed to remove storage bucket: ${error.message}`);
620
+ }
621
+ }
622
+ /**
623
+ * Request a password change
624
+ * @returns {Promise<boolean>} True if the request was successful, false otherwise
625
+ */
626
+ async requestPasswordChange() {
627
+ this._requireReady();
628
+ try {
629
+ return await this.call("users:request-password-change", {});
630
+ } catch (error) {
631
+ throw new Error(`Failed to request password change: ${error.message}`);
632
+ }
633
+ }
634
+ /**
635
+ * Confirm a password change
636
+ * @param {string} verificationCode - The verification code
637
+ * @param {string} newPassword - The new password
638
+ * @param {string} confirmPassword - The confirmation password
639
+ * @returns {Promise<boolean>} True if the password was changed, false otherwise
640
+ */
641
+ async confirmPasswordChange(verificationCode, newPassword, confirmPassword) {
642
+ try {
643
+ return await this.call("users:confirm-password-change", {
644
+ verificationCode,
645
+ newPassword,
646
+ confirmPassword
647
+ });
648
+ } catch (error) {
649
+ throw new Error(`Failed to confirm password change: ${error.message}`);
650
+ }
651
+ }
652
+ async updateUserProfile(profileData) {
653
+ this._requireReady();
654
+ try {
655
+ return await this.call("users:update-profile", profileData);
656
+ } catch (error) {
657
+ throw new Error(`Failed to update user profile: ${error.message}`);
658
+ }
659
+ }
660
+ /**
661
+ * Duplicate a project
662
+ * @param {string} projectId - The ID of the project to duplicate
663
+ * @param {string} newName - The new name of the project (optional)
664
+ * @param {string} newKey - The new key of the project (optional)
665
+ * @returns {Promise<Object>} The duplicated project
666
+ */
667
+ async duplicateProject(projectId, newName, newKey) {
668
+ this._requireReady();
669
+ try {
670
+ return await this.call("projects:duplicate", {
671
+ projectId,
672
+ newName,
673
+ newKey
674
+ });
675
+ } catch (error) {
676
+ throw new Error(`Failed to duplicate project: ${error.message}`);
677
+ }
678
+ }
679
+ }