@evjs/create-app 0.0.29 → 0.0.32
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/README.md +7 -4
- package/dist/index.js +11 -12
- package/package.json +1 -1
- package/templates/{basic-server-routes → api-routes}/README.md +2 -2
- package/templates/api-routes/ev.config.ts +16 -0
- package/templates/{basic-plugins → api-routes}/package.json +2 -2
- package/templates/api-routes/src/api/demo.server.ts +5 -0
- package/templates/{basic-server-routes → api-routes}/src/pages/home.tsx +37 -0
- package/templates/{basic-server-routes → api-routes}/src/server.ts +3 -1
- package/templates/basic/README.md +27 -0
- package/templates/{basic-csr → basic}/ev.config.ts +1 -1
- package/templates/{basic-csr → basic}/package.json +2 -2
- package/templates/basic/src/main.tsx +37 -0
- package/templates/basic/src/pages/__root.tsx +49 -0
- package/templates/basic/src/pages/home.tsx +207 -0
- package/templates/complex-routing/README.md +13 -17
- package/templates/complex-routing/index.html +9 -9
- package/templates/complex-routing/package.json +4 -2
- package/templates/complex-routing/src/api/data.server.ts +86 -0
- package/templates/{basic-csr → complex-routing}/src/main.tsx +11 -6
- package/templates/complex-routing/src/pages/__root.tsx +60 -0
- package/templates/complex-routing/src/pages/catch.tsx +26 -0
- package/templates/complex-routing/src/pages/dashboard.tsx +79 -0
- package/templates/complex-routing/src/pages/home.tsx +35 -0
- package/templates/complex-routing/src/pages/posts/index.tsx +100 -0
- package/templates/complex-routing/src/pages/search.tsx +71 -0
- package/templates/complex-routing/src/pages/user.tsx +28 -0
- package/templates/complex-routing/tsconfig.json +6 -11
- package/templates/{websocket-fns → custom-ws-transport}/README.md +2 -2
- package/templates/{websocket-fns → custom-ws-transport}/package.json +2 -2
- package/templates/{basic-mpa → mpa}/README.md +3 -3
- package/templates/{basic-mpa → mpa}/package.json +2 -2
- package/templates/{basic-plugins → plugin-authoring}/README.md +3 -3
- package/templates/{basic-server-fns → plugin-authoring}/package.json +2 -2
- package/templates/{sqlite-server-fns → with-sqlite}/README.md +2 -2
- package/templates/{server-fns-query → with-sqlite}/package.json +2 -2
- package/templates/with-tailwind/package.json +1 -1
- package/templates/{trpc-server-fns → with-trpc}/README.md +2 -2
- package/templates/{trpc-server-fns → with-trpc}/package.json +2 -2
- package/templates/basic-csr/README.md +0 -23
- package/templates/basic-csr/index.html +0 -11
- package/templates/basic-csr/src/pages/__root.tsx +0 -28
- package/templates/basic-csr/src/pages/about.tsx +0 -19
- package/templates/basic-csr/src/pages/home.tsx +0 -19
- package/templates/basic-csr/src/pages/posts/index.tsx +0 -63
- package/templates/basic-fns-ecma/README.md +0 -28
- package/templates/basic-fns-ecma/index.html +0 -11
- package/templates/basic-fns-ecma/package.json +0 -24
- package/templates/basic-fns-ecma/server.entry.mjs +0 -40
- package/templates/basic-fns-ecma/src/api/messages.server.ts +0 -33
- package/templates/basic-fns-ecma/src/main.tsx +0 -15
- package/templates/basic-fns-ecma/src/pages/__root.tsx +0 -19
- package/templates/basic-fns-ecma/src/pages/home.tsx +0 -76
- package/templates/basic-fns-ecma/swMock.entry.js +0 -85
- package/templates/basic-plugins/tsconfig.json +0 -18
- package/templates/basic-server-fns/README.md +0 -24
- package/templates/basic-server-fns/src/pages/__root.tsx +0 -15
- package/templates/basic-server-fns/src/pages/home.tsx +0 -76
- package/templates/basic-server-routes/ev.config.ts +0 -7
- package/templates/basic-server-routes/package.json +0 -23
- package/templates/complex-routing/ev.config.ts +0 -14
- package/templates/complex-routing/src/about/main.tsx +0 -23
- package/templates/complex-routing/src/home/main.tsx +0 -23
- package/templates/configured-server-fns/README.md +0 -25
- package/templates/configured-server-fns/ev.config.ts +0 -39
- package/templates/configured-server-fns/index.html +0 -11
- package/templates/configured-server-fns/package.json +0 -23
- package/templates/configured-server-fns/src/api/users.server.ts +0 -22
- package/templates/configured-server-fns/src/pages/__root.tsx +0 -20
- package/templates/configured-server-fns/src/pages/home.tsx +0 -68
- package/templates/server-fns-query/README.md +0 -24
- package/templates/server-fns-query/src/api/posts.server.ts +0 -29
- package/templates/server-fns-query/src/api/users.server.ts +0 -49
- package/templates/server-fns-query/src/main.tsx +0 -21
- package/templates/server-fns-query/src/pages/__root.tsx +0 -22
- package/templates/server-fns-query/src/pages/home.tsx +0 -123
- package/templates/server-fns-query/src/pages/search.tsx +0 -64
- package/templates/server-fns-query/src/pages/user.tsx +0 -39
- package/templates/sqlite-server-fns/package.json +0 -23
- package/templates/sqlite-server-fns/src/main.tsx +0 -15
- package/templates/sqlite-server-fns/tsconfig.json +0 -17
- package/templates/trpc-server-fns/index.html +0 -11
- package/templates/trpc-server-fns/tsconfig.json +0 -17
- package/templates/websocket-fns/src/main.tsx +0 -15
- package/templates/websocket-fns/tsconfig.json +0 -17
- /package/templates/{basic-server-routes → api-routes}/index.html +0 -0
- /package/templates/{basic-server-routes → api-routes}/src/api/health.routes.ts +0 -0
- /package/templates/{basic-server-routes → api-routes}/src/api/posts.routes.ts +0 -0
- /package/templates/{basic-server-routes → api-routes}/src/main.tsx +0 -0
- /package/templates/{basic-server-routes → api-routes}/src/pages/__root.tsx +0 -0
- /package/templates/{basic-fns-ecma → api-routes}/tsconfig.json +0 -0
- /package/templates/{basic-server-fns → basic}/index.html +0 -0
- /package/templates/{basic-server-fns → basic}/src/api/users.server.ts +0 -0
- /package/templates/{basic-server-fns → basic}/tsconfig.json +0 -0
- /package/templates/{websocket-fns → custom-ws-transport}/index.html +0 -0
- /package/templates/{websocket-fns → custom-ws-transport}/src/api/users.server.ts +0 -0
- /package/templates/{basic-server-fns → custom-ws-transport}/src/main.tsx +0 -0
- /package/templates/{websocket-fns → custom-ws-transport}/src/pages/__root.tsx +0 -0
- /package/templates/{websocket-fns → custom-ws-transport}/src/pages/home.tsx +0 -0
- /package/templates/{basic-server-routes → custom-ws-transport}/tsconfig.json +0 -0
- /package/templates/{basic-mpa → mpa}/ev.config.ts +0 -0
- /package/templates/{basic-mpa → mpa}/index.html +0 -0
- /package/templates/{basic-mpa → mpa}/src/about/main.tsx +0 -0
- /package/templates/{basic-mpa → mpa}/src/home/main.tsx +0 -0
- /package/templates/{basic-csr → mpa}/tsconfig.json +0 -0
- /package/templates/{basic-plugins → plugin-authoring}/ev.config.ts +0 -0
- /package/templates/{basic-plugins → plugin-authoring}/index.html +0 -0
- /package/templates/{basic-plugins → plugin-authoring}/src/main.tsx +0 -0
- /package/templates/{basic-plugins → plugin-authoring}/src/pages/__root.tsx +0 -0
- /package/templates/{basic-plugins → plugin-authoring}/src/pages/home.tsx +0 -0
- /package/templates/{basic-mpa → plugin-authoring}/tsconfig.json +0 -0
- /package/templates/{sqlite-server-fns → with-sqlite}/index.html +0 -0
- /package/templates/{sqlite-server-fns → with-sqlite}/src/api/db.server.ts +0 -0
- /package/templates/{configured-server-fns → with-sqlite}/src/main.tsx +0 -0
- /package/templates/{sqlite-server-fns → with-sqlite}/src/pages/__root.tsx +0 -0
- /package/templates/{sqlite-server-fns → with-sqlite}/src/pages/home.tsx +0 -0
- /package/templates/{configured-server-fns → with-sqlite}/tsconfig.json +0 -0
- /package/templates/{server-fns-query → with-trpc}/index.html +0 -0
- /package/templates/{trpc-server-fns → with-trpc}/src/api/trpc.server.ts +0 -0
- /package/templates/{trpc-server-fns → with-trpc}/src/main.tsx +0 -0
- /package/templates/{trpc-server-fns → with-trpc}/src/pages/__root.tsx +0 -0
- /package/templates/{trpc-server-fns → with-trpc}/src/pages/home.tsx +0 -0
- /package/templates/{trpc-server-fns → with-trpc}/src/trpc.ts +0 -0
- /package/templates/{server-fns-query → with-trpc}/tsconfig.json +0 -0
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# basic-csr
|
|
2
|
-
|
|
3
|
-
Minimal client-side rendering example — no server functions, just routing.
|
|
4
|
-
|
|
5
|
-
## Run
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm run dev -w example-basic-csr
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Key Files
|
|
12
|
-
|
|
13
|
-
| File | Purpose |
|
|
14
|
-
|------|---------|
|
|
15
|
-
| `src/main.tsx` | App bootstrap with `createApp` |
|
|
16
|
-
| `src/pages/` | Route components (Home, Posts) |
|
|
17
|
-
|
|
18
|
-
## What It Demonstrates
|
|
19
|
-
|
|
20
|
-
- `createApp` + `createRoute` from `@evjs/client`
|
|
21
|
-
- Client-side routing with TanStack Router
|
|
22
|
-
- Dynamic route params (`$postId`)
|
|
23
|
-
- No server functions — pure CSR
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { createRootRoute, Link, Outlet } from "@evjs/client";
|
|
2
|
-
|
|
3
|
-
function Root() {
|
|
4
|
-
return (
|
|
5
|
-
<div style={{ fontFamily: "system-ui, sans-serif", padding: "1rem" }}>
|
|
6
|
-
<nav
|
|
7
|
-
style={{
|
|
8
|
-
display: "flex",
|
|
9
|
-
gap: "1rem",
|
|
10
|
-
borderBottom: "1px solid #e5e7eb",
|
|
11
|
-
paddingBottom: "0.5rem",
|
|
12
|
-
marginBottom: "1rem",
|
|
13
|
-
}}
|
|
14
|
-
>
|
|
15
|
-
<Link to="/" style={{ fontWeight: "bold" }}>
|
|
16
|
-
Home
|
|
17
|
-
</Link>
|
|
18
|
-
<Link to="/about">About</Link>
|
|
19
|
-
<Link to="/posts">Posts</Link>
|
|
20
|
-
</nav>
|
|
21
|
-
<Outlet />
|
|
22
|
-
</div>
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const rootRoute = createRootRoute({
|
|
27
|
-
component: Root,
|
|
28
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { createRoute } from "@evjs/client";
|
|
2
|
-
import { rootRoute } from "./__root";
|
|
3
|
-
|
|
4
|
-
function About() {
|
|
5
|
-
return (
|
|
6
|
-
<div>
|
|
7
|
-
<h1>About</h1>
|
|
8
|
-
<p>
|
|
9
|
-
Code-based routing with TanStack Router via <code>createApp</code>.
|
|
10
|
-
</p>
|
|
11
|
-
</div>
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const aboutRoute = createRoute({
|
|
16
|
-
getParentRoute: () => rootRoute,
|
|
17
|
-
path: "/about",
|
|
18
|
-
component: About,
|
|
19
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { createRoute } from "@evjs/client";
|
|
2
|
-
import { rootRoute } from "./__root";
|
|
3
|
-
|
|
4
|
-
function Home() {
|
|
5
|
-
return (
|
|
6
|
-
<div>
|
|
7
|
-
<h1>Welcome Home!</h1>
|
|
8
|
-
<p>
|
|
9
|
-
A basic client-side rendered app built with <code>@evjs/client</code>.
|
|
10
|
-
</p>
|
|
11
|
-
</div>
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const homeRoute = createRoute({
|
|
16
|
-
getParentRoute: () => rootRoute,
|
|
17
|
-
path: "/",
|
|
18
|
-
component: Home,
|
|
19
|
-
});
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { createRoute, Link, Outlet } from "@evjs/client";
|
|
2
|
-
import { rootRoute } from "../__root";
|
|
3
|
-
|
|
4
|
-
const posts = [
|
|
5
|
-
{ id: "1", title: "First Post", body: "Hello world!" },
|
|
6
|
-
{ id: "2", title: "Second Post", body: "Another day, another post." },
|
|
7
|
-
{ id: "3", title: "Third Post", body: "Three's a charm." },
|
|
8
|
-
];
|
|
9
|
-
|
|
10
|
-
function Posts() {
|
|
11
|
-
return (
|
|
12
|
-
<div style={{ display: "flex", gap: "1rem" }}>
|
|
13
|
-
<ul style={{ listStyle: "none", padding: 0, minWidth: 160 }}>
|
|
14
|
-
{posts.map((p) => (
|
|
15
|
-
<li key={p.id} style={{ marginBottom: "0.25rem" }}>
|
|
16
|
-
<Link
|
|
17
|
-
to="/posts/$postId"
|
|
18
|
-
params={{ postId: p.id }}
|
|
19
|
-
activeProps={{ style: { fontWeight: "bold" } }}
|
|
20
|
-
>
|
|
21
|
-
{p.title}
|
|
22
|
-
</Link>
|
|
23
|
-
</li>
|
|
24
|
-
))}
|
|
25
|
-
</ul>
|
|
26
|
-
<Outlet />
|
|
27
|
-
</div>
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const postsRoute = createRoute({
|
|
32
|
-
getParentRoute: () => rootRoute,
|
|
33
|
-
path: "/posts",
|
|
34
|
-
component: Posts,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
function PostsIndex() {
|
|
38
|
-
return <p style={{ color: "#6b7280" }}>Select a post.</p>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const postsIndexRoute = createRoute({
|
|
42
|
-
getParentRoute: () => postsRoute,
|
|
43
|
-
path: "/",
|
|
44
|
-
component: PostsIndex,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
function PostDetail() {
|
|
48
|
-
const { postId } = postDetailRoute.useParams();
|
|
49
|
-
const post = posts.find((p) => p.id === postId);
|
|
50
|
-
if (!post) return <p>Post not found.</p>;
|
|
51
|
-
return (
|
|
52
|
-
<div>
|
|
53
|
-
<h2>{post.title}</h2>
|
|
54
|
-
<p>{post.body}</p>
|
|
55
|
-
</div>
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const postDetailRoute = createRoute({
|
|
60
|
-
getParentRoute: () => postsRoute,
|
|
61
|
-
path: "$postId",
|
|
62
|
-
component: PostDetail,
|
|
63
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
# basic-fns-ecma
|
|
2
|
-
|
|
3
|
-
Server functions with the ECMA/Fetch adapter — works on Deno, Bun, and edge runtimes.
|
|
4
|
-
|
|
5
|
-
## Run
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm run dev -w example-basic-fns-ecma
|
|
9
|
-
|
|
10
|
-
# Production
|
|
11
|
-
npm run build -w example-basic-fns-ecma
|
|
12
|
-
npm run start -w example-basic-fns-ecma
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## Key Files
|
|
16
|
-
|
|
17
|
-
| File | Purpose |
|
|
18
|
-
|------|---------|
|
|
19
|
-
| `src/main.tsx` | App bootstrap |
|
|
20
|
-
| `src/routes.tsx` | Routes + components |
|
|
21
|
-
| `src/api/messages.server.ts` | Server functions |
|
|
22
|
-
| `server.entry.mjs` | Custom ECMA server entry using `@evjs/server/ecma` runtime |
|
|
23
|
-
|
|
24
|
-
## What It Demonstrates
|
|
25
|
-
|
|
26
|
-
- `createFetchHandler` from `@evjs/server/ecma`
|
|
27
|
-
- Portable server bundle (no Node.js-specific APIs)
|
|
28
|
-
- Custom runtime configuration via `ev.config.ts`
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "example-basic-fns-ecma",
|
|
3
|
-
"version": "0.0.29",
|
|
4
|
-
"private": true,
|
|
5
|
-
"scripts": {
|
|
6
|
-
"dev": "npx ev dev",
|
|
7
|
-
"build": "npx ev build",
|
|
8
|
-
"start": "node dist/server/start.mjs",
|
|
9
|
-
"check-types": "tsc --noEmit"
|
|
10
|
-
},
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"react": "^19.2.4",
|
|
13
|
-
"react-dom": "^19.2.4"
|
|
14
|
-
},
|
|
15
|
-
"devDependencies": {
|
|
16
|
-
"@evjs/cli": "*",
|
|
17
|
-
"@evjs/client": "*",
|
|
18
|
-
"@evjs/ev": "*",
|
|
19
|
-
"@evjs/server": "*",
|
|
20
|
-
"@types/react": "^19.1.8",
|
|
21
|
-
"@types/react-dom": "^19.1.8",
|
|
22
|
-
"typescript": "^6.0.2"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ECMA runtime adapter entry point.
|
|
3
|
-
*
|
|
4
|
-
* Loads the server bundle (which registers server functions via the runtime),
|
|
5
|
-
* creates a Hono app, and exports a standard fetch handler.
|
|
6
|
-
*
|
|
7
|
-
* Works in any Fetch API-compatible runtime:
|
|
8
|
-
* - Deno: `deno serve start.mjs`
|
|
9
|
-
* - Bun: `bun run start.mjs`
|
|
10
|
-
* - Edge runtimes: deploy as-is
|
|
11
|
-
* - Node.js 18+: use with @hono/node-server
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { readFileSync } from "node:fs";
|
|
15
|
-
import { createRequire } from "node:module";
|
|
16
|
-
|
|
17
|
-
const require = createRequire(import.meta.url);
|
|
18
|
-
|
|
19
|
-
// Read server manifest to find the entry bundle
|
|
20
|
-
const manifest = JSON.parse(
|
|
21
|
-
readFileSync(new URL("./manifest.json", import.meta.url), "utf-8"),
|
|
22
|
-
);
|
|
23
|
-
const bundle = require(`./server/${manifest.server.entry}`);
|
|
24
|
-
const app = bundle.createApp();
|
|
25
|
-
|
|
26
|
-
// Export via ECMA adapter — compatible with Deno, Bun, Workers
|
|
27
|
-
const { createFetchHandler } = require("@evjs/server/ecma");
|
|
28
|
-
const handler = createFetchHandler(app);
|
|
29
|
-
export default handler;
|
|
30
|
-
|
|
31
|
-
// For Node.js, start the server:
|
|
32
|
-
if (typeof process !== "undefined" && process.argv[1]?.endsWith("start.mjs")) {
|
|
33
|
-
const { serve } = await import("@hono/node-server");
|
|
34
|
-
const port = Number(process.env.PORT) || 3001;
|
|
35
|
-
serve({ fetch: app.fetch, port }, (info) => {
|
|
36
|
-
console.log(
|
|
37
|
-
`ECMA runtime server listening on http://localhost:${info.port}`,
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use server";
|
|
2
|
-
|
|
3
|
-
/** Simulated message store. */
|
|
4
|
-
const messages: { id: string; text: string; timestamp: string }[] = [
|
|
5
|
-
{
|
|
6
|
-
id: "1",
|
|
7
|
-
text: "Hello from the ECMA runtime!",
|
|
8
|
-
timestamp: new Date().toISOString(),
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
id: "2",
|
|
12
|
-
text: "This server runs on any Fetch-compatible runtime.",
|
|
13
|
-
timestamp: new Date().toISOString(),
|
|
14
|
-
},
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
/** Get all messages. */
|
|
18
|
-
export async function getMessages() {
|
|
19
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
20
|
-
return messages;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Post a new message. */
|
|
24
|
-
export async function postMessage(text: string) {
|
|
25
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
26
|
-
const msg = {
|
|
27
|
-
id: String(messages.length + 1),
|
|
28
|
-
text,
|
|
29
|
-
timestamp: new Date().toISOString(),
|
|
30
|
-
};
|
|
31
|
-
messages.push(msg);
|
|
32
|
-
return msg;
|
|
33
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { createApp } from "@evjs/client";
|
|
2
|
-
import { rootRoute } from "./pages/__root";
|
|
3
|
-
import { messagesRoute } from "./pages/home";
|
|
4
|
-
|
|
5
|
-
const routeTree = rootRoute.addChildren([messagesRoute]);
|
|
6
|
-
|
|
7
|
-
const app = createApp({ routeTree });
|
|
8
|
-
|
|
9
|
-
declare module "@tanstack/react-router" {
|
|
10
|
-
interface Register {
|
|
11
|
-
router: typeof app.router;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
app.render("#app");
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { createRootRoute, Link, Outlet } from "@evjs/client";
|
|
2
|
-
|
|
3
|
-
function Root() {
|
|
4
|
-
return (
|
|
5
|
-
<div style={{ fontFamily: "system-ui, sans-serif", padding: "1rem" }}>
|
|
6
|
-
<h1>ECMA Runtime Example</h1>
|
|
7
|
-
<p style={{ color: "#666" }}>
|
|
8
|
-
Server bundle is environment-agnostic — works in Node.js, Deno, Bun, any
|
|
9
|
-
Fetch-compatible runtime.
|
|
10
|
-
</p>
|
|
11
|
-
<nav style={{ display: "flex", gap: "1rem", marginBottom: "1rem" }}>
|
|
12
|
-
<Link to="/">Messages</Link>
|
|
13
|
-
</nav>
|
|
14
|
-
<Outlet />
|
|
15
|
-
</div>
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const rootRoute = createRootRoute({ component: Root });
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { createRoute } from "@evjs/client";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
import { getMessages, postMessage } from "../api/messages.server";
|
|
4
|
-
import { rootRoute } from "./__root";
|
|
5
|
-
|
|
6
|
-
function MessagesPage() {
|
|
7
|
-
const [text, setText] = useState("");
|
|
8
|
-
const [messages, setMessages] = useState<
|
|
9
|
-
{ id: string; text: string; timestamp: string }[]
|
|
10
|
-
>([]);
|
|
11
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
12
|
-
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
let mounted = true;
|
|
15
|
-
getMessages()
|
|
16
|
-
.then((data) => {
|
|
17
|
-
if (mounted) {
|
|
18
|
-
setMessages(data);
|
|
19
|
-
setIsLoading(false);
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
.catch((err) => {
|
|
23
|
-
console.error(err);
|
|
24
|
-
if (mounted) setIsLoading(false);
|
|
25
|
-
});
|
|
26
|
-
return () => {
|
|
27
|
-
mounted = false;
|
|
28
|
-
};
|
|
29
|
-
}, []);
|
|
30
|
-
|
|
31
|
-
async function handlePost(e: { preventDefault: () => void }) {
|
|
32
|
-
e.preventDefault();
|
|
33
|
-
if (!text) return;
|
|
34
|
-
try {
|
|
35
|
-
await postMessage(text);
|
|
36
|
-
setText("");
|
|
37
|
-
const data = await getMessages();
|
|
38
|
-
setMessages(data);
|
|
39
|
-
} catch (err) {
|
|
40
|
-
console.error(err);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (isLoading) return <p>Loading messages from server…</p>;
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
<div>
|
|
48
|
-
<h2>Messages (via server functions)</h2>
|
|
49
|
-
<ul>
|
|
50
|
-
{messages.map((m) => (
|
|
51
|
-
<li key={m.id}>
|
|
52
|
-
<strong>{m.text}</strong>
|
|
53
|
-
<br />
|
|
54
|
-
<small style={{ color: "#999" }}>{m.timestamp}</small>
|
|
55
|
-
</li>
|
|
56
|
-
))}
|
|
57
|
-
</ul>
|
|
58
|
-
|
|
59
|
-
<h3>Post a Message</h3>
|
|
60
|
-
<form onSubmit={handlePost} style={{ display: "flex", gap: "0.5rem" }}>
|
|
61
|
-
<input
|
|
62
|
-
placeholder="Message"
|
|
63
|
-
value={text}
|
|
64
|
-
onChange={(e) => setText(e.target.value)}
|
|
65
|
-
/>
|
|
66
|
-
<button type="submit">Send</button>
|
|
67
|
-
</form>
|
|
68
|
-
</div>
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export const messagesRoute = createRoute({
|
|
73
|
-
getParentRoute: () => rootRoute,
|
|
74
|
-
path: "/",
|
|
75
|
-
component: MessagesPage,
|
|
76
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Service Worker adapter — runs server functions in the browser.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors server.entry.mjs but for the Service Worker environment.
|
|
5
|
-
* Intercepts /api/* requests (matching the default rpcEndpoint) and
|
|
6
|
-
* handles them locally via Hono. Update the path prefix below if
|
|
7
|
-
* using a custom rpcEndpoint.
|
|
8
|
-
*
|
|
9
|
-
* When running in Node.js (no native SW APIs), mock implementations
|
|
10
|
-
* are provided so the entry can be tested outside a browser.
|
|
11
|
-
*/
|
|
12
|
-
import { readFileSync } from "node:fs";
|
|
13
|
-
import { createRequire } from "node:module";
|
|
14
|
-
|
|
15
|
-
// ── Mock Service Worker APIs for Node.js testing ──
|
|
16
|
-
|
|
17
|
-
if (typeof self === "undefined") {
|
|
18
|
-
const listeners = {};
|
|
19
|
-
globalThis.self = /** @type {any} */ ({
|
|
20
|
-
addEventListener(type, handler) {
|
|
21
|
-
if (!listeners[type]) {
|
|
22
|
-
listeners[type] = [];
|
|
23
|
-
}
|
|
24
|
-
listeners[type].push(handler);
|
|
25
|
-
},
|
|
26
|
-
__listeners: listeners,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (typeof FetchEvent === "undefined") {
|
|
31
|
-
globalThis.FetchEvent = class FetchEvent extends Event {
|
|
32
|
-
/** @type {Request} */
|
|
33
|
-
request;
|
|
34
|
-
/** @type {Promise<Response> | Response | undefined} */
|
|
35
|
-
_response;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @param {string} type
|
|
39
|
-
* @param {{ request: Request }} init
|
|
40
|
-
*/
|
|
41
|
-
constructor(type, init) {
|
|
42
|
-
super(type);
|
|
43
|
-
this.request = init.request;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** @param {Promise<Response> | Response} response */
|
|
47
|
-
respondWith(response) {
|
|
48
|
-
this._response = response;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ── Actual SW entry logic ──
|
|
54
|
-
|
|
55
|
-
const require = createRequire(import.meta.url);
|
|
56
|
-
|
|
57
|
-
const manifest = JSON.parse(
|
|
58
|
-
readFileSync(new URL("./manifest.json", import.meta.url), "utf-8"),
|
|
59
|
-
);
|
|
60
|
-
const bundle = require(`./server/${manifest.server.entry}`);
|
|
61
|
-
const app = bundle.createApp();
|
|
62
|
-
|
|
63
|
-
// SW fetch event
|
|
64
|
-
self.addEventListener("fetch", (event) => {
|
|
65
|
-
const url = new URL(event.request.url);
|
|
66
|
-
if (url.pathname.startsWith("/api/")) {
|
|
67
|
-
event.respondWith(app.fetch(event.request));
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// ── Test helper: dispatch a mock FetchEvent and return the response ──
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Dispatch a mock fetch event through the SW handler.
|
|
75
|
-
* @param {Request} request
|
|
76
|
-
* @returns {Promise<Response | undefined>}
|
|
77
|
-
*/
|
|
78
|
-
export async function dispatchFetch(request) {
|
|
79
|
-
const event = new FetchEvent("fetch", { request });
|
|
80
|
-
const handlers = self.__listeners?.fetch || [];
|
|
81
|
-
for (const handler of handlers) {
|
|
82
|
-
handler(event);
|
|
83
|
-
}
|
|
84
|
-
return event._response ? await event._response : undefined;
|
|
85
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"jsx": "react-jsx",
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
7
|
-
"types": ["node"],
|
|
8
|
-
"moduleResolution": "Bundler",
|
|
9
|
-
"verbatimModuleSyntax": true,
|
|
10
|
-
"noEmit": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"strict": true,
|
|
13
|
-
"noUnusedLocals": true,
|
|
14
|
-
"noUnusedParameters": true,
|
|
15
|
-
"noFallthroughCasesInSwitch": true
|
|
16
|
-
},
|
|
17
|
-
"include": ["src"]
|
|
18
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# basic-server-fns
|
|
2
|
-
|
|
3
|
-
Basic server functions with type-safe `useQuery()` / `useMutation()`.
|
|
4
|
-
|
|
5
|
-
## Run
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm run dev -w example-basic-server-fns
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Key Files
|
|
12
|
-
|
|
13
|
-
| File | Purpose |
|
|
14
|
-
|------|---------|
|
|
15
|
-
| `src/main.tsx` | App bootstrap |
|
|
16
|
-
| `src/routes.tsx` | Routes + components |
|
|
17
|
-
| `src/api/users.server.ts` | `"use server"` CRUD functions |
|
|
18
|
-
|
|
19
|
-
## What It Demonstrates
|
|
20
|
-
|
|
21
|
-
- `"use server"` directive for auto-discovered server functions
|
|
22
|
-
- `useQuery(getUsers)` for type-safe data fetching
|
|
23
|
-
- `useMutation({ mutationFn: createUser })` for mutations
|
|
24
|
-
- `getFnQueryKey(getUsers)` for cache invalidation
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { createRootRoute, 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
|
-
</nav>
|
|
10
|
-
<Outlet />
|
|
11
|
-
</div>
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const rootRoute = createRootRoute({ component: Root });
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createRoute,
|
|
3
|
-
getFnQueryKey,
|
|
4
|
-
useMutation,
|
|
5
|
-
useQuery,
|
|
6
|
-
useQueryClient,
|
|
7
|
-
} from "@evjs/client";
|
|
8
|
-
import { useState } from "react";
|
|
9
|
-
import { createUser, getUsers } from "../api/users.server";
|
|
10
|
-
import { rootRoute } from "./__root";
|
|
11
|
-
|
|
12
|
-
console.log("***** ", getUsers, createUser);
|
|
13
|
-
|
|
14
|
-
Object.assign(window, { getUsers, createUser });
|
|
15
|
-
|
|
16
|
-
function UsersPage() {
|
|
17
|
-
const [name, setName] = useState("");
|
|
18
|
-
const [email, setEmail] = useState("");
|
|
19
|
-
const queryClient = useQueryClient();
|
|
20
|
-
|
|
21
|
-
// Use the framework's useQuery hook instead of manual useState + useEffect
|
|
22
|
-
const { data: users = [], isLoading } = useQuery(getUsers);
|
|
23
|
-
|
|
24
|
-
// Use useMutation for server function calls that modify data
|
|
25
|
-
const createUserMutation = useMutation({
|
|
26
|
-
mutationFn: createUser,
|
|
27
|
-
onSuccess: () => {
|
|
28
|
-
// Invalidate and refetch users list after successful creation
|
|
29
|
-
queryClient.invalidateQueries({ queryKey: getFnQueryKey(getUsers) });
|
|
30
|
-
setName("");
|
|
31
|
-
setEmail("");
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
function handleCreate(e: { preventDefault: () => void }) {
|
|
36
|
-
e.preventDefault();
|
|
37
|
-
if (!name || !email) return;
|
|
38
|
-
createUserMutation.mutate({ name, email });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (isLoading) return <p>Loading users from server…</p>;
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
<div>
|
|
45
|
-
<h2>Users (fetched via direct server function call)</h2>
|
|
46
|
-
<ul>
|
|
47
|
-
{users.map((u) => (
|
|
48
|
-
<li key={u.id}>
|
|
49
|
-
{u.name} — {u.email}
|
|
50
|
-
</li>
|
|
51
|
-
))}
|
|
52
|
-
</ul>
|
|
53
|
-
|
|
54
|
-
<h3>Add User</h3>
|
|
55
|
-
<form onSubmit={handleCreate} style={{ display: "flex", gap: "0.5rem" }}>
|
|
56
|
-
<input
|
|
57
|
-
placeholder="Name"
|
|
58
|
-
value={name}
|
|
59
|
-
onChange={(e) => setName(e.target.value)}
|
|
60
|
-
/>
|
|
61
|
-
<input
|
|
62
|
-
placeholder="Email"
|
|
63
|
-
value={email}
|
|
64
|
-
onChange={(e) => setEmail(e.target.value)}
|
|
65
|
-
/>
|
|
66
|
-
<button type="submit">Create</button>
|
|
67
|
-
</form>
|
|
68
|
-
</div>
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export const usersRoute = createRoute({
|
|
73
|
-
getParentRoute: () => rootRoute,
|
|
74
|
-
path: "/",
|
|
75
|
-
component: UsersPage,
|
|
76
|
-
});
|