Skip to content

Commit bc8be74

Browse files
authored
Merge pull request #16 from onkernel/update-app-framework
update app framework to support no-payload actions
2 parents 8e22c75 + d1014c3 commit bc8be74

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

src/core/app-framework.ts

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface KernelContext {
44

55
export interface KernelAction {
66
name: string;
7-
handler: (context: KernelContext, input: any) => Promise<any>;
7+
handler: (context: KernelContext, payload?: any) => Promise<any>;
88
}
99

1010
export interface KernelJson {
@@ -34,45 +34,28 @@ export class KernelApp {
3434
* Define an action
3535
*/
3636
action<T, R>(
37-
nameOrHandler: string | ((input: T) => Promise<R>) | ((context: KernelContext, input: T) => Promise<R>),
38-
handler?: ((input: T) => Promise<R>) | ((context: KernelContext, input: T) => Promise<R>),
37+
name: string,
38+
handler: ((context: KernelContext) => Promise<R>) | ((context: KernelContext, payload?: T) => Promise<R>),
3939
) {
40-
let actionName: string;
41-
let actionHandler: (context: KernelContext, input: T) => Promise<R>;
42-
43-
if (typeof nameOrHandler === 'string' && handler) {
44-
// Case: app.action("name", handler)
45-
actionName = nameOrHandler;
46-
47-
// Create a handler that accepts context and input, adapting if needed
48-
if (handler.length === 1) {
49-
// Original handler only takes input, so we adapt it to the new signature
50-
const singleArgHandler = handler as (input: T) => Promise<R>;
51-
actionHandler = async (context: KernelContext, input: T) => singleArgHandler(input);
52-
} else {
53-
// Handler takes both context and input
54-
actionHandler = handler as (context: KernelContext, input: T) => Promise<R>;
55-
}
56-
} else if (typeof nameOrHandler === 'function') {
57-
// Case: app.action(handler)
58-
actionName = nameOrHandler.name || 'default';
59-
60-
// Create a handler that accepts context and input, adapting if needed
61-
if (nameOrHandler.length === 1) {
62-
// Original handler only takes input, so we adapt it to the new signature
63-
const singleArgHandler = nameOrHandler as (input: T) => Promise<R>;
64-
actionHandler = async (context: KernelContext, input: T) => singleArgHandler(input);
65-
} else {
66-
// Handler takes both context and input
67-
actionHandler = nameOrHandler as (context: KernelContext, input: T) => Promise<R>;
68-
}
40+
let actionHandler: (context: KernelContext, payload?: T) => Promise<R>;
41+
42+
// Create a handler that accepts context and payload, adapting if needed
43+
if (handler.length === 0) {
44+
// Handlers with no arguments are not supported
45+
throw new Error('Action handlers must accept at least the context parameter');
46+
} else if (handler.length === 1) {
47+
// Handler takes context only
48+
const contextOnlyHandler = handler as (context: KernelContext) => Promise<R>;
49+
actionHandler = async (context: KernelContext, _payload?: T) => contextOnlyHandler(context);
6950
} else {
70-
throw new Error('Invalid action definition');
51+
// Handler takes both context and payload
52+
const twoArgHandler = handler as (context: KernelContext, payload?: T) => Promise<R>;
53+
actionHandler = twoArgHandler;
7154
}
7255

7356
// Register the action
74-
this.actions.set(actionName, {
75-
name: actionName,
57+
this.actions.set(name, {
58+
name,
7659
handler: actionHandler,
7760
});
7861

0 commit comments

Comments
 (0)