vite-plugin-server-actions 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.nvmrc +1 -0
- package/README.md +52 -60
- package/package.json +11 -9
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v20.17.0
|
package/README.md
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ⚡ Vite Server Actions
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/vite-plugin-server-actions)
|
|
4
4
|
[](https://www.npmjs.com/package/vite-plugin-server-actions)
|
|
5
|
-
[](https://github.com/HelgeSverre/vite-plugin-server-actions/actions)
|
|
6
5
|
[](https://opensource.org/licenses/MIT)
|
|
7
6
|
|
|
8
7
|
> 🚧 **Experimental:** This is currently a proof of concept. Use at your own risk.
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
allowing you to call them from the client-side as if they were local functions.
|
|
9
|
+
**Vite Server Actions** is a Vite plugin that enables you to create server-side functions and call them from your
|
|
10
|
+
client-side code as if they were local functions.
|
|
13
11
|
|
|
14
12
|
## ✨ Features
|
|
15
13
|
|
|
@@ -26,22 +24,30 @@ allowing you to call them from the client-side as if they were local functions.
|
|
|
26
24
|
# Install using npm
|
|
27
25
|
npm install vite-plugin-server-actions
|
|
28
26
|
|
|
29
|
-
#
|
|
27
|
+
# Or using yarn
|
|
30
28
|
yarn add vite-plugin-server-actions
|
|
31
29
|
```
|
|
32
30
|
|
|
33
|
-
2. Add to your `vite.config.js
|
|
31
|
+
2. Add it to your `vite.config.js` file ([example](examples/todo-app/vite.config.js)):
|
|
34
32
|
|
|
35
33
|
```javascript
|
|
36
|
-
|
|
37
|
-
import
|
|
34
|
+
// vite.config.js
|
|
35
|
+
import { defineConfig } from "vite";
|
|
36
|
+
|
|
37
|
+
// Import the plugin
|
|
38
|
+
import serverActions from "vite-plugin-server-actions";
|
|
38
39
|
|
|
39
40
|
export default defineConfig({
|
|
40
|
-
plugins: [
|
|
41
|
+
plugins: [
|
|
42
|
+
// Add the plugin
|
|
43
|
+
serverActions(),
|
|
44
|
+
],
|
|
41
45
|
});
|
|
42
46
|
```
|
|
43
47
|
|
|
44
|
-
|
|
48
|
+
2. Create a server action file (e.g., `todo.server.js`):
|
|
49
|
+
|
|
50
|
+
You can put it anywhere in your project, but it has to end with `.server.js`.
|
|
45
51
|
|
|
46
52
|
```javascript
|
|
47
53
|
// ex: src/actions/todo.server.js
|
|
@@ -88,12 +94,12 @@ export async function listTodos() {
|
|
|
88
94
|
await saveTodoToJsonFile({ id: Math.random(), text: newTodoText });
|
|
89
95
|
newTodoText = "";
|
|
90
96
|
await fetchTodos();
|
|
91
|
-
|
|
97
|
+
}
|
|
92
98
|
|
|
93
99
|
async function removeTodo(id) {
|
|
94
100
|
await deleteTodoById(id);
|
|
95
101
|
await fetchTodos();
|
|
96
|
-
|
|
102
|
+
}
|
|
97
103
|
|
|
98
104
|
fetchTodos();
|
|
99
105
|
</script>
|
|
@@ -113,44 +119,26 @@ export async function listTodos() {
|
|
|
113
119
|
</div>
|
|
114
120
|
```
|
|
115
121
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
participant Plugin Middleware
|
|
133
|
-
participant Server Function
|
|
134
|
-
participant File System
|
|
135
|
-
Client ->> Vite Dev Server: import { addTodo } from './server/todos.server.js'
|
|
136
|
-
Vite Dev Server ->> Client: Returns proxied function
|
|
137
|
-
Client ->> Client: Call addTodo({ text: 'New todo' })
|
|
138
|
-
Client ->> Vite Dev Server: POST /api/todos/addTodo
|
|
139
|
-
Vite Dev Server ->> Plugin Middleware: Handle POST request
|
|
140
|
-
Plugin Middleware ->> Server Function: Call addTodo function
|
|
141
|
-
Server Function ->> File System: Read todos.json
|
|
142
|
-
File System ->> Server Function: Return current todos
|
|
143
|
-
Server Function ->> Server Function: Add new todo
|
|
144
|
-
Server Function ->> File System: Write updated todos.json
|
|
145
|
-
File System ->> Server Function: Write confirmation
|
|
146
|
-
Server Function ->> Plugin Middleware: Return new todo
|
|
147
|
-
Plugin Middleware ->> Vite Dev Server: Send JSON response
|
|
148
|
-
Vite Dev Server ->> Client: Return new todo data
|
|
149
|
-
```
|
|
122
|
+
That's it! Your server actions are now ready to use. 🎉
|
|
123
|
+
|
|
124
|
+
## 📝 Examples
|
|
125
|
+
|
|
126
|
+
To see a real-world example of how to use Vite Server Actions, check out the TODO app example:
|
|
127
|
+
|
|
128
|
+
- [TODO App Example](examples/todo-app/README.md)
|
|
129
|
+
|
|
130
|
+
## 📚 How it works
|
|
131
|
+
|
|
132
|
+
**Vite Server Actions** creates an API endpoint for each server function you define. When you import a server action in
|
|
133
|
+
your client-side code, it returns a proxy function) that sends a request to the server endpoint instead of executing the
|
|
134
|
+
function locally.
|
|
135
|
+
|
|
136
|
+
In _development_, the server actions run as a middleware in the Vite dev server.
|
|
137
|
+
While in _production_, it's bundled into a single file that can be run with Node.js.
|
|
150
138
|
|
|
151
139
|
## 🔧 Configuration
|
|
152
140
|
|
|
153
|
-
Vite Server Actions works out of the box, but you can customize it:
|
|
141
|
+
Vite Server Actions works out of the box, but you can customize it by passing options to the plugin:
|
|
154
142
|
|
|
155
143
|
```javascript
|
|
156
144
|
serverActions({
|
|
@@ -160,15 +148,7 @@ serverActions({
|
|
|
160
148
|
|
|
161
149
|
## 🛠️ Configuration Options
|
|
162
150
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
| Option | Type | Default | Description |
|
|
166
|
-
|------------------|----------------------------------------|-------------|----------------------------------|
|
|
167
|
-
| logLevel | 'error' \| 'warn' \| 'info' \| 'debug' | 'info' | Server log level |
|
|
168
|
-
| serverPath | string | '/api' | Base path for server endpoints |
|
|
169
|
-
| serverPort | number | 3000 | Port for the server |
|
|
170
|
-
| serverHost | string | 'localhost' | Host for the server |
|
|
171
|
-
| serverMiddleware | (app: Express) => void | - | Custom middleware for the server |
|
|
151
|
+
Coming soon...
|
|
172
152
|
|
|
173
153
|
## TODO
|
|
174
154
|
|
|
@@ -182,10 +162,22 @@ This is a proof of concept, and things are still missing, such as:
|
|
|
182
162
|
- [ ] Add more examples (Vue, React, etc.)
|
|
183
163
|
- [ ] Publish to npm
|
|
184
164
|
|
|
185
|
-
##
|
|
165
|
+
## 🧑💻 Development Setup
|
|
186
166
|
|
|
187
|
-
|
|
188
|
-
|
|
167
|
+
To set up the project for development, follow these steps:
|
|
168
|
+
|
|
169
|
+
```shell
|
|
170
|
+
# Clone the repository
|
|
171
|
+
git clone git@github.com:HelgeSverre/vite-plugin-server-actions.git
|
|
172
|
+
cd vite-plugin-server-actions
|
|
173
|
+
|
|
174
|
+
# Install dependencies
|
|
175
|
+
npm install
|
|
176
|
+
npm run dev
|
|
177
|
+
|
|
178
|
+
# Format code
|
|
179
|
+
npm run format
|
|
180
|
+
```
|
|
189
181
|
|
|
190
182
|
## 📝 License
|
|
191
183
|
|
package/package.json
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-server-actions",
|
|
3
|
+
"version": "0.1.1",
|
|
3
4
|
"description": "Automatically proxy imported backend functions to a server endpoint in Vite",
|
|
4
|
-
"version": "0.1.0",
|
|
5
|
-
"type": "module",
|
|
6
5
|
"keywords": [
|
|
7
6
|
"vite-plugin",
|
|
8
7
|
"server",
|
|
9
8
|
"actions"
|
|
10
9
|
],
|
|
11
|
-
"license": "MIT",
|
|
12
10
|
"homepage": "https://github.com/HelgeSverre/vite-plugin-server-actions",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/HelgeSverre/vite-plugin-server-actions/issues"
|
|
13
|
+
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
15
16
|
"url": "https://github.com/HelgeSverre/vite-plugin-server-actions"
|
|
16
17
|
},
|
|
17
|
-
"
|
|
18
|
-
"url": "https://github.com/HelgeSverre/vite-plugin-server-actions/issues"
|
|
19
|
-
},
|
|
20
|
-
"readme": "README.md",
|
|
18
|
+
"license": "MIT",
|
|
21
19
|
"author": {
|
|
22
20
|
"name": "Helge Sverre",
|
|
23
21
|
"email": "helge.sverre@gmail.com",
|
|
24
22
|
"url": "https://helgesver.re"
|
|
25
23
|
},
|
|
24
|
+
"type": "module",
|
|
26
25
|
"main": "src/index.js",
|
|
27
26
|
"scripts": {
|
|
27
|
+
"example:build": "npm run build --prefix examples/todo-app",
|
|
28
|
+
"example:dev": "npm run dev --prefix examples/todo-app",
|
|
28
29
|
"format": "npx prettier --write src/ README.md",
|
|
29
30
|
"sort": "npx sort-package-json"
|
|
30
31
|
},
|
|
@@ -34,6 +35,7 @@
|
|
|
34
35
|
"vitest": "^0.34.0"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
|
-
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0"
|
|
38
|
-
}
|
|
38
|
+
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0"
|
|
39
|
+
},
|
|
40
|
+
"readme": "README.md"
|
|
39
41
|
}
|