UNPKG

@kellanjs/actioncraft

Version:

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

67 lines (66 loc) 3.84 kB
import type { Handler, InferDataFromHandler, HandlerParams, CraftedAction } from "../types/actions.js"; import type { Config, Schemas, Errors, Callbacks } from "../types/builder.js"; import { INTERNAL, type CrafterInternals } from "./internal.js"; export declare class CraftBuilder<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): CraftBuilder<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): CraftBuilder<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): CraftBuilder<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): CraftBuilder<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): CraftBuilder<TConfig, TSchemas, TErrors, TNewCallbacks, TData>; /** * @returns Internal properties of the CraftBuilder instance */ [INTERNAL](): CrafterInternals<TConfig, TSchemas, TErrors, TCallbacks, TData>; } /** * Represents the function that the user passes to `craft()` in order to build an action. */ type CraftFn<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TCallbacks extends Callbacks<TConfig, TSchemas, TErrors, TData>, TData> = (builder: CraftBuilder<Config, Record<string, never>, Record<string, never>, Record<string, never>, unknown>) => CraftBuilder<TConfig, TSchemas, TErrors, TCallbacks, TData> | Promise<CraftBuilder<TConfig, TSchemas, TErrors, TCallbacks, TData>>; /** * One of two entry points to the Actioncraft system. * It provides you with an empty CraftBuilder instance on which you can call any of the fluent * CraftBuilder methods to configure and define your action. * * Example Usage: * ```ts * const myAction = craft(async (action) => { * return action * .config(...) * .schemas(...) * .errors(...) * .handler(...) * .callbacks(...) * }); * ``` * * @param craftFn - The function that the user passes to `craft()` in order to build an action. * @returns The fully-typed server action function that can be used in your app. */ export declare function craft<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TCallbacks extends Callbacks<TConfig, TSchemas, TErrors, TData>, TData>(craftFn: CraftFn<TConfig, TSchemas, TErrors, TCallbacks, TData>): CraftedAction<TConfig, TSchemas, TErrors, TData>; export {};