Skip to content

Commit d3aecbc

Browse files
committed
🎉 feat: 0.8.5
1 parent 48d525a commit d3aecbc

File tree

7 files changed

+220
-165
lines changed

7 files changed

+220
-165
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.8.5 - 24 Jan 2024
2+
Bug fix:
3+
- [#39](https://github.com/elysiajs/elysia-swagger/issues/39) Array type does not work
4+
15
# 0.8.4 - 24 Jan 2024
26
Feature:
37
- [#96](https://github.com/elysiajs/elysia-swagger/pull/96) move to scalar configuration prop

example/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Elysia } from 'elysia'
1+
import { Elysia, InternalRoute } from 'elysia'
22
import { swagger } from '../src/index'
33
import { plugin } from './plugin'
4+
import { registerSchemaPath } from '../src/utils'
45

56
const app = new Elysia()
67
.use(
78
swagger({
9+
provider: 'scalar',
810
documentation: {
911
info: {
1012
title: 'Elysia Scalar',

example/plugin.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,40 @@ export const plugin = new Elysia({
4242
summary: 'Using reference model'
4343
}
4444
})
45-
// .post(
46-
// '/json/:id',
47-
// ({ body, params: { id }, query: { name } }) => ({
48-
// ...body,
49-
// id
50-
// }),
51-
// {
52-
// transform({ params }) {
53-
// params.id = +params.id
54-
// },
55-
// schema: {
56-
// body: 'sign',
57-
// params: t.Object({
58-
// id: t.Number()
59-
// }),
60-
// response: {
61-
// 200: t.Object(
62-
// {
63-
// id: t.Number(),
64-
// username: t.String(),
65-
// password: t.String()
66-
// },
67-
// {
68-
// title: 'User',
69-
// description:
70-
// "Contains user's confidential metadata"
71-
// }
72-
// ),
73-
// 400: t.Object({
74-
// error: t.String()
75-
// })
76-
// },
77-
// detail: {
78-
// summary: 'Transform path parameter'
79-
// }
80-
// }
81-
// }
82-
// )
45+
.post(
46+
'/json/:id',
47+
({ body, params: { id }, query: { name } }) => ({
48+
...body,
49+
id
50+
}),
51+
{
52+
body: 'sign',
53+
params: t.Object({
54+
id: t.Numeric()
55+
}),
56+
response: {
57+
200: t.Object(
58+
{
59+
id: t.Number(),
60+
username: t.String(),
61+
password: t.String()
62+
},
63+
{
64+
title: 'User',
65+
description: "Contains user's confidential metadata"
66+
}
67+
),
68+
418: t.Array(
69+
t.Object({
70+
error: t.String()
71+
})
72+
),
73+
},
74+
detail: {
75+
summary: 'Complex JSON'
76+
}
77+
}
78+
)
8379
.post('/file', ({ body: { file } }) => file, {
8480
type: 'formdata',
8581
body: t.Object({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/swagger",
3-
"version": "0.8.4",
3+
"version": "0.8.5",
44
"description": "Plugin for Elysia to auto-generate Swagger page",
55
"author": {
66
"name": "saltyAom",

src/index.ts

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -62,92 +62,102 @@ export const swagger =
6262

6363
const relativePath = path.startsWith('/') ? path.slice(1) : path
6464

65-
app.get(path, () => {
66-
const combinedSwaggerOptions = {
67-
url: `${relativePath}/json`,
68-
dom_id: '#swagger-ui',
69-
...swaggerOptions
70-
}
71-
const stringifiedSwaggerOptions = JSON.stringify(
72-
combinedSwaggerOptions,
73-
(key, value) => {
74-
if (typeof value == 'function') {
75-
return undefined
76-
} else {
77-
return value
78-
}
65+
app.get(
66+
path,
67+
(() => {
68+
const combinedSwaggerOptions = {
69+
url: `${relativePath}/json`,
70+
dom_id: '#swagger-ui',
71+
...swaggerOptions
7972
}
80-
)
8173

82-
const scalarConfiguration: ReferenceConfiguration = {
83-
spec: {
84-
url: `${relativePath}/json`
85-
},
86-
...scalarConfig
87-
}
74+
const stringifiedSwaggerOptions = JSON.stringify(
75+
combinedSwaggerOptions,
76+
(key, value) => {
77+
if (typeof value == 'function') return undefined
8878

89-
return new Response(
90-
provider === 'swagger-ui'
91-
? SwaggerUIRender(
92-
info,
93-
version,
94-
theme,
95-
stringifiedSwaggerOptions,
96-
autoDarkMode
97-
)
98-
: ScalarRender(scalarVersion, scalarConfiguration, scalarCDN),
99-
{
100-
headers: {
101-
'content-type': 'text/html; charset=utf8'
79+
return value
10280
}
81+
)
82+
83+
const scalarConfiguration: ReferenceConfiguration = {
84+
spec: {
85+
...scalarConfig.spec,
86+
url: `${relativePath}/json`,
87+
},
88+
...scalarConfig
10389
}
104-
)
105-
}).get(`${path}/json`, () => {
106-
const routes = app.routes as InternalRoute[]
107-
108-
if (routes.length !== totalRoutes) {
109-
totalRoutes = routes.length
110-
111-
routes.forEach((route: InternalRoute) => {
112-
if (excludeMethods.includes(route.method)) return
113-
114-
registerSchemaPath({
115-
schema,
116-
hook: route.hooks,
117-
method: route.method,
118-
path: route.path,
119-
// @ts-ignore
120-
models: app.definitions?.type,
121-
contentType: route.hooks.type
122-
})
123-
})
124-
}
12590

126-
return {
127-
openapi: '3.0.3',
128-
...{
129-
...documentation,
130-
info: {
131-
title: 'Elysia Documentation',
132-
description: 'Development documentation',
133-
version: '0.0.0',
134-
...documentation.info
135-
}
136-
},
137-
paths: filterPaths(schema, {
138-
excludeStaticFile,
139-
exclude: Array.isArray(exclude) ? exclude : [exclude]
140-
}),
141-
components: {
142-
...documentation.components,
143-
schemas: {
144-
// @ts-ignore
145-
...app.definitions?.type,
146-
...documentation.components?.schemas
91+
return new Response(
92+
provider === 'swagger-ui'
93+
? SwaggerUIRender(
94+
info,
95+
version,
96+
theme,
97+
stringifiedSwaggerOptions,
98+
autoDarkMode
99+
)
100+
: ScalarRender(
101+
scalarVersion,
102+
scalarConfiguration,
103+
scalarCDN
104+
),
105+
{
106+
headers: {
107+
'content-type': 'text/html; charset=utf8'
108+
}
147109
}
110+
)
111+
})()
112+
).get(
113+
`${path}/json`,
114+
() => {
115+
const routes = app.routes as InternalRoute[]
116+
117+
if (routes.length !== totalRoutes) {
118+
totalRoutes = routes.length
119+
120+
routes.forEach((route: InternalRoute) => {
121+
if (excludeMethods.includes(route.method)) return
122+
123+
registerSchemaPath({
124+
schema,
125+
hook: route.hooks,
126+
method: route.method,
127+
path: route.path,
128+
// @ts-ignore
129+
models: app.definitions?.type,
130+
contentType: route.hooks.type
131+
})
132+
})
148133
}
149-
} satisfies OpenAPIV3.Document
150-
})
134+
135+
return {
136+
openapi: '3.0.3',
137+
...{
138+
...documentation,
139+
info: {
140+
title: 'Elysia Documentation',
141+
description: 'Development documentation',
142+
version: '0.0.0',
143+
...documentation.info
144+
}
145+
},
146+
paths: filterPaths(schema, {
147+
excludeStaticFile,
148+
exclude: Array.isArray(exclude) ? exclude : [exclude]
149+
}),
150+
components: {
151+
...documentation.components,
152+
schemas: {
153+
// @ts-ignore
154+
...app.definitions?.type,
155+
...documentation.components?.schemas
156+
}
157+
}
158+
} satisfies OpenAPIV3.Document
159+
}
160+
)
151161

152162
// This is intentional to prevent deeply nested type
153163
return app

0 commit comments

Comments
 (0)