Skip to content

Commit

Permalink
🎉 feat: 0.8.5
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Jan 24, 2024
1 parent 48d525a commit d3aecbc
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 165 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.8.5 - 24 Jan 2024
Bug fix:
- [#39](https://github.com/elysiajs/elysia-swagger/issues/39) Array type does not work

# 0.8.4 - 24 Jan 2024
Feature:
- [#96](https://github.com/elysiajs/elysia-swagger/pull/96) move to scalar configuration prop
Expand Down
4 changes: 3 additions & 1 deletion example/index.ts
@@ -1,10 +1,12 @@
import { Elysia } from 'elysia'
import { Elysia, InternalRoute } from 'elysia'
import { swagger } from '../src/index'
import { plugin } from './plugin'
import { registerSchemaPath } from '../src/utils'

const app = new Elysia()
.use(
swagger({
provider: 'scalar',
documentation: {
info: {
title: 'Elysia Scalar',
Expand Down
72 changes: 34 additions & 38 deletions example/plugin.ts
Expand Up @@ -42,44 +42,40 @@ export const plugin = new Elysia({
summary: 'Using reference model'
}
})
// .post(
// '/json/:id',
// ({ body, params: { id }, query: { name } }) => ({
// ...body,
// id
// }),
// {
// transform({ params }) {
// params.id = +params.id
// },
// schema: {
// body: 'sign',
// params: t.Object({
// id: t.Number()
// }),
// response: {
// 200: t.Object(
// {
// id: t.Number(),
// username: t.String(),
// password: t.String()
// },
// {
// title: 'User',
// description:
// "Contains user's confidential metadata"
// }
// ),
// 400: t.Object({
// error: t.String()
// })
// },
// detail: {
// summary: 'Transform path parameter'
// }
// }
// }
// )
.post(
'/json/:id',
({ body, params: { id }, query: { name } }) => ({
...body,
id
}),
{
body: 'sign',
params: t.Object({
id: t.Numeric()
}),
response: {
200: t.Object(
{
id: t.Number(),
username: t.String(),
password: t.String()
},
{
title: 'User',
description: "Contains user's confidential metadata"
}
),
418: t.Array(
t.Object({
error: t.String()
})
),
},
detail: {
summary: 'Complex JSON'
}
}
)
.post('/file', ({ body: { file } }) => file, {
type: 'formdata',
body: t.Object({
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@elysiajs/swagger",
"version": "0.8.4",
"version": "0.8.5",
"description": "Plugin for Elysia to auto-generate Swagger page",
"author": {
"name": "saltyAom",
Expand Down
166 changes: 88 additions & 78 deletions src/index.ts
Expand Up @@ -62,92 +62,102 @@ export const swagger =

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
} else {
return value
}
app.get(
path,
(() => {
const combinedSwaggerOptions = {
url: `${relativePath}/json`,
dom_id: '#swagger-ui',
...swaggerOptions
}
)

const scalarConfiguration: ReferenceConfiguration = {
spec: {
url: `${relativePath}/json`
},
...scalarConfig
}
const stringifiedSwaggerOptions = JSON.stringify(
combinedSwaggerOptions,
(key, value) => {
if (typeof value == 'function') return undefined

return new Response(
provider === 'swagger-ui'
? SwaggerUIRender(
info,
version,
theme,
stringifiedSwaggerOptions,
autoDarkMode
)
: ScalarRender(scalarVersion, scalarConfiguration, scalarCDN),
{
headers: {
'content-type': 'text/html; charset=utf8'
return value
}
)

const scalarConfiguration: ReferenceConfiguration = {
spec: {
...scalarConfig.spec,
url: `${relativePath}/json`,
},
...scalarConfig
}
)
}).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
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
})
})
}
} satisfies OpenAPIV3.Document
})

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
Expand Down

0 comments on commit d3aecbc

Please sign in to comment.