@stream-io/video-client 0.0.9 → 0.0.11

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/index.cjs.js CHANGED
@@ -10222,7 +10222,7 @@ class TokenManager {
10222
10222
  return;
10223
10223
  // Don't allow empty token for non-server side client.
10224
10224
  if (!this.secret && !tokenOrProvider) {
10225
- throw new Error('User token can not be empty');
10225
+ throw new Error('UserWithId token can not be empty');
10226
10226
  }
10227
10227
  if (tokenOrProvider &&
10228
10228
  typeof tokenOrProvider !== 'string' &&
@@ -10535,7 +10535,7 @@ class StreamClient {
10535
10535
  /**
10536
10536
  * connectUser - Set the current user and open a WebSocket connection
10537
10537
  *
10538
- * @param {OwnUserResponse} user Data about this user. IE {name: "john"}
10538
+ * @param user Data about this user. IE {name: "john"}
10539
10539
  * @param {TokenOrProvider} userTokenOrProvider Token or provider
10540
10540
  *
10541
10541
  * @return {ConnectAPIResponse} Returns a promise that resolves when the connection is setup
@@ -10612,7 +10612,7 @@ class StreamClient {
10612
10612
  this.openConnection = () => __awaiter(this, void 0, void 0, function* () {
10613
10613
  var _d, _e, _f;
10614
10614
  if (!this.userID) {
10615
- throw Error('User is not set on client, use client.connectUser or client.connectAnonymousUser instead');
10615
+ throw Error('UserWithId is not set on client, use client.connectUser or client.connectAnonymousUser instead');
10616
10616
  }
10617
10617
  if (((_d = this.wsConnection) === null || _d === void 0 ? void 0 : _d.isConnecting) && this.wsPromise) {
10618
10618
  this.logger('info', 'client:openConnection() - connection already in progress', {
@@ -10672,7 +10672,9 @@ class StreamClient {
10672
10672
  });
10673
10673
  this.doAxiosRequest = (type, url, data, options = {}) => __awaiter(this, void 0, void 0, function* () {
10674
10674
  var _g;
10675
- yield this.tokenManager.tokenReady();
10675
+ if (!options.publicEndpoint || this.user) {
10676
+ yield this.tokenManager.tokenReady();
10677
+ }
10676
10678
  const requestConfig = this._enrichAxiosOptions(options);
10677
10679
  try {
10678
10680
  let response;
@@ -11025,7 +11027,7 @@ class StreamClient {
11025
11027
  }
11026
11028
  getUserAgent() {
11027
11029
  return (this.userAgent ||
11028
- `stream-video-javascript-client-${this.node ? 'node' : 'browser'}-${"0.0.8"}`);
11030
+ `stream-video-javascript-client-${this.node ? 'node' : 'browser'}-${"0.0.10"}`);
11029
11031
  }
11030
11032
  setUserAgent(userAgent) {
11031
11033
  this.userAgent = userAgent;
@@ -11036,7 +11038,7 @@ class StreamClient {
11036
11038
  config: {},
11037
11039
  }) {
11038
11040
  var _a;
11039
- const token = this._getToken();
11041
+ const token = options.publicEndpoint && !this.user ? undefined : this._getToken();
11040
11042
  const authorization = token ? { Authorization: token } : undefined;
11041
11043
  let signal = null;
11042
11044
  if (this.nextRequestAbortController !== null) {
@@ -11046,7 +11048,9 @@ class StreamClient {
11046
11048
  if (!((_a = options.headers) === null || _a === void 0 ? void 0 : _a['x-client-request-id'])) {
11047
11049
  options.headers = Object.assign(Object.assign({}, options.headers), { 'x-client-request-id': randomId() });
11048
11050
  }
11049
- return Object.assign(Object.assign(Object.assign({ params: Object.assign({ user_id: this.userID, connection_id: this._getConnectionID(), api_key: this.key }, options.params), headers: Object.assign(Object.assign(Object.assign({}, authorization), { 'stream-auth-type': this.getAuthType(), 'X-Stream-Client': this.getUserAgent() }), options.headers) }, (signal ? { signal } : {})), options.config), this.options.axiosRequestConfig);
11051
+ return Object.assign(Object.assign(Object.assign({ params: Object.assign({ user_id: this.userID, connection_id: this._getConnectionID(), api_key: this.key }, options.params), headers: Object.assign(Object.assign(Object.assign({}, authorization), { 'stream-auth-type': options.publicEndpoint && !this.user
11052
+ ? 'anonymous'
11053
+ : this.getAuthType(), 'X-Stream-Client': this.getUserAgent() }), options.headers) }, (signal ? { signal } : {})), options.config), this.options.axiosRequestConfig);
11050
11054
  }
11051
11055
  _getToken() {
11052
11056
  if (!this.tokenManager)
@@ -11063,7 +11067,7 @@ class StreamClient {
11063
11067
  * createToken - Creates a token to authenticate this user. This function is used server side.
11064
11068
  * The resulting token should be passed to the client side when the users registers or logs in.
11065
11069
  *
11066
- * @param {string} userID The User ID
11070
+ * @param {string} userID The UserWithId ID
11067
11071
  * @param {number} [exp] The expiration time for the token expressed in the number of seconds since the epoch
11068
11072
  * @param call_cids for anonymous tokens you have to provide the call cids the use can join
11069
11073
  *
@@ -11098,108 +11102,6 @@ class StreamVideoClient {
11098
11102
  */
11099
11103
  constructor(apiKey, opts) {
11100
11104
  this.eventHandlersToUnregister = [];
11101
- /**
11102
- * Connects the given user to the client.
11103
- * Only one user can connect at a time, if you want to change users, call `disconnectUser` before connecting a new user.
11104
- * If the connection is successful, the connected user [state variable](#readonlystatestore) will be updated accordingly.
11105
- *
11106
- * @param user the user to connect.
11107
- * @param tokenOrProvider a token or a function that returns a token.
11108
- */
11109
- this.connectUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
11110
- var _a;
11111
- const connectUser = () => {
11112
- return this.streamClient.connectUser(
11113
- // @ts-expect-error
11114
- user, tokenOrProvider);
11115
- };
11116
- this.connectionPromise = this.disconnectionPromise
11117
- ? this.disconnectionPromise.then(() => connectUser())
11118
- : connectUser();
11119
- (_a = this.connectionPromise) === null || _a === void 0 ? void 0 : _a.finally(() => (this.connectionPromise = undefined));
11120
- const connectUserResponse = yield this.connectionPromise;
11121
- this.writeableStateStore.setConnectedUser(user);
11122
- this.eventHandlersToUnregister.push(this.on('connection.changed', (e) => {
11123
- const event = e;
11124
- if (event.online) {
11125
- const callsToReWatch = this.writeableStateStore.calls
11126
- .filter((call) => call.watching)
11127
- .map((call) => call.cid);
11128
- if (callsToReWatch.length > 0) {
11129
- this.queryCalls({
11130
- watch: true,
11131
- filter_conditions: {
11132
- cid: { $in: callsToReWatch },
11133
- },
11134
- sort: [{ field: 'cid', direction: 1 }],
11135
- }).catch((err) => {
11136
- console.warn('Failed to re-watch calls', err);
11137
- });
11138
- }
11139
- }
11140
- }));
11141
- this.eventHandlersToUnregister.push(this.on('call.created', (event) => {
11142
- if (event.type !== 'call.created')
11143
- return;
11144
- const { call, members } = event;
11145
- if (user.id === call.created_by.id) {
11146
- console.warn('Received `call.created` sent by the current user');
11147
- return;
11148
- }
11149
- this.writeableStateStore.registerCall(new Call({
11150
- streamClient: this.streamClient,
11151
- type: call.type,
11152
- id: call.id,
11153
- metadata: call,
11154
- members,
11155
- clientStore: this.writeableStateStore,
11156
- }));
11157
- }));
11158
- this.eventHandlersToUnregister.push(this.on('call.ring', (event) => __awaiter(this, void 0, void 0, function* () {
11159
- if (event.type !== 'call.ring')
11160
- return;
11161
- const { call, members } = event;
11162
- if (user.id === call.created_by.id) {
11163
- console.warn('Received `call.ring` sent by the current user');
11164
- return;
11165
- }
11166
- // The call might already be tracked by the client,
11167
- // if `call.created` was received before `call.ring`.
11168
- // In that case, we just reuse the already tracked call.
11169
- let theCall = this.writeableStateStore.findCall(call.type, call.id);
11170
- if (!theCall) {
11171
- // otherwise, we create a new call
11172
- theCall = new Call({
11173
- streamClient: this.streamClient,
11174
- type: call.type,
11175
- id: call.id,
11176
- members,
11177
- clientStore: this.writeableStateStore,
11178
- ringing: true,
11179
- });
11180
- }
11181
- // we fetch the latest metadata for the call from the server
11182
- yield theCall.get({ ring: true });
11183
- this.writeableStateStore.registerCall(theCall);
11184
- })));
11185
- return connectUserResponse;
11186
- });
11187
- /**
11188
- * Connects the given anonymous user to the client.
11189
- *
11190
- * @param user the user to connect.
11191
- * @param tokenOrProvider a token or a function that returns a token.
11192
- */
11193
- this.connectAnonymousUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
11194
- const connectAnonymousUser = () =>
11195
- // @ts-expect-error
11196
- this.streamClient.connectAnonymousUser(user, tokenOrProvider);
11197
- this.connectionPromise = this.disconnectionPromise
11198
- ? this.disconnectionPromise.then(() => connectAnonymousUser())
11199
- : connectAnonymousUser();
11200
- this.connectionPromise.finally(() => (this.connectionPromise = undefined));
11201
- return this.connectionPromise;
11202
- });
11203
11105
  /**
11204
11106
  * Disconnects the currently connected user from the client.
11205
11107
  *
@@ -11260,7 +11162,7 @@ class StreamVideoClient {
11260
11162
  * @param data the data for the guest user.
11261
11163
  */
