Skip to content

Commit

Permalink
chore(website): update serverCodeFn documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed May 15, 2024
1 parent ea1b083 commit 8a4309c
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions website/docs/safe-action-client/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,18 @@ Note that when you use `stateAction`, and also want to access `prevResult` in `s
### `serverCodeFn`

```typescript title="Stateless action"
serverCodeFn<S, BAS, Data, Ctx, MD> = (args: {
parsedInput: S extends Schema ? Infer<S> : undefined;
bindArgsParsedInputs: InferArray<BAS>;
ctx: Ctx;
metadata: MD;
}) => Promise<Data>;
serverCodeFn(
args: { parsedInput, bindArgsParsedInputs, ctx, metadata }
) => Promise<Data>;
```

```typescript title="Stateful action"
serverCodeFn<ServerError, S extends Schema | undefined, BAS extends readonly Schema[], FVE, FBAVE, Ctx, MD, Data> = (
args: {
parsedInput: S extends Schema ? Infer<S> : undefined;
bindArgsParsedInputs: InferArray<BAS>;
ctx: Ctx;
metadata: MD;
},
utils: { prevResult: Prettify<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>> }
serverCodeFn = (
args: { parsedInput, bindArgsParsedInputs, ctx, metadata },
utils: { prevResult }
) => Promise<Data>;
```

`serverCodeFn` is the async function that will be executed on the **server side** when the action is invoked. If input validation fails, or execution gets halted in a middleware function, the server code function will not be called.
`serverCodeFn` is the async function of type [`ServerCodeFn`](/docs/types#servercodefn)/[`StateServerCodeFn`](/docs/types#stateservercodefn) that will be executed on the **server side** when the action is invoked. If input validation fails, or execution gets halted in a middleware function, the server code function will not be called.

In the case of a stateful safe action, `serverCodeFn` will also receive the `prevResult` as a property of the second argument (`utils` object) from the previous action execution, thanks to the [`useStateAction`](/docs/execution/hooks/usestateaction) hook (that uses React's [`useActionState`](https://react.dev/reference/react/useActionState) hook under the hood).

0 comments on commit 8a4309c

Please sign in to comment.