mirror of
https://github.com/zoriya/elysia-swagger.git
synced 2026-06-03 06:05:35 +00:00
swagger ui options
This commit is contained in:
+4
-1
@@ -33,7 +33,10 @@ const app = new Elysia({
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
swaggerOptions: {
|
||||
persistAuthorization: true
|
||||
},
|
||||
})
|
||||
)
|
||||
.use(plugin)
|
||||
|
||||
@@ -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)
|
||||
@@ -47,6 +47,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/lodash.clonedeep": "^4.5.7",
|
||||
"@types/swagger-ui": "^3.52.0",
|
||||
"lodash.clonedeep": "^4.5.0"
|
||||
}
|
||||
}
|
||||
|
||||
+21
-6
@@ -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>
|
||||
|
||||
@@ -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'
|
||||
>
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user