feat(web): generate and consume API types from OpenAPI - #256
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
从 OpenAPI 契约生成并消费 TypeScript 类型,收紧 Web 网络边界与后端响应契约的一致性。#255 已合并,本 PR 直接消费其定义的响应契约所生成的类型。
Closes #226
实现
openapi-typescript7.13:只生成类型,支持 OpenAPI 3,不引入新的请求 runtime、重试或状态管理。npm run api:generate,直接导入本地 FastAPI app,确定性导出web/openapi.json并生成web/src/services/api/generated/schema.ts。web/openapi.json是生成的中间产物,加入.gitignore不入库(本地运行npm run api:generate即可生成查看);入库的只有schema.ts,两者都禁止手工编辑。npm run api:checkdrift 检查:重新生成契约后对比入库的schema.ts,过期即失败;已接入scripts/web_validate.sh。web/src/services/api/的主要资源模块迁移到生成的 request/response 类型;保留现有 fetch runtime 和缓存行为。Pick、Omit、交叉类型和显式 adapter 隔离传输 DTO 与前端 view model。有意保留的手写类型
以下类型属于前端 UI/domain,而不是后端传输契约:
迁移中显式化的契约边界
extra_data是前端草稿字段,后端 create/update 不接收;adapter 在发送前明确剥离。person_ids不属于当前 TaskCreate/TaskUpdate;adapter 在传输边界明确剥离。本 PR 不改变 API payload、endpoint、缓存 key 或请求 runtime。
CI 修复与验证
Validate Frontend失败:openapi-typescript引入的@redocly/openapi-core依赖存在漏洞的 js-yaml 4.3.0,导致npm audit --audit-level=high失败;通过web/package.jsonoverrides 固定 js-yaml 4.3.1(与 fix(web): upgrade js-yaml to 4.3.1 for CVE-2026-59870 #268 一致)解决。api:check需要uv导出后端 OpenAPI 文档,但前端 CI 任务只安装了 Node;在.github/workflows/validate.yml的 frontend job 中补充 Python、uv 与uv sync --extra web --frozen。web/openapi.json已加入.gitignore,drift 检查只对比入库的schema.ts。35f3806)全部通过:Validate Frontend、Validate Default Toolchain、Validate Runtime Matrix(Python 3.10/3.11/3.12)、Validate CLI Integration (PostgreSQL)。bash ./scripts/web_validate.sh:通过(npm ci、audit、api:checkdrift、i18n、build、lint,86 个测试文件 / 302 个测试)。