Skip to content

Commit d855779

Browse files
committed
add isOwnUser type guard and add OwnUser to Client User type
1 parent 90ae067 commit d855779

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/client.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
Mute,
6464
MuteUserOptions,
6565
MuteUserResponse,
66+
OwnUserResponse,
6667
PartialUserUpdate,
6768
PermissionAPIResponse,
6869
PermissionsAPIResponse,
@@ -101,7 +102,7 @@ export class StreamChat<
101102
ReactionType extends UnknownType = UnknownType,
102103
UserType extends UnknownType = UnknownType
103104
> {
104-
_user?: UserResponse<UserType>;
105+
_user?: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>;
105106
activeChannels: {
106107
[key: string]: Channel<
107108
AttachmentType,
@@ -159,7 +160,7 @@ export class StreamChat<
159160
setUserPromise: ConnectAPIResponse<ChannelType, CommandType, UserType> | null;
160161
state: ClientState<UserType>;
161162
tokenManager: TokenManager<UserType>;
162-
user?: UserResponse<UserType>;
163+
user?: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>;
163164
userAgent?: string;
164165
userID?: string;
165166
wsBaseURL?: string;
@@ -334,13 +335,13 @@ export class StreamChat<
334335
/**
335336
* connectUser - Set the current user and open a WebSocket connection
336337
*
337-
* @param {UserResponse<UserType>} user Data about this user. IE {name: "john"}
338+
* @param {OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>} user Data about this user. IE {name: "john"}
338339
* @param {TokenOrProvider} userTokenOrProvider Token or provider
339340
*
340341
* @return {ConnectAPIResponse<ChannelType, CommandType, UserType>} Returns a promise that resolves when the connection is setup
341342
*/
342343
connectUser = (
343-
user: UserResponse<UserType>,
344+
user: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>,
344345
userTokenOrProvider: TokenOrProvider,
345346
): ConnectAPIResponse<ChannelType, CommandType, UserType> => {
346347
if (this.userID) {
@@ -386,21 +387,23 @@ export class StreamChat<
386387
*
387388
* setUser - Set the current user and open a WebSocket connection
388389
*
389-
* @param {UserResponse<UserType>} user Data about this user. IE {name: "john"}
390+
* @param {OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>} user Data about this user. IE {name: "john"}
390391
* @param {TokenOrProvider} userTokenOrProvider Token or provider
391392
*
392393
* @return {ConnectAPIResponse<ChannelType, CommandType, UserType>} Returns a promise that resolves when the connection is setup
393394
*/
394395
setUser = (
395-
user: UserResponse<UserType>,
396+
user: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>,
396397
userTokenOrProvider: TokenOrProvider,
397398
): ConnectAPIResponse<ChannelType, CommandType, UserType> =>
398399
this.connectUser(user, userTokenOrProvider);
399400

400401
_setToken = (user: UserResponse<UserType>, userTokenOrProvider: TokenOrProvider) =>
401402
this.tokenManager.setTokenOrProvider(userTokenOrProvider, user);
402403

403-
_setUser(user: UserResponse<UserType>) {
404+
_setUser(
405+
user: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>,
406+
) {
404407
// this one is used by the frontend
405408
this.user = user;
406409
// this one is actually used for requests...

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export * from './permissions';
99
export * from './signing';
1010
export * from './token_manager';
1111
export * from './types';
12-
export { chatCodes, logChatPromiseExecution } from './utils';
12+
export { isOwnUser, chatCodes, logChatPromiseExecution } from './utils';

src/utils.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import FormData from 'form-data';
2-
import { AscDesc, QuerySort } from './types';
2+
import {
3+
AscDesc,
4+
LiteralStringForUnion,
5+
OwnUserResponse,
6+
QuerySort,
7+
UnknownType,
8+
UserResponse,
9+
} from './types';
310

411
/**
512
* logChatPromiseExecution - utility function for logging the execution of a promise..
@@ -55,6 +62,19 @@ function isFileWebAPI(uri: unknown): uri is File {
5562
return typeof window !== 'undefined' && 'File' in window && uri instanceof File;
5663
}
5764

65+
export function isOwnUser<
66+
ChannelType extends UnknownType = UnknownType,
67+
CommandType extends string = LiteralStringForUnion,
68+
UserType extends UnknownType = UnknownType
69+
>(
70+
user?: OwnUserResponse<ChannelType, CommandType, UserType> | UserResponse<UserType>,
71+
): user is OwnUserResponse<ChannelType, CommandType, UserType> {
72+
return (
73+
(user as OwnUserResponse<ChannelType, CommandType, UserType>)?.total_unread_count !==
74+
undefined
75+
);
76+
}
77+
5878
export function addFileToFormData(
5979
uri: string | NodeJS.ReadableStream | Buffer | File,
6080
name?: string,

0 commit comments

Comments
 (0)