Skip to content

Function: createAction()

createAction<Input, ExceptionHandler>(options): ActionBuilder<Input, never, Ctx, false, ExceptionHandler>

Defined in: packages/action/src/index.ts:36

Creates a new action builder for composing middleware and action functions.

Type Parameters

Input

Input = never

The input type that will be passed to the action when executed.

ExceptionHandler

ExceptionHandler extends (ex) => unknown = (ex) => never

Function type that handles exceptions thrown during execution. Must return a value or throw.

Parameters

options

Configuration options for the action

onThrow?

ExceptionHandler

Optional custom exception handler function

Returns

ActionBuilder<Input, never, Ctx, false, ExceptionHandler>

A new ActionBuilder instance

Example

typescript
const action = createAction<{ token: string }>({ onThrow: console.error })
  .with(async ({ input }) => {
    const session = await getSession(input.token);
    if (!session) throw new Error('Session not found');
    return next({ session });
  })
  .with(async ({ ctx }) => {
    const user = await getUser(ctx.session.userId);
    if (!user) throw new Error('User not found');
    return next({ user });
  })
  .do(async ({ ctx }) => {
    const blogPosts = await getBlogPosts(ctx.user.id);
    return blogPosts ?? [];
  });

const result = await action({ token: 'test' });
// BlogPost[]