@stream-io/video-client 0.0.5 → 0.0.7

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.
@@ -1,7 +1,7 @@
1
1
  import { Call } from './Call';
2
2
  import { StreamClient } from './coordinator/connection/client';
3
3
  import { StreamVideoReadOnlyStateStore } from './store';
4
- import type { CreateCallTypeRequest, CreateCallTypeResponse, CreateDeviceRequest, CreateGuestRequest, CreateGuestResponse, GetCallTypeResponse, GetEdgesResponse, ListCallTypeResponse, ListDevicesResponse, QueryCallsRequest, UpdateCallTypeRequest, UpdateCallTypeResponse } from './gen/coordinator';
4
+ import type { ConnectedEvent, CreateCallTypeRequest, CreateCallTypeResponse, CreateDeviceRequest, CreateGuestRequest, CreateGuestResponse, GetCallTypeResponse, GetEdgesResponse, ListCallTypeResponse, ListDevicesResponse, QueryCallsRequest, UpdateCallTypeRequest, UpdateCallTypeResponse } from './gen/coordinator';
5
5
  import type { EventHandler, EventTypes, StreamClientOptions, TokenOrProvider, User } from './coordinator/connection/types';
6
6
  /**
7
7
  * A `StreamVideoClient` instance lets you communicate with our API, and authenticate users.
@@ -14,6 +14,8 @@ export declare class StreamVideoClient {
14
14
  private readonly writeableStateStore;
15
15
  streamClient: StreamClient;
16
16
  private eventHandlersToUnregister;
17
+ private connectionPromise;
18
+ private disconnectionPromise;
17
19
  /**
18
20
  * You should create only one instance of `StreamVideoClient`.
19
21
  * @param apiKey your Stream API key
@@ -28,14 +30,14 @@ export declare class StreamVideoClient {
28
30
  * @param user the user to connect.
29
31
  * @param tokenOrProvider a token or a function that returns a token.
30
32
  */
31
- connectUser: (user: User, tokenOrProvider: TokenOrProvider) => Promise<void | import("./gen/coordinator").ConnectedEvent>;
33
+ connectUser: (user: User, tokenOrProvider: TokenOrProvider) => Promise<void | ConnectedEvent>;
32
34
  /**
33
35
  * Connects the given anonymous user to the client.
34
36
  *
35
37
  * @param user the user to connect.
36
38
  * @param tokenOrProvider a token or a function that returns a token.
37
39
  */
38
- connectAnonymousUser: (user: User, tokenOrProvider: TokenOrProvider) => Promise<void>;
40
+ connectAnonymousUser: (user: User, tokenOrProvider: TokenOrProvider) => Promise<void | ConnectedEvent>;
39
41
  /**
40
42
  * Disconnects the currently connected user from the client.
41
43
  *
@@ -124,4 +126,15 @@ export declare class StreamVideoClient {
124
126
  *
125
127
  */
126
128
  removeDevice: (id: string, userID?: string) => Promise<unknown>;
129
+ /**
130
+ * createToken - Creates a token to authenticate this user. This function is used server side.
131
+ * The resulting token should be passed to the client side when the users registers or logs in.
132
+ *
133
+ * @param {string} userID The User ID
134
+ * @param {number} [exp] The expiration time for the token expressed in the number of seconds since the epoch
135
+ * @param call_cids for anonymous tokens you have to provide the call cids the use can join
136
+ *
137
+ * @return {string} Returns a token
138
+ */
139
+ createToken(userID: string, exp?: number, iat?: number, call_cids?: string[]): string;
127
140
  }
