Skip to content

Commit

Permalink
refactor: rename SafeActionCallbacks type to SafeActionUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 21, 2024
1 parent 84f94fb commit 8195fb7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { Infer, InferArray, InferIn, InferInArray, Schema, ValidationAdapte
import type {
MiddlewareFn,
MiddlewareResult,
SafeActionCallbacks,
SafeActionClientOpts,
SafeActionFn,
SafeActionResult,
SafeActionUtils,
SafeStateActionFn,
ServerCodeFn,
StateServerCodeFn,
Expand Down Expand Up @@ -53,13 +53,13 @@ export function actionBuilder<
function buildAction({ withState }: { withState: false }): {
action: <Data>(
serverCodeFn: ServerCodeFn<MD, Ctx, S, BAS, Data>,
cb?: SafeActionCallbacks<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
utils?: SafeActionUtils<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
) => SafeActionFn<ServerError, S, BAS, CVE, CBAVE, Data>;
};
function buildAction({ withState }: { withState: true }): {
action: <Data>(
serverCodeFn: StateServerCodeFn<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>,
cb?: SafeActionCallbacks<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
utils?: SafeActionUtils<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
) => SafeStateActionFn<ServerError, S, BAS, CVE, CBAVE, Data>;
};
function buildAction({ withState }: { withState: boolean }) {
Expand All @@ -68,7 +68,7 @@ export function actionBuilder<
serverCodeFn:
| ServerCodeFn<MD, Ctx, S, BAS, Data>
| StateServerCodeFn<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>,
cb?: SafeActionCallbacks<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
utils?: SafeActionUtils<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
) => {
return async (...clientInputs: unknown[]) => {
let prevCtx: unknown = undefined;
Expand Down Expand Up @@ -260,7 +260,7 @@ export function actionBuilder<
// If an internal framework error occurred, throw it, so it will be processed by Next.js.
if (frameworkError) {
await Promise.resolve(
cb?.onSuccess?.({
utils?.onSuccess?.({
data: undefined,
metadata: args.metadata,
ctx: prevCtx as Ctx,
Expand All @@ -274,7 +274,7 @@ export function actionBuilder<
);

await Promise.resolve(
cb?.onSettled?.({
utils?.onSettled?.({
metadata: args.metadata,
ctx: prevCtx as Ctx,
clientInput: clientInputs.at(-1) as S extends Schema ? InferIn<S> : undefined,
Expand Down Expand Up @@ -311,7 +311,7 @@ export function actionBuilder<
}

await Promise.resolve(
cb?.onSuccess?.({
utils?.onSuccess?.({
metadata: args.metadata,
ctx: prevCtx as Ctx,
data: actionResult.data as Data,
Expand All @@ -325,7 +325,7 @@ export function actionBuilder<
);
} else {
await Promise.resolve(
cb?.onError?.({
utils?.onError?.({
metadata: args.metadata,
ctx: prevCtx as Ctx,
clientInput: clientInputs.at(-1) as S extends Schema ? InferIn<S> : undefined,
Expand All @@ -337,7 +337,7 @@ export function actionBuilder<

// onSettled, if provided, is always executed.
await Promise.resolve(
cb?.onSettled?.({
utils?.onSettled?.({
metadata: args.metadata,
ctx: prevCtx as Ctx,
clientInput: clientInputs.at(-1) as S extends Schema ? InferIn<S> : undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ export type StateServerCodeFn<
) => Promise<Data>;

/**
* Type of action execution callbacks. These are called after the action is executed, on the server side.
* Type of action execution utils. It includes action callbacks and other utils.
*/
export type SafeActionCallbacks<
export type SafeActionUtils<
ServerError,
MD,
Ctx,
Expand Down
10 changes: 5 additions & 5 deletions packages/next-safe-action/src/safe-action-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { Infer, Schema, ValidationAdapter } from "./adapters/types";
import type {
DVES,
MiddlewareFn,
SafeActionCallbacks,
SafeActionClientOpts,
SafeActionUtils,
ServerCodeFn,
StateServerCodeFn,
} from "./index.types";
Expand Down Expand Up @@ -213,7 +213,7 @@ export class SafeActionClient<
*/
action<Data>(
serverCodeFn: ServerCodeFn<MD, Ctx, S, BAS, Data>,
cb?: SafeActionCallbacks<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
utils?: SafeActionUtils<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
) {
return actionBuilder({
handleReturnedServerError: this.#handleReturnedServerError,
Expand All @@ -228,7 +228,7 @@ export class SafeActionClient<
handleValidationErrorsShape: this.#handleValidationErrorsShape,
handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape,
throwValidationErrors: this.#throwValidationErrors,
}).action(serverCodeFn, cb);
}).action(serverCodeFn, utils);
}

/**
Expand All @@ -241,7 +241,7 @@ export class SafeActionClient<
*/
stateAction<Data>(
serverCodeFn: StateServerCodeFn<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>,
cb?: SafeActionCallbacks<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
utils?: SafeActionUtils<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
) {
return actionBuilder({
handleReturnedServerError: this.#handleReturnedServerError,
Expand All @@ -256,6 +256,6 @@ export class SafeActionClient<
handleValidationErrorsShape: this.#handleValidationErrorsShape,
handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape,
throwValidationErrors: this.#throwValidationErrors,
}).stateAction(serverCodeFn, cb);
}).stateAction(serverCodeFn, utils);
}
}
4 changes: 2 additions & 2 deletions website/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ export type StateServerCodeFn<
) => Promise<Data>;
```

### `SafeActionCallbacks`
### `SafeActionUtils`

Type of action execution callbacks. These are called after the action is executed, on the server side.
Type of action execution utils. It includes action callbacks and other utils.

```typescript
export type SafeActionCallbacks<
Expand Down

0 comments on commit 8195fb7

Please sign in to comment.