Skip to content

Commit

Permalink
docs(website): reorganize documentation, add v7 sections
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Feb 5, 2024
1 parent fd64e29 commit f70a0d7
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 28 deletions.
2 changes: 1 addition & 1 deletion website/docs/contributing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 9
sidebar_position: 8
description: Learn how to contribute to next-safe-action via GitHub.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/examples/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 6
sidebar_position: 5
---

# Examples
2 changes: 1 addition & 1 deletion website/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ export default function Login() {
}
```

You also can execute Server Actions with hooks, which are a more powerful way to handle mutations. For more information about these, check out the [`useAction`](/docs/usage-from-client/hooks/useaction) and [`useOptimisticAction`](/docs/usage-from-client/hooks/useoptimisticaction) hooks sections.
You also can execute Server Actions with hooks, which are a more powerful way to handle mutations. For more information about these, check out the [`useAction`](/docs/usage/client-components/hooks/useaction) and [`useOptimisticAction`](/docs/usage/client-components/hooks/useoptimisticaction) hooks sections.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 8
sidebar_position: 7
description: Learn how to migrate from a version to the next one.
title: Migration
---
---

# Migrations
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
sidebar_position: 1
description: Learn how to migrate from next-safe-action version 3 to version 4.
title: v3 to v4
sidebar_label: v3 to v4
---

# Migration from v3 to v4
Expand All @@ -28,7 +28,7 @@ You can continue to use version 3 of the library if you want to. There are no se
- Reorganized callbacks arguments for `onSuccess` and `onError`:
- from `onSuccess(data, reset, input)` to `onSuccess(data, input, reset)`
- from `onError(error, reset, input)` to `onError(error, input, reset)`
- `useOptimisticAction` just required a safe action and an initial optimistic state before. Now it requires a `reducer` function too, that determines the behavior of the optimistic state update when the `execute` function is called. Also, now only one input argument is required by `execute`, instead of two. The same input passed to the actual safe action is now passed to the `reducer` function too, as the second argument (`input`). More information about this hook can be found [here](/docs/usage-from-client/hooks/useoptimisticaction).
- `useOptimisticAction` just required a safe action and an initial optimistic state before. Now it requires a `reducer` function too, that determines the behavior of the optimistic state update when the `execute` function is called. Also, now only one input argument is required by `execute`, instead of two. The same input passed to the actual safe action is now passed to the `reducer` function too, as the second argument (`input`). More information about this hook can be found [here](/docs/usage/client-components/hooks/useoptimisticaction).

### Types

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
sidebar_position: 2
description: Learn how to migrate from next-safe-action version 4 to version 5.
title: v4 to v5
sidebar_label: v4 to v5
---

# Migration from v4 to v5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
sidebar_position: 3
description: Learn how to migrate from next-safe-action version 5 to version 6.
title: v5 to v6
sidebar_label: v5 to v6
---

# Migration from v5 to v6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
sidebar_position: 4
description: Learn how to migrate from next-safe-action version 6 to version 7.
title: v6 to v7
sidebar_label: v6 to v7
---

# Migration from v6 to v7

## What's new?

WIP
TODO: WIP

## BREAKING CHANGES

WIP
TODO: WIP
2 changes: 1 addition & 1 deletion website/docs/safe-action-client/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 5
sidebar_position: 4
description: Safe action client is the instance that you can use to create typesafe actions.
---

Expand Down
50 changes: 48 additions & 2 deletions website/docs/types.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 7
sidebar_position: 6
description: List of exported types.
---

Expand Down Expand Up @@ -42,6 +42,14 @@ type ServerCodeFn<S extends Schema, Data, Context> = (
) => Promise<Data>;
```

### `ValidationErrors`

Type of the returned object when input validation fails.

```typescript
export type ValidationErrors<S extends Schema> = Extend<ErrorList & SchemaErrors<Infer<S>>>;
```

## /hooks

### `HookResult`
Expand Down Expand Up @@ -87,6 +95,44 @@ type HookActionStatus = "idle" | "executing" | "hasSucceeded" | "hasErrored";

---

## Utility types

### `MaybePromise`

Returns type or promise of type.

```typescript
export type MaybePromise<T> = Promise<T> | T;
```

### `Extend`

Extends an object without printing "&".

```typescript
export type Extend<S> = S extends infer U ? { [K in keyof U]: U[K] } : never;
```

### `ErrorList`

