Skip to content

Commit

Permalink
Merge pull request ojoanalogo#23 from UlrichFrancomme/feat/tag-groups
Browse files Browse the repository at this point in the history
The PR includes :

- option to use the x-tagGroups extension,
- a fix on the lint script
- a fix in the redoc-module unit test file
  • Loading branch information
Alfonso Reyes authored Nov 12, 2020
2 parents a66c189 + 51b8264 commit 44b77b2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "tsc -p ./tsconfig.build.json",
"format": "prettier --write \"./src/**/*.ts\"",
"format:check": "prettier --check \"./src/**/*.ts\"",
"lint": "eslint 'src/**/*.ts' --fix",
"lint": "eslint \"./src/**/*.ts\" --fix",
"prepublish:next": "npm run build",
"publish:next": "npm publish --access public --tag next",
"prepublish": "npm run build",
Expand Down
6 changes: 6 additions & 0 deletions sample/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ async function bootstrap() {
user: 'admin',
password: '123',
},
tagGroups: [
{
name: 'Core resources',
tags: ['cats'],
},
],
};
await RedocModule.setup('docs', app, document, redocOptions);
await app.listen(8000);
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/redocDocument.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { OpenAPIObject } from '@nestjs/swagger';
import { LogoOptions } from './redocOptions.interface';
import { LogoOptions, TagGroupOptions } from './redocOptions.interface';

export interface RedocDocument extends Partial<OpenAPIObject> {
info: OpenAPIObject['info'] & {
'x-logo'?: LogoOptions;
};
'x-tagGroups': TagGroupOptions[];
}
10 changes: 10 additions & 0 deletions src/interfaces/redocOptions.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface RedocOptions {
// If auth is enabled but no password is provided the default value is "123"
password: string;
};

/** Vendor extensions */

/** If set, group tags in categories in the side menu. Tags not added to a group will not be displayed. */
tagGroups?: TagGroupOptions[];
}

export interface LogoOptions {
Expand All @@ -61,3 +66,8 @@ export interface LogoOptions {
/** href tag for logo, it defaults to the one used in your API spec */
href?: string;
}

export interface TagGroupOptions {
name: string;
tags: string[];
}
8 changes: 8 additions & 0 deletions src/model/options.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ export const schema = (document: OpenAPIObject) =>
user: Joi.string().default('admin'),
password: Joi.string().default('123'),
},
tagGroups: Joi.array()
.items(
Joi.object({
name: Joi.string(),
tags: Joi.array().items(Joi.string()),
})
)
.optional(),
});
14 changes: 4 additions & 10 deletions src/redoc-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FastifyAdapter } from '@nestjs/platform-fastify';
import { DocumentBuilder, OpenAPIObject, SwaggerModule } from '@nestjs/swagger';
import { Test } from '@nestjs/testing';
import 'reflect-metadata';
import * as request from 'supertest';
import request from 'supertest';
import { RedocModule } from './redoc-module';

describe('redoc-module', () => {
Expand Down Expand Up @@ -99,15 +99,9 @@ describe('redoc-module', () => {

it('should throw an error for now', async () => {
try {
await RedocModule.setup(
'some/path',
app,
swagger,
{
logo: { url: 'notaUrl' },
},
{ enable: false }
);
await RedocModule.setup('some/path', app, swagger, {
logo: { url: 'notaUrl' },
});
} catch (error) {
// console.log(error);
// expect(typeof error).toBe(TypeError);
Expand Down
6 changes: 6 additions & 0 deletions src/redoc-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import pathModule from 'path';
import { resolve } from 'url';
import { LogoOptions, RedocDocument, RedocOptions } from './interfaces';
import { schema } from './model/options.model';

export class RedocModule {
/**
* Setup ReDoc frontend
Expand Down Expand Up @@ -173,6 +174,11 @@ export class RedocModule {
const logoOption: Partial<LogoOptions> = { ...options.logo };
document.info['x-logo'] = logoOption;
}

if (options.tagGroups) {
document['x-tagGroups'] = options.tagGroups;
}

return document;
}
}

0 comments on commit 44b77b2

Please sign in to comment.