From 62e45706d544abf09757befed2f4cbb338d56bdf Mon Sep 17 00:00:00 2001 From: Ishan Garg <73502828+ishangarg0@users.noreply.github.com> Date: Mon, 27 Nov 2023 10:26:07 -0800 Subject: [PATCH] rtm-api: add support for custom webClient (#1696) --- docs/_packages/rtm_api.md | 24 ++++++++++++++++++++++++ docs/_reference/rtm-api.md | 8 ++++++++ packages/rtm-api/src/RTMClient.ts | 4 +++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/_packages/rtm_api.md b/docs/_packages/rtm_api.md index 3079f34a2..63c254dc9 100644 --- a/docs/_packages/rtm_api.md +++ b/docs/_packages/rtm_api.md @@ -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 diff --git a/docs/_reference/rtm-api.md b/docs/_reference/rtm-api.md index 8b38176b7..980069cc4 100644 --- a/docs/_reference/rtm-api.md +++ b/docs/_reference/rtm-api.md @@ -15,6 +15,7 @@ slug: rtm-api Name Type Required +Description @@ -31,6 +32,13 @@ slug: rtm-api ✗ + +webClient +WebClient +✗ +An optional parameter to provide a customized WebClient. Any desired options for the custom client must be set in this parameter (webClient) as they will take precedence over other arguments passed into RTMClient. + + Options: diff --git a/packages/rtm-api/src/RTMClient.ts b/packages/rtm-api/src/RTMClient.ts index 8e901c9b6..f4fb19ae3 100644 --- a/packages/rtm-api/src/RTMClient.ts +++ b/packages/rtm-api/src/RTMClient.ts @@ -348,9 +348,10 @@ export class RTMClient extends EventEmitter { serverPongTimeout, replyAckOnReconnectTimeout = 2000, tls = undefined, + webClient, }: RTMClientOptions = {}) { super(); - this.webClient = new WebClient(token, { + this.webClient = webClient || new WebClient(token, { slackApiUrl, logger, logLevel, @@ -672,6 +673,7 @@ export default RTMClient; */ export interface RTMClientOptions { + webClient?: WebClient; slackApiUrl?: string; logger?: Logger; logLevel?: LogLevel;