next-action-forge
Version:
A simple, type-safe toolkit for Next.js server actions with Zod validation
37 lines (35 loc) • 1.16 kB
TypeScript
interface ServerActionError {
message: string;
code?: string;
field?: string;
fields?: Record<string, string[]>;
statusCode?: number;
shouldRedirect?: boolean;
redirectTo?: string;
}
type RedirectConfig = {
url: string;
replace?: boolean;
delay?: number;
};
type ServerActionResponse<T = void> = {
success: true;
data: T;
redirect?: string | RedirectConfig;
} | {
success: false;
error: ServerActionError;
};
type ServerAction<TInput, TOutput> = TInput extends void ? () => Promise<ServerActionResponse<TOutput>> : (input: TInput) => Promise<ServerActionResponse<TOutput>>;
interface ActionResult<TOutput> {
data?: TOutput;
serverError?: ServerActionError;
validationErrors?: Record<string, string[]>;
fetchError?: string;
}
type ExtractSuccessData<T> = T extends ServerActionResponse<infer U> ? U : never;
type IsErrorResponse<T> = T extends {
success: false;
error: ServerActionError;
} ? true : false;
export type { ActionResult as A, ExtractSuccessData as E, IsErrorResponse as I, RedirectConfig as R, ServerActionError as S, ServerActionResponse as a, ServerAction as b };