add swagger
This commit is contained in:
parent
35cc963ca8
commit
5a5306b10d
@ -24,7 +24,7 @@
|
||||
"@fastify/static": "^8.2.0",
|
||||
"@nestjs/common": "^10.0.3",
|
||||
"@nestjs/core": "^10.0.3",
|
||||
"@nestjs/platform-fastify": "^10.0.3",
|
||||
"@nestjs/platform-fastify": "^11.1.3",
|
||||
"@nestjs/swagger": "^7.4.2",
|
||||
"@nestjs/typeorm": "^11.0.0",
|
||||
"chalk": "^5.4.1",
|
||||
|
529
pnpm-lock.yaml
529
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
43
src/config/api.config.ts
Normal file
43
src/config/api.config.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Configure } from '@/modules/config/configure';
|
||||
import { ConfigureFactory } from '@/modules/config/types';
|
||||
import * as contentControllers from '@/modules/content/controllers';
|
||||
import { ApiConfig, VersionOption } from '@/modules/restful/types';
|
||||
|
||||
export const v1 = async (configure: Configure): Promise<VersionOption> => {
|
||||
return {
|
||||
routes: [
|
||||
{
|
||||
name: 'app',
|
||||
path: '/',
|
||||
controllers: [],
|
||||
doc: {
|
||||
description: 'app name desc',
|
||||
tags: [
|
||||
{ name: '分类操作', description: '对分类进行CRUD操作' },
|
||||
{ name: '标签操作', description: '对标签进行CRUD操作' },
|
||||
{ name: '文章操作', description: '对文章进行CRUD操作' },
|
||||
{ name: '评论操作', description: '对评论进行CRUD操作' },
|
||||
],
|
||||
},
|
||||
children: [
|
||||
{
|
||||
name: 'app.content',
|
||||
path: 'content',
|
||||
controllers: Object.values(contentControllers),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
export const api: ConfigureFactory<ApiConfig> = {
|
||||
register: async (configure: Configure) => ({
|
||||
title: configure.env.get('API_TITLE', `${await configure.get<string>('app.name')} API`),
|
||||
auth: true,
|
||||
docuri: 'api/docs',
|
||||
default: configure.env.get('API_DEFAULT_VERSION', 'v1'),
|
||||
enabled: [],
|
||||
versions: { v1: await v1(configure) },
|
||||
}),
|
||||
};
|
@ -2,3 +2,4 @@ export * from './database.config';
|
||||
export * from './content.config';
|
||||
export * from './app.config';
|
||||
export * from './meili.config';
|
||||
export * from './api.config';
|
||||
|
@ -2,7 +2,6 @@ import { DynamicModule, Module, ModuleMetadata } from '@nestjs/common';
|
||||
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import * as controllers from '@/modules/content/controllers';
|
||||
import * as entities from '@/modules/content/entities';
|
||||
import * as repositories from '@/modules/content/repositories';
|
||||
import * as services from '@/modules/content/services';
|
||||
@ -71,7 +70,6 @@ export class ContentModule {
|
||||
TypeOrmModule.forFeature(Object.values(entities)),
|
||||
DatabaseModule.forRepository(Object.values(repositories)),
|
||||
],
|
||||
controllers: Object.values(controllers),
|
||||
providers,
|
||||
exports,
|
||||
};
|
||||
|
@ -11,9 +11,13 @@ import {
|
||||
SerializeOptions,
|
||||
} from '@nestjs/common';
|
||||
|
||||
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';
|
||||
|
||||
@Depends(ContentModule)
|
||||
@Controller('category')
|
||||
export class CategoryController {
|
||||
constructor(protected service: CategoryService) {}
|
||||
|
@ -1,5 +1,8 @@
|
||||
import { Body, Controller, Delete, Get, Post, Query, SerializeOptions } from '@nestjs/common';
|
||||
|
||||
import { Depends } from '@/modules/restful/decorators/depend.decorator';
|
||||
|
||||
import { ContentModule } from '../content.module';
|
||||
import {
|
||||
CreateCommentDto,
|
||||
DeleteCommentDto,
|
||||
@ -8,6 +11,7 @@ import {
|
||||
} from '../dtos/comment.dto';
|
||||
import { CommentService } from '../services';
|
||||
|
||||
@Depends(ContentModule)
|
||||
@Controller('comment')
|
||||
export class CommentController {
|
||||
constructor(protected service: CommentService) {}
|
||||
|
@ -14,8 +14,12 @@ import {
|
||||
import { CreatePostDto, QueryPostDto, UpdatePostDto } from '@/modules/content/dtos/post.dto';
|
||||
import { PostService } from '@/modules/content/services/post.service';
|
||||
|
||||
import { Depends } from '@/modules/restful/decorators/depend.decorator';
|
||||
|
||||
import { ContentModule } from '../content.module';
|
||||
import { DeleteWithTrashDto, RestoreDto } from '../dtos/delete.with.trash.dto';
|
||||
|
||||
@Depends(ContentModule)
|
||||
@Controller('posts')
|
||||
export class PostController {
|
||||
constructor(private postService: PostService) {}
|
||||
|
@ -13,9 +13,13 @@ import {
|
||||
|
||||
import { DeleteDto } from '@/modules/content/dtos/delete.dto';
|
||||
|
||||
import { Depends } from '@/modules/restful/decorators/depend.decorator';
|
||||
|
||||
import { ContentModule } from '../content.module';
|
||||
import { CreateTagDto, QueryTagDto, UpdateTagDto } from '../dtos/tag.dto';
|
||||
import { TagService } from '../services';
|
||||
|
||||
@Depends(ContentModule)
|
||||
@Controller('tag')
|
||||
export class TagController {
|
||||
constructor(protected service: TagService) {}
|
||||
|
@ -23,7 +23,7 @@ export const createAppConfig: (
|
||||
defaultRegister: (configure) => getDefaultAppConfig(configure),
|
||||
hook: (configure: Configure, value) => {
|
||||
if (isNil(value.url)) {
|
||||
value.url = `${value.https ? 'https' : 'http'}//${value.host}:${value.port}`;
|
||||
value.url = `${value.https ? 'https:' : 'http:'}//${value.host}:${value.port}`;
|
||||
}
|
||||
return value;
|
||||
},
|
||||
|
@ -33,9 +33,6 @@ export const createApp = (options: CreateOptions) => async (): Promise<App> => {
|
||||
|
||||
app.container = await builder({ configure: app.configure, BootModule });
|
||||
|
||||
if (app.configure.has('app.prefix')) {
|
||||
app.container.setGlobalPrefix(await app.configure.get<string>('app.prefix'));
|
||||
}
|
||||
useContainer(app.container.select(BootModule), { fallbackOnErrors: true });
|
||||
return app;
|
||||
};
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { INestApplication, Type } from '@nestjs/common';
|
||||
import { INestApplication, Injectable, Type } from '@nestjs/common';
|
||||
import { RouterModule } from '@nestjs/core';
|
||||
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
@ -15,6 +15,7 @@ import {
|
||||
} from './types';
|
||||
import { trimPath } from './utils';
|
||||
|
||||
@Injectable()
|
||||
export class Restful extends BaseRestful {
|
||||
protected _docs!: { [version: string]: ApiDocOption };
|
||||
|
||||
|
@ -4,18 +4,19 @@ import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify
|
||||
|
||||
import * as configs from './config';
|
||||
import { ContentModule } from './modules/content/content.module';
|
||||
import { CoreModule } from './modules/core/core.module';
|
||||
import { CreateOptions } from './modules/core/types';
|
||||
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';
|
||||
|
||||
export const createOptions: CreateOptions = {
|
||||
config: { factories: configs as any, storage: { enable: true } },
|
||||
modules: async (configure) => [
|
||||
DatabaseModule.forRoot(configure),
|
||||
MeiliModule.forRoot(configure),
|
||||
RestfulModule.forRoot(configure),
|
||||
ContentModule.forRoot(configure),
|
||||
CoreModule.forRoot(configure),
|
||||
],
|
||||
globals: {},
|
||||
builder: async ({ configure, BootModule }) => {
|
||||
@ -27,6 +28,8 @@ export const createOptions: CreateOptions = {
|
||||
logger: ['error', 'warn'],
|
||||
},
|
||||
);
|
||||
const restful = container.get(Restful);
|
||||
await restful.factoryDocs(container);
|
||||
return container;
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user