TypeScript Clean Architecture 项目教程
1. 项目的目录结构及介绍
typescript-clean-architecture/
├── src/
│ ├── adapters/
│ ├── app/
│ ├── domain/
│ ├── infrastructure/
│ ├── presentation/
│ ├── use-cases/
│ └── index.ts
├── test/
├── .eslintrc
├── .gitignore
├── LICENSE
├── README.md
├── docker-compose.local.yaml
├── docker-compose.test.yaml
├── jest.json
├── ormconfig.json
├── package-lock.json
├── package.json
├── sonar-project.properties
├── tsconfig.build.json
└── tsconfig.json
目录结构介绍
-
src/: 项目的主要源代码目录。
- adapters/: 包含适配器代码,用于将基础设施层的接口适配为用例层熟悉的接口。
- app/: 包含应用程序的业务逻辑。
- domain/: 包含核心业务逻辑和领域模型。
- infrastructure/: 包含基础设施代码,如数据库访问、外部服务调用等。
- presentation/: 包含用户界面相关的代码,如API控制器、视图等。
- use-cases/: 包含应用程序的业务用例。
- index.ts: 项目的入口文件。
-
test/: 包含测试代码。
-
.eslintrc: ESLint 配置文件。
-
.gitignore: Git 忽略文件配置。
-
LICENSE: 项目许可证。
-
README.md: 项目说明文档。
-
docker-compose.local.yaml: 本地开发环境的 Docker 配置文件。
-
docker-compose.test.yaml: 测试环境的 Docker 配置文件。
-
jest.json: Jest 测试框架配置文件。
-
ormconfig.json: 对象关系映射配置文件。
-
package-lock.json: npm 依赖锁定文件。
-
package.json: 项目依赖和脚本配置文件。
-
sonar-project.properties: SonarQube 代码质量分析配置文件。
-
tsconfig.build.json: TypeScript 编译配置文件(用于构建)。
-
tsconfig.json: TypeScript 编译配置文件。
2. 项目的启动文件介绍
项目的启动文件是 src/index.ts
。这个文件是应用程序的入口点,负责初始化应用程序并启动服务器。
// src/index.ts
import { App } from './app';
const app = new App();
app.start();
3. 项目的配置文件介绍
package.json
package.json
文件包含了项目的依赖、脚本和其他元数据。
{
"name": "typescript-clean-architecture",
"version": "1.0.0",
"scripts": {
"start": "ts-node src/index.ts",
"build": "tsc -p tsconfig.build.json",
"test": "jest"
},
"dependencies": {
"express": "^4.17.1",
"typescript": "^4.1.3"
},
"devDependencies": {
"@types/express": "^4.17.9",
"ts-node": "^9.1.1",
"jest": "^26.6.3"
}
}
tsconfig.json
tsconfig.json
文件是 TypeScript 的编译配置文件,定义了 TypeScript 编译器的行为。
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
.eslintrc
.eslintrc
文件是 ESLint 的配置文件,用于代码风格检查和错误检测。
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["plugin:@typescript-eslint/recommended