UNPKG

@kellanjs/actioncraft

Version:

Fluent, type-safe builder for Next.js server actions.

60 lines (59 loc) 3.03 kB
import type { Handler, CraftedAction, InferDataFromHandler, HandlerParams } from "../types/actions.js"; import type { Config, Schemas, Errors, Callbacks } from "../types/builder.js"; export declare class ActionBuilder<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TCallbacks extends Callbacks<TConfig, TSchemas, TErrors, TData>, TData> { private readonly _config; private readonly _schemas; private readonly _errors; private readonly _callbacks; private readonly _handler?; constructor(config: TConfig, schemas: TSchemas, errors: TErrors, callbacks: TCallbacks, handler?: Handler<TConfig, TSchemas, TErrors, TData>); /** * Defines configuration options for the action. * Resets previously defined handler and callbacks. */ config<TNewConfig extends Config>(config: TNewConfig): ActionBuilder<TNewConfig, TSchemas, TErrors, Record<string, never>, unknown>; /** * Defines validation schemas for input, output, and bind arguments. * Resets previously defined handler and callbacks. */ schemas<TNewSchemas extends Schemas>(schemas: TNewSchemas): ActionBuilder<TConfig, TNewSchemas, TErrors, Record<string, never>, unknown>; /** * Defines error functions for returning typed errors from the handler. * Resets previously defined handler and callbacks. */ errors<const TNewErrors extends Errors>(errors: TNewErrors): ActionBuilder<TConfig, TSchemas, TNewErrors, Record<string, never>, unknown>; /** * Defines the handler function containing the server action's business logic. * Resets previously defined callbacks. */ handler<TFn extends (params: HandlerParams<TConfig, TSchemas, TErrors, TData>) => Promise<unknown>>(fn: TFn): ActionBuilder<TConfig, TSchemas, TErrors, Record<string, never>, InferDataFromHandler<TFn>>; /** * Defines lifecycle callbacks to be triggered during the exection of an action. * Must be called after handler() for correct type inference. */ callbacks<TNewCallbacks extends Callbacks<TConfig, TSchemas, TErrors, TData>>(callbacks: TNewCallbacks): ActionBuilder<TConfig, TSchemas, TErrors, TNewCallbacks, TData>; /** * Builds and returns the final executable server action. * This is the terminal method for the ActionBuilder fluent API. */ craft(): CraftedAction<TConfig, TSchemas, TErrors, TData>; } /** * One of two entry points to the Actioncraft system. * Creates a new ActionBuilder instance for the fluent API that ends with craft(). * This provides an alternative syntax for building your server actions. * * Example Usage: * ```ts * const myAction = action() * .config(...) * .schemas(...) * .errors(...) * .handler(...) * .callbacks(...) * .craft(); * ``` * * @returns A new ActionBuilder instance to start building your action. */ export declare function action(): ActionBuilder<Record<string, never>, Record<string, never>, Record<string, never>, Record<string, never>, unknown>;