Skip to content

Commit 3935e9f

Browse files
committedApr 23, 2024··
feat: allow direct use of action method, without schema call
1 parent 054487c commit 3935e9f

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
"is-ci": "^3.0.1",
3939
"semantic-release": "^22.0.12",
4040
"turbo": "^1.13.2"
41-
}
41+
},
42+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
4243
}

‎packages/example-app/src/app/(examples)/empty-schema/empty-action.ts ‎packages/example-app/src/app/(examples)/without-arguments/empty-action.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { action } from "@/lib/safe-action";
44

55
export const emptyAction = action
66
.metadata({ actionName: "onboardUser" })
7-
.schema()
8-
.action(async () => {
7+
.action(async (obj) => {
8+
console.log("OBJ ->", obj);
9+
910
await new Promise((res) => setTimeout(res, 500));
1011

1112
return {

‎packages/example-app/src/app/(examples)/empty-schema/page.tsx ‎packages/example-app/src/app/(examples)/without-arguments/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function EmptySchema() {
3535

3636
return (
3737
<main className="w-96 max-w-full px-4">
38-
<StyledHeading>Action without schema</StyledHeading>
38+
<StyledHeading>Action without arguments</StyledHeading>
3939
<form
4040
className="flex flex-col mt-8 space-y-4"
4141
onSubmit={(e) => {

‎packages/example-app/src/app/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function Home() {
1515
<span className="font-mono">useOptimisticAction</span> hook
1616
</ExampleLink>
1717
<ExampleLink href="/bind-arguments">Bind arguments</ExampleLink>
18-
<ExampleLink href="/empty-schema">Empty schema</ExampleLink>
18+
<ExampleLink href="/without-arguments">Without arguments</ExampleLink>
1919
<ExampleLink href="/server-form">Server Form</ExampleLink>
2020
<ExampleLink href="/client-form">Client Form</ExampleLink>
2121
<ExampleLink href="/react-hook-form">React Hook Form</ExampleLink>

‎packages/next-safe-action/src/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
8080
formatValidationErrors: utils?.formatValidationErrors,
8181
metadata: data,
8282
}),
83+
action: <const Data = null>(serverCodeFn: ServerCodeFn<undefined, [], Data, Ctx, Metadata>) =>
84+
this.#action({
85+
serverCodeFn,
86+
bindArgsSchemas: [],
87+
metadata: data,
88+
}),
8389
};
8490
}
91+
8592
/**
8693
* Pass an input schema to define safe action arguments.
8794
* @param schema An input schema supported by [TypeSchema](https://typeschema.com/#coverage).
@@ -100,6 +107,19 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
100107
});
101108
}
102109

110+
/**
111+
* Define a new safe action without input arguments.
112+
* @param serverCodeFn Server code function
113+
* @returns
114+
*/
115+
public action<const Data = null>(serverCodeFn: ServerCodeFn<undefined, [], Data, Ctx, null>) {
116+
return this.#action({
117+
serverCodeFn,
118+
bindArgsSchemas: [],
119+
metadata: null,
120+
});
121+
}
122+
103123
#schema<
104124
const S extends Schema | undefined = undefined,
105125
const FVE = ValidationErrors<S>,

‎website/docs/safe-action-client/instance-methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bindArgsSchemas<const BAS extends Schema[], const FBAVE = BindArgsValidationErro
4747
action<const Data = null>(serverCodeFn: ServerCodeFn<S, BAS, Data, Ctx, MD>) => SafeActionFn<ServerError, S, BAS, FVE, FBAVE, Data>
4848
```
4949

50-
`action` is the final method in the list. It accepts a [`serverCodeFn`](#servercodefn) of type [`ServerCodeFn`](/docs/types#servercodefn) and returns a new safe action function of type [`SafeActionFn`](/docs/types#safeactionfn), which can be called from your components.
50+
`action` is the final method in the list. It accepts a [`serverCodeFn`](#servercodefn) of type [`ServerCodeFn`](/docs/types#servercodefn) and returns a new safe action function of type [`SafeActionFn`](/docs/types#safeactionfn), which can be called from your components. When an action doesn't need input arguments, you can directly use this method without passing a schema to [`schema`](#schema) method.
5151

5252
When the action is executed, all middleware functions in the chain will be called at runtime, in the order they were defined.
5353

0 commit comments

Comments
 (0)
Please sign in to comment.