Skip to content

Commit

Permalink
refactor!: convert Generics into a single Generic (#837)
Browse files Browse the repository at this point in the history
* Refactor Generics into Single Generic

* fix missing bracket

* run linter

Co-authored-by: Amin Mahboubi <amin@getstream.io>
Co-authored-by: Vishal Narkhede <vishalnarkhede.iitd@gmail.com>
  • Loading branch information
3 people authored Feb 11, 2022
1 parent 9794e59 commit 6406db4
Show file tree
Hide file tree
Showing 12 changed files with 1,224 additions and 1,811 deletions.
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ Documentation for this JavaScript client are available at the [Stream Website](h
The StreamChat client is setup to allow extension of the base types through use of generics when instantiated. The default instantiation has all generics set to `Record<string, unknown>`.

```typescript
StreamChat<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>
StreamChat<{
attachmentType: AttachmentType;
channelType: ChannelType;
commandType: CommandType;
eventType: EventType;
messageType: MessageType;
reactionType: ReactionType;
userType: UserType;
}>
```

Custom types provided when initializing the client will carry through to all client returns and provide intellisense to queries.
Expand All @@ -57,26 +65,26 @@ type CustomReaction = { size?: number };
type ChatEvent = { quitChannel?: boolean };
type CustomCommands = 'giphy';

type StreamType = {
attachmentType: ChatAttachment;
channelType: ChatChannel;
commandType: CustomCommands;
eventType: ChatEvent;
messageType: UserMessage | AdminMessage;
reactionType: CustomReaction;
userType: ChatUser1 | ChatUser2;
};

// Instantiate a new client (server side)
// you can also use `new StreamChat<T,T,...>()`
const client = StreamChat.getInstance<
ChatAttachment,
ChatChannel,
CustomCommands,
ChatEvent,
UserMessage | AdminMessage,
CustomReaction,
ChatUser1 | ChatUser2
>('YOUR_API_KEY', 'API_KEY_SECRET');
const client = StreamChat.getInstance<StreamType>('YOUR_API_KEY', 'API_KEY_SECRET');

/**
* Instantiate a new client (client side)
* Unused generics default to Record<string, unknown>
* with the exception of Command which defaults to string & {}
*/
const client = StreamChat.getInstance<{}, ChatChannel, {}, {}, UserMessage | AdminMessage, {}, ChatUser1 | ChatUser2>(
'YOUR_API_KEY',
);
const client = StreamChat.getInstance<StreamType>('YOUR_API_KEY');
```

Query operations will return results that utilize the custom types added via generics. In addition the query filters are type checked and provide intellisense using both the key and type of the parameter to ensure accurate use.
Expand Down
Loading

0 comments on commit 6406db4

Please sign in to comment.