Skip to content

Commit 2085900

Browse files
feat(web-api): expose public ts property on ChatStream (#2611)
Co-authored-by: William Bergamin <wbergamin@salesforce.com>
1 parent f430ae8 commit 2085900

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"@slack/web-api": minor
3+
---
4+
5+
feat: expose public read-only `ts` getter on `ChatStreamer` for fallback to [`chat.update`](https://docs.slack.dev/reference/methods/chat.update) when a stream expires server-side
6+
7+
```js
8+
import { WebClient } from "@slack/web-api";
9+
10+
const client = new WebClient(process.env.SLACK_BOT_TOKEN);
11+
12+
const streamer = client.chatStream({
13+
channel: "C0123456789",
14+
thread_ts: "1700000001.123456",
15+
recipient_team_id: "T0123456789",
16+
recipient_user_id: "U0123456789",
17+
});
18+
19+
await streamer.append({ markdown_text: "hello!" });
20+
// streamer.ts is now set after the first flush
21+
console.log(streamer.ts);
22+
23+
await streamer.stop();
24+
```

packages/web-api/src/WebClient.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,34 @@ describe('WebClient', () => {
12951295
scope.done();
12961296
});
12971297

1298+
it('ts is undefined before flush and set after', async () => {
1299+
const scope = nock('https://slack.com')
1300+
.post('/api/chat.startStream')
1301+
.reply(200, {
1302+
ok: true,
1303+
ts: '123.123',
1304+
})
1305+
.post('/api/chat.stopStream')
1306+
.reply(200, {
1307+
ok: true,
1308+
});
1309+
const streamer = client.chatStream({
1310+
buffer_size: 5,
1311+
channel: 'C0123456789',
1312+
thread_ts: '123.000',
1313+
recipient_team_id: 'T0123456789',
1314+
recipient_user_id: 'U0123456789',
1315+
});
1316+
assert.strictEqual(streamer.ts, undefined);
1317+
1318+
await streamer.append({ markdown_text: 'hello!' });
1319+
assert.strictEqual(streamer.ts, '123.123');
1320+
1321+
await streamer.stop();
1322+
assert.strictEqual(streamer.ts, '123.123');
1323+
scope.done();
1324+
});
1325+
12981326
it('streams a long message', async () => {
12991327
const contextActionsBlock: ContextActionsBlock = {
13001328
type: 'context_actions',

packages/web-api/src/chat-stream.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export class ChatStreamer {
5454
this.streamArgs = args;
5555
}
5656

57+
/**
58+
* @description The message timestamp of the stream. Returns `undefined` until the first flush
59+
* (when `chat.startStream` is called).
60+
* @see {@link https://docs.slack.dev/reference/methods/chat.update}
61+
*/
62+
get ts(): string | undefined {
63+
return this.streamTs;
64+
}
65+
5766
/**
5867
* Append to the stream.
5968
*

0 commit comments

Comments
 (0)