Skip to content

feat(frontend): add shared API client infrastructure - #117

Merged
nighca merged 11 commits into
1024XEngineer:mainfrom
huyanxius:feat/shared-api-client
Aug 5, 2026
Merged

feat(frontend): add shared API client infrastructure#117
nighca merged 11 commits into
1024XEngineer:mainfrom
huyanxius:feat/shared-api-client

Conversation

@huyanxius

@huyanxius huyanxius commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

功能说明

  • 增加不含业务语义的共享 HTTP 客户端,为后续各 XxxApis 实现提供统一请求入口。
  • 对齐后端公共响应、分页、业务码与 Bearer Token 契约。
  • 提供惰性的 Token Provider 注册边界,供后续登录模块注入读取函数。
  • 保持 Projects、entities、pages、Mock 和 UI 不变。

实现方式

  • VITE_API_BASE_URL 读取服务地址,并在请求发出时读取当前 Token。
  • 统一处理 JSON、查询参数、Response<T>ListResponse<T> 与四类 API 错误。
  • 通过 registerApiAccessTokenProvider 注册读取函数,公共层不保存、刷新或解析 Token。
  • 将 HTTP 列表结果留在 shared/api 自身边界内,避免违反主线同层模块不互相依赖的规则。

测试

  • npm run format:check:通过,46 个文件。
  • npm run lint:通过。
  • npm run typecheck:通过。
  • npm run test:通过,4 个测试文件、17 项测试。
  • npm run build:通过,89 个模块完成构建。
  • git diff --check:通过。

范围边界

  • 不包含 Project 或 Character 接口接入。
  • 不修改页面、路由、UI、Mock 或 livedemo。
  • 不负责登录、Token 保存、自动刷新或业务 DTO 映射。
  • 不增加运行时依赖。

关联

Frontend entity APIs need a shared transport boundary aligned with backend response contracts.

Add environment-based URL resolution, Bearer token injection, envelope decoding, pagination mapping, and normalized errors.

Future entity implementations can reuse one business-agnostic client without page-level configuration.
The shared client needs executable evidence for the response and authentication rules declared by the backend.

Cover success, business errors, pagination, request serialization, Bearer headers, invalid envelopes, HTTP failures, and network failures.

Contract regressions now fail before entity API implementations depend on the transport layer.
The architecture docs need to distinguish the new transport layer from business-specific API implementations.

Describe shared API ownership, environment configuration, token consumption, response handling, and explicit exclusions.

Future entity work can reuse the client without expanding shared-layer responsibilities.
@vercel

vercel Bot commented Aug 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
windup Ready Ready Preview Aug 5, 2026 3:48am

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new client looks sound overall, but I found one concrete transport-classification bug.

Comment thread frontend/src/shared/api/index.ts Outdated
@huyanxius
huyanxius requested a review from xyh202131 August 4, 2026 18:42
Non-2xx responses could be misclassified as business errors when their envelopes also used non-200 codes.

Check the HTTP status before evaluating the backend business code.

Transport failures now retain HTTP semantics while HTTP 200 business failures remain unchanged.
The transport classifier needs a regression case where both HTTP status and backend business code indicate failure.

Add a response fixture with HTTP 503 and business code 500.

The test prevents non-2xx responses from regressing to business-error classification.
Business API clients need a shared lazy token boundary.

Register and restore token reader functions without storing token values.

Project and character adapters can consume authentication supplied later.
The shared token boundary needs deterministic registration behavior.

Cover latest-provider selection and restoration after unregistering.

Future login integration can rely on the provider lifecycle.
Shared API documentation needs to describe the consumed authentication edge.

Explain provider registration while keeping token ownership outside shared code.

Later login work can integrate without redefining the transport layer.
The shared API boundary now exposes a provider registry for future login work.

Document the lazy token registration path in the architecture and frontend guide.

Business API modules can depend on one stable injection boundary without owning auth state.
Projects documentation must merge independently after the shared API pull request.

Move transport guidance away from project status and workspace routing text.

The two pull requests can update their own documentation without overlapping hunks.
@johnnyzhang-eng

Copy link
Copy Markdown

集成验证反馈:信封形状这条踩对了,另附一个争用点

main + 本 PR + #119 + #111 + #110 合成一个本地集成分支做了端到端验证(构建通过、单测 63/63、连真实后端跑通只读链路)。两条反馈:

1. requestList 对信封形状的假设是对的,建议写进注释钉住

后端把 total / page / page_size 放在信封顶层、与 data 同级data 直接就是数组:

{ "code": 0, "message": "...", "data": [ ... ], "total": 5, "page": 1, "page_size": 12 }

本 PR 的三个 Number.isInteger 断言正好按这个形状写,实测全过。

之所以专门提一句:这里有个很容易写反的坑。在另一处代码里按 raw.data.map(...) 解析过(即假设 data 里还嵌一层分页对象),表现是页面报「角色列表读取失败」,看起来像后端挂了,实际是解析层的问题,排查方向会被带偏。修法就是按数组解析。

建议:把这个形状以注释形式钉在 requestList 上,并让 entities 层统一走它,不要各自 raw.data 解析——否则同一个坑会在每个 entity 里各踩一次。

2. frontend/src/entities/index.ts 是当前前端 PR 群唯一的争用点

试合验证时发现:#95 / #86 / #107 相互之间的冲突全部只冲这一个 barrel 文件,其余文件零冲突。也就是说合并成本几乎完全集中在这里。

建议在本 PR(基础设施层)里就把这个 barrel 的归并规则定下来(例如按 entity 分文件导出、index 只做再导出且按字母序),后续几个 PR 的合并成本会明显下降。

Review feedback identified the pagination envelope as an integration hotspot for future entity APIs.

Document that list data is an array and pagination fields remain at the envelope top level.

Future API implementations now have an explicit parsing boundary without changing runtime behavior.
@huyanxius

Copy link
Copy Markdown
Collaborator Author

感谢集成验证,这两点都确认了。

  1. requestList 当前按后端真实契约处理:data 是数组,total / page / page_size 位于响应信封顶层。已在 d888e9a 中补充注释,明确这一固定形状,避免后续实体层重复按 raw.data 解析。

  2. frontend/src/entities/index.ts 确实是多个实体 PR 的共同冲突点。但它属于实体导出归并问题,不属于本 PR 的共享 API 传输层范围。当前 PR 不修改该文件,以避免与 feat(quick-start): add guided creation and animation review #95feat(workflow-run): implement resumable generation orchestration #86feat(workflow-controller): add resumable workflow coordination #107 产生额外冲突;后续按统一的 barrel 导出规则集中处理。

@nighca
nighca merged commit db597eb into 1024XEngineer:main Aug 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: 增加前端通用 API 客户端基础设施

3 participants