@symbo.ls/sdk 3.1.1 → 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 (55) hide show
  1. package/README.md +172 -11
  2. package/dist/cjs/config/environment.js +39 -33
  3. package/dist/cjs/index.js +35 -8
  4. package/dist/cjs/services/AuthService.js +44 -3
  5. package/dist/cjs/services/BasedService.js +530 -24
  6. package/dist/cjs/services/CoreService.js +1751 -0
  7. package/dist/cjs/services/SocketIOService.js +34 -36
  8. package/dist/cjs/services/SymstoryService.js +135 -49
  9. package/dist/cjs/services/index.js +4 -4
  10. package/dist/cjs/utils/TokenManager.js +374 -0
  11. package/dist/cjs/utils/basedQuerys.js +120 -0
  12. package/dist/cjs/utils/permission.js +4 -4
  13. package/dist/cjs/utils/services.js +32 -9
  14. package/dist/cjs/utils/symstoryClient.js +32 -1
  15. package/dist/esm/config/environment.js +39 -33
  16. package/dist/esm/index.js +8964 -11076
  17. package/dist/esm/services/AuthService.js +48 -7
  18. package/dist/esm/services/BasedService.js +683 -56
  19. package/dist/esm/services/CoreService.js +2264 -0
  20. package/dist/esm/services/SocketIOService.js +71 -68
  21. package/dist/esm/services/SymstoryService.js +293 -101
  22. package/dist/esm/services/index.js +8905 -11066
  23. package/dist/esm/utils/TokenManager.js +360 -0
  24. package/dist/esm/utils/basedQuerys.js +120 -0
  25. package/dist/esm/utils/permission.js +4 -4
  26. package/dist/esm/utils/services.js +32 -9
  27. package/dist/esm/utils/symstoryClient.js +69 -33
  28. package/dist/esm/utils/validation.js +89 -19
  29. package/dist/node/config/environment.js +39 -33
  30. package/dist/node/index.js +43 -10
  31. package/dist/node/services/AuthService.js +44 -3
  32. package/dist/node/services/BasedService.js +531 -25
  33. package/dist/node/services/CoreService.js +1722 -0
  34. package/dist/node/services/SocketIOService.js +34 -36
  35. package/dist/node/services/SymstoryService.js +135 -49
  36. package/dist/node/services/index.js +4 -4
  37. package/dist/node/utils/TokenManager.js +355 -0
  38. package/dist/node/utils/basedQuerys.js +120 -0
  39. package/dist/node/utils/permission.js +4 -4
  40. package/dist/node/utils/services.js +32 -9
  41. package/dist/node/utils/symstoryClient.js +32 -1
  42. package/package.json +16 -13
  43. package/src/config/environment.js +40 -35
  44. package/src/index.js +49 -10
  45. package/src/services/AuthService.js +52 -3
  46. package/src/services/BasedService.js +602 -23
  47. package/src/services/CoreService.js +1943 -0
  48. package/src/services/SocketIOService.js +49 -71
  49. package/src/services/SymstoryService.js +150 -64
  50. package/src/services/index.js +4 -4
  51. package/src/utils/TokenManager.js +424 -0
  52. package/src/utils/basedQuerys.js +123 -0
  53. package/src/utils/permission.js +4 -4
  54. package/src/utils/services.js +32 -9
  55. package/src/utils/symstoryClient.js +35 -1
