@evjs/create-app 0.3.20 → 0.3.22
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 +8 -5
- package/templates/api-routes/package.json +1 -1
- package/templates/api-routes/src/apis/api/health/api.ts +12 -3
- package/templates/api-routes/src/apis/api/policies.ts +21 -0
- package/templates/api-routes/src/apis/api/posts/$id/api.ts +8 -12
- package/templates/api-routes/src/apis/api/posts/api.ts +16 -16
- package/templates/api-routes/src/apis/api/posts/validate-post.ts +28 -0
- package/templates/api-routes/src/middlewares/middleware.ts +1 -1
- package/templates/api-routes/src/middlewares/response-metadata.ts +1 -1
- 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/templates/api-routes/src/apis/api/middleware.ts +0 -15
package/package.json
CHANGED
|
@@ -17,8 +17,9 @@ npm run dev
|
|
|
17
17
|
| `src/pages/layout.tsx` | File-convention root layout |
|
|
18
18
|
| `src/middlewares/middleware.ts` | Ordered global middleware composition anchor |
|
|
19
19
|
| `src/middlewares/response-metadata.ts` | Ordinary middleware module composed by the anchor |
|
|
20
|
-
| `src/apis/api/
|
|
20
|
+
| `src/apis/api/policies.ts` | Shared policy chain explicitly composed in HTTP method exports |
|
|
21
21
|
| `src/apis/api/posts/api.ts` | List/create handlers for `/api/posts` |
|
|
22
|
+
| `src/apis/api/posts/validate-post.ts` | POST-only validation composed with `withMiddlewares` |
|
|
22
23
|
| `src/apis/api/posts/$id/api.ts` | Dynamic handlers for `/api/posts/:id` |
|
|
23
24
|
| `src/apis/api/health/api.ts` | Health check endpoint |
|
|
24
25
|
| `src/apis/api/posts/posts-store.ts` | Private helper colocated in the `/api/posts` route scope |
|
|
@@ -29,9 +30,11 @@ npm run dev
|
|
|
29
30
|
- Dynamic route directories (`$id/api.ts` -> `:id`)
|
|
30
31
|
- Query string parsing (`?limit=N`)
|
|
31
32
|
- Custom status codes (201, 204, 404)
|
|
32
|
-
- Auto `OPTIONS` and `405 Method Not Allowed`
|
|
33
|
-
-
|
|
34
|
-
-
|
|
33
|
+
- Auto `HEAD` runs the GET chain; auto `OPTIONS` and `405 Method Not Allowed` run global middleware
|
|
34
|
+
- An explicit `HEAD` health probe
|
|
35
|
+
- Explicitly ordered global middleware composition with `MiddlewareChain` and `requestLogger` from `@evjs/ev/middleware`
|
|
36
|
+
- Shared policy arrays reused through ordinary imports
|
|
37
|
+
- Method-only middleware with `withMiddlewares(handler, middlewares)` from `@evjs/ev/api`
|
|
35
38
|
- Colocated private helpers that are not named `api.ts`
|
|
36
39
|
- A root `page.tsx` anchor mapped to `/` by file convention
|
|
37
40
|
|
|
@@ -63,6 +66,6 @@ curl http://localhost:3000/api/health
|
|
|
63
66
|
# Auto OPTIONS
|
|
64
67
|
curl -X OPTIONS http://localhost:3000/api/posts -i
|
|
65
68
|
|
|
66
|
-
# API
|
|
69
|
+
# Explicit API middleware short-circuit
|
|
67
70
|
curl -H 'x-block-api: true' http://localhost:3000/api/posts -i
|
|
68
71
|
```
|
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Health check route handler.
|
|
3
3
|
*
|
|
4
|
-
* Demonstrates a
|
|
4
|
+
* Demonstrates a dedicated HEAD probe alongside a JSON GET handler.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import { withMiddlewares } from "@evjs/ev/api";
|
|
8
|
+
import { apiPolicies } from "../policies";
|
|
9
|
+
|
|
10
|
+
export const GET = withMiddlewares(async () => {
|
|
8
11
|
return Response.json({
|
|
9
12
|
status: "ok",
|
|
10
13
|
uptime: process.uptime(),
|
|
11
14
|
timestamp: new Date().toISOString(),
|
|
12
15
|
});
|
|
13
|
-
};
|
|
16
|
+
}, apiPolicies);
|
|
17
|
+
|
|
18
|
+
export const HEAD = withMiddlewares(
|
|
19
|
+
() =>
|
|
20
|
+
new Response(null, { status: 204, headers: { "x-health-probe": "head" } }),
|
|
21
|
+
apiPolicies,
|
|
22
|
+
);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { MiddlewareChain, MiddlewareHandler } from "@evjs/ev/middleware";
|
|
2
|
+
|
|
3
|
+
const blockRequest: MiddlewareHandler = async (ctx, next) => {
|
|
4
|
+
if (ctx.req.header("x-block-api") === "true") {
|
|
5
|
+
return Response.json(
|
|
6
|
+
{ error: "blocked by API middleware" },
|
|
7
|
+
{ status: 403 },
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
await next();
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const apiMetadata: MiddlewareHandler = async (ctx, next) => {
|
|
14
|
+
await next();
|
|
15
|
+
ctx.header("x-api-policy", "applied");
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const apiPolicies = [
|
|
19
|
+
blockRequest,
|
|
20
|
+
apiMetadata,
|
|
21
|
+
] satisfies MiddlewareChain;
|
|
@@ -1,25 +1,21 @@
|
|
|
1
1
|
/** Dynamic route handlers for /api/posts/:id. */
|
|
2
2
|
|
|
3
|
+
import { withMiddlewares } from "@evjs/ev/api";
|
|
3
4
|
import { posts } from "@/apis/api/posts/posts-store";
|
|
4
|
-
|
|
5
|
-
interface RouteContext {
|
|
6
|
-
req: {
|
|
7
|
-
param(name: string): string;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
5
|
+
import { apiPolicies } from "../../policies";
|
|
10
6
|
|
|
11
7
|
/** Get a single post. */
|
|
12
|
-
export const GET = async (_req
|
|
8
|
+
export const GET = withMiddlewares(async (_req, ctx) => {
|
|
13
9
|
const id = ctx.req.param("id");
|
|
14
10
|
const post = posts.find((p) => p.id === id);
|
|
15
11
|
if (!post) {
|
|
16
12
|
return Response.json({ error: "Post not found" }, { status: 404 });
|
|
17
13
|
}
|
|
18
14
|
return Response.json(post);
|
|
19
|
-
};
|
|
15
|
+
}, apiPolicies);
|
|
20
16
|
|
|
21
17
|
/** Update a single post. */
|
|
22
|
-
export const PUT = async (req
|
|
18
|
+
export const PUT = withMiddlewares(async (req, ctx) => {
|
|
23
19
|
const id = ctx.req.param("id");
|
|
24
20
|
const idx = posts.findIndex((p) => p.id === id);
|
|
25
21
|
if (idx === -1) {
|
|
@@ -34,10 +30,10 @@ export const PUT = async (req: Request, ctx: RouteContext) => {
|
|
|
34
30
|
if (body) posts[idx].body = body;
|
|
35
31
|
|
|
36
32
|
return Response.json(posts[idx]);
|
|
37
|
-
};
|
|
33
|
+
}, apiPolicies);
|
|
38
34
|
|
|
39
35
|
/** Delete a single post. */
|
|
40
|
-
export const DELETE = async (_req
|
|
36
|
+
export const DELETE = withMiddlewares(async (_req, ctx) => {
|
|
41
37
|
const id = ctx.req.param("id");
|
|
42
38
|
const idx = posts.findIndex((p) => p.id === id);
|
|
43
39
|
if (idx === -1) {
|
|
@@ -45,4 +41,4 @@ export const DELETE = async (_req: Request, ctx: RouteContext) => {
|
|
|
45
41
|
}
|
|
46
42
|
posts.splice(idx, 1);
|
|
47
43
|
return new Response(null, { status: 204 });
|
|
48
|
-
};
|
|
44
|
+
}, apiPolicies);
|
|
@@ -5,30 +5,30 @@
|
|
|
5
5
|
* - Multiple HTTP methods on one api.ts anchor
|
|
6
6
|
* - JSON request/response
|
|
7
7
|
* - Custom status codes
|
|
8
|
+
* - Method-specific middleware with a shared request body cache
|
|
8
9
|
*/
|
|
9
10
|
|
|
11
|
+
import { withMiddlewares } from "@evjs/ev/api";
|
|
12
|
+
import { apiPolicies } from "../policies";
|
|
10
13
|
import { createPost, posts } from "./posts-store";
|
|
14
|
+
import validatePost from "./validate-post";
|
|
11
15
|
|
|
12
16
|
/** List posts. */
|
|
13
|
-
export const GET = async (req
|
|
17
|
+
export const GET = withMiddlewares(async (req) => {
|
|
14
18
|
const url = new URL(req.url);
|
|
15
19
|
const limit = Number(url.searchParams.get("limit")) || posts.length;
|
|
16
20
|
return Response.json(posts.slice(0, limit));
|
|
17
|
-
};
|
|
21
|
+
}, apiPolicies);
|
|
18
22
|
|
|
19
23
|
/** Create a post. */
|
|
20
|
-
export const POST =
|
|
21
|
-
|
|
22
|
-
title
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
export const POST = withMiddlewares(
|
|
25
|
+
async (_req, ctx) => {
|
|
26
|
+
const { title, body } = await ctx.req.json<{
|
|
27
|
+
title: string;
|
|
28
|
+
body: string;
|
|
29
|
+
}>();
|
|
25
30
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return Response.json(createPost({ title, body }), { status: 201 });
|
|
34
|
-
};
|
|
31
|
+
return Response.json(createPost({ title, body }), { status: 201 });
|
|
32
|
+
},
|
|
33
|
+
[...apiPolicies, validatePost],
|
|
34
|
+
);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from "@evjs/ev/middleware";
|
|
2
|
+
|
|
3
|
+
const validatePost: MiddlewareHandler = async (ctx, next) => {
|
|
4
|
+
let value: unknown;
|
|
5
|
+
try {
|
|
6
|
+
value = await ctx.req.json();
|
|
7
|
+
} catch {
|
|
8
|
+
return ctx.json({ error: "A JSON request body is required" }, 400);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (
|
|
12
|
+
!value ||
|
|
13
|
+
typeof value !== "object" ||
|
|
14
|
+
!("title" in value) ||
|
|
15
|
+
typeof value.title !== "string" ||
|
|
16
|
+
!value.title ||
|
|
17
|
+
!("body" in value) ||
|
|
18
|
+
typeof value.body !== "string" ||
|
|
19
|
+
!value.body
|
|
20
|
+
) {
|
|
21
|
+
return ctx.json({ error: "title and body are required" }, 400);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
await next();
|
|
25
|
+
ctx.header("x-post-validated", "true");
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default validatePost;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { MiddlewareHandler } from "@evjs/ev/server-context";
|
|
2
|
-
|
|
3
|
-
const middleware: MiddlewareHandler = async (ctx, next) => {
|
|
4
|
-
if (ctx.req.header("x-block-api") === "true") {
|
|
5
|
-
return Response.json(
|
|
6
|
-
{ error: "blocked by route middleware" },
|
|
7
|
-
{ status: 403 },
|
|
8
|
-
);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
await next();
|
|
12
|
-
ctx.header("x-api-scope", "api");
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export default middleware;
|