Skip to content

Commit

Permalink
chore(website): update docs with async functions requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Dec 8, 2024
1 parent a3ca439 commit 416ed0d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
12 changes: 7 additions & 5 deletions website/docs/define-actions/action-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Starting from v7.4.0, you can now pass optional `throwServerError` and `throwVal
- `onError`: called when action execution fails (validation errors or server error).
- `onSettled`: called when action execution succeeds or fails.

With action callbacks you can perform custom logic after the action is executed, on the server side. You can pass them to [`action`/`stateAction`](/docs/define-actions/instance-methods#action--stateaction) method in the second argument, after the server code function. They don't return anything and can be async or not.
With action callbacks you can perform custom logic after the action is executed, on the server side. You can pass them to [`action`/`stateAction`](/docs/define-actions/instance-methods#action--stateaction) method as the second argument, after the server code function. Their return value is not used and they **must** be async functions.

```tsx
import { actionClient } from "@/lib/safe-action";
Expand All @@ -29,7 +29,7 @@ const action = actionClient
.action(async () => {
// ...
}, {
onSuccess: ({
onSuccess: async ({
data,
ctx,
metadata,
Expand All @@ -39,22 +39,24 @@ const action = actionClient
bindArgsParsedInputs,
hasRedirected,
hasNotFound,
hasForbidden
}) => {},
onError: ({
onError: async ({
error,
ctx,
metadata,
clientInput,
bindArgsClientInputs
}) => {},
onSettled: ({
onSettled: async ({
result,
ctx,
metadata,
clientInput,
bindArgsClientInputs,
hasRedirected,
hasNotFound
hasNotFound,
hasForbidden
}) => {},
});
```
4 changes: 2 additions & 2 deletions website/docs/define-actions/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ metadata(data: Metadata) => new SafeActionClient()
schema(inputSchema: S, utils?: { handleValidationErrorsShape?: HandleValidationErrorsShapeFn } }) => new SafeActionClient()
```

`schema` accepts an input schema of type `Schema` or a function that returns a promise of type `Schema` and an optional `utils` object that accepts a [`handleValidationErrorsShape`](/docs/define-actions/validation-errors#customize-validation-errors-format) function. The schema is used to define the arguments that the safe action will receive, the optional [`handleValidationErrorsShape`](/docs/define-actions/validation-errors#customize-validation-errors-format) function is used to return a custom format for validation errors. If you don't pass an input schema, `parsedInput` and validation errors will be typed `undefined`, and `clientInput` will be typed `void`. It returns a new instance of the safe action client.
`schema` accepts an input schema of type `Schema` or a function that returns a promise of type `Schema` and an optional `utils` object that accepts an async [`handleValidationErrorsShape`](/docs/define-actions/validation-errors#customize-validation-errors-format) function. The schema is used to define the arguments that the safe action will receive, the optional [`handleValidationErrorsShape`](/docs/define-actions/validation-errors#customize-validation-errors-format) function is used to return a custom format for validation errors. If you don't pass an input schema, `parsedInput` and validation errors will be typed `undefined`, and `clientInput` will be typed `void`. It returns a new instance of the safe action client.

### `bindArgsSchemas`

```typescript
bindArgsSchemas(bindArgsSchemas: BAS, bindArgsUtils?: { handleBindArgsValidationErrorsShape?: HandleBindArgsValidationErrorsShapeFn }) => new SafeActionClient()
```

`bindArgsSchemas` accepts an array of bind input schemas of type `Schema[]` and an optional `bindArgsUtils` object that accepts a `handleBindArgsValidationErrorsShape` function. The schema is used to define the bind arguments that the safe action will receive, the optional `handleBindArgsValidationErrorsShape` function is used to [return a custom format for bind arguments validation errors](/docs/define-actions/validation-errors#customize-validation-errors-format). It returns a new instance of the safe action client.
`bindArgsSchemas` accepts an array of bind input schemas of type `Schema[]` and an optional `bindArgsUtils` object that accepts an async [`handleBindArgsValidationErrorsShape`](/docs/define-actions/validation-errors#customize-validation-errors-format) function. The schema is used to define the bind arguments that the safe action will receive, the optional [`handleBindArgsValidationErrorsShape`](/docs/define-actions/validation-errors#customize-validation-errors-format) function is used to [return a custom format for bind arguments validation errors](/docs/define-actions/validation-errors#customize-validation-errors-format). It returns a new instance of the safe action client.

### `outputSchema`

Expand Down
6 changes: 3 additions & 3 deletions website/docs/define-actions/validation-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ next-safe-action, by default, emulates Zod's [`format`](https://zod.dev/ERROR_HA

This can be customized both at the safe action client level and at the action level by:
- using [`defaultValidationErrorsShape`](/docs/define-actions/create-the-client#defaultvalidationerrorsshape) optional property in `createSafeActionClient`;
- using `handleValidationErrorsShape` and `handleBindArgsValidationErrorsShape` optional functions in [`schema`](/docs/define-actions/instance-methods#schema) and [`bindArgsSchemas`](/docs/define-actions/instance-methods#bindargsschemas) methods.
- using `handleValidationErrorsShape` and `handleBindArgsValidationErrorsShape` optional async functions in [`schema`](/docs/define-actions/instance-methods#schema) and [`bindArgsSchemas`](/docs/define-actions/instance-methods#bindargsschemas) methods.

The second way overrides the shape set at the instance level, per action.

Expand Down Expand Up @@ -39,13 +39,13 @@ export const loginUser = actionClient
// Here we use the `flattenValidationErrors` function to customize the returned validation errors
// object to the client.
// highlight-next-line
handleValidationErrorsShape: (ve, utils) => flattenValidationErrors(ve).fieldErrors,
handleValidationErrorsShape: async (ve, utils) => flattenValidationErrors(ve).fieldErrors,
})
.bindArgsSchemas(bindArgsSchemas, {
// Here we use the `flattenBindArgsValidatonErrors` function to customize the returned bind args
// validation errors object array to the client.
// highlight-next-line
handleBindArgsValidationErrorsShape: (ve, utils) => flattenBindArgsValidationErrors(ve),
handleBindArgsValidationErrorsShape: async (ve, utils) => flattenBindArgsValidationErrors(ve),
})
.action(async ({ parsedInput: { username, password } }) => {
// Your code here...
Expand Down
4 changes: 4 additions & 0 deletions website/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import TabItem from '@theme/TabItem';
- A supported validation library: Zod, Valibot, Yup, TypeBox
:::

:::warning
Next.js >= 15.0.5 is required for using next-safe-action >= 7.9.10. This is due to internal error handling framework changes. So, please upgrade to the latest version to use this library with Next.js 15.0.5 or later.
:::

**next-safe-action** provides a typesafe Server Actions implementation for Next.js App Router applications.

## Installation
Expand Down

0 comments on commit 416ed0d

Please sign in to comment.