|
1 | 1 | import { Elysia } from '../src' |
2 | 2 |
|
3 | | -// ❌ BROKEN: Using separate instances with .use() |
4 | | -const typesRoutes = new Elysia({ prefix: '/types' }) |
5 | | - .get('/', () => 'List all types') |
6 | | - .get('/:id', ({ params }) => `Get type: ${params.id}`) |
7 | | - |
8 | | -const itemsRoutes = new Elysia() |
9 | | - .get('/', () => 'List all items') |
10 | | - .get('/:id', ({ params }) => `Get item: ${params.id}`) |
11 | | - |
12 | | -const brokenModule = new Elysia({ prefix: '/items' }) |
13 | | - .use(typesRoutes) // Static prefix - should match first |
14 | | - .use(itemsRoutes) // Has /:id catch-all |
15 | | - |
16 | | -const typesRoutesNoPrefix = new Elysia() |
17 | | - .get('/', () => 'List all types') |
18 | | - .get('/:id', ({ params }) => `Get type: ${params.id}`) |
19 | | - |
20 | | -const brokenModuleWithGroup = new Elysia({ prefix: '/items' }) |
21 | | - .group('/types', (app) => app.use(typesRoutesNoPrefix)) |
22 | | - .use(itemsRoutes) |
23 | | - |
24 | | -// ✅ WORKS: All routes in single instance |
25 | | -const workingModule = new Elysia({ prefix: '/items' }) |
26 | | - .get('/:id', ({ params }) => `Get item: ${params.id}`) |
27 | | - .get('/', () => 'List all items') |
28 | | - .get('/types', () => 'List all types') |
29 | | - .get('/types/:id', ({ params }) => `Get type: ${params.id}`) |
30 | | - |
31 | | -const work = new Elysia({ systemRouter: true }) |
32 | | - .use(workingModule) // GET /items/types returns "List all types" ✅ |
33 | | - // .listen(3000) |
34 | | - |
35 | | -const notWork = new Elysia({ systemRouter: true }) |
36 | | - .use(brokenModule) // GET /items/types returns "Get item: types" ❌ |
37 | | - .use(brokenModuleWithGroup) // GET /items/types returns "Get item: types" ❌ |
38 | | - .listen(3001) |
39 | | - |
40 | | -Bun.serve({ |
41 | | - port: 3002, |
42 | | - routes: { |
43 | | - '/items/types/': { |
44 | | - GET: () => new Response('/items/types/') |
45 | | - }, |
46 | | - '/items/types/:id': { |
47 | | - GET: () => new Response('/items/types/:id') |
| 3 | +const useMount = true // true => hangs, false => works |
| 4 | + |
| 5 | +const handler = async (request: Request) => { |
| 6 | + console.log("A") |
| 7 | + // Rebuild request with new headers (problematic in mount mode) |
| 8 | + const headers = new Headers(request.headers) |
| 9 | + headers.set('x-request-id', 'req_test') |
| 10 | + const patched = new Request(request, { headers }) |
| 11 | + |
| 12 | + // This is where it hangs when mounted |
| 13 | + const body = await patched.text() |
| 14 | + |
| 15 | + return Response.json({ |
| 16 | + ok: true, |
| 17 | + body, |
| 18 | + requestId: patched.headers.get('x-request-id') |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +const app = useMount |
| 23 | + ? new Elysia().mount('/v1/', handler) |
| 24 | + : new Elysia().all('/v1/*', ({ request }) => handler(request)) |
| 25 | + |
| 26 | +app.listen(3000) |
| 27 | + |
| 28 | +app.handle( |
| 29 | + new Request('http://localhost:3000/v1/test', { |
| 30 | + headers: { |
| 31 | + 'content-type': 'hello' |
48 | 32 | }, |
49 | | - '/items/:id': { |
50 | | - GET: () => new Response('/items/:id') |
51 | | - } |
52 | | - } |
53 | | -}) |
| 33 | + body: 'hello' |
| 34 | + }) |
| 35 | +) |
| 36 | + .then((x) => x.status) |
| 37 | + .then(console.log) |
0 commit comments