@symbo.ls/sdk 2.34.35 → 3.1.2

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 +2 -143
  2. package/dist/cjs/config/environment.js +30 -98
  3. package/dist/cjs/index.js +24 -144
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +305 -738
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +1185 -0
  8. package/dist/cjs/services/CoreService.js +1751 -0
  9. package/dist/cjs/services/SocketIOService.js +307 -0
  10. package/dist/cjs/services/SocketService.js +161 -0
  11. package/dist/cjs/services/SymstoryService.js +571 -0
  12. package/dist/cjs/services/index.js +16 -64
  13. package/dist/cjs/utils/TokenManager.js +30 -78
  14. package/dist/cjs/utils/basedQuerys.js +181 -0
  15. package/dist/cjs/utils/services.js +103 -301
  16. package/dist/cjs/utils/symstoryClient.js +259 -0
  17. package/dist/cjs/utils/validation.js +3 -0
  18. package/dist/esm/config/environment.js +30 -98
  19. package/dist/esm/index.js +8797 -49416
  20. package/dist/esm/services/AIService.js +185 -0
  21. package/dist/esm/services/AuthService.js +386 -1493
  22. package/dist/esm/services/BaseService.js +6 -757
  23. package/dist/esm/services/BasedService.js +5278 -0
  24. package/dist/esm/services/CoreService.js +2264 -0
  25. package/dist/esm/services/SocketIOService.js +470 -0
  26. package/dist/esm/services/SocketService.js +191 -0
  27. package/dist/esm/services/SymstoryService.js +7041 -0
  28. package/dist/esm/services/index.js +8690 -49015
  29. package/dist/esm/utils/TokenManager.js +30 -78
  30. package/dist/esm/utils/basedQuerys.js +163 -0
  31. package/dist/esm/utils/services.js +103 -301
  32. package/dist/esm/utils/symstoryClient.js +370 -0
  33. package/dist/esm/utils/validation.js +7 -4
  34. package/dist/node/config/environment.js +30 -98
  35. package/dist/node/index.js +32 -175
  36. package/dist/node/services/AIService.js +136 -0
  37. package/dist/node/services/AuthService.js +310 -742
  38. package/dist/node/services/BaseService.js +6 -148
  39. package/dist/node/services/BasedService.js +1156 -0
  40. package/dist/node/services/CoreService.js +1722 -0
  41. package/dist/node/services/SocketIOService.js +278 -0
  42. package/dist/node/services/SocketService.js +142 -0
  43. package/dist/node/services/SymstoryService.js +542 -0
  44. package/dist/node/services/index.js +16 -64
  45. package/dist/node/utils/TokenManager.js +30 -78
  46. package/dist/node/utils/basedQuerys.js +162 -0
  47. package/dist/node/utils/services.js +103 -301
  48. package/dist/node/utils/symstoryClient.js +230 -0
  49. package/dist/node/utils/validation.js +3 -0
  50. package/package.json +16 -35
  51. package/src/config/environment.js +28 -99
  52. package/src/index.js +36 -181
  53. package/src/services/AIService.js +150 -0
  54. package/src/services/AuthService.js +328 -874
  55. package/src/services/BaseService.js +6 -166
  56. package/src/services/BasedService.js +1301 -0
  57. package/src/services/CoreService.js +1943 -0
  58. package/src/services/SocketIOService.js +334 -0
  59. package/src/services/SocketService.js +168 -0
  60. package/src/services/SymstoryService.js +649 -0
  61. package/src/services/index.js +13 -80
  62. package/src/utils/TokenManager.js +33 -88
  63. package/src/utils/basedQuerys.js +164 -0
  64. package/src/utils/services.js +107 -326
  65. package/src/utils/symstoryClient.js +252 -0
  66. package/src/utils/validation.js +3 -0
  67. package/dist/cjs/services/AdminService.js +0 -351
  68. package/dist/cjs/services/BranchService.js +0 -484
  69. package/dist/cjs/services/CollabService.js +0 -743
  70. package/dist/cjs/services/DnsService.js +0 -340
  71. package/dist/cjs/services/FeatureFlagService.js +0 -175
  72. package/dist/cjs/services/FileService.js +0 -201
  73. package/dist/cjs/services/IntegrationService.js +0 -538
  74. package/dist/cjs/services/MetricsService.js +0 -62
  75. package/dist/cjs/services/PaymentService.js +0 -271
  76. package/dist/cjs/services/PlanService.js +0 -426
  77. package/dist/cjs/services/ProjectService.js +0 -1207
  78. package/dist/cjs/services/PullRequestService.js +0 -503
  79. package/dist/cjs/services/ScreenshotService.js +0 -304
  80. package/dist/cjs/services/SubscriptionService.js +0 -396
  81. package/dist/cjs/services/TrackingService.js +0 -661
  82. package/dist/cjs/services/WaitlistService.js +0 -148
  83. package/dist/cjs/state/RootStateManager.js +0 -65
  84. package/dist/cjs/state/rootEventBus.js +0 -74
  85. package/dist/cjs/utils/CollabClient.js +0 -223
  86. package/dist/cjs/utils/changePreprocessor.js +0 -199
  87. package/dist/cjs/utils/jsonDiff.js +0 -145
  88. package/dist/cjs/utils/ordering.js +0 -309
  89. package/dist/esm/services/AdminService.js +0 -1132
  90. package/dist/esm/services/BranchService.js +0 -1265
  91. package/dist/esm/services/CollabService.js +0 -26838
  92. package/dist/esm/services/DnsService.js +0 -1121
  93. package/dist/esm/services/FeatureFlagService.js +0 -956
  94. package/dist/esm/services/FileService.js +0 -982
  95. package/dist/esm/services/IntegrationService.js +0 -1319
  96. package/dist/esm/services/MetricsService.js +0 -843
  97. package/dist/esm/services/PaymentService.js +0 -1052
  98. package/dist/esm/services/PlanService.js +0 -1207
  99. package/dist/esm/services/ProjectService.js +0 -2526
  100. package/dist/esm/services/PullRequestService.js +0 -1284
  101. package/dist/esm/services/ScreenshotService.js +0 -1085
  102. package/dist/esm/services/SubscriptionService.js +0 -1177
  103. package/dist/esm/services/TrackingService.js +0 -18343
  104. package/dist/esm/services/WaitlistService.js +0 -929
  105. package/dist/esm/state/RootStateManager.js +0 -90
  106. package/dist/esm/state/rootEventBus.js +0 -56
  107. package/dist/esm/utils/CollabClient.js +0 -18901
  108. package/dist/esm/utils/changePreprocessor.js +0 -542
  109. package/dist/esm/utils/jsonDiff.js +0 -7011
  110. package/dist/esm/utils/ordering.js +0 -291
  111. package/dist/node/services/AdminService.js +0 -332
  112. package/dist/node/services/BranchService.js +0 -465
  113. package/dist/node/services/CollabService.js +0 -724
  114. package/dist/node/services/DnsService.js +0 -321
  115. package/dist/node/services/FeatureFlagService.js +0 -156
  116. package/dist/node/services/FileService.js +0 -182
  117. package/dist/node/services/IntegrationService.js +0 -519
  118. package/dist/node/services/MetricsService.js +0 -43
  119. package/dist/node/services/PaymentService.js +0 -252
  120. package/dist/node/services/PlanService.js +0 -407
  121. package/dist/node/services/ProjectService.js +0 -1188
  122. package/dist/node/services/PullRequestService.js +0 -484
  123. package/dist/node/services/ScreenshotService.js +0 -285
  124. package/dist/node/services/SubscriptionService.js +0 -377
  125. package/dist/node/services/TrackingService.js +0 -632
  126. package/dist/node/services/WaitlistService.js +0 -129
  127. package/dist/node/state/RootStateManager.js +0 -36
  128. package/dist/node/state/rootEventBus.js +0 -55
  129. package/dist/node/utils/CollabClient.js +0 -194
  130. package/dist/node/utils/changePreprocessor.js +0 -180
  131. package/dist/node/utils/jsonDiff.js +0 -116
  132. package/dist/node/utils/ordering.js +0 -290
  133. package/src/services/AdminService.js +0 -374
  134. package/src/services/BranchService.js +0 -536
  135. package/src/services/CollabService.js +0 -900
  136. package/src/services/DnsService.js +0 -366
  137. package/src/services/FeatureFlagService.js +0 -174
  138. package/src/services/FileService.js +0 -213
  139. package/src/services/IntegrationService.js +0 -548
  140. package/src/services/MetricsService.js +0 -40
  141. package/src/services/PaymentService.js +0 -287
  142. package/src/services/PlanService.js +0 -468
  143. package/src/services/ProjectService.js +0 -1366
  144. package/src/services/PullRequestService.js +0 -537
  145. package/src/services/ScreenshotService.js +0 -258
  146. package/src/services/SubscriptionService.js +0 -425
  147. package/src/services/TrackingService.js +0 -853
  148. package/src/services/WaitlistService.js +0 -130
  149. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  150. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  151. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  152. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  153. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  154. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  155. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  156. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  157. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  158. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  159. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  160. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  161. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  162. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  163. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  164. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  165. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  166. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  167. package/src/services/tests/FileService/validateFile.test.js +0 -63
  168. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  169. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  170. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  171. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  172. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  173. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  174. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  175. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  176. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  177. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  178. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  179. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  180. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  181. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  182. package/src/state/RootStateManager.js +0 -76
  183. package/src/state/rootEventBus.js +0 -67
  184. package/src/utils/CollabClient.js +0 -248
  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,649 @@