Object with an optional list of validation errors. Used in [`ValidationErrors`](#validationerrors) type.

```typescript
export type ErrorList = { _errors?: string[] } & {};
```

### `SchemaErrors`

Creates nested schema validation errors type using recursion. Used in [`ValidationErrors`](#validationerrors) type.

```typescript
export type SchemaErrors<S> = {
[K in keyof S]?: S[K] extends object ? Extend<ErrorList & SchemaErrors<S[K]>> : ErrorList;
} & {};
```

---

## TypeSchema library

`Infer`, `InferIn`, `Schema` types come from [TypeSchema](https://typeschema.com/#types) library.
`Infer`, `InferIn`, `Schema` types come from [TypeSchema](https://typeschema.com/#types) library.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
sidebar_position: 4
description: Action result object is the result of an action execution, returned by hooks.
sidebar_position: 3
description: Action result object is the result of an action execution.
---

# Action result object
Expand All @@ -11,5 +11,5 @@ Here's how action result object is structured (all keys are optional):
| Name | When | Value |
|--------------------|--------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `data?` | Execution is successful. | What you returned in action's server code. |
| `validationErrors?` | Input data doesn't pass schema validation. | A partial `Record` of input schema keys as key or `_root`, and `string[]` as value. `_root` is a reserved key, used for global validation issues. Example: `{ _root: ["A global error"], email: ["Email is required."] }`. |
| `validationErrors?` | Input data doesn't pass validation. | An object whose keys are the names of the fields that failed validation. Each key's value is either an `ErrorList` or a nested key with an `ErrorList` inside.<br />`ErrorList` is defined as: `{ errors?: string[] }`.
| `serverError?` | An error occurs during action's server code execution. | A `string` that by default is "Something went wrong while executing the operation" for every server error that occurs, but this is [configurable](/docs/safe-action-client/custom-server-error-handling#handlereturnedservererror) when instantiating a new client. |
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export default function Login({ loginUser }: Props) {

### Action result object

Every action you execute returns an object with the same structure. This is described in the [action result object](/docs/usage-from-client/action-result-object) section.
Every action you execute returns an object with the same structure. This is described in the [action result object](/docs/usage/action-result-object) section.

Explore a working example [here](https://github.com/TheEdoRan/next-safe-action/tree/main/packages/example-app/src/app).
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Learn how to use the `useAction` hook.
# `useAction`

:::info
`useAction` **waits** for the action to finish execution before returning the result. If you need to perform optimistic updates, use [`useOptimisticAction`](/docs/usage-from-client/hooks/useoptimisticaction) instead.
`useAction` **waits** for the action to finish execution before returning the result. If you need to perform optimistic updates, use [`useOptimisticAction`](/docs/usage/client-components/hooks/useoptimisticaction) instead.
:::

With this hook, you get full control of the action execution flow.
Expand Down Expand Up @@ -60,7 +60,7 @@ As you can see, here we display a greet message after the action is performed, i
| Name | Type | Purpose |
|--------------|--------------------------------------------|--------------------------------------------------------------------------------------------------|
| `safeAction` | [SafeAction](/docs/types#safeaction) | This is the action that will be called when you use `execute` from hook's return object. |
| `callbacks?` | [HookCallbacks](/docs/types#hookcallbacks) | Optional callbacks. More information about them [here](/docs/usage-from-client/hooks/callbacks). |
| `callbacks?` | [HookCallbacks](/docs/types#hookcallbacks) | Optional callbacks. More information about them [here](/docs/usage/client-components/hooks/callbacks). |

### `useAction` return object

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Learn how to use the `useOptimisticAction` hook.
# `useOptimisticAction`

:::info
`useOptimisticAction` **does not wait** for the action to finish execution before returning the optimistic data. It is then synced with the real result from server when the action has finished its execution. If you need to perform normal mutations, use [`useAction`](/docs/usage-from-client/hooks/useaction) instead.
`useOptimisticAction` **does not wait** for the action to finish execution before returning the optimistic data. It is then synced with the real result from server when the action has finished its execution. If you need to perform normal mutations, use [`useAction`](/docs/usage/client-components/hooks/useaction) instead.
:::

:::caution warning
Expand Down Expand Up @@ -105,7 +105,7 @@ export default function AddLikes({ numOfLikes }: Props) {
| `safeAction` | [`SafeAction`](/docs/types#safeaction) | This is the action that will be called when you use `execute` from hook's return object. |
| `initialOptimisticData` | `Data` (return type of the `safeAction` you passed as first argument) | An initializer for the optimistic state. Usually this value comes from the parent Server Component. |
| `reducer` | `(state: Data, input: InferIn<S>) => Data` | When you call the action via `execute`, this function determines how the optimistic update is performed. Basically, here you define what happens **immediately** after `execute` is called, and before the actual result comes back from the server. |
| `callbacks?` | [`HookCallbacks`](/docs/types#hookcallbacks) | Optional callbacks. More information about them [here](/docs/usage-from-client/hooks/callbacks). |
| `callbacks?` | [`HookCallbacks`](/docs/types#hookcallbacks) | Optional callbacks. More information about them [here](/docs/usage/client-components/hooks/callbacks). |


### `useOptimisticAction` return object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
sidebar_position: 3
sidebar_position: 1
description: Learn how to execute safe actions inside Client Components.
sidebar_label: Client Components
---

# Usage from client
# Usage with Client Components

There are three different ways to execute Server Actions from Client Components. First one is the "direct way", the most simple one, but the least powerful too. The other two are by using `useAction` and `useOptimisticAction` hooks, which we will cover in the next sections.
8 changes: 8 additions & 0 deletions website/docs/usage/custom-validation-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
sidebar_position: 4
description: Set custom validation errors in schema or in action's server code function.
---

# Custom validation errors

TODO: WIP
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
sidebar_position: 4
sidebar_position: 2
description: Learn how to execute safe actions as form actions.
sidebar_label: Forms
---

# Usage with forms
Expand Down
6 changes: 6 additions & 0 deletions website/docs/usage/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
sidebar_position: 3
description: Learn how to migrate from a version to the next one.
---

# Usage
2 changes: 1 addition & 1 deletion website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default {
},
footer: {
style: "light",
copyright: `Copyleft <span class="copyleft">&copy;</span> ${new Date().getFullYear()} Edoardo Ranghieri`,
copyright: `Copyleft <span class="copyleft">&copy;</span> 2023 Edoardo Ranghieri`,
},
prism: {
additionalLanguages: ["typescript"],
Expand Down

0 comments on commit f70a0d7

Please sign in to comment.