Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/web-api/src/WebClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,62 @@ describe('WebClient', () => {
}
});
});

describe('accepts an AbortSignal to cancel requests', () => {
let scope: nock.Scope;
beforeEach(() => {
scope = nock('https://slack.com').post(/api/).delay(50).reply(200, { ok: true });
});

it('cancels the request when the signal is aborted', async () => {
const client = new WebClient(token);
const controller = new AbortController();
const { signal } = controller;

setTimeout(() => {
controller.abort();
}, 1);

try {
await client.conversations.list({}, { signal });
assert.fail('Expected error to be thrown');
} catch (error) {
scope.done();
}
});

it('completes the request when the signal is not aborted', async () => {
const client = new WebClient(token);
const controller = new AbortController();
const { signal } = controller;

try {
const response = await client.conversations.list({}, { signal });
assert.equal(response.ok, true);
scope.done();
} catch (error) {
assert.fail(`Did not expect an error to be thrown: ${(error as Error).message}`);
}
});

it('uses the AbortSignal reason', async () => {
const client = new WebClient(token);
const controller = new AbortController();
const { signal } = controller;
const abortReason = new Error('Abort reason');
setTimeout(() => {
controller.abort(abortReason);
}, 1);

try {
await client.conversations.list({}, { signal });
assert.fail('Expected error to be thrown');
} catch (error) {
assert.equal(error, abortReason);
scope.done();
}
});
});
});

// Helpers
Expand Down
12 changes: 11 additions & 1 deletion packages/web-api/src/WebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ export class WebClient extends Methods {
* @param method - the Web API method to call {@link https://docs.slack.dev/reference/methods}
* @param options - options
*/
public async apiCall(method: string, options: Record<string, unknown> = {}): Promise<WebAPICallResult> {
public async apiCall(
method: string,
options: Record<string, unknown> = {},
config?: { signal?: AbortSignal },
): Promise<WebAPICallResult> {
this.logger.debug(`apiCall('${method}') start`);

warnDeprecations(method, this.logger);
Expand All @@ -370,6 +374,7 @@ export class WebClient extends Methods {
...options,
},
headers,
config,
);
const result = await this.buildResult(response);
this.logger.debug(`http request result: ${JSON.stringify(result)}`);
Expand Down Expand Up @@ -677,6 +682,7 @@ export class WebClient extends Methods {
url: string,
body: Record<string, unknown>,
headers: Record<string, string> = {},
options?: { signal?: AbortSignal },
): Promise<AxiosResponse> {
// TODO: better input types - remove any
const task = () =>
Expand All @@ -685,6 +691,7 @@ export class WebClient extends Methods {
// biome-ignore lint/suspicious/noExplicitAny: TODO: type this
const config: any = {
headers,
signal: options?.signal,
...this.tlsConfig,
};
// admin.analytics.getFile returns a binary response
Expand Down Expand Up @@ -762,6 +769,9 @@ export class WebClient extends Methods {
// biome-ignore lint/suspicious/noExplicitAny: errors can be anything
const e = error as any;
this.logger.warn('http request failed', e.message);
if (e.name === 'CanceledError' && options?.signal?.reason) {
throw new pRetry.AbortError(options.signal.reason);
}
if (e.request) {
throw requestErrorWithOriginal(e, this.attachOriginalToWebAPIRequestError);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/web-api/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,11 @@ import { type WebAPICallResult, WebClient, type WebClientEvent } from './WebClie
*/
type MethodWithRequiredArgument<MethodArguments, MethodResult extends WebAPICallResult = WebAPICallResult> = (
options: MethodArguments,
config?: { signal?: AbortSignal },
) => Promise<MethodResult>;
type MethodWithOptionalArgument<MethodArguments, MethodResult extends WebAPICallResult = WebAPICallResult> = (
options?: MethodArguments,
config?: { signal?: AbortSignal },
) => Promise<MethodResult>;

export default MethodWithOptionalArgument;
Expand Down Expand Up @@ -578,7 +580,11 @@ export abstract class Methods extends EventEmitter<WebClientEvent> {
}
}

public abstract apiCall(method: string, options?: Record<string, unknown>): Promise<WebAPICallResult>;
public abstract apiCall(
method: string,
options?: Record<string, unknown>,
config?: { signal?: AbortSignal },
): Promise<WebAPICallResult>;
public abstract filesUploadV2(options: FilesUploadV2Arguments): Promise<WebAPICallResult>;

public readonly admin = {
Expand Down
Loading