|
1 |
| -import { Elysia } from 'elysia' |
2 |
| -import { html, Fragment } from '../src/index' |
| 1 | +import { Elysia, t } from 'elysia' |
| 2 | +import { html } from '../src' |
3 | 3 |
|
4 |
| -const page = `<html lang="en"> |
5 |
| - <head> |
6 |
| - <title>Hello World</title> |
7 |
| - </head> |
8 |
| - <body> |
9 |
| - <h1>Hello World</h1> |
10 |
| - </body> |
11 |
| -</html>` |
| 4 | +function page({ name }: { name: string }): string { |
| 5 | + return ` |
| 6 | + <html lang="en"> |
| 7 | + <head> |
| 8 | + <title>Hello ${name}!</title> |
| 9 | + </head> |
| 10 | + <body> |
| 11 | + <h1>Hello ${name}!</h1> |
| 12 | + </body> |
| 13 | + </html> |
| 14 | + ` |
| 15 | +} |
12 | 16 |
|
13 |
| -const jsx = ( |
14 |
| - <html lang="en"> |
15 |
| - <head hx-a="A"> |
16 |
| - <title>Hello World</title> |
17 |
| - </head> |
18 |
| - <body> |
19 |
| - <h1>Hello World</h1> |
20 |
| - </body> |
21 |
| - </html> |
22 |
| -) |
| 17 | +// https://elysiajs.com/plugins/html.html#jsx |
| 18 | +function tsxPage({ name }: { name: string }): string { |
| 19 | + return ( |
| 20 | + <html lang="en"> |
| 21 | + <head> |
| 22 | + <title>Hello ${name}!</title> |
| 23 | + </head> |
| 24 | + <body> |
| 25 | + <h1>Hello ${name}!</h1> |
| 26 | + </body> |
| 27 | + </html> |
| 28 | + ) |
| 29 | +} |
23 | 30 |
|
24 |
| -const app = new Elysia() |
25 |
| - .use(html()) |
26 |
| - .get('/', () => page) |
27 |
| - .get('/jsx', () => jsx) |
28 |
| - .get('/html', ({ html }) => html(page)) |
29 |
| - .get('/a', () => ( |
30 |
| - <> |
31 |
| - <h1>Hello World</h1> |
32 |
| - </> |
33 |
| - )) |
34 |
| - .listen(8080) |
| 31 | +export function createApp() { |
| 32 | + // https://xelysiajs.com/concept/schema.html |
| 33 | + const indexSchema = { |
| 34 | + params: t.Object({ |
| 35 | + name: t.String({ default: 'World' }) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + new Elysia() |
| 41 | + // https://elysiajs.com/plugins/html.html#options |
| 42 | + .use(html()) |
| 43 | + .get('/', ({ params }) => page(params), indexSchema) |
| 44 | + .get('/tsx', ({ params }) => tsxPage(params), indexSchema) |
| 45 | + .listen(8080) |
| 46 | + ) |
| 47 | +} |
0 commit comments