oRPC 项目使用教程
orpc Typesafe API's Made Simple 🪄 项目地址: https://gitcode.com/gh_mirrors/or/orpc
1. 项目目录结构及介绍
oRPC 是一个基于 TypeScript 的 RPC 框架,它致力于提供端到端的类型安全,并且遵循 OpenAPI 标准。以下是项目的目录结构及其介绍:
orpc/
├── .github/ # GitHub 工作流和模板文件
├── .vscode/ # VSCode 项目配置文件
├── apps/ # 应用程序代码
├── content/ # 静态内容或文档
├── packages/ # 相关的 npm 包
├── playgrounds/ # 演示或示例代码
├── .gitignore # Git 忽略文件列表
├── .npmrc # npm 配置文件
├── LICENSE # 项目许可证信息
├── README.md # 项目说明文件
├── eslint.config.js # ESLint 配置文件
├── knip.json # 项目配置文件
├── package.json # npm 包配置文件
├── pnpm-lock.yaml # pnpm 锁文件
├── pnpm-workspace.yaml # pnpm 工作区配置文件
├── tsconfig.base.json # TypeScript 基础配置文件
├── tsconfig.json # TypeScript 配置文件
├── tsconfig.lib.json # TypeScript 库配置文件
├── vitest.jsdom.ts # Vitest 测试配置文件
└── vitest.workspace.ts # Vitest 工作区配置文件
.github/
:包含 GitHub Actions 工作流和相关模板。.vscode/
:包含 Visual Studio Code 的项目配置。apps/
:存放项目中的应用程序代码。content/
:可能包含项目的静态内容或者文档。packages/
:存放项目的 npm 包。playgrounds/
:包含一些演示或示例代码。.gitignore
:定义了 Git 应该忽略的文件和目录。.npmrc
:包含了 npm 的配置选项。LICENSE
:项目使用的许可证。README.md
:项目的说明文件,包含了项目的介绍、安装和使用说明。eslint.config.js
:ESLint 的配置文件,用于代码风格检查。knip.json
:项目的配置文件。package.json
:项目的 npm 配置文件,定义了项目的依赖、脚本等。pnpm-lock.yaml
和pnpm-workspace.yaml
:pnpm 的锁文件和工作区配置。tsconfig.json
及相关文件:TypeScript 的配置文件。
2. 项目的启动文件介绍
项目的启动文件通常是 index.ts
或 main.ts
。以下是启动文件的示例结构:
// index.ts
import { createServer } from 'node:http';
import { RPCHandler } from '@orpc/server/node';
import { CORSPlugin } from '@orpc/server/plugins';
// 引入路由器
import { router } from './router';
const handler = new RPCHandler(router, {
plugins: [new CORSPlugin()]
});
const server = createServer(async (req, res) => {
const result = await handler.handle(req, res, {
context: { headers: req.headers }
});
if (!result.matched) {
res.statusCode = 404;
res.end('No procedure matched');
}
});
// 启动服务
server.listen(3000, '127.0.0.1', () => {
console.log('Listening on 127.0.0.1:3000');
});
这个文件负责创建 HTTP 服务并使用 oRPC 处理器来处理请求。
3. 项目的配置文件介绍
项目的配置文件可能包括 package.json
、tsconfig.json
和 .eslintrc.js
等。以下是这些配置文件的简要介绍:
package.json
:这个文件定义了项目的名称、版本、描述、依赖项、脚本和更多。例如,以下是scripts
部分的示例:
"scripts": {
"start": "node index.ts",
"build": "tsc",
"test": "vitest"
}
tsconfig.json
:这个文件用于配置 TypeScript 编译器的选项。以下是一些常用选项:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
}
}
.eslintrc.js
:这个文件用于配置 ESLint 的规则,确保代码风格的一致性。以下是一个配置示例:
module.exports = {
"extends": ["eslint:recommended"],
"rules": {
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double"],
"semi": ["error", "always"],
"no-unused-vars": ["warn"]
}
};
这些配置文件帮助维护项目的结构和代码质量,确保项目可以在不同的环境中正确运行。
orpc Typesafe API's Made Simple 🪄 项目地址: https://gitcode.com/gh_mirrors/or/orpc
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考