Skip to content

createActions

createActions<TUser>(config): object

Defined in: packages/actions/src/create-actions.ts:106

Creates a scoped action factory bound to a shared context provider.

Returns two functions — createAction and composeActions — that share the same getContext callback. This ensures every action in the application resolves its database, user, and grants consistently.

TUser = any

The shape of the authenticated user object.

ActionsConfig<TUser>

The ActionsConfig providing the getContext callback.

An object with createAction and composeActions functions.

composeActions: <TActions>(actions) => ComposedActions<TActions>

Combines multiple action definitions into a single route handler that dispatches by the _action discriminator field.

The returned ComposedActions object provides a unified .action handler, a .loader() wrapper that checks permissions for all actions at once, and a .client descriptor covering every action name.

TActions extends Record<string, ActionDefinition<any, any, any>>

A record mapping action names to their definitions.

TActions

An object of named action definitions to compose.

ComposedActions<TActions>

A ComposedActions object with combined handler, loader, and client descriptor.

const composed = composeActions({
deletePost,
publishPost,
unpublishPost,
});
export const action = composed.action;
export const loader = composed.loader(async ({ request, params }) => {
return { post: await getPost(params.slug) };
});

createAction: <TInput, TResult>(operationsFn) => ActionDefinition<TInput, TResult, TUser>

Defines a single permission-aware action.

Takes an OperationsFn that builds a database Operation from the parsed input and action context. Returns an ActionDefinition with .action, .loader(), .client, and .buildOperation facets.

TInput

The expected input shape for this action.

TResult

The return type of the action handler.

OperationsFn<TInput, TResult, TUser>

A function that builds the database operation for this action.

ActionDefinition<TInput, TResult, TUser>

An ActionDefinition with action handler, loader wrapper, client descriptor, and build method.

const deletePost = createAction<{ postId: string }, Response>(
(db, input, ctx) =>
compose(
[db.delete(posts).where(eq(posts.id, input.postId))],
async (runDelete) => {
await runDelete({});
return redirect("/");
},
),
);
export const action = deletePost.action;
import { createActions } from "@cfast/actions";
export const { createAction, composeActions } = createActions({
getContext: async ({ request }) => {
const ctx = await requireAuthContext(request);
const db = createCfDb(env.DB, ctx);
return { db, user: ctx.user, grants: ctx.grants };
},
});