Skip to content

Commit

Permalink
Merge pull request #18 from mohankumargupta/feat-swaggerui-options
Browse files Browse the repository at this point in the history
swagger ui options
  • Loading branch information
SaltyAom committed Sep 26, 2023
2 parents 929c08d + c726674 commit 37e0697
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
5 changes: 4 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const app = new Elysia({
}
}
}
}
},
swaggerOptions: {
persistAuthorization: true
},
})
)
.use(plugin)
Expand Down
46 changes: 46 additions & 0 deletions example/index2.ts
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 21 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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(
`<!DOCTYPE html>
<html lang="en">
Expand All @@ -62,10 +80,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: '${pathWithPrefix}/json',
dom_id: '#swagger-ui',
});
window.ui = SwaggerUIBundle(${stringifiedSwaggerOptions});
};
</script>
</body>
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OpenAPIV3 } from 'openapi-types'
import {SwaggerUIOptions} from 'swagger-ui'

export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
/**
Expand Down Expand Up @@ -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 changes: 17 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 37e0697

Please sign in to comment.