End-to-end typesafe APIs made easy
介绍
¥Introduction
tRPC 允许你轻松构建和使用完全类型安全的 API,而无需模式或代码生成。
¥tRPC allows you to easily build & consume fully typesafe APIs without schemas or code generation.
随着 TypeScript 和静态类型日益成为 Web 开发中的最佳实践,API 约定成为了一个主要痛点。我们需要更好的方法来静态键入 API 端点并在客户端和服务器(或服务器到服务器)之间共享这些类型。我们着手构建一个简单的库,用于构建类型安全的 API,充分利用现代 TypeScript 的功能。
¥As TypeScript and static typing increasingly becomes a best practice in web development, API contracts present a major pain point. We need better ways to statically type our API endpoints and share those types between our client and server (or server-to-server). We set out to build a simple library for building typesafe APIs that leverages the full power of modern TypeScript.
传统 REST 或 GraphQL 的替代方案
¥An alternative to traditional REST or GraphQL
目前,GraphQL 是在 TypeScript (太棒了!) 中实现类型安全 API 的主要方式。由于 GraphQL 被设计为用于实现 API 的与语言无关的规范,因此它没有充分利用 TypeScript 这样的语言的强大功能。
¥Currently, GraphQL is the dominant way to implement typesafe APIs in TypeScript (and it's amazing!). Since GraphQL is designed as a language-agnostic specification for implementing APIs, it doesn't take full advantage of the power of a language like TypeScript.
如果你的项目是使用全栈 TypeScript 构建的,你可以直接在客户端和服务器之间共享类型,而无需依赖代码生成。
¥If your project is built with full-stack TypeScript, you can share types directly between your client and server, without relying on code generation.
tRPC 适合谁?
¥Who is tRPC for?
tRPC 适用于全栈 TypeScript 开发者。它使你可以轻松编写可以在应用的前端和后端安全使用的端点。API 约定的类型错误将在构建时被捕获,从而减少应用在运行时出现错误的可能性。
¥tRPC is for full-stack TypeScript developers. It makes it easy to write endpoints that you can safely use in both the front and backend of your app. Type errors with your API contracts will be caught at build time, reducing the surface for bugs in your application at runtime.
特性
¥Features
✅ 经过充分测试并准备生产。
¥✅ Well-tested and production ready.
🧙♂️ 客户端上针对输入、输出和错误的完全静态类型安全和自动补齐。
¥🧙♂️ Full static typesafety & autocompletion on the client, for inputs, outputs, and errors.
🐎 敏捷 DX - 没有代码生成、运行时膨胀或构建管道。
¥🐎 Snappy DX - No code generation, run-time bloat, or build pipeline.
🍃 光 - tRPC 具有零依赖和极小的客户端占用空间。
¥🍃 Light - tRPC has zero deps and a tiny client-side footprint.
🐻 对于新旧项目 - 易于启动或添加到你现有的棕地项目。
¥🐻 For new and old projects - Easy to start with or add to your existing brownfield project.
🔋 框架无关 - tRPC 社区已经为所有最流行的框架构建了 适配器。
¥🔋 Framework agnostic - The tRPC community has built adapters for all of the most popular frameworks.
🥃 订阅支持 - 向你的应用添加类型安全的可观察性。
¥🥃 Subscriptions support - Add typesafe observability to your application.
⚡ 请求批处理 - 同时提出的请求可以自动合并为一个。
¥⚡️ Request batching - Requests made at the same time can be automatically combined into one.
👀 示例 - 查看 示例 来学习或用作起点。
¥👀 Examples - Check out an example to learn with or use as a starting point.