@@ -34,7 +34,7 @@ class SocketService extends BaseService {
34
34
  init() {
35
35
  try {
36
36
  const { _context, _options } = this;
37
- const socketUrl = environment.socketUrl || _options.socketUrl || "https://socket.symbols.app/";
37
+ const socketUrl = environment.socketUrl || _options.socketUrl;
38
38
  if (!socketUrl) {
39
39
  throw new Error("Socket URL is required");
40
40
  }
@@ -71,6 +71,9 @@ class SocketService extends BaseService {
71
71
  userId: (_e = _context.user) == null ? void 0 : _e.id,
72
72
  socketUrl: this._info.config.url,
73
73
  location: window.location.host,
74
+ // onConnect: () => {
75
+ // console.log('waz')
76
+ // },
74
77
  onChange: this._handleMessage.bind(this),
75
78
  sessionId: this._sessionId,
76
79
  usersName: (_f = _context.user) == null ? void 0 : _f.name,
@@ -112,7 +115,7 @@ class SocketService extends BaseService {
112
115
  send.call(
113
116
  this._socket,
114
117
  type,
115
- opts.preventDeepStringify ? payload : deepStringify(payload)
118
+ opts.useDeepStringify ? deepStringify(payload) : payload
116
119
  );
117
120
  }
118
121
  _handleMessage(event, data) {
@@ -143,38 +146,27 @@ class SocketService extends BaseService {
143
146
  }
144
147
  }
145
148
  _handleChangeEvent(data) {
146
- var _a, _b, _c;
147
- if (data.canvas) {
148
- const { clients } = data.canvas;
149
- const [firstClientKey] = Object.keys(clients);
150
- const monaco = clients && clients[firstClientKey] && clients[firstClientKey].monaco;
151
- if (monaco) {
152
- const Canvas = this._context.element && this._context.element.getCanvas();
153
- if (Canvas) {
154
- Canvas.Chosen.EditorPanels.update({}, { forceMonacoUpdate: true });
155
- }
156
- }
157
- return;
158
- }
159
- const componentUpdates = ((_a = data.schema) == null ? void 0 : _a.components) || ((_b = data.schema) == null ? void 0 : _b.pages);
160
- if (window.location.pathname.includes("/canvas") && componentUpdates) {
161
- for (const updatedComponent in componentUpdates) {
162
- if (Object.hasOwn(componentUpdates, updatedComponent)) {
163
- (_c = this._context.element) == null ? void 0 : _c.updateComponentOnCanvas(updatedComponent);
164
- }
165
- }
149
+ const { type, changes, version } = data;
150
+ if (version) this._context.state.version = version;
151
+ if (changes) {
152
+ window.requestAnimationFrame(async () => {
153
+ await this._context.state.setPathCollection(changes, {
154
+ preventReplace: type === "canvas",
155
+ preventUpdate: true,
156
+ fromSocket: true,
157
+ userId: data.userId,
158
+ changes
159
+ });
160
+ });
166
161
  }
167
162
  }
168
163
  _handleClientsEvent(data) {
169
- const { root } = Object.getPrototypeOf(this._context.state);
164
+ const { root } = this._context.state;
170
165
  root.replace(
171
166
  { clients: data },
172
167
  {
173
- fromSync: true,
174
- preventUpdate: ["Iframe"],
175
- preventModalUpdate: true,
176
- preventStateUpdateListener: true,
177
- execStateFunction: true
168
+ fromSocket: true,
169
+ preventUpdate: true
178
170
  }
179
171
  );
180
172
  }
@@ -191,9 +183,9 @@ class SocketService extends BaseService {
191
183
  element.__ref.root,
192
184
  {},
193
185
  {
194
- fromSync: true,
186
+ fromSocket: true,
195
187
  updateStateOptions: {
196
- fromSync: true,
188
+ fromSocket: true,
197
189
  preventStateUpdateListener: 1
198
190
  // !isModalRoute(data.route, element)
199
191
  }
@@ -208,9 +200,9 @@ class SocketService extends BaseService {
208
200
  element.__ref.root,
209
201
  {},
210
202
  {
211
- fromSync: true,
203
+ fromSocket: true,
212
204
  updateStateOptions: {
213
- fromSync: true
205
+ fromSocket: true
214
206
  }
215
207
  }
216
208
  );
@@ -230,11 +222,17 @@ class SocketService extends BaseService {
230
222
  this._reconnectAttempts++;
231
223
  this._updateStatus("reconnecting");
232
224
  setTimeout(() => {
233
- this.connect().then(() => {
234
- this._reconnectAttempts = 0;
235
- }).catch(() => {
225
+ try {
226
+ const connected = this.connect();
227
+ if (connected) {
228
+ this._reconnectAttempts = 0;
229
+ } else {
230
+ this._handleReconnect();
231
+ }
232
+ } catch (error) {
233
+ console.error("Reconnection failed:", error);
236
234
  this._handleReconnect();
237
- });
235
+ }
238
236
  }, this._reconnectDelay * this._reconnectAttempts);
239
237
  } else {
240
238
  this._updateStatus("failed");
@@ -8,7 +8,7 @@ class SymstoryService extends BaseService {
8
8
  super(config);
9
9
  this._client = null;
10
10
  this._cache = /* @__PURE__ */ new Map();
11
- this._state = null;
11
+ this._state = {};
12
12
  this._socketService = this._context.services.socket;
13
13
  this._undoStack = [];
14
14
  this._redoStack = [];
@@ -27,7 +27,7 @@ class SymstoryService extends BaseService {
27
27
  }
28
28
  });
29
29
  this._client = symstory.client;
30
- this._state = state;
30
+ this._state = this._isObject(state) ? state : {};
31
31
  if (socketUrl) {
32
32
  await this._socketService.init();
33
33
  }
@@ -47,6 +47,30 @@ class SymstoryService extends BaseService {
47
47
  throw error;
48
48
  }
49
49
  }
50
+ // publish a new version
51
+ async publish({ version, type = "minor" } = {}) {
52
+ if (version) {
53
+ await this._client.publishVersion(version, { type });
54
+ } else {
55
+ await this.updateData([], { type });
56
+ }
57
+ }
58
+ // get changes between versions
59
+ async getChanges({ versionId, versionValue, branch } = {}) {
60
+ return this._client.getChanges({ versionId, versionValue, branch });
61
+ }
62
+ safeStringify(obj) {
63
+ const seen = /* @__PURE__ */ new WeakSet();
64
+ return JSON.stringify(obj, (key, value) => {
65
+ if (typeof value === "object" && value !== null) {
66
+ if (seen.has(value)) {
67
+ return;
68
+ }
69
+ seen.add(value);
70
+ }
71
+ return value;
72
+ });
73
+ }
50
74
  // Update project data
51
75
  async updateData(changes, options = {}, callback) {
52
76
  var _a;
@@ -61,35 +85,33 @@ class SymstoryService extends BaseService {
61
85
  isRedo
62
86
  } = options;
63
87
  try {
64
- if ("isOld" in this._context.state && this._context.state.isOld) {
88
+ const { state } = this._context;
89
+ if ("isOld" in state && state.isOld) {
65
90
  return;
66
91
  }
67
- const updates = changes.map((change) => {
68
- var _a2;
69
- return {
70
- change,
71
- prev: (_a2 = this._context.state) == null ? void 0 : _a2.getByPath(change[1])
72
- };
73
- });
74
- if (this._context.state && "setPathCollection" in this._context.state && !quietUpdate) {
75
- this._context.state.setPathCollection(changes, {
92
+ const updates = changes.map((change) => ({
93
+ change,
94
+ prev: state == null ? void 0 : state.getByPath(change[1])
95
+ }));
96
+ if (state && "setPathCollection" in state && !quietUpdate) {
97
+ await state.setPathCollection(changes, {
76
98
  preventUpdate: true,
77
99
  ...options
78
100
  });
79
- if ("__element" in this._state && isFunction(callback)) {
80
- callback.call(this._state.__element);
81
- }
82
- }
83
- const filteredUpdates = updates.filter(
84
- ({ change, prev }) => change[3] !== void 0 || JSON.stringify(change[2]) !== JSON.stringify(prev)
85
- );
86
- if (!filteredUpdates.length) {
87
- return;
88
101
  }
89
- if (!fromSocket && !isUndo) {
90
- if (!updates.length) {
91
- return;
102
+ const filteredUpdates = updates.filter(({ change, prev }) => {
103
+ if (change && change.err) {
104
+ delete change.err;
105
+ }
106
+ if (prev && prev.err) {
107
+ delete prev.err;
92
108
  }
109
+ return (
110
+ // eslint-disable-next-line no-undefined
111
+ change[3] !== void 0 || this.safeStringify(change[2]) !== this.safeStringify(prev)
112
+ );
113
+ });
114
+ if (!fromSocket && !isUndo) {
93
115
  if (!isRedo) {
94
116
  this._redoStack.length = 0;
95
117
  }
@@ -106,27 +128,49 @@ class SymstoryService extends BaseService {
106
128
  if (isFunction(change)) {
107
129
  return [action, path, (change == null ? void 0 : change.toString()) ?? change];
108
130
  }
131
+ if (change && change.err) {
132
+ delete change.err;
133
+ }
109
134
  return [
110
135
  action,
111
136
  path,
112
137
  isObjectLike(change) ? deepStringify(change, Array.isArray(change) ? [] : {}) : change
113
138
  ];
114
139
  });
115
- const res = await this._client.update(stringifiedData, {
116
- type,
117
- message,
118
- branch
119
- });
140
+ const res = await this._context.services.core.applyProjectChanges(
141
+ state.projectId,
142
+ stringifiedData,
143
+ {
144
+ type,
145
+ message,
146
+ branch
147
+ }
148
+ );
149
+ if (this._socketService._socket) {
150
+ this._socketService.send("change", {
151
+ type: "update",
152
+ changes: stringifiedData,
153
+ version: res == null ? void 0 : res.value
154
+ });
155
+ }
120
156
  if (res == null ? void 0 : res.value) {
121
157
  this._context.symstory = {
122
158
  ...this._context.symstory,
123
159
  version: res.value
124
160
  };
125
- if (this._context.state && "quietUpdate" in this._context.state) {
126
- this._context.state.quietUpdate({ version: res.value });
161
+ if (state && "quietUpdate" in state) {
162
+ const { isVersionsOpen } = state;
163
+ if (isVersionsOpen) {
164
+ state.quietUpdate({ version: res.value });
165
+ } else {
166
+ state.version = res.value;
167
+ }
127
168
  }
128
169
  this._cache.clear();
129
170
  }
171
+ if ("__element" in this._state && isFunction(callback)) {
172
+ await callback.call(this._state.__element, changes, res);
173
+ }
130
174
  return res;
131
175
  } catch (error) {
132
176
  if (isFunction(callback)) {
@@ -152,13 +196,12 @@ class SymstoryService extends BaseService {
152
196
  })),
153
197
  options
154
198
  });
155
- return new Promise(
156
- (resolve) => this.updateData(
157
- changes,
158
- { ...options, isUndo: true, message: `Undo: ${options.message || ""}` },
159
- () => resolve(changes)
160
- )
199
+ await this.updateData(
200
+ changes,
201
+ { ...options, isUndo: true, message: `Undo: ${options.message || ""}` },
202
+ () => changes
161
203
  );
204
+ return changes;
162
205
  }
163
206
  async redo() {
164
207
  if (!this._redoStack.length) {
@@ -170,27 +213,26 @@ class SymstoryService extends BaseService {
170
213
  change[1],
171
214
  prev
172
215
  ]);
173
- return new Promise(
174
- (resolve) => this.updateData(
175
- changes,
176
- { ...options, isRedo: true, message: `Redo: ${options.message || ""}` },
177
- () => resolve(changes)
178
- )
216
+ await this.updateData(
217
+ changes,
218
+ { ...options, isRedo: true, message: `Redo: ${options.message || ""}` },
219
+ () => changes
179
220
  );
221
+ return changes;
180
222
  }
181
223
  // Delete project data
182
- async deleteData(path, options = {}) {
224
+ async deleteData(path, options = {}, callback) {
183
225
  this._requireReady();
184
226
  try {
185
227
  const changes = [["delete", path]];
186
- return await this._client.update(changes, options);
228
+ return await this.updateData(changes, options, callback);
187
229
  } catch (error) {
188
230
  throw new Error(`Failed to delete data: ${error.message}`);
189
231
  }
190
232
  }
191
233
  // Get project data
192
234
  async getData(query, options = {}) {
193
- var _a, _b;
235
+ var _a, _b, _c;
194
236
  this._requireReady();
195
237
  try {
196
238
  const {
@@ -215,7 +257,7 @@ class SymstoryService extends BaseService {
215
257
  try {
216
258
  const result = await this._client.get(query, branch, version);
217
259
  if (!bypassCache && cacheKey) {
218
- this._cache.set(cacheKey, result);
260
+ (_c = this._cache) == null ? void 0 : _c.set(cacheKey, result);
219
261
  }
220
262
  return result;
221
263
  } finally {
@@ -228,6 +270,10 @@ class SymstoryService extends BaseService {
228
270
  throw new Error(`Failed to get data: ${error.message}`);
229
271
  }
230
272
  }
273
+ // Helper method to check if a variable is a valid object
274
+ _isObject(variable) {
275
+ return variable !== null && typeof variable === "object" && !Array.isArray(variable);
276
+ }
231
277
  // Helper method to generate cache key
232
278
  _generateCacheKey(query, branch, version) {
233
279
  if (!query) {
@@ -261,6 +307,9 @@ class SymstoryService extends BaseService {
261
307
  }
262
308
  async createBranch(branch, options = {}) {
263
309
  this._requireReady();
310
+ if (!branch) {
311
+ throw new Error("Branch name is required.");
312
+ }
264
313
  try {
265
314
  return await this._client.createBranch(branch, options);
266
315
  } catch (error) {
@@ -269,6 +318,9 @@ class SymstoryService extends BaseService {
269
318
  }
270
319
  async editBranch(branch, options = {}) {
271
320
  this._requireReady();
321
+ if (!branch) {
322
+ throw new Error("Branch name is required.");
323
+ }
272
324
  try {
273
325
  return await this._client.editBranch(branch, options);
274
326
  } catch (error) {
@@ -277,6 +329,9 @@ class SymstoryService extends BaseService {
277
329
  }
278
330
  async deleteBranch(branch) {
279
331
  this._requireReady();
332
+ if (!branch) {
333
+ throw new Error("Branch name is required.");
334
+ }
280
335
  try {
281
336
  return await this._client.deleteBranch(branch);
282
337
  } catch (error) {
@@ -285,6 +340,9 @@ class SymstoryService extends BaseService {
285
340
  }
286
341
  async mergeBranch(branch, options = {}) {
287
342
  this._requireReady();
343
+ if (!branch) {
344
+ throw new Error("Branch name is required.");
345
+ }
288
346
  try {
289
347
  return await this._client.mergeBranch(branch, options);
290
348
  } catch (error) {
@@ -325,7 +383,8 @@ class SymstoryService extends BaseService {
325
383
  return await this.updateData(
326
384
  [
327
385
  ["update", [type, data.key], value],
328
- ["update", ["schema", type, data.key], schema]
386
+ ["update", ["schema", type, data.key], schema],
387
+ ...options.additionalChanges || []
329
388
  ],
330
389
  {
331
390
  message: `Created ${data.key} in ${type}`,
@@ -337,6 +396,32 @@ class SymstoryService extends BaseService {
337
396
  throw new Error(`Failed to add item: ${error.message}`);
338
397
  }
339
398
  }
399
+ async addMultipleItems(items, options = {}, callback) {
400
+ this._requireReady();
401
+ const updateData = [];
402
+ items.forEach((item) => {
403
+ const [type, data] = item;
404
+ const { value, ...schema } = data;
405
+ validateParams.type(type);
406
+ validateParams.data(data, type);
407
+ updateData.push(
408
+ ["update", [type, data.key], value],
409
+ ["update", ["schema", type, data.key], schema]
410
+ );
411
+ });
412
+ try {
413
+ return await this.updateData(
414
+ [...updateData, ...options.additionalChanges || []],
415
+ {
416
+ message: `Created ${updateData.length} items`,
417
+ ...options
418
+ },
419
+ isFunction(options) ? options : callback
420
+ );
421
+ } catch (error) {
422
+ throw new Error(`Failed to add item: ${error.message}`);
423
+ }
424
+ }
340
425
  async updateItem(type, data, options = {}, callback) {
341
426
  this._requireReady();
342
427
  try {
@@ -384,7 +469,8 @@ class SymstoryService extends BaseService {
384
469
  return await this.updateData(
385
470
  [
386
471
  ["delete", [type, key]],
387
- ["delete", ["schema", type, key]]
472
+ ["delete", ["schema", type, key]],
473
+ ...options.additionalChanges || []
388
474
  ],
389
475
  {
390
476
  message: `Deleted ${key} from ${type}`,
@@ -1,23 +1,23 @@
1
- import { BasedService } from "./BasedService.js";
2
1
  import { SymstoryService } from "./SymstoryService.js";
3
2
  import { AuthService } from "./AuthService.js";
4
3
  import { AIService } from "./AIService.js";
5
4
  import { SocketService } from "./SocketIOService.js";
5
+ import { CoreService } from "./CoreService.js";
6
6
  const createService = (ServiceClass, config) => new ServiceClass(config);
7
- const createBasedService = (config) => createService(BasedService, config);
8
7
  const createSymstoryService = (config) => createService(SymstoryService, config);
9
8
  const createAuthService = (config) => createService(AuthService, config);
10
9
  const createAIService = (config) => createService(AIService, config);
11
10
  const createSocketService = (config) => createService(SocketService, config);
11
+ const createCoreService = (config) => createService(CoreService, config);
12
12
  export {
13
13
  AIService,
14
14
  AuthService,
15
- BasedService,
15
+ CoreService,
16
16
  SocketService,
17
17
  SymstoryService,
18
18
  createAIService,
19
19
  createAuthService,
20
- createBasedService,
20
+ createCoreService,
21
21
  createSocketService,
22
22
  createSymstoryService
23
23
  };