Skip to content

Commit

Permalink
Merge pull request #15 from joemaidman/feat/favicon-and-docUrl
Browse files Browse the repository at this point in the history
Feat/favicon and doc url
  • Loading branch information
Alfonso Reyes authored Aug 27, 2020
2 parents 50411e3 + 2b6569b commit 901c52c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ await RedocModule.setup('/docs', app, document, redocOptions);

| Option | Description | Type | Note |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------------------ |
| title | Web site title (e.g: ReDoc documentation) | string | Fallbacks to the document title if not set |
| title | Web site title (e.g: ReDoc documentation) | string |
| favicon | Web site favicon URL | string | Fallbacks to the document title if not set |
| logo | Logo Options | LogoOptions | See LogoOptions table |
| theme | Theme options | ThemeOptions | See ThemeOptions info |
| untrustedSpec | If set, the spec is considered untrusted and all HTML/markdown is sanitized to prevent XSS, by default is false | boolean |
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/redoc_options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export interface RedocOptions {
/** Web site title (e.g: ReDoc documentation) */
title?: string;
/** Web site favicon URL */
favicon?: string;
/** Logo Options */
logo?: LogoOptions;
/** Theme options */
Expand Down Expand Up @@ -36,6 +38,8 @@ export interface RedocOptions {
disableSearch?: boolean;
/** Shows only required fileds in request samples */
onlyRequiredInSamples?: boolean;
/** Name of the swagger json spec file */
docName?: string;
}

export interface RedocTheme {
Expand Down
6 changes: 5 additions & 1 deletion src/model/options.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const schema = (document: OpenAPIObject) =>
title: Joi.string()
.optional()
.default(document.info ? document.info.title : 'Swagger documentation'),
favicon: Joi.string().optional(),
logo: {
url: Joi.string()
.optional()
Expand Down Expand Up @@ -58,5 +59,8 @@ export const schema = (document: OpenAPIObject) =>
.default(false),
onlyRequiredInSamples: Joi.boolean()
.optional()
.default(false)
.default(false),
docName: Joi.string()
.optional()
.default('swagger')
});
11 changes: 6 additions & 5 deletions src/redoc-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class RedocModule {
const resolvedPath =
finalPath.slice(-1) !== '/' ? finalPath + '/' : finalPath;
// Serve swagger spec in another URL appended to the normalized path
const swaggerDocUrl = resolve(resolvedPath, 'swagger.json');
const docUrl = resolve(resolvedPath, `${options.docName}.json`);
const hbs = handlebars.create({
helpers: {
toJSON: function(object: any) {
Expand All @@ -101,11 +101,12 @@ export class RedocModule {
app.set('views', join(__dirname, '..', 'views'));
// Serve ReDoc Frontend
app.getHttpAdapter().get(finalPath, (req: Request, res: Response) => {
const { title, theme, logo, ...otherOptions } = options;
const { title, favicon, theme, logo, ...otherOptions } = options;
const renderData = {
data: {
title: title,
docUrl: swaggerDocUrl,
title,
docUrl,
favicon,
options: otherOptions,
...(theme && {
theme: {
Expand All @@ -122,7 +123,7 @@ export class RedocModule {
});
});
// Serve swagger spec json
app.getHttpAdapter().get(swaggerDocUrl, (req: Request, res: Response) => {
app.getHttpAdapter().get(docUrl, (req: Request, res: Response) => {
res.setHeader('Content-Type', 'application/json');
res.send(document);
});
Expand Down
6 changes: 4 additions & 2 deletions views/redoc.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<title>{{ data.title }}</title>
<!-- needed for adaptive design -->
<meta charset="utf-8" />
<link rel="shortcut icon" type="image/x-icon" href="https://quizizz.com/favicon.ico" />
{{#if data.favicon}}
<link rel="shortcut icon" type="image/x-icon" href="{{ data.favicon }}"/>
{{/if}}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<!--
Expand Down Expand Up @@ -41,4 +43,4 @@
</script>
</body>

</html>
</html>

0 comments on commit 901c52c

Please sign in to comment.