Skip to content

Commit

Permalink
feat: add a trailing slash redirect in docs path
Browse files Browse the repository at this point in the history
  • Loading branch information
mo4islona committed Aug 12, 2021
1 parent e57d37b commit ba4d11c
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 155 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@nestjs/testing": "^8.0.0",
"@types/express": "^4.17.8",
"@types/express-handlebars": "^3.1.0",
"@types/hapi__joi": "^17.1.6",
"@types/joi": "^17.2.3",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@types/supertest": "^2.0.10",
Expand Down
2 changes: 1 addition & 1 deletion src/model/options.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Joi = require('@hapi/joi');
import Joi = require('joi');
import { OpenAPIObject } from '@nestjs/swagger';

export const schema = (document: OpenAPIObject) =>
Expand Down
9 changes: 8 additions & 1 deletion src/redoc-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import handlebars from 'express-handlebars';
import pathModule from 'path';
import { resolve } from 'url';
import { LogoOptions, RedocDocument, RedocOptions } from './interfaces';
import { schema } from './model/options.model';
import { schema } from './model';

export class RedocModule {
/**
Expand Down Expand Up @@ -121,10 +121,16 @@ export class RedocModule {
'views',
'redoc.handlebars'
);

// get handlebars rendered HTML
const redocHTML = await hbs.render(redocFilePath, renderData);
// Serve ReDoc Frontend
httpAdapter.get(finalPath, async (req: Request, res: Response) => {
if (!req.url.endsWith('/')) {
res.redirect(301, req.url + '/');
return;
}

const sendPage = () => {
// Content-Security-Policy: worker-src 'self' blob:
res.setHeader(
Expand All @@ -147,6 +153,7 @@ export class RedocModule {
sendPage();
}
});

// Serve swagger spec json
httpAdapter.get(docUrl, (req: Request, res: Response) => {
res.setHeader('Content-Type', 'application/json');
Expand Down
Loading

0 comments on commit ba4d11c

Please sign in to comment.