swagger ui options

This commit is contained in:
Mohan Gupta
2023-09-14 17:51:41 +10:00
parent 47a5b26abc
commit b58f6379cf
6 changed files with 101 additions and 7 deletions
+4 -1
View File
@@ -33,7 +33,10 @@ const app = new Elysia({
}
}
}
}
},
swaggerOptions: {
persistAuthorization: true
},
})
)
.use(plugin)
+46
View File
@@ -0,0 +1,46 @@
import { Elysia } from 'elysia'
import { swagger } from '../src/index'
import { plugin } from './plugin'
const app = new Elysia({
// aot: false
})
.use(
swagger({
documentation: {
info: {
title: 'Elysia',
version: '0.6.10'
},
tags: [
{
name: 'Test',
description: 'Hello'
}
],
security: [
{JwtAuth: []}
],
components: {
schemas: {
User: {
description: 'string'
}
},
securitySchemes: {
JwtAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Enter JWT Bearer token **_only_**'
}
}
}
},
swaggerOptions: {
persistAuthorization: true
},
})
)
.use(plugin)
.listen(8080)
+1
View File
@@ -47,6 +47,7 @@
},
"dependencies": {
"@types/lodash.clonedeep": "^4.5.7",
"@types/swagger-ui": "^3.52.0",
"lodash.clonedeep": "^4.5.0"
}
}
+21 -6
View File
@@ -17,13 +17,15 @@ export const swagger =
version = '4.18.2',
excludeStaticFile = true,
path = '/swagger' as Path,
exclude = []
exclude = [],
swaggerOptions = {},
}: ElysiaSwaggerConfig<Path> = {
documentation: {},
version: '4.18.2',
excludeStaticFile: true,
path: '/swagger' as Path,
exclude: []
exclude: [],
swaggerOptions: {},
}
) =>
(app: Elysia) => {
@@ -38,6 +40,22 @@ export const swagger =
}
app.get(path, () => {
const combinedSwaggerOptions = {
url: `${path}/json`,
dom_id: '#swagger-ui',
...swaggerOptions
}
const stringifiedSwaggerOptions = JSON.stringify(combinedSwaggerOptions,
(key,value) => {
if (typeof value == "function") {
return undefined;
}
else {
return value;
}
}
)
return new Response(
`<!DOCTYPE html>
<html lang="en">
@@ -60,10 +78,7 @@ export const swagger =
<script src="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '${path}/json',
dom_id: '#swagger-ui',
});
window.ui = SwaggerUIBundle(${stringifiedSwaggerOptions});
};
</script>
</body>
+12
View File
@@ -1,4 +1,5 @@
import type { OpenAPIV3 } from 'openapi-types'
import {SwaggerUIOptions} from 'swagger-ui'
export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
/**
@@ -37,4 +38,15 @@ export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
* @default []
*/
exclude?: string | RegExp | (string | RegExp)[]
/**
* Options to send to SwaggerUIBundle
* Currently, options that are defined as functions such as requestInterceptor
* and onComplete are not supported.
*/
swaggerOptions?: Omit<Partial<SwaggerUIOptions>,
'dom_id'|'dom_node'|'spec'|'url'|'urls'
|'layout' | 'pluginsOptions' | 'plugins'|'presets'
|'onComplete' |'requestInterceptor'|'responseInterceptor'
|'modelPropertyMacro'|'parameterMacro'
>
}
+17
View File
@@ -65,4 +65,21 @@ describe('Swagger', () => {
const res = await app.handle(req('/v2/swagger'))
expect(res.status).toBe(200)
})
it('Swagger UI options', async () => {
const app = new Elysia().use(
swagger({
swaggerOptions: {
persistAuthorization: true
}
})
)
const res = await app.handle(req('/swagger')).then((x) => x.text())
const expected = `
window.onload = () => {
window.ui = SwaggerUIBundle({"url":"/swagger/json","dom_id":"#swagger-ui","persistAuthorization":true});
};
`
expect(res.trim().includes(expected.trim())).toBe(true)
})
})