Skip to content

Commit

Permalink
Refactor arguments and type tests for functions.* APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Dec 7, 2023
1 parent 456c49b commit 2f1f9da
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
22 changes: 9 additions & 13 deletions packages/web-api/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ import type { AdminBarriersCreateArguments, AdminBarriersDeleteArguments, AdminB
import type { AdminConversationsArchiveArguments, AdminConversationsBulkArchiveArguments, AdminConversationsBulkDeleteArguments, AdminConversationsBulkMoveArguments, AdminConversationsConvertToPrivateArguments, AdminConversationsConvertToPublicArguments, AdminConversationsCreateArguments, AdminConversationsDeleteArguments, AdminConversationsDisconnectSharedArguments, AdminConversationsEKMListOriginalConnectedChannelInfoArguments, AdminConversationsGetConversationPrefsArguments, AdminConversationsGetCustomRetentionArguments, AdminConversationsGetTeamsArguments, AdminConversationsInviteArguments, AdminConversationsLookupArguments, AdminConversationsRemoveCustomRetentionArguments, AdminConversationsRenameArguments, AdminConversationsRestrictAccessAddGroupArguments, AdminConversationsRestrictAccessListGroupsArguments, AdminConversationsRestrictAccessRemoveGroupArguments, AdminConversationsSearchArguments, AdminConversationsSetConversationPrefsArguments, AdminConversationsSetCustomRetentionArguments, AdminConversationsSetTeamsArguments, AdminConversationsUnarchiveArguments } from './types/request/admin/conversations';
import type { AdminEmojiAddArguments, AdminEmojiAddAliasArguments, AdminEmojiListArguments, AdminEmojiRemoveArguments, AdminEmojiRenameArguments } from './types/request/admin/emoji';
import type { AdminFunctionsListArguments, AdminFunctionsPermissionsLookupArguments, AdminFunctionsPermissionsSetArguments } from './types/request/admin/functions';
import type { FunctionsCompleteErrorArguments, FunctionsCompleteSuccessArguments } from './types/request/functions';

/**
* Generic method definition
Expand Down Expand Up @@ -1311,7 +1312,15 @@ export abstract class Methods extends EventEmitter<WebClientEvent> {
};

public readonly functions = {
/**
* @description Signal the failure to execute a Custom Function.
* @see {@link https://api.slack.com/methods/functions.completeError `functions.completeError` API reference}.
*/
completeError: bindApiCall<FunctionsCompleteErrorArguments, FunctionsCompleteErrorResponse>(this, 'functions.completeError'),
/**
* @description Signal the successful completion of a Custom Function.
* @see {@link https://api.slack.com/methods/functions.completeSuccess `functions.completeSuccess` API reference}.
*/
completeSuccess: bindApiCall<FunctionsCompleteSuccessArguments, FunctionsCompleteSuccessResponse>(
this,
'functions.completeSuccess',
Expand Down Expand Up @@ -1957,17 +1966,4 @@ export interface AdminWorkflowsUnpublishArguments extends TokenOverridable {
workflow_ids: string[];
}

/*
* `functions.*`
*/
export interface FunctionsCompleteErrorArguments extends TokenOverridable {
function_execution_id: string;
error: string;
}

export interface FunctionsCompleteSuccessArguments extends TokenOverridable {
function_execution_id: string;
outputs: Record<string, unknown>;
}

export * from '@slack/types';
2 changes: 1 addition & 1 deletion packages/web-api/src/types/request/files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Stream } from 'node:stream';
import { CursorPaginationEnabled, OptionalTeamAssignable, TokenOverridable, TraditionalPagingEnabled } from './common';
import type { CursorPaginationEnabled, OptionalTeamAssignable, TokenOverridable, TraditionalPagingEnabled } from './common';
import type { FilesGetUploadURLExternalResponse } from '../response';

interface FileArgument {
Expand Down
15 changes: 15 additions & 0 deletions packages/web-api/src/types/request/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { TokenOverridable } from './common';

interface ExecutionID {
function_execution_id: string;
}

// https://api.slack.com/methods/functions.completeError
export interface FunctionsCompleteErrorArguments extends ExecutionID, TokenOverridable {
error: string;
}

// https://api.slack.com/methods/functions.completeSuccess
export interface FunctionsCompleteSuccessArguments extends ExecutionID, TokenOverridable {
outputs: Record<string, unknown>;
}
36 changes: 36 additions & 0 deletions packages/web-api/test/types/methods/functions.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expectAssignable, expectError } from 'tsd';
import { WebClient } from '../../../src/WebClient';

const web = new WebClient('TOKEN');

// functions.completeError
// -- sad path
expectError(web.functions.completeError()); // lacking argument
expectError(web.functions.completeError({})); // empty argument
expectError(web.functions.completeError({
function_execution_id: 'Fx1234', // missing error
}));
expectError(web.functions.completeError({
error: 'boomsies', // missing function_execution_id
}));
// -- happy path
expectAssignable<Parameters<typeof web.functions.completeError>>([{
function_execution_id: 'Fx1234',
error: 'oh noes',
}]);

// functions.completeSuccess
// -- sad path
expectError(web.functions.completeSuccess()); // lacking argument
expectError(web.functions.completeSuccess({})); // empty argument
expectError(web.functions.completeSuccess({
function_execution_id: 'Fx1234', // missing output
}));
expectError(web.functions.completeSuccess({
outputs: {}, // missing function_execution_id
}));
// -- happy path
expectAssignable<Parameters<typeof web.functions.completeSuccess>>([{
function_execution_id: 'Fx1234',
outputs: {},
}]);

0 comments on commit 2f1f9da

Please sign in to comment.