From 8195fb75ef1507c5102fb972c105c3fa90714da1 Mon Sep 17 00:00:00 2001 From: Edoardo Ranghieri Date: Mon, 22 Jul 2024 00:20:55 +0200 Subject: [PATCH] refactor: rename `SafeActionCallbacks` type to `SafeActionUtils` --- .../next-safe-action/src/action-builder.ts | 18 +++++++++--------- packages/next-safe-action/src/index.types.ts | 4 ++-- .../next-safe-action/src/safe-action-client.ts | 10 +++++----- website/docs/types.md | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/next-safe-action/src/action-builder.ts b/packages/next-safe-action/src/action-builder.ts index 85ef67f0..17860253 100644 --- a/packages/next-safe-action/src/action-builder.ts +++ b/packages/next-safe-action/src/action-builder.ts @@ -5,10 +5,10 @@ import type { Infer, InferArray, InferIn, InferInArray, Schema, ValidationAdapte import type { MiddlewareFn, MiddlewareResult, - SafeActionCallbacks, SafeActionClientOpts, SafeActionFn, SafeActionResult, + SafeActionUtils, SafeStateActionFn, ServerCodeFn, StateServerCodeFn, @@ -53,13 +53,13 @@ export function actionBuilder< function buildAction({ withState }: { withState: false }): { action: ( serverCodeFn: ServerCodeFn, - cb?: SafeActionCallbacks + utils?: SafeActionUtils ) => SafeActionFn; }; function buildAction({ withState }: { withState: true }): { action: ( serverCodeFn: StateServerCodeFn, - cb?: SafeActionCallbacks + utils?: SafeActionUtils ) => SafeStateActionFn; }; function buildAction({ withState }: { withState: boolean }) { @@ -68,7 +68,7 @@ export function actionBuilder< serverCodeFn: | ServerCodeFn | StateServerCodeFn, - cb?: SafeActionCallbacks + utils?: SafeActionUtils ) => { return async (...clientInputs: unknown[]) => { let prevCtx: unknown = undefined; @@ -260,7 +260,7 @@ export function actionBuilder< // If an internal framework error occurred, throw it, so it will be processed by Next.js. if (frameworkError) { await Promise.resolve( - cb?.onSuccess?.({ + utils?.onSuccess?.({ data: undefined, metadata: args.metadata, ctx: prevCtx as Ctx, @@ -274,7 +274,7 @@ export function actionBuilder< ); await Promise.resolve( - cb?.onSettled?.({ + utils?.onSettled?.({ metadata: args.metadata, ctx: prevCtx as Ctx, clientInput: clientInputs.at(-1) as S extends Schema ? InferIn : undefined, @@ -311,7 +311,7 @@ export function actionBuilder< } await Promise.resolve( - cb?.onSuccess?.({ + utils?.onSuccess?.({ metadata: args.metadata, ctx: prevCtx as Ctx, data: actionResult.data as Data, @@ -325,7 +325,7 @@ export function actionBuilder< ); } else { await Promise.resolve( - cb?.onError?.({ + utils?.onError?.({ metadata: args.metadata, ctx: prevCtx as Ctx, clientInput: clientInputs.at(-1) as S extends Schema ? InferIn : undefined, @@ -337,7 +337,7 @@ export function actionBuilder< // onSettled, if provided, is always executed. await Promise.resolve( - cb?.onSettled?.({ + utils?.onSettled?.({ metadata: args.metadata, ctx: prevCtx as Ctx, clientInput: clientInputs.at(-1) as S extends Schema ? InferIn : undefined, diff --git a/packages/next-safe-action/src/index.types.ts b/packages/next-safe-action/src/index.types.ts index 1cb4a4a3..59bb0e22 100644 --- a/packages/next-safe-action/src/index.types.ts +++ b/packages/next-safe-action/src/index.types.ts @@ -145,9 +145,9 @@ export type StateServerCodeFn< ) => Promise; /** - * Type of action execution callbacks. These are called after the action is executed, on the server side. + * Type of action execution utils. It includes action callbacks and other utils. */ -export type SafeActionCallbacks< +export type SafeActionUtils< ServerError, MD, Ctx, diff --git a/packages/next-safe-action/src/safe-action-client.ts b/packages/next-safe-action/src/safe-action-client.ts index f0938437..ca918927 100644 --- a/packages/next-safe-action/src/safe-action-client.ts +++ b/packages/next-safe-action/src/safe-action-client.ts @@ -4,8 +4,8 @@ import type { Infer, Schema, ValidationAdapter } from "./adapters/types"; import type { DVES, MiddlewareFn, - SafeActionCallbacks, SafeActionClientOpts, + SafeActionUtils, ServerCodeFn, StateServerCodeFn, } from "./index.types"; @@ -213,7 +213,7 @@ export class SafeActionClient< */ action( serverCodeFn: ServerCodeFn, - cb?: SafeActionCallbacks + utils?: SafeActionUtils ) { return actionBuilder({ handleReturnedServerError: this.#handleReturnedServerError, @@ -228,7 +228,7 @@ export class SafeActionClient< handleValidationErrorsShape: this.#handleValidationErrorsShape, handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape, throwValidationErrors: this.#throwValidationErrors, - }).action(serverCodeFn, cb); + }).action(serverCodeFn, utils); } /** @@ -241,7 +241,7 @@ export class SafeActionClient< */ stateAction( serverCodeFn: StateServerCodeFn, - cb?: SafeActionCallbacks + utils?: SafeActionUtils ) { return actionBuilder({ handleReturnedServerError: this.#handleReturnedServerError, @@ -256,6 +256,6 @@ export class SafeActionClient< handleValidationErrorsShape: this.#handleValidationErrorsShape, handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape, throwValidationErrors: this.#throwValidationErrors, - }).stateAction(serverCodeFn, cb); + }).stateAction(serverCodeFn, utils); } } diff --git a/website/docs/types.md b/website/docs/types.md index 7be406db..d0b77f35 100644 --- a/website/docs/types.md +++ b/website/docs/types.md @@ -191,9 +191,9 @@ export type StateServerCodeFn< ) => Promise; ``` -### `SafeActionCallbacks` +### `SafeActionUtils` -Type of action execution callbacks. These are called after the action is executed, on the server side. +Type of action execution utils. It includes action callbacks and other utils. ```typescript export type SafeActionCallbacks<