Skip to content

Commit a297105

Browse files
authored
Use Fastify logos by default (#49)
* 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]>
1 parent bf84b4d commit a297105

File tree

11 files changed

+124
-6
lines changed

11 files changed

+124
-6
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ A Fastify plugin for serving [Swagger UI](https://swagger.io/tools/swagger-ui/).
88

99
Supports Fastify versions `4.x`.
1010

11+
![Demo](./demo.png)
12+
1113
<a name="install"></a>
1214
## Install
1315
```
@@ -199,6 +201,7 @@ await fastify.register(require('@fastify/swagger-ui'), {
199201
You can add custom JavaScript and CSS to the Swagger UI web page by using the theme option.
200202

201203
##### Example
204+
202205
```js
203206
const fastify = require('fastify')()
204207

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

231+
You can add custom JavaScript and CSS to the Swagger UI web page by using the theme option.
232+
233+
#### logo
234+
235+
It's possible to override the logo displayed in the top bar by specifying:
236+
237+
```
238+
await fastify.register(require('@fastify/swagger-ui'), {
239+
logo: {
240+
type: 'image/png',
241+
content: Buffer.from('iVBOR...', 'base64')
242+
},
243+
theme: {
244+
favicon: [
245+
{
246+
filename: 'favicon.png',
247+
rel: 'icon',
248+
sizes: '16x16',
249+
type: 'image/png',
250+
content: Buffer.from('iVBOR...', 'base64')
251+
}
252+
]
253+
}
254+
})
255+
```
256+
228257
#### Protect your documentation routes
229258

230259
You can protect your documentation by configuring an authentication hook.

demo.png

102 KB
Loading

favicon-16x16.png

284 Bytes
Loading

favicon-32x32.png

600 Bytes
Loading

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

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

5-
function fastifySwaggerUi (fastify, opts, next) {
6+
async function fastifySwaggerUi (fastify, opts) {
67
fastify.decorate('swaggerCSP', require('./static/csp.json'))
78

89
fastify.register(require('./lib/routes'), {
@@ -11,10 +12,9 @@ function fastifySwaggerUi (fastify, opts, next) {
1112
initOAuth: opts.initOAuth || {},
1213
hooks: opts.uiHooks,
1314
theme: opts.theme || {},
15+
logo: opts.logo || { type: 'image/svg+xml', content: await readFile(require.resolve('./static/logo.svg')) },
1416
...opts
1517
})
16-
17-
next()
1818
}
1919

2020
module.exports = fp(fastifySwaggerUi, {

lib/swagger-initializer.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const serialize = require('./serialize')
44

55
function swaggerInitializer (opts) {
6+
const logoBase64 = Buffer.from(opts.logo.content).toString('base64')
7+
const logoData = `data:${opts.logo.type};base64,${logoBase64}`
8+
69
return `window.onload = function () {
710
function resolveUrl(url) {
811
const anchor = document.createElement('a')
@@ -28,7 +31,13 @@ function swaggerInitializer (opts) {
2831
});
2932
3033
const ui = SwaggerUIBundle(resConfig)
31-
window.ui = ui
34+
const logoData = '${logoData}'
35+
36+
if (logoData) {
37+
const img = document.querySelector('#swagger-ui > section > div.topbar > div > div > a > img')
38+
img.src = logoData
39+
}
40+
3241
ui.initOAuth(${serialize(opts.initOAuth)})
3342
}`
3443
}

logo.svg

Lines changed: 43 additions & 0 deletions
Loading

scripts/prepare-swagger-ui.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ fse.emptyDirSync(resolve(`./${folderName}`))
1010

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

29+
const overrides = [
30+
'favicon-16x16.png',
31+
'favicon-32x32.png',
32+
'logo.svg'
33+
]
34+
overrides.forEach(filename => {
35+
fse.copySync(`./${filename}`, resolve(`./static/${filename}`))
36+
})
37+
3138
const sha = {
3239
script: [],
3340
style: []

test/swagger-initializer.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,19 @@ test('/documentation/static/swagger-initializer.js should have configurable init
7676
t.equal(res.statusCode, 200)
7777
t.ok(res.payload.includes('ui.initOAuth({"clientId":"someId"})'))
7878
})
79+
80+
test('customize logo', async (t) => {
81+
const config = {
82+
mode: 'static',
83+
specification: {
84+
path: './examples/example-static-specification.yaml'
85+
}
86+
}
87+
88+
const fastify = Fastify()
89+
await fastify.register(fastifySwagger, config)
90+
await fastify.register(fastifySwaggerUi, { logo: { type: 'image/png', content: 'foobar' } })
91+
92+
const res = await fastify.inject('/documentation/static/swagger-initializer.js')
93+
t.equal(res.body.indexOf(Buffer.from('foobar').toString('base64')) > -1, true)
94+
})

types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ declare namespace fastifySwaggerUi {
5353

5454
theme?: FastifySwaggerUiTheme
5555

56+
logo?: FastifySwaggerUILogo
57+
5658
transformSpecification?: (swaggerObject: Readonly<Record<string, any>>, request: FastifyRequest, reply: FastifyReply) => Record<string, any>
5759
transformSpecificationClone?: boolean
5860
}
@@ -63,6 +65,11 @@ declare namespace fastifySwaggerUi {
6365
favicon: { filename: string; rel: string; type: string; sizes: string; content: string; }[];
6466
}
6567

68+
type FastifySwaggerUILogo = {
69+
type: string;
70+
content: string | Buffer;
71+
}
72+
6673
type SupportedHTTPMethods = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";
6774

6875
interface PluginsOptions {

0 commit comments

Comments
 (0)