-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.ts
63 lines (57 loc) · 2 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// <reference lib="deno.ns" />
import { Hono } from "hono/mod.ts"
import { html } from "hono/middleware/html/index.ts"
import { generateOpenApi } from "npm:@ts-rest/open-api"
import type { InfoObject } from "npm:[email protected]"
import { contract } from "../contracts/src/mod.ts"
export const info = {
title: "Reviews Patch API (WIP)",
version: "0.0.2-alpha",
} satisfies InfoObject
export const specs = generateOpenApi(contract, { info }, {
setOperationId: false,
})
export const swaggerUrl = "/swagger-v2"
export const swaggerJsonUrl = `${swaggerUrl}/openapi.json`
const id = "swagger-ui"
type SwaggerOption = {
path: string | URL
info: InfoObject
version?: string
}
export const swaggerUiByUrl = (
{ info: { title, description }, path, version = "4.18.2" }: SwaggerOption,
) =>
html`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>${title}</title>
<meta name="description" content="${description}" />
<meta name="og:description" content="${description}" />
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css" />
</head>
<body>
<div id="${id}"></div>
<script src="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({ url: "${path}", dom_id: "#${id}" })
}
</script>
</body>
</html>
`
if (import.meta.main) {
const { logger } = await import("hono/middleware.ts")
const app = new Hono()
.use("*", logger())
.get(swaggerJsonUrl, (c) => c.json(specs))
.get(
swaggerUrl,
(c) => c.html(swaggerUiByUrl({ info, path: swaggerJsonUrl })),
)
Deno.serve({ port: 8000 }, app.fetch)
}