From 52bee97694932da92ab523c109a0781037e4ff9d Mon Sep 17 00:00:00 2001 From: Edoardo Ranghieri Date: Tue, 20 Aug 2024 19:19:10 +0200 Subject: [PATCH] refactor: type return of `createSafeActionClient` --- packages/next-safe-action/src/index.ts | 30 ++++++++++++++----- .../src/safe-action-client.ts | 4 +-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/next-safe-action/src/index.ts b/packages/next-safe-action/src/index.ts index b40e2417..b98c8a93 100644 --- a/packages/next-safe-action/src/index.ts +++ b/packages/next-safe-action/src/index.ts @@ -9,6 +9,7 @@ import { formatBindArgsValidationErrors, formatValidationErrors, } from "./validation-errors"; +import type { HandleBindArgsValidationErrorsShapeFn, HandleValidationErrorsShapeFn } from "./validation-errors.types"; export { createMiddleware } from "./middleware"; export { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE } from "./utils"; @@ -32,12 +33,25 @@ export type * from "./validation-errors.types"; * {@link https://next-safe-action.dev/docs/safe-action-client/initialization-options See docs for more information} */ export const createSafeActionClient = < - ODVES extends DVES | undefined = undefined, ServerError = string, MetadataSchema extends Schema | undefined = undefined, + ODVES extends DVES | undefined = undefined, + CVE = undefined, + CBAVE = undefined, >( createOpts?: SafeActionClientOpts -) => { +): SafeActionClient< + ServerError, + ODVES, + MetadataSchema, + MetadataSchema extends Schema ? Infer : undefined, + {}, + undefined, + undefined, + readonly [], + CVE, + CBAVE +> => { // If server log function is not provided, default to `console.error` for logging // server error messages. const handleServerErrorLog = @@ -67,11 +81,11 @@ export const createSafeActionClient = < metadata: undefined as MetadataSchema extends Schema ? Infer : undefined, defaultValidationErrorsShape: (createOpts?.defaultValidationErrorsShape ?? "formatted") as ODVES, throwValidationErrors: Boolean(createOpts?.throwValidationErrors), - handleValidationErrorsShape: - createOpts?.defaultValidationErrorsShape === "flattened" ? flattenValidationErrors : formatValidationErrors, - handleBindArgsValidationErrorsShape: - createOpts?.defaultValidationErrorsShape === "flattened" - ? flattenBindArgsValidationErrors - : formatBindArgsValidationErrors, + handleValidationErrorsShape: (createOpts?.defaultValidationErrorsShape === "flattened" + ? flattenValidationErrors + : formatValidationErrors) as unknown as HandleValidationErrorsShapeFn, + handleBindArgsValidationErrorsShape: (createOpts?.defaultValidationErrorsShape === "flattened" + ? flattenBindArgsValidationErrors + : formatBindArgsValidationErrors) as HandleBindArgsValidationErrorsShapeFn, }); }; diff --git a/packages/next-safe-action/src/safe-action-client.ts b/packages/next-safe-action/src/safe-action-client.ts index 22f0fc5c..3d7e9f83 100644 --- a/packages/next-safe-action/src/safe-action-client.ts +++ b/packages/next-safe-action/src/safe-action-client.ts @@ -22,7 +22,7 @@ export class SafeActionClient< ServerError, ODVES extends DVES | undefined, // override default validation errors shape MetadataSchema extends Schema | undefined = undefined, - MD = MetadataSchema extends Schema ? Infer : undefined, + MD = MetadataSchema extends Schema ? Infer : undefined, Ctx extends object = {}, SF extends (() => Promise) | undefined = undefined, // schema function S extends Schema | undefined = SF extends Function ? Awaited> : undefined, @@ -233,7 +233,7 @@ export class SafeActionClient< } /** - * Define the stateful action (without input validation schema, bind arguments validation schemas or metadata). + * Define the stateful action. * To be used with the [`useStateAction`](https://next-safe-action.dev/docs/execution/hooks/usestateaction) hook. * @param serverCodeFn Code that will be executed on the **server side** * @param [cb] Optional callbacks that will be called after action execution, on the server.