11262
11164
  this.createGuestUser = (data) => __awaiter(this, void 0, void 0, function* () {
11263
- return this.streamClient.post('/guest', data);
11165
+ return this.streamClient.doAxiosRequest('post', '/guest', data, { publicEndpoint: true });
11264
11166
  });
11265
11167
  /**
11266
11168
  * Will query the API for calls matching the given filters.
@@ -11341,10 +11243,123 @@ class StreamVideoClient {
11341
11243
  this.removeDevice = (id, userID) => __awaiter(this, void 0, void 0, function* () {
11342
11244
  return yield this.streamClient.delete('/devices', Object.assign({ id }, (userID ? { user_id: userID } : {})));
11343
11245
  });
11246
+ /**
11247
+ * Connects the given anonymous user to the client.
11248
+ *
11249
+ * @param user the user to connect.
11250
+ * @param tokenOrProvider a token or a function that returns a token.
11251
+ */
11252
+ this.connectAnonymousUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
11253
+ const connectAnonymousUser = () => this.streamClient.connectAnonymousUser(user, tokenOrProvider);
11254
+ this.connectionPromise = this.disconnectionPromise
11255
+ ? this.disconnectionPromise.then(() => connectAnonymousUser())
11256
+ : connectAnonymousUser();
11257
+ this.connectionPromise.finally(() => (this.connectionPromise = undefined));
11258
+ return this.connectionPromise;
11259
+ });
11344
11260
  this.streamClient = new StreamClient(apiKey, Object.assign({ persistUserOnConnectionFailure: true }, opts));
