fix: swaggerUI middleware option's required property (#329)

* feat: make swaggerUI middleware option optional

* chore: changeset
pull/332/head
Sor4chi 2023-12-29 21:49:35 +09:00 committed by GitHub
parent 7844bfd4a1
commit 5b17228c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/swagger-ui': patch
---
improve SwaggerUI middleware option's type support

View File

@ -1,13 +1,16 @@
import type { SwaggerConfigs } from 'swagger-ui-dist' import type { SwaggerConfigs } from 'swagger-ui-dist'
type RequireOne<T, K extends keyof T = keyof T> = K extends keyof T ? PartialRequire<T, K> : never
type PartialRequire<O, K extends keyof O> = {
[P in K]-?: O[P]
} & O
export type DistSwaggerUIOptions = { export type DistSwaggerUIOptions = {
configUrl?: SwaggerConfigs['configUrl'] configUrl?: SwaggerConfigs['configUrl']
deepLinking?: SwaggerConfigs['deepLinking'] deepLinking?: SwaggerConfigs['deepLinking']
presets?: string[] presets?: string[]
plugins?: string[] plugins?: string[]
spec?: SwaggerConfigs['spec'] spec?: SwaggerConfigs['spec']
url?: SwaggerConfigs['url']
urls?: SwaggerConfigs['urls']
layout?: SwaggerConfigs['layout'] layout?: SwaggerConfigs['layout']
docExpansion?: SwaggerConfigs['docExpansion'] docExpansion?: SwaggerConfigs['docExpansion']
maxDisplayedTags?: SwaggerConfigs['maxDisplayedTags'] maxDisplayedTags?: SwaggerConfigs['maxDisplayedTags']
@ -15,7 +18,10 @@ export type DistSwaggerUIOptions = {
requestInterceptor?: string requestInterceptor?: string
responseInterceptor?: string responseInterceptor?: string
persistAuthorization?: boolean persistAuthorization?: boolean
} } & RequireOne<{
url?: SwaggerConfigs['url']
urls?: SwaggerConfigs['urls']
}> // least one of `url` or `urls` is required because the output html will be broken if both are missing
const RENDER_TYPE = { const RENDER_TYPE = {
STRING_ARRAY: 'string_array', STRING_ARRAY: 'string_array',

View File

@ -38,6 +38,32 @@ describe('SwaggerUI Rendering', () => {
`) `)
}) })
it('use urls for configuration', () => {
expect(
SwaggerUI({
urls: [
{
name: 'Petstore',
url,
},
],
}).toString()
).toEqual(`
<div>
<div id="swagger-ui"></div>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
dom_id: '#swagger-ui',urls: [{"name":"Petstore","url":"${url}"}],
})
}
</script>
</div>
`)
})
it('renders correctly with custom UI', () => { it('renders correctly with custom UI', () => {
expect( expect(
SwaggerUI({ SwaggerUI({