diff --git a/.changeset/yellow-coins-talk.md b/.changeset/yellow-coins-talk.md new file mode 100644 index 00000000..e4e27e03 --- /dev/null +++ b/.changeset/yellow-coins-talk.md @@ -0,0 +1,5 @@ +--- +'@hono/swagger-ui': patch +--- + +improve SwaggerUI middleware option's type support diff --git a/packages/swagger-ui/src/swagger/renderer.ts b/packages/swagger-ui/src/swagger/renderer.ts index bb7956b5..93874ac8 100644 --- a/packages/swagger-ui/src/swagger/renderer.ts +++ b/packages/swagger-ui/src/swagger/renderer.ts @@ -1,13 +1,16 @@ import type { SwaggerConfigs } from 'swagger-ui-dist' +type RequireOne = K extends keyof T ? PartialRequire : never +type PartialRequire = { + [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', diff --git a/packages/swagger-ui/test/index.test.ts b/packages/swagger-ui/test/index.test.ts index a600ac8c..c65be060 100644 --- a/packages/swagger-ui/test/index.test.ts +++ b/packages/swagger-ui/test/index.test.ts @@ -38,6 +38,32 @@ describe('SwaggerUI Rendering', () => { `) }) + it('use urls for configuration', () => { + expect( + SwaggerUI({ + urls: [ + { + name: 'Petstore', + url, + }, + ], + }).toString() + ).toEqual(` +
+
+ + + +
+ `) + }) + it('renders correctly with custom UI', () => { expect( SwaggerUI({