11345
11261
  this.writeableStateStore = new StreamVideoWriteableStateStore();
11346
11262
  this.readOnlyStateStore = new StreamVideoReadOnlyStateStore(this.writeableStateStore);
11347
11263
  }
11264
+ /**
11265
+ * Connects the given user to the client.
11266
+ * Only one user can connect at a time, if you want to change users, call `disconnectUser` before connecting a new user.
11267
+ * If the connection is successful, the connected user [state variable](#readonlystatestore) will be updated accordingly.
11268
+ *
11269
+ * @param user the user to connect.
11270
+ * @param tokenOrProvider a token or a function that returns a token.
11271
+ */
11272
+ connectUser(user, token) {
11273
+ var _a;
11274
+ return __awaiter(this, void 0, void 0, function* () {
11275
+ if (user.type === 'anonymous') {
11276
+ user.id = '!anon';
11277
+ return this.connectAnonymousUser(user, token);
11278
+ }
11279
+ if (user.type === 'guest') {
11280
+ const response = yield this.createGuestUser({
11281
+ user: Object.assign(Object.assign({}, user), { role: 'guest' }),
11282
+ });
11283
+ return this.connectUser(response.user, response.access_token);
11284
+ }
11285
+ const connectUser = () => {
11286
+ return this.streamClient.connectUser(user, token);
11287
+ };
11288
+ this.connectionPromise = this.disconnectionPromise
11289
+ ? this.disconnectionPromise.then(() => connectUser())
11290
+ : connectUser();
11291
+ (_a = this.connectionPromise) === null || _a === void 0 ? void 0 : _a.finally(() => (this.connectionPromise = undefined));
11292
+ const connectUserResponse = yield this.connectionPromise;
11293
+ // connectUserResponse will be void if connectUser called twice for the same user
11294
+ if (connectUserResponse === null || connectUserResponse === void 0 ? void 0 : connectUserResponse.me) {
11295
+ this.writeableStateStore.setConnectedUser(connectUserResponse.me);
11296
+ }
11297
+ this.eventHandlersToUnregister.push(this.on('connection.changed', (e) => {
11298
+ const event = e;
11299
+ if (event.online) {
11300
+ const callsToReWatch = this.writeableStateStore.calls
11301
+ .filter((call) => call.watching)
11302
+ .map((call) => call.cid);
11303
+ if (callsToReWatch.length > 0) {
11304
+ this.queryCalls({
11305
+ watch: true,
11306
+ filter_conditions: {
11307
+ cid: { $in: callsToReWatch },
11308
+ },
11309
+ sort: [{ field: 'cid', direction: 1 }],
11310
+ }).catch((err) => {
11311
+ console.warn('Failed to re-watch calls', err);
11312
+ });
11313
+ }
11314
+ }
11315
+ }));
11316
+ this.eventHandlersToUnregister.push(this.on('call.created', (event) => {
11317
+ if (event.type !== 'call.created')
11318
+ return;
11319
+ const { call, members } = event;
11320
+ if (user.id === call.created_by.id) {
11321
+ console.warn('Received `call.created` sent by the current user');
11322
+ return;
11323
+ }
11324
+ this.writeableStateStore.registerCall(new Call({
11325
+ streamClient: this.streamClient,
11326
+ type: call.type,
11327
+ id: call.id,
11328
+ metadata: call,
11329
+ members,
11330
+ clientStore: this.writeableStateStore,
11331
+ }));
11332
+ }));
11333
+ this.eventHandlersToUnregister.push(this.on('call.ring', (event) => __awaiter(this, void 0, void 0, function* () {
11334
+ if (event.type !== 'call.ring')
11335
+ return;
11336
+ const { call, members } = event;
11337
+ if (user.id === call.created_by.id) {
11338
+ console.warn('Received `call.ring` sent by the current user');
11339
+ return;
11340
+ }
11341
+ // The call might already be tracked by the client,
11342
+ // if `call.created` was received before `call.ring`.
11343
+ // In that case, we just reuse the already tracked call.
11344
+ let theCall = this.writeableStateStore.findCall(call.type, call.id);
11345
+ if (!theCall) {
11346
+ // otherwise, we create a new call
11347
+ theCall = new Call({
11348
+ streamClient: this.streamClient,
11349
+ type: call.type,
11350
+ id: call.id,
11351
+ members,
11352
+ clientStore: this.writeableStateStore,
11353
+ ringing: true,
11354
+ });
11355
+ }
11356
+ // we fetch the latest metadata for the call from the server
11357
+ yield theCall.get({ ring: true });
11358
+ this.writeableStateStore.registerCall(theCall);
11359
+ })));
11360
+ return connectUserResponse;
11361
+ });
11362
+ }
11348
11363
  /**
11349
11364
  * addDevice - Adds a push device for a user.
11350
11365
  *