@evjs/create-app 0.0.17 → 0.0.19
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/package.json +1 -1
- package/templates/basic-csr/ev.config.ts +1 -1
- package/templates/basic-csr/package.json +2 -1
- package/templates/basic-fns-ecma/README.md +28 -0
- package/templates/basic-fns-ecma/index.html +11 -0
- package/templates/basic-fns-ecma/package.json +24 -0
- package/templates/basic-fns-ecma/server.entry.mjs +40 -0
- package/templates/basic-fns-ecma/src/api/messages.server.ts +33 -0
- package/templates/basic-fns-ecma/src/main.tsx +15 -0
- package/templates/basic-fns-ecma/src/pages/__root.tsx +19 -0
- package/templates/basic-fns-ecma/src/pages/home.tsx +76 -0
- package/templates/basic-fns-ecma/swMock.entry.js +85 -0
- package/templates/basic-fns-ecma/tsconfig.json +17 -0
- package/templates/basic-plugins/README.md +20 -0
- package/templates/basic-plugins/ev.config.ts +56 -0
- package/templates/basic-plugins/index.html +11 -0
- package/templates/basic-plugins/package.json +25 -0
- package/templates/basic-plugins/src/main.tsx +15 -0
- package/templates/basic-plugins/src/pages/__root.tsx +13 -0
- package/templates/basic-plugins/src/pages/home.tsx +24 -0
- package/templates/basic-plugins/tsconfig.json +18 -0
- package/templates/basic-server-fns/package.json +2 -1
- package/templates/basic-server-fns/src/main.tsx +4 -1
- package/templates/basic-server-fns/src/pages/__root.tsx +15 -0
- package/templates/basic-server-fns/src/{routes.tsx → pages/home.tsx} +3 -27
- package/templates/basic-server-routes/ev.config.ts +1 -1
- package/templates/basic-server-routes/package.json +2 -1
- package/templates/basic-server-routes/src/main.tsx +4 -1
- package/templates/basic-server-routes/src/pages/__root.tsx +15 -0
- package/templates/basic-server-routes/src/{routes.tsx → pages/home.tsx} +3 -24
- package/templates/complex-routing/package.json +2 -1
- package/templates/configured-server-fns/ev.config.ts +2 -21
- package/templates/configured-server-fns/package.json +2 -2
- package/templates/configured-server-fns/src/main.tsx +4 -1
- package/templates/configured-server-fns/src/pages/__root.tsx +20 -0
- package/templates/configured-server-fns/src/{routes.tsx → pages/home.tsx} +3 -37
- package/templates/server-fns-query/README.md +24 -0
- package/templates/server-fns-query/index.html +11 -0
- package/templates/server-fns-query/package.json +23 -0
- package/templates/server-fns-query/src/api/posts.server.ts +29 -0
- package/templates/server-fns-query/src/api/users.server.ts +49 -0
- package/templates/server-fns-query/src/main.tsx +21 -0
- package/templates/server-fns-query/src/pages/__root.tsx +22 -0
- package/templates/server-fns-query/src/pages/home.tsx +123 -0
- package/templates/server-fns-query/src/pages/search.tsx +64 -0
- package/templates/server-fns-query/src/pages/user.tsx +39 -0
- package/templates/server-fns-query/tsconfig.json +17 -0
- package/templates/sqlite-server-fns/README.md +26 -0
- package/templates/sqlite-server-fns/index.html +11 -0
- package/templates/sqlite-server-fns/package.json +23 -0
- package/templates/sqlite-server-fns/src/api/db.server.ts +174 -0
- package/templates/sqlite-server-fns/src/main.tsx +15 -0
- package/templates/sqlite-server-fns/src/pages/__root.tsx +35 -0
- package/templates/sqlite-server-fns/src/pages/home.tsx +271 -0
- package/templates/sqlite-server-fns/tsconfig.json +17 -0
- package/templates/trpc-server-fns/README.md +23 -0
- package/templates/trpc-server-fns/index.html +11 -0
- package/templates/trpc-server-fns/package.json +26 -0
- package/templates/trpc-server-fns/src/api/trpc.server.ts +33 -0
- package/templates/trpc-server-fns/src/main.tsx +15 -0
- package/templates/trpc-server-fns/src/pages/__root.tsx +32 -0
- package/templates/trpc-server-fns/src/pages/home.tsx +110 -0
- package/templates/trpc-server-fns/src/trpc.ts +16 -0
- package/templates/trpc-server-fns/tsconfig.json +17 -0
- package/templates/websocket-fns/README.md +25 -0
- package/templates/websocket-fns/index.html +11 -0
- package/templates/websocket-fns/package.json +25 -0
- package/templates/websocket-fns/src/api/users.server.ts +31 -0
- package/templates/websocket-fns/src/main.tsx +15 -0
- package/templates/websocket-fns/src/pages/__root.tsx +15 -0
- package/templates/websocket-fns/src/pages/home.tsx +152 -0
- package/templates/websocket-fns/tsconfig.json +17 -0
- package/templates/with-tailwind/ev.config.ts +1 -1
- package/templates/with-tailwind/package.json +2 -1
- package/templates/configured-server-fns/src/env.d.ts +0 -4
- package/templates/configured-server-fns/src/message.txt +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "example-basic-server-routes",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"private": true,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "npx ev dev",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@evjs/cli": "*",
|
|
16
16
|
"@evjs/client": "*",
|
|
17
|
+
"@evjs/ev": "*",
|
|
17
18
|
"@evjs/server": "*",
|
|
18
19
|
"@types/react": "^19.1.8",
|
|
19
20
|
"@types/react-dom": "^19.1.8",
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { createApp } from "@evjs/client";
|
|
2
|
-
import {
|
|
2
|
+
import { rootRoute } from "./pages/__root";
|
|
3
|
+
import { postsRoute } from "./pages/home";
|
|
4
|
+
|
|
5
|
+
const routeTree = rootRoute.addChildren([postsRoute]);
|
|
3
6
|
|
|
4
7
|
const app = createApp({ routeTree });
|
|
5
8
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRootRoute, Outlet } from "@evjs/client";
|
|
2
|
+
|
|
3
|
+
function Root() {
|
|
4
|
+
return (
|
|
5
|
+
<div style={{ fontFamily: "system-ui, sans-serif", padding: "1rem" }}>
|
|
6
|
+
<h1>Route Handlers Example</h1>
|
|
7
|
+
<p style={{ color: "#666" }}>
|
|
8
|
+
REST endpoints powered by <code>route()</code>
|
|
9
|
+
</p>
|
|
10
|
+
<Outlet />
|
|
11
|
+
</div>
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const rootRoute = createRootRoute({ component: Root });
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRoute } from "@evjs/client";
|
|
2
2
|
import { useState } from "react";
|
|
3
|
+
import { rootRoute } from "./__root";
|
|
3
4
|
|
|
4
5
|
// ── Types ──
|
|
5
6
|
|
|
@@ -10,22 +11,6 @@ interface Post {
|
|
|
10
11
|
createdAt: string;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
// ── Root Route ──
|
|
14
|
-
|
|
15
|
-
function Root() {
|
|
16
|
-
return (
|
|
17
|
-
<div style={{ fontFamily: "system-ui, sans-serif", padding: "1rem" }}>
|
|
18
|
-
<h1>Route Handlers Example</h1>
|
|
19
|
-
<p style={{ color: "#666" }}>
|
|
20
|
-
REST endpoints powered by <code>route()</code>
|
|
21
|
-
</p>
|
|
22
|
-
<Outlet />
|
|
23
|
-
</div>
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const rootRoute = createRootRoute({ component: Root });
|
|
28
|
-
|
|
29
14
|
// ── Posts Page ──
|
|
30
15
|
|
|
31
16
|
function PostsPage() {
|
|
@@ -213,14 +198,8 @@ function HealthCheck() {
|
|
|
213
198
|
);
|
|
214
199
|
}
|
|
215
200
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const postsRoute = createRoute({
|
|
201
|
+
export const postsRoute = createRoute({
|
|
219
202
|
getParentRoute: () => rootRoute,
|
|
220
203
|
path: "/",
|
|
221
204
|
component: PostsPage,
|
|
222
205
|
});
|
|
223
|
-
|
|
224
|
-
// ── Route Tree ──
|
|
225
|
-
|
|
226
|
-
export const routeTree = rootRoute.addChildren([postsRoute]);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "example-complex-routing",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"private": true,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "npx ev dev",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@evjs/cli": "*",
|
|
16
16
|
"@evjs/client": "*",
|
|
17
|
+
"@evjs/ev": "*",
|
|
17
18
|
"@evjs/server": "*",
|
|
18
19
|
"@types/react": "^19.1.8",
|
|
19
20
|
"@types/react-dom": "^19.1.8",
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { defineConfig } from "@evjs/cli";
|
|
1
|
+
import { defineConfig } from "@evjs/ev";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Advanced ev.config.ts example.
|
|
6
5
|
*
|
|
7
|
-
* This file demonstrates
|
|
6
|
+
* This file demonstrates the available configuration options.
|
|
8
7
|
* All fields are optional — evjs works out of the box.
|
|
9
8
|
*/
|
|
10
9
|
export default defineConfig({
|
|
@@ -37,22 +36,4 @@ export default defineConfig({
|
|
|
37
36
|
port: 4001,
|
|
38
37
|
},
|
|
39
38
|
},
|
|
40
|
-
|
|
41
|
-
// Build-time plugins
|
|
42
|
-
plugins: [
|
|
43
|
-
{
|
|
44
|
-
name: "example-txt-plugin",
|
|
45
|
-
setup() {
|
|
46
|
-
return {
|
|
47
|
-
// Type-safe webpack config via helper
|
|
48
|
-
bundler: webpack((config) => {
|
|
49
|
-
config.module?.rules?.push({
|
|
50
|
-
test: /\.txt$/,
|
|
51
|
-
use: "raw-loader",
|
|
52
|
-
});
|
|
53
|
-
}),
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
],
|
|
58
39
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "example-configured-server-fns",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"private": true,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "npx ev dev",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@evjs/cli": "*",
|
|
16
16
|
"@evjs/client": "*",
|
|
17
|
+
"@evjs/ev": "*",
|
|
17
18
|
"@evjs/server": "*",
|
|
18
19
|
"@types/react": "^19.1.8",
|
|
19
20
|
"@types/react-dom": "^19.1.8",
|
|
20
|
-
"raw-loader": "^4.0.2",
|
|
21
21
|
"typescript": "^6.0.2"
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { createApp } from "@evjs/client";
|
|
2
|
-
import {
|
|
2
|
+
import { rootRoute } from "./pages/__root";
|
|
3
|
+
import { usersRoute } from "./pages/home";
|
|
4
|
+
|
|
5
|
+
const routeTree = rootRoute.addChildren([usersRoute]);
|
|
3
6
|
|
|
4
7
|
const app = createApp({ routeTree });
|
|
5
8
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createAppRootRoute, Link, Outlet } from "@evjs/client";
|
|
2
|
+
|
|
3
|
+
function Root() {
|
|
4
|
+
return (
|
|
5
|
+
<div style={{ fontFamily: "system-ui, sans-serif", padding: "2rem" }}>
|
|
6
|
+
<h1>Configured Server Functions</h1>
|
|
7
|
+
<p style={{ color: "#666" }}>
|
|
8
|
+
Custom <code>ev.config.ts</code> with server function queries.
|
|
9
|
+
</p>
|
|
10
|
+
<nav style={{ marginBottom: "1rem" }}>
|
|
11
|
+
<Link to="/" style={{ textDecoration: "none", fontWeight: "bold" }}>
|
|
12
|
+
Users
|
|
13
|
+
</Link>
|
|
14
|
+
</nav>
|
|
15
|
+
<Outlet />
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const rootRoute = createAppRootRoute({ component: Root });
|
|
@@ -1,45 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
-
createAppRootRoute,
|
|
3
2
|
createRoute,
|
|
4
3
|
getFnQueryKey,
|
|
5
|
-
Link,
|
|
6
|
-
Outlet,
|
|
7
4
|
useMutation,
|
|
8
5
|
useQuery,
|
|
9
6
|
useQueryClient,
|
|
10
7
|
} from "@evjs/client";
|
|
11
8
|
import { useState } from "react";
|
|
12
|
-
import { createUser, getUsers } from "
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
// ── Root Route ──
|
|
16
|
-
|
|
17
|
-
function Root() {
|
|
18
|
-
return (
|
|
19
|
-
<div style={{ fontFamily: "system-ui, sans-serif", padding: "2rem" }}>
|
|
20
|
-
<h1>Configured Server Functions</h1>
|
|
21
|
-
<p style={{ color: "#666" }}>
|
|
22
|
-
Custom <code>ev.config.ts</code> with server function queries.
|
|
23
|
-
</p>
|
|
24
|
-
<nav style={{ marginBottom: "1rem" }}>
|
|
25
|
-
<Link to="/" style={{ textDecoration: "none", fontWeight: "bold" }}>
|
|
26
|
-
Users
|
|
27
|
-
</Link>
|
|
28
|
-
</nav>
|
|
29
|
-
<div
|
|
30
|
-
id="plugin-test"
|
|
31
|
-
style={{ padding: "1rem", background: "#f0f0f0", marginBottom: "1rem" }}
|
|
32
|
-
>
|
|
33
|
-
{messagePluginText}
|
|
34
|
-
</div>
|
|
35
|
-
<Outlet />
|
|
36
|
-
</div>
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const rootRoute = createAppRootRoute({ component: Root });
|
|
41
|
-
|
|
42
|
-
// ── Users Route ──
|
|
9
|
+
import { createUser, getUsers } from "../api/users.server";
|
|
10
|
+
import { rootRoute } from "./__root";
|
|
43
11
|
|
|
44
12
|
function UsersPage() {
|
|
45
13
|
const [name, setName] = useState("");
|
|
@@ -93,10 +61,8 @@ function UsersPage() {
|
|
|
93
61
|
);
|
|
94
62
|
}
|
|
95
63
|
|
|
96
|
-
const usersRoute = createRoute({
|
|
64
|
+
export const usersRoute = createRoute({
|
|
97
65
|
getParentRoute: () => rootRoute,
|
|
98
66
|
path: "/",
|
|
99
67
|
component: UsersPage,
|
|
100
68
|
});
|
|
101
|
-
|
|
102
|
-
export const routeTree = rootRoute.addChildren([usersRoute]);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# server-fns-query
|
|
2
|
+
|
|
3
|
+
Query + mutation patterns with multiple API modules (users + posts).
|
|
4
|
+
|
|
5
|
+
## Run
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm run dev -w example-server-fns-query
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Key Files
|
|
12
|
+
|
|
13
|
+
| File | Purpose |
|
|
14
|
+
|------|---------|
|
|
15
|
+
| `src/routes.tsx` | Multi-resource page with `useQuery` / `useMutation` |
|
|
16
|
+
| `src/api/users.server.ts` | User CRUD |
|
|
17
|
+
| `src/api/posts.server.ts` | Post CRUD |
|
|
18
|
+
|
|
19
|
+
## What It Demonstrates
|
|
20
|
+
|
|
21
|
+
- `useQuery(getUsers)` / `useMutation({ mutationFn })` with auto-generated query keys
|
|
22
|
+
- `getUsers.queryKey()` for cache invalidation on mutation success
|
|
23
|
+
- Multi-arg server functions: `useQuery(searchUsers, name, email)`
|
|
24
|
+
- Error handling with `ServerFunctionError`
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "example-server-fns-query",
|
|
3
|
+
"version": "0.0.19",
|
|
4
|
+
"private": true,
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "npx ev dev",
|
|
7
|
+
"build": "npx ev build",
|
|
8
|
+
"check-types": "tsc --noEmit"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"react": "^19.2.4",
|
|
12
|
+
"react-dom": "^19.2.4"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@evjs/cli": "*",
|
|
16
|
+
"@evjs/client": "*",
|
|
17
|
+
"@evjs/ev": "*",
|
|
18
|
+
"@evjs/server": "*",
|
|
19
|
+
"@types/react": "^19.1.8",
|
|
20
|
+
"@types/react-dom": "^19.1.8",
|
|
21
|
+
"typescript": "^6.0.2"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
/** Simulated posts database. */
|
|
4
|
+
const posts = [
|
|
5
|
+
{
|
|
6
|
+
id: "101",
|
|
7
|
+
title: "Hello Webpack",
|
|
8
|
+
content: "Building a custom evjs plugin",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
id: "102",
|
|
12
|
+
title: "Server Functions",
|
|
13
|
+
content: "Zero config RPC is awesome",
|
|
14
|
+
},
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/** Get all posts. */
|
|
18
|
+
export async function getPosts() {
|
|
19
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
20
|
+
return posts;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Create a new post. */
|
|
24
|
+
export async function createPost(data: { title: string; content: string }) {
|
|
25
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
26
|
+
const newPost = { id: String(posts.length + 101), ...data };
|
|
27
|
+
posts.push(newPost);
|
|
28
|
+
return newPost;
|
|
29
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
import { ServerError } from "@evjs/server";
|
|
4
|
+
|
|
5
|
+
/** Simulated user database. */
|
|
6
|
+
const users = [
|
|
7
|
+
{ id: "1", name: "Alice", email: "alice@example.com" },
|
|
8
|
+
{ id: "2", name: "Bob", email: "bob@example.com" },
|
|
9
|
+
{ id: "3", name: "Charlie", email: "charlie@example.com" },
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
/** Get all users. */
|
|
13
|
+
export async function getUsers() {
|
|
14
|
+
// Simulate server latency
|
|
15
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
16
|
+
return users;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Get a single user by ID. Throws ServerError if not found. */
|
|
20
|
+
export async function getUser(id: string) {
|
|
21
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
22
|
+
const user = users.find((u) => u.id === id);
|
|
23
|
+
if (!user) {
|
|
24
|
+
throw new ServerError("User not found", { status: 404, data: { id } });
|
|
25
|
+
}
|
|
26
|
+
return user;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Search users by name and email (multi-arg). */
|
|
30
|
+
export async function searchUsers(name: string, email: string) {
|
|
31
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
32
|
+
return users.filter((u) => {
|
|
33
|
+
const matchName = name
|
|
34
|
+
? u.name.toLowerCase().includes(name.toLowerCase())
|
|
35
|
+
: true;
|
|
36
|
+
const matchEmail = email
|
|
37
|
+
? u.email.toLowerCase().includes(email.toLowerCase())
|
|
38
|
+
: true;
|
|
39
|
+
return matchName && matchEmail;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Create a new user. */
|
|
44
|
+
export async function createUser(data: { name: string; email: string }) {
|
|
45
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
46
|
+
const newUser = { id: String(users.length + 1), ...data };
|
|
47
|
+
users.push(newUser);
|
|
48
|
+
return newUser;
|
|
49
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createApp } from "@evjs/client";
|
|
2
|
+
import { rootRoute } from "./pages/__root";
|
|
3
|
+
import { usersRoute } from "./pages/home";
|
|
4
|
+
import { searchRoute } from "./pages/search";
|
|
5
|
+
import { userDetailRoute } from "./pages/user";
|
|
6
|
+
|
|
7
|
+
const routeTree = rootRoute.addChildren([
|
|
8
|
+
usersRoute,
|
|
9
|
+
searchRoute,
|
|
10
|
+
userDetailRoute,
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
const app = createApp({ routeTree });
|
|
14
|
+
|
|
15
|
+
declare module "@tanstack/react-router" {
|
|
16
|
+
interface Register {
|
|
17
|
+
router: typeof app.router;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
app.render("#app");
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createAppRootRoute, Link, Outlet } from "@evjs/client";
|
|
2
|
+
|
|
3
|
+
function Root() {
|
|
4
|
+
return (
|
|
5
|
+
<div style={{ fontFamily: "system-ui, sans-serif", padding: "1rem" }}>
|
|
6
|
+
<h1>Server Functions Example</h1>
|
|
7
|
+
<nav style={{ display: "flex", gap: "1rem", marginBottom: "1rem" }}>
|
|
8
|
+
<Link to="/">Users</Link>
|
|
9
|
+
<Link to="/search">Search</Link>
|
|
10
|
+
<Link to="/user/$userId" params={{ userId: "1" }}>
|
|
11
|
+
User #1
|
|
12
|
+
</Link>
|
|
13
|
+
<Link to="/user/$userId" params={{ userId: "999" }}>
|
|
14
|
+
User #999 (error)
|
|
15
|
+
</Link>
|
|
16
|
+
</nav>
|
|
17
|
+
<Outlet />
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const rootRoute = createAppRootRoute({ component: Root });
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRoute,
|
|
3
|
+
getFnQueryKey,
|
|
4
|
+
useMutation,
|
|
5
|
+
useQuery,
|
|
6
|
+
useQueryClient,
|
|
7
|
+
} from "@evjs/client";
|
|
8
|
+
import { useState } from "react";
|
|
9
|
+
import { createPost, getPosts } from "../api/posts.server";
|
|
10
|
+
import { createUser, getUsers } from "../api/users.server";
|
|
11
|
+
import { rootRoute } from "./__root";
|
|
12
|
+
|
|
13
|
+
function UsersPage() {
|
|
14
|
+
const [name, setName] = useState("");
|
|
15
|
+
const [email, setEmail] = useState("");
|
|
16
|
+
|
|
17
|
+
const { data: users = [], isLoading: isLoadingUsers } = useQuery(getUsers);
|
|
18
|
+
|
|
19
|
+
const queryClient = useQueryClient();
|
|
20
|
+
const { mutateAsync: createUserMutation } = useMutation({
|
|
21
|
+
mutationFn: createUser,
|
|
22
|
+
onSuccess: () => {
|
|
23
|
+
queryClient.invalidateQueries({ queryKey: getFnQueryKey(getUsers) });
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
async function handleCreateUser(e: { preventDefault: () => void }) {
|
|
28
|
+
e.preventDefault();
|
|
29
|
+
if (!name || !email) return;
|
|
30
|
+
await createUserMutation({ name, email });
|
|
31
|
+
setName("");
|
|
32
|
+
setEmail("");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Posts State
|
|
36
|
+
const [title, setTitle] = useState("");
|
|
37
|
+
const [content, setContent] = useState("");
|
|
38
|
+
|
|
39
|
+
const { data: posts = [], isLoading: isLoadingPosts } = useQuery(getPosts);
|
|
40
|
+
|
|
41
|
+
const { mutateAsync: createPostMutation } = useMutation({
|
|
42
|
+
mutationFn: createPost,
|
|
43
|
+
onSuccess: () => {
|
|
44
|
+
queryClient.invalidateQueries({ queryKey: getFnQueryKey(getPosts) });
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
async function handleCreatePost(e: { preventDefault: () => void }) {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
if (!title || !content) return;
|
|
51
|
+
await createPostMutation({ title, content });
|
|
52
|
+
setTitle("");
|
|
53
|
+
setContent("");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (isLoadingUsers || isLoadingPosts) return <p>Loading data from server…</p>;
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div>
|
|
60
|
+
<h2>Users</h2>
|
|
61
|
+
<ul id="user-list">
|
|
62
|
+
{users.map((u) => (
|
|
63
|
+
<li key={u.id}>
|
|
64
|
+
{u.name} — {u.email}
|
|
65
|
+
</li>
|
|
66
|
+
))}
|
|
67
|
+
</ul>
|
|
68
|
+
|
|
69
|
+
<h3>Add User</h3>
|
|
70
|
+
<form
|
|
71
|
+
onSubmit={handleCreateUser}
|
|
72
|
+
style={{ display: "flex", gap: "0.5rem", marginBottom: "2rem" }}
|
|
73
|
+
>
|
|
74
|
+
<input
|
|
75
|
+
placeholder="Name"
|
|
76
|
+
value={name}
|
|
77
|
+
onChange={(e) => setName(e.target.value)}
|
|
78
|
+
/>
|
|
79
|
+
<input
|
|
80
|
+
placeholder="Email"
|
|
81
|
+
value={email}
|
|
82
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
83
|
+
/>
|
|
84
|
+
<button type="submit">Create User</button>
|
|
85
|
+
</form>
|
|
86
|
+
|
|
87
|
+
<hr style={{ margin: "2rem 0", borderColor: "#eee" }} />
|
|
88
|
+
|
|
89
|
+
<h2>Posts</h2>
|
|
90
|
+
<ul>
|
|
91
|
+
{posts.map((p) => (
|
|
92
|
+
<li key={p.id}>
|
|
93
|
+
<strong>{p.title}</strong> — {p.content}
|
|
94
|
+
</li>
|
|
95
|
+
))}
|
|
96
|
+
</ul>
|
|
97
|
+
|
|
98
|
+
<h3>Add Post</h3>
|
|
99
|
+
<form
|
|
100
|
+
onSubmit={handleCreatePost}
|
|
101
|
+
style={{ display: "flex", gap: "0.5rem" }}
|
|
102
|
+
>
|
|
103
|
+
<input
|
|
104
|
+
placeholder="Title"
|
|
105
|
+
value={title}
|
|
106
|
+
onChange={(e) => setTitle(e.target.value)}
|
|
107
|
+
/>
|
|
108
|
+
<input
|
|
109
|
+
placeholder="Content"
|
|
110
|
+
value={content}
|
|
111
|
+
onChange={(e) => setContent(e.target.value)}
|
|
112
|
+
/>
|
|
113
|
+
<button type="submit">Create Post</button>
|
|
114
|
+
</form>
|
|
115
|
+
</div>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export const usersRoute = createRoute({
|
|
120
|
+
getParentRoute: () => rootRoute,
|
|
121
|
+
path: "/",
|
|
122
|
+
component: UsersPage,
|
|
123
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createRoute, useQuery } from "@evjs/client";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { searchUsers } from "../api/users.server";
|
|
4
|
+
import { rootRoute } from "./__root";
|
|
5
|
+
|
|
6
|
+
function SearchPage() {
|
|
7
|
+
const [searchName, setSearchName] = useState("");
|
|
8
|
+
const [searchEmail, setSearchEmail] = useState("");
|
|
9
|
+
|
|
10
|
+
const { data: results, isLoading } = useQuery(
|
|
11
|
+
searchUsers,
|
|
12
|
+
searchName || "",
|
|
13
|
+
searchEmail || "",
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
function handleSearch(e: { preventDefault: () => void }) {
|
|
17
|
+
e.preventDefault();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<div>
|
|
22
|
+
<h2>Search Users (multi-arg)</h2>
|
|
23
|
+
<form
|
|
24
|
+
onSubmit={handleSearch}
|
|
25
|
+
style={{ display: "flex", gap: "0.5rem", marginBottom: "1rem" }}
|
|
26
|
+
>
|
|
27
|
+
<input
|
|
28
|
+
placeholder="Name"
|
|
29
|
+
id="search-name"
|
|
30
|
+
value={searchName}
|
|
31
|
+
onChange={(e) => setSearchName(e.target.value)}
|
|
32
|
+
/>
|
|
33
|
+
<input
|
|
34
|
+
placeholder="Email"
|
|
35
|
+
id="search-email"
|
|
36
|
+
value={searchEmail}
|
|
37
|
+
onChange={(e) => setSearchEmail(e.target.value)}
|
|
38
|
+
/>
|
|
39
|
+
<button type="submit">Search</button>
|
|
40
|
+
</form>
|
|
41
|
+
|
|
42
|
+
{isLoading && <p>Searching…</p>}
|
|
43
|
+
|
|
44
|
+
{results && (
|
|
45
|
+
<div id="search-results">
|
|
46
|
+
<p>Found {results.length} result(s)</p>
|
|
47
|
+
<ul>
|
|
48
|
+
{results.map((u) => (
|
|
49
|
+
<li key={u.id}>
|
|
50
|
+
{u.name} — {u.email}
|
|
51
|
+
</li>
|
|
52
|
+
))}
|
|
53
|
+
</ul>
|
|
54
|
+
</div>
|
|
55
|
+
)}
|
|
56
|
+
</div>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const searchRoute = createRoute({
|
|
61
|
+
getParentRoute: () => rootRoute,
|
|
62
|
+
path: "/search",
|
|
63
|
+
component: SearchPage,
|
|
64
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createRoute, ServerFunctionError, useQuery } from "@evjs/client";
|
|
2
|
+
import { getUser } from "../api/users.server";
|
|
3
|
+
import { rootRoute } from "./__root";
|
|
4
|
+
|
|
5
|
+
function UserDetailPage() {
|
|
6
|
+
const { userId } = userDetailRoute.useParams();
|
|
7
|
+
const { data: user, error, isLoading } = useQuery(getUser, userId);
|
|
8
|
+
|
|
9
|
+
if (isLoading) return <p>Loading user…</p>;
|
|
10
|
+
|
|
11
|
+
if (error) {
|
|
12
|
+
const isServerError = error instanceof ServerFunctionError;
|
|
13
|
+
return (
|
|
14
|
+
<div id="user-error">
|
|
15
|
+
<h2>Error Loading User</h2>
|
|
16
|
+
<p id="error-message">{error.message}</p>
|
|
17
|
+
{isServerError && (
|
|
18
|
+
<p id="error-type">ServerFunctionError (status: {error.status})</p>
|
|
19
|
+
)}
|
|
20
|
+
</div>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!user) return null;
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<div id="user-detail">
|
|
28
|
+
<h2>User Detail</h2>
|
|
29
|
+
<p id="user-name">Name: {user.name}</p>
|
|
30
|
+
<p id="user-email">Email: {user.email}</p>
|
|
31
|
+
</div>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const userDetailRoute = createRoute({
|
|
36
|
+
getParentRoute: () => rootRoute,
|
|
37
|
+
path: "/user/$userId",
|
|
38
|
+
component: UserDetailPage,
|
|
39
|
+
});
|