-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathindex.ts
78 lines (69 loc) · 2.13 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Env, MiddlewareHandler } from 'hono'
import type { RedocRawOptions } from 'redoc'
import { renderRedocOptions } from './redoc/renderer'
import type { AssetURLs } from './redoc/resource'
import { remoteAssets } from './redoc/resource'
type OriginalRedocOptions = {
version?: string
/**
* manuallyRedocHtml is a function that returns a string to customize the ReDoc HTML.
* All options except for the version are ignored.
*
* @example
* const redocUI = RedocUI({
* manuallyRedocHtml: (asset) => `
* <div>
* <script src="${asset.js[0]}" crossorigin="anonymous"></script>
* <div id="redoc-container"></div>
* <script>
* Redoc.init('https://petstore.swagger.io/v2/swagger.json', {}, document.getElementById('redoc-container'));
* </script>
* </div>
* `,
* })
*/
manuallyReDocHtml?: (asset: AssetURLs) => string
title?: string
}
type RedocOptions = OriginalRedocOptions &
RedocRawOptions & {
url: string
}
const ReDoc = (options: RedocOptions): string => {
const asset = remoteAssets({ version: options?.version })
delete options.version
if (options.manuallyReDocHtml) {
return options.manuallyReDocHtml(asset)
}
const optionsStrings = renderRedocOptions(options)
return `
<div>
<script src="${asset.js[0]}" crossorigin="anonymous"></script>
<div id="redoc-container"></div>
<script>
Redoc.init('${options.url}', {${optionsStrings}}, document.getElementById('redoc-container'));
</script>
</div>
`
}
const middleware =
<E extends Env>(options: RedocOptions): MiddlewareHandler<E> =>
async (c) => {
const title = options?.title ?? 'ReDoc'
return c.html(/* html */ `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="ReDoc" />
<title>${title}</title>
</head>
<body>
${ReDoc(options)}
</body>
</html>
`)
}
export { middleware as redoc, ReDoc }
export { RedocOptions }