Skip to content

Commit

Permalink
fix: uiConfig types and initOAuth in static mode (#381)
Browse files Browse the repository at this point in the history
* fix: missing initOAuth in static mode

* fix: missing uiConfig typings
  • Loading branch information
climba03003 authored Mar 11, 2021
1 parent 9532d51 commit a6269ed
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
45 changes: 34 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lib/mode/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
51 changes: 51 additions & 0 deletions test/mode/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 12 additions & 1 deletion test/types/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,15 @@ app
})
.ready((err) => {
app.swagger();
});
});

app.register(fastifySwagger, {
uiConfig: {
deepLinking: true,
defaultModelsExpandDepth: -1,
defaultModelExpandDepth: 1
}
})
.ready((err) => {
app.swagger();
})

0 comments on commit a6269ed

Please sign in to comment.