# nestjs-template **Repository Path**: hule_sky/nestjs-template ## Basic Information - **Project Name**: nestjs-template - **Description**: Hully Nestjs+Mongodb - **Primary Language**: TypeScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-03-10 - **Last Updated**: 2022-09-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Nestjs Template Hully Nestjs Project Template ## Description Hully Nestjs 项目模版 ## Installation ```bash $ npm install ``` ## Running the app ```bash # development $ npm run start # watch mode $ npm run start:dev # production mode $ npm run start:prod ``` ## Test ```bash # unit tests $ npm run test # e2e tests $ npm run test:e2e # test coverage $ npm run test:cov ``` #### API Docs - 采用内置 Swagger 模块,项目启动后在浏览器里输入 `http://127.0.0.1:port/apidoc`即可访问到文档,其中端口号与 api 地址前缀设置在`main.ts`文件中,此方式需要 在 dto 文件中注明参数属性。 #### Pipe 管道 > 两种类型: 转换 与 验证 1. 系统自带 - ValidationPipe - ParseIntPipe - ParseBoolPipe - ParseArrayPipe - ParseUUIDPipe - DefaultValuePipe - ParseEnumPipe - ParseFloatPipe 2. 自定义管道 ``` // 自定义路由管道,实现统一请求参数校验 app.useGlobalPipes(new KwValidationPipe()); export class KwValidationPipe implements PipeTransform { async transform(value: any, metadata: ArgumentMetadata) { } } ``` #### 使用拦截器、异常过滤器实现统一返回格式 ``` // 全局统一返回格式拦截器 app.useGlobalInterceptors(new KwResponseTransformInterceptor()); export class KwResponseTransformInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable | Promise> { const ctx = context.switchToHttp(); const response = ctx.getResponse(); response.status(KWCode.success); return next.handle().pipe( map((data: any) => { if (data.paging) { return new KWPagingResponse(data.data, data.paging, data.reference); } if (data.reference) { return new KWResponse(data.data, data.reference); } return new KWResponse(data); }) ); } } // 全局异常过滤器, 捕获所有的异常 app.useGlobalFilters(new KwExceptionFilter()); @Catch() export class KwExceptionFilter implements ExceptionFilter { catch(exception: Error, host: ArgumentsHost) {} } ``` # Git Commit 规范 - `feat` 增加新功能 - `fix` BUG Fixed - `docs` 文档和注释 - `chore` 依赖更新、脚手架配置修改等 - `refactor` 重构 - `build` 编译相关的修改,例如发布版本、对项目构建或者依赖的改动 - `test` 测试用例相关 - `style` 代码格式修改 - `perf` 代码优化相关,比如提升性能、体验 - `revert` 代码回滚 - `ci` 持续集成 - `types` 类型定义文件更改