Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtm-api: add support for custom webClient #1696

Merged
merged 5 commits into from Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/_packages/rtm_api.md
Expand Up @@ -644,6 +644,30 @@ const rtm = new RTMClient(token, options);

---

### Custom WebClient

In some cases, you might want to customize the underlying component making HTTP requests to the Slack API, the [`WebClient`](reference/web-api#webclient), beyond the provided [`RTMClientOptions`](reference/rtm-api#rtmclientoptions). Note that overriding the [`WebClient`](reference/web-api#webclient) instance takes precedence over any other [`RTMClientOptions`](reference/rtm-api#rtmclientoptions) specified.

```javascript
const { RTMClient } = require('@slack/rtm-api');
const { WebClient, WebClientOptions } = require('@slack/web-api');
const token = process.env.SLACK_BOT_TOKEN;

// Configure the client to have custom headers
const options = {
headers: {
'Cookie': 'myCookie=cookieValue;'
}
} as WebClientOptions;

const webClient = new WebClient(token, options);

// Initialize a client using the configuration
const rtm = new RTMClient(token, { webClient });
```

---

### Workspace state snapshot

The client can receive a snapshot of a portion of the workspace's state while its connecting. This can be useful if your
Expand Down
8 changes: 8 additions & 0 deletions docs/_reference/rtm-api.md
Expand Up @@ -15,6 +15,7 @@ slug: rtm-api
<th align="center">Name</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Description</th>
<th></th>
</tr>
</thead>
Expand All @@ -31,6 +32,13 @@ slug: rtm-api
<td align="center">✗</td>
<td></td>
</tr>
<tr>
<td align="center">webClient</td>
<td align="center"><code><a href="web-api#webclient" title="">WebClient</a></code></td>
<td align="center">✗</td>
<td align="center">An optional parameter to provide a customized <a href="web-api#webclient">WebClient</a>. Any desired options for the custom client must be set in this parameter (<code>webClient</code>) as they will take precedence over other arguments passed into <code>RTMClient</code>.</td>
<td></td>
</tr>
</tbody>
</table>
<strong>Options:</strong>
Expand Down
4 changes: 3 additions & 1 deletion packages/rtm-api/src/RTMClient.ts
Expand Up @@ -348,9 +348,10 @@
serverPongTimeout,
replyAckOnReconnectTimeout = 2000,
tls = undefined,
webClient,
}: RTMClientOptions = {}) {
super();
this.webClient = new WebClient(token, {
this.webClient = webClient || new WebClient(token, {
slackApiUrl,
logger,
logLevel,
Expand Down Expand Up @@ -628,7 +629,7 @@
let event;
try {
event = JSON.parse(data);
} catch (parseError: any) {

Check warning on line 632 in packages/rtm-api/src/RTMClient.ts

View workflow job for this annotation

GitHub Actions / test (18.x, packages/rtm-api)

Unexpected any. Specify a different type

Check warning on line 632 in packages/rtm-api/src/RTMClient.ts

View workflow job for this annotation

GitHub Actions / test (20.x, packages/rtm-api)

Unexpected any. Specify a different type
// prevent application from crashing on a bad message, but log an error to bring attention
this.logger.error(
`unable to parse incoming websocket message: ${parseError.message}\n message contents: "${data}"`,
Expand Down Expand Up @@ -672,6 +673,7 @@
*/

export interface RTMClientOptions {
webClient?: WebClient;
slackApiUrl?: string;
logger?: Logger;
logLevel?: LogLevel;
Expand Down