/
index.ts
166 lines (148 loc) · 5.47 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { type Elysia, type InternalRoute } from 'elysia'
import { SwaggerUIRender } from './swagger'
import { ScalarRender } from './scalar'
import { filterPaths, registerSchemaPath } from './utils'
import type { OpenAPIV3 } from 'openapi-types'
import type { ReferenceConfiguration } from './scalar/types'
import type { ElysiaSwaggerConfig } from './types'
/**
* Plugin for [elysia](https://github.com/elysiajs/elysia) that auto-generate Swagger page.
*
* @see https://github.com/elysiajs/elysia-swagger
*/
export const swagger =
<Path extends string = '/swagger'>(
{
provider = 'scalar',
scalarVersion = 'latest',
scalarCDN = '',
scalarConfig = {},
documentation = {},
version = '5.9.0',
excludeStaticFile = true,
path = '/swagger' as Path,
exclude = [],
swaggerOptions = {},
theme = `https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css`,
autoDarkMode = true,
excludeMethods = ['OPTIONS']
}: ElysiaSwaggerConfig<Path> = {
provider: 'scalar',
scalarVersion: 'latest',
scalarCDN: '',
scalarConfig: {},
documentation: {},
version: '5.9.0',
excludeStaticFile: true,
path: '/swagger' as Path,
exclude: [],
swaggerOptions: {},
autoDarkMode: true,
excludeMethods: ['OPTIONS']
}
) =>
(app: Elysia) => {
const schema = {}
let totalRoutes = 0
if (!version)
version = `https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css`
const info = {
title: 'Elysia Documentation',
description: 'Development documentation',
version: '0.0.0',
...documentation.info
}
const relativePath = path.startsWith('/') ? path.slice(1) : path
app.get(
path,
() => {
const combinedSwaggerOptions = {
url: `${relativePath}/json`,
dom_id: '#swagger-ui',
...swaggerOptions
}
const stringifiedSwaggerOptions = JSON.stringify(
combinedSwaggerOptions,
(key, value) => {
if (typeof value == 'function') return undefined
return value
}
)
const scalarConfiguration: ReferenceConfiguration = {
spec: {
...scalarConfig.spec,
url: `${relativePath}/json`,
},
...scalarConfig
}
return new Response(
provider === 'swagger-ui'
? SwaggerUIRender(
info,
version,
theme,
stringifiedSwaggerOptions,
autoDarkMode
)
: ScalarRender(
scalarVersion,
scalarConfiguration,
scalarCDN
),
{
headers: {
'content-type': 'text/html; charset=utf8'
}
}
)
}
).get(
`${path}/json`,
() => {
const routes = app.routes as InternalRoute[]
if (routes.length !== totalRoutes) {
totalRoutes = routes.length
routes.forEach((route: InternalRoute) => {
if (excludeMethods.includes(route.method)) return
registerSchemaPath({
schema,
hook: route.hooks,
method: route.method,
path: route.path,
// @ts-ignore
models: app.definitions?.type,
contentType: route.hooks.type
})
})
}
return {
openapi: '3.0.3',
...{
...documentation,
info: {
title: 'Elysia Documentation',
description: 'Development documentation',
version: '0.0.0',
...documentation.info
}
},
paths: filterPaths(schema, {
excludeStaticFile,
exclude: Array.isArray(exclude) ? exclude : [exclude]
}),
components: {
...documentation.components,
schemas: {
// @ts-ignore
...app.definitions?.type,
...documentation.components?.schemas
}
}
} satisfies OpenAPIV3.Document
}
)
// This is intentional to prevent deeply nested type
return app
}
export default swagger