📝 add @nestjs/swagger
This commit is contained in:
		
							parent
							
								
									9e6e36e115
								
							
						
					
					
						commit
						02d200d9f6
					
				| @ -6,6 +6,7 @@ | |||||||
|     "spec": false |     "spec": false | ||||||
|   }, |   }, | ||||||
|   "compilerOptions": { |   "compilerOptions": { | ||||||
|     "deleteOutDir": true |     "deleteOutDir": true, | ||||||
|  |     "plugins": ["@nestjs/swagger/plugin"] | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -30,6 +30,7 @@ | |||||||
|     "@nestjs/core": "^9.0.0", |     "@nestjs/core": "^9.0.0", | ||||||
|     "@nestjs/mapped-types": "^1.2.2", |     "@nestjs/mapped-types": "^1.2.2", | ||||||
|     "@nestjs/platform-express": "^9.0.0", |     "@nestjs/platform-express": "^9.0.0", | ||||||
|  |     "@nestjs/swagger": "^6.2.1", | ||||||
|     "@prisma/client": "^4.10.1", |     "@prisma/client": "^4.10.1", | ||||||
|     "class-transformer": "^0.5.1", |     "class-transformer": "^0.5.1", | ||||||
|     "class-validator": "^0.14.0", |     "class-validator": "^0.14.0", | ||||||
|  | |||||||
| @ -8,6 +8,7 @@ specifiers: | |||||||
|   '@nestjs/mapped-types': ^1.2.2 |   '@nestjs/mapped-types': ^1.2.2 | ||||||
|   '@nestjs/platform-express': ^9.0.0 |   '@nestjs/platform-express': ^9.0.0 | ||||||
|   '@nestjs/schematics': ^9.0.0 |   '@nestjs/schematics': ^9.0.0 | ||||||
|  |   '@nestjs/swagger': ^6.2.1 | ||||||
|   '@prisma/client': ^4.10.1 |   '@prisma/client': ^4.10.1 | ||||||
|   '@types/express': ^4.17.13 |   '@types/express': ^4.17.13 | ||||||
|   '@types/node': 18.11.18 |   '@types/node': 18.11.18 | ||||||
| @ -38,6 +39,7 @@ dependencies: | |||||||
|   '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm |   '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm | ||||||
|   '@nestjs/mapped-types': 1.2.2_sm5si6oczf3vcbfl6qa276t67m |   '@nestjs/mapped-types': 1.2.2_sm5si6oczf3vcbfl6qa276t67m | ||||||
|   '@nestjs/platform-express': 9.3.9_77foi4w27ghy47yutmnzv7krjy |   '@nestjs/platform-express': 9.3.9_77foi4w27ghy47yutmnzv7krjy | ||||||
|  |   '@nestjs/swagger': 6.2.1_ldzmua2hsw2tga2e42i6lmmdty | ||||||
|   '@prisma/client': 4.10.1_prisma@4.10.1 |   '@prisma/client': 4.10.1_prisma@4.10.1 | ||||||
|   class-transformer: 0.5.1 |   class-transformer: 0.5.1 | ||||||
|   class-validator: 0.14.0 |   class-validator: 0.14.0 | ||||||
| @ -497,6 +499,35 @@ packages: | |||||||
|       - chokidar |       - chokidar | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|  |   /@nestjs/swagger/6.2.1_ldzmua2hsw2tga2e42i6lmmdty: | ||||||
|  |     resolution: {integrity: sha512-9M2vkfJHIzLqDZwvM5TEZO0MxRCvIb0xVy0LsmWwxH1lrb0z/4MhU+r2CWDhBtTccVJrKxVPiU2s3T3b9uUJbg==} | ||||||
|  |     peerDependencies: | ||||||
|  |       '@fastify/static': ^6.0.0 | ||||||
|  |       '@nestjs/common': ^9.0.0 | ||||||
|  |       '@nestjs/core': ^9.0.0 | ||||||
|  |       class-transformer: '*' | ||||||
|  |       class-validator: '*' | ||||||
|  |       reflect-metadata: ^0.1.12 | ||||||
|  |     peerDependenciesMeta: | ||||||
|  |       '@fastify/static': | ||||||
|  |         optional: true | ||||||
|  |       class-transformer: | ||||||
|  |         optional: true | ||||||
|  |       class-validator: | ||||||
|  |         optional: true | ||||||
|  |     dependencies: | ||||||
|  |       '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|  |       '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm | ||||||
|  |       '@nestjs/mapped-types': 1.2.2_sm5si6oczf3vcbfl6qa276t67m | ||||||
|  |       class-transformer: 0.5.1 | ||||||
|  |       class-validator: 0.14.0 | ||||||
|  |       js-yaml: 4.1.0 | ||||||
|  |       lodash: 4.17.21 | ||||||
|  |       path-to-regexp: 3.2.0 | ||||||
|  |       reflect-metadata: 0.1.13 | ||||||
|  |       swagger-ui-dist: 4.15.5 | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|   /@nodelib/fs.scandir/2.1.5: |   /@nodelib/fs.scandir/2.1.5: | ||||||
|     resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} |     resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} | ||||||
|     engines: {node: '>= 8'} |     engines: {node: '>= 8'} | ||||||
| @ -1061,7 +1092,6 @@ packages: | |||||||
| 
 | 
 | ||||||
