Skip to content

Commit 60fba56

Browse files
committed
chore(website): update docs
1 parent bfda754 commit 60fba56

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

website/docs/define-actions/action-utils.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Action utils is an object with useful properties and callbacks func
55

66
# Action utils
77

8-
Action utils is an object with some useful properties and callbacks passed as the second argument of the [`action`/`stateAction`](/docs/define-actions/instance-methods#action--stateaction) method.
8+
Action utils is an object with some useful properties and callbacks passed as the second argument of the [`action()`/`stateAction()`](/docs/define-actions/instance-methods#action--stateaction) method.
99

1010
## Throw errors when they occur
1111

@@ -14,11 +14,12 @@ Starting from v7.4.0, you can now pass optional `throwServerError` and `throwVal
1414

1515
## Action callbacks
1616

17-
- `onSuccess`: called when action execution succeeds.
18-
- `onError`: called when action execution fails (validation errors or server error).
19-
- `onSettled`: called when action execution succeeds or fails.
17+
- `onSuccess()`: called when action execution succeeds.
18+
- `onError()`: called when action execution fails (validation errors or server error).
19+
- `onNavigation()`: called when a `next/navigation` function is called in a middleware function or in the action's server code function.
20+
- `onSettled()`: called when action execution succeeds or fails.
2021

21-
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.
22+
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.
2223

2324
```tsx
2425
import { actionClient } from "@/lib/safe-action";
@@ -49,6 +50,13 @@ const action = actionClient
4950
clientInput,
5051
bindArgsClientInputs
5152
}) => {},
53+
onNavigation: async ({
54+
navigationKind,
55+
ctx,
56+
metadata,
57+
clientInput,
58+
bindArgsClientInputs,
59+
}) => {},
5260
onSettled: async ({
5361
result,
5462
ctx,
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
---
22
sidebar_position: 6
3-
description: Learn how to use next-safe-action with a i18n solution.
3+
description: Learn how to extend previous schema(s)
44
---
55

66
# Extend previous schema(s)
77

8-
Sometimes it's useful to define an action "template" with a base schema and then extend it with additional properties. This can be done inside the [`schema`](/docs/define-actions/instance-methods#schema) method by passing an async function that has the previous schema as its argument. See the example below:
8+
Sometimes it's useful to define an action "template" with a base input schema and then extend it with additional properties. This can be done inside the [`inputSchema`](/docs/define-actions/instance-methods#inputschema) method by passing an async function that has the previous schema as its argument. See the example below:
99

1010
```typescript
1111
"use server";
1212

1313
import { actionClient } from "@/lib/safe-action";
1414
import { z } from "zod";
1515

16-
const schema = z.object({
16+
const inputSchema = z.object({
1717
username: z.string(),
1818
});
1919

2020
const myAction = actionClient
21-
.schema(schema)
21+
.inputSchema(inputSchema)
2222
// highlight-start
23-
.schema(async (prevSchema) => {
23+
.inputSchema(async (prevSchema) => {
2424
// Here we extend the previous schema with `password` property.
2525
return prevSchema.extend({ password: z.string() });
2626
})
27-
.schema(async (prevSchema) => {
27+
.inputSchema(async (prevSchema) => {
2828
// Here with `age` property.
2929
return prevSchema.extend({ age: z.number().positive() });
3030
})
@@ -35,4 +35,4 @@ const myAction = actionClient
3535
});
3636
```
3737

38-
Note that if you don't use `prevSchema` inside the `schema` method, the previous schema(s) will be overwritten.
38+
Note that if you don't use `prevSchema` inside the `inputSchema` method, the previous schema(s) will be overwritten.

website/docs/define-actions/validation-errors.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: Learn how to customize or manually creating validation errors.
1010
next-safe-action, by default, emulates Zod's [`format()`](https://zod.dev/ERROR_HANDLING?id=formatting-errors) method for building both validation and bind args validation errors and return them to the client.
1111

1212
This can be customized both at the safe action client level and at the action level by:
13-
- using [`defaultValidationErrorsShape`](/docs/define-actions/create-the-client#defaultvalidationerrorsshape) optional property in `createSafeActionClient`;
13+
- using [`defaultValidationErrorsShape`](/docs/define-actions/create-the-client#defaultvalidationerrorsshape) optional property in `createSafeActionClient()`;
1414
- using `handleValidationErrorsShape()` optional async function in [`inputSchema()`](/docs/define-actions/instance-methods#inputschema) method.
1515

1616
The second way overrides the shape set at the instance level, per action.
@@ -90,7 +90,7 @@ When input data fails schema validation, a `validationErrors` object is returned
9090

9191
It's often useful to also define custom logic to set additional validation errors by ourselves, for example when a user is signing up and password/confirm password fields don't match, and/or when the email is already in use.
9292

93-
Let's see how to implement this specific case in the optimal way, using both schema refinements and errors set in action's server code function, thanks to `returnValidationErrors`.
93+
Let's see how to implement this specific case in the optimal way, using both schema refinements and errors set in action's server code function, thanks to `returnValidationErrors()`.
9494

9595
### Schema refinements
9696

@@ -144,7 +144,7 @@ const signupAction = actionClient
144144

145145
Note that:
146146

147-
- You're required to pass a schema as the first argument of `returnValidationErrors`. This is used to infer the type of the validation errors set via the second argument.
148-
- Errors set using `returnValidationErrors` will not be merged with the schema ones. If schema validation fails, the execution stops before reaching action's server code function. Otherwise, the action's backend code would receive invalid parsed input.
147+
- You're required to pass a schema as the first argument of `returnValidationErrors()`. This is used to infer the type of the validation errors set via the second argument.
148+
- Errors set using `returnValidationErrors()` will not be merged with the schema ones. If schema validation fails, the execution stops before reaching action's server code function. Otherwise, the action's backend code would receive invalid parsed input.
149149
- `returnValidationErrors()` returns `never`. This means that internally it throws an error that gets caught and processed by next-safe-action, so code declared below the `returnValidationErrors()` invocation will not be executed.
150150
- Since it returns `never`, you don't need to use `return` before this function call, and you can call it only once per execution path (it works the same way as Next.js `redirect()` and `notFound()` functions).

website/docs/migrations/v7-to-v8.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Version 8 introduces significant changes to the validation system, improves type
1111
Legend:
1212
- ⚠️ Breaking change
1313
- 🆕 New feature
14-
- ⏳ Deprecation
1514
- ✨ Improvement
15+
- 🔄 Refactor
1616

1717
## What's new?
1818

@@ -41,7 +41,8 @@ The behavior when using functions from `next/navigation` was very unclear and co
4141

4242
This behavior has been changed in v8. Now, when you're using functions imported from `next/navigation` in an action:
4343
- the hooks `status` value will be `"hasNavigated"` instead of `"hasSucceeded"`;
44-
- a new `onNavigation` callback will be triggered, both for actions and hooks, instead of `onSuccess`. This callback receives a `navigationKind` value, that indicates the type of navigation that occurred.
44+
- a new `onNavigation` callback will be triggered, both for actions and hooks, instead of `onSuccess`. This callback receives a `navigationKind` value, that indicates the type of navigation that occurred;
45+
- the `success` property of the middleware result will now be `false`, instead of `true`, if a navigation function was called in a middleware function or in the action's server code function.
4546

4647
```typescript
4748
import { useAction } from "next-safe-action/hooks";
@@ -91,24 +92,6 @@ const result = await boundAction(input);
9192

9293
The deprecated `executeOnMount` hook functionality has been removed in v8. Server Actions should be used only for mutations, so it doesn't make sense to execute them on mount. Or at least, it shouldn't be a common case and, above all, a library job. If you still need to do it, just use `useEffect` to trigger the execution, however you want.
9394

94-
### `schema` method renamed to `inputSchema`
95-
96-
The library, since version 7.8.0, supports both input and output validation, respectively using the `schema` and `outputSchema` methods. In v8, the `schema` method has been renamed to `inputSchema` to better reflect its purpose, and avoid potential confusion.
97-
98-
The `schema` method is deprecated and will be removed in a future version, but it's still available for backward compatibility. It's now just an alias for `inputSchema`:
99-
100-
```typescript title="v7"
101-
actionClient.schema(/* ... */)
102-
```
103-
104-
```typescript title="v8"
105-
actionClient.inputSchema(/* ... */)
106-
```
107-
108-
:::info UPDATE EXISTING ACTIONS
109-
To update your actions, you can just use the search and replace feature of your editor to replace all occurrences of `.schema` with `.inputSchema`.
110-
:::
111-
11295
### ✨ Type-checked metadata
11396

11497
This is a big improvement in type safety over v7. Metadata is now statically type-checked when passed to actions. So, now if you forget to pass the expected metadata shape, as defined by the `defineMetadataSchema` init option, you will get a type error immediately:
@@ -144,7 +127,7 @@ const action = actionClient
144127
);
145128
```
146129

147-
### Safe action result always defined
130+
### 🔄 Safe action result always defined
148131

149132
The action result object is now always defined. This allows you to destructure it without the need to check if it's defined or not first:
150133

@@ -164,7 +147,26 @@ if (result?.data) {
164147
const { data } = await action(input);
165148
```
166149

167-
### Deprecated `useStateAction` hook
150+
### 🔄 `schema` method renamed to `inputSchema`
151+
152+
The library, since version 7.8.0, supports both input and output validation, respectively using the `schema` and `outputSchema` methods. In v8, the `schema` method has been renamed to `inputSchema` to better reflect its purpose, and avoid potential confusion.
153+
154+
The `schema` method is deprecated and will be removed in a future version, but it's still available for backward compatibility. It's now just an alias for `inputSchema`:
155+
156+
```typescript title="v7"
157+
actionClient.schema(/* ... */)
158+
```
159+
160+
```typescript title="v8"
161+
actionClient.inputSchema(/* ... */)
162+
```
163+
164+
:::info UPDATE EXISTING ACTIONS
165+
To update your actions, you can just use the search and replace feature of your editor to replace all occurrences of `.schema` with `.inputSchema`.
166+
:::
167+
168+
169+
### 🔄 Deprecation of `useStateAction` hook
168170

169171
The `useStateAction` hook has been deprecated. It's always been kind of a hack to begin with, and it doesn't support progressive enhancement, since it tries to do what the `useAction` and `useOptimisticAction` hooks do.
170172

0 commit comments

Comments
 (0)