Files
antd-admin/apps/docs/content/zh/backend/proxying-api-requests.mdx
4dc473ba55 refactor(api): modular defineApi layer with shared API_PREFIX (#1181)
* refactor(api): introduce defineApi + axios request layer

Replace ad-hoc fetch httpClient with api/request.ts (axios interceptors,
envelope unwrap, auth refresh) and defineApi-based resource modules.
Move session hydration into api/auth, sync both templates, and update docs.

Co-authored-by: zuiidea <zuiiidea@gmail.com>

* chore: unify Cursor rules, fix monorepo install hooks, refresh basic docs

Add root preinstall to set githooks and disable parallel vp config git lock races; merge per-app Cursor thin rules into app.mdx; keep template READMEs free of monorepo-only paths and describe basic as default English rather than English-only.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: add root turbo fmt and format app sources

Expose fmt via turbo at the monorepo root, apply Oxfmt in basic/with-lingui, and run vp staged through pnpm exec in pre-commit so hooks work without a global vp.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: drop root eslint/prettier and bump turbo to 2.11.9

Root formatting and lint stay on turbo fmt/lint via Vite+ in workspace apps.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: consolidate agent guidance into AGENTS.md

Merge scoped instructions into per-app AGENTS.md, add monorepo AGENTS.md and add-resource skill, and remove template .github Copilot files plus the Cursor app router rule.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore(basic): add add-resource skill and portable app-root paths

Align basic with with-lingui for CRUD scaffolding via AGENTS.md and .agents/skills, and drop hardcoded monorepo paths in agent docs.

Co-authored-by: Cursor <cursoragent@cursor.com>

* refactor(api): split modules under shared/ with API_PREFIX

Move auth and users into folder modules, centralize path constants, and align MSW with endpoint paths in both template apps.

Co-authored-by: Cursor <cursoragent@cursor.com>

* docs: document modular API layer in README files

Expand template READMEs with src/api layout, fix CLI table and E2E script references.

Co-authored-by: Cursor <cursoragent@cursor.com>

* docs: align site with modular src/api layout

Document API_PREFIX, folder structure, templates, and E2E script usage in en/zh.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: zuiidea <zuiidea@example.com>
2026-10-22 23:13:22 +08:00

37 lines
2.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 开发代理与 Mock
description: MSW、API 基地址与对接真实后端
---
# 开发代理与 Mock
## MSW(开发默认)
模板在开发环境使用 **MSW** 拦截 HTTP,**handlers** 位于 `src/mocks/handlers`。无后端即可演示登录、菜单、用户 CRUD 等。
**Worker 目录**由 `package.json` 的 **`msw.workerDirectory`** 配置为 `public`,与 [public 目录](/styles-and-assets/public-folder) 说明一致。
## API 层(模板约定)
数据流:**Zod schema → `src/api/<module>/index.ts` 中的 `defineApi` → 路由/组件只调用资源函数**(如 `listUsers`、`login`)。页面不要 import axios 或 `@/api/request`。
```text
src/api/
├── request.ts # axios、响应 envelope、token 刷新
├── schemas.ts # 兼容 re-export;新代码用 @/api/shared 与各 module schemas
├── shared/ # API_PREFIX、分页/查询/菜单等共用 schema
├── auth/ # paths、schemas、登录与 hydrateSession
└── users/ # schemas、CRUD、userQueryKeys、路径常量
```
- REST 前缀:**`API_PREFIX`**(`src/api/shared/prefix.ts`,默认 `/api`)。`AUTH_ENDPOINTS`、`USERS_PATH` 与 **MSW handlers** 共用同一套常量。
- 新增资源:对照 **`users`**,见各应用 **`.agents/skills/add-resource/SKILL.md`** 与 **`AGENTS.md`**。
## 对接真实 API
模板传输层为 **`src/api/request.ts`(axios)**:`baseURL` 来自环境变量 **`VITE_API_BASE_URL`**(见 `src/utils/constants.ts`)。业务调用请走 **`src/api/<module>/index.ts`** 导出的 `defineApi` 函数,不要在页面里直连 axios。
将 `VITE_API_BASE_URL` 指向真实网关,并按环境关闭或绕过 MSW 初始化(具体开关以模板 `main.tsx` / 环境变量为准)。
跨域时需由**浏览器可访问**的网关配置 **CORS**;或通过开发代理将 **`API_PREFIX`(默认 `/api`)** 转发到后端(在 `vite.config.ts` 配置 `server.proxy`,按团队规范填写目标地址)。