stream-chat 4.4.3-dev.2 → 5.0.0

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 (53) hide show
  1. package/README.md +4 -13
  2. package/dist/browser.es.js +1229 -720
  3. package/dist/browser.es.js.map +1 -1
  4. package/dist/browser.full-bundle.min.js +1 -1
  5. package/dist/browser.full-bundle.min.js.map +1 -1
  6. package/dist/browser.js +1229 -719
  7. package/dist/browser.js.map +1 -1
  8. package/dist/index.es.js +1229 -720
  9. package/dist/index.es.js.map +1 -1
  10. package/dist/index.js +1229 -719
  11. package/dist/index.js.map +1 -1
  12. package/dist/types/base64.d.ts.map +1 -1
  13. package/dist/types/channel.d.ts +19 -15
  14. package/dist/types/channel.d.ts.map +1 -1
  15. package/dist/types/channel_state.d.ts +2 -2
  16. package/dist/types/channel_state.d.ts.map +1 -1
  17. package/dist/types/client.d.ts +25 -42
  18. package/dist/types/client.d.ts.map +1 -1
  19. package/dist/types/client_state.d.ts +2 -2
  20. package/dist/types/client_state.d.ts.map +1 -1
  21. package/dist/types/connection.d.ts +14 -49
  22. package/dist/types/connection.d.ts.map +1 -1
  23. package/dist/types/connection_fallback.d.ts +41 -0
  24. package/dist/types/connection_fallback.d.ts.map +1 -0
  25. package/dist/types/errors.d.ts +14 -0
  26. package/dist/types/errors.d.ts.map +1 -0
  27. package/dist/types/insights.d.ts +17 -10
  28. package/dist/types/insights.d.ts.map +1 -1
  29. package/dist/types/permissions.d.ts.map +1 -1
  30. package/dist/types/signing.d.ts +3 -3
  31. package/dist/types/signing.d.ts.map +1 -1
  32. package/dist/types/token_manager.d.ts +2 -2
  33. package/dist/types/token_manager.d.ts.map +1 -1
  34. package/dist/types/types.d.ts +94 -88
  35. package/dist/types/types.d.ts.map +1 -1
  36. package/dist/types/utils.d.ts +13 -3
  37. package/dist/types/utils.d.ts.map +1 -1
  38. package/package.json +4 -4
  39. package/src/base64.ts +1 -4
  40. package/src/channel.ts +133 -461
  41. package/src/channel_state.ts +31 -158
  42. package/src/client.ts +291 -712
  43. package/src/client_state.ts +2 -2
  44. package/src/connection.ts +146 -395
  45. package/src/connection_fallback.ts +205 -0
  46. package/src/errors.ts +58 -0
  47. package/src/insights.ts +38 -32
  48. package/src/permissions.ts +3 -24
  49. package/src/signing.ts +6 -17
  50. package/src/token_manager.ts +6 -18
  51. package/src/types.ts +268 -512
  52. package/src/utils.ts +58 -24
  53. package/CHANGELOG.md +0 -844
package/src/client.ts CHANGED
@@ -11,6 +11,8 @@ import { StableWSConnection } from './connection';
11
11
  import { isValidEventType } from './events';
12
12
  import { JWTUserToken, DevToken, CheckSignature } from './signing';
13
13
  import { TokenManager } from './token_manager';
