diff --git a/example/index.ts b/example/index.ts index 843cec4..b7b531e 100644 --- a/example/index.ts +++ b/example/index.ts @@ -33,7 +33,10 @@ const app = new Elysia({ } } } - } + }, + swaggerOptions: { + persistAuthorization: true + }, }) ) .use(plugin) diff --git a/example/index2.ts b/example/index2.ts new file mode 100644 index 0000000..badcd80 --- /dev/null +++ b/example/index2.ts @@ -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) diff --git a/src/index.ts b/src/index.ts index 55e8032..7ebc155 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,13 +17,15 @@ export const swagger = version = '4.18.2', excludeStaticFile = true, path = '/swagger' as Path, - exclude = [] + exclude = [], + swaggerOptions = {}, }: ElysiaSwaggerConfig = { documentation: {}, version: '4.18.2', excludeStaticFile: true, path: '/swagger' as Path, - exclude: [] + exclude: [], + swaggerOptions: {}, } ) => (app: Elysia) => { @@ -40,6 +42,22 @@ export const swagger = const pathWithPrefix = `${app.config.prefix}${path}`; app.get(path, () => { + const combinedSwaggerOptions = { + url: '${pathWithPrefix}/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( ` @@ -62,10 +80,7 @@ export const swagger = diff --git a/src/types.ts b/src/types.ts index 860af2b..e2085fb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import type { OpenAPIV3 } from 'openapi-types' +import {SwaggerUIOptions} from 'swagger-ui' export interface ElysiaSwaggerConfig { /** @@ -37,4 +38,15 @@ export interface ElysiaSwaggerConfig { * @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, + 'dom_id'|'dom_node'|'spec'|'url'|'urls' + |'layout' | 'pluginsOptions' | 'plugins'|'presets' + |'onComplete' |'requestInterceptor'|'responseInterceptor' + |'modelPropertyMacro'|'parameterMacro' + > } diff --git a/test/index.test.ts b/test/index.test.ts index f100a51..d3bca67 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -66,6 +66,23 @@ describe('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) + }) + it('should not return content response when using Void type', async () => { const app = new Elysia().use( swagger())