@@ -167,4 +167,15 @@ export declare class StreamClient {
167
167
  * creates an abort controller that will be used by the next HTTP Request.
168
168
  */
169
169
  createAbortControllerForNextRequest(): AbortController;
170
+ /**
171
+ * createToken - Creates a token to authenticate this user. This function is used server side.
172
+ * The resulting token should be passed to the client side when the users registers or logs in.
173
+ *
174
+ * @param {string} userID The User ID
175
+ * @param {number} [exp] The expiration time for the token expressed in the number of seconds since the epoch
176
+ * @param call_cids for anonymous tokens you have to provide the call cids the use can join
177
+ *
178
+ * @return {string} Returns a token
179
+ */
180
+ createToken(userID: string, exp?: number, iat?: number, call_cids?: string[]): string;
170
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/video-client",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "packageManager": "yarn@3.2.4",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.es.js",
@@ -5,6 +5,7 @@ import {
5
5
  StreamVideoWriteableStateStore,
6
6
  } from './store';
7
7
  import type {
8
+ ConnectedEvent,
8
9
  CreateCallTypeRequest,
9
10
  CreateCallTypeResponse,
10
11
  CreateDeviceRequest,
@@ -40,6 +41,8 @@ export class StreamVideoClient {
40
41
  streamClient: StreamClient;
41
42
 
42
43
  private eventHandlersToUnregister: Array<() => void> = [];
44
+ private connectionPromise: Promise<void | ConnectedEvent> | undefined;
45
+ private disconnectionPromise: Promise<void> | undefined;
43
46
 
44
47
  /**
45
48
  * You should create only one instance of `StreamVideoClient`.
@@ -48,8 +51,6 @@ export class StreamVideoClient {
48
51
  */
49
52
  constructor(apiKey: string, opts?: StreamClientOptions) {
50
53
  this.streamClient = new StreamClient(apiKey, {
51
- // FIXME: OL: fix SSR.
52
- browser: true,
53
54
  persistUserOnConnectionFailure: true,
54
55
  ...opts,
55
56
  });
@@ -69,11 +70,19 @@ export class StreamVideoClient {
69
70
  * @param tokenOrProvider a token or a function that returns a token.
70
71
  */
71
72
  connectUser = async (user: User, tokenOrProvider: TokenOrProvider) => {
72
- const connectUserResponse = await this.streamClient.connectUser(
73
- // @ts-expect-error
74
- user,
75
- tokenOrProvider,
76
- );
73
+ const connectUser = () => {
74
+ return this.streamClient.connectUser(
75
+ // @ts-expect-error
76
+ user,
77
+ tokenOrProvider,
78
+ );
79
+ };
80
+ this.connectionPromise = this.disconnectionPromise
81
+ ? this.disconnectionPromise.then(() => connectUser())
82
+ : connectUser();
83
+
84
+ this.connectionPromise?.finally(() => (this.connectionPromise = undefined));
85
+ const connectUserResponse = await this.connectionPromise;
77
86
  this.writeableStateStore.setConnectedUser(user);
78
87
 
79
88
  this.eventHandlersToUnregister.push(
@@ -165,8 +174,14 @@ export class StreamVideoClient {
165
174
  user: User,
166
175
  tokenOrProvider: TokenOrProvider,
167
176
  ) => {
168
- // @ts-expect-error
169
- return this.streamClient.connectAnonymousUser(user, tokenOrProvider);
177
+ const connectAnonymousUser = () =>
178
+ // @ts-expect-error
179
+ this.streamClient.connectAnonymousUser(user, tokenOrProvider);
180
+ this.connectionPromise = this.disconnectionPromise
181
+ ? this.disconnectionPromise.then(() => connectAnonymousUser())
182
+ : connectAnonymousUser();
183
+ this.connectionPromise.finally(() => (this.connectionPromise = undefined));
184
+ return this.connectionPromise;
170
185
  };
171
186
 
172
187
  /**
@@ -178,7 +193,14 @@ export class StreamVideoClient {
178
193
  * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
179
194
  */
180
195
  disconnectUser = async (timeout?: number) => {
181
- await this.streamClient.disconnectUser(timeout);
196
+ const disconnectUser = () => this.streamClient.disconnectUser(timeout);
197
+ this.disconnectionPromise = this.connectionPromise
198
+ ? this.connectionPromise.then(() => disconnectUser())
199
+ : disconnectUser();
200
+ this.disconnectionPromise.finally(
201
+ () => (this.disconnectionPromise = undefined),
202
+ );
203
+ await this.disconnectionPromise;
182
204
  this.eventHandlersToUnregister.forEach((unregister) => unregister());
183
205
  this.eventHandlersToUnregister = [];
184
206
  this.writeableStateStore.setConnectedUser(undefined);
@@ -370,4 +392,23 @@ export class StreamVideoClient {
370
392
  ...(userID ? { user_id: userID } : {}),
371
393
  });
372
394
  };
395
+
396
+ /**
397
+ * createToken - Creates a token to authenticate this user. This function is used server side.
398
+ * The resulting token should be passed to the client side when the users registers or logs in.
399
+ *
400
+ * @param {string} userID The User ID
401
+ * @param {number} [exp] The expiration time for the token expressed in the number of seconds since the epoch
402
+ * @param call_cids for anonymous tokens you have to provide the call cids the use can join
403
+ *
404
+ * @return {string} Returns a token
405
+ */
406
+ createToken(
407
+ userID: string,
408
+ exp?: number,
409
+ iat?: number,
410
+ call_cids?: string[],
411
+ ) {
412
+ return this.streamClient.createToken(userID, exp, iat, call_cids);
413
+ }
373
414
  }
@@ -8,7 +8,7 @@ import axios, {
8
8
  import https from 'https';
9
9
  import WebSocket from 'isomorphic-ws';
10
10
  import { StableWSConnection } from './connection';
11
- import { DevToken } from './signing';
11
+ import { DevToken, JWTUserToken } from './signing';
12
12
  import { TokenManager } from './token_manager';
13
13
  import { WSConnectionFallback } from './connection_fallback';
14
14
  import { isErrorResponse, isWSFailure } from './errors';
@@ -804,4 +804,42 @@ export class StreamClient {
804
804
  createAbortControllerForNextRequest() {
805
805
  return (this.nextRequestAbortController = new AbortController());
806
806
  }
807
+
808
+ /**
809
+ * createToken - Creates a token to authenticate this user. This function is used server side.
810
+ * The resulting token should be passed to the client side when the users registers or logs in.
811
+ *
812
+ * @param {string} userID The User ID
813
+ * @param {number} [exp] The expiration time for the token expressed in the number of seconds since the epoch
814
+ * @param call_cids for anonymous tokens you have to provide the call cids the use can join
815
+ *
816
+ * @return {string} Returns a token
817
+ */
818
+ createToken(
819
+ userID: string,
820
+ exp?: number,
821
+ iat?: number,
822
+ call_cids?: string[],
823
+ ) {
824
+ if (this.secret == null) {
825
+ throw Error(
826
+ `tokens can only be created server-side using the API Secret`,
827
+ );
828
+ }
829
+ const extra: { exp?: number; iat?: number; call_cids?: string[] } = {};
830
+
831
+ if (exp) {
832
+ extra.exp = exp;
833
+ }
834
+
835
+ if (iat) {
836
+ extra.iat = iat;
837
+ }
838
+
839
+ if (call_cids) {
840
+ extra.call_cids = call_cids;
841
+ }
842
+
843
+ return JWTUserToken(this.secret, userID, extra, {});
844
+ }
807
845
  }