Skip to content

Commit

Permalink
Use Fastify logos by default (#49)
Browse files Browse the repository at this point in the history
* Use Fastify logos by default

Signed-off-by: Matteo Collina <[email protected]>

* add demo in the README

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Mar 28, 2023
1 parent bf84b4d commit a297105
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 6 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ A Fastify plugin for serving [Swagger UI](https://swagger.io/tools/swagger-ui/).

Supports Fastify versions `4.x`.

![Demo](./demo.png)

<a name="install"></a>
## Install
```
Expand Down Expand Up @@ -199,6 +201,7 @@ await fastify.register(require('@fastify/swagger-ui'), {
You can add custom JavaScript and CSS to the Swagger UI web page by using the theme option.

##### Example

```js
const fastify = require('fastify')()

Expand All @@ -225,6 +228,32 @@ await fastify.register(require('@fastify/swagger-ui'), {
})
```

You can add custom JavaScript and CSS to the Swagger UI web page by using the theme option.

#### logo

It's possible to override the logo displayed in the top bar by specifying:

```
await fastify.register(require('@fastify/swagger-ui'), {
logo: {
type: 'image/png',
content: Buffer.from('iVBOR...', 'base64')
},
theme: {
favicon: [
{
filename: 'favicon.png',
rel: 'icon',
sizes: '16x16',
type: 'image/png',
content: Buffer.from('iVBOR...', 'base64')
}
]
}
})
```

#### Protect your documentation routes

You can protect your documentation by configuring an authentication hook.
Expand Down
Binary file added demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

const fp = require('fastify-plugin')
const { readFile } = require('fs/promises')

function fastifySwaggerUi (fastify, opts, next) {
async function fastifySwaggerUi (fastify, opts) {
fastify.decorate('swaggerCSP', require('./static/csp.json'))

fastify.register(require('./lib/routes'), {
Expand All @@ -11,10 +12,9 @@ function fastifySwaggerUi (fastify, opts, next) {
initOAuth: opts.initOAuth || {},
hooks: opts.uiHooks,
theme: opts.theme || {},
logo: opts.logo || { type: 'image/svg+xml', content: await readFile(require.resolve('./static/logo.svg')) },
...opts
})

next()
}

module.exports = fp(fastifySwaggerUi, {
Expand Down
11 changes: 10 additions & 1 deletion lib/swagger-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const serialize = require('./serialize')

function swaggerInitializer (opts) {
const logoBase64 = Buffer.from(opts.logo.content).toString('base64')
const logoData = `data:${opts.logo.type};base64,${logoBase64}`

return `window.onload = function () {
function resolveUrl(url) {
const anchor = document.createElement('a')
Expand All @@ -28,7 +31,13 @@ function swaggerInitializer (opts) {
});
const ui = SwaggerUIBundle(resConfig)
window.ui = ui
const logoData = '${logoData}'
if (logoData) {
const img = document.querySelector('#swagger-ui > section > div.topbar > div > div > a > img')
img.src = logoData
}
ui.initOAuth(${serialize(opts.initOAuth)})
}`
}
Expand Down
43 changes: 43 additions & 0 deletions logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions scripts/prepare-swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ fse.emptyDirSync(resolve(`./${folderName}`))

// since the original swagger-ui-dist folder contains non UI files
const filesToCopy = [
'favicon-16x16.png',
'favicon-32x32.png',
'index.html',
'index.css',
'oauth2-redirect.html',
Expand All @@ -28,6 +26,15 @@ filesToCopy.forEach(filename => {
fse.copySync(`${swaggerUiAssetPath}/${filename}`, resolve(`./static/${filename}`))
})

const overrides = [
'favicon-16x16.png',
'favicon-32x32.png',
'logo.svg'
]
overrides.forEach(filename => {
fse.copySync(`./${filename}`, resolve(`./static/${filename}`))
})

const sha = {
script: [],
style: []
Expand Down
16 changes: 16 additions & 0 deletions test/swagger-initializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ test('/documentation/static/swagger-initializer.js should have configurable init
t.equal(res.statusCode, 200)
t.ok(res.payload.includes('ui.initOAuth({"clientId":"someId"})'))
})

test('customize logo', async (t) => {
const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}

const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, { logo: { type: 'image/png', content: 'foobar' } })

const res = await fastify.inject('/documentation/static/swagger-initializer.js')
t.equal(res.body.indexOf(Buffer.from('foobar').toString('base64')) > -1, true)
})
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ declare namespace fastifySwaggerUi {

theme?: FastifySwaggerUiTheme

logo?: FastifySwaggerUILogo

transformSpecification?: (swaggerObject: Readonly<Record<string, any>>, request: FastifyRequest, reply: FastifyReply) => Record<string, any>
transformSpecificationClone?: boolean
}
Expand All @@ -63,6 +65,11 @@ declare namespace fastifySwaggerUi {
favicon: { filename: string; rel: string; type: string; sizes: string; content: string; }[];
}

type FastifySwaggerUILogo = {
type: string;
content: string | Buffer;
}

type SupportedHTTPMethods = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";

interface PluginsOptions {
Expand Down
7 changes: 7 additions & 0 deletions types/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,10 @@ app.register(fastifySwaggerUi, {
return swaggerObj
}
})

app.register(fastifySwaggerUi, {
logo: {
type: 'image/png',
content: 'somethingsomething'
}
})

0 comments on commit a297105

Please sign in to comment.