@evjs/create-app 0.3.22 → 0.3.24
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/api-routes/README.md +3 -0
- package/templates/api-routes/package.json +1 -1
- package/templates/api-routes/src/apis/api/posts/api.ts +2 -4
- package/templates/api-routes/src/apis/api/posts/posts-store.ts +2 -8
- package/templates/api-routes/src/pages/page.tsx +7 -20
- package/templates/api-routes/src/shared/posts.ts +10 -0
- package/templates/basic/package.json +1 -1
- package/templates/complex-routing/package.json +1 -1
- package/templates/custom-ws-transport/package.json +1 -1
- package/templates/mpa/package.json +1 -1
- package/templates/plugin-authoring/package.json +1 -1
- package/templates/with-sqlite/package.json +1 -1
- package/templates/with-tailwind/package.json +1 -1
- package/templates/with-trpc/package.json +1 -1
package/package.json
CHANGED
|
@@ -14,6 +14,7 @@ npm run dev
|
|
|
14
14
|
|------|---------|
|
|
15
15
|
| `ev.config.ts` | Selects SPA routing mode |
|
|
16
16
|
| `src/pages/page.tsx` | Root Page that exercises the REST endpoints |
|
|
17
|
+
| `src/shared/posts.ts` | JSON DTOs shared through type-only imports by client and server |
|
|
17
18
|
| `src/pages/layout.tsx` | File-convention root layout |
|
|
18
19
|
| `src/middlewares/middleware.ts` | Ordered global middleware composition anchor |
|
|
19
20
|
| `src/middlewares/response-metadata.ts` | Ordinary middleware module composed by the anchor |
|
|
@@ -26,6 +27,8 @@ npm run dev
|
|
|
26
27
|
|
|
27
28
|
## What It Demonstrates
|
|
28
29
|
|
|
30
|
+
- Application HTTP calls through `import { api } from '@evjs/ev'` and `.json<Post>()`
|
|
31
|
+
- Handwritten shared DTOs, existing runtime validation, and no API type generation
|
|
29
32
|
- Directory-owned `api.ts` anchors with uppercase method exports (`GET`, `POST`, `PUT`, `DELETE`)
|
|
30
33
|
- Dynamic route directories (`$id/api.ts` -> `:id`)
|
|
31
34
|
- Query string parsing (`?limit=N`)
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { withMiddlewares } from "@evjs/ev/api";
|
|
12
|
+
import type { CreatePostInput } from "@/shared/posts";
|
|
12
13
|
import { apiPolicies } from "../policies";
|
|
13
14
|
import { createPost, posts } from "./posts-store";
|
|
14
15
|
import validatePost from "./validate-post";
|
|
@@ -23,10 +24,7 @@ export const GET = withMiddlewares(async (req) => {
|
|
|
23
24
|
/** Create a post. */
|
|
24
25
|
export const POST = withMiddlewares(
|
|
25
26
|
async (_req, ctx) => {
|
|
26
|
-
const { title, body } = await ctx.req.json<
|
|
27
|
-
title: string;
|
|
28
|
-
body: string;
|
|
29
|
-
}>();
|
|
27
|
+
const { title, body } = await ctx.req.json<CreatePostInput>();
|
|
30
28
|
|
|
31
29
|
return Response.json(createPost({ title, body }), { status: 201 });
|
|
32
30
|
},
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export interface Post {
|
|
3
|
-
id: string;
|
|
4
|
-
title: string;
|
|
5
|
-
body: string;
|
|
6
|
-
createdAt: string;
|
|
7
|
-
}
|
|
1
|
+
import type { CreatePostInput, Post } from "@/shared/posts";
|
|
8
2
|
|
|
9
3
|
export const posts: Post[] = [
|
|
10
4
|
{
|
|
@@ -23,7 +17,7 @@ export const posts: Post[] = [
|
|
|
23
17
|
|
|
24
18
|
let nextId = 3;
|
|
25
19
|
|
|
26
|
-
export function createPost(input:
|
|
20
|
+
export function createPost(input: CreatePostInput): Post {
|
|
27
21
|
const post: Post = {
|
|
28
22
|
id: String(nextId++),
|
|
29
23
|
title: input.title,
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
+
import { api } from "@evjs/ev";
|
|
1
2
|
import { useState } from "react";
|
|
2
|
-
|
|
3
|
-
// ── Types ──
|
|
4
|
-
|
|
5
|
-
interface Post {
|
|
6
|
-
id: string;
|
|
7
|
-
title: string;
|
|
8
|
-
body: string;
|
|
9
|
-
createdAt: string;
|
|
10
|
-
}
|
|
3
|
+
import type { CreatePostInput, Post } from "@/shared/posts";
|
|
11
4
|
|
|
12
5
|
// ── Posts Page ──
|
|
13
6
|
|
|
@@ -24,9 +17,7 @@ export default function PostsPage() {
|
|
|
24
17
|
setIsLoading(true);
|
|
25
18
|
setError(null);
|
|
26
19
|
try {
|
|
27
|
-
const
|
|
28
|
-
if (!res.ok) throw new Error(`Failed: ${res.status}`);
|
|
29
|
-
const data = await res.json();
|
|
20
|
+
const data = await api.get("/api/posts").json<Post[]>();
|
|
30
21
|
setPosts(data);
|
|
31
22
|
} catch (err) {
|
|
32
23
|
setError(err instanceof Error ? err.message : String(err));
|
|
@@ -42,12 +33,8 @@ export default function PostsPage() {
|
|
|
42
33
|
|
|
43
34
|
setIsCreating(true);
|
|
44
35
|
try {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
headers: { "Content-Type": "application/json" },
|
|
48
|
-
body: JSON.stringify({ title, body }),
|
|
49
|
-
});
|
|
50
|
-
if (!res.ok) throw new Error(`Failed: ${res.status}`);
|
|
36
|
+
const input: CreatePostInput = { title, body };
|
|
37
|
+
await api.post("/api/posts", { json: input }).json<Post>();
|
|
51
38
|
setTitle("");
|
|
52
39
|
setBody("");
|
|
53
40
|
await fetchPosts();
|
|
@@ -61,7 +48,7 @@ export default function PostsPage() {
|
|
|
61
48
|
// Delete a post via DELETE
|
|
62
49
|
async function handleDelete(id: string) {
|
|
63
50
|
try {
|
|
64
|
-
const res = await
|
|
51
|
+
const res = await api.delete(`/api/posts/${encodeURIComponent(id)}`);
|
|
65
52
|
if (!res.ok) throw new Error(`Failed: ${res.status}`);
|
|
66
53
|
await fetchPosts();
|
|
67
54
|
} catch (err) {
|
|
@@ -209,7 +196,7 @@ function HealthCheck() {
|
|
|
209
196
|
const [health, setHealth] = useState<Record<string, unknown> | null>(null);
|
|
210
197
|
|
|
211
198
|
async function checkHealth() {
|
|
212
|
-
const res = await
|
|
199
|
+
const res = await api.get("/api/health");
|
|
213
200
|
setHealth(await res.json());
|
|
214
201
|
}
|
|
215
202
|
|