UNPKG

next-safe-action

Version:

Type safe and validated Server Actions in your Next.js project.

188 lines (182 loc) 5.82 kB
import { M as MiddlewareFn, S as StandardSchemaV1, V as ValidationErrors, D as DVES, C as CreateClientOpts, a as SafeActionClient, b as InferOutputOrDefault, } from "./index.types-CJ06jFQp.mjs"; export { F as FlattenedValidationErrors, H as HandleServerErrorFn, s as HandleValidationErrorsShapeFn, o as InferCtx, p as InferMetadata, n as InferMiddlewareFnNextCtx, l as InferSafeActionFnInput, m as InferSafeActionFnResult, q as InferServerError, r as IssueWithUnionErrors, h as MiddlewareResult, N as NavigationKind, d as SafeActionClientArgs, f as SafeActionFn, e as SafeActionResult, k as SafeActionUtils, g as SafeStateActionFn, i as ServerCodeFn, c as ServerErrorFunctionUtils, j as StateServerCodeFn, } from "./index.types-CJ06jFQp.mjs"; /** * Creates a standalone middleware function. It accepts a generic object with optional `serverError`, `ctx` and `metadata` * properties, if you need one or all of them to be typed. The type for each property that is passed as generic is the * **minimum** shape required to define the middleware function, but it can also be larger than that. * * {@link https://next-safe-action.dev/docs/define-actions/middleware#create-standalone-middleware See docs for more information} */ declare const createMiddleware: < BaseData extends { serverError?: any; ctx?: object; metadata?: any; }, >() => { define: <NextCtx extends object>( middlewareFn: MiddlewareFn< BaseData extends { serverError: infer SE; } ? SE : any, BaseData extends { metadata: infer MD; } ? MD : any, BaseData extends { ctx: infer Ctx extends object; } ? Ctx : object, NextCtx > ) => MiddlewareFn< BaseData extends { serverError: infer SE; } ? SE : any, BaseData extends { metadata: infer MD; } ? MD : any, BaseData extends { ctx: infer Ctx extends object; } ? Ctx : object, NextCtx >; }; declare const DEFAULT_SERVER_ERROR_MESSAGE = "Something went wrong while executing the operation."; declare class ActionValidationError<CVE> extends Error { validationErrors: CVE; constructor(validationErrors: CVE, overriddenErrorMessage?: string); } /** * Return custom validation errors to the client from the action's server code function. * Code declared after this function invocation will not be executed. * @param schema Input schema * @param validationErrors Validation errors object * * {@link https://next-safe-action.dev/docs/define-actions/validation-errors#returnvalidationerrors See docs for more information} */ declare function returnValidationErrors< S extends StandardSchemaV1 | (() => Promise<StandardSchemaV1>), AS extends StandardSchemaV1 = S extends () => Promise<StandardSchemaV1> ? Awaited<ReturnType<S>> : S, >(schema: S, validationErrors: ValidationErrors<AS>): never; /** * Default validation errors format. * Emulation of `zod`'s [`format`](https://zod.dev/ERROR_HANDLING?id=formatting-errors) function. */ declare function formatValidationErrors<VE extends ValidationErrors<any>>(validationErrors: VE): VE; /** * Transform default formatted validation errors into flattened structure. * `formErrors` contains global errors, and `fieldErrors` contains errors for each field, * one level deep. It discards errors for nested fields. * Emulation of `zod`'s [`flatten`](https://zod.dev/ERROR_HANDLING?id=flattening-errors) function. * @param {ValidationErrors} [validationErrors] Validation errors object * * {@link https://next-safe-action.dev/docs/define-actions/validation-errors#flattenvalidationerrorsutility-function See docs for more information} */ declare function flattenValidationErrors<VE extends ValidationErrors<any>>( validationErrors: VE ): { formErrors: string[]; fieldErrors: { [K in keyof Omit<VE, "_errors">]?: string[] | undefined }; }; /** * This error is thrown when an action metadata is invalid, i.e. when there's a mismatch between the * type of the metadata schema returned from `defineMetadataSchema` and the actual data passed. */ declare class ActionMetadataValidationError<MDS extends StandardSchemaV1 | undefined> extends Error { validationErrors: ValidationErrors<MDS>; constructor(validationErrors: ValidationErrors<MDS>); } /** * This error is thrown when an action's data (output) is invalid, i.e. when there's a mismatch between the * type of the data schema passed to `dataSchema` method and the actual return of the action. */ declare class ActionOutputDataValidationError<DS extends StandardSchemaV1 | undefined> extends Error { validationErrors: ValidationErrors<DS>; constructor(validationErrors: ValidationErrors<DS>); } /** * Create a new safe action client. * Note: this client only works with Zod as the validation library. * @param createOpts Initialization options * * {@link https://next-safe-action.dev/docs/define-actions/create-the-client#initialization-options See docs for more information} */ declare const createSafeActionClient: < ODVES extends DVES | undefined = undefined, ServerError = string, MetadataSchema extends StandardSchemaV1 | undefined = undefined, >( createOpts?: CreateClientOpts<ODVES, ServerError, MetadataSchema> ) => SafeActionClient< ServerError, ODVES, MetadataSchema, InferOutputOrDefault<MetadataSchema, undefined>, MetadataSchema extends undefined ? true : false, {}, undefined, undefined, undefined, readonly [], | { formErrors: string[]; fieldErrors: {}; } | undefined >; export { ActionMetadataValidationError, ActionOutputDataValidationError, ActionValidationError, CreateClientOpts, DEFAULT_SERVER_ERROR_MESSAGE, DVES, MiddlewareFn, SafeActionClient, ValidationErrors, createMiddleware, createSafeActionClient, flattenValidationErrors, formatValidationErrors, returnValidationErrors, };