Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84309be

Browse files
committed
docs: improve action docs
1 parentd0ced5e commit84309be

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

‎packages/action/README.md‎

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
Davstack Action is a simple and flexible library for building Next.js Server actions.
44

5-
It is designed to work seamlessly with React Query, react hook fo
6-
75
###Why Use Davstack Action?
86

9-
- ⚡️ Super Simple API with zero boiler plate
10-
- 🔋 Batteries included - input/output parsing, auth middlewares, file uploads
11-
- 🧩 Flexible - Works well with react query, react hook form, or form actions
12-
- 🏠 Familiar syntax, inspired by tRPC
13-
- ✅ TypeScript-first - inputs, outputs and middleware are inferred
7+
- ✅ Simple and familiar syntax
8+
- ✅ Input/output parsing
9+
- ✅ Auth in middleware
10+
- ✅ Easy file uploads
11+
- ✅ Works well with Zod, React Query and React-Hook-Form.
12+
- ✅ Strongly interred types
1413

1514
###Installation
1615

@@ -22,6 +21,87 @@ Visit the [DavStack Action Docs](https://davstack.com/action/overview) for more
2221

2322
##Demo Usage
2423

24+
```ts
25+
// api/actions/todo-actions.ts
26+
'use server';
27+
import {authedAction }from'@/lib/action';
28+
import {z }from'zod';
29+
30+
exportconst getTodos=authedAction.query(async ({ctx })=> {
31+
returnctx.db.todo.findMany({
32+
where: {
33+
createdBy: { id:ctx.user.id },
34+
},
35+
});
36+
});
37+
38+
exportconst createTodo=authedAction
39+
.input({ name:z.string().min(1) })
40+
.mutation(async ({ctx,input })=> {
41+
returnctx.db.todo.create({
42+
data: {
43+
name:input.name,
44+
createdBy: { connect: { id:ctx.user.id } },
45+
},
46+
});
47+
});
48+
```
49+
50+
```tsx
51+
'use client';
52+
import {useState }from'react';
53+
import {useQuery,useMutation }from'@tanstack/react-query';
54+
import {createTodo,getTodos }from'@/app/actions/todo';
55+
56+
exportfunction TodosList() {
57+
const {data: todos }=useQuery({
58+
queryKey: ['todos'],
59+
queryFn: ()=>getTodos(),
60+
});
61+
return (
62+
<divclassName="flex flex-col gap-1 py-4">
63+
{todos.map((todo)=> (
64+
<TodoItemkey={todo.id}todo={todo} />
65+
))}
66+
</div>
67+
);
68+
}
69+
70+
function CreateTodoForm() {
71+
const [name, setName]=useState('');
72+
73+
const createTodoMutation=useMutation({
74+
mutationFn:createTodo,
75+
onSuccess: ()=> {
76+
invalidateTodos();
77+
setName('');
78+
},
79+
});
80+
81+
return (
82+
<form
83+
onSubmit={(e)=> {
84+
e.preventDefault();
85+
createTodoMutation.mutate({name });
86+
}}
87+
className="flex"
88+
>
89+
<input
90+
type="text"
91+
placeholder="Enter todo name"
92+
value={name}
93+
onChange={(e)=>setName(e.target.value)}
94+
/>
95+
<buttontype="submit"disabled={createTodoMutation.isPending}>
96+
{createTodoMutation.isPending?'loading':'add'}
97+
</button>
98+
</form>
99+
);
100+
}
101+
```
102+
103+
##Usage Guide
104+
25105
###Defining Actions
26106

27107
Import the public/authed action builders from the action file, and define your actions. You can use the`query` or`mutation` methods to define the action function.

‎packages/action/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"@davstack/action",
3-
"version":"0.0.1",
3+
"version":"0.0.3",
44
"main":"./dist/index.js",
55
"module":"./dist/index.mjs",
66
"types":"./dist/index.d.ts",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp