Skip to content

Commit ae742d4

Browse files
committed
修正无用的内容
1 parent 29d3705 commit ae742d4

10 files changed

Lines changed: 415 additions & 134 deletions

AICONTEXT.md

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ simapi-vue/
1313
│ ├── simapi.core.ts # 核心类 SimApiCore,零框架依赖
1414
│ └── simapi.pinia.ts # Pinia Store,Vue3 适配层
1515
├── dist/ # 构建产物
16-
├── package.json # exports: "/" → core, "/pinia" → vue
17-
├── vite.core.config.ts # 构建 core → dist/index.mjs/cjs
18-
├── vite.pinia.config.ts # 构建 pinia → dist/pinia.mjs
16+
├── package.json # exports: "." → core, "/pinia" → vue
17+
├── vite.config.ts # 统一构建配置,生成 .mjs 和 .cjs
1918
└── tsconfig.build.json # tsc 生成类型声明
2019
```
2120

@@ -43,22 +42,21 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co
4342
- **无 Cookie**:所有请求使用 `credentials: 'omit'`,避免 CORS 问题
4443
- **Token 传递**:通过请求头 `Token` 传递认证信息
4544
- **超时控制**:通过 `fetchWithTimeout` 辅助函数实现
46-
- `setEndpoints()` 会自动调用 `fetchVersions()` 查询后端版本(`/versions`),打印到控制台
47-
- `handleResponse()` 在响应非 200 时触发 `businessCallback`,不抛出异常
4845
- `query()` 抛出异常的只有网络/HTTP 错误,业务错误码通过回调处理
4946
- `isLoggedIn` 是 getter,基于 localStorage 中的 token 判断
47+
- **版本号**`getVersion()` 返回的版本信息中,`uiSimApi` 由构建时注入的 `SimApiVersion` 填充,`uiApp` 由调用方的 `AppVersion` 填充(未注入时为 `0.0.0-develop`
5048

5149
**修改建议**
5250

5351
- 改请求方法(GET/PUT/DELETE):在 `fetchPost()` 内新增 `method` 参数分支,或新增 `fetchGet()`/`fetchPut()` 方法
5452
- 改 Token 存储:替换 `localStorage``sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()`
5553
- 改登录/登出逻辑:修改 `login()`/`logout()` 方法
5654
- 改超时处理:修改 `fetchWithTimeout()` 函数
57-
- **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-dev`
55+
- **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-develop`
5856

5957
**autoInit 设计约束**
6058

61-
- 仅读取 `window.simapi` 的顶级字段:`endpoints``defaultEndpoint``debug``uiAppVersion`
59+
- 仅读取 `window.simapi` 的顶级字段:`endpoints``defaultEndpoint``debug`
6260
- 业务回调(`businessCallback`/`responseCallback`)不支持从 window 读取,必须在代码中通过 `setBusinessCallback` 注册
6361

6462
---
@@ -69,7 +67,7 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co
6967

7068
**关键设计**
7169

72-
- **无独立状态**:state 中只有一个 `_core` 实例,不维护 `debug``versions``token` 等独立数据
70+
- **无独立状态**:state 中只有一个 `_core` 实例,不维护 `debug``token` 等独立数据
7371
- **单例 Core**:在 store state 中实例化 `SimApiCore`,整个应用共享一个实例
7472
- **纯代理映射**:所有 getters 直接映射到 `this._core` 的属性,所有 actions 直接调用 `this._core` 的方法
7573
- **响应式**:通过 Pinia 的响应式系统,当 core 状态变化时自动更新
@@ -92,7 +90,7 @@ api.configure({
9290
})
9391

9492
// 所有方法与 SimApiCore 完全一致
95-
await api.query('/users/list', { page:1 })
93+
await api.query('/users/list', { page: 1 })
9694
```
9795

9896
---
@@ -101,39 +99,33 @@ await api.query('/users/list', { page:1 })
10199

102100
```
103101
npm run build
104-
→ vite build vite.core.config.ts 输出 dist/index.mjs / index.cjs
105-
→ vite build vite.pinia.config.ts 输出 dist/pinia.mjs
106-
→ tsc -p tsconfig.build.json 输出 *.d.ts 类型声明
102+
→ vite build 输出 dist/index.mjs / index.cjs / pinia.mjs / pinia.cjs
103+
→ tsc -p tsconfig.build.json 输出 *.d.ts 类型声明
107104
```
108105

109106
**版本号注入:**
110107

111-
版本号通过 `vite.core.config.ts``define` 配置注入,从 npm config 读取:
108+
版本号通过 `vite.config.ts``define` 配置注入,从 npm config 读取:
112109

113110
```typescript
114111
define: {
115-
'AppVersion': JSON.stringify(process.env.npm_config_AppVersion || '0.0.0-develop'),
116112
'SimApiVersion': JSON.stringify(process.env.npm_config_SimApiVersion || '0.0.0-develop'),
117113
}
118114
```
119115

120116
**本地构建示例:**
121117
```bash
122-
# 通过 npm config 传递(推荐)
123-
npm run build -- --AppVersion=1.0.0 --SimApiVersion=1.0.0
124-
125-
# 或通过环境变量
118+
# 通过环境变量
126119
# Windows PowerShell
127-
$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build
120+
$env:npm_config_SimApiVersion='1.0.0'; npm run build
128121

129122
# Linux/Mac
130-
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
123+
npm_config_SimApiVersion=1.0.0 npm run build
131124
```
132125