14
+ import { WSConnectionFallback } from './connection_fallback';
15
+ import { isWSFailure } from './errors';
14
16
  import {
15
17
  isFunction,
16
18
  isOwnUserBaseProperty,
@@ -20,6 +22,7 @@ import {
20
22
  randomId,
21
23
  sleep,
22
24
  retryInterval,
25
+ isOnline,
23
26
  } from './utils';
24
27
 
25
28
  import {
@@ -45,7 +48,6 @@ import {
45
48
  CheckSQSResponse,
46
49
  Configs,
47
50
  ConnectAPIResponse,
48
- ConnectionChangeEvent,
49
51
  CreateChannelOptions,
50
52
  CreateChannelResponse,
51
53
  CreateCommandOptions,
@@ -95,7 +97,7 @@ import {
95
97
  TestSQSDataInput,
96
98
  TokenOrProvider,
97
99
  UnBanUserOptions,
98
- UnknownType,
100
+ UR,
99
101
  UpdateChannelOptions,
100
102
  UpdateChannelResponse,
101
103
  UpdateCommandOptions,
@@ -117,35 +119,28 @@ import {
117
119
  DeleteUserOptions,
118
120
  DeleteChannelsResponse,
119
121
  TaskResponse,
122
+ ReservedMessageFields,
120
123
  } from './types';
121
- import { InsightTypes, InsightMetrics } from './insights';
124
+ import { InsightMetrics, postInsights } from './insights';
122
125
 
123
126
  function isString(x: unknown): x is string {
124
127
  return typeof x === 'string' || x instanceof String;
125
128
  }
126
129
 
127
130
  export class StreamChat<
128
- AttachmentType extends UnknownType = UnknownType,
129
- ChannelType extends UnknownType = UnknownType,
131
+ AttachmentType extends UR = UR,
132
+ ChannelType extends UR = UR,
130
133
  CommandType extends string = LiteralStringForUnion,
131
- EventType extends UnknownType = UnknownType,
132
- MessageType extends UnknownType = UnknownType,
133
- ReactionType extends UnknownType = UnknownType,
134
- UserType extends UnknownType = UnknownType
134
+ EventType extends UR = UR,
135
+ MessageType extends UR = UR,
136
+ ReactionType extends UR = UR,
137
+ UserType extends UR = UR
135
138
  > {
136
139
  private static _instance?: unknown | StreamChat; // type is undefined|StreamChat, unknown is due to TS limitations with statics
137
140
 
138
141
  _user?: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>;
139
142
  activeChannels: {
140
- [key: string]: Channel<
141
- AttachmentType,
142
- ChannelType,
143
- CommandType,
144
- EventType,
145
- MessageType,
146
- ReactionType,
147
- UserType
148
- >;
143
+ [key: string]: Channel<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>;
149
144
  };
150
145
  anonymous: boolean;
151
146
  axiosInstance: AxiosInstance;
@@ -154,22 +149,10 @@ export class StreamChat<
154
149
  cleaningIntervalRef?: NodeJS.Timeout;
155
150
  clientID?: string;
156
151
  configs: Configs<CommandType>;
157
- connectionID?: string;
158
- failures?: number;
159
152
  key: string;
160
153
  listeners: {
161
154
  [key: string]: Array<
162
- (
163
- event: Event<
164
- AttachmentType,
165
- ChannelType,
166
- CommandType,
167
- EventType,
168
- MessageType,
169
- ReactionType,
170
- UserType
171
- >,
172
- ) => void
155
+ (event: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>) => void
173
156
  >;
174
157
  };
175
158
  logger: Logger;
@@ -195,10 +178,29 @@ export class StreamChat<
195
178
  userAgent?: string;
196
179
  userID?: string;
197
180
  wsBaseURL?: string;
198
- wsConnection: StableWSConnection<ChannelType, CommandType, UserType> | null;
181
+ wsConnection: StableWSConnection<
182
+ ChannelType,
183
+ CommandType,
184
+ UserType,
185
+ AttachmentType,
186
+ EventType,
187
+ MessageType,
188
+ ReactionType
189
+ > | null;
190
+ wsFallback?: WSConnectionFallback<
191
+ AttachmentType,
192
+ ChannelType,
193
+ CommandType,
194
+ EventType,
195
+ MessageType,
196
+ ReactionType,
197
+ UserType
198
+ >;
199
199
  wsPromise: ConnectAPIResponse<ChannelType, CommandType, UserType> | null;
200
200
  consecutiveFailures: number;
201
201
  insightMetrics: InsightMetrics;
202
+ defaultWSTimeoutWithFallback: number;
203
+ defaultWSTimeout: number;
202
204
 
203
205
  /**
204
206
  * Initialize a client
@@ -221,11 +223,7 @@ export class StreamChat<
221
223
  */
222
224
  constructor(key: string, options?: StreamChatOptions);
223
225
  constructor(key: string, secret?: string, options?: StreamChatOptions);
224
- constructor(
225
- key: string,
226
- secretOrOptions?: StreamChatOptions | string,
227
- options?: StreamChatOptions,
228
- ) {
226
+ constructor(key: string, secretOrOptions?: StreamChatOptions | string, options?: StreamChatOptions) {
229
227
  // set the key
230
228
  this.key = key;
231
229
  this.listeners = {};
@@ -240,16 +238,9 @@ export class StreamChat<
240
238
  }
241
239
 
242
240
  // set the options... and figure out defaults...
243
- const inputOptions = options
244
- ? options
245
- : secretOrOptions && !isString(secretOrOptions)
246
- ? secretOrOptions
247
- : {};
248
-
249
- this.browser =
250
- typeof inputOptions.browser !== 'undefined'
251
- ? inputOptions.browser
252
- : typeof window !== 'undefined';
241
+ const inputOptions = options ? options : secretOrOptions && !isString(secretOrOptions) ? secretOrOptions : {};
242
+
243
+ this.browser = typeof inputOptions.browser !== 'undefined' ? inputOptions.browser : typeof window !== 'undefined';
253
244
  this.node = !this.browser;
254
245
 
255
246
  this.options = {
@@ -295,6 +286,9 @@ export class StreamChat<
295
286
  this.consecutiveFailures = 0;
296
287
  this.insightMetrics = new InsightMetrics();
297
288
 
289
+ this.defaultWSTimeoutWithFallback = 6000;
290
+ this.defaultWSTimeout = 15000;
291
+
298
292
  /**
299
293
  * logger function should accept 3 parameters:
300
294
  * @param logLevel string
@@ -370,67 +364,43 @@ export class StreamChat<
370
364
  * StreamChat.getInstance('api_key', "secret", { httpsAgent: customAgent })
371
365
  */
372
366
  public static getInstance<
373
- AttachmentType extends UnknownType = UnknownType,
374
- ChannelType extends UnknownType = UnknownType,
367
+ AttachmentType extends UR = UR,
368
+ ChannelType extends UR = UR,
375
369
  CommandType extends string = LiteralStringForUnion,
376
- EventType extends UnknownType = UnknownType,
377
- MessageType extends UnknownType = UnknownType,
378
- ReactionType extends UnknownType = UnknownType,
379
- UserType extends UnknownType = UnknownType
370
+ EventType extends UR = UR,
371
+ MessageType extends UR = UR,
372
+ ReactionType extends UR = UR,
373
+ UserType extends UR = UR
380
374
  >(
381
375
  key: string,
382
376
  options?: StreamChatOptions,
383
- ): StreamChat<
384
- AttachmentType,
385
- ChannelType,
386
- CommandType,
387
- EventType,
388
- MessageType,
389
- ReactionType,
390
- UserType
391
- >;
377
+ ): StreamChat<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>;
392
378
  public static getInstance<
393
- AttachmentType extends UnknownType = UnknownType,
394
- ChannelType extends UnknownType = UnknownType,
379
+ AttachmentType extends UR = UR,
380
+ ChannelType extends UR = UR,
395
381
  CommandType extends string = LiteralStringForUnion,
396
- EventType extends UnknownType = UnknownType,
397
- MessageType extends UnknownType = UnknownType,
398
- ReactionType extends UnknownType = UnknownType,
399
- UserType extends UnknownType = UnknownType
382
+ EventType extends UR = UR,
383
+ MessageType extends UR = UR,
384
+ ReactionType extends UR = UR,
385
+ UserType extends UR = UR
400
386
  >(
401
387
  key: string,
402
388
  secret?: string,
403
389
  options?: StreamChatOptions,
404
- ): StreamChat<
405
- AttachmentType,
406
- ChannelType,
407
- CommandType,
408
- EventType,
409
- MessageType,
410
- ReactionType,
411
- UserType
412
- >;
390
+ ): StreamChat<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>;
413
391
  public static getInstance<
414
- AttachmentType extends UnknownType = UnknownType,
415
- ChannelType extends UnknownType = UnknownType,
392
+ AttachmentType extends UR = UR,
393
+ ChannelType extends UR = UR,
416
394
  CommandType extends string = LiteralStringForUnion,
417
- EventType extends UnknownType = UnknownType,
418
- MessageType extends UnknownType = UnknownType,
419
- ReactionType extends UnknownType = UnknownType,
420
- UserType extends UnknownType = UnknownType
395
+ EventType extends UR = UR,
396
+ MessageType extends UR = UR,
397
+ ReactionType extends UR = UR,
398
+ UserType extends UR = UR
421
399
  >(
422
400
  key: string,
423
401
  secretOrOptions?: StreamChatOptions | string,
424
402
  options?: StreamChatOptions,
425
- ): StreamChat<
426
- AttachmentType,
427
- ChannelType,
428
- CommandType,
429
- EventType,
430
- MessageType,
431
- ReactionType,
432
- UserType
433
- > {
403
+ ): StreamChat<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType> {
434
404
  if (!StreamChat._instance) {
435
405
  if (typeof secretOrOptions === 'string') {
436
406
  StreamChat._instance = new StreamChat<
@@ -479,7 +449,9 @@ export class StreamChat<
479
449
  this.wsBaseURL = this.baseURL.replace('http', 'ws').replace(':3030', ':8800');
480
450
  }
481
451
 
482
- _hasConnectionID = () => Boolean(this.wsConnection?.connectionID);
452
+ _getConnectionID = () => this.wsConnection?.connectionID || this.wsFallback?.connectionID;
453
+
454
+ _hasConnectionID = () => Boolean(this._getConnectionID());
483
455
 
484
456
  /**
485
457
  * connectUser - Set the current user and open a WebSocket connection
@@ -514,10 +486,7 @@ export class StreamChat<
514
486
  );
515
487
  }
516
488
 
517
- if (
518
- (this._isUsingServerAuth() || this.node) &&
519
- !this.options.allowServerSideConnect
520
- ) {
489
+ if ((this._isUsingServerAuth() || this.node) && !this.options.allowServerSideConnect) {
521
490
  console.warn(
522
491
  'Please do not use connectUser server side. connectUser impacts MAU and concurrent connection usage and thus your bill. If you have a valid use-case, add "allowServerSideConnect: true" to the client options to disable this warning.',
523
492
  );
@@ -560,9 +529,7 @@ export class StreamChat<
560
529
  _setToken = (user: UserResponse<UserType>, userTokenOrProvider: TokenOrProvider) =>
561
530
  this.tokenManager.setTokenOrProvider(userTokenOrProvider, user);
562
531
 
563
- _setUser(
564
- user: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>,
565
- ) {
532
+ _setUser(user: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>) {
566
533
  /**
567
534
  * This one is used by the frontend. This is a copy of the current user object stored on backend.
568
535
  * It contains reserved properties and own user properties which are not present in `this._user`.
@@ -585,17 +552,14 @@ export class StreamChat<
585
552
  * @param timeout Max number of ms, to wait for close event of websocket, before forcefully assuming succesful disconnection.
586
553
  * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
587
554
  */
588
- closeConnection = (timeout?: number) => {
555
+ closeConnection = async (timeout?: number) => {
589
556
  if (this.cleaningIntervalRef != null) {
590
557
  clearInterval(this.cleaningIntervalRef);
591
558
  this.cleaningIntervalRef = undefined;
592
559
  }
593
560
 
594
- if (!this.wsConnection) {
595
- return Promise.resolve();
596
- }
597
-
598
- return this.wsConnection.disconnect(timeout);
561
+ await Promise.all([this.wsConnection?.disconnect(timeout), this.wsFallback?.disconnect(timeout)]);
562
+ return Promise.resolve();
599
563
  };
600
564
 
601
565
  /**
@@ -603,19 +567,13 @@ export class StreamChat<
603
567
  */
604
568
  openConnection = async () => {
605
569
  if (!this.userID) {
606
- throw Error(
607
- 'User is not set on client, use client.connectUser or client.connectAnonymousUser instead',
608
- );
570
+ throw Error('User is not set on client, use client.connectUser or client.connectAnonymousUser instead');
609
571
  }
610
572
 
611
- if (this.wsConnection?.isHealthy && this._hasConnectionID()) {
612
- this.logger(
613
- 'info',
614
- 'client:openConnection() - openConnection called twice, healthy connection already exists',
615
- {
616
- tags: ['connection', 'client'],
617
- },
618
- );
573
+ if ((this.wsConnection?.isHealthy || this.wsFallback?.isHealthy()) && this._hasConnectionID()) {
574
+ this.logger('info', 'client:openConnection() - openConnection called twice, healthy connection already exists', {
575
+ tags: ['connection', 'client'],
576
+ });
619
577
 
620
578
  return Promise.resolve();
621
579
  }
@@ -661,9 +619,7 @@ export class StreamChat<
661
619
  */
662
620
  async updateAppSettings(options: AppSettings) {
663
621
  if (options.apn_config?.p12_cert) {
664
- options.apn_config.p12_cert = Buffer.from(options.apn_config.p12_cert).toString(
665
- 'base64',
666
- );
622
+ options.apn_config.p12_cert = Buffer.from(options.apn_config.p12_cert).toString('base64');
667
623
  }
668
624
  return await this.patch<APIResponse>(this.baseURL + '/app', options);
669
625
  }
@@ -674,9 +630,7 @@ export class StreamChat<
674
630
  }
675
631
 
676
632
  if (before === '') {
677
- throw new Error(
678
- "Don't pass blank string for since, use null instead if resetting the token revoke",
679
- );
633
+ throw new Error("Don't pass blank string for since, use null instead if resetting the token revoke");
680
634
  }
681
635
 
682
636
  return before;
@@ -748,12 +702,8 @@ export class StreamChat<
748
702
  ...(data.messageID ? { message_id: data.messageID } : {}),
749
703
  ...(data.apnTemplate ? { apn_template: data.apnTemplate } : {}),
750
704
  ...(data.firebaseTemplate ? { firebase_template: data.firebaseTemplate } : {}),
751
- ...(data.firebaseDataTemplate
752
- ? { firebase_data_template: data.firebaseDataTemplate }
753
- : {}),
754
- ...(data.huaweiDataTemplate
755
- ? { huawei_data_template: data.huaweiDataTemplate }
756
- : {}),
705
+ ...(data.firebaseDataTemplate ? { firebase_data_template: data.firebaseDataTemplate } : {}),
706
+ ...(data.huaweiDataTemplate ? { huawei_data_template: data.huaweiDataTemplate } : {}),
757
707
  ...(data.skipDevices ? { skip_devices: true } : {}),
758
708
  });
759
709
  }
@@ -801,7 +751,7 @@ export class StreamChat<
801
751
  // reset client state
802
752
  this.state = new ClientState();
803
753
  // reset token manager
804
- this.tokenManager.reset();
754
+ setTimeout(this.tokenManager.reset); // delay reseting to use token for disconnect calls
805
755
 
806
756
  // close the WS connection
807
757
  return closePromise;
@@ -819,10 +769,7 @@ export class StreamChat<
819
769
  * connectAnonymousUser - Set an anonymous user and open a WebSocket connection
820
770
  */
821
771
  connectAnonymousUser = () => {
822
- if (
823
- (this._isUsingServerAuth() || this.node) &&
824
- !this.options.allowServerSideConnect
825
- ) {
772
+ if ((this._isUsingServerAuth() || this.node) && !this.options.allowServerSideConnect) {
826
773
  console.warn(
827
774
  'Please do not use connectUser server side. connectUser impacts MAU and concurrent connection usage and thus your bill. If you have a valid use-case, add "allowServerSideConnect: true" to the client options to disable this warning.',
828
775
  );
@@ -857,9 +804,10 @@ export class StreamChat<
857
804
  let response: { access_token: string; user: UserResponse<UserType> } | undefined;
858
805
  this.anonymous = true;
859
806
  try {
860
- response = await this.post<
861
- APIResponse & { access_token: string; user: UserResponse<UserType> }
862
- >(this.baseURL + '/guest', { user });
807
+ response = await this.post<APIResponse & { access_token: string; user: UserResponse<UserType> }>(
808
+ this.baseURL + '/guest',
809
+ { user },
810
+ );
863
811
  } catch (e) {
864
812
  this.anonymous = false;
865
813
  throw e;
@@ -867,10 +815,7 @@ export class StreamChat<
867
815
  this.anonymous = false;
868
816
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
869
817
  const { created_at, updated_at, last_active, online, ...guestUser } = response.user;
870
- return await this.connectUser(
871
- guestUser as UserResponse<UserType>,
872
- response.access_token,
873
- );
818
+ return await this.connectUser(guestUser as UserResponse<UserType>, response.access_token);
874
819
  }
875
820
 
876
821
  /**
@@ -912,39 +857,15 @@ export class StreamChat<
912
857
  * @return {{ unsubscribe: () => void }} Description
913
858
  */
914
859
  on(
915
- callback: EventHandler<
916
- AttachmentType,
917
- ChannelType,
918
- CommandType,
919
- EventType,
920
- MessageType,
921
- ReactionType,
922
- UserType
923
- >,
860
+ callback: EventHandler<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
924
861
  ): { unsubscribe: () => void };
925
862
  on(
926
863
  eventType: string,
927
- callback: EventHandler<
928
- AttachmentType,
929
- ChannelType,
930
- CommandType,
931
- EventType,
932
- MessageType,
933
- ReactionType,
934
- UserType
935
- >,
864
+ callback: EventHandler<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
936
865
  ): { unsubscribe: () => void };
937
866
  on(
938
867
  callbackOrString:
939
- | EventHandler<
940
- AttachmentType,
941
- ChannelType,
942
- CommandType,
943
- EventType,
944
- MessageType,
945
- ReactionType,
946
- UserType
947
- >
868
+ | EventHandler<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>
948
869
  | string,
949
870
  callbackOrNothing?: EventHandler<
950
871
  AttachmentType,
@@ -995,39 +916,15 @@ export class StreamChat<
995
916
  *
996
917
  */
997
918
  off(
998
- callback: EventHandler<
999
- AttachmentType,
1000
- ChannelType,
1001
- CommandType,
1002
- EventType,
1003
- MessageType,
1004
- ReactionType,
1005
- UserType
1006
- >,
919
+ callback: EventHandler<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
1007
920
  ): void;
1008
921
  off(
1009
922
  eventType: string,
1010
- callback: EventHandler<
1011
- AttachmentType,
1012
- ChannelType,
1013
- CommandType,
1014
- EventType,
1015
- MessageType,
1016
- ReactionType,
1017
- UserType
1018
- >,
923
+ callback: EventHandler<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
1019
924
  ): void;
1020
925
  off(
1021
926
  callbackOrString:
1022
- | EventHandler<
1023
- AttachmentType,
1024
- ChannelType,
1025
- CommandType,
1026
- EventType,
1027
- MessageType,
1028
- ReactionType,
1029
- UserType
1030
- >
927
+ | EventHandler<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>
1031
928
  | string,
1032
929
  callbackOrNothing?: EventHandler<
1033
930
  AttachmentType,
@@ -1082,15 +979,11 @@ export class StreamChat<
1082
979
  }
1083
980
 
1084
981
  _logApiResponse<T>(type: string, url: string, response: AxiosResponse<T>) {
1085
- this.logger(
1086
- 'info',
1087
- `client:${type} - Response - url: ${url} > status ${response.status}`,
1088
- {
1089
- tags: ['api', 'api_response', 'client'],
1090
- url,
1091
- response,
1092
- },
1093
- );
982
+ this.logger('info', `client:${type} - Response - url: ${url} > status ${response.status}`, {
983
+ tags: ['api', 'api_response', 'client'],
984
+ url,
985
+ response,
986
+ });
1094
987
  }
1095
988
 
1096
989
  _logApiError(type: string, url: string, error: unknown) {
@@ -1139,15 +1032,14 @@ export class StreamChat<
1139
1032
  this._logApiResponse<T>(type, url, response);
1140
1033
  this.consecutiveFailures = 0;
1141
1034
  return this.handleResponse(response);
1142
- } catch (e) {
1035
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1036
+ } catch (e: any /**TODO: generalize error types */) {
1143
1037
  e.client_request_id = requestConfig.headers?.['x-client-request-id'];
1144
1038
  this._logApiError(type, url, e);
1145
1039
  this.consecutiveFailures += 1;
1146
1040
  if (e.response) {
1147
- if (
1148
- e.response.data.code === chatCodes.TOKEN_EXPIRED &&
1149
- !this.tokenManager.isStatic()
1150
- ) {
1041
+ /** connection_fallback depends on this token expiration logic */
1042
+ if (e.response.data.code === chatCodes.TOKEN_EXPIRED && !this.tokenManager.isStatic()) {
1151
1043
  if (this.consecutiveFailures > 1) {
1152
1044
  await sleep(retryInterval(this.consecutiveFailures));
1153
1045
  }
@@ -1205,9 +1097,7 @@ export class StreamChat<
1205
1097
  let err: Error & { code?: number; response?: AxiosResponse<T>; status?: number };
1206
1098
  err = new Error(`StreamChat error HTTP code: ${response.status}`);
1207
1099
  if (response.data && response.data.code) {
1208
- err = new Error(
1209
- `StreamChat error code ${response.data.code}: ${response.data.message}`,
1210
- );
1100
+ err = new Error(`StreamChat error code ${response.data.code}: ${response.data.message}`);
1211
1101
  err.code = response.data.code;
1212
1102
  }
1213
1103
  err.response = response;
@@ -1224,16 +1114,10 @@ export class StreamChat<
1224
1114
  }
1225
1115
 
1226
1116
  dispatchEvent = (
1227
- event: Event<
1228
- AttachmentType,
1229
- ChannelType,
1230
- CommandType,
1231
- EventType,
1232
- MessageType,
1233
- ReactionType,
1234
- UserType
1235
- >,
1117
+ event: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
1236
1118
  ) => {
1119
+ if (!event.received_at) event.received_at = new Date();
1120
+
1237
1121
  // client event handlers
1238
1122
  const postListenerCallbacks = this._handleClientEvent(event);
1239
1123
 
@@ -1265,7 +1149,6 @@ export class StreamChat<
1265
1149
  ReactionType,
1266
1150
  UserType
1267
1151
  >;
1268
- event.received_at = new Date();
1269
1152
  this.dispatchEvent(event);
1270
1153
  };
1271
1154
 
@@ -1350,15 +1233,7 @@ export class StreamChat<
1350
1233
  * @param {Event} event
1351
1234
  */
1352
1235
  _handleUserEvent = (
1353
- event: Event<
1354
- AttachmentType,
1355
- ChannelType,
1356
- CommandType,
1357
- EventType,
1358
- MessageType,
1359
- ReactionType,
1360
- UserType
1361
- >,
1236
+ event: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
1362
1237
  ) => {
1363
1238
  if (!event.user) {
1364
1239
  return;
@@ -1400,42 +1275,22 @@ export class StreamChat<
1400
1275
  this._updateUserMessageReferences(event.user);
1401
1276
  }
1402
1277
 
1403
- if (
1404
- event.type === 'user.deleted' &&
1405
- event.user.deleted_at &&
1406
- (event.mark_messages_deleted || event.hard_delete)
1407
- ) {
1278
+ if (event.type === 'user.deleted' && event.user.deleted_at && (event.mark_messages_deleted || event.hard_delete)) {
1408
1279
  this._deleteUserMessageReference(event.user, event.hard_delete);
1409
1280
  }
1410
1281
  };
1411
1282
 
1412
1283
  _handleClientEvent(
1413
- event: Event<
1414
- AttachmentType,
1415
- ChannelType,
1416
- CommandType,
1417
- EventType,
1418
- MessageType,
1419
- ReactionType,
1420
- UserType
1421
- >,
1284
+ event: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
1422
1285
  ) {
1423
1286
  const client = this;
1424
1287
  const postListenerCallbacks = [];
1425
- this.logger(
1426
- 'info',
1427
- `client:_handleClientEvent - Received event of type { ${event.type} }`,
1428
- {
1429
- tags: ['event', 'client'],
1430
- event,
1431
- },
1432
- );
1288
+ this.logger('info', `client:_handleClientEvent - Received event of type { ${event.type} }`, {
1289
+ tags: ['event', 'client'],
1290
+ event,
1291
+ });
1433
1292
 
1434
- if (
1435
- event.type === 'user.presence.changed' ||
1436
- event.type === 'user.updated' ||
1437
- event.type === 'user.deleted'
1438
- ) {
1293
+ if (event.type === 'user.presence.changed' || event.type === 'user.updated' || event.type === 'user.deleted') {
1439
1294
  this._handleUserEvent(event);
1440
1295
  }
1441
1296
 
@@ -1454,12 +1309,8 @@ export class StreamChat<
1454
1309
  const currentMutedChannelIds: string[] = [];
1455
1310
  const nextMutedChannelIds: string[] = [];
1456
1311
 
1457
- this.mutedChannels.forEach(
1458
- (mute) => mute.channel && currentMutedChannelIds.push(mute.channel.cid),
1459
- );
1460
- event.me.channel_mutes.forEach(
1461
- (mute) => mute.channel && nextMutedChannelIds.push(mute.channel.cid),
1462
- );
1312
+ this.mutedChannels.forEach((mute) => mute.channel && currentMutedChannelIds.push(mute.channel.cid));
1313
+ event.me.channel_mutes.forEach((mute) => mute.channel && nextMutedChannelIds.push(mute.channel.cid));
1463
1314
 
1464
1315
  /** Set the unread count of un-muted channels to 0, which is the behaviour of backend */
1465
1316
  currentMutedChannelIds.forEach((cid) => {
@@ -1475,11 +1326,7 @@ export class StreamChat<
1475
1326
  this.mutedUsers = event.me.mutes;
1476
1327
  }
1477
1328
 
1478
- if (
1479
- (event.type === 'channel.deleted' ||
1480
- event.type === 'notification.channel_deleted') &&
1481
- event.cid
1482
- ) {
1329
+ if ((event.type === 'channel.deleted' || event.type === 'notification.channel_deleted') && event.cid) {
1483
1330
  client.state.deleteAllChannelReference(event.cid);
1484
1331
  this.activeChannels[event.cid]?._disconnect();
1485
1332
 
@@ -1499,9 +1346,7 @@ export class StreamChat<
1499
1346
  const mute = this.mutedChannels[i];
1500
1347
  if (mute.channel?.cid === cid) {
1501
1348
  muteStatus = {
1502
- muted: mute.expires
1503
- ? new Date(mute.expires).getTime() > new Date().getTime()
1504
- : true,
1349
+ muted: mute.expires ? new Date(mute.expires).getTime() > new Date().getTime() : true,
1505
1350
  createdAt: mute.created_at ? new Date(mute.created_at) : new Date(),
1506
1351
  expiresAt: mute.expires ? new Date(mute.expires) : null,
1507
1352
  };
@@ -1521,30 +1366,12 @@ export class StreamChat<
1521
1366
  }
1522
1367
 
1523
1368
  _callClientListeners = (
1524
- event: Event<
1525
- AttachmentType,
1526
- ChannelType,
1527
- CommandType,
1528
- EventType,
1529
- MessageType,
1530
- ReactionType,
1531
- UserType
1532
- >,
1369
+ event: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>,
1533
1370
  ) => {
1534
1371
  const client = this;
1535
1372
  // gather and call the listeners
1536
1373
  const listeners: Array<
1537
- (
1538
- event: Event<
1539
- AttachmentType,
1540
- ChannelType,
1541
- CommandType,
1542
- EventType,
1543
- MessageType,
1544
- ReactionType,
1545
- UserType
1546
- >,
1547
- ) => void
1374
+ (event: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>) => void
1548
1375
  > = [];
1549
1376
  if (client.listeners.all) {
1550
1377
  listeners.push(...client.listeners.all);
@@ -1560,21 +1387,15 @@ export class StreamChat<
1560
1387
  };
1561
1388
 
1562
1389
  recoverState = async () => {
1563
- this.logger(
1564
- 'info',
1565
- `client:recoverState() - Start of recoverState with connectionID ${this.wsConnection?.connectionID}`,
1566
- {
1567
- tags: ['connection'],
1568
- },
1569
- );
1390
+ this.logger('info', `client:recoverState() - Start of recoverState with connectionID ${this._getConnectionID()}`, {
1391
+ tags: ['connection'],
1392
+ });
1570
1393
 
1571
1394
  const cids = Object.keys(this.activeChannels);
1572
1395
  if (cids.length && this.recoverStateOnReconnect) {
1573
- this.logger(
1574
- 'info',
1575
- `client:recoverState() - Start the querying of ${cids.length} channels`,
1576
- { tags: ['connection', 'client'] },
1577
- );
1396
+ this.logger('info', `client:recoverState() - Start the querying of ${cids.length} channels`, {
1397
+ tags: ['connection', 'client'],
1398
+ });
1578
1399
 
1579
1400
  await this.queryChannels(
1580
1401
  { cid: { $in: cids } } as ChannelFilters<ChannelType, CommandType, UserType>,
@@ -1603,57 +1424,76 @@ export class StreamChat<
1603
1424
  * @private
1604
1425
  */
1605
1426
  async connect() {
1606
- const client = this;
1607
- this.failures = 0;
1608
-
1609
- if (client.userID == null || this._user == null) {
1610
- throw Error(
1611
- 'Call connectUser or connectAnonymousUser before starting the connection',
1612
- );
1427
+ if (!this.userID || !this._user) {
1428
+ throw Error('Call connectUser or connectAnonymousUser before starting the connection');
1613
1429
  }
1614
-
1615
- if (client.wsBaseURL == null) {
1430
+ if (!this.wsBaseURL) {
1616
1431
  throw Error('Websocket base url not set');
1617
1432
  }
1618
-
1619
- if (client.clientID == null) {
1433
+ if (!this.clientID) {
1620
1434
  throw Error('clientID is not set');
1621
1435
  }
1622
1436
 
1623
1437
  if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
1624
- this.sendBeacon();
1438
+ this._sayHi();
1625
1439
  }
1626
1440
 
1627
1441
  // The StableWSConnection handles all the reconnection logic.
1628
- this.wsConnection = new StableWSConnection<ChannelType, CommandType, UserType>({
1629
- wsBaseURL: client.wsBaseURL,
1630
- clientID: client.clientID,
1631
- userID: client.userID,
1632
- tokenManager: client.tokenManager,
1633
- user: this._user,
1634
- authType: this.getAuthType(),
1635
- userAgent: this.getUserAgent(),
1636
- apiKey: this.key,
1637
- recoverCallback: this.recoverState,
1638
- messageCallback: this.handleEvent,
1639
- eventCallback: this.dispatchEvent as (event: ConnectionChangeEvent) => void,
1640
- logger: this.logger,
1641
- device: this.options.device,
1642
- postInsights: this.options.enableInsights ? this.postInsights : undefined,
1643
- insightMetrics: this.insightMetrics,
1644
- });
1645
- return await this.wsConnection.connect();
1442
+ this.wsConnection = new StableWSConnection<
1443
+ ChannelType,
1444
+ CommandType,
1445
+ UserType,
1446
+ AttachmentType,
1447
+ EventType,
1448
+ MessageType,
1449
+ ReactionType
1450
+ >({ client: this });
1451
+
1452
+ try {
1453
+ // if WSFallback is enabled, ws connect should timeout faster so fallback can try
1454
+ return await this.wsConnection.connect(
1455
+ this.options.enableWSFallback ? this.defaultWSTimeoutWithFallback : this.defaultWSTimeout,
1456
+ );
1457
+ } catch (err) {
1458
+ // run fallback only if it's WS/Network error and not a normal API error
1459
+ // make sure browser is online before even trying the longpoll
1460
+ if (this.options.enableWSFallback && isWSFailure(err) && isOnline()) {
1461
+ this.logger('info', 'client:connect() - WS failed, fallback to longpoll', { tags: ['connection', 'client'] });
1462
+
1463
+ this.wsConnection._destroyCurrentWSConnection();
1464
+ this.wsConnection.disconnect().then(); // close WS so no retry
1465
+ this.wsFallback = new WSConnectionFallback<
1466
+ AttachmentType,
1467
+ ChannelType,
1468
+ CommandType,
1469
+ EventType,
1470
+ MessageType,
1471
+ ReactionType,
1472
+ UserType
1473
+ >({ client: this });
1474
+ return await this.wsFallback.connect();
1475
+ }
1476
+
1477
+ throw err;
1478
+ }
1646
1479
  }
1647
1480
 
1648
- sendBeacon() {
1481
+ /**
1482
+ * Check the connectivity with server for warmup purpose.
1483
+ *
1484
+ * @private
1485
+ */
1486
+ _sayHi() {
1649
1487
  const client_request_id = randomId();
1650
1488
  const opts = { headers: { 'x-client-request-id': client_request_id } };
1651
- this.doAxiosRequest('get', this.baseURL + '/beacon', null, opts).catch((e) => {
1652
- this.postInsights('http_beacon_failed', {
1653
- api_key: this.key,
1654
- err: e,
1655
- client_request_id,
1656
- });
1489
+ this.doAxiosRequest('get', this.baseURL + '/hi', null, opts).catch((e) => {
1490
+ if (this.options.enableInsights) {
1491
+ postInsights('http_hi_failed', {
1492
+ api_key: this.key,
1493
+ err: e,
1494
+ client_request_id,
1495
+ });
1496
+ }
1657
1497
  });
1658
1498
  }
1659
1499
 
@@ -1667,11 +1507,7 @@ export class StreamChat<
1667
1507
  *
1668
1508
  * @return {Promise<APIResponse & { users: Array<UserResponse<UserType>> }>} User Query Response
1669
1509
  */
1670
- async queryUsers(
1671
- filterConditions: UserFilters<UserType>,
1672
- sort: UserSort<UserType> = [],
1673
- options: UserOptions = {},
1674
- ) {
1510
+ async queryUsers(filterConditions: UserFilters<UserType>, sort: UserSort<UserType> = [], options: UserOptions = {}) {
1675
1511
  const defaultOptions = {
1676
1512
  presence: false,
1677
1513
  };
@@ -1737,10 +1573,7 @@ export class StreamChat<
1737
1573
  *
1738
1574
  * @return {Promise<MessageFlagsResponse<ChannelType, CommandType, UserType>>} Message Flags Response
1739
1575
  */
1740
- async queryMessageFlags(
1741
- filterConditions: MessageFlagsFilters = {},
1742
- options: MessageFlagsPaginationOptions = {},
1743
- ) {
1576
+ async queryMessageFlags(filterConditions: MessageFlagsFilters = {}, options: MessageFlagsPaginationOptions = {}) {
1744
1577
  // Return a list of message flags
1745
1578
  return await this.get<MessageFlagsResponse<ChannelType, CommandType, UserType>>(
1746
1579
  this.baseURL + '/moderation/flags/message',
@@ -1794,14 +1627,7 @@ export class StreamChat<
1794
1627
  };
1795
1628
 
1796
1629
  const data = await this.post<{
1797
- channels: ChannelAPIResponse<
1798
- AttachmentType,
1799
- ChannelType,
1800
- CommandType,
1801
- MessageType,
1802
- ReactionType,
1803
- UserType
1804
- >[];
1630
+ channels: ChannelAPIResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>[];
1805
1631
  }>(this.baseURL + '/channels', payload);
1806
1632
 
1807
1633
  const channels: Channel<
@@ -1847,34 +1673,16 @@ export class StreamChat<
1847
1673
  */
1848
1674
  async search(
1849
1675
  filterConditions: ChannelFilters<ChannelType, CommandType, UserType>,
1850
- query:
1851
- | string
1852
- | MessageFilters<
1853
- AttachmentType,
1854
- ChannelType,
1855
- CommandType,
1856
- MessageType,
1857
- ReactionType,
1858
- UserType
1859
- >,
1676
+ query: string | MessageFilters<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>,
1860
1677
  options: SearchOptions<MessageType> = {},
1861
1678
  ) {
1862
1679
  if (options.offset && (options.sort || options.next)) {
1863
1680
  throw Error(`Cannot specify offset with sort or next parameters`);
1864
1681
  }
1865
- const payload: SearchPayload<
1866
- AttachmentType,
1867
- ChannelType,
1868
- CommandType,
1869
- MessageType,
1870
- ReactionType,
1871
- UserType
1872
- > = {
1682
+ const payload: SearchPayload<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType> = {
1873
1683
  filter_conditions: filterConditions,
1874
1684
  ...options,
1875
- sort: options.sort
1876
- ? normalizeQuerySort<SearchMessageSortBase<MessageType>>(options.sort)
1877
- : undefined,
1685
+ sort: options.sort ? normalizeQuerySort<SearchMessageSortBase<MessageType>>(options.sort) : undefined,
1878
1686
  };
1879
1687
  if (typeof query === 'string') {
1880
1688
  payload.query = query;
@@ -1888,14 +1696,7 @@ export class StreamChat<
1888
1696
  await this.setUserPromise;
1889
1697
 
1890
1698
  return await this.get<
1891
- SearchAPIResponse<
1892
- AttachmentType,
1893
- ChannelType,
1894
- CommandType,
1895
- MessageType,
1896
- ReactionType,
1897
- UserType
1898
- >
1699
+ SearchAPIResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>
1899
1700
  >(this.baseURL + '/search', {
1900
1701
  payload,
1901
1702
  });
@@ -1910,7 +1711,7 @@ export class StreamChat<
1910
1711
  *
1911
1712
  */
1912
1713
  setLocalDevice(device: BaseDeviceFields) {
1913
- if (this.wsConnection) {
1714
+ if (this.wsConnection || this.wsFallback) {
1914
1715
  throw new Error('you can only set device before opening a websocket connection');
1915
1716
  }
1916
1717
 
@@ -1986,14 +1787,7 @@ export class StreamChat<
1986
1787
  }
1987
1788
 
1988
1789
  _addChannelConfig(
1989
- channelState: ChannelAPIResponse<
1990
- AttachmentType,
1991
- ChannelType,
1992
- CommandType,
1993
- MessageType,
1994
- ReactionType,
1995
- UserType
1996
- >,
1790
+ channelState: ChannelAPIResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>,
1997
1791
  ) {
1998
1792
  this.configs[channelState.channel.type] = channelState.channel.config;
1999
1793
  }
@@ -2016,27 +1810,11 @@ export class StreamChat<
2016
1810
  channelType: string,
2017
1811
  channelID?: string | null,
2018
1812
  custom?: ChannelData<ChannelType>,
2019
- ): Channel<
2020
- AttachmentType,
2021
- ChannelType,
2022
- CommandType,
2023
- EventType,
2024
- MessageType,
2025
- ReactionType,
2026
- UserType
2027
- >;
1813
+ ): Channel<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>;
2028
1814
  channel(
2029
1815
  channelType: string,
2030
1816
  custom?: ChannelData<ChannelType>,
2031
- ): Channel<
2032
- AttachmentType,
2033
- ChannelType,
2034
- CommandType,
2035
- EventType,
2036
- MessageType,
2037
- ReactionType,
2038
- UserType
2039
- >;
1817
+ ): Channel<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>;
2040
1818
  channel(
2041
1819
  channelType: string,
2042
1820
  channelIDOrCustom?: string | ChannelData<ChannelType> | null,
@@ -2054,15 +1832,12 @@ export class StreamChat<
2054
1832
  // support channel("messaging", undefined, {options})
2055
1833
  // support channel("messaging", "", {options})
2056
1834
  if (channelIDOrCustom == null || channelIDOrCustom === '') {
2057
- return new Channel<
2058
- AttachmentType,
2059
- ChannelType,
2060
- CommandType,
2061
- EventType,
2062
- MessageType,
2063
- ReactionType,
2064
- UserType
2065
- >(this, channelType, undefined, custom);
1835
+ return new Channel<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>(
1836
+ this,
1837
+ channelType,
1838
+ undefined,
1839
+ custom,
1840
+ );
2066
1841
  }
2067
1842
 
2068
1843
  // support channel("messaging", {options})
@@ -2115,9 +1890,7 @@ export class StreamChat<
2115
1890
  }
2116
1891
 
2117
1892
  if (key.indexOf(`${channelType}:!members-`) === 0) {
2118
- const membersStrInExistingChannel = Object.keys(channel.state.members)
2119
- .sort()
2120
- .join(',');
1893
+ const membersStrInExistingChannel = Object.keys(channel.state.members).sort().join(',');
2121
1894
  if (membersStrInExistingChannel === membersStr) {
2122
1895
  return channel;
2123
1896
  }
@@ -2156,11 +1929,7 @@ export class StreamChat<
2156
1929
  *
2157
1930
  * @return {channel} The channel object, initialize it using channel.watch()
2158
1931
  */
2159
- getChannelById = (
2160
- channelType: string,
2161
- channelID: string,
2162
- custom: ChannelData<ChannelType>,
2163
- ) => {
1932
+ getChannelById = (channelType: string, channelID: string, custom: ChannelData<ChannelType>) => {
2164
1933
  if (typeof channelID === 'string' && ~channelID.indexOf(':')) {
2165
1934
  throw Error(`Invalid channel id ${channelID}, can't contain the : character`);
2166
1935
  }
@@ -2308,10 +2077,7 @@ export class StreamChat<
2308
2077
  });
2309
2078
  }
2310
2079
 
2311
- async deactivateUser(
2312
- userID: string,
2313
- options?: { created_by_id?: string; mark_messages_deleted?: boolean },
2314
- ) {
2080
+ async deactivateUser(userID: string, options?: { created_by_id?: string; mark_messages_deleted?: boolean }) {
2315
2081
  return await this.post<APIResponse & { user: UserResponse<UserType> }>(
2316
2082
  this.baseURL + `/users/${userID}/deactivate`,
2317
2083
  {
@@ -2323,14 +2089,7 @@ export class StreamChat<
2323
2089
  async exportUser(userID: string, options?: Record<string, string>) {
2324
2090
  return await this.get<
2325
2091
  APIResponse & {
2326
- messages: MessageResponse<
2327
- AttachmentType,
2328
- ChannelType,
2329
- CommandType,
2330
- MessageType,
2331
- ReactionType,
2332
- UserType
2333
- >[];
2092
+ messages: MessageResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>[];
2334
2093
  reactions: ReactionResponse<ReactionType, UserType>[];
2335
2094
  user: UserResponse<UserType>;
2336
2095
  }
@@ -2346,20 +2105,6 @@ export class StreamChat<
2346
2105
  * @returns {Promise<APIResponse>}
2347
2106
  */
2348
2107
  async banUser(targetUserID: string, options?: BanUserOptions<UserType>) {
2349
- if (options?.user_id !== undefined) {
2350
- options.banned_by_id = options.user_id;
2351
- delete options.user_id;
2352
- console.warn(
2353
- "banUser: 'user_id' is deprecated, please consider switching to 'banned_by_id'",
2354
- );
2355
- }
2356
- if (options?.user !== undefined) {
2357
- options.banned_by = options.user;
2358
- delete options.user;
2359
- console.warn(
2360
- "banUser: 'user' is deprecated, please consider switching to 'banned_by'",
2361
- );
2362
- }
2363
2108
  return await this.post<APIResponse>(this.baseURL + '/moderation/ban', {
2364
2109
  target_user_id: targetUserID,
2365
2110
  ...options,
@@ -2412,19 +2157,12 @@ export class StreamChat<
2412
2157
  * @param {MuteUserOptions<UserType>} [options]
2413
2158
  * @returns {Promise<MuteUserResponse<ChannelType, CommandType, UserType>>}
2414
2159
  */
2415
- async muteUser(
2416
- targetID: string,
2417
- userID?: string,
2418
- options: MuteUserOptions<UserType> = {},
2419
- ) {
2420
- return await this.post<MuteUserResponse<ChannelType, CommandType, UserType>>(
2421
- this.baseURL + '/moderation/mute',
2422
- {
2423
- target_id: targetID,
2424
- ...(userID ? { user_id: userID } : {}),
2425
- ...options,
2426
- },
2427
- );
2160
+ async muteUser(targetID: string, userID?: string, options: MuteUserOptions<UserType> = {}) {
2161
+ return await this.post<MuteUserResponse<ChannelType, CommandType, UserType>>(this.baseURL + '/moderation/mute', {
2162
+ target_id: targetID,
2163
+ ...(userID ? { user_id: userID } : {}),
2164
+ ...options,
2165
+ });
2428
2166
  }
2429
2167
 
2430
2168
  /** unmuteUser - unmutes a user
@@ -2463,13 +2201,10 @@ export class StreamChat<
2463
2201
  * @returns {Promise<APIResponse>}
2464
2202
  */
2465
2203
  async flagMessage(targetMessageID: string, options: { user_id?: string } = {}) {
2466
- return await this.post<FlagMessageResponse<UserType>>(
2467
- this.baseURL + '/moderation/flag',
2468
- {
2469
- target_message_id: targetMessageID,
2470
- ...options,
2471
- },
2472
- );
2204
+ return await this.post<FlagMessageResponse<UserType>>(this.baseURL + '/moderation/flag', {
2205
+ target_message_id: targetMessageID,
2206
+ ...options,
2207
+ });
2473
2208
  }
2474
2209
 
2475
2210
  /**
@@ -2479,13 +2214,10 @@ export class StreamChat<
2479
2214
  * @returns {Promise<APIResponse>}
2480
2215
  */
2481
2216
  async flagUser(targetID: string, options: { user_id?: string } = {}) {
2482
- return await this.post<FlagUserResponse<UserType>>(
2483
- this.baseURL + '/moderation/flag',
2484
- {
2485
- target_user_id: targetID,
2486
- ...options,
2487
- },
2488
- );
2217
+ return await this.post<FlagUserResponse<UserType>>(this.baseURL + '/moderation/flag', {
2218
+ target_user_id: targetID,
2219
+ ...options,
2220
+ });
2489
2221
  }
2490
2222
 
2491
2223
  /**
@@ -2495,13 +2227,10 @@ export class StreamChat<
2495
2227
  * @returns {Promise<APIResponse>}
2496
2228
  */
2497
2229
  async unflagMessage(targetMessageID: string, options: { user_id?: string } = {}) {
2498
- return await this.post<FlagMessageResponse<UserType>>(
2499
- this.baseURL + '/moderation/unflag',
2500
- {
2501
- target_message_id: targetMessageID,
2502
- ...options,
2503
- },
2504
- );
2230
+ return await this.post<FlagMessageResponse<UserType>>(this.baseURL + '/moderation/unflag', {
2231
+ target_message_id: targetMessageID,
2232
+ ...options,
2233
+ });
2505
2234
  }
2506
2235
 
2507
2236
  /**
@@ -2511,13 +2240,10 @@ export class StreamChat<
2511
2240
  * @returns {Promise<APIResponse>}
2512
2241
  */
2513
2242
  async unflagUser(targetID: string, options: { user_id?: string } = {}) {
2514
- return await this.post<FlagUserResponse<UserType>>(
2515
- this.baseURL + '/moderation/unflag',
2516
- {
2517
- target_user_id: targetID,
2518
- ...options,
2519
- },
2520
- );
2243
+ return await this.post<FlagUserResponse<UserType>>(this.baseURL + '/moderation/unflag', {
2244
+ target_user_id: targetID,
2245
+ ...options,
2246
+ });
2521
2247
  }
2522
2248
 
2523
2249
  /**
@@ -2545,10 +2271,7 @@ export class StreamChat<
2545
2271
  }
2546
2272
 
2547
2273
  createCommand(data: CreateCommandOptions<CommandType>) {
2548
- return this.post<CreateCommandResponse<CommandType>>(
2549
- this.baseURL + '/commands',
2550
- data,
2551
- );
2274
+ return this.post<CreateCommandResponse<CommandType>>(this.baseURL + '/commands', data);
2552
2275
  }
2553
2276
 
2554
2277
  getCommand(name: string) {
@@ -2556,16 +2279,11 @@ export class StreamChat<
2556
2279
  }
2557
2280
 
2558
2281
  updateCommand(name: string, data: UpdateCommandOptions<CommandType>) {
2559
- return this.put<UpdateCommandResponse<CommandType>>(
2560
- this.baseURL + `/commands/${name}`,
2561
- data,
2562
- );
2282
+ return this.put<UpdateCommandResponse<CommandType>>(this.baseURL + `/commands/${name}`, data);
2563
2283
  }
2564
2284
 
2565
2285
  deleteCommand(name: string) {
2566
- return this.delete<DeleteCommandResponse<CommandType>>(
2567
- this.baseURL + `/commands/${name}`,
2568
- );
2286
+ return this.delete<DeleteCommandResponse<CommandType>>(this.baseURL + `/commands/${name}`);
2569
2287
  }
2570
2288
 
2571
2289
  listCommands() {
@@ -2574,23 +2292,15 @@ export class StreamChat<
2574
2292
 
2575
2293
  createChannelType(data: CreateChannelOptions<CommandType>) {
2576
2294
  const channelData = Object.assign({}, { commands: ['all'] }, data);
2577
- return this.post<CreateChannelResponse<CommandType>>(
2578
- this.baseURL + '/channeltypes',
2579
- channelData,
2580
- );
2295
+ return this.post<CreateChannelResponse<CommandType>>(this.baseURL + '/channeltypes', channelData);
2581
2296
  }
2582
2297
 
2583
2298
  getChannelType(channelType: string) {
2584
- return this.get<GetChannelTypeResponse<CommandType>>(
2585
- this.baseURL + `/channeltypes/${channelType}`,
2586
- );
2299
+ return this.get<GetChannelTypeResponse<CommandType>>(this.baseURL + `/channeltypes/${channelType}`);
2587
2300
  }
2588
2301
 
2589
2302
  updateChannelType(channelType: string, data: UpdateChannelOptions<CommandType>) {
2590
- return this.put<UpdateChannelResponse<CommandType>>(
2591
- this.baseURL + `/channeltypes/${channelType}`,
2592
- data,
2593
- );
2303
+ return this.put<UpdateChannelResponse<CommandType>>(this.baseURL + `/channeltypes/${channelType}`, data);
2594
2304
  }
2595
2305
 
2596
2306
  deleteChannelType(channelType: string) {
@@ -2611,15 +2321,7 @@ export class StreamChat<
2611
2321
  */
2612
2322
  async translateMessage(messageId: string, language: string) {
2613
2323
  return await this.post<
2614
- APIResponse &
2615
- MessageResponse<
2616
- AttachmentType,
2617
- ChannelType,
2618
- CommandType,
2619
- MessageType,
2620
- ReactionType,
2621
- UserType
2622
- >
2324
+ APIResponse & MessageResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>
2623
2325
  >(this.baseURL + `/messages/${messageId}/translate`, {
2624
2326
  language,
2625
2327
  });
@@ -2648,10 +2350,7 @@ export class StreamChat<
2648
2350
  * @param {string | { id: string }} messageOrMessageId message object or message id
2649
2351
  * @param {string} errorText error message to report in case of message id absence
2650
2352
  */
2651
- _validateAndGetMessageId(
2652
- messageOrMessageId: string | { id: string },
2653
- errorText: string,
2654
- ) {
2353
+ _validateAndGetMessageId(messageOrMessageId: string | { id: string }, errorText: string) {
2655
2354
  let messageId: string;
2656
2355
  if (typeof messageOrMessageId === 'string') {
2657
2356
  messageId = messageOrMessageId;
@@ -2696,10 +2395,7 @@ export class StreamChat<
2696
2395
  * @param {string | { id: string }} messageOrMessageId message object or message id
2697
2396
  * @param {string | { id: string }} [userId]
2698
2397
  */
2699
- unpinMessage(
2700
- messageOrMessageId: string | { id: string },
2701
- userId?: string | { id: string },
2702
- ) {
2398
+ unpinMessage(messageOrMessageId: string | { id: string }, userId?: string | { id: string }) {
2703
2399
  const messageId = this._validateAndGetMessageId(
2704
2400
  messageOrMessageId,
2705
2401
  'Please specify the message id when calling unpinMessage',
@@ -2725,14 +2421,7 @@ export class StreamChat<
2725
2421
  * @return {APIResponse & { message: MessageResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType> }} Response that includes the message
2726
2422
  */
2727
2423
  async updateMessage(
2728
- message: UpdatedMessage<
2729
- AttachmentType,
2730
- ChannelType,
2731
- CommandType,
2732
- MessageType,
2733
- ReactionType,
2734
- UserType
2735
- >,
2424
+ message: UpdatedMessage<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>,
2736
2425
  userId?: string | { id: string },
2737
2426
  options?: { skip_enrich_url?: boolean },
2738
2427
  ) {
@@ -2743,24 +2432,13 @@ export class StreamChat<
2743
2432
  const clonedMessage: Message = Object.assign({}, message);
2744
2433
  delete clonedMessage.id;
2745
2434
 
2746
- const reservedMessageFields: Array<
2747
- | 'command'
2748
- | 'created_at'
2749
- | 'html'
2750
- | 'latest_reactions'
2751
- | 'own_reactions'
2752
- | 'reaction_counts'
2753
- | 'reply_count'
2754
- | 'type'
2755
- | 'updated_at'
2756
- | 'user'
2757
- | '__html'
2758
- > = [
2435
+ const reservedMessageFields: Array<ReservedMessageFields> = [
2759
2436
  'command',
2760
2437
  'created_at',
2761
2438
  'html',
2762
2439
  'latest_reactions',
2763
2440
  'own_reactions',
2441
+ 'quoted_message',
2764
2442
  'reaction_counts',
2765
2443
  'reply_count',
2766
2444
  'type',
@@ -2787,24 +2465,12 @@ export class StreamChat<
2787
2465
  * Server always expects mentioned_users to be array of string. We are adding extra check, just in case
2788
2466
  * SDK missed this conversion.
2789
2467
  */
2790
- if (
2791
- Array.isArray(clonedMessage.mentioned_users) &&
2792
- !isString(clonedMessage.mentioned_users[0])
2793
- ) {
2794
- clonedMessage.mentioned_users = clonedMessage.mentioned_users.map(
2795
- (mu) => ((mu as unknown) as UserResponse).id,
2796
- );
2468
+ if (Array.isArray(clonedMessage.mentioned_users) && !isString(clonedMessage.mentioned_users[0])) {
2469
+ clonedMessage.mentioned_users = clonedMessage.mentioned_users.map((mu) => ((mu as unknown) as UserResponse).id);
2797
2470
  }
2798
2471
 
2799
2472
  return await this.post<
2800
- UpdateMessageAPIResponse<
2801
- AttachmentType,
2802
- ChannelType,
2803
- CommandType,
2804
- MessageType,
2805
- ReactionType,
2806
- UserType
2807
- >
2473
+ UpdateMessageAPIResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>
2808
2474
  >(this.baseURL + `/messages/${message.id}`, {
2809
2475
  message: clonedMessage,
2810
2476
  ...options,
@@ -2838,14 +2504,7 @@ export class StreamChat<
2838
2504
  user = { id: userId };
2839
2505
  }
2840
2506
  return await this.put<
2841
- UpdateMessageAPIResponse<
2842
- AttachmentType,
2843
- ChannelType,
2844
- CommandType,
2845
- MessageType,
2846
- ReactionType,
2847
- UserType
2848
- >
2507
+ UpdateMessageAPIResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>
2849
2508
  >(this.baseURL + `/messages/${id}`, {
2850
2509
  ...partialMessageObject,
2851
2510
  ...options,
@@ -2860,14 +2519,7 @@ export class StreamChat<
2860
2519
  }
2861
2520
  return await this.delete<
2862
2521
  APIResponse & {
2863
- message: MessageResponse<
2864
- AttachmentType,
2865
- ChannelType,
2866
- CommandType,
2867
- MessageType,
2868
- ReactionType,
2869
- UserType
2870
- >;
2522
+ message: MessageResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>;
2871
2523
  }
2872
2524
  >(this.baseURL + `/messages/${messageID}`, params);
2873
2525
  }
@@ -2875,24 +2527,14 @@ export class StreamChat<
2875
2527
  async getMessage(messageID: string) {
2876
2528
  return await this.get<
2877
2529
  APIResponse & {
2878
- message: MessageResponse<
2879
- AttachmentType,
2880
- ChannelType,
2881
- CommandType,
2882
- MessageType,
2883
- ReactionType,
2884
- UserType
2885
- >;
2530
+ message: MessageResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>;
2886
2531
  }
2887
2532
  >(this.baseURL + `/messages/${messageID}`);
2888
2533
  }
2889
2534
 
2890
2535
  getUserAgent() {
2891
2536
  return (
2892
- this.userAgent ||
2893
- `stream-chat-javascript-client-${this.node ? 'node' : 'browser'}-${
2894
- process.env.PKG_VERSION
2895
- }`
2537
+ this.userAgent || `stream-chat-javascript-client-${this.node ? 'node' : 'browser'}-${process.env.PKG_VERSION}`
2896
2538
  );
2897
2539
  }
2898
2540
 
@@ -2911,11 +2553,13 @@ export class StreamChat<
2911
2553
  headers: {},
2912
2554
  config: {},
2913
2555
  },
2914
- ) {
2556
+ ): AxiosRequestConfig {
2915
2557
  const token = this._getToken();
2558
+ const authorization = token ? { Authorization: token } : undefined;
2916
2559
 
2917
2560
  if (!options.headers?.['x-client-request-id']) {
2918
2561
  options.headers = {
2562
+ ...options.headers,
2919
2563
  'x-client-request-id': randomId(),
2920
2564
  };
2921
2565
  }
@@ -2925,10 +2569,10 @@ export class StreamChat<
2925
2569
  user_id: this.userID,
2926
2570
  ...options.params,
2927
2571
  api_key: this.key,
2928
- connection_id: this.wsConnection?.connectionID,
2572
+ connection_id: this._getConnectionID(),
2929
2573
  },
2930
2574
  headers: {
2931
- Authorization: token,
2575
+ ...authorization,
2932
2576
  'stream-auth-type': this.getAuthType(),
2933
2577
  'X-Stream-Client': this.getUserAgent(),
2934
2578
  ...options.headers,
@@ -2956,6 +2600,20 @@ export class StreamChat<
2956
2600
  }, 500);
2957
2601
  }
2958
2602
 
2603
+ /**
2604
+ * encode ws url payload
2605
+ * @private
2606
+ * @returns json string
2607
+ */
2608
+ _buildWSPayload = (client_request_id?: string) => {
2609
+ return JSON.stringify({
2610
+ user_id: this.userID,
2611
+ user_details: this._user,
2612
+ device: this.options.device,
2613
+ client_request_id,
2614
+ });
2615
+ };
2616
+
2959
2617
  verifyWebhook(requestBody: string, xSignature: string) {
2960
2618
  return !!this.secret && CheckSignature(requestBody, this.secret, xSignature);
2961
2619
  }
@@ -3042,15 +2700,7 @@ export class StreamChat<
3042
2700
  sync(channel_cids: string[], last_sync_at: string) {
3043
2701
  return this.post<
3044
2702
  APIResponse & {
3045
- events: Event<
3046
- AttachmentType,
3047
- ChannelType,
3048
- CommandType,
3049
- EventType,
3050
- MessageType,
3051
- ReactionType,
3052
- UserType
3053
- >[];
2703
+ events: Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>[];
3054
2704
  }
3055
2705
  >(`${this.baseURL}/sync`, {
3056
2706
  channel_cids,
@@ -3077,15 +2727,11 @@ export class StreamChat<
3077
2727
  }
3078
2728
 
3079
2729
  listBlockLists() {
3080
- return this.get<APIResponse & { blocklists: BlockListResponse[] }>(
3081
- `${this.baseURL}/blocklists`,
3082
- );
2730
+ return this.get<APIResponse & { blocklists: BlockListResponse[] }>(`${this.baseURL}/blocklists`);
3083
2731
  }
3084
2732
 
3085
2733
  getBlockList(name: string) {
3086
- return this.get<APIResponse & { blocklist: BlockListResponse }>(
3087
- `${this.baseURL}/blocklists/${name}`,
3088
- );
2734
+ return this.get<APIResponse & { blocklist: BlockListResponse }>(`${this.baseURL}/blocklists/${name}`);
3089
2735
  }
3090
2736
 
3091
2737
  updateBlockList(name: string, data: { words: string[] }) {
@@ -3096,18 +2742,12 @@ export class StreamChat<
3096
2742
  return this.delete<APIResponse>(`${this.baseURL}/blocklists/${name}`);
3097
2743
  }
3098
2744
 
3099
- exportChannels(
3100
- request: Array<ExportChannelRequest>,
3101
- options: ExportChannelOptions = {},
3102
- ) {
2745
+ exportChannels(request: Array<ExportChannelRequest>, options: ExportChannelOptions = {}) {
3103
2746
  const payload = {
3104
2747
  channels: request,
3105
2748
  ...options,
3106
2749
  };
3107
- return this.post<APIResponse & ExportChannelResponse>(
3108
- `${this.baseURL}/export_channels`,
3109
- payload,
3110
- );
2750
+ return this.post<APIResponse & ExportChannelResponse>(`${this.baseURL}/export_channels`, payload);
3111
2751
  }
3112
2752
 
3113
2753
  exportChannel(request: ExportChannelRequest, options?: ExportChannelOptions) {
@@ -3115,9 +2755,7 @@ export class StreamChat<
3115
2755
  }
3116
2756
 
3117
2757
  getExportChannelStatus(id: string) {
3118
- return this.get<APIResponse & ExportChannelStatusResponse>(
3119
- `${this.baseURL}/export_channels/${id}`,
3120
- );
2758
+ return this.get<APIResponse & ExportChannelStatusResponse>(`${this.baseURL}/export_channels/${id}`);
3121
2759
  }
3122
2760
 
3123
2761
  /**
@@ -3128,10 +2766,7 @@ export class StreamChat<
3128
2766
  * @return {Segment} The Created Segment
3129
2767
  */
3130
2768
  async createSegment(params: SegmentData) {
3131
- const { segment } = await this.post<{ segment: Segment }>(
3132
- this.baseURL + `/segments`,
3133
- { segment: params },
3134
- );
2769
+ const { segment } = await this.post<{ segment: Segment }>(this.baseURL + `/segments`, { segment: params });
3135
2770
  return segment;
3136
2771
  }
3137
2772
 
@@ -3143,9 +2778,7 @@ export class StreamChat<
3143
2778
  * @return {Segment} A Segment
3144
2779
  */
3145
2780
  async getSegment(id: string) {
3146
- const { segment } = await this.get<{ segment: Segment }>(
3147
- this.baseURL + `/segments/${id}`,
3148
- );
2781
+ const { segment } = await this.get<{ segment: Segment }>(this.baseURL + `/segments/${id}`);
3149
2782
  return segment;
3150
2783
  }
3151
2784
 
@@ -3156,10 +2789,7 @@ export class StreamChat<
3156
2789
  * @return {Segment[]} Segments
3157
2790
  */
3158
2791
  async listSegments(options: { limit?: number; offset?: number }) {
3159
- const { segments } = await this.get<{ segments: Segment[] }>(
3160
- this.baseURL + `/segments`,
3161
- options,
3162
- );
2792
+ const { segments } = await this.get<{ segments: Segment[] }>(this.baseURL + `/segments`, options);
3163
2793
  return segments;
3164
2794
  }
3165
2795
 
@@ -3172,10 +2802,7 @@ export class StreamChat<
3172
2802
  * @return {Segment} Updated Segment
3173
2803
  */
3174
2804
  async updateSegment(id: string, params: Partial<SegmentData>) {
3175
- const { segment } = await this.put<{ segment: Segment }>(
3176
- this.baseURL + `/segments/${id}`,
3177
- { segment: params },
3178
- );
2805
+ const { segment } = await this.put<{ segment: Segment }>(this.baseURL + `/segments/${id}`, { segment: params });
3179
2806
  return segment;
3180
2807
  }
3181
2808
 
@@ -3198,10 +2825,7 @@ export class StreamChat<
3198
2825
  * @return {Campaign} The Created Campaign
3199
2826
  */
3200
2827
  async createCampaign(params: CampaignData) {
3201
- const { campaign } = await this.post<{ campaign: Campaign }>(
3202
- this.baseURL + `/campaigns`,
3203
- { campaign: params },
3204
- );
2828
+ const { campaign } = await this.post<{ campaign: Campaign }>(this.baseURL + `/campaigns`, { campaign: params });
3205
2829
  return campaign;
3206
2830
  }
3207
2831
 
@@ -3213,9 +2837,7 @@ export class StreamChat<
3213
2837
  * @return {Campaign} A Campaign
3214
2838
  */
3215
2839
  async getCampaign(id: string) {
3216
- const { campaign } = await this.get<{ campaign: Campaign }>(
3217
- this.baseURL + `/campaigns/${id}`,
3218
- );
2840
+ const { campaign } = await this.get<{ campaign: Campaign }>(this.baseURL + `/campaigns/${id}`);
3219
2841
  return campaign;
3220
2842
  }
3221
2843
 
@@ -3226,10 +2848,7 @@ export class StreamChat<
3226
2848
  * @return {Campaign[]} Campaigns
3227
2849
  */
3228
2850
  async listCampaigns(options: { limit?: number; offset?: number }) {
3229
- const { campaigns } = await this.get<{ campaigns: Campaign[] }>(
3230
- this.baseURL + `/campaigns`,
3231
- options,
3232
- );
2851
+ const { campaigns } = await this.get<{ campaigns: Campaign[] }>(this.baseURL + `/campaigns`, options);
3233
2852
  return campaigns;
3234
2853
  }
3235
2854
 
@@ -3242,10 +2861,9 @@ export class StreamChat<
3242
2861
  * @return {Campaign} Updated Campaign
3243
2862
  */
3244
2863
  async updateCampaign(id: string, params: Partial<CampaignData>) {
3245
- const { campaign } = await this.put<{ campaign: Campaign }>(
3246
- this.baseURL + `/campaigns/${id}`,
3247
- { campaign: params },
3248
- );
2864
+ const { campaign } = await this.put<{ campaign: Campaign }>(this.baseURL + `/campaigns/${id}`, {
2865
+ campaign: params,
2866
+ });
3249
2867
  return campaign;
3250
2868
  }
3251
2869
 
@@ -3270,10 +2888,9 @@ export class StreamChat<
3270
2888
  */
3271
2889
  async scheduleCampaign(id: string, params: { sendAt: number }) {
3272
2890
  const { sendAt } = params;
3273
- const { campaign } = await this.patch<{ campaign: Campaign }>(
3274
- this.baseURL + `/campaigns/${id}/schedule`,
3275
- { send_at: sendAt },
3276
- );
2891
+ const { campaign } = await this.patch<{ campaign: Campaign }>(this.baseURL + `/campaigns/${id}/schedule`, {
2892
+ send_at: sendAt,
2893
+ });
3277
2894
  return campaign;
3278
2895
  }
3279
2896
 
@@ -3285,9 +2902,7 @@ export class StreamChat<
3285
2902
  * @return {Campaign} Stopped Campaign
3286
2903
  */
3287
2904
  async stopCampaign(id: string) {
3288
- const { campaign } = await this.patch<{ campaign: Campaign }>(
3289
- this.baseURL + `/campaigns/${id}/stop`,
3290
- );
2905
+ const { campaign } = await this.patch<{ campaign: Campaign }>(this.baseURL + `/campaigns/${id}/stop`);
3291
2906
  return campaign;
3292
2907
  }
3293
2908
 
@@ -3299,9 +2914,7 @@ export class StreamChat<
3299
2914
  * @return {Campaign} Resumed Campaign
3300
2915
  */
3301
2916
  async resumeCampaign(id: string) {
3302
- const { campaign } = await this.patch<{ campaign: Campaign }>(
3303
- this.baseURL + `/campaigns/${id}/resume`,
3304
- );
2917
+ const { campaign } = await this.patch<{ campaign: Campaign }>(this.baseURL + `/campaigns/${id}/resume`);
3305
2918
  return campaign;
3306
2919
  }
3307
2920
 
@@ -3314,10 +2927,7 @@ export class StreamChat<
3314
2927
  */
3315
2928
  async testCampaign(id: string, params: { users: string[] }) {
3316
2929
  const { users } = params;
3317
- const { campaign } = await this.post<{ campaign: Campaign }>(
3318
- this.baseURL + `/campaigns/${id}/test`,
3319
- { users },
3320
- );
2930
+ const { campaign } = await this.post<{ campaign: Campaign }>(this.baseURL + `/campaigns/${id}/test`, { users });
3321
2931
  return campaign;
3322
2932
  }
3323
2933
 
@@ -3351,33 +2961,12 @@ export class StreamChat<
3351
2961
  * @return {DeleteChannelsResponse} Result of the soft deletion, if server-side, it holds the task ID as well
3352
2962
  */
3353
2963
  async deleteChannels(cids: string[], options: { hard_delete?: boolean } = {}) {
3354
- return await this.post<APIResponse & DeleteChannelsResponse>(
3355
- this.baseURL + `/channels/delete`,
3356
- { cids, ...options },
3357
- );
2964
+ return await this.post<APIResponse & DeleteChannelsResponse>(this.baseURL + `/channels/delete`, {
2965
+ cids,
2966
+ ...options,
2967
+ });
3358
2968
  }
3359
2969
 
3360
- postInsights = async (insightType: InsightTypes, insights: Record<string, unknown>) => {
3361
- const maxAttempts = 3;
3362
- for (let i = 0; i < maxAttempts; i++) {
3363
- try {
3364
- await this.axiosInstance.post(
3365
- `https://chat-insights.getstream.io/insights/${insightType}`,
3366
- insights,
3367
- );
3368
- } catch (e) {
3369
- this.logger('warn', `failed to send insights event ${insightType}`, {
3370
- tags: ['insights', 'connection'],
3371
- error: e,
3372
- insights,
3373
- });
3374
- await sleep((i + 1) * 3000);
3375
- continue;
3376
- }
3377
- break;
3378
- }
3379
- };
3380
-
3381
2970
  /**
3382
2971
  * deleteUsers - Batch Delete Users
3383
2972
  *
@@ -3390,21 +2979,11 @@ export class StreamChat<
3390
2979
  if (options?.user !== 'soft' && options?.user !== 'hard') {
3391
2980
  throw new Error('Invalid delete user options. user must be one of [soft hard]');
3392
2981
  }
3393
- if (
3394
- options.messages !== undefined &&
3395
- options.messages !== 'soft' &&
3396
- options.messages !== 'hard'
3397
- ) {
2982
+ if (options.messages !== undefined && options.messages !== 'soft' && options.messages !== 'hard') {
3398
2983
  throw new Error('Invalid delete user options. messages must be one of [soft hard]');
3399
2984
  }
3400
- if (
3401
- options.conversations !== undefined &&
3402
- options.conversations !== 'soft' &&
3403
- options.conversations !== 'hard'
3404
- ) {
3405
- throw new Error(
3406
- 'Invalid delete user options. conversations must be one of [soft hard]',
3407
- );
2985
+ if (options.conversations !== undefined && options.conversations !== 'soft' && options.conversations !== 'hard') {
2986
+ throw new Error('Invalid delete user options. conversations must be one of [soft hard]');
3408
2987
  }
3409
2988
  return await this.post<APIResponse & TaskResponse>(this.baseURL + `/users/delete`, {
3410
2989
  user_ids,