add swagger
This commit is contained in:
@@ -11,23 +11,33 @@ import {
|
||||
SerializeOptions,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { Depends } from '@/modules/restful/decorators/depend.decorator';
|
||||
|
||||
import { ContentModule } from '../content.module';
|
||||
import { CreateCategoryDto, QueryCategoryDto, UpdateCategoryDto } from '../dtos/category.dto';
|
||||
import { CategoryService } from '../services';
|
||||
|
||||
@ApiTags('Category Operate')
|
||||
@Depends(ContentModule)
|
||||
@Controller('category')
|
||||
export class CategoryController {
|
||||
constructor(protected service: CategoryService) {}
|
||||
|
||||
/**
|
||||
* Search category tree
|
||||
*/
|
||||
@Get('tree')
|
||||
@SerializeOptions({ groups: ['category-tree'] })
|
||||
async tree() {
|
||||
return this.service.findTrees();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询分类列表
|
||||
* @param options
|
||||
*/
|
||||
@Get()
|
||||
@SerializeOptions({ groups: ['category-list'] })
|
||||
async list(
|
||||
@@ -37,6 +47,10 @@ export class CategoryController {
|
||||
return this.service.paginate(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询分类明细
|
||||
* @param id
|
||||
*/
|
||||
@Get(':id')
|
||||
@SerializeOptions({ groups: ['category-detail'] })
|
||||
async detail(@Param('id', new ParseUUIDPipe()) id: string) {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { INestApplication, Injectable, Type } from '@nestjs/common';
|
||||
import { RouterModule } from '@nestjs/core';
|
||||
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { omit, trim } from 'lodash';
|
||||
import { isNil, omit, trim } from 'lodash';
|
||||
|
||||
import { BaseRestful } from './base';
|
||||
import {
|
||||
@@ -119,7 +119,10 @@ export class Restful extends BaseRestful {
|
||||
this._docs.default = this.getDocOption(this._default, defaultVersion, true);
|
||||
}
|
||||
|
||||
async factoryDocs<T extends INestApplication>(container: T) {
|
||||
async factoryDocs<T extends INestApplication>(
|
||||
container: T,
|
||||
metadata?: () => Promise<RecordAny>,
|
||||
) {
|
||||
const docs = Object.values(this._docs)
|
||||
.map((doc) => [doc.default, ...Object.values(doc.routes ?? [])])
|
||||
.reduce((o, n) => [...o, ...n], [])
|
||||
@@ -146,6 +149,10 @@ export class Restful extends BaseRestful {
|
||||
}
|
||||
builder.setVersion(version);
|
||||
|
||||
if (!isNil(metadata)) {
|
||||
await SwaggerModule.loadPluginMetadata(metadata);
|
||||
}
|
||||
|
||||
const document = SwaggerModule.createDocument(container, builder.build(), {
|
||||
include: include.length > 0 ? include : [() => undefined as any],
|
||||
ignoreGlobalPrefix: true,
|
||||
|
||||
+17
-2
@@ -2,6 +2,9 @@ import { NestFactory } from '@nestjs/core';
|
||||
|
||||
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
|
||||
import { existsSync } from 'fs-extra';
|
||||
import { isNil } from 'lodash';
|
||||
|
||||
import * as configs from './config';
|
||||
import { ContentModule } from './modules/content/content.module';
|
||||
import { CreateOptions } from './modules/core/types';
|
||||
@@ -9,6 +12,8 @@ import { DatabaseModule } from './modules/database/database.module';
|
||||
import { MeiliModule } from './modules/meilisearch/meili.module';
|
||||
import { Restful } from './modules/restful/restful';
|
||||
import { RestfulModule } from './modules/restful/restful.module';
|
||||
import { ApiConfig } from './modules/restful/types';
|
||||
import { join } from 'path';
|
||||
|
||||
export const createOptions: CreateOptions = {
|
||||
config: { factories: configs as any, storage: { enable: true } },
|
||||
@@ -28,8 +33,18 @@ export const createOptions: CreateOptions = {
|
||||
logger: ['error', 'warn'],
|
||||
},
|
||||
);
|
||||
const restful = container.get(Restful);
|
||||
await restful.factoryDocs(container);
|
||||
if (!isNil(await configure.get<ApiConfig>('api', null))) {
|
||||
const restful = container.get(Restful);
|
||||
let metadata: () => Promise<RecordAny>;
|
||||
if (existsSync(join(__dirname, 'metadata.js'))) {
|
||||
metadata = (await import(join(__dirname, 'metadata.js'))).default;
|
||||
}
|
||||
if (existsSync(join(__dirname, 'metadata.ts'))) {
|
||||
metadata = (await import(join(__dirname, 'metadata.ts'))).default;
|
||||
}
|
||||
await restful.factoryDocs(container, metadata);
|
||||
}
|
||||
|
||||
return container;
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user