@scryme/chat 0.0.1 → 2.13.4

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.
package/dist/sdk.js CHANGED
@@ -55,16 +55,30 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
55
  return to.concat(ar || Array.prototype.slice.call(from));
56
56
  };
57
57
  import axios from 'axios';
58
+ import { setGlobalToken } from './custom-instance';
58
59
  import { getSkyrmeChatAPI } from './generated/v3-server';
60
+ /**
61
+ * The primary client SDK class for accessing the Scryme Chat platform APIs.
62
+ * Supports automated OAuth2 Token management and provides clean, type-safe namespaces.
63
+ */
59
64
  var ScrymeSDK = /** @class */ (function () {
65
+ /**
66
+ * Constructs a new ScrymeSDK instance.
67
+ * @param options Configuration options for baseURL, tokens, and credentials.
68
+ */
60
69
  function ScrymeSDK(options) {
61
70
  if (options === void 0) { options = {}; }
62
71
  var _a;
72
+ /** The currently cached access token. */
63
73
  this.token = null;
74
+ /** Timestamp in milliseconds indicating when the cached token will expire. */
64
75
  this.tokenExpiresAt = null;
65
76
  this.clientId = options.clientId;
66
77
  this.clientSecret = options.clientSecret;
67
78
  this.token = options.token || null;
79
+ if (this.token) {
80
+ this.syncToken(this.token);
81
+ }
68
82
  var url = options.baseURL || '';
69
83
  if (!url && typeof window !== 'undefined') {
70
84
  url = window.localStorage.getItem('CUSTOM_API_URL') || '';
@@ -73,7 +87,9 @@ var ScrymeSDK = /** @class */ (function () {
73
87
  var g = globalThis;
74
88
  var env = ((_a = g.process) === null || _a === void 0 ? void 0 : _a.env) || g.__env__ || {};
75
89
  var isProd = env.NODE_ENV === 'production' ||
76
- (typeof window !== 'undefined' && window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1');
90
+ (typeof window !== 'undefined' &&
91
+ window.location.hostname !== 'localhost' &&
92
+ window.location.hostname !== '127.0.0.1');
77
93
  url =
78
94
  env.API_URL ||
79
95
  env.NEXT_PUBLIC_API_URL ||
@@ -83,7 +99,23 @@ var ScrymeSDK = /** @class */ (function () {
83
99
  this.baseURL = url.replace(/\/$/, '');
84
100
  }
85
101
  /**
86
- * Automatically retrieves or refreshes the M2M OAuth2 Token using client_credentials
102
+ * Synchronizes the authentication token to local storage and the global configuration.
103
+ * @param token The token to synchronize.
104
+ */
105
+ ScrymeSDK.prototype.syncToken = function (token) {
106
+ if (!token)
107
+ return;
108
+ setGlobalToken(token);
109
+ if (typeof window !== 'undefined') {
110
+ window.localStorage.setItem('bearer_token', token);
111
+ window.localStorage.setItem('better-auth.session-token', token);
112
+ window.localStorage.setItem('better-auth.session_token', token);
113
+ }
114
+ };
115
+ /**
116
+ * Automatically retrieves a cached token, or fetches a new one via M2M OAuth2 Client Credentials
117
+ * if a clientId and clientSecret are configured.
118
+ * @returns A promise resolving to the token string, or null if unauthenticated.
87
119
  */
88
120
  ScrymeSDK.prototype.getOrFetchToken = function () {
89
121
  return __awaiter(this, void 0, void 0, function () {
@@ -111,6 +143,7 @@ var ScrymeSDK = /** @class */ (function () {
111
143
  response = _e.sent();
112
144
  if (((_a = response.data) === null || _a === void 0 ? void 0 : _a.success) && ((_c = (_b = response.data) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.access_token)) {
113
145
  this.token = response.data.data.access_token;
146
+ this.syncToken(this.token);
114
147
  if (response.data.data.expires_in) {
115
148
  // Expire 10 seconds early as a safety buffer
116
149
  this.tokenExpiresAt = Date.now() + (response.data.data.expires_in - 10) * 1000;
@@ -123,6 +156,7 @@ var ScrymeSDK = /** @class */ (function () {
123
156
  else if ((_d = response.data) === null || _d === void 0 ? void 0 : _d.access_token) {
124
157
  // Fallback in case response is not wrapped
125
158
  this.token = response.data.access_token;
159
+ this.syncToken(this.token);
126
160
  if (response.data.expires_in) {
127
161
  this.tokenExpiresAt = Date.now() + (response.data.expires_in - 10) * 1000;
128
162
  }
@@ -142,7 +176,8 @@ var ScrymeSDK = /** @class */ (function () {
142
176
  });
143
177
  };
144
178
  /**
145
- * Gets the axios request config containing authorization and base url
179
+ * Generates the default request configuration containing the authorization headers and baseURL.
180
+ * @returns Request configuration object.
146
181
  */
147
182
  ScrymeSDK.prototype.getRequestConfig = function () {
148
183
  return __awaiter(this, void 0, void 0, function () {
@@ -214,67 +249,155 @@ var ScrymeSDK = /** @class */ (function () {
214
249
  });
215
250
  Object.defineProperty(ScrymeSDK.prototype, "workspace", {
216
251
  // --- High-level nested namespace chains for excellent DX ---
252
+ /**
253
+ * Operations for managing workspaces, including creation, updating, retrieval,
254
+ * members, and channels.
255
+ */
217
256
  get: function () {
218
257
  var _this = this;
219
258
  return {
259
+ /**
260
+ * Lists all workspaces in the authenticated organization context.
261
+ * @param options Optional request config override.
262
+ * @returns List of workspaces returned exactly from the endpoint.
263
+ */
220
264
  list: function (options) { return __awaiter(_this, void 0, void 0, function () {
221
265
  return __generator(this, function (_a) {
222
266
  return [2 /*return*/, this.raw.v3WorkspacesControllerGetWorkspaces(options)];
223
267
  });
224
268
  }); },
269
+ /**
270
+ * Retrieves detailed information of a specific workspace by its slug.
271
+ * @param slug The unique workspace slug identifier.
272
+ * @param options Optional request config override.
273
+ * @returns Workspace details returned exactly from the endpoint.
274
+ */
225
275
  get: function (slug, options) { return __awaiter(_this, void 0, void 0, function () {
226
276
  return __generator(this, function (_a) {
227
277
  return [2 /*return*/, this.raw.v3WorkspacesControllerGetWorkspaceBySlug(slug, options)];
228
278
  });
229
279
  }); },
280
+ /**
281
+ * Provisions a new workspace inside the organization.
282
+ * @param data Workspace creation and configuration data.
283
+ * @param options Optional request config override.
284
+ * @returns Provisioned workspace and bot configuration exactly from the endpoint.
285
+ */
230
286
  create: function (data, options) { return __awaiter(_this, void 0, void 0, function () {
231
287
  return __generator(this, function (_a) {
232
288
  return [2 /*return*/, this.raw.v3WorkspacesControllerProvisionWorkspace(data, options)];
233
289
  });
234
290
  }); },
291
+ /**
292
+ * Updates the configurations and metadata of an existing workspace.
293
+ * @param slug The unique workspace slug identifier.
294
+ * @param data Fields to update.
295
+ * @param options Optional request config override.
296
+ * @returns The updated workspace details exactly from the endpoint.
297
+ */
235
298
  update: function (slug, data, options) { return __awaiter(_this, void 0, void 0, function () {
236
299
  return __generator(this, function (_a) {
237
300
  return [2 /*return*/, this.raw.v3WorkspacesControllerUpdateWorkspace(slug, data, options)];
238
301
  });
239
302
  }); },
303
+ /**
304
+ * Permanently deletes a specific workspace by its slug.
305
+ * @param slug The unique workspace slug identifier.
306
+ * @param options Optional request config override.
307
+ * @returns Deletion status response exactly from the endpoint.
308
+ */
240
309
  delete: function (slug, options) { return __awaiter(_this, void 0, void 0, function () {
241
310
  return __generator(this, function (_a) {
242
311
  return [2 /*return*/, this.raw.v3WorkspacesControllerDeleteWorkspace(slug, options)];
243
312
  });
244
313
  }); },
314
+ /**
315
+ * Operations for managing workspace members, including listing, adding, role updates, and removal.
316
+ */
245
317
  members: {
318
+ /**
319
+ * Lists all members currently in a workspace.
320
+ * @param slug The unique workspace slug identifier.
321
+ * @param options Optional request config override.
322
+ * @returns List of workspace members exactly from the endpoint.
323
+ */
246
324
  list: function (slug, options) { return __awaiter(_this, void 0, void 0, function () {
247
325
  return __generator(this, function (_a) {
248
326
  return [2 /*return*/, this.raw.v3WorkspacesControllerGetWorkspaceMembers(slug, options)];
249
327
  });
250
328
  }); },
329
+ /**
330
+ * Adds a new member to the workspace.
331
+ * @param slug The unique workspace slug identifier.
332
+ * @param data Input DTO containing the user's email and role.
333
+ * @param options Optional request config override.
334
+ * @returns Newly added member details exactly from the endpoint.
335
+ */
251
336
  add: function (slug, data, options) { return __awaiter(_this, void 0, void 0, function () {
252
337
  return __generator(this, function (_a) {
253
338
  return [2 /*return*/, this.raw.v3WorkspacesControllerAddWorkspaceMember(slug, data, options)];
254
339
  });
255
340
  }); },
341
+ /**
342
+ * Retrieves membership details of a specific member in a workspace.
343
+ * @param slug The unique workspace slug identifier.
344
+ * @param memberId Unique ID of the workspace member (user ID).
345
+ * @param options Optional request config override.
346
+ * @returns Workspace member details exactly from the endpoint.
347
+ */
256
348
  get: function (slug, memberId, options) { return __awaiter(_this, void 0, void 0, function () {
257
349
  return __generator(this, function (_a) {
258
350
  return [2 /*return*/, this.raw.v3WorkspacesControllerGetWorkspaceMember(slug, memberId, options)];
259
351
  });
260
352
  }); },
353
+ /**
354
+ * Updates the role or configuration of a workspace member.
355
+ * @param slug The unique workspace slug identifier.
356
+ * @param memberId Unique ID of the workspace member (user ID).
357
+ * @param data Update details containing the target role.
358
+ * @param options Optional request config override.
359
+ * @returns The updated workspace member details exactly from the endpoint.
360
+ */
261
361
  update: function (slug, memberId, data, options) { return __awaiter(_this, void 0, void 0, function () {
262
362
  return __generator(this, function (_a) {
263
363
  return [2 /*return*/, this.raw.v3WorkspacesControllerUpdateWorkspaceMember(slug, memberId, data, options)];
264
364
  });
265
365
  }); },
366
+ /**
367
+ * Removes a member from the workspace.
368
+ * @param slug The unique workspace slug identifier.
369
+ * @param memberId Unique ID of the workspace member (user ID).
370
+ * @param options Optional request config override.
371
+ * @returns Workspace member deletion confirmation exactly from the endpoint.
372
+ */
266
373
  delete: function (slug, memberId, options) { return __awaiter(_this, void 0, void 0, function () {
267
374
  return __generator(this, function (_a) {
268
375
  return [2 /*return*/, this.raw.v3WorkspacesControllerDeleteWorkspaceMember(slug, memberId, options)];
269
376
  });
270
377
  }); },
271
378
  },
379
+ /**
380
+ * Operations for listing and creating channels inside a workspace.
381
+ */
272
382
  channels: {
383
+ /**
384
+ * Lists all public channels (and private channels the user has access to) in a workspace.
385
+ * @param slug The unique workspace slug identifier.
386
+ * @param options Optional request config override.
387
+ * @returns List of channels returned exactly from the endpoint.
388
+ */
273
389
  list: function (slug, options) { return __awaiter(_this, void 0, void 0, function () {
274
390
  return __generator(this, function (_a) {
275
391
  return [2 /*return*/, this.raw.channelsControllerGetWorkspaceChannels(slug, options)];
276
392
  });
277
393
  }); },
394
+ /**
395
+ * Creates a new channel within a workspace.
396
+ * @param slug The unique workspace slug identifier.
397
+ * @param data Configuration DTO for the new channel.
398
+ * @param options Optional request config override.
399
+ * @returns Details of the created channel exactly from the endpoint.
400
+ */
278
401
  create: function (slug, data, options) { return __awaiter(_this, void 0, void 0, function () {
279
402
  return __generator(this, function (_a) {
280
403
  return [2 /*return*/, this.raw.channelsControllerCreateChannel(slug, data, options)];
@@ -287,30 +410,71 @@ var ScrymeSDK = /** @class */ (function () {
287
410
  configurable: true
288
411
  });
289
412
  Object.defineProperty(ScrymeSDK.prototype, "channel", {
413
+ /**
414
+ * Operations for managing specific channels and channel message actions.
415
+ */
290
416
  get: function () {
291
417
  var _this = this;
292
418
  return {
419
+ /**
420
+ * Retrieves detailed information of a specific channel.
421
+ * @param slug The unique workspace slug identifier.
422
+ * @param channelId Unique identifier of the channel.
423
+ * @param options Optional request config override.
424
+ * @returns Channel details returned exactly from the endpoint.
425
+ */
293
426
  get: function (slug, channelId, options) { return __awaiter(_this, void 0, void 0, function () {
294
427
  return __generator(this, function (_a) {
295
428
  return [2 /*return*/, this.raw.channelsControllerGetChannel(slug, channelId, options)];
296
429
  });
297
430
  }); },
431
+ /**
432
+ * Updates configuration, description, icon or status of an existing channel.
433
+ * @param slug The unique workspace slug identifier.
434
+ * @param channelId Unique identifier of the channel.
435
+ * @param data Configuration options to update.
436
+ * @param options Optional request config override.
437
+ * @returns The updated channel details exactly from the endpoint.
438
+ */
298
439
  update: function (slug, channelId, data, options) { return __awaiter(_this, void 0, void 0, function () {
299
440
  return __generator(this, function (_a) {
300
441
  return [2 /*return*/, this.raw.channelsControllerUpdateChannel(slug, channelId, data, options)];
301
442
  });
302
443
  }); },
444
+ /**
445
+ * Permanently deletes a channel from a workspace.
446
+ * @param slug The unique workspace slug identifier.
447
+ * @param channelId Unique identifier of the channel to delete.
448
+ * @param options Optional request config override.
449
+ * @returns Success status indicating that the channel was deleted exactly from the endpoint.
450
+ */
303
451
  delete: function (slug, channelId, options) { return __awaiter(_this, void 0, void 0, function () {
304
452
  return __generator(this, function (_a) {
305
453
  return [2 /*return*/, this.raw.channelsControllerDeleteChannel(slug, channelId, options)];
306
454
  });
307
455
  }); },
456
+ /**
457
+ * Sub-namespace for managing messages inside a channel.
458
+ */
308
459
  message: {
460
+ /**
461
+ * Lists messages in a channel with cursor pagination support.
462
+ * @param channelId Unique identifier of the channel.
463
+ * @param params Query parameters for limiting, sorting, or pagination cursors.
464
+ * @param options Optional request config override.
465
+ * @returns Object containing the messages array and next pagination cursor exactly from the endpoint.
466
+ */
309
467
  list: function (channelId, params, options) { return __awaiter(_this, void 0, void 0, function () {
310
468
  return __generator(this, function (_a) {
311
469
  return [2 /*return*/, this.raw.channelsControllerGetMessages(channelId, params, options)];
312
470
  });
313
471
  }); },
472
+ /**
473
+ * Sends a new message to a channel.
474
+ * @param channelId Unique identifier of the target channel.
475
+ * @param options Optional request config override. Note that standard message data (like text content) can be passed inside options.data.
476
+ * @returns The created message exactly from the endpoint.
477
+ */
314
478
  create: function (channelId, options) { return __awaiter(_this, void 0, void 0, function () {
315
479
  return __generator(this, function (_a) {
316
480
  return [2 /*return*/, this.raw.channelsControllerCreateMessage(channelId, options)];
@@ -323,24 +487,58 @@ var ScrymeSDK = /** @class */ (function () {
323
487
  configurable: true
324
488
  });
325
489
  Object.defineProperty(ScrymeSDK.prototype, "message", {
490
+ /**
491
+ * Operations for modifying, reacting to, or deleting existing channel messages.
492
+ */
326
493
  get: function () {
327
494
  var _this = this;
328
495
  return {
496
+ /**
497
+ * Updates the content of a previously sent message.
498
+ * @param channelId Unique identifier of the channel containing the message.
499
+ * @param messageId Unique identifier of the message to update.
500
+ * @param data The new content payload.
501
+ * @param options Optional request config override.
502
+ * @returns The updated message details exactly from the endpoint.
503
+ */
329
504
  update: function (channelId, messageId, data, options) { return __awaiter(_this, void 0, void 0, function () {
330
505
  return __generator(this, function (_a) {
331
506
  return [2 /*return*/, this.raw.channelsControllerUpdateMessage(channelId, messageId, data, options)];
332
507
  });
333
508
  }); },
509
+ /**
510
+ * Permanently deletes a message.
511
+ * @param channelId Unique identifier of the channel containing the message.
512
+ * @param messageId Unique identifier of the message to delete.
513
+ * @param options Optional request config override.
514
+ * @returns Success status indicating that the message was deleted exactly from the endpoint.
515
+ */
334
516
  delete: function (channelId, messageId, options) { return __awaiter(_this, void 0, void 0, function () {
335
517
  return __generator(this, function (_a) {
336
518
  return [2 /*return*/, this.raw.channelsControllerDeleteMessage(channelId, messageId, options)];
337
519
  });
338
520
  }); },
521
+ /**
522
+ * Adds a reaction (emoji) to a message.
523
+ * @param channelId Unique identifier of the channel containing the message.
524
+ * @param messageId Unique identifier of the message.
525
+ * @param data Object containing the target emoji character.
526
+ * @param options Optional request config override.
527
+ * @returns The reaction response returned exactly from the endpoint.
528
+ */
339
529
  addReaction: function (channelId, messageId, data, options) { return __awaiter(_this, void 0, void 0, function () {
340
530
  return __generator(this, function (_a) {
341
531
  return [2 /*return*/, this.raw.channelsControllerAddReaction(channelId, messageId, data, options)];
342
532
  });
343
533
  }); },
534
+ /**
535
+ * Removes a reaction (emoji) from a message.
536
+ * @param channelId Unique identifier of the channel containing the message.
537
+ * @param messageId Unique identifier of the message.
538
+ * @param emoji The emoji character to remove.
539
+ * @param options Optional request config override.
540
+ * @returns The reaction removal response returned exactly from the endpoint.
541
+ */
344
542
  removeReaction: function (channelId, messageId, emoji, options) { return __awaiter(_this, void 0, void 0, function () {
345
543
  return __generator(this, function (_a) {
346
544
  return [2 /*return*/, this.raw.channelsControllerRemoveReaction(channelId, messageId, emoji, options)];
@@ -352,35 +550,77 @@ var ScrymeSDK = /** @class */ (function () {
352
550
  configurable: true
353
551
  });
354
552
  Object.defineProperty(ScrymeSDK.prototype, "dm", {
553
+ /**
554
+ * Operations for managing direct messages (DMs) and direct message conversations.
555
+ */
355
556
  get: function () {
356
557
  var _this = this;
357
558
  return {
559
+ /**
560
+ * Lists all active direct message conversations for the authenticated user.
561
+ * @param options Optional request config override.
562
+ * @returns List of active DM conversations returned exactly from the endpoint.
563
+ */
358
564
  list: function (options) { return __awaiter(_this, void 0, void 0, function () {
359
565
  return __generator(this, function (_a) {
360
566
  return [2 /*return*/, this.raw.dmsControllerGetDms(options)];
361
567
  });
362
568
  }); },
569
+ /**
570
+ * Creates/initiates a direct message conversation with specified users.
571
+ * @param data Create direct message details containing target participant IDs.
572
+ * @param options Optional request config override.
573
+ * @returns Details of the created DM conversation exactly from the endpoint.
574
+ */
363
575
  create: function (data, options) { return __awaiter(_this, void 0, void 0, function () {
364
576
  return __generator(this, function (_a) {
365
577
  return [2 /*return*/, this.raw.dmsControllerCreateDm(data, options)];
366
578
  });
367
579
  }); },
580
+ /**
581
+ * Retrieves details of a specific direct message conversation.
582
+ * @param dmId Unique identifier of the direct message conversation.
583
+ * @param options Optional request config override.
584
+ * @returns Detailed direct message conversation object exactly from the endpoint.
585
+ */
368
586
  get: function (dmId, options) { return __awaiter(_this, void 0, void 0, function () {
369
587
  return __generator(this, function (_a) {
370
588
  return [2 /*return*/, this.raw.dmsControllerGetDm(dmId, options)];
371
589
  });
372
590
  }); },
591
+ /**
592
+ * Deletes/closes an active direct message conversation.
593
+ * @param dmId Unique identifier of the direct message conversation to close.
594
+ * @param options Optional request config override.
595
+ * @returns Success status indicating that the DM conversation was deleted exactly from the endpoint.
596
+ */
373
597
  delete: function (dmId, options) { return __awaiter(_this, void 0, void 0, function () {
374
598
  return __generator(this, function (_a) {
375
599
  return [2 /*return*/, this.raw.dmsControllerDeleteDm(dmId, options)];
376
600
  });
377
601
  }); },
602
+ /**
603
+ * Sub-namespace for managing direct messages in a specific DM conversation.
604
+ */
378
605
  message: {
606
+ /**
607
+ * Lists messages in a direct message conversation with cursor pagination.
608
+ * @param dmId Unique identifier of the direct message conversation.
609
+ * @param params Query parameters for pagination limits, cursors or search filters.
610
+ * @param options Optional request config override.
611
+ * @returns List of direct messages and next pagination cursor exactly from the endpoint.
612
+ */
379
613
  list: function (dmId, params, options) { return __awaiter(_this, void 0, void 0, function () {
380
614
  return __generator(this, function (_a) {
381
615
  return [2 /*return*/, this.raw.dmsControllerGetMessages(dmId, params, options)];
382
616
  });
383
617
  }); },
618
+ /**
619
+ * Sends a new message in a direct message conversation.
620
+ * @param dmId Unique identifier of the direct message conversation.
621
+ * @param options Optional request config override. Note that content/attachments can be passed inside options.data.
622
+ * @returns The sent message exactly from the endpoint.
623
+ */
384
624
  create: function (dmId, options) { return __awaiter(_this, void 0, void 0, function () {
385
625
  return __generator(this, function (_a) {
386
626
  return [2 /*return*/, this.raw.dmsControllerCreateMessage(dmId, options)];
@@ -393,19 +633,40 @@ var ScrymeSDK = /** @class */ (function () {
393
633
  configurable: true
394
634
  });
395
635
  Object.defineProperty(ScrymeSDK.prototype, "user", {
636
+ /**
637
+ * Operations for retrieving information about the current user or other user profiles,
638
+ * as well as performing user searches.
639
+ */
396
640
  get: function () {
397
641
  var _this = this;
398
642
  return {
643
+ /**
644
+ * Retrieves the profile details of the currently authenticated user.
645
+ * @param options Optional request config override.
646
+ * @returns The active user's profile returned exactly from the endpoint.
647
+ */
399
648
  me: function (options) { return __awaiter(_this, void 0, void 0, function () {
400
649
  return __generator(this, function (_a) {
401
650
  return [2 /*return*/, this.raw.usersControllerGetMe(options)];
402
651
  });
403
652
  }); },
653
+ /**
654
+ * Retrieves the public profile of a user by their user ID.
655
+ * @param userId Unique identifier of the target user.
656
+ * @param options Optional request config override.
657
+ * @returns Public user profile returned exactly from the endpoint.
658
+ */
404
659
  get: function (userId, options) { return __awaiter(_this, void 0, void 0, function () {
405
660
  return __generator(this, function (_a) {
406
661
  return [2 /*return*/, this.raw.usersControllerGetUser(userId, options)];
407
662
  });
408
663
  }); },
664
+ /**
665
+ * Searches the organization or workspace directory for user profiles matching specific queries.
666
+ * @param params Object containing search filters and query string parameters.
667
+ * @param options Optional request config override.
668
+ * @returns List of matching user profiles returned exactly from the endpoint.
669
+ */
409
670
  search: function (params, options) { return __awaiter(_this, void 0, void 0, function () {
410
671
  return __generator(this, function (_a) {
411
672
  return [2 /*return*/, this.raw.usersControllerSearchUsers(params, options)];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scryme/chat",
3
- "version": "0.0.1",
3
+ "version": "2.13.4",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -37,6 +37,14 @@ const getBaseURL = () => {
37
37
  return url.replace(/\/$/, '') + '/api';
38
38
  };
39
39
 
40
+ let globalToken: string | null = null;
41
+
42
+ export const setGlobalToken = (token: string | null) => {
43
+ globalToken = token;
44
+ };
45
+
46
+ export const getGlobalToken = () => globalToken;
47
+
40
48
  export const AXIOS_INSTANCE = axios.create({
41
49
  baseURL: getBaseURL(),
42
50
  timeout: 10000,
@@ -44,6 +52,23 @@ export const AXIOS_INSTANCE = axios.create({
44
52
  });
45
53
 
46
54
  AXIOS_INSTANCE.interceptors.request.use(config => {
55
+ if (!config.headers) {
56
+ config.headers = {} as any;
57
+ }
58
+
59
+ // Check if an Authorization header is already present (case-insensitive)
60
+ const hasAuth =
61
+ config.headers.Authorization ||
62
+ config.headers.authorization ||
63
+ (config.headers as any)['Authorization'] ||
64
+ (config.headers as any)['authorization'];
65
+
66
+ // 1. Apply global token if available and no Auth header is set yet
67
+ if (globalToken && !hasAuth) {
68
+ config.headers.Authorization = `Bearer ${globalToken}`;
69
+ }
70
+
71
+ // 2. Check for browser-based session/bearer tokens
47
72
  if (typeof window !== 'undefined') {
48
73
  const getCookie = (name: string) => {
49
74
  const value = `; ${document.cookie}`;
@@ -80,7 +105,14 @@ AXIOS_INSTANCE.interceptors.request.use(config => {
80
105
  }
81
106
  }
82
107
 
83
- if (token) {
108
+ // Re-check for Authorization header before applying browser token
109
+ const hasAuthNow =
110
+ config.headers.Authorization ||
111
+ config.headers.authorization ||
112
+ (config.headers as any)['Authorization'] ||
113
+ (config.headers as any)['authorization'];
114
+
115
+ if (token && !hasAuthNow) {
84
116
  config.headers.Authorization = `Bearer ${token}`;
85
117
  }
86
118
  }