|   /argparse/2.0.1: |   /argparse/2.0.1: | ||||||
|     resolution: {integrity: sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==} |     resolution: {integrity: sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==} | ||||||
|     dev: true |  | ||||||
| 
 | 
 | ||||||
|   /array-flatten/1.1.1: |   /array-flatten/1.1.1: | ||||||
|     resolution: {integrity: sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==} |     resolution: {integrity: sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==} | ||||||
| @ -2261,7 +2291,6 @@ packages: | |||||||
|     hasBin: true |     hasBin: true | ||||||
|     dependencies: |     dependencies: | ||||||
|       argparse: 2.0.1 |       argparse: 2.0.1 | ||||||
|     dev: true |  | ||||||
| 
 | 
 | ||||||
|   /json-parse-even-better-errors/2.3.1: |   /json-parse-even-better-errors/2.3.1: | ||||||
|     resolution: {integrity: sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==} |     resolution: {integrity: sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==} | ||||||
| @ -3250,6 +3279,10 @@ packages: | |||||||
|     engines: {node: '>= 0.4'} |     engines: {node: '>= 0.4'} | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|  |   /swagger-ui-dist/4.15.5: | ||||||
|  |     resolution: {integrity: sha512-V3eIa28lwB6gg7/wfNvAbjwJYmDXy1Jo1POjyTzlB6wPcHiGlRxq39TSjYGVjQrUSAzpv+a7nzp7mDxgNy57xA==} | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|   /symbol-observable/4.0.0: |   /symbol-observable/4.0.0: | ||||||
|     resolution: {integrity: sha512-b19dMThMV4HVFynSAM1++gBHAbk2Tc/osgLIBZMKsyqh34jb2e8Os7T6ZW/Bt3pJFdBTd2JwAnAAEQV7rSNvcQ==} |     resolution: {integrity: sha512-b19dMThMV4HVFynSAM1++gBHAbk2Tc/osgLIBZMKsyqh34jb2e8Os7T6ZW/Bt3pJFdBTd2JwAnAAEQV7rSNvcQ==} | ||||||
|     engines: {node: '>=0.10'} |     engines: {node: '>=0.10'} | ||||||
|  | |||||||
							
								
								
									
										10
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/main.ts
									
									
									
									
									
								
							| @ -1,6 +1,7 @@ | |||||||
| import { HttpAdapterHost, NestFactory } from '@nestjs/core' | import { HttpAdapterHost, NestFactory } from '@nestjs/core' | ||||||
| import { ValidationPipe } from '@nestjs/common' | import { ValidationPipe } from '@nestjs/common' | ||||||
| import { ConfigService } from '@nestjs/config' | import { ConfigService } from '@nestjs/config' | ||||||
|  | import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger' | ||||||
| import { PrismaClientExceptionFilter, PrismaService } from 'nestjs-prisma' | import { PrismaClientExceptionFilter, PrismaService } from 'nestjs-prisma' | ||||||
| import { AppModule } from './app.module' | import { AppModule } from './app.module' | ||||||
| 
 | 
 | ||||||
