<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Contract_First | 锤子前端</title><description>Now vibe coding, so learning hammer FE ?</description><link>https://channel.meo-ai.com</link><item><title>《人生苦短，何必手写 API 类型：OpenAPI 驱动的 React 开发》</title><link>https://channel.meo-ai.com/posts/569</link><guid isPermaLink="true">https://channel.meo-ai.com/posts/569</guid><pubDate>Mon, 02 Mar 2026 02:28:02 GMT</pubDate><content:encoded>&lt;span class=&quot;post-title-source&quot;&gt;《人生苦短，何必手写 API 类型：OpenAPI 驱动的 React 开发》&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;标签：&lt;a href=&quot;/search/result?q=%23%E5%89%8D%E7%AB%AF&quot; title=&quot;#前端&quot;&gt;#前端&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23React&quot; title=&quot;#React&quot;&gt;#React&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23OpenAPI&quot; title=&quot;#OpenAPI&quot;&gt;#OpenAPI&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23TypeScript&quot; title=&quot;#TypeScript&quot;&gt;#TypeScript&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23Hey_API&quot; title=&quot;#Hey_API&quot;&gt;#Hey_API&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23MSW&quot; title=&quot;#MSW&quot;&gt;#MSW&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23Nanoquery&quot; title=&quot;#Nanoquery&quot;&gt;#Nanoquery&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23Contract_First&quot; title=&quot;#Contract_First&quot;&gt;#Contract_First&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;总结&lt;br /&gt;&lt;br /&gt;本文是 Evil Martians 团队&quot;契约优先&quot;系列第三篇，介绍如何通过 OpenAPI 规范驱动 React 前端开发。核心思路是将 OpenAPI Spec 作为单一事实来源，利用 Hey API 自动生成 TypeScript 类型、SDK 客户端和 Zod 验证模式，配合 Nanostores 与 Nanoquery 实现类型安全的状态管理，并使用 MSW 在浏览器层面拦截网络请求进行模拟，实现前后端并行开发。当后端契约变更时，重新生成代码即可让 TypeScript 立即暴露所有需要更新的地方，避免生产环境出现类型不匹配错误。&lt;br /&gt;&lt;br /&gt;文章要点：&lt;br /&gt;&lt;br /&gt;- **手写 API 类型的痛点**：类型与后端脱节、缺乏运行时验证、重复样板代码、错误只能在生产环境发现&lt;br /&gt;- **Hey API 自动生成**：通过 &lt;code&gt;openapi-ts&lt;/code&gt; 从 OpenAPI Spec 生成 TypeScript 类型、SDK 函数、Zod 验证模式和客户端配置，实现编译时与运行时双重安全&lt;br /&gt;- **Nanostores + Nanoquery 状态管理**：将 API 数据视为全局状态存储在组件外，支持缓存、自动重取、跨组件共享，避免传统 Hooks 的数据获取陷阱&lt;br /&gt;- **MSW 网络层模拟**：在浏览器层面拦截真实 HTTP 请求返回模拟数据，支持随机延迟、分页、过滤、错误场景，实现不依赖后端的功能开发&lt;br /&gt;- **完整实战示例**：包含酒店管理系统代码，展示表单验证、CRUD 操作、路由集成、环境配置切换等完整开发流程&lt;br /&gt;- **契约优先工作流**：后端更新 Spec → 前端重新生成 → TypeScript 报错定位 → 修复代码，确保集成一次成功&lt;br /&gt;&lt;br /&gt;文章URL：&lt;a href=&quot;https://evilmartians.com/chronicles/lifes-too-short-to-hand-write-api-types-openapi-driven-react&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;https://evilmartians.com/chronicles/lifes-too-short-to-hand-write-api-types-openapi-driven-react&quot;&gt;https://evilmartians.com/chronicles/lifes-too-short-to-hand-write-api-types-openapi-driven-react&lt;/a&gt;&lt;a class=&quot;tgme_widget_message_link_preview&quot; href=&quot;https://evilmartians.com/chronicles/lifes-too-short-to-hand-write-api-types-openapi-driven-react&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;Transform your React development with contract-first APIs. Generate TypeScript types, build type-safe clients, and develop with mocks while backend implements—no more waiting, no more integration chaos.&quot;&gt;
  
  &lt;div class=&quot;link_preview_site_name accent_color&quot;&gt;evilmartians.com&lt;/div&gt;
  &lt;img class=&quot;link_preview_image&quot; alt=&quot;Life&apos;s too short to hand-write API types: OpenAPI-driven React—Martian Chronicles, Evil Martians’ team blog&quot; src=&quot;/static/https://cdn4.telesco.pe/file/fFx49yUeDXr1tceP7qxaoV31dlNscRSoE9WwXNp7qwXZl82yRqOwikqCKNaCAEUrGiJYTGcQWP1VsMIEkEPnU5MDpv0c0FfuaUrRkWLrjvFkAfz7yEfmNRDqyhv2z-3SNmDCam-EQ5EpRlFVOBcwCOhPcRC5aKOm8VeNuTrtoASQ_ou8Txo40rzs66MZyEcFSUPJ42uE7D_8_IX6FWsit7zGNzwUQxY1OQELkejPJC1Narwd_XR3p8FROvGDSbkhCpaTFOUDTgojjwfyKJi1XqQK9dXbljZjSMXI9oMECh5Zj6M5xLrzfX9FvDrFwdmOmgUXuXGAx6yX6mOHUPNDzg.jpg&quot; width=&quot;1200&quot; height=&quot;630&quot; loading=&quot;eager&quot; /&gt;
  &lt;div class=&quot;link_preview_title&quot;&gt;Life&apos;s too short to hand-write API types: OpenAPI-driven React—Martian Chronicles, Evil Martians’ team blog&lt;/div&gt;
  &lt;div class=&quot;link_preview_description&quot;&gt;Transform your React development with &lt;mark class=&quot;highlight&quot;&gt;contract&lt;/mark&gt;-&lt;mark class=&quot;highlight&quot;&gt;first&lt;/mark&gt; APIs. Generate TypeScript types, build type-safe clients, and develop with mocks while backend implements—no more waiting, no more integration chaos.&lt;/div&gt;
&lt;/a&gt;</content:encoded></item></channel></rss>