UNPKG

@kellanjs/actioncraft

Version:

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

67 lines (66 loc) 3.93 kB
import type { Handler, InferDataFromHandler, HandlerParams, CraftedAction } from "../types/actions.js"; import type { CrafterConfig, CrafterSchemas, CrafterErrors, CrafterCallbacks } from "../types/crafter.js"; import { INTERNAL, type CrafterInternals } from "./internal.js"; export declare class Crafter<TConfig extends CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<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 CrafterConfig>(config: TNewConfig): Crafter<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 CrafterSchemas>(schemas: TNewSchemas): Crafter<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 CrafterErrors>(errors: TNewErrors): Crafter<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): Crafter<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 CrafterCallbacks<TConfig, TSchemas, TErrors, TData>>(callbacks: TNewCallbacks): Crafter<TConfig, TSchemas, TErrors, TNewCallbacks, TData>; /** * @returns Internal properties of the Crafter 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 CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>, TData> = (crafter: Crafter<CrafterConfig, Record<string, never>, Record<string, never>, Record<string, never>, unknown>) => Crafter<TConfig, TSchemas, TErrors, TCallbacks, TData> | Promise<Crafter<TConfig, TSchemas, TErrors, TCallbacks, TData>>; /** * One of two entry points to the Actioncraft system. * It provides you with an empty Crafter instance on which you can call any of the fluent * Crafter 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 CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>, TData>(craftFn: CraftFn<TConfig, TSchemas, TErrors, TCallbacks, TData>): CraftedAction<TConfig, TSchemas, TErrors, TData>; export {};