UNPKG

next-safe-action

Version:

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

220 lines (218 loc) 6.67 kB
import { C as SafeActionClient, D as ValidationErrors, E as IssueWithUnionErrors, M as StandardSchemaV1, S as StateServerCodeFn, T as HandleValidationErrorsShapeFn, _ as SafeActionResult, a as InferMetadata, b as ServerCodeFn, c as InferSafeActionFnResult, d as InputSchemaFactoryFn, f as MiddlewareFn, g as SafeActionFn, h as SafeActionClientArgs, i as InferCtx, j as InferOutputOrDefault, l as InferServerError, m as NavigationKind, n as DVES, o as InferMiddlewareFnNextCtx, p as MiddlewareResult, r as HandleServerErrorFn, s as InferSafeActionFnInput, t as CreateClientOpts, u as InputSchemaClientOpts, v as SafeActionUtils, w as FlattenedValidationErrors, x as ServerErrorFunctionUtils, y as SafeStateActionFn, } from "./index.types-CqcWrJ4z.mjs"; //#region src/middleware.d.ts /** * 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 >; }; //#endregion //#region src/utils.d.ts declare const DEFAULT_SERVER_ERROR_MESSAGE = "Something went wrong while executing the operation."; //#endregion //#region src/validation-errors.d.ts declare class ActionValidationError<CVE> extends Error { validationErrors: CVE; constructor(validationErrors: CVE, overriddenErrorMessage?: string); } declare class ActionBindArgsValidationError extends Error { validationErrors: unknown[]; constructor(validationErrors: unknown[]); } /** * 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>); } //#endregion //#region src/index.d.ts /** * 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 >; //#endregion export { ActionBindArgsValidationError, ActionMetadataValidationError, ActionOutputDataValidationError, ActionValidationError, CreateClientOpts, DEFAULT_SERVER_ERROR_MESSAGE, DVES, FlattenedValidationErrors, HandleServerErrorFn, HandleValidationErrorsShapeFn, InferCtx, InferMetadata, InferMiddlewareFnNextCtx, InferSafeActionFnInput, InferSafeActionFnResult, InferServerError, InputSchemaClientOpts, InputSchemaFactoryFn, IssueWithUnionErrors, MiddlewareFn, MiddlewareResult, NavigationKind, SafeActionClient, SafeActionClientArgs, SafeActionFn, SafeActionResult, SafeActionUtils, SafeStateActionFn, ServerCodeFn, ServerErrorFunctionUtils, StateServerCodeFn, ValidationErrors, createMiddleware, createSafeActionClient, flattenValidationErrors, formatValidationErrors, returnValidationErrors, }; //# sourceMappingURL=index.d.mts.map