From b58f6379cfe04f5f6e49014c351e64320dc4a558 Mon Sep 17 00:00:00 2001 From: Mohan Gupta Date: Thu, 14 Sep 2023 17:51:41 +1000 Subject: [PATCH] swagger ui options --- example/index.ts | 5 ++++- example/index2.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/index.ts | 27 +++++++++++++++++++++------ src/types.ts | 12 ++++++++++++ test/index.test.ts | 17 +++++++++++++++++ 6 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 example/index2.ts diff --git a/example/index.ts b/example/index.ts index 34640bc..0da5561 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/package.json b/package.json index c257a26..de36398 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ }, "dependencies": { "@types/lodash.clonedeep": "^4.5.7", + "@types/swagger-ui": "^3.52.0", "lodash.clonedeep": "^4.5.0" } } diff --git a/src/index.ts b/src/index.ts index 37615aa..288ce24 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) => { @@ -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( ` @@ -60,10 +78,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 e696f48..2023cb7 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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) + }) })