Skip to content

Commit

Permalink
chore(website): add action throwValidationErrors and `throwServerEr…
Browse files Browse the repository at this point in the history
…ror` props sections
  • Loading branch information
TheEdoRan committed Jul 22, 2024
1 parent a5e687e commit 73aa5da
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
---
sidebar_position: 3
description: Action callbacks are a way to perform custom logic after the action is executed, on the server.
description: Action utils is an object with useful properties and callbacks functions that you can use to customize the action execution flow.
---

# Action callbacks
# Action utils

With action callbacks you can perform custom logic after the action is executed, on the server side. You can provide them to [`action`/`stateAction`](/docs/safe-action-client/instance-methods#action--stateaction) method as the second argument, after the server code function:
Action utils is an object with some useful properties and callbacks passed as the second argument of the [`action`/`stateAction`](/docs/safe-action-client/instance-methods#action--stateaction) method.

## Throw errors when they occur

Starting from v7.4.0, you can now pass optional `throwServerError` and `throwValidationErrors` properties at the action level, if you want or need that behavior. Note that the `throwValidationErrors` property set at the action level has a higher priority than the one at the instance level, so if you set it to `false` while the one at the instance level is `true`, validation errors will **not** be thrown.


## Callbacks

With action callbacks you can perform custom logic after the action is executed, on the server side. You can provide them to [`action`/`stateAction`](/docs/safe-action-client/instance-methods#action--stateaction) method in the second argument, after the server code function:

```tsx
import { actionClient } from "@/lib/safe-action";
Expand All @@ -27,8 +36,22 @@ const action = actionClient
hasRedirected,
hasNotFound,
}) => {},
onError: ({ error, ctx, metadata, clientInput, bindArgsClientInputs }) => {},
onSettled: ({ result, ctx, metadata, clientInput, bindArgsClientInputs }) => {},
onError: ({
error,
ctx,
metadata,
clientInput,
bindArgsClientInputs
}) => {},
onSettled: ({
result,
ctx,
metadata,
clientInput,
bindArgsClientInputs,
hasRedirected,
hasNotFound
}) => {},
});
```

Expand Down
2 changes: 1 addition & 1 deletion website/docs/migrations/v6-to-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ When working with i18n solutions, often you'll find implementations that require

### [Support action execution callbacks](https://github.com/TheEdoRan/next-safe-action/issues/162)

It's sometimes useful to be able to execute custom logic on the server side after an action succeeds or fails. Starting from version 7, next-safe-action allows you to pass action callbacks when defining an action. More information about this feature can be found [here](/docs/execution/action-callbacks).
It's sometimes useful to be able to execute custom logic on the server side after an action succeeds or fails. Starting from version 7, next-safe-action allows you to pass action callbacks when defining an action. More information about this feature can be found [here](/docs/execution/action-utils#callbacks).

### [Support stateful actions using React `useActionState` hook](https://github.com/TheEdoRan/next-safe-action/issues/91)

Expand Down
2 changes: 1 addition & 1 deletion website/docs/safe-action-client/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ action(serverCodeFn: ServerCodeFn, utils?: SafeActionUtils) => SafeActionFn
stateAction(serverCodeFn: StateServerCodeFn, utils?: SafeActionUtils) => SafeStateActionFn
```

`action`/`stateAction` is the final method in the list. It accepts a [`serverCodeFn`](#servercodefn) of type [`ServerCodeFn`](/docs/types#servercodefn)/[`StateServerCodeFn`](/docs/types#stateservercodefn) and an object with optional [action callbacks](/docs/execution/action-callbacks), and it returns a new safe action function of type [`SafeActionFn`](/docs/types#safeactionfn)/[`SafeStateActionFn`](/docs/types#safestateactionfn), which can be called from your components. When an action doesn't need input arguments, you can directly use this method without passing a schema to [`schema`](#schema) method.
`action`/`stateAction` is the final method in the list. It accepts a [`serverCodeFn`](#servercodefn) of type [`ServerCodeFn`](/docs/types#servercodefn)/[`StateServerCodeFn`](/docs/types#stateservercodefn) and an optional object with [action utils](/docs/execution/action-utils), and it returns a new safe action function of type [`SafeActionFn`](/docs/types#safeactionfn)/[`SafeStateActionFn`](/docs/types#safestateactionfn), which can be called from your components. When an action doesn't need input arguments, you can directly use this method without passing a schema to [`schema`](#schema) method.

When the action is executed, all middleware functions in the chain will be called at runtime, in the order they were defined.

Expand Down
2 changes: 2 additions & 0 deletions website/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ export type SafeActionUtils<
CBAVE,
Data,
> = {
throwServerError?: boolean;
throwValidationErrors?: boolean;
onSuccess?: (args: {
data?: Data;
clientInput: S extends Schema ? InferIn<S> : undefined;
Expand Down

0 comments on commit 73aa5da

Please sign in to comment.