create-dp-koa 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
# Swagger 集成使用指南
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
本文档介绍如何在 DP Koa Framework 中使用 Swagger 注解来生成 API 文档。
|
|
6
|
+
|
|
7
|
+
## 功能特性
|
|
8
|
+
|
|
9
|
+
- ✅ **注解式文档生成**:通过装饰器生成 Swagger 文档
|
|
10
|
+
- ✅ **自动生成 UI**:提供 Swagger UI 界面查看和测试 API
|
|
11
|
+
- ✅ **开发环境专用**:仅在开发环境下启用
|
|
12
|
+
- ✅ **类型安全**:支持 TypeScript 类型提示
|
|
13
|
+
|
|
14
|
+
## 快速开始
|
|
15
|
+
|
|
16
|
+
### 1. 访问 Swagger UI
|
|
17
|
+
|
|
18
|
+
启动应用后,访问以下地址:
|
|
19
|
+
|
|
20
|
+
- **Swagger UI**:http://localhost:3000/swagger
|
|
21
|
+
- **Swagger JSON**:http://localhost:3000/swagger.json
|
|
22
|
+
|
|
23
|
+
### 2. 基础用法
|
|
24
|
+
|
|
25
|
+
#### 类级别注解
|
|
26
|
+
|
|
27
|
+
使用 `@ApiTags` 为 Controller 类添加标签:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { ApiTags } from '@src/framework/decorator/swagger';
|
|
31
|
+
|
|
32
|
+
@ApiTags('用户管理', '用户相关接口')
|
|
33
|
+
export class UserController extends BaseController {
|
|
34
|
+
// ...
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### 方法级别注解
|
|
39
|
+
|
|
40
|
+
使用 `@Api` 为方法添加基本信息:
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { Api } from '@src/framework/decorator/swagger';
|
|
44
|
+
|
|
45
|
+
@Get('/user')
|
|
46
|
+
@Api({
|
|
47
|
+
tags: ['用户管理'],
|
|
48
|
+
summary: '获取用户列表',
|
|
49
|
+
description: '根据查询参数获取用户列表',
|
|
50
|
+
operationId: 'getUserList',
|
|
51
|
+
})
|
|
52
|
+
async getUserList() {
|
|
53
|
+
// ...
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### 响应注解
|
|
58
|
+
|
|
59
|
+
使用 `@ApiResponse` 定义响应结构:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { ApiResponse } from '@src/framework/decorator/swagger';
|
|
63
|
+
|
|
64
|
+
@ApiResponse({
|
|
65
|
+
status: 200,
|
|
66
|
+
description: '成功返回用户列表',
|
|
67
|
+
schema: {
|
|
68
|
+
type: 'object',
|
|
69
|
+
properties: {
|
|
70
|
+
code: { type: 'number' },
|
|
71
|
+
data: { type: 'array' },
|
|
72
|
+
message: { type: 'string' },
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## 完整示例
|
|
79
|
+
|
|
80
|
+
### 1. GET 请求示例
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { Get, Query } from '@src/framework/decorator/controller';
|
|
84
|
+
import { Api, ApiResponse } from '@src/framework/decorator/swagger';
|
|
85
|
+
|
|
86
|
+
@Api({
|
|
87
|
+
tags: ['用户管理'],
|
|
88
|
+
summary: '获取用户列表',
|
|
89
|
+
description: '根据查询参数获取用户列表',
|
|
90
|
+
})
|
|
91
|
+
@ApiResponse({
|
|
92
|
+
status: 200,
|
|
93
|
+
description: '成功返回用户列表',
|
|
94
|
+
})
|
|
95
|
+
@ApiResponse({
|
|
96
|
+
status: 400,
|
|
97
|
+
description: '参数错误',
|
|
98
|
+
})
|
|
99
|
+
@Get('/user/list')
|
|
100
|
+
async getUserList(@Query() query: { page: number; pageSize: number }) {
|
|
101
|
+
return this.success({
|
|
102
|
+
users: [],
|
|
103
|
+
total: 0,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 2. POST 请求示例
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { Post, Body } from '@src/framework/decorator/controller';
|
|
112
|
+
import { Api, ApiBody, ApiResponse } from '@src/framework/decorator/swagger';
|
|
113
|
+
|
|
114
|
+
@Api({
|
|
115
|
+
tags: ['用户管理'],
|
|
116
|
+
summary: '创建用户',
|
|
117
|
+
description: '创建新用户',
|
|
118
|
+
})
|
|
119
|
+
@ApiBody({
|
|
120
|
+
description: '用户信息',
|
|
121
|
+
required: true,
|
|
122
|
+
schema: {
|
|
123
|
+
type: 'object',
|
|
124
|
+
required: ['name', 'email'],
|
|
125
|
+
properties: {
|
|
126
|
+
name: { type: 'string' },
|
|
127
|
+
email: { type: 'string', format: 'email' },
|
|
128
|
+
age: { type: 'number' },
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
@ApiResponse({
|
|
133
|
+
status: 200,
|
|
134
|
+
description: '成功创建用户',
|
|
135
|
+
})
|
|
136
|
+
@Post('/user')
|
|
137
|
+
async createUser(@Body() body: { name: string; email: string; age?: number }) {
|
|
138
|
+
return this.success({ id: 1, ...body });
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 3. 带路径参数的示例
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { Get, Params } from '@src/framework/decorator/controller';
|
|
146
|
+
import { Api, ApiResponse } from '@src/framework/decorator/swagger';
|
|
147
|
+
|
|
148
|
+
@Api({
|
|
149
|
+
tags: ['用户管理'],
|
|
150
|
+
summary: '获取用户详情',
|
|
151
|
+
description: '根据用户ID获取用户信息',
|
|
152
|
+
})
|
|
153
|
+
@ApiResponse({
|
|
154
|
+
status: 200,
|
|
155
|
+
description: '成功返回用户详情',
|
|
156
|
+
})
|
|
157
|
+
@ApiResponse({
|
|
158
|
+
status: 404,
|
|
159
|
+
description: '用户不存在',
|
|
160
|
+
})
|
|
161
|
+
@Get('/user/:id')
|
|
162
|
+
async getUserById(@Params() params: { id: number }) {
|
|
163
|
+
return this.success({ id: params.id, name: '张三' });
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## 装饰器说明
|
|
168
|
+
|
|
169
|
+
### @ApiTags
|
|
170
|
+
|
|
171
|
+
为类添加标签,用于分组。
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
@ApiTags('用户管理', '示例')
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### @Api
|
|
178
|
+
|
|
179
|
+
为方法添加基本信息。
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
@Api({
|
|
183
|
+
tags?: string[]; // 标签
|
|
184
|
+
summary?: string; // 摘要
|
|
185
|
+
description?: string; // 详细描述
|
|
186
|
+
operationId?: string; // 操作ID
|
|
187
|
+
})
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### @ApiResponse
|
|
191
|
+
|
|
192
|
+
定义 API 响应结构。
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
@ApiResponse({
|
|
196
|
+
status: number; // HTTP 状态码
|
|
197
|
+
description: string; // 响应描述
|
|
198
|
+
schema?: any; // 响应 schema
|
|
199
|
+
examples?: Record<string, any>; // 示例数据
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### @ApiBody
|
|
204
|
+
|
|
205
|
+
定义请求体结构。
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
@ApiBody({
|
|
209
|
+
description?: string; // 描述
|
|
210
|
+
required?: boolean; // 是否必需
|
|
211
|
+
schema: any; // Schema
|
|
212
|
+
examples?: Record<string, any>; // 示例数据
|
|
213
|
+
})
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### @ApiParam
|
|
217
|
+
|
|
218
|
+
定义路径参数。
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
@ApiParam({
|
|
222
|
+
name: string; // 参数名
|
|
223
|
+
in: 'path' | 'query' | 'header' | 'cookie';
|
|
224
|
+
description?: string; // 描述
|
|
225
|
+
required?: boolean; // 是否必需
|
|
226
|
+
schema?: any; // Schema
|
|
227
|
+
example?: any; // 示例
|
|
228
|
+
})
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## 响应 Schema 定义
|
|
232
|
+
|
|
233
|
+
### 基础类型
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
{
|
|
237
|
+
type: 'string',
|
|
238
|
+
type: 'number',
|
|
239
|
+
type: 'boolean',
|
|
240
|
+
type: 'integer',
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### 对象类型
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
{
|
|
248
|
+
type: 'object',
|
|
249
|
+
properties: {
|
|
250
|
+
name: { type: 'string' },
|
|
251
|
+
age: { type: 'number' },
|
|
252
|
+
},
|
|
253
|
+
required: ['name'], // 必需字段
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### 数组类型
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
{
|
|
261
|
+
type: 'array',
|
|
262
|
+
items: {
|
|
263
|
+
type: 'string',
|
|
264
|
+
},
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### 复杂对象
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
{
|
|
272
|
+
type: 'object',
|
|
273
|
+
properties: {
|
|
274
|
+
code: { type: 'number', example: 0 },
|
|
275
|
+
data: {
|
|
276
|
+
type: 'array',
|
|
277
|
+
items: {
|
|
278
|
+
type: 'object',
|
|
279
|
+
properties: {
|
|
280
|
+
id: { type: 'number' },
|
|
281
|
+
name: { type: 'string' },
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
message: { type: 'string' },
|
|
286
|
+
},
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## 最佳实践
|
|
291
|
+
|
|
292
|
+
### 1. 统一响应格式
|
|
293
|
+
|
|
294
|
+
定义统一的响应格式:
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
@ApiResponse({
|
|
298
|
+
status: 200,
|
|
299
|
+
description: '成功响应',
|
|
300
|
+
schema: {
|
|
301
|
+
type: 'object',
|
|
302
|
+
properties: {
|
|
303
|
+
code: { type: 'number', example: 0 },
|
|
304
|
+
data: { type: 'object' },
|
|
305
|
+
message: { type: 'string', example: '操作成功' },
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
})
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### 2. 错误响应
|
|
312
|
+
|
|
313
|
+
为所有接口定义错误响应:
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
@ApiResponse({
|
|
317
|
+
status: 400,
|
|
318
|
+
description: '参数错误',
|
|
319
|
+
})
|
|
320
|
+
@ApiResponse({
|
|
321
|
+
status: 401,
|
|
322
|
+
description: '未授权',
|
|
323
|
+
})
|
|
324
|
+
@ApiResponse({
|
|
325
|
+
status: 403,
|
|
326
|
+
description: '权限不足',
|
|
327
|
+
})
|
|
328
|
+
@ApiResponse({
|
|
329
|
+
status: 500,
|
|
330
|
+
description: '服务器错误',
|
|
331
|
+
})
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### 3. 使用标签分组
|
|
335
|
+
|
|
336
|
+
使用 `@ApiTags` 对 API 进行分组:
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
@ApiTags('用户管理')
|
|
340
|
+
@ApiTags('订单管理')
|
|
341
|
+
@ApiTags('商品管理')
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### 4. 提供完整示例
|
|
345
|
+
|
|
346
|
+
为复杂的数据结构提供示例:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
@ApiBody({
|
|
350
|
+
schema: {
|
|
351
|
+
type: 'object',
|
|
352
|
+
properties: {
|
|
353
|
+
name: { type: 'string' },
|
|
354
|
+
email: { type: 'string' },
|
|
355
|
+
},
|
|
356
|
+
},
|
|
357
|
+
examples: {
|
|
358
|
+
example1: {
|
|
359
|
+
summary: '示例数据',
|
|
360
|
+
value: {
|
|
361
|
+
name: '张三',
|
|
362
|
+
email: 'zhang@example.com',
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
})
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## 查看示例
|
|
370
|
+
|
|
371
|
+
项目提供了完整的示例:
|
|
372
|
+
|
|
373
|
+
1. **基础示例**:`src/controllers/example/ExampleController.ts`
|
|
374
|
+
2. **完整示例**:`src/controllers/example/SwaggerExampleController.ts`
|
|
375
|
+
|
|
376
|
+
启动应用后,访问以下地址查看:
|
|
377
|
+
|
|
378
|
+
- http://localhost:3000/swagger
|
|
379
|
+
- http://localhost:3000/swagger/user/list (GET)
|
|
380
|
+
- http://localhost:3000/swagger/user/:id (GET)
|
|
381
|
+
- http://localhost:3000/swagger/user (POST)
|
|
382
|
+
- http://localhost:3000/swagger/user/:id (PUT)
|
|
383
|
+
- http://localhost:3000/swagger/user/:id (DELETE)
|
|
384
|
+
|
|
385
|
+
## 注意事项
|
|
386
|
+
|
|
387
|
+
1. **仅开发环境**:Swagger UI 仅在开发环境下启用
|
|
388
|
+
2. **类型安全**:建议为 DTO 类添加类验证器
|
|
389
|
+
3. **完整描述**:为所有接口提供完整的描述和示例
|
|
390
|
+
4. **统一格式**:保持响应格式的一致性
|
|
391
|
+
5. **版本控制**:在生产环境中关闭 Swagger 访问
|
|
392
|
+
|
|
393
|
+
## 故障排查
|
|
394
|
+
|
|
395
|
+
### 问题1:无法访问 Swagger UI
|
|
396
|
+
|
|
397
|
+
**解决方案**:
|
|
398
|
+
- 确认环境变量 `NODE_ENV` 不是 `production`
|
|
399
|
+
- 检查端口是否正确
|
|
400
|
+
- 查看控制台是否有错误信息
|
|
401
|
+
|
|
402
|
+
### 问题2:文档不完整
|
|
403
|
+
|
|
404
|
+
**解决方案**:
|
|
405
|
+
- 检查是否添加了必要的装饰器
|
|
406
|
+
- 确认路由已正确注册
|
|
407
|
+
- 查看生成的 JSON 文件
|
|
408
|
+
|
|
409
|
+
## 扩展阅读
|
|
410
|
+
|
|
411
|
+
- [Swagger OpenAPI 规范](https://swagger.io/specification/)
|
|
412
|
+
- [koa2-swagger-ui 文档](https://www.npmjs.com/package/koa2-swagger-ui)
|
|
413
|
+
- [swagger-jsdoc 文档](https://github.com/Surnet/swagger-jsdoc)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|