Val Town Blog 10月02日 20:53
内部外部API的权衡与解决方案
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文探讨了在Val Town平台开发中,如何在快速迭代新功能与提供稳定、易用的外部API之间取得平衡。文章指出,Remix和tRPC在加速内部功能开发方面表现出色,但并不适合作为公开API。而通过Fastify构建RESTful API并生成SDK,虽然能支持外部开发者,却会显著减缓开发流程。作者分析了现有解决方案的局限性,如OpenAPI对Server-Sent Events的支持不足以及tRPC与OpenAPI集成的问题,并提出将尝试在本地快速构建SDK以加速前端开发,同时探索oRPC和Hono等新方案。

💡 **开发速度与API可用性的双重挑战**: Val Town平台在快速开发新功能(通过Remix和tRPC)与提供稳定的外部API(通过Fastify和OpenAPI)之间面临权衡。两种需求在技术选型上存在矛盾,难以同时满足。

🚀 **内部开发效率工具的局限性**: Remix和tRPC提供了卓越的开发体验,能够快速迭代功能。然而,Remix loaders不适合作为公开API,tRPC的通信格式是实现细节,不适合构建需要明确接口定义的外部API。

🛠️ **外部API构建的流程摩擦**: 为每个功能构建Fastify RESTful API并生成SDK,会增加API设计与前端实现之间的巨大时间成本和流程冗余,显著拖慢开发进度。

⚠️ **现有API规范与工具的不足**: OpenAPI规范未能完全支持Server-Sent Events等现代API特性。tRPC与OpenAPI的集成支持有限且不稳定,Hono等综合性方案也存在重大bug,oRPC和stl-api尚处于早期阶段。

🧭 **探索中的解决方案**: Val Town计划通过在本地快速构建SDK来加速前端开发,并积极探索oRPC和Hono等新兴技术,以期找到一个能够兼顾快速迭代和强大API支持的更优解决方案。

Solving the internal / external API riddle

on

I’ve been thinking about that APIs and applications question again for Val Town. We’re trying to resolve two priorities: rapidly developing features, and providing an API for our users. As it turns out, some of our tools are well-suited for rapid development, and others shine when creating externally-consumable REST APIs, but we haven’t found a way to do both yet.

For application development, we want to move as fast as possible. A lot of pull requests include both a feature in our Remix-based frontend and some backend functionality, either in Remix loaders, a tRPC route, or, less often, a new route in the Fastify server that powers our public API.

Specifically, the workflow of using tRPC and Remix is pretty spectacular: their homepage example tells most of the story. You write a method on the backend and the frontend tRPC client is immediately able to call it with the correct TypeScript types for both inputs & outputs. It all works without code generation, and minimal boilerplate. The developer experience is extraordinary.

On the other hand, we’re also trying to give our users the ability to build on the Val Town platform in all kinds of new ways. When we introduce something like Projects, we want users to be able to programmatically create projects and files within them. Our SDK should let them do that on day one. But right now, it doesn’t: we built the application interface for projects using tRPC and Remix, and then implemented the same with Fastify so that we have an OpenAPI spec that includes it, and then we can generate our SDK with that OpenAPI spec.

Could we both iterate quickly on the application layer and provide a lot of functionality programmatically to users? Possibly! But there are a lot of dragons here.

    Remix loaders are definitely not a public API. Since the introduction of Single Fetch, loaders don’t even output JSON, but they use turbo-stream. Remix loaders definitely aren’t the REST model.tRPC is probably a bad way to implement OpenAPI. tRPC does technically kinda support OpenAPI via trpc-to-openapi, but the first-party support is archived and the fork doesn’t have much uptake, so this is really a fringe ability that I wouldn’t count on. We’ve experimented with allowing people to make programmatic requests against our tRPC routes, but this always ended up in disappointment. The actual requests and formats that tRPC uses for communication are an implementation detail. tRPC is not a REST provider.Building Fastify routes and generating SDKs for every feature, before we can even start on frontend development would introduce a tremendous amount of friction into our development process and create a waterflow between API design and frontend implementation. Imagine having to write a REST API method, merge and deploy it, generate a new SDK and release it as an NPM module, just to install that new SDK version into the application and start building a frontend feature.The OpenAPI specification does not support Server-Sent Events. Thankfully, Stainless, which helps us produce our SDKs, has its own support, but it’s disappointing that such a central specification for designing APIs won’t actually be able to describe our current API surface in full.Batching is good and we’ll lose it. tRPC supports batching requests, and Remix’s Single Fetch batches requests for nested loaders. This is a real benefit for frontend applications like Val Town: we tend to make a lot of HTTP requests, and request overhead is a real problem. Generic request batching is not something you can do with OpenAPI or any of these generated clients.

Here’s a quick table survey of the solution space that I’m aware of:

As you can see, there are a few systems that try to provide first-class support for both REST-based APIs that we can generate an SDK around, and RPC-like clients with which we can iterate quickly. Unfortunately, Hono, the most established option and one I’m pretty optimistic about - which supports both OpenAPI and RPC - has a fairly severe bug for using the two together. oRPC is extremely promising, but a new and fairly single-handed effort. stl-api is interesting, but in an alpha stage of development with no releases yet and very little development happening.

We’re going to experiment with a strategy to build our SDKs quickly and locally so that we can develop frontend features against the Fastify server. In parallel, I’ll probably kick the tires on oRPC and Hono as alternative approaches to this problem.

Decisions like this can cause a lot of churn, which hurts development momentum and makes it harder for everyone to understand the codebase, so it’s a decision we’re making pretty methodically and incrementally.

Have you seen good solutions to this problem? Let us know!

Edit this page

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

API Val Town Remix tRPC Fastify OpenAPI SDK 开发效率 技术权衡 oRPC Hono
相关文章