Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load swagger file from relative path #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-redoc",
"version": "2.2.2",
"name": "@mo4ilsona/nestjs-redoc",
"version": "2.3.1",
"description": "NestJS ReDoc frontend",
"repository": "https://github.com/mxarc/nestjs-redoc",
"author": "Alfonso Reyes",
Expand All @@ -22,28 +22,28 @@
"sample": "ts-node ./sample/main"
},
"peerDependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/swagger": "^5.0.0",
"reflect-metadata": "^0.1.12"
"@nestjs/common": "^9.4.3",
"@nestjs/core": "^9.4.3",
"@nestjs/swagger": "^6.3.0",
"reflect-metadata": "^0.1.13"
},
"dependencies": {
"@hapi/joi": "^17.1.1",
"@nestjs/common": "^8.0.0",
"@nestjs/swagger": "^5.0.0",
"express-basic-auth": "^1.2.0",
"express-handlebars": "^5.1.0"
"joi": "^17.11.0",
"@nestjs/common": "^9.4.3",
"@nestjs/swagger": "^6.3.0",
"express-basic-auth": "^1.2.1",
"express-handlebars": "^6.0.7"
},
"devDependencies": {
"@commitlint/cli": "^9.1.2",
"@commitlint/config-conventional": "^9.1.2",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/platform-fastify": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/express": "^4.17.8",
"@nestjs/core": "^9.4.3",
"@nestjs/platform-express": "^9.4.3",
"@nestjs/platform-fastify": "^9.4.3",
"@nestjs/testing": "^9.4.3",
"@types/express": "^4.17.20",
"@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 All @@ -58,11 +58,11 @@
"prettier": "^2.1.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"supertest": "^6.0.1",
"ts-jest": "^26.4.3",
"ts-node": "^9.0.0",
"typescript": "^4.3.5"
"rxjs": "^7.8.1",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"config": {
"commitizen": {
Expand Down
1 change: 1 addition & 0 deletions sample/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function bootstrap() {
hideHostname: false,
noAutoAuth: true,
pathInMiddlePanel: true,
expandResponses: '200',
auth: {
enabled: true,
user: 'admin',
Expand Down
4 changes: 2 additions & 2 deletions src/model/options.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Joi = require('@hapi/joi');
import Joi from 'joi';
import { OpenAPIObject } from '@nestjs/swagger';

export const schema = (document: OpenAPIObject) =>
export const schema = (document: OpenAPIObject): Joi.Schema =>
Joi.object().keys({
redocVersion: Joi.string().default('latest'),
title: Joi.string()
Expand Down
13 changes: 10 additions & 3 deletions src/redoc-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { NestExpressApplication } from '@nestjs/platform-express';
import { OpenAPIObject } from '@nestjs/swagger';
import { Request, Response } from 'express';
import expressAuth from 'express-basic-auth';
import handlebars from 'express-handlebars';
import { create } 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 @@ -90,7 +90,7 @@ export class RedocModule {
// Serve swagger spec in another URL appended to the normalized path
const docUrl = resolve(resolvedPath, `${options.docName}.json`);
// create helper to convert metadata to JSON
const hbs = handlebars.create({
const hbs = create({
helpers: {
toJSON: function (object: any) {
return JSON.stringify(object);
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
2 changes: 1 addition & 1 deletion views/redoc.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
let themeJSON = '{{{ toJSON data.theme }}}';
if (themeJSON === '') { themeJSON = undefined }
Redoc.init(
'{{ data.docUrl }}',
'./swagger.json',
{
...(themeJSON && {
theme: {
Expand Down
Loading