-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
48 lines (44 loc) · 1.24 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import path from 'node:path'
import { Elysia, t } from 'elysia'
export const app = new Elysia()
.model(
'user',
t.Object({
name: t.String(),
age: t.Number()
})
)
.get('/', () => 'yay')
.post('/user', ({ body }) => body, { body: 'user' })
// below routes are excluded from open-api.json due to `export` above
app
.get('/json', () => Bun.file(path.join(__dirname, 'out', 'open-api.json')))
.get('/swagger', ({ set }) => {
set.headers['content-type'] = 'text/html'
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/json',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>`
})
if (process.env.NODE_ENV !== 'test') {
app.listen(3000)
console.log(`View docs at ${app.server!.url}swagger`)
}