-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.ts
165 lines (146 loc) · 3.8 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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { 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 = async <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'],
excludeTags = []
}: 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'],
excludeTags: []
}
) => {
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
const app = new Elysia({ name: '@elysiajs/swagger' })
app.get(path, function documentation() {
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' : `${path}/json`, function openAPISchema() {
// @ts-expect-error Private property
const routes = app.getGlobalRoutes() 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,
tags: documentation.tags?.filter(
(tag) => !excludeTags?.includes(tag?.name)
),
info: {
title: 'Elysia Documentation',
description: 'Development documentation',
version: '0.0.0',
...documentation.info
}
},
paths: {
...filterPaths(schema, relativePath, {
excludeStaticFile,
exclude: Array.isArray(exclude) ? exclude : [exclude]
}),
...documentation.paths
},
components: {
...documentation.components,
schemas: {
// @ts-ignore
...app.definitions?.type,
...documentation.components?.schemas
}
}
} satisfies OpenAPIV3.Document
})
return app
}
export type { ElysiaSwaggerConfig }
export default swagger