@@ -12,59 +12,59 @@ import {
12
12
Context ,
13
13
} from './types' ;
14
14
import processMiddleware from './middleware/process' ;
15
- import { WorkflowFunctionInitializationError } from './errors' ;
15
+ import { CustomFunctionInitializationError } from './errors' ;
16
16
17
17
/** Interfaces */
18
18
19
- export interface FunctionCompleteArguments {
19
+ interface FunctionCompleteArguments {
20
20
outputs ?: {
21
21
[ key : string ] : any ;
22
22
} ;
23
23
}
24
24
25
- export interface FunctionFailArguments {
26
- error : string ;
27
- }
28
-
29
25
export interface FunctionCompleteFn {
30
26
( params ?: FunctionCompleteArguments ) : Promise < FunctionsCompleteSuccessResponse > ;
31
27
}
32
28
29
+ interface FunctionFailArguments {
30
+ error : string ;
31
+ }
32
+
33
33
export interface FunctionFailFn {
34
34
( params : FunctionFailArguments ) : Promise < FunctionsCompleteErrorResponse > ;
35
35
}
36
36
37
- export interface WorkflowFunctionExecuteMiddlewareArgs extends SlackEventMiddlewareArgs < 'function_executed' > {
37
+ export interface CustomFunctionExecuteMiddlewareArgs extends SlackEventMiddlewareArgs < 'function_executed' > {
38
38
complete : FunctionCompleteFn ;
39
39
fail : FunctionFailFn ;
40
40
}
41
41
42
42
/** Types */
43
43
44
- export type SlackWorkflowFunctionMiddlewareArgs = WorkflowFunctionExecuteMiddlewareArgs ;
44
+ export type SlackCustomFunctionMiddlewareArgs = CustomFunctionExecuteMiddlewareArgs ;
45
45
46
- export type WorkflowFunctionExecuteMiddleware = Middleware < WorkflowFunctionExecuteMiddlewareArgs > ;
46
+ type CustomFunctionExecuteMiddleware = Middleware < CustomFunctionExecuteMiddlewareArgs > ;
47
47
48
- export type WorkflowFunctionMiddleware = WorkflowFunctionExecuteMiddleware [ ] ;
48
+ export type CustomFunctionMiddleware = CustomFunctionExecuteMiddleware [ ] ;
49
49
50
- export type AllWorkflowFunctionMiddlewareArgs
51
- < T extends SlackWorkflowFunctionMiddlewareArgs = SlackWorkflowFunctionMiddlewareArgs > = T & AllMiddlewareArgs ;
50
+ export type AllCustomFunctionMiddlewareArgs
51
+ < T extends SlackCustomFunctionMiddlewareArgs = SlackCustomFunctionMiddlewareArgs > = T & AllMiddlewareArgs ;
52
52
53
53
/** Constants */
54
54
55
55
const VALID_PAYLOAD_TYPES = new Set ( [ 'function_executed' ] ) ;
56
56
57
57
/** Class */
58
58
59
- export class WorkflowFunction {
59
+ export class CustomFunction {
60
60
/** Function callback_id */
61
61
public callbackId : string ;
62
62
63
- private middleware : WorkflowFunctionMiddleware ;
63
+ private middleware : CustomFunctionMiddleware ;
64
64
65
65
public constructor (
66
66
callbackId : string ,
67
- middleware : WorkflowFunctionMiddleware ,
67
+ middleware : CustomFunctionMiddleware ,
68
68
) {
69
69
validate ( callbackId , middleware ) ;
70
70
@@ -81,35 +81,69 @@ export class WorkflowFunction {
81
81
} ;
82
82
}
83
83
84
- private matchesConstraints ( args : SlackWorkflowFunctionMiddlewareArgs ) : boolean {
84
+ private matchesConstraints ( args : SlackCustomFunctionMiddlewareArgs ) : boolean {
85
85
return args . payload . function . callback_id === this . callbackId ;
86
86
}
87
87
88
- private async processEvent ( args : AllWorkflowFunctionMiddlewareArgs ) : Promise < void > {
88
+ private async processEvent ( args : AllCustomFunctionMiddlewareArgs ) : Promise < void > {
89
89
const functionArgs = prepareFunctionArgs ( args ) ;
90
90
const stepMiddleware = this . getStepMiddleware ( ) ;
91
91
return processStepMiddleware ( functionArgs , stepMiddleware ) ;
92
92
}
93
93
94
- private getStepMiddleware ( ) : WorkflowFunctionMiddleware {
94
+ private getStepMiddleware ( ) : CustomFunctionMiddleware {
95
95
return this . middleware ;
96
96
}
97
+
98
+ /**
99
+ * Factory for `complete()` utility
100
+ * @param args function_executed event
101
+ */
102
+ public static createFunctionComplete ( context : Context , client : WebClient ) : FunctionCompleteFn {
103
+ const token = selectToken ( context ) ;
104
+ const { functionExecutionId } = context ;
105
+
106
+ return ( params : Parameters < FunctionCompleteFn > [ 0 ] = { } ) => client . functions . completeSuccess ( {
107
+ token,
108
+ outputs : params . outputs || { } ,
109
+ function_execution_id : functionExecutionId ,
110
+ } ) ;
111
+ }
112
+
113
+ /**
114
+ * Factory for `fail()` utility
115
+ * @param args function_executed event
116
+ */
117
+ public static createFunctionFail ( context : Context , client : WebClient ) : FunctionFailFn {
118
+ const token = selectToken ( context ) ;
119
+
120
+ return ( params : Parameters < FunctionFailFn > [ 0 ] ) => {
121
+ const { error } = params ?? { } ;
122
+ const { functionExecutionId } = context ;
123
+
124
+ return client . functions . completeError ( {
125
+ token,
126
+ error,
127
+ function_execution_id : functionExecutionId ,
128
+ } ) ;
129
+ } ;
130
+ }
97
131
}
98
132
99
133
/** Helper Functions */
100
134
101
- export function validate ( callbackId : string , listeners : WorkflowFunctionMiddleware ) : void {
135
+ export function validate ( callbackId : string , listeners : CustomFunctionMiddleware ) : void {
102
136
// Ensure callbackId is valid
103
137
if ( typeof callbackId !== 'string' ) {
104
- const errorMsg = 'WorkflowFunction expects a callback_id as the first argument' ;
105
- throw new WorkflowFunctionInitializationError ( errorMsg ) ;
138
+ const errorMsg = 'CustomFunction expects a callback_id as the first argument' ;
139
+ throw new CustomFunctionInitializationError ( errorMsg ) ;
106
140
}
107
141
108
142
// Ensure all listeners are functions
109
143
listeners . forEach ( ( listener ) => {
110
144
if ( ! ( listener instanceof Function ) ) {
111
- const errorMsg = 'All WorkflowFunction listeners must be functions' ;
112
- throw new WorkflowFunctionInitializationError ( errorMsg ) ;
145
+ const errorMsg = 'All CustomFunction listeners must be functions' ;
146
+ throw new CustomFunctionInitializationError ( errorMsg ) ;
113
147
}
114
148
} ) ;
115
149
}
@@ -119,8 +153,8 @@ export function validate(callbackId: string, listeners: WorkflowFunctionMiddlewa
119
153
* @param args workflow_step_edit action
120
154
*/
121
155
export async function processStepMiddleware (
122
- args : AllWorkflowFunctionMiddlewareArgs ,
123
- middleware : WorkflowFunctionMiddleware ,
156
+ args : AllCustomFunctionMiddlewareArgs ,
157
+ middleware : CustomFunctionMiddleware ,
124
158
) : Promise < void > {
125
159
const { context, client, logger } = args ;
126
160
const callbacks = [ ...middleware ] as Middleware < AnyMiddlewareArgs > [ ] ;
@@ -134,59 +168,13 @@ export async function processStepMiddleware(
134
168
}
135
169
}
136
170
137
- export function isFunctionEvent ( args : AnyMiddlewareArgs ) : args is AllWorkflowFunctionMiddlewareArgs {
171
+ function isFunctionEvent ( args : AnyMiddlewareArgs ) : args is AllCustomFunctionMiddlewareArgs {
138
172
return VALID_PAYLOAD_TYPES . has ( args . payload . type ) ;
139
173
}
140
174
141
175
function selectToken ( context : Context ) : string | undefined {
142
176
// If attachFunctionToken = false, fallback to botToken or userToken
143
- return context . functionBotToken ? context . functionBotToken : context . botToken || context . userToken ;
144
- }
145
-
146
- /**
147
- * Factory for `complete()` utility
148
- * @param args function_executed event
149
- */
150
- export function createFunctionComplete (
151
- args : AllWorkflowFunctionMiddlewareArgs < WorkflowFunctionExecuteMiddlewareArgs > ,
152
- ) : FunctionCompleteFn {
153
- const {
154
- context,
155
- client,
156
- payload : { function_execution_id } ,
157
- } = args ;
158
- const token = selectToken ( context ) ;
159
-
160
- return ( params : Parameters < FunctionCompleteFn > [ 0 ] = { } ) => client . functions . completeSuccess ( {
161
- token,
162
- outputs : params . outputs || { } ,
163
- function_execution_id,
164
- } ) ;
165
- }
166
-
167
- /**
168
- * Factory for `fail()` utility
169
- * @param args function_executed event
170
- */
171
- export function createFunctionFail (
172
- args : AllWorkflowFunctionMiddlewareArgs < WorkflowFunctionExecuteMiddlewareArgs > ,
173
- ) : FunctionFailFn {
174
- const {
175
- context,
176
- client,
177
- payload : { function_execution_id } ,
178
- } = args ;
179
- const token = selectToken ( context ) ;
180
-
181
- return ( params : Parameters < FunctionFailFn > [ 0 ] ) => {
182
- const { error } = params ?? { } ;
183
-
184
- return client . functions . completeError ( {
185
- token,
186
- error,
187
- function_execution_id,
188
- } ) ;
189
- } ;
177
+ return context . functionBotAccessToken ? context . functionBotAccessToken : context . botToken || context . userToken ;
190
178
}
191
179
192
180
/**
@@ -195,20 +183,20 @@ export function createFunctionFail(
195
183
* - events will *not* continue down global middleware chain to subsequent listeners
196
184
* 2. augments args with step lifecycle-specific properties/utilities
197
185
* */
198
- export function prepareFunctionArgs ( args : any ) : AllWorkflowFunctionMiddlewareArgs {
186
+ function prepareFunctionArgs ( args : any ) : AllCustomFunctionMiddlewareArgs {
199
187
const { next : _next , ...functionArgs } = args ;
200
188
const preparedArgs : any = { ...functionArgs } ;
201
189
const token = selectToken ( functionArgs . context ) ;
202
190
203
- // Making calls with a functionBotToken establishes continuity between
191
+ // Making calls with a functionBotAccessToken establishes continuity between
204
192
// a function_executed event and subsequent interactive events (actions)
205
193
const client = new WebClient ( token , { ...functionArgs . client } ) ;
206
194
preparedArgs . client = client ;
207
195
208
196
// Utility args
209
197
preparedArgs . inputs = preparedArgs . event . inputs ;
210
- preparedArgs . complete = createFunctionComplete ( preparedArgs ) ;
211
- preparedArgs . fail = createFunctionFail ( preparedArgs ) ;
198
+ preparedArgs . complete = CustomFunction . createFunctionComplete ( preparedArgs . context , client ) ;
199
+ preparedArgs . fail = CustomFunction . createFunctionFail ( preparedArgs . context , client ) ;
212
200
213
201
return preparedArgs ;
214
202
}
0 commit comments