Skip to content

Commit

Permalink
Merge pull request #858 from samchon/features/tags
Browse files Browse the repository at this point in the history
Support swagger tag description
  • Loading branch information
samchon authored Apr 2, 2024
2 parents 3007667 + 76525dc commit 08a14fc
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@nestia/station",
"version": "2.6.3",
"version": "2.6.4",
"description": "Nestia station",
"main": "prettier.config.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "2.6.3",
"version": "2.6.4",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "^2.6.3",
"@nestia/fetcher": "^2.6.4",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"detect-ts-node": "^1.0.5",
Expand All @@ -48,7 +48,7 @@
"typia": "^5.5.7"
},
"peerDependencies": {
"@nestia/fetcher": ">=2.6.3",
"@nestia/fetcher": ">=2.6.4",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/fetcher",
"version": "2.6.3",
"version": "2.6.4",
"description": "Fetcher library of Nestia SDK",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "2.6.3",
"version": "2.6.4",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "^2.6.3",
"@nestia/fetcher": "^2.6.4",
"cli": "^1.0.1",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -45,7 +45,7 @@
"typia": "^5.5.7"
},
"peerDependencies": {
"@nestia/fetcher": ">=2.6.3",
"@nestia/fetcher": ">=2.6.4",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
17 changes: 15 additions & 2 deletions packages/sdk/src/INestiaConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { INestApplication } from "@nestjs/common";

import type { INormalizedInput } from "./structures/INormalizedInput";
import type { ISwagger } from "./structures/ISwagger";
import type { ISwaggerInfo } from "./structures/ISwaggerInfo";
import type { ISwaggerSecurityScheme } from "./structures/ISwaggerSecurityScheme";
import type { ISwaggerServer } from "./structures/ISwaggerServer";
import type { ISwaggerTag } from "./structures/ISwaggerTag";

/**
* Definition for the `nestia.config.ts` file.
Expand Down Expand Up @@ -217,7 +218,7 @@ export namespace INestiaConfig {
/**
* List of server addresses.
*/
servers?: ISwagger.IServer[];
servers?: ISwaggerServer[];

/**
* Security schemes.
Expand All @@ -228,6 +229,18 @@ export namespace INestiaConfig {
*/
security?: Record<string, ISwaggerSecurityScheme>;

/**
* List of tag names with description.
*
* It is possible to omit this property or skip some tag name even if
* the tag name is used in the API routes. In that case, the tag name
* would be used without description.
*
* Of course, if you've written a comment like `@tag {name} {descrition}`,
* you can entirely replace this property specification.
*/
tags?: ISwaggerTag[];

/**
* Decompose query DTO.
*
Expand Down
27 changes: 24 additions & 3 deletions packages/sdk/src/generates/SwaggerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export namespace SwaggerGenerator {
lazySchemas: Array<ISwaggerLazySchema>;
lazyProperties: Array<ISwaggerLazyProperty>;
errors: ISwaggerError[];
swagger: ISwagger;
}

export const generate =
Expand Down Expand Up @@ -80,6 +81,7 @@ export namespace SwaggerGenerator {
lazySchemas,
lazyProperties,
errors,
swagger,
})(route);
}
swagger.paths = {};
Expand Down Expand Up @@ -329,6 +331,7 @@ export namespace SwaggerGenerator {
},
paths: {},
components: {},
tags: config.tags ?? [],
};
};

Expand All @@ -344,8 +347,11 @@ export namespace SwaggerGenerator {
const generate_route =
(props: IProps) =>
(route: IRoute): ISwaggerRoute => {
// FIND REQUEST BODY
const body = route.parameters.find((param) => param.category === "body");
const getJsDocTexts = (name: string) =>

// CONSTRUCT SUMMARY & DESCRIPTION
const getJsDocTexts = (name: string): string[] =>
route.jsDocTags
.filter(
(tag) =>
Expand All @@ -356,7 +362,6 @@ export namespace SwaggerGenerator {
) !== undefined,
)
.map((tag) => tag.text!.find((elem) => elem.kind === "text")!.text);

const description: string | undefined = route.description?.length
? route.description
: undefined;
Expand All @@ -376,9 +381,25 @@ export namespace SwaggerGenerator {
(tag) => tag.name === "deprecated",
);

// CONSTRUCT TAGS
const tagSet: Set<string> = new Set([
...route.swaggerTags,
...getJsDocTexts("tag").map((tag) => tag.split(" ")[0]),
]);
for (const tag of tagSet)
if (props.swagger.tags.find((elem) => elem.name === tag) === undefined)
props.swagger.tags.push({ name: tag });
for (const texts of getJsDocTexts("tag")) {
const [name, ...description] = texts.split(" ");
if (description.length)
props.swagger.tags.find((elem) => elem.name === name)!.description ??=
description.join(" ");
}

// FINALIZE
return {
deprecated: deprecated ? true : undefined,
tags: [...route.swaggerTags, ...new Set([...getJsDocTexts("tag")])],
tags: [...tagSet],
operationId:
route.operationId ??
props.config.operationId?.({
Expand Down
41 changes: 8 additions & 33 deletions packages/sdk/src/structures/ISwagger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ISwaggerComponents } from "./ISwaggerComponents";
import { ISwaggerInfo } from "./ISwaggerInfo";
import { ISwaggerRoute } from "./ISwaggerRoute";
import { ISwaggerServer } from "./ISwaggerServer";
import { ISwaggerTag } from "./ISwaggerTag";

/**
* Swagger Document.
Expand All @@ -22,7 +24,7 @@ export interface ISwagger {
/**
* List of servers that provide the API.
*/
servers: ISwagger.IServer[];
servers: ISwaggerServer[];

/**
* Information about the API.
Expand All @@ -46,6 +48,11 @@ export interface ISwagger {
*/
components: ISwaggerComponents;

/**
* List of tags.
*/
tags: ISwaggerTag[];

// /**
// * A declaration of which security mechanisms can be used across the API.
// *
Expand All @@ -57,35 +64,3 @@ export interface ISwagger {
// */
// security?: Record<string, string[]>[];
}
export namespace ISwagger {
/**
* Remote server definition.
*/
export interface IServer {
/**
* A URL to the target host.
*
* @format uri
*/
url: string;

/**
* An optional string describing the target server.
*/
description?: string;
}

export interface IExternalDocs {
/**
* The URL for target documentation.
*
* @format uri
*/
url: string;

/**
* A short description of the target documentation.
*/
description?: string;
}
}
16 changes: 16 additions & 0 deletions packages/sdk/src/structures/ISwaggerServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Remote server definition.
*/
export interface ISwaggerServer {
/**
* A URL to the target host.
*
* @format uri
*/
url: string;

/**
* An optional string describing the target server.
*/
description?: string;
}
9 changes: 9 additions & 0 deletions packages/sdk/src/structures/ISwaggerTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Tag name and description.
*
* @author Jeongho Nam - https://github.com/samchon
*/
export interface ISwaggerTag {
name: string;
description?: string;
}
1 change: 1 addition & 0 deletions test/features/tags/nestia.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const NESTIA_CONFIG: INestiaConfig = {
e2e: "src/test",
swagger: {
output: "swagger.json",
beautify: true,
},
};
export default NESTIA_CONFIG;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { IBbsArticle } from "../../../structures/IBbsArticle";
/**
* Would be shown without any mark.
*
* @tag public
* @tag public Some description describing public group...
* @summary Public API
* @param section Section code
* @param input Content to store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class BbsArticlesController {
/**
* Would be shown without any mark.
*
* @tag public
* @tag public Some description describing public group...
* @summary Public API
* @param section Section code
* @param input Content to store
Expand Down
Loading

0 comments on commit 08a14fc

Please sign in to comment.