diff --git a/AICONTEXT.md b/AICONTEXT.md index 660bcec..01511a6 100644 --- a/AICONTEXT.md +++ b/AICONTEXT.md @@ -13,9 +13,8 @@ simapi-vue/ │ ├── simapi.core.ts # 核心类 SimApiCore,零框架依赖 │ └── simapi.pinia.ts # Pinia Store,Vue3 适配层 ├── dist/ # 构建产物 -├── package.json # exports: "/" → core, "/pinia" → vue -├── vite.core.config.ts # 构建 core → dist/index.mjs/cjs -├── vite.pinia.config.ts # 构建 pinia → dist/pinia.mjs +├── package.json # exports: "." → core, "/pinia" → vue +├── vite.config.ts # 统一构建配置,生成 .mjs 和 .cjs └── tsconfig.build.json # tsc 生成类型声明 ``` @@ -43,10 +42,9 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co - **无 Cookie**:所有请求使用 `credentials: 'omit'`,避免 CORS 问题 - **Token 传递**:通过请求头 `Token` 传递认证信息 - **超时控制**:通过 `fetchWithTimeout` 辅助函数实现 -- `setEndpoints()` 会自动调用 `fetchVersions()` 查询后端版本(`/versions`),打印到控制台 -- `handleResponse()` 在响应非 200 时触发 `businessCallback`,不抛出异常 - `query()` 抛出异常的只有网络/HTTP 错误,业务错误码通过回调处理 - `isLoggedIn` 是 getter,基于 localStorage 中的 token 判断 +- **版本号**:`getVersion()` 返回的版本信息中,`uiSimApi` 由构建时注入的 `SimApiVersion` 填充,`uiApp` 由调用方的 `AppVersion` 填充(未注入时为 `0.0.0-develop`) **修改建议**: @@ -54,11 +52,11 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co - 改 Token 存储:替换 `localStorage` 为 `sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()` - 改登录/登出逻辑:修改 `login()`/`logout()` 方法 - 改超时处理:修改 `fetchWithTimeout()` 函数 -- **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-dev` +- **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-develop` **autoInit 设计约束**: -- 仅读取 `window.simapi` 的顶级字段:`endpoints`、`defaultEndpoint`、`debug`、`uiAppVersion` +- 仅读取 `window.simapi` 的顶级字段:`endpoints`、`defaultEndpoint`、`debug` - 业务回调(`businessCallback`/`responseCallback`)不支持从 window 读取,必须在代码中通过 `setBusinessCallback` 注册 --- @@ -69,7 +67,7 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co **关键设计**: -- **无独立状态**:state 中只有一个 `_core` 实例,不维护 `debug`、`versions`、`token` 等独立数据 +- **无独立状态**:state 中只有一个 `_core` 实例,不维护 `debug`、`token` 等独立数据 - **单例 Core**:在 store state 中实例化 `SimApiCore`,整个应用共享一个实例 - **纯代理映射**:所有 getters 直接映射到 `this._core` 的属性,所有 actions 直接调用 `this._core` 的方法 - **响应式**:通过 Pinia 的响应式系统,当 core 状态变化时自动更新 @@ -92,7 +90,7 @@ api.configure({ }) // 所有方法与 SimApiCore 完全一致 -await api.query('/users/list', { page:1 }) +await api.query('/users/list', { page: 1 }) ``` --- @@ -101,39 +99,33 @@ await api.query('/users/list', { page:1 }) ``` npm run build - → vite build vite.core.config.ts 输出 dist/index.mjs / index.cjs - → vite build vite.pinia.config.ts 输出 dist/pinia.mjs - → tsc -p tsconfig.build.json 输出 *.d.ts 类型声明 + → vite build 输出 dist/index.mjs / index.cjs / pinia.mjs / pinia.cjs + → tsc -p tsconfig.build.json 输出 *.d.ts 类型声明 ``` **版本号注入:** -版本号通过 `vite.core.config.ts` 的 `define` 配置注入,从 npm config 读取: +版本号通过 `vite.config.ts` 的 `define` 配置注入,从 npm config 读取: ```typescript define: { - 'AppVersion': JSON.stringify(process.env.npm_config_AppVersion || '0.0.0-develop'), 'SimApiVersion': JSON.stringify(process.env.npm_config_SimApiVersion || '0.0.0-develop'), } ``` **本地构建示例:** ```bash -# 通过 npm config 传递(推荐) -npm run build -- --AppVersion=1.0.0 --SimApiVersion=1.0.0 - -# 或通过环境变量 +# 通过环境变量 # Windows PowerShell -$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build +$env:npm_config_SimApiVersion='1.0.0'; npm run build # Linux/Mac -AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build +npm_config_SimApiVersion=1.0.0 npm run build ``` **GitHub Actions 自动发布:** ```yaml env: - AppVersion: ${{ github.ref_name }} SimApiVersion: ${{ github.ref_name }} run: npm run build ``` @@ -142,9 +134,10 @@ run: npm run build ``` dist/ -├── index.mjs # core ESM (5.59 KB) -├── index.cjs # core CJS (4.15 KB) -├── pinia.mjs # pinia ESM (6.81 KB) +├── index.mjs # core ESM +├── index.cjs # core CJS +├── pinia.mjs # pinia ESM +├── pinia.cjs # pinia CJS ├── simapi.core.d.ts ├── simapi.pinia.d.ts └── types.d.ts @@ -155,7 +148,7 @@ dist/ ## package.json exports ```json -".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs", "types": "./dist/simapi.core.d.ts" } +".": { "import": "./dist/index.mjs", "types": "./dist/simapi.core.d.ts" } "./pinia": { "import": "./dist/pinia.mjs", "types": "./dist/simapi.pinia.d.ts" } ``` @@ -169,4 +162,5 @@ dist/ - **无 Cookie**:所有请求不发送 Cookie,Token 通过请求头传递 - **零依赖**:不需要安装 axios,使用原生 fetch - 删除了 Angular 支持,如需恢复参考 git 历史 -- **版本号管理**:使用 `declare const` + Vite `define` 注入,不再需要 sed 替换脚本 +- **版本号管理**:使用 `declare const` + Vite `define` 注入 +- **AppVersion** 不注入,留给调用方自己管理 diff --git a/README.md b/README.md index f45e920..2e7a211 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ await api.query('/stats', {}, 'admin') ## autoInit — 从 window 读取配置 -支持字段:`endpoints`、`defaultEndpoint`、`debug`、`uiAppVersion`。业务回调需在代码中通过 `setBusinessCallback` 处理。 +支持字段:`endpoints`、`defaultEndpoint`、`debug`。业务回调需在代码中通过 `setBusinessCallback` 处理。 ```html