UNPKG

next-action-forge

Version:

A simple, type-safe toolkit for Next.js server actions with Zod validation

78 lines (73 loc) 4.38 kB
import { S as ServerActionError, a as ServerActionResponse, R as RedirectConfig, b as ServerAction } from '../index-CeOJE6T8.mjs'; import { z } from 'zod'; declare function handleServerActionError(error: unknown, customHandler?: (error: unknown) => ServerActionError | undefined): { success: false; error: ServerActionError; }; declare function isErrorResponse<T>(response: ServerActionResponse<T>): response is { success: false; error: ServerActionError; }; type InferZodOutput<T> = T extends z.ZodTypeAny ? z.output<T> : never; type InferZodInput<T> = T extends z.ZodTypeAny ? z.input<T> : never; type ServerActionMiddleware<Context, Input = unknown> = (params: { context: Context; input: Input; }) => Promise<{ context: Context; } | { error: ServerActionError; }>; type SafeActionClientArgs<Context extends object, InputSchema extends z.ZodTypeAny | undefined, OutputSchema extends z.ZodTypeAny | undefined> = { middlewareFns: ServerActionMiddleware<any, any>[]; inputSchema?: InputSchema; outputSchema?: OutputSchema; onError?: (error: unknown, context: { parsedInput?: unknown; }) => ServerActionError | undefined; ctxType?: Context; redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined); }; declare class ServerActionClient<Context extends object = {}, InputSchema extends z.ZodTypeAny | undefined = undefined, OutputSchema extends z.ZodTypeAny | undefined = undefined> { #private; constructor(args?: SafeActionClientArgs<Context, InputSchema, OutputSchema>); /** * Use a middleware function. * @param middlewareFn Middleware function */ use<NextContext extends object>(middlewareFn: ServerActionMiddleware<Context & NextContext, InputSchema extends z.ZodTypeAny ? z.output<InputSchema> : undefined>): ServerActionClient<Context & NextContext, InputSchema, OutputSchema>; /** * Define the input validation schema for the action. * @param schema Input validation schema */ inputSchema<IS extends z.ZodTypeAny>(schema: IS): ServerActionClient<Context, IS, OutputSchema>; /** * Define the output validation schema for the action. * @param schema Output validation schema */ outputSchema<OS extends z.ZodTypeAny>(schema: OS): ServerActionClient<Context, InputSchema, OS>; /** * Define a custom error handler for the action. * @param handler Error handler function */ onError(handler: (error: unknown, context: { parsedInput?: unknown; }) => ServerActionError | undefined): ServerActionClient<Context, InputSchema, OutputSchema>; /** * Define a redirect configuration for successful actions. * @param config Redirect URL, config object, or function that returns redirect config based on result */ redirect<Data = any>(config: string | RedirectConfig | ((result: Data) => string | RedirectConfig | undefined)): ServerActionClient<Context, InputSchema, OutputSchema>; /** * Define the action. * @param serverCodeFn Code that will be executed on the server side */ action<Data>(serverCodeFn: InputSchema extends undefined ? (context: Context) => Promise<Data> : InputSchema extends z.ZodTypeAny ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data> : (context: Context) => Promise<Data>): ServerAction<InputSchema extends undefined ? void : InputSchema extends z.ZodTypeAny ? InferZodInput<InputSchema> : void, OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>; /** * Define a form action that accepts FormData. * @param serverCodeFn Code that will be executed on the server side */ formAction<Data>(serverCodeFn: InputSchema extends undefined ? (context: Context) => Promise<Data> : InputSchema extends z.ZodTypeAny ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data> : (context: Context) => Promise<Data>): (prev: ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>, formData: FormData) => Promise<ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>>; } declare function createActionClient(): ServerActionClient<{}, undefined, undefined>; export { ServerActionClient, createActionClient, handleServerActionError, isErrorResponse };