Skip to content

Commit

Permalink
add set icon option (#43)
Browse files Browse the repository at this point in the history
feat: add set icon option
  • Loading branch information
yujunlong2000 authored and scttcper committed May 4, 2018
1 parent d740ef1 commit 5a6eecf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ swaggerOptions: { // passed to SwaggerUi()
defaultModelRendering: 'schema',
showRequestHeaders: false,
swaggerVersion: 'x.x.x' // read from package.json,

},
routePrefix: '/docs', // route where the view is returned
hideTopbar: false, // hide swagger top bar
favicon16: '/favicon-16x16.png', // default icon 16x16, set for self icon
favicon32: '/favicon-32x32.png', // default icon 32x32, set for self icon
```

## example
Expand Down
Binary file added lib/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 lib/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.
4 changes: 2 additions & 2 deletions lib/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<title>{{title}}</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/{{swaggerVersion}}/swagger-ui.css">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="{{favicon32}}" sizes="32x32" />
<link rel="icon" type="image/png" href="{{favicon16}}" sizes="16x16" />
<style>
html
{
Expand Down
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ const defaultOptions = {
routePrefix: '/docs',
swaggerVersion: json.devDependencies['swagger-ui-dist'],
hideTopbar: false,
favicon16: '/favicon-16x16.png',
favicon32: '/favicon-32x32.png',
};

module.exports = function koaSwagger(config) {
const extFavicon16 = config && config.favicon16;
const extFavicon32 = config && config.favicon32;
const favicon16Path = path.join(__dirname, defaultOptions.favicon16);
const favicon32Path = path.join(__dirname, defaultOptions.favicon32);
const options = defaultsDeep(config || {}, defaultOptions);
Handlebars.registerHelper('json', context => JSON.stringify(context));
Handlebars.registerHelper('strfnc', fnc => fnc);
Expand All @@ -31,6 +37,16 @@ module.exports = function koaSwagger(config) {
ctx.body = index(options);
return true;
}
if (!extFavicon16 && ctx.path === defaultOptions.favicon16) {
ctx.type = 'image/png';
ctx.body = fs.createReadStream(favicon16Path);
return true;
}
if (!extFavicon32 && ctx.path === defaultOptions.favicon32) {
ctx.type = 'image/png';
ctx.body = fs.createReadStream(favicon32Path);
return true;
}
return next();
};
};
12 changes: 12 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ describe('koa2-swagger-ui', function() {
.expect('Content-Type', 'text/css')
.expect(200);
});
it('should return icon16x16', function() {
return request(app.callback())
.get('/favicon-16x16.png')
.expect('Content-Type', /png/)
.expect(200);
});
it('should return icon32x32', function() {
return request(app.callback())
.get('/favicon-32x32.png')
.expect('Content-Type', /png/)
.expect(200);
});
});

0 comments on commit 5a6eecf

Please sign in to comment.