@kellanjs/actioncraft
Version:
Fluent, type-safe builder for Next.js server actions.
95 lines • 3.64 kB
JavaScript
import { CraftBuilder } from "./craft-builder.js";
import { Executor } from "./executor/executor.js";
// ============================================================================
// ACTION BUILDER CLASS - Alternative fluent API syntax ending with craft()
// ============================================================================
export class ActionBuilder {
_config;
_schemas;
_errors;
_callbacks;
_handler;
constructor(config, schemas, errors, callbacks, handler) {
this._config = config;
this._schemas = schemas;
this._errors = errors;
this._callbacks = callbacks;
this._handler = handler;
}
// --------------------------------------------------------------------------
// FLUENT API METHODS (same as CraftBuilder)
// --------------------------------------------------------------------------
/**
* Defines configuration options for the action.
* Resets previously defined handler and callbacks.
*/
config(config) {
return new ActionBuilder(config, this._schemas, this._errors, {}, undefined);
}
/**
* Defines validation schemas for input, output, and bind arguments.
* Resets previously defined handler and callbacks.
*/
schemas(schemas) {
return new ActionBuilder(this._config, schemas, this._errors, {}, undefined);
}
/**
* Defines error functions for returning typed errors from the handler.
* Resets previously defined handler and callbacks.
*/
errors(errors) {
return new ActionBuilder(this._config, this._schemas, errors, {}, undefined);
}
/**
* Defines the handler function containing the server action's business logic.
* Resets previously defined callbacks.
*/
handler(fn) {
return new ActionBuilder(this._config, this._schemas, this._errors, {}, fn);
}
/**
* Defines lifecycle callbacks to be triggered during the exection of an action.
* Must be called after handler() for correct type inference.
*/
callbacks(callbacks) {
return new ActionBuilder(this._config, this._schemas, this._errors, callbacks, this._handler);
}
// --------------------------------------------------------------------------
// CRAFT METHOD - Final step to create the action
// --------------------------------------------------------------------------
/**
* Builds and returns the final executable server action.
* This is the terminal method for the ActionBuilder fluent API.
*/
craft() {
// Convert ActionBuilder to CraftBuilder and use existing Executor logic
const builder = new CraftBuilder(this._config, this._schemas, this._errors, this._callbacks, this._handler);
const executor = new Executor(builder);
return executor.craft();
}
}
// ============================================================================
// PUBLIC API FUNCTION
// ============================================================================
/**
* 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 function action() {
return new ActionBuilder({}, {}, {}, {}, undefined);
}
//# sourceMappingURL=action-builder.js.map