Skip to content

Commit 1617d22

Browse files
committedApr 8, 2024··
refactor: rename define method to action
1 parent dbd9639 commit 1617d22

File tree

18 files changed

+33
-33
lines changed

18 files changed

+33
-33
lines changed
 

‎packages/example-app/src/app/(examples)/bind-arguments/onboard-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const onboardUser = action
1414
z.string().uuid(),
1515
z.number().min(18).max(150),
1616
])
17-
.define(
17+
.action(
1818
async ({
1919
parsedInput: { username },
2020
bindArgsParsedInputs: [userId, age],

‎packages/example-app/src/app/(examples)/direct/login-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const schema = z.object({
1212
export const loginUser = action
1313
.metadata({ actionName: "loginUser" })
1414
.schema(schema)
15-
.define(async ({ parsedInput: { username, password } }) => {
15+
.action(async ({ parsedInput: { username, password } }) => {
1616
if (username === "johndoe") {
1717
returnValidationErrors(schema, {
1818
username: {

‎packages/example-app/src/app/(examples)/hook/deleteuser-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const schema = z.object({
1010
export const deleteUser = action
1111
.metadata({ actionName: "deleteUser" })
1212
.schema(schema)
13-
.define(async ({ parsedInput: { userId } }) => {
13+
.action(async ({ parsedInput: { userId } }) => {
1414
await new Promise((res) => setTimeout(res, 1000));
1515

1616
if (Math.random() > 0.5) {

‎packages/example-app/src/app/(examples)/nested-schema/shop-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const schema = z
7171
export const buyProduct = action
7272
.metadata({ actionName: "buyProduct" })
7373
.schema(schema)
74-
.define(async () => {
74+
.action(async () => {
7575
return {
7676
success: true,
7777
};

‎packages/example-app/src/app/(examples)/optimistic-hook/addlikes-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const schema = z.object({
2020
export const addLikes = action
2121
.metadata({ actionName: "addLikes" })
2222
.schema(schema)
23-
.define(async ({ parsedInput: { incrementBy } }) => {
23+
.action(async ({ parsedInput: { incrementBy } }) => {
2424
await new Promise((res) => setTimeout(res, 2000));
2525

2626
const likesCount = incrementLikes(incrementBy);

‎packages/example-app/src/app/(examples)/react-hook-form/buyproduct-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { schema } from "./validation";
77
export const buyProduct = action
88
.metadata({ actionName: "buyProduct" })
99
.schema(schema)
10-
.define(async ({ parsedInput: { productId } }) => {
10+
.action(async ({ parsedInput: { productId } }) => {
1111
return {
1212
productId,
1313
transactionId: randomUUID(),

‎packages/example-app/src/app/(examples)/server-form/signup-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const schema = zfd.formData({
1212
export const signup = action
1313
.metadata({ actionName: "signup" })
1414
.schema(schema)
15-
.define(async ({ parsedInput: { email, password } }) => {
15+
.action(async ({ parsedInput: { email, password } }) => {
1616
console.log("Email:", email, "Password:", password);
1717
return {
1818
success: true,

‎packages/example-app/src/app/(examples)/with-context/edituser-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const schema = object({
1111
export const editUser = authAction
1212
.metadata({ actionName: "editUser" })
1313
.schema(schema)
14-
.define(
14+
.action(
1515
// Here you have access to `userId`, and `sessionId which comes from middleware functions
1616
// defined before.
1717
// \\\\\\\\\\\\\\\\\\

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class SafeActionClient<const ServerError, const Ctx = null> {
8787
* @param serverCodeFn A function that executes the server code.
8888
* @returns {SafeActionFn}
8989
*/
90-
#define<const S extends Schema, const BAS extends Schema[], const Data = null>(
90+
#action<const S extends Schema, const BAS extends Schema[], const Data = null>(
9191
schema: S,
9292
bindArgsSchemas: BAS,
9393
serverCodeFn: ServerCodeFn<S, BAS, Data, Ctx>
@@ -238,8 +238,8 @@ class SafeActionClient<const ServerError, const Ctx = null> {
238238
bindArgsSchemas: BAS
239239
) {
240240
return {
241-
define: <const Data = null>(serverCodeFn: ServerCodeFn<S, BAS, Data, Ctx>) =>
242-
this.#define(mainSchema, bindArgsSchemas, serverCodeFn),
241+
action: <const Data = null>(serverCodeFn: ServerCodeFn<S, BAS, Data, Ctx>) =>
242+
this.#action(mainSchema, bindArgsSchemas, serverCodeFn),
243243
};
244244
}
245245

@@ -252,8 +252,8 @@ class SafeActionClient<const ServerError, const Ctx = null> {
252252
return {
253253
bindArgsSchemas: <const BAS extends Schema[]>(bindArgsSchemas: BAS) =>
254254
this.#bindArgsSchemas(schema, bindArgsSchemas),
255-
define: <const Data = null>(serverCodeFn: ServerCodeFn<S, [], Data, Ctx>) =>
256-
this.#define(schema, [], serverCodeFn),
255+
action: <const Data = null>(serverCodeFn: ServerCodeFn<S, [], Data, Ctx>) =>
256+
this.#action(schema, [], serverCodeFn),
257257
};
258258
}
259259
}

‎website/docs/getting-started.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ This is a basic client, without any options or middleware functions. If you want
4747

4848
### 2. Define a new action
4949

50-
This is how a safe action is created. Providing a validation input schema to the function via `schema()`, we're sure that data that comes in is type safe and validated.
51-
The `define()` method lets you define what happens on the server when the action is called from client, via an async function that receives the parsed input and context as arguments. In short, this is your _server code_. **It never runs on the client**:
50+
This is how a safe action is created. Providing a validation input schema to the function via [`schema()`](docs/safe-action-client/instance-methods#schema), we're sure that data that comes in is type safe and validated.
51+
The [`action()`](/docs/safe-action-client/instance-methods#action) method lets you define what happens on the server when the action is called from client, via an async function that receives the parsed input and context as arguments. In short, this is your _server code_. **It never runs on the client**:
5252

5353
```typescript title="src/app/login-action.ts"
5454
"use server"; // don't forget to add this!
@@ -64,7 +64,7 @@ const schema = z.object({
6464

6565
export const loginUser = actionClient
6666
.schema(schema)
67-
.define(async ({ parsedInput: { username, password } }) => {
67+
.action(async ({ parsedInput: { username, password } }) => {
6868
if (username === "johndoe" && password === "123456") {
6969
return {
7070
success: "Successfully logged in",
@@ -75,7 +75,7 @@ export const loginUser = actionClient
7575
});
7676
```
7777

78-
`action` returns a new function that can be called from the client.
78+
`action` returns a function that can be called from the client.
7979

8080
### 3. Import and execute the action
8181

‎website/docs/integrations/react-hook-form.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { schema } from "./validation";
3737

3838
export const buyProduct = actionClient
3939
.schema(schema)
40-
.define(async ({ parsedInput: { productId } }) => {
40+
.action(async ({ parsedInput: { productId } }) => {
4141
// We're just returning the productId passed to the action here.
4242
return {
4343
productId,

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ use<const ClientInput, const NextCtx>(middlewareFn: MiddlewareFn<ClientInput, Ct
2929
metadata(data: ActionMetadata) => { schema() }
3030
```
3131

32-
`metadata` expects an object of type [`ActionMetadata`](/docs/types#actionmetadata) that lets you specify useful data about the safe action you're defining, and it returns the [`schema`](#schema) method, since metadata is action specific and not shared with other actions. As of now, the only data you can pass in is the `actionName`, but that could be extended in the future. You can then access it in the `middlewareFn` passed to [`use`](#use) and in [`serverCodeFn`](#servercodefn) passed to [`define`](#define).
32+
`metadata` expects an object of type [`ActionMetadata`](/docs/types#actionmetadata) that lets you specify useful data about the safe action you're defining, and it returns the [`schema`](#schema) method, since metadata is action specific and not shared with other actions. As of now, the only data you can pass in is the `actionName`, but that could be extended in the future. You can then access it in the `middlewareFn` passed to [`use`](#use) and in [`serverCodeFn`](#servercodefn) passed to [`action`](#action).
3333

3434
## `schema`
3535

3636
```typescript
37-
schema<const S extends Schema>(schema: S) => { define(), bindArgsSchemas() }
37+
schema<const S extends Schema>(schema: S) => { action(), bindArgsSchemas() }
3838
```
3939

40-
`schema` accepts an input schema of type `Schema` (from TypeSchema), which is used to define the arguments that the safe action will receive, and returns the [`define`](#define) and [`bindArgsSchemas`](#bindargsschemas) methods, which allows you, respectively, to define a new action using that input schema or extend the arguments with additional bound ones.
40+
`schema` accepts an input schema of type `Schema` (from TypeSchema), which is used to define the arguments that the safe action will receive, and returns the [`action`](#action) and [`bindArgsSchemas`](#bindargsschemas) methods, which allows you, respectively, to define a new action using that input schema or extend the arguments with additional bound ones.
4141

4242
## `bindArgsSchemas`
4343

4444
```typescript
45-
bindArgsSchemas<const BAS extends Schema[]>(bindArgsSchemas: BAS) => { define() }
45+
bindArgsSchemas<const BAS extends Schema[]>(bindArgsSchemas: BAS) => { action() }
4646
```
4747

48-
`bindArgsSchemas` accepts an array of bind input schemas of type `Schema` (from TypeSchema), which is used to define the bind arguments that the safe action will receive, and returns the [`define`](#define) method, which allows you, to define a new action using the input and bind inputs schemas.
48+
`bindArgsSchemas` accepts an array of bind input schemas of type `Schema` (from TypeSchema), which is used to define the bind arguments that the safe action will receive, and returns the [`action`](#action) method, which allows you, to define a new action using the input and bind inputs schemas.
4949

50-
## `define`
50+
## `action`
5151

5252
```typescript
53-
define<const Data = null>(serverCodeFn: ServerCodeFn<S, Data, Ctx>) => SafeActionFn<S, Data>
53+
action<const Data = null>(serverCodeFn: ServerCodeFn<S, Data, Ctx>) => SafeActionFn<S, Data>
5454
```
5555

56-
`define` 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.
56+
`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.
5757

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

‎website/docs/usage/bind-arguments.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const onboardUser = action
2929
z.string().uuid(),
3030
z.number().min(18).max(150),
3131
])
32-
.define(
32+
.action(
3333
async ({
3434
parsedInput: { username },
3535
bindArgsParsedInputs: [userId, age],

‎website/docs/usage/client-components/hooks/useaction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const schema = z.object({
2323

2424
export const greetUser = actionClient
2525
.schema(schema)
26-
.define(async ({ parsedInput: { name } }) => {
26+
.action(async ({ parsedInput: { name } }) => {
2727
return { message: `Hello ${name}!` };
2828
});
2929
```

‎website/docs/usage/client-components/hooks/useoptimisticaction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const getLikes = () => likes;
3131

3232
export const addLikes = actionClient
3333
.schema(schema)
34-
.define(async ({ parsedInput: { amount } }) => {
34+
.action(async ({ parsedInput: { amount } }) => {
3535
await new Promise((resolve) => setTimeout(resolve, 1000));
3636

3737
// Mutate data in fake db. This would be a database call in the real world.

‎website/docs/usage/custom-validation-errors.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { action } from "@/lib/safe-action";
4141
// Here we're using the same schema declared above.
4242
const signupAction = actionClient
4343
.schema(schema)
44-
.define(async ({ parsedInput: { email } }) => {
44+
.action(async ({ parsedInput: { email } }) => {
4545
// Assume this is a database call.
4646
if (!isEmailAvailable(email)) {
4747
returnValidationErrors(schema, {

‎website/docs/usage/forms.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const schema = zfd.formData({
2828

2929
export const signup = action
3030
.schema(schema)
31-
.define(async ({ email, password }) => {
31+
.action(async ({ email, password }) => {
3232
console.log("Email:", email, "Password:", password);
3333
// Do something useful here.
3434
});

‎website/docs/usage/middleware.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const editProfile = authActionClient
8989
// Here we pass the input schema.
9090
.schema(z.object({ newUsername: z.string() }))
9191
// Here we get `userId` from the middleware defined in `authActionClient`.
92-
.define(async ({ parsedInput: { newUsername }, ctx: { userId } }) => {
92+
.action(async ({ parsedInput: { newUsername }, ctx: { userId } }) => {
9393
await saveNewUsernameInDb(userId, newUsername);
9494

9595
return {
@@ -141,7 +141,7 @@ const deleteUser = authActionClient
141141
})
142142
.metadata({ actionName: "deleteUser" })
143143
.schema(z.void())
144-
.define(async ({ ctx: { userId } }) => {
144+
.action(async ({ ctx: { userId } }) => {
145145
// Here we're sure that the user that is performing this operation is an admin.
146146
await deleteUserFromDb(userId);
147147
});

0 commit comments

Comments
 (0)
Please sign in to comment.