├── .gitignore ├── .vscode └── launch.json ├── README.md ├── data-receive ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package.json ├── public │ └── index.html ├── src │ ├── app.module.ts │ ├── main.ts │ └── person │ │ ├── dto │ │ └── create-person.dto.ts │ │ ├── person.controller.ts │ │ └── person.module.ts ├── test │ ├── app.e2e-spec.ts │ └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json ├── notes-management ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src │ ├── app.module.ts │ ├── main.ts │ └── notes │ │ ├── dto │ │ ├── create-note.dto.ts │ │ ├── find-note.dto.ts │ │ └── update-note.dto.ts │ │ ├── entities │ │ └── note.entity.ts │ │ ├── notes.controller.ts │ │ ├── notes.module.ts │ │ └── notes.service.ts ├── test │ ├── app.e2e-spec.ts │ └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json ├── params-validation ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package.json ├── src │ ├── app.controller.spec.ts │ ├── app.controller.ts │ ├── app.module.ts │ ├── app.service.ts │ ├── main.ts │ ├── person │ │ ├── dto │ │ │ ├── create-person.dto.ts │ │ │ └── update-person.dto.ts │ │ ├── entities │ │ │ └── person.entity.ts │ │ ├── person.controller.spec.ts │ │ ├── person.controller.ts │ │ ├── person.module.ts │ │ ├── person.service.spec.ts │ │ └── person.service.ts │ └── pipes │ │ └── MyValidationPipe.ts ├── test │ ├── app.e2e-spec.ts │ └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json └── status ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package.json ├── src ├── app.controller.ts ├── app.module.ts └── main.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | *-lock.json 4 | yarn.lock 5 | dist 6 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "type": "node", 5 | "request": "launch", 6 | "name": "文章管理", 7 | "runtimeExecutable": "npm", 8 | "runtimeArgs": [ 9 | "run", 10 | "start" 11 | ], 12 | "port": 9229, 13 | "cwd": "${workspaceFolder}/article-management", 14 | "skipFiles": [ 15 | "/**" 16 | ] 17 | }, 18 | { 19 | "type": "node", 20 | "request": "launch", 21 | "name": "参数校验", 22 | "runtimeExecutable": "npm", 23 | "runtimeArgs": [ 24 | "run", 25 | "start" 26 | ], 27 | "port": 9229, 28 | "cwd": "${workspaceFolder}/params-validation", 29 | "skipFiles": [ 30 | "/**" 31 | ] 32 | } 33 | ] 34 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nestjs-exercize 2 | 3 | [笔记管理](./notes-management) 4 | 5 | [参数校验](./params-validation) 6 | 7 | [数据接收](./data-receive) -------------------------------------------------------------------------------- /data-receive/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /data-receive/.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json -------------------------------------------------------------------------------- /data-receive/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /data-receive/README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | 19 | Support us 20 | 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Installation 30 | 31 | ```bash 32 | $ npm install 33 | ``` 34 | 35 | ## Running the app 36 | 37 | ```bash 38 | # development 39 | $ npm run start 40 | 41 | # watch mode 42 | $ npm run start:dev 43 | 44 | # production mode 45 | $ npm run start:prod 46 | ``` 47 | 48 | ## Test 49 | 50 | ```bash 51 | # unit tests 52 | $ npm run test 53 | 54 | # e2e tests 55 | $ npm run test:e2e 56 | 57 | # test coverage 58 | $ npm run test:cov 59 | ``` 60 | 61 | ## Support 62 | 63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 64 | 65 | ## Stay in touch 66 | 67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 68 | - Website - [https://nestjs.com](https://nestjs.com/) 69 | - Twitter - [@nestframework](https://twitter.com/nestframework) 70 | 71 | ## License 72 | 73 | Nest is [MIT licensed](LICENSE). 74 | -------------------------------------------------------------------------------- /data-receive/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /data-receive/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "data-receive", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^8.0.0", 25 | "@nestjs/core": "^8.0.0", 26 | "@nestjs/mapped-types": "*", 27 | "@nestjs/platform-express": "^8.0.0", 28 | "reflect-metadata": "^0.1.13", 29 | "rimraf": "^3.0.2", 30 | "rxjs": "^7.2.0" 31 | }, 32 | "devDependencies": { 33 | "@nestjs/cli": "^8.0.0", 34 | "@nestjs/schematics": "^8.0.0", 35 | "@nestjs/testing": "^8.0.0", 36 | "@types/express": "^4.17.13", 37 | "@types/jest": "27.0.2", 38 | "@types/multer": "^1.4.7", 39 | "@types/node": "^16.0.0", 40 | "@types/supertest": "^2.0.11", 41 | "@typescript-eslint/eslint-plugin": "^5.0.0", 42 | "@typescript-eslint/parser": "^5.0.0", 43 | "eslint": "^8.0.1", 44 | "eslint-config-prettier": "^8.3.0", 45 | "eslint-plugin-prettier": "^4.0.0", 46 | "jest": "^27.2.5", 47 | "prettier": "^2.3.2", 48 | "source-map-support": "^0.5.20", 49 | "supertest": "^6.1.3", 50 | "ts-jest": "^27.0.3", 51 | "ts-loader": "^9.2.3", 52 | "ts-node": "^10.0.0", 53 | "tsconfig-paths": "^3.10.1", 54 | "typescript": "^4.3.5" 55 | }, 56 | "jest": { 57 | "moduleFileExtensions": [ 58 | "js", 59 | "json", 60 | "ts" 61 | ], 62 | "rootDir": "src", 63 | "testRegex": ".*\\.spec\\.ts$", 64 | "transform": { 65 | "^.+\\.(t|j)s$": "ts-jest" 66 | }, 67 | "collectCoverageFrom": [ 68 | "**/*.(t|j)s" 69 | ], 70 | "coverageDirectory": "../coverage", 71 | "testEnvironment": "node" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /data-receive/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | 12 | 13 | 62 | 63 | -------------------------------------------------------------------------------- /data-receive/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { PersonModule } from './person/person.module'; 3 | 4 | @Module({ 5 | imports: [PersonModule], 6 | controllers: [], 7 | providers: [], 8 | }) 9 | export class AppModule {} 10 | -------------------------------------------------------------------------------- /data-receive/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { NestExpressApplication } from '@nestjs/platform-express'; 3 | import { join } from 'path'; 4 | import { AppModule } from './app.module'; 5 | 6 | async function bootstrap() { 7 | const app = await NestFactory.create(AppModule); 8 | app.useStaticAssets(join(__dirname, '..', 'public'), { 9 | prefix: '/static' 10 | }); 11 | await app.listen(3000); 12 | } 13 | bootstrap(); 14 | -------------------------------------------------------------------------------- /data-receive/src/person/dto/create-person.dto.ts: -------------------------------------------------------------------------------- 1 | export class CreatePersonDto { 2 | name: string; 3 | age: number; 4 | } 5 | -------------------------------------------------------------------------------- /data-receive/src/person/person.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseInterceptors, UploadedFiles } from '@nestjs/common'; 2 | import { AnyFilesInterceptor } from '@nestjs/platform-express'; 3 | import { CreatePersonDto } from './dto/create-person.dto'; 4 | 5 | @Controller('api/person') 6 | export class PersonController { 7 | constructor() {} 8 | 9 | @Get('find') 10 | query(@Query('name') name: string, @Query('age') age: number) { 11 | return `received: name=${name},age=${age}`; 12 | } 13 | 14 | @Get(':id') 15 | urlParm(@Param('id') id: string) { 16 | return `received: id=${id}`; 17 | } 18 | 19 | @Post() 20 | body(@Body() createPersonDto: CreatePersonDto) { 21 | return `received: ${JSON.stringify(createPersonDto)}` 22 | } 23 | 24 | @Post('file') 25 | @UseInterceptors(AnyFilesInterceptor()) 26 | body2(@Body() createPersonDto: CreatePersonDto, @UploadedFiles() files: Array) { 27 | console.log(files); 28 | return `received: ${JSON.stringify(createPersonDto)}` 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /data-receive/src/person/person.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { PersonController } from './person.controller'; 3 | 4 | @Module({ 5 | controllers: [PersonController], 6 | providers: [] 7 | }) 8 | export class PersonModule {} 9 | -------------------------------------------------------------------------------- /data-receive/test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /data-receive/test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /data-receive/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /data-receive/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /notes-management/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /notes-management/.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json -------------------------------------------------------------------------------- /notes-management/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /notes-management/README.md: -------------------------------------------------------------------------------- 1 | 原理文章: 2 | 3 | [Nest.js 快速入门:实现对 Mysql 单表的 CRUD](https://mp.weixin.qq.com/s?__biz=Mzg3OTYzMDkzMg==&mid=2247487058&idx=1&sn=fef0fe0e114ec5a7420f679d224a5899&chksm=cf00c169f877487fda71afc03d6f1b7b28c6e9135421292ef688f7c9a3461f1c96beff030a6d&scene=178&cur_album_id=2198094412235309060#rd) -------------------------------------------------------------------------------- /notes-management/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /notes-management/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "article-management", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^8.0.0", 25 | "@nestjs/core": "^8.0.0", 26 | "@nestjs/mapped-types": "*", 27 | "@nestjs/platform-express": "^8.0.0", 28 | "@nestjs/typeorm": "^8.0.2", 29 | "mysql": "^2.18.1", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^7.2.0", 33 | "typeorm": "^0.2.41" 34 | }, 35 | "devDependencies": { 36 | "@nestjs/cli": "^8.0.0", 37 | "@nestjs/schematics": "^8.0.0", 38 | "@nestjs/testing": "^8.0.0", 39 | "@types/express": "^4.17.13", 40 | "@types/jest": "27.0.2", 41 | "@types/node": "^16.11.14", 42 | "@types/supertest": "^2.0.11", 43 | "@typescript-eslint/eslint-plugin": "^5.0.0", 44 | "@typescript-eslint/parser": "^5.0.0", 45 | "eslint": "^8.0.1", 46 | "eslint-config-prettier": "^8.3.0", 47 | "eslint-plugin-prettier": "^4.0.0", 48 | "jest": "^27.2.5", 49 | "prettier": "^2.3.2", 50 | "source-map-support": "^0.5.20", 51 | "supertest": "^6.1.3", 52 | "ts-jest": "^27.0.3", 53 | "ts-loader": "^9.2.3", 54 | "ts-node": "^10.0.0", 55 | "tsconfig-paths": "^3.10.1", 56 | "typescript": "^4.3.5" 57 | }, 58 | "jest": { 59 | "moduleFileExtensions": [ 60 | "js", 61 | "json", 62 | "ts" 63 | ], 64 | "rootDir": "src", 65 | "testRegex": ".*\\.spec\\.ts$", 66 | "transform": { 67 | "^.+\\.(t|j)s$": "ts-jest" 68 | }, 69 | "collectCoverageFrom": [ 70 | "**/*.(t|j)s" 71 | ], 72 | "coverageDirectory": "../coverage", 73 | "testEnvironment": "node" 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /notes-management/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { TypeOrmModule } from '@nestjs/typeorm'; 3 | import { NotesModule } from './notes/notes.module'; 4 | 5 | @Module({ 6 | imports: [ 7 | TypeOrmModule.forRoot({ 8 | type: 'mysql', 9 | host: 'localhost', 10 | port: 3306, 11 | username: 'root', 12 | password: 'xiaoguang1024', 13 | database: 'hello', 14 | autoLoadEntities: true, 15 | synchronize: true, 16 | }), 17 | NotesModule 18 | ] 19 | }) 20 | export class AppModule {} 21 | -------------------------------------------------------------------------------- /notes-management/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | import "reflect-metadata"; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | await app.listen(3000); 8 | } 9 | bootstrap(); 10 | -------------------------------------------------------------------------------- /notes-management/src/notes/dto/create-note.dto.ts: -------------------------------------------------------------------------------- 1 | export class CreateNoteDto { 2 | title: string; 3 | 4 | content: string; 5 | 6 | createTime: Date; 7 | 8 | updateTime: Date; 9 | 10 | isDelete: boolean; 11 | } 12 | -------------------------------------------------------------------------------- /notes-management/src/notes/dto/find-note.dto.ts: -------------------------------------------------------------------------------- 1 | 2 | export class FindNoteDto { 3 | id: string; 4 | } 5 | -------------------------------------------------------------------------------- /notes-management/src/notes/dto/update-note.dto.ts: -------------------------------------------------------------------------------- 1 | export class UpdateNoteDto { 2 | title: string; 3 | 4 | content: string; 5 | 6 | createTime: Date; 7 | 8 | updateTime: Date; 9 | 10 | isDelete: boolean; 11 | } 12 | -------------------------------------------------------------------------------- /notes-management/src/notes/entities/note.entity.ts: -------------------------------------------------------------------------------- 1 | import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"; 2 | 3 | @Entity() 4 | export class Note{ 5 | 6 | @PrimaryGeneratedColumn() 7 | id: number; 8 | 9 | @Column() 10 | title: string; 11 | 12 | @Column() 13 | content: string; 14 | 15 | @Column() 16 | createTime: Date; 17 | 18 | @Column() 19 | updateTime: Date; 20 | 21 | @Column() 22 | isDelete: boolean; 23 | } 24 | -------------------------------------------------------------------------------- /notes-management/src/notes/notes.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; 2 | import { NotesService } from './notes.service'; 3 | import { CreateNoteDto } from './dto/create-note.dto'; 4 | import { UpdateNoteDto } from './dto/update-note.dto'; 5 | 6 | @Controller('notes') 7 | export class NotesController { 8 | constructor(private readonly notesService: NotesService) {} 9 | 10 | @Post() 11 | async create(@Body() createNoteDto: CreateNoteDto) { 12 | return await this.notesService.create(createNoteDto); 13 | } 14 | 15 | @Get() 16 | async findAll() { 17 | return await this.notesService.findAll(); 18 | } 19 | 20 | @Get(':id') 21 | async findOne(@Param('id') id: string) { 22 | return await this.notesService.findOne(+id); 23 | } 24 | 25 | @Patch(':id') 26 | async update(@Param('id') id: string, @Body() updateNoteDto: UpdateNoteDto) { 27 | return await this.notesService.update(+id, updateNoteDto); 28 | } 29 | 30 | @Delete(':id') 31 | async remove(@Param('id') id: string) { 32 | return await this.notesService.remove(+id); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /notes-management/src/notes/notes.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { NotesService } from './notes.service'; 3 | import { NotesController } from './notes.controller'; 4 | import { TypeOrmModule } from '@nestjs/typeorm'; 5 | import { Note } from './entities/note.entity'; 6 | 7 | @Module({ 8 | imports: [TypeOrmModule.forFeature([Note])], 9 | controllers: [NotesController], 10 | providers: [NotesService] 11 | }) 12 | export class NotesModule {} 13 | -------------------------------------------------------------------------------- /notes-management/src/notes/notes.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { InjectRepository } from '@nestjs/typeorm'; 3 | import { Repository } from 'typeorm'; 4 | import { Note } from './entities/note.entity'; 5 | import { CreateNoteDto } from './dto/create-note.dto'; 6 | import { UpdateNoteDto } from './dto/update-note.dto'; 7 | 8 | @Injectable() 9 | export class NotesService { 10 | constructor( 11 | @InjectRepository(Note) private noteRepository: Repository 12 | ) {} 13 | 14 | async create(createNoteDto: CreateNoteDto) { 15 | createNoteDto.createTime = createNoteDto.updateTime = new Date(); 16 | createNoteDto.isDelete = false; 17 | return await this.noteRepository.save(createNoteDto); 18 | } 19 | 20 | async findAll() { 21 | return await this.noteRepository.find(); 22 | } 23 | 24 | async findOne(id: number) { 25 | return await this.noteRepository.findByIds([id]); 26 | } 27 | 28 | async update(id: number, updateNoteDto: UpdateNoteDto) { 29 | updateNoteDto.updateTime = new Date(); 30 | return await this.noteRepository.update(id, updateNoteDto); 31 | } 32 | 33 | async remove(id: number) { 34 | return await this.noteRepository.delete(id); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /notes-management/test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /notes-management/test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /notes-management/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /notes-management/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /params-validation/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /params-validation/.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json -------------------------------------------------------------------------------- /params-validation/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /params-validation/README.md: -------------------------------------------------------------------------------- 1 | 原理文章: 2 | 3 | [一个参数验证,学会 Nest.js 的两大机制:Pipe、ExceptionFilter](https://mp.weixin.qq.com/s?__biz=Mzg3OTYzMDkzMg==&mid=2247487140&idx=1&sn=2f01cdd5d8716b283ca2e81229d67f76&chksm=cf00c19ff87748893209e691e954ca5239b0408ae363ab4a76654539e9b8f751e17693766a01&scene=178&cur_album_id=2198094412235309060#rd) -------------------------------------------------------------------------------- /params-validation/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /params-validation/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "params-validation", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^8.0.0", 25 | "@nestjs/core": "^8.0.0", 26 | "@nestjs/mapped-types": "*", 27 | "@nestjs/platform-express": "^8.0.0", 28 | "class-transformer": "^0.5.1", 29 | "class-validator": "^0.13.2", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^7.2.0" 33 | }, 34 | "devDependencies": { 35 | "@nestjs/cli": "^8.0.0", 36 | "@nestjs/schematics": "^8.0.0", 37 | "@nestjs/testing": "^8.0.0", 38 | "@types/express": "^4.17.13", 39 | "@types/jest": "27.0.2", 40 | "@types/node": "^16.0.0", 41 | "@types/supertest": "^2.0.11", 42 | "@typescript-eslint/eslint-plugin": "^5.0.0", 43 | "@typescript-eslint/parser": "^5.0.0", 44 | "eslint": "^8.0.1", 45 | "eslint-config-prettier": "^8.3.0", 46 | "eslint-plugin-prettier": "^4.0.0", 47 | "jest": "^27.2.5", 48 | "prettier": "^2.3.2", 49 | "source-map-support": "^0.5.20", 50 | "supertest": "^6.1.3", 51 | "ts-jest": "^27.0.3", 52 | "ts-loader": "^9.2.3", 53 | "ts-node": "^10.0.0", 54 | "tsconfig-paths": "^3.10.1", 55 | "typescript": "^4.3.5" 56 | }, 57 | "jest": { 58 | "moduleFileExtensions": [ 59 | "js", 60 | "json", 61 | "ts" 62 | ], 63 | "rootDir": "src", 64 | "testRegex": ".*\\.spec\\.ts$", 65 | "transform": { 66 | "^.+\\.(t|j)s$": "ts-jest" 67 | }, 68 | "collectCoverageFrom": [ 69 | "**/*.(t|j)s" 70 | ], 71 | "coverageDirectory": "../coverage", 72 | "testEnvironment": "node" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /params-validation/src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /params-validation/src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller() 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get() 9 | getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /params-validation/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { PersonModule } from './person/person.module'; 5 | 6 | @Module({ 7 | imports: [PersonModule], 8 | controllers: [AppController], 9 | providers: [AppService], 10 | }) 11 | export class AppModule {} 12 | -------------------------------------------------------------------------------- /params-validation/src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /params-validation/src/main.ts: -------------------------------------------------------------------------------- 1 | import { ValidationPipe } from '@nestjs/common'; 2 | import { NestFactory } from '@nestjs/core'; 3 | import { AppModule } from './app.module'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | app.useGlobalPipes(new ValidationPipe()); 8 | await app.listen(3000); 9 | } 10 | bootstrap(); 11 | -------------------------------------------------------------------------------- /params-validation/src/person/dto/create-person.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsEmail, IsNotEmpty, IsPhoneNumber, IsString } from "class-validator"; 2 | 3 | export class CreatePersonDto { 4 | @IsNotEmpty({ 5 | message: 'name 不能为空' 6 | }) 7 | @IsString() 8 | name: string; 9 | 10 | @IsPhoneNumber("CN", { 11 | message: 'phone 不是一个电话号码' 12 | }) 13 | phone: string; 14 | 15 | @IsEmail({}, { 16 | message: 'email 不是一个合法邮箱' 17 | }) 18 | email: string; 19 | } 20 | -------------------------------------------------------------------------------- /params-validation/src/person/dto/update-person.dto.ts: -------------------------------------------------------------------------------- 1 | import { PartialType } from '@nestjs/mapped-types'; 2 | import { CreatePersonDto } from './create-person.dto'; 3 | 4 | export class UpdatePersonDto extends PartialType(CreatePersonDto) {} 5 | -------------------------------------------------------------------------------- /params-validation/src/person/entities/person.entity.ts: -------------------------------------------------------------------------------- 1 | export class Person {} 2 | -------------------------------------------------------------------------------- /params-validation/src/person/person.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { PersonController } from './person.controller'; 3 | import { PersonService } from './person.service'; 4 | 5 | describe('PersonController', () => { 6 | let controller: PersonController; 7 | 8 | beforeEach(async () => { 9 | const module: TestingModule = await Test.createTestingModule({ 10 | controllers: [PersonController], 11 | providers: [PersonService], 12 | }).compile(); 13 | 14 | controller = module.get(PersonController); 15 | }); 16 | 17 | it('should be defined', () => { 18 | expect(controller).toBeDefined(); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /params-validation/src/person/person.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; 2 | import { PersonService } from './person.service'; 3 | import { CreatePersonDto } from './dto/create-person.dto'; 4 | import { UpdatePersonDto } from './dto/update-person.dto'; 5 | 6 | @Controller('person') 7 | export class PersonController { 8 | constructor(private readonly personService: PersonService) {} 9 | 10 | @Post() 11 | create(@Body() createPersonDto: CreatePersonDto) { 12 | return this.personService.create(createPersonDto); 13 | } 14 | 15 | @Get() 16 | findAll() { 17 | return this.personService.findAll(); 18 | } 19 | 20 | @Get(':id') 21 | findOne(@Param('id') id: string) { 22 | return this.personService.findOne(+id); 23 | } 24 | 25 | @Patch(':id') 26 | update(@Param('id') id: string, @Body() updatePersonDto: UpdatePersonDto) { 27 | return this.personService.update(+id, updatePersonDto); 28 | } 29 | 30 | @Delete(':id') 31 | remove(@Param('id') id: string) { 32 | return this.personService.remove(+id); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /params-validation/src/person/person.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { PersonService } from './person.service'; 3 | import { PersonController } from './person.controller'; 4 | 5 | @Module({ 6 | controllers: [PersonController], 7 | providers: [PersonService] 8 | }) 9 | export class PersonModule {} 10 | -------------------------------------------------------------------------------- /params-validation/src/person/person.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { PersonService } from './person.service'; 3 | 4 | describe('PersonService', () => { 5 | let service: PersonService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [PersonService], 10 | }).compile(); 11 | 12 | service = module.get(PersonService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /params-validation/src/person/person.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { CreatePersonDto } from './dto/create-person.dto'; 3 | import { UpdatePersonDto } from './dto/update-person.dto'; 4 | 5 | @Injectable() 6 | export class PersonService { 7 | create(createPersonDto: CreatePersonDto) { 8 | return 'This action adds a new person'; 9 | } 10 | 11 | findAll() { 12 | return `This action returns all person`; 13 | } 14 | 15 | findOne(id: number) { 16 | return `This action returns a #${id} person`; 17 | } 18 | 19 | update(id: number, updatePersonDto: UpdatePersonDto) { 20 | return `This action updates a #${id} person`; 21 | } 22 | 23 | remove(id: number) { 24 | return `This action removes a #${id} person`; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /params-validation/src/pipes/MyValidationPipe.ts: -------------------------------------------------------------------------------- 1 | import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common'; 2 | import { validate } from 'class-validator'; 3 | import { plainToClass } from 'class-transformer'; 4 | 5 | @Injectable() 6 | export class MyValidationPipe implements PipeTransform { 7 | async transform(value: any, { metatype }: ArgumentMetadata) { 8 | if (!metatype) { 9 | return value; 10 | } 11 | const object = plainToClass(metatype, value); 12 | const errors = await validate(object); 13 | if (errors.length > 0) { 14 | throw new BadRequestException('Validation failed'); 15 | } 16 | return value; 17 | } 18 | } -------------------------------------------------------------------------------- /params-validation/test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /params-validation/test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /params-validation/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /params-validation/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /status/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /status/.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json -------------------------------------------------------------------------------- /status/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /status/README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | 19 | Support us 20 | 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Installation 30 | 31 | ```bash 32 | $ npm install 33 | ``` 34 | 35 | ## Running the app 36 | 37 | ```bash 38 | # development 39 | $ npm run start 40 | 41 | # watch mode 42 | $ npm run start:dev 43 | 44 | # production mode 45 | $ npm run start:prod 46 | ``` 47 | 48 | ## Test 49 | 50 | ```bash 51 | # unit tests 52 | $ npm run test 53 | 54 | # e2e tests 55 | $ npm run test:e2e 56 | 57 | # test coverage 58 | $ npm run test:cov 59 | ``` 60 | 61 | ## Support 62 | 63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 64 | 65 | ## Stay in touch 66 | 67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 68 | - Website - [https://nestjs.com](https://nestjs.com/) 69 | - Twitter - [@nestframework](https://twitter.com/nestframework) 70 | 71 | ## License 72 | 73 | Nest is [MIT licensed](LICENSE). 74 | -------------------------------------------------------------------------------- /status/nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /status/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "status", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^8.0.0", 25 | "@nestjs/core": "^8.0.0", 26 | "@nestjs/jwt": "^8.0.0", 27 | "@nestjs/passport": "^8.2.1", 28 | "@nestjs/platform-express": "^8.0.0", 29 | "express-session": "^1.17.2", 30 | "passport": "^0.5.2", 31 | "passport-local": "^1.0.0", 32 | "reflect-metadata": "^0.1.13", 33 | "rimraf": "^3.0.2", 34 | "rxjs": "^7.2.0" 35 | }, 36 | "devDependencies": { 37 | "@nestjs/cli": "^8.0.0", 38 | "@nestjs/schematics": "^8.0.0", 39 | "@nestjs/testing": "^8.0.0", 40 | "@types/express": "^4.17.13", 41 | "@types/express-session": "^1.17.4", 42 | "@types/jest": "27.0.2", 43 | "@types/node": "^16.0.0", 44 | "@types/passport-local": "^1.0.34", 45 | "@types/supertest": "^2.0.11", 46 | "@typescript-eslint/eslint-plugin": "^5.0.0", 47 | "@typescript-eslint/parser": "^5.0.0", 48 | "eslint": "^8.0.1", 49 | "eslint-config-prettier": "^8.3.0", 50 | "eslint-plugin-prettier": "^4.0.0", 51 | "jest": "^27.2.5", 52 | "prettier": "^2.3.2", 53 | "source-map-support": "^0.5.20", 54 | "supertest": "^6.1.3", 55 | "ts-jest": "^27.0.3", 56 | "ts-loader": "^9.2.3", 57 | "ts-node": "^10.0.0", 58 | "tsconfig-paths": "^3.10.1", 59 | "typescript": "^4.3.5" 60 | }, 61 | "jest": { 62 | "moduleFileExtensions": [ 63 | "js", 64 | "json", 65 | "ts" 66 | ], 67 | "rootDir": "src", 68 | "testRegex": ".*\\.spec\\.ts$", 69 | "transform": { 70 | "^.+\\.(t|j)s$": "ts-jest" 71 | }, 72 | "collectCoverageFrom": [ 73 | "**/*.(t|j)s" 74 | ], 75 | "coverageDirectory": "../coverage", 76 | "testEnvironment": "node" 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /status/src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Header, Headers, Req, Res, Session } from '@nestjs/common'; 2 | import { JwtService } from '@nestjs/jwt' 3 | import { Response } from 'express'; 4 | 5 | @Controller() 6 | export class AppController { 7 | constructor(private readonly jwtService: JwtService) {} 8 | 9 | @Get('/session') 10 | session(@Session() session): string { 11 | 12 | session.count = session.count ? session.count + 1 : 1; 13 | 14 | return session.count; 15 | } 16 | 17 | @Get('/jwt') 18 | jwt(@Headers('authorization') authorization, @Res() res: Response) { 19 | if (authorization) { 20 | const token = authorization.split(' ')[1]; 21 | const { count } = this.jwtService.verify(token) as any; 22 | 23 | const newToken = this.jwtService.sign({ 24 | count: count + 1 25 | }); 26 | 27 | res.header('authorization', 'barer ' + newToken); 28 | res.end(Buffer.from(count + 1 + '')); 29 | } else { 30 | const newToken = this.jwtService.sign({ 31 | count: 1 32 | }); 33 | 34 | res.header('authorization', 'barer ' + newToken); 35 | res.end(Buffer.from('1')); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /status/src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { JwtModule } from '@nestjs/jwt' 4 | 5 | @Module({ 6 | imports: [JwtModule.register({ 7 | secret: 'guang', 8 | signOptions: { expiresIn: '60s' }, 9 | })], 10 | controllers: [AppController], 11 | providers: [], 12 | }) 13 | export class AppModule {} 14 | -------------------------------------------------------------------------------- /status/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | import * as session from 'express-session'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | app.use( 8 | session({ 9 | secret: 'guang-and-dong' 10 | }), 11 | ); 12 | await app.listen(3000); 13 | } 14 | bootstrap(); 15 | -------------------------------------------------------------------------------- /status/test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /status/test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /status/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /status/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | --------------------------------------------------------------------------------