1
+ /* eslint-disable default-param-last */
2
+ /* eslint-disable no-param-reassign */
3
+ import { BaseService } from './BaseService.js'
4
+ import symstory from '../utils/symstoryClient.js'
5
+
6
+ import * as utils from '@domql/utils'
7
+ import { validateParams } from '../utils/validation.js'
8
+ const { deepStringify, isFunction, isObjectLike } = utils.default || utils
9
+
10
+ export class SymstoryService extends BaseService {
11
+ constructor (config) {
12
+ super(config)
13
+ this._client = null
14
+ this._cache = new Map()
15
+ this._state = {}
16
+ this._socketService = this._context.services.socket
17
+ this._undoStack = []
18
+ this._redoStack = []
19
+ }
20
+
21
+ async init () {
22
+ try {
23
+ const { appKey, authToken, state, socketUrl } = this._context || {}
24
+
25
+ if (!appKey) {
26
+ this._setReady(false)
27
+ return
28
+ }
29
+
30
+ // Initialize client with auth headers
31
+ symstory.init(appKey, {
32
+ headers: {
33
+ ...(authToken && { Authorization: `Bearer ${authToken}` })
34
+ }
35
+ })
36
+
37
+ this._client = symstory.client
38
+ this._state = this._isObject(state) ? state : {}
39
+
40
+ // Initialize socket service if URL provided
41
+ if (socketUrl) {
42
+ await this._socketService.init()
43
+ }
44
+
45
+ this._info = {
46
+ config: {
47
+ appKey: `${appKey.substr(0, 4)}...${appKey.substr(-4)}`,
48
+ hasToken: Boolean(authToken),
49
+ hasState: Boolean(state),
50
+ hasSocket: this._socketService._socket !== null,
51
+ socketStatus:
52
+ this._socketService._info?.config?.status || 'disconnected',
53
+ timestamp: new Date().toISOString()
54
+ }
55
+ }
56
+
57
+ this._setReady()
58
+ } catch (error) {
59
+ this._setError(error)
60
+ throw error
61
+ }
62
+ }
63
+
64
+ // publish a new version
65
+
66
+ async publish ({ version, type = 'minor' } = {}) {
67
+ if (version) {
68
+ await this._client.publishVersion(version, { type })
69
+ } else {
70
+ await this.updateData([], { type })
71
+ }
72
+ }
73
+
74
+ // get changes between versions
75
+
76
+ async getChanges ({ versionId, versionValue, branch } = {}) {
77
+ return this._client.getChanges({ versionId, versionValue, branch })
78
+ }
79
+
80
+ safeStringify (obj) {
81
+ const seen = new WeakSet()
82
+ return JSON.stringify(obj, (key, value) => {
83
+ if (typeof value === 'object' && value !== null) {
84
+ if (seen.has(value)) {
85
+ return
86
+ }
87
+ seen.add(value)
88
+ }
89
+ return value
90
+ })
91
+ }
92
+
93
+ // Update project data
94
+
95
+ async updateData (changes, options = {}, callback) {
96
+ this._requireReady()
97
+
98
+ const {
99
+ type = 'patch',
100
+ message = '',
101
+ branch = this._context.symstory?.branch || 'main',
102
+ fromSocket = false,
103
+ quietUpdate = false,
104
+ isUndo,
105
+ isRedo
106
+ } = options
107
+
108
+ try {
109
+ const { state } = this._context
110
+
111
+ // if not on the latest version don't proceed
112
+ if ('isOld' in state && state.isOld) {
113
+ return
114
+ }
115
+
116
+ const updates = changes.map(change => ({
117
+ change,
118
+ prev: state?.getByPath(change[1])
119
+ }))
120
+
121
+ // Update local state if available
122
+ if (state && 'setPathCollection' in state && !quietUpdate) {
123
+ await state.setPathCollection(changes, {
124
+ preventUpdate: true,
125
+ ...options
126
+ })
127
+ }
128
+
129
+ const filteredUpdates = updates.filter(({ change, prev }) => {
130
+ if (change && change.err) {
131
+ delete change.err
132
+ }
133
+ if (prev && prev.err) {
134
+ delete prev.err
135
+ }
136
+ return (
137
+ // eslint-disable-next-line no-undefined
138
+ change[3] !== undefined ||
139
+ this.safeStringify(change[2]) !== this.safeStringify(prev)
140
+ )
141
+ })
142
+
143
+ if (!fromSocket && !isUndo) {
144
+ if (!isRedo) {
145
+ this._redoStack.length = 0
146
+ }
147
+ this._undoStack.push({
148
+ updates: filteredUpdates,
149
+ options,
150
+ time: new Date()
151
+ })
152
+ }
153
+
154
+ // Don't proceed with backend/socket updates if change came from socket
155
+ if (fromSocket) {
156
+ return
157
+ }
158
+
159
+ // Prepare stringified data for backend and socket
160
+ const stringifiedData = changes.map(([action, path, change]) => {
161
+ if (isFunction(change)) {
162
+ return [action, path, change?.toString() ?? change]
163
+ }
164
+ if (change && change.err) {
165
+ delete change.err
166
+ }
167
+ return [
168
+ action,
169
+ path,
170
+ isObjectLike(change)
171
+ ? deepStringify(change, Array.isArray(change) ? [] : {})
172
+ : change
173
+ ]
174
+ })
175
+
176
+ const res = await this._context.services.core.applyProjectChanges(
177
+ state.projectId,
178
+ stringifiedData,
179
+ {
180
+ type,
181
+ message,
182
+ branch
183
+ }
184
+ )
185
+
186
+ // Send to socket if connected
187
+ if (this._socketService._socket) {
188
+ this._socketService.send('change', {
189
+ type: 'update',
190
+ changes: stringifiedData,
191
+ version: res?.value
192
+ })
193
+ }
194
+
195
+ if (res?.value) {
196
+ // Update context with new version
197
+ this._context.symstory = {
198
+ ...this._context.symstory,
199
+ version: res.value
200
+ }
201
+ if (state && 'quietUpdate' in state) {
202
+ const { isVersionsOpen } = state
203
+ if (isVersionsOpen) {
204
+ state.quietUpdate({ version: res.value })
205
+ } else {
206
+ state.version = res.value
207
+ }
208
+ }
209
+ // Clear cache after successful update
210
+ this._cache.clear()
211
+ }
212
+
213
+ if ('__element' in this._state && isFunction(callback)) {
214
+ await callback.call(this._state.__element, changes, res)
215
+ }
216
+
217
+ return res
218
+ } catch (error) {
219
+ if (isFunction(callback)) {
220
+ callback(error)
221
+ }
222
+ throw new Error(`Failed to update data: ${error.message}`)
223
+ }
224
+ }
225
+
226
+ async undo () {
227
+ if (!this._undoStack.length) {
228
+ throw new Error('Nothing to undo')
229
+ }
230
+ const { updates, options } = this._undoStack.pop()
231
+ const changes = updates.map(({ change, prev }) => [
232
+ change[0],
233
+ change[1],
234
+ prev
235
+ ])
236
+ this._redoStack.push({
237
+ updates: updates.map(({ change, prev }) => ({
238
+ change: [change[0], change[1], prev],
239
+ prev: change[2]
240
+ })),
241
+ options
242
+ })
243
+ await this.updateData(
244
+ changes,
245
+ { ...options, isUndo: true, message: `Undo: ${options.message || ''}` },
246
+ () => changes
247
+ )
248
+ return changes
249
+ }
250
+
251
+ async redo () {
252
+ if (!this._redoStack.length) {
253
+ throw new Error('Nothing to redo')
254
+ }
255
+ const { updates, options } = this._redoStack.pop()
256
+ const changes = updates.map(({ change, prev }) => [
257
+ change[0],
258
+ change[1],
259
+ prev
260
+ ])
261
+ await this.updateData(
262
+ changes,
263
+ { ...options, isRedo: true, message: `Redo: ${options.message || ''}` },
264
+ () => changes
265
+ )
266
+ return changes
267
+ }
268
+
269
+ // Delete project data
270
+ async deleteData (path, options = {}, callback) {
271
+ this._requireReady()
272
+
273
+ try {
274
+ const changes = [['delete', path]]
275
+ return await this.updateData(changes, options, callback)
276
+ } catch (error) {
277
+ throw new Error(`Failed to delete data: ${error.message}`)
278
+ }
279
+ }
280
+
281
+ // Get project data
282
+
283
+ async getData (query, options = {}) {
284
+ this._requireReady()
285
+
286
+ try {
287
+ const {
288
+ branch = this._context.symstory?.branch || 'main',
289
+ version = this._context.symstory?.version,
290
+ bypassCache = false,
291
+ timeout = 30000
292
+ } = options
293
+
294
+ // Generate cache key if caching is enabled
295
+ const cacheKey =
296
+ !bypassCache && this._generateCacheKey(query, branch, version)
297
+
298
+ // Check cache first if enabled
299
+ if (!bypassCache && cacheKey && this._cache.has(cacheKey)) {
300
+ return this._cache.get(cacheKey)
301
+ }
302
+
303
+ // Validate query if provided
304
+ if (query && typeof query === 'object') {
305
+ // Ensure query is properly structured
306
+ if (!query.$find && !query.$filter) {
307
+ throw new Error(
308
+ 'Invalid query structure. Must include $find or $filter.'
309
+ )
310
+ }
311
+ }
312
+
313
+ // Make the request with timeout
314
+ const controller = new AbortController()
315
+ const timeoutId = setTimeout(() => controller.abort(), timeout)
316
+
317
+ try {
318
+ const result = await this._client.get(query, branch, version)
319
+
320
+ // Cache the result if caching is enabled
321
+ if (!bypassCache && cacheKey) {
322
+ this._cache?.set(cacheKey, result)
323
+ }
324
+
325
+ return result
326
+ } finally {
327
+ clearTimeout(timeoutId)
328
+ }
329
+ } catch (error) {
330
+ if (error.name === 'AbortError') {
331
+ throw new Error(`Request timed out after ${options.timeout}ms`)
332
+ }
333
+ throw new Error(`Failed to get data: ${error.message}`)
334
+ }
335
+ }
336
+
337
+ // Helper method to check if a variable is a valid object
338
+ _isObject (variable) {
339
+ return (
340
+ variable !== null &&
341
+ typeof variable === 'object' &&
342
+ !Array.isArray(variable)
343
+ )
344
+ }
345
+
346
+ // Helper method to generate cache key
347
+ _generateCacheKey (query, branch, version) {
348
+ if (!query) {
349
+ return null
350
+ }
351
+ return JSON.stringify({
352
+ query,
353
+ branch,
354
+ version
355
+ })
356
+ }
357
+
358
+ // Helper method to clear cache
359
+ clearCache () {
360
+ this._cache.clear()
361
+ }
362
+
363
+ // Helper method to remove specific cache entry
364
+ removeCacheEntry (query, branch, version) {
365
+ const cacheKey = this._generateCacheKey(query, branch, version)
366
+ if (cacheKey) {
367
+ this._cache.delete(cacheKey)
368
+ }
369
+ }
370
+
371
+ // Branch Management
372
+ async getBranches () {
373
+ this._requireReady()
374
+
375
+ try {
376
+ return await this._client.getBranches()
377
+ } catch (error) {
378
+ throw new Error(`Failed to get branches: ${error.message}`)
379
+ }
380
+ }
381
+
382
+ async createBranch (branch, options = {}) {
383
+ this._requireReady()
384
+ if (!branch) {
385
+ throw new Error('Branch name is required.')
386
+ }
387
+ try {
388
+ return await this._client.createBranch(branch, options)
389
+ } catch (error) {
390
+ throw new Error(`Failed to create branch: ${error.message}`)
391
+ }
392
+ }
393
+
394
+ async editBranch (branch, options = {}) {
395
+ this._requireReady()
396
+ if (!branch) {
397
+ throw new Error('Branch name is required.')
398
+ }
399
+ try {
400
+ return await this._client.editBranch(branch, options)
401
+ } catch (error) {
402
+ throw new Error(`Failed to edit branch: ${error.message}`)
403
+ }
404
+ }
405
+
406
+ async deleteBranch (branch) {
407
+ this._requireReady()
408
+ if (!branch) {
409
+ throw new Error('Branch name is required.')
410
+ }
411
+ try {
412
+ return await this._client.deleteBranch(branch)
413
+ } catch (error) {
414
+ throw new Error(`Failed to delete branch: ${error.message}`)
415
+ }
416
+ }
417
+
418
+ async mergeBranch (branch, options = {}) {
419
+ this._requireReady()
420
+ if (!branch) {
421
+ throw new Error('Branch name is required.')
422
+ }
423
+ try {
424
+ return await this._client.mergeBranch(branch, options)
425
+ } catch (error) {
426
+ throw new Error(`Failed to merge branch: ${error.message}`)
427
+ }
428
+ }
429
+
430
+ async restoreVersion (version, options = {}) {
431
+ this._requireReady()
432
+
433
+ const { branch = this._context.symstory?.branch } = options
434
+
435
+ version ||= this._context.symstory?.version
436
+
437
+ try {
438
+ return await this._client.restoreVersion(version, { ...options, branch })
439
+ } catch (error) {
440
+ throw new Error(`Failed to restore version: ${error.message}`)
441
+ }
442
+ }
443
+
444
+ // Cleanup
445
+ destroy () {
446
+ this._client = null
447
+ this._setReady(false)
448
+ }
449
+
450
+ // Data management methods
451
+ async getItem (query, options = {}) {
452
+ this._requireReady()
453
+
454
+ try {
455
+ return await this.getData(query, options)
456
+ } catch (error) {
457
+ throw new Error(`Failed to get item: ${error.message}`)
458
+ }
459
+ }
460
+
461
+ async addItem (type, data, options = {}, callback) {
462
+ this._requireReady()
463
+
464
+ try {
465
+ validateParams.type(type)
466
+ validateParams.data(data, type)
467
+
468
+ const { value, ...schema } = data
469
+
470
+ return await this.updateData(
471
+ [
472
+ ['update', [type, data.key], value],
473
+ ['update', ['schema', type, data.key], schema],
474
+ ...(options.additionalChanges || [])
475
+ ],
476
+ {
477
+ message: `Created ${data.key} in ${type}`,
478
+ ...options
479
+ },
480
+ isFunction(options) ? options : callback
481
+ )
482
+ } catch (error) {
483
+ throw new Error(`Failed to add item: ${error.message}`)
484
+ }
485
+ }
486
+
487
+ async addMultipleItems (items, options = {}, callback) {
488
+ this._requireReady()
489
+
490
+ const updateData = []
491
+
492
+ items.forEach(item => {
493
+ const [type, data] = item
494
+ const { value, ...schema } = data
495
+
496
+ validateParams.type(type)
497
+ validateParams.data(data, type)
498
+
499
+ updateData.push(
500
+ ['update', [type, data.key], value],
501
+ ['update', ['schema', type, data.key], schema]
502
+ )
503
+ })
504
+
505
+ try {
506
+ return await this.updateData(
507
+ [...updateData, ...(options.additionalChanges || [])],
508
+ {
509
+ message: `Created ${updateData.length} items`,
510
+ ...options
511
+ },
512
+ isFunction(options) ? options : callback
513
+ )
514
+ } catch (error) {
515
+ throw new Error(`Failed to add item: ${error.message}`)
516
+ }
517
+ }
518
+
519
+ async updateItem (type, data, options = {}, callback) {
520
+ this._requireReady()
521
+
522
+ try {
523
+ validateParams.type(type)
524
+ validateParams.data(data, type)
525
+
526
+ const { value, ...schema } = data
527
+ return await this.updateData(
528
+ [
529
+ ['update', [type, data.key], value],
530
+ ['update', ['schema', type, data.key], schema]
531
+ ],
532
+ {
533
+ message: `Updated ${data.key} in ${type}`,
534
+ ...options
535
+ },
536
+ isFunction(options) ? options : callback
537
+ )
538
+ } catch (error) {
539
+ throw new Error(`Failed to update item: ${error.message}`)
540
+ }
541
+ }
542
+
543
+ async set (path, value, options = {}, callback) {
544
+ this._requireReady()
545
+
546
+ if (!utils.isUndefined(path) || utils.isUndefined(value)) {
547
+ return new Error(`Path ${path} or ${value} value is not defined`)
548
+ }
549
+
550
+ try {
551
+ return await this.updateData(
552
+ [['update', path, value]],
553
+ {
554
+ message: `Updated ${utils.isArray(path) ? path.join('.') : path}`,
555
+ ...options
556
+ },
557
+ isFunction(options) ? options : callback
558
+ )
559
+ } catch (error) {
560
+ throw new Error(`Failed to update item: ${error.message}`)
561
+ }
562
+ }
563
+
564
+ async deleteItem (type, key, options = {}, callback) {
565
+ this._requireReady()
566
+
567
+ try {
568
+ validateParams.type(type)
569
+ validateParams.key(key, type)
570
+
571
+ return await this.updateData(
572
+ [
573
+ ['delete', [type, key]],
574
+ ['delete', ['schema', type, key]],
575
+ ...(options.additionalChanges || [])
576
+ ],
577
+ {
578
+ message: `Deleted ${key} from ${type}`,
579
+ ...options
580
+ },
581
+ isFunction(options) ? options : callback
582
+ )
583
+ } catch (error) {
584
+ throw new Error(`Failed to delete item: ${error.message}`)
585
+ }
586
+ }
587
+
588
+ // Helper methods
589
+
590
+ _createQuery (filters = []) {
591
+ return {
592
+ $find: {
593
+ $traverse: 'children',
594
+ $filter: filters.filter(Boolean).map(([field, operator, value]) => ({
595
+ $field: field,
596
+ $operator: operator,
597
+ $value: value
598
+ }))
599
+ }
600
+ }
601
+ }
602
+
603
+ _checkRequiredContext () {
604
+ return Boolean(
605
+ this._context?.appKey && this._context?.authToken && this._client
606
+ )
607
+ }
608
+
609
+ isReady () {
610
+ if (this._checkRequiredContext()) {
611
+ this._setReady(true)
612
+ }
613
+
614
+ return this._ready
615
+ }
616
+
617
+ async switchBranch (branch) {
618
+ this._requireReady()
619
+
620
+ try {
621
+ this.updateContext({
622
+ symstory: {
623
+ ...this._context.symstory,
624
+ branch,
625
+ version: null
626
+ }
627
+ })
628
+ return await this.getData()
629
+ } catch (error) {
630
+ throw new Error(`Failed to switch branch: ${error.message}`)
631
+ }
632
+ }
633
+
634
+ async switchVersion (version) {
635
+ this._requireReady()
636
+
637
+ try {
638
+ this.updateContext({
639
+ symstory: {
640
+ ...this._context.symstory,
641
+ version
642
+ }
643
+ })
644
+ return await this.getData()
645
+ } catch (error) {
646
+ throw new Error(`Failed to switch version: ${error.message}`)
647
+ }
648
+ }
649
+ }