Skip to content

Commit 4345fb1

Browse files
committed
feat: add Middleware Guide and update documentation links
1 parent 67ae0c8 commit 4345fb1

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default defineConfig({
3737
items: [
3838
{ text: 'Getting Started', link: '/guide/getting-started' },
3939
{ text: 'CLI', link: '/guide/cli' },
40+
{ text: 'Middleware', link: '/guide/middleware' },
4041
{ text: 'Database & Modeling', link: '/guide/database-modeling' },
4142
{ text: 'API Reference', link: '/api' },
4243
]

docs/guide/express-runtime.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Express Runtime Interaction
22

3-
This page documents every supported way to interact directly with Arkstack's Express runtime.
3+
Arkstack's Express runtime integration provides multiple ways to interact with the underlying Express instance, allowing for flexible middleware application, route handling, and lifecycle control.
44

55
## Bootstrap Exports
66

@@ -91,5 +91,6 @@ await app.shutdown();
9191
## Notes
9292

9393
- `app.boot(port)` mounts public assets, binds router, applies middleware, registers error handling, starts the server, and attaches graceful shutdown.
94+
- For middleware layering and recommended usage, see [Middleware Guide](/guide/middleware).
9495
- Use the router contract (`getRouter`) for framework-agnostic behavior where possible.
9596
- Prefer `expressApp` only when you specifically need native Express APIs.

docs/guide/h3-runtime.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# H3 Runtime Interaction
22

3-
This page documents every supported way to interact directly with Arkstack's H3 runtime.
3+
Arkstack's H3 runtime integration provides multiple ways to interact with the underlying H3 instance, allowing for flexible middleware application, route handling, and lifecycle control.
44

55
## Bootstrap Exports
66

@@ -93,5 +93,6 @@ await app.shutdown();
9393
## Notes
9494

9595
- `app.boot(port)` mounts public assets, binds router, applies middleware, starts the server, and attaches graceful shutdown.
96+
- For middleware layering and recommended usage, see [Middleware Guide](/guide/middleware).
9697
- Use the router contract (`getRouter`) for framework-agnostic behavior where possible.
9798
- Prefer `h3App` only when you specifically need native H3 APIs.

docs/guide/middleware.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Middleware Guide
2+
3+
Arkstack provides a structured approach to middleware configuration and execution. This guide covers where to define middleware, the execution order, and best practices for layering.
4+
5+
## Where middleware is configured
6+
7+
Define middleware in your kit's `src/config/middleware.ts` file.
8+
9+
Each config returns the same shape:
10+
11+
```ts
12+
{
13+
global: [],
14+
before: [],
15+
after: [],
16+
}
17+
```
18+
19+
## Middleware execution order
20+
21+
Arkstack applies middleware in this order during `app.boot(port)`:
22+
23+
1. Public assets are mounted.
24+
2. `global` middleware runs.
25+
3. `before` middleware runs.
26+
4. Routes are bound.
27+
5. `after` middleware runs.
28+
6. Runtime-specific startup continues (Express error handler registration, then server start).
29+
30+
Use each group with this intent:
31+
32+
- `global`: middleware that should always run for every request (body parsing, CORS, method override, baseline security headers).
33+
- `before`: middleware that should run before route handlers are bound/executed (request context setup, auth gates, request-level tracing).
34+
- `after`: middleware that should run after routes are bound (post-route transforms, tail processing).
35+
36+
## Express example
37+
38+
`express/src/config/middleware.ts`:
39+
40+
```ts
41+
import express, { Express } from 'express';
42+
import cors from 'cors';
43+
import methodOverride from 'method-override';
44+
import { MiddlewareConfig } from 'src/types/config';
45+
46+
const config = (_app: Express): MiddlewareConfig => {
47+
return {
48+
global: [
49+
express.json(),
50+
express.urlencoded({ extended: true }),
51+
methodOverride('X-HTTP-Method'),
52+
cors(),
53+
],
54+
before: [],
55+
after: [],
56+
};
57+
};
58+
59+
export default config;
60+
```
61+
62+
## H3 example
63+
64+
`h3/src/config/middleware.ts`:
65+
66+
```ts
67+
import { H3 } from 'h3';
68+
import { MiddlewareConfig } from 'src/types/config';
69+
import { cors } from '@app/http/middlewares/cors';
70+
71+
const config = (_app: H3): MiddlewareConfig => {
72+
return {
73+
global: [cors()],
74+
before: [],
75+
after: [],
76+
};
77+
};
78+
79+
export default config;
80+
```
81+
82+
## Route-level middleware
83+
84+
Use config middleware for app-wide concerns. For route-specific behavior, attach middleware at route declaration time.
85+
86+
Example from Express route registration:
87+
88+
```ts
89+
import { Router } from 'src/core/router';
90+
91+
Router.get(
92+
'/',
93+
({ res }) => {
94+
res
95+
.setHeader('Content-Type', 'text/html')
96+
.send('Welcome to the Express application!');
97+
},
98+
[
99+
() => {
100+
console.log('Middleware for root route');
101+
},
102+
],
103+
);
104+
```
105+
106+
## Team expectation
107+
108+
- Prefer `src/config/middleware.ts` for cross-cutting concerns.
109+
- Keep `global` minimal and predictable.
110+
- Put request gating/initialization in `before`.
111+
- Reserve `after` for true post-route concerns.
112+
- Keep route-level middleware close to the route when the concern is route-specific.

0 commit comments

Comments
 (0)