fix: swaggerUI middleware option's required property (#329)
* feat: make swaggerUI middleware option optional * chore: changesetpull/332/head
parent
7844bfd4a1
commit
5b17228c0b
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@hono/swagger-ui': patch
|
||||
---
|
||||
|
||||
improve SwaggerUI middleware option's type support
|
|
@ -1,13 +1,16 @@
|
|||
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 = {
|
||||
configUrl?: SwaggerConfigs['configUrl']
|
||||
deepLinking?: SwaggerConfigs['deepLinking']
|
||||
presets?: string[]
|
||||
plugins?: string[]
|
||||
spec?: SwaggerConfigs['spec']
|
||||
url?: SwaggerConfigs['url']
|
||||
urls?: SwaggerConfigs['urls']
|
||||
layout?: SwaggerConfigs['layout']
|
||||
docExpansion?: SwaggerConfigs['docExpansion']
|
||||
maxDisplayedTags?: SwaggerConfigs['maxDisplayedTags']
|
||||
|
@ -15,7 +18,10 @@ export type DistSwaggerUIOptions = {
|
|||
requestInterceptor?: string
|
||||
responseInterceptor?: string
|
||||
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 = {
|
||||
STRING_ARRAY: 'string_array',
|
||||
|
|
|
@ -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', () => {
|
||||
expect(
|
||||
SwaggerUI({
|
||||
|
|
Loading…
Reference in New Issue