133126
**GitHub Actions 自动发布:**
134127
```yaml
135128
env:
136-
AppVersion: ${{ github.ref_name }}
137129
SimApiVersion: ${{ github.ref_name }}
138130
run: npm run build
139131
```
@@ -142,9 +134,10 @@ run: npm run build
142134
143135
```
144136
dist/
145-
├── index.mjs # core ESM (5.59 KB)
146-
├── index.cjs # core CJS (4.15 KB)
147-
├── pinia.mjs # pinia ESM (6.81 KB)
137+
├── index.mjs # core ESM
138+
├── index.cjs # core CJS
139+
├── pinia.mjs # pinia ESM
140+
├── pinia.cjs # pinia CJS
148141
├── simapi.core.d.ts
149142
├── simapi.pinia.d.ts
150143
└── types.d.ts
@@ -155,7 +148,7 @@ dist/
155148
## package.json exports
156149

157150
```json
158-
".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs", "types": "./dist/simapi.core.d.ts" }
151+
".": { "import": "./dist/index.mjs", "types": "./dist/simapi.core.d.ts" }
159152
"./pinia": { "import": "./dist/pinia.mjs", "types": "./dist/simapi.pinia.d.ts" }
160153
```
161154

@@ -169,4 +162,5 @@ dist/
169162
- **无 Cookie**:所有请求不发送 Cookie,Token 通过请求头传递
170163
- **零依赖**:不需要安装 axios,使用原生 fetch
171164
- 删除了 Angular 支持,如需恢复参考 git 历史
172-
- **版本号管理**:使用 `declare const` + Vite `define` 注入,不再需要 sed 替换脚本
165+
- **版本号管理**:使用 `declare const` + Vite `define` 注入
166+
- **AppVersion** 不注入,留给调用方自己管理

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ await api.query('/stats', {}, 'admin')
105105

106106
## autoInit — 从 window 读取配置
107107

108-
支持字段:`endpoints``defaultEndpoint``debug``uiAppVersion`。业务回调需在代码中通过 `setBusinessCallback` 处理。
108+
支持字段:`endpoints``defaultEndpoint``debug`。业务回调需在代码中通过 `setBusinessCallback` 处理。
109109

110110
```html
111111
<script>
@@ -153,9 +153,6 @@ interface SimApiOptions {
153153
/** 调试模式,默认 true */
154154
debug?: boolean
155155

156-
/** UI 应用版本,如果不指定则使用库内置的版本号 */
157-
uiAppVersion?: string
158-
159156
/** 认证相关配置 */
160157
auth?: Partial<SimApiAuthConfig>
161158

@@ -308,7 +305,7 @@ api.configure({
308305
| 方法/属性 | 说明 |
309306
|-----------|------|
310307
| `configure(options)` | 批量配置(深合并) |
311-
| `autoInit()` |`window.simapi` 读取配置(endpoints、defaultEndpoint、debug、uiAppVersion|
308+
| `autoInit()` |`window.simapi` 读取配置(endpoints、defaultEndpoint、debug) |
312309
| `setEndpoints(map)` | 设置端点 |
313310
| `setBusinessCallback(code, fn)` | 注册业务错误码回调 |
314311
| `query(uri, params?, endpointKey?, headers?)` | POST 请求,返回 `Promise<SimApiBaseResponse<T>>` |
@@ -321,7 +318,6 @@ api.configure({
321318
| `removeToken()` | 清除 Token |
322319
| `isLoggedIn` | getter,是否已登录 |
323320
| `debug` | boolean,调试模式 |
324-
| `uiAppVersion` | UI 应用版本号(可选配置) |
325321
| `logDebug(...args)` | 日志工具(仅在 debug 模式输出) |
326322

327323
## 构建
@@ -334,29 +330,42 @@ npm link # 本地调试
334330

335331
### 版本号注入
336332

337-
库使用 `declare const` 声明版本常量,构建时通过 Vite 的 `define` 注入版本号:
333+
库使用 `declare const` 声明版本常量构建时通过 Vite 的 `define` 注入版本号
338334

339-
**本地构建:**
340-
```bash
341-
# 通过 npm config 传递
342-
npm run build -- --AppVersion=1.0.0 --SimApiVersion=1.0.0
335+
**注意:**
336+
- **SimApiVersion**:由 simapi 库构建时注入
337+
- **AppVersion**:不注入,留给调用方 APP 注入
338+
339+
**本地构建:**
343340

344-
# 或设置环境变量后构建
341+
```bash
342+
# 通过环境变量注入 SimApiVersion
345343
# Windows PowerShell
346-
$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build
344+
$env:npm_config_SimApiVersion='1.0.0'; npm run build
347345

348346
# Linux/Mac
349-
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
347+
npm_config_SimApiVersion=1.0.0 npm run build
350348
```
351349

352-
**CI/CD 构建版本号:**
353-
```bash
350+
未指定时默认为 `0.0.0-develop`
351+
352+
**CI/CD 构建版本号:**
353+
354+
```yaml
354355
env:
355-
AppVersion: ${{ github.ref_name }}
356356
SimApiVersion: ${{ github.ref_name }}
357+
run: npm run build
357358
```
358359
359-
如果未指定环境变量,版本号默认为 `0.0.0-develop`
360+
**调用方注入 AppVersion:**
361+
362+
调用方在自己的 `vite.config.ts` 中添加:
363+
364+
```typescript
365+
define: {
366+
'AppVersion': JSON.stringify('1.0.0')
367+
}
368+
```
360369

361370
---
362371

0 commit comments

Comments
 (0)