| @ -18,6 +19,15 @@ async function bootstrap() { | |||||||
|   const { httpAdapter } = app.get(HttpAdapterHost) |   const { httpAdapter } = app.get(HttpAdapterHost) | ||||||
|   app.useGlobalFilters(new PrismaClientExceptionFilter(httpAdapter)) |   app.useGlobalFilters(new PrismaClientExceptionFilter(httpAdapter)) | ||||||
| 
 | 
 | ||||||
|  |   // Swagger Api
 | ||||||
|  |   const options = new DocumentBuilder() | ||||||
|  |     .setTitle('Nest Project') | ||||||
|  |     .setDescription('The Nest-Project API description') | ||||||
|  |     .setVersion('1.0') | ||||||
|  |     .build() | ||||||
|  |   const document = SwaggerModule.createDocument(app, options) | ||||||
|  |   SwaggerModule.setup('api', app, document) | ||||||
|  | 
 | ||||||
|   const configService = app.get(ConfigService) |   const configService = app.get(ConfigService) | ||||||
|   const PORT = configService.get<number>('PORT', 12400) |   const PORT = configService.get<number>('PORT', 12400) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,7 +1,6 @@ | |||||||
| import { | import { | ||||||
|   IsNotEmpty, |   IsNotEmpty, | ||||||
|   IsOptional, |   IsOptional, | ||||||
|   IsString, |  | ||||||
|   Length, |   Length, | ||||||
|   IsEmail, |   IsEmail, | ||||||
|   IsStrongPassword, |   IsStrongPassword, | ||||||
| @ -13,11 +12,9 @@ export class CreateUserDto { | |||||||
|   email: string |   email: string | ||||||
| 
 | 
 | ||||||
|   @IsOptional() |   @IsOptional() | ||||||
|   @IsString() |  | ||||||
|   @Length(5, 20) |   @Length(5, 20) | ||||||
|   username?: string |   username?: string | ||||||
| 
 | 
 | ||||||
|   @IsString() |  | ||||||
|   @IsNotEmpty() |   @IsNotEmpty() | ||||||
|   @IsStrongPassword() |   @IsStrongPassword() | ||||||
|   password: string |   password: string | ||||||
|  | |||||||
							
								
								
									
										10
									
								
								src/users/entities/user.entity.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/users/entities/user.entity.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | |||||||
|  | import { User } from '@prisma/client' | ||||||
|  | 
 | ||||||
|  | export class UserEntity implements User { | ||||||
|  |   id: string | ||||||
|  |   email: string | ||||||
|  |   username: string | null | ||||||
|  |   password: string | ||||||
|  |   createdAt: Date | ||||||
|  |   updatedAt: Date | ||||||
|  | } | ||||||
| @ -1,16 +1,22 @@ | |||||||
| import { Controller, Get, Post, Body, Param } from '@nestjs/common' | import { Controller, Get, Post, Body, Param } from '@nestjs/common' | ||||||
| import { UsersService } from './users.service' | import { UsersService } from './users.service' | ||||||
| import { CreateUserDto } from './dto/create-user.dto' | import { CreateUserDto } from './dto/create-user.dto' | ||||||
|  | import { ApiOkResponse, ApiTags } from '@nestjs/swagger' | ||||||
|  | import { UserEntity } from './entities/user.entity' | ||||||
| 
 | 
 | ||||||
|  | @ApiTags('user') | ||||||
| @Controller() | @Controller() | ||||||
| export class UsersController { | export class UsersController { | ||||||
|   constructor(private readonly userService: UsersService) {} |   constructor(private readonly userService: UsersService) {} | ||||||
| 
 | 
 | ||||||
|  |   // TODO: 分页
 | ||||||
|  |   @ApiOkResponse({ type: UserEntity, isArray: true }) | ||||||
|   @Get('users') |   @Get('users') | ||||||
|   async findUsers() { |   async findUsers() { | ||||||
|     return this.userService.findUsers() |     return this.userService.findUsers() | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   @ApiOkResponse({ type: UserEntity }) | ||||||
|   @Get('users/:id') |   @Get('users/:id') | ||||||
|   async getUserInfo(@Param('id') id: string) { |   async getUserInfo(@Param('id') id: string) { | ||||||
|     return this.userService.getUserInfo(id) |     return this.userService.getUserInfo(id) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user