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.
Files changed (3) hide show
  1. package/.nvmrc +1 -0
  2. package/README.md +52 -60
  3. package/package.json +11 -9
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ v20.17.0
package/README.md CHANGED
@@ -1,15 +1,13 @@
1
- # 🚀 Vite Server Actions
1
+ # Vite Server Actions
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/vite-plugin-server-actions.svg?style=flat)](https://www.npmjs.com/package/vite-plugin-server-actions)
4
4
  [![Downloads](https://img.shields.io/npm/dm/vite-plugin-server-actions.svg?style=flat)](https://www.npmjs.com/package/vite-plugin-server-actions)
5
- [![Build Status](https://img.shields.io/github/workflow/status/HelgeSverre/vite-plugin-server-actions/CI)](https://github.com/HelgeSverre/vite-plugin-server-actions/actions)
6
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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
- **Vite Server Actions** is a Vite plugin that makes it easy to create functions (actions) that runs on the server, while
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
- # Install using yarn
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
- import {defineConfig} from "vite";
37
- import serverActions from "helgesverre/vite-plugin-server-actions";
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: [serverActions()],
41
+ plugins: [
42
+ // Add the plugin
43
+ serverActions(),
44
+ ],
41
45
  });
42
46
  ```
43
47
 
44
- 3. Create a `[whatever].server.js` file anywhere in your project:
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
- ## 🤯 How it works
117
-
118
- Vite Server Actions works by creating an API endpoint for each server function you define.
119
-
120
- When you import a server action in your client-side code, Vite Server Actions will intercept the import and return a
121
- proxy function that sends a request to the server endpoint instead of executing the function locally.
122
-
123
- In development mode, the server is run as a middleware in the Vite dev server, while in production mode, the server is
124
- bundled into a single file that can be run with Node.js.
125
-
126
- ### Sequence Diagram
127
-
128
- ```mermaid
129
- sequenceDiagram
130
- participant Client
131
- participant Vite Dev Server
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
- TODO: Add configuration options and descriptions
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
- ## 🤝 Contributing
165
+ ## 🧑‍💻 Development Setup
186
166
 
187
- Contributions, issues, and feature requests are welcome! Feel free to
188
- check [issues page](https://github.com/helgesverre/vite-plugin-server-actions/issues).
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
- "bugs": {
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
  }