Skip to content

Commit

Permalink
feat: allow direct use of action method, without schema call
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Apr 23, 2024
1 parent 054487c commit 3935e9f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
"is-ci": "^3.0.1",
"semantic-release": "^22.0.12",
"turbo": "^1.13.2"
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { action } from "@/lib/safe-action";

export const emptyAction = action
.metadata({ actionName: "onboardUser" })
.schema()
.action(async () => {
.action(async (obj) => {
console.log("OBJ ->", obj);

await new Promise((res) => setTimeout(res, 500));

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function EmptySchema() {

return (
<main className="w-96 max-w-full px-4">
<StyledHeading>Action without schema</StyledHeading>
<StyledHeading>Action without arguments</StyledHeading>
<form
className="flex flex-col mt-8 space-y-4"
onSubmit={(e) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/example-app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function Home() {
<span className="font-mono">useOptimisticAction</span> hook
</ExampleLink>
<ExampleLink href="/bind-arguments">Bind arguments</ExampleLink>
<ExampleLink href="/empty-schema">Empty schema</ExampleLink>
<ExampleLink href="/without-arguments">Without arguments</ExampleLink>
<ExampleLink href="/server-form">Server Form</ExampleLink>
<ExampleLink href="/client-form">Client Form</ExampleLink>
<ExampleLink href="/react-hook-form">React Hook Form</ExampleLink>
Expand Down
20 changes: 20 additions & 0 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,15 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
formatValidationErrors: utils?.formatValidationErrors,
metadata: data,
}),
action: <const Data = null>(serverCodeFn: ServerCodeFn<undefined, [], Data, Ctx, Metadata>) =>
this.#action({
serverCodeFn,
bindArgsSchemas: [],
metadata: data,
}),
};
}

/**
* Pass an input schema to define safe action arguments.
* @param schema An input schema supported by [TypeSchema](https://typeschema.com/#coverage).
Expand All @@ -100,6 +107,19 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
});
}

/**
* Define a new safe action without input arguments.
* @param serverCodeFn Server code function
* @returns
*/
public action<const Data = null>(serverCodeFn: ServerCodeFn<undefined, [], Data, Ctx, null>) {
return this.#action({
serverCodeFn,
bindArgsSchemas: [],
metadata: null,
});
}

#schema<
const S extends Schema | undefined = undefined,
const FVE = ValidationErrors<S>,
Expand Down
2 changes: 1 addition & 1 deletion website/docs/safe-action-client/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bindArgsSchemas<const BAS extends Schema[], const FBAVE = BindArgsValidationErro
action<const Data = null>(serverCodeFn: ServerCodeFn<S, BAS, Data, Ctx, MD>) => SafeActionFn<ServerError, S, BAS, FVE, FBAVE, Data>
```

`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.
`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.

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

Expand Down

0 comments on commit 3935e9f

Please sign in to comment.