Skip to content

Commit e2a7077

Browse files
committed
fix(slack): narrow cached write client options
1 parent 742981f commit e2a7077

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

extensions/slack/src/client.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
33
import os from "node:os";
44
import path from "node:path";
5-
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
5+
import type { WebClientOptions } from "@slack/web-api";
6+
import { afterEach, beforeAll, beforeEach, describe, expect, expectTypeOf, it, vi } from "vitest";
67

78
vi.mock("@slack/web-api", () => {
89
const WebClient = vi.fn(function WebClientMock(
@@ -255,16 +256,20 @@ describe("slack web client config", () => {
255256
expect(WebClient).toHaveBeenCalledTimes(2);
256257
});
257258

259+
it("only exposes API-root options on cached write clients", () => {
260+
expectTypeOf<NonNullable<Parameters<typeof getSlackWriteClient>[1]>>().toEqualTypeOf<
261+
Pick<WebClientOptions, "slackApiUrl">
262+
>();
263+
});
264+
258265
it("keeps write clients separated by Slack API URL client options", () => {
259266
clearProxyEnvForTest();
260267
try {
261268
const firstOptions = {
262269
slackApiUrl: "http://127.0.0.1:49152/api/",
263-
timeout: 1000,
264270
};
265271
const secondOptions = {
266272
slackApiUrl: "http://127.0.0.1:49153/api/",
267-
timeout: 1000,
268273
};
269274
const first = getSlackWriteClient("xoxb-test", firstOptions);
270275
const second = getSlackWriteClient("xoxb-test", secondOptions);

extensions/slack/src/client.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Slack plugin module implements client behavior.
22
import { createHash } from "node:crypto";
33
import { type WebClientOptions, WebClient } from "@slack/web-api";
4-
import {
5-
resolveSlackWebClientOptions,
6-
resolveSlackWriteClientOptions,
7-
} from "./client-options.js";
4+
import { resolveSlackWebClientOptions, resolveSlackWriteClientOptions } from "./client-options.js";
85

96
const SLACK_WRITE_CLIENT_CACHE_MAX = 32;
107
const slackWriteClientCache = new Map<string, WebClient>();
118

9+
type SlackWriteClientCacheOptions = Pick<WebClientOptions, "slackApiUrl">;
10+
1211
export {
1312
resolveSlackWebClientOptions,
1413
resolveSlackWriteClientOptions,
@@ -28,12 +27,15 @@ export function createSlackTokenCacheKey(token: string): string {
2827
return `sha256:${createHash("sha256").update(token).digest("base64url")}`;
2928
}
3029

31-
function slackWriteClientCacheKey(token: string, options: WebClientOptions): string {
30+
function slackWriteClientCacheKey(token: string, options: SlackWriteClientCacheOptions): string {
3231
const tokenKey = createSlackTokenCacheKey(token);
3332
return options.slackApiUrl ? `${tokenKey}:api:${options.slackApiUrl}` : tokenKey;
3433
}
3534

36-
export function getSlackWriteClient(token: string, options: WebClientOptions = {}): WebClient {
35+
export function getSlackWriteClient(
36+
token: string,
37+
options: SlackWriteClientCacheOptions = {},
38+
): WebClient {
3739
const resolvedOptions = resolveSlackWriteClientOptions(options);
3840
const tokenKey = slackWriteClientCacheKey(token, resolvedOptions);
3941
const cached = slackWriteClientCache.get(tokenKey);

0 commit comments

Comments
 (0)