@symbo.ls/sdk 3.2.3 → 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 (183) hide show
  1. package/README.md +141 -0
  2. package/dist/cjs/config/environment.js +94 -10
  3. package/dist/cjs/index.js +152 -12
  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 +439 -116
  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 +60 -4
  23. package/dist/cjs/state/RootStateManager.js +2 -23
  24. package/dist/cjs/state/rootEventBus.js +9 -0
  25. package/dist/cjs/utils/CollabClient.js +78 -12
  26. package/dist/cjs/utils/TokenManager.js +16 -3
  27. package/dist/cjs/utils/changePreprocessor.js +199 -0
  28. package/dist/cjs/utils/jsonDiff.js +46 -4
  29. package/dist/cjs/utils/ordering.js +309 -0
  30. package/dist/cjs/utils/services.js +285 -128
  31. package/dist/cjs/utils/validation.js +0 -3
  32. package/dist/esm/config/environment.js +94 -10
  33. package/dist/esm/index.js +47862 -18248
  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 +24956 -16089
  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 +47373 -18027
  53. package/dist/esm/state/RootStateManager.js +11 -23
  54. package/dist/esm/state/rootEventBus.js +9 -0
  55. package/dist/esm/utils/CollabClient.js +17526 -16120
  56. package/dist/esm/utils/TokenManager.js +16 -3
  57. package/dist/esm/utils/changePreprocessor.js +542 -0
  58. package/dist/esm/utils/jsonDiff.js +958 -43
  59. package/dist/esm/utils/ordering.js +291 -0
  60. package/dist/esm/utils/services.js +285 -128
  61. package/dist/esm/utils/validation.js +116 -50
  62. package/dist/node/config/environment.js +94 -10
  63. package/dist/node/index.js +183 -16
  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 +439 -116
  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 +60 -4
  83. package/dist/node/state/RootStateManager.js +2 -23
  84. package/dist/node/state/rootEventBus.js +9 -0
  85. package/dist/node/utils/CollabClient.js +77 -11
  86. package/dist/node/utils/TokenManager.js +16 -3
  87. package/dist/node/utils/changePreprocessor.js +180 -0
  88. package/dist/node/utils/jsonDiff.js +46 -4
  89. package/dist/node/utils/ordering.js +290 -0
  90. package/dist/node/utils/services.js +285 -128
  91. package/dist/node/utils/validation.js +0 -3
  92. package/package.json +30 -18
  93. package/src/config/environment.js +95 -10
  94. package/src/index.js +190 -23
  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 +557 -148
  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 +79 -5
  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 +37 -32
  148. package/src/state/rootEventBus.js +19 -0
  149. package/src/utils/CollabClient.js +99 -12
  150. package/src/utils/TokenManager.js +20 -3
  151. package/src/utils/changePreprocessor.js +239 -0
  152. package/src/utils/jsonDiff.js +40 -5
  153. package/src/utils/ordering.js +271 -0
  154. package/src/utils/services.js +306 -139
  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 -2295
  159. package/dist/cjs/services/SocketService.js +0 -309
  160. package/dist/cjs/services/SymstoryService.js +0 -571
  161. package/dist/cjs/utils/basedQuerys.js +0 -181
  162. package/dist/cjs/utils/symstoryClient.js +0 -259
  163. package/dist/esm/services/AIService.js +0 -185
  164. package/dist/esm/services/BasedService.js +0 -5262
  165. package/dist/esm/services/CoreService.js +0 -2827
  166. package/dist/esm/services/SocketService.js +0 -456
  167. package/dist/esm/services/SymstoryService.js +0 -7025
  168. package/dist/esm/utils/basedQuerys.js +0 -163
  169. package/dist/esm/utils/symstoryClient.js +0 -354
  170. package/dist/node/services/AIService.js +0 -136
  171. package/dist/node/services/BasedService.js +0 -1156
  172. package/dist/node/services/CoreService.js +0 -2266
  173. package/dist/node/services/SocketService.js +0 -280
  174. package/dist/node/services/SymstoryService.js +0 -542
  175. package/dist/node/utils/basedQuerys.js +0 -162
  176. package/dist/node/utils/symstoryClient.js +0 -230
  177. package/src/services/AIService.js +0 -150
  178. package/src/services/BasedService.js +0 -1302
  179. package/src/services/CoreService.js +0 -2548
  180. package/src/services/SocketService.js +0 -336
  181. package/src/services/SymstoryService.js +0 -649
  182. package/src/utils/basedQuerys.js +0 -164
  183. package/src/utils/symstoryClient.js +0 -252
@@ -1,649 +0,0 @@
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
- }