From a6269edef9d417b65b1718095772acbea0c470ef Mon Sep 17 00:00:00 2001 From: KaKa Date: Thu, 11 Mar 2021 17:47:18 +0800 Subject: [PATCH] fix: uiConfig types and initOAuth in static mode (#381) * fix: missing initOAuth in static mode * fix: missing uiConfig typings --- index.d.ts | 45 ++++++++++++++++++++++++++--------- lib/mode/static.js | 1 + test/mode/static.js | 51 ++++++++++++++++++++++++++++++++++++++++ test/types/types.test.ts | 13 +++++++++- 4 files changed, 98 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3b457ae6..2146d175 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,6 +44,40 @@ export interface FastifySwaggerOptions { * @default false */ exposeRoute?: boolean; + /** + * Swagger UI Config + */ + uiConfig?: Partial<{ + deepLinking: boolean + displayOperationId: boolean + defaultModelsExpandDepth: number + defaultModelExpandDepth: number + defaultModelRendering: string + displayRequestDuration: boolean + docExpansion: string + filter: boolean | string + maxDisplayedTags: number + showExtensions: boolean + showCommonExtensions: boolean + useUnsafeMarkdown: boolean + syntaxHighlight: { + activate?: boolean + theme?: string + } | false + tryItOutEnabled: boolean + }> + + initOAuth?: Partial<{ + clientId: string, + clientSecret: string, + realm: string, + appName: string, + scopeSeparator: string, + scopes: string | string[], + additionalQueryStringParams: { [key: string]: any }, + useBasicAuthenticationWithAccessCodeGrant: boolean, + usePkceWithAuthorizationCodeGrant: boolean + }> } export interface FastifyDynamicSwaggerOptions extends FastifySwaggerOptions { @@ -60,17 +94,6 @@ export interface FastifyDynamicSwaggerOptions extends FastifySwaggerOptions { * Overwrite the route schema */ transform?: Function; - initOAuth?: Partial<{ - clientId: string, - clientSecret: string, - realm: string, - appName: string, - scopeSeparator: string, - scopes: string | string[], - additionalQueryStringParams: { [key: string]: any }, - useBasicAuthenticationWithAccessCodeGrant: boolean, - usePkceWithAuthorizationCodeGrant: boolean - }> } export interface StaticPathSpec { diff --git a/lib/mode/static.js b/lib/mode/static.js index a0cbe3fe..44f1dde6 100644 --- a/lib/mode/static.js +++ b/lib/mode/static.js @@ -62,6 +62,7 @@ module.exports = function (fastify, opts, done) { const options = { prefix: opts.routePrefix || '/documentation', uiConfig: opts.uiConfig || {}, + initOAuth: opts.initOAuth || {}, baseDir: opts.specification.baseDir } diff --git a/test/mode/static.js b/test/mode/static.js index f652d8c2..33f39650 100644 --- a/test/mode/static.js +++ b/test/mode/static.js @@ -805,6 +805,57 @@ test('/documentation/uiConfig can be customize', t => { }) }) +test('/documentation/initOAuth should have default', t => { + const config = { + exposeRoute: true, + mode: 'static', + specification: { + path: './examples/example-static-specification.yaml', + baseDir: resolve(__dirname, '..', '..', 'static') + } + } + + t.plan(3) + const fastify = new Fastify() + fastify.register(fastifySwagger, config) + + fastify.inject({ + method: 'GET', + url: '/documentation/initOAuth' + }, (err, res) => { + t.error(err) + t.strictEqual(res.statusCode, 200) + t.strictEqual(res.payload, '{}') + }) +}) + +test('/documentation/initOAuth can be customize', t => { + const config = { + exposeRoute: true, + mode: 'static', + specification: { + path: './examples/example-static-specification.yaml', + baseDir: resolve(__dirname, '..', '..', 'static') + }, + initOAuth: { + scopes: ['openid', 'profile', 'email', 'offline_access'] + } + } + + t.plan(3) + const fastify = new Fastify() + fastify.register(fastifySwagger, config) + + fastify.inject({ + method: 'GET', + url: '/documentation/initOAuth' + }, (err, res) => { + t.error(err) + t.strictEqual(res.statusCode, 200) + t.strictEqual(res.payload, '{"scopes":["openid","profile","email","offline_access"]}') + }) +}) + test('should still return valid swagger object when missing package.json', t => { const config = { mode: 'dynamic', diff --git a/test/types/types.test.ts b/test/types/types.test.ts index 8a4f1e4c..0d41c7b6 100644 --- a/test/types/types.test.ts +++ b/test/types/types.test.ts @@ -118,4 +118,15 @@ app }) .ready((err) => { app.swagger(); - }); \ No newline at end of file + }); + +app.register(fastifySwagger, { + uiConfig: { + deepLinking: true, + defaultModelsExpandDepth: -1, + defaultModelExpandDepth: 1 + } +}) +.ready((err) => { + app.swagger(); +}) \ No newline at end of file