Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6d5a5dbe1 | ||
|
|
e91ddd36bf | ||
|
|
903f70b0ba |
@@ -31,7 +31,10 @@ jobs:
|
|||||||
run: npm ci
|
run: npm ci
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: npm run build -- --define=$VERSION
|
env:
|
||||||
|
AppVersion: ${{ github.ref_name }}
|
||||||
|
SimApiVersion: ${{ github.ref_name }}
|
||||||
|
run: npm run build
|
||||||
|
|
||||||
- name: Publish to npm
|
- name: Publish to npm
|
||||||
run: npm publish --access public
|
run: npm publish --access public
|
||||||
+31
-1
@@ -54,10 +54,11 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co
|
|||||||
- 改 Token 存储:替换 `localStorage` 为 `sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()`
|
- 改 Token 存储:替换 `localStorage` 为 `sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()`
|
||||||
- 改登录/登出逻辑:修改 `login()`/`logout()` 方法
|
- 改登录/登出逻辑:修改 `login()`/`logout()` 方法
|
||||||
- 改超时处理:修改 `fetchWithTimeout()` 函数
|
- 改超时处理:修改 `fetchWithTimeout()` 函数
|
||||||
|
- **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-dev`
|
||||||
|
|
||||||
**autoInit 设计约束**:
|
**autoInit 设计约束**:
|
||||||
|
|
||||||
- 仅读取 `window.simapi` 的三个顶级字段:`endpoints`、`defaultEndpoint`、`debug`
|
- 仅读取 `window.simapi` 的顶级字段:`endpoints`、`defaultEndpoint`、`debug`、`uiAppVersion`
|
||||||
- 业务回调(`businessCallback`/`responseCallback`)不支持从 window 读取,必须在代码中通过 `setBusinessCallback` 注册
|
- 业务回调(`businessCallback`/`responseCallback`)不支持从 window 读取,必须在代码中通过 `setBusinessCallback` 注册
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -105,6 +106,34 @@ npm run build
|
|||||||
→ tsc -p tsconfig.build.json 输出 *.d.ts 类型声明
|
→ tsc -p tsconfig.build.json 输出 *.d.ts 类型声明
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**版本号注入:**
|
||||||
|
|
||||||
|
版本号通过 `vite.core.config.ts` 的 `define` 配置注入,从环境变量读取:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
define: {
|
||||||
|
'AppVersion': JSON.stringify(process.env.AppVersion || process.env.npm_config_AppVersion || '0.0.0-develop'),
|
||||||
|
'SimApiVersion': JSON.stringify(process.env.SimApiVersion || process.env.npm_config_SimApiVersion || '0.0.0-develop'),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**本地构建示例:**
|
||||||
|
```bash
|
||||||
|
# Windows PowerShell
|
||||||
|
$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build
|
||||||
|
|
||||||
|
# Linux/Mac
|
||||||
|
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
**GitHub Actions 自动发布:**
|
||||||
|
```yaml
|
||||||
|
env:
|
||||||
|
AppVersion: ${{ github.ref_name }}
|
||||||
|
SimApiVersion: ${{ github.ref_name }}
|
||||||
|
run: npm run build
|
||||||
|
```
|
||||||
|
|
||||||
**dist 输出是平铺的**,core 和 pinia 的编译产物全部在同一目录:
|
**dist 输出是平铺的**,core 和 pinia 的编译产物全部在同一目录:
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -136,3 +165,4 @@ dist/
|
|||||||
- **无 Cookie**:所有请求不发送 Cookie,Token 通过请求头传递
|
- **无 Cookie**:所有请求不发送 Cookie,Token 通过请求头传递
|
||||||
- **零依赖**:不需要安装 axios,使用原生 fetch
|
- **零依赖**:不需要安装 axios,使用原生 fetch
|
||||||
- 删除了 Angular 支持,如需恢复参考 git 历史
|
- 删除了 Angular 支持,如需恢复参考 git 历史
|
||||||
|
- **版本号管理**:使用 `declare const` + Vite `define` 注入,不再需要 sed 替换脚本
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ await api.query('/stats', {}, 'admin')
|
|||||||
|
|
||||||
## autoInit — 从 window 读取配置
|
## autoInit — 从 window 读取配置
|
||||||
|
|
||||||
仅支持三个字段:`endpoints`、`defaultEndpoint`、`debug`。业务回调需在代码中通过 `setBusinessCallback` 处理。
|
支持字段:`endpoints`、`defaultEndpoint`、`debug`、`uiAppVersion`。业务回调需在代码中通过 `setBusinessCallback` 处理。
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script>
|
<script>
|
||||||
@@ -153,6 +153,9 @@ interface SimApiOptions {
|
|||||||
/** 调试模式,默认 true */
|
/** 调试模式,默认 true */
|
||||||
debug?: boolean
|
debug?: boolean
|
||||||
|
|
||||||
|
/** UI 应用版本,如果不指定则使用库内置的版本号 */
|
||||||
|
uiAppVersion?: string
|
||||||
|
|
||||||
/** 认证相关配置 */
|
/** 认证相关配置 */
|
||||||
auth?: Partial<SimApiAuthConfig>
|
auth?: Partial<SimApiAuthConfig>
|
||||||
|
|
||||||
@@ -305,21 +308,21 @@ api.configure({
|
|||||||
| 方法/属性 | 说明 |
|
| 方法/属性 | 说明 |
|
||||||
|-----------|------|
|
|-----------|------|
|
||||||
| `configure(options)` | 批量配置(深合并) |
|
| `configure(options)` | 批量配置(深合并) |
|
||||||
| `autoInit()` | 从 `window.simapi` 读取配置(endpoints、defaultEndpoint、debug) |
|
| `autoInit()` | 从 `window.simapi` 读取配置(endpoints、defaultEndpoint、debug、uiAppVersion) |
|
||||||
| `setEndpoints(map)` | 设置端点,自动触发版本检查 |
|
| `setEndpoints(map)` | 设置端点 |
|
||||||
| `setBusinessCallback(code, fn)` | 注册业务错误码回调 |
|
| `setBusinessCallback(code, fn)` | 注册业务错误码回调 |
|
||||||
| `setDebug(debug)` | 设置调试模式 |
|
|
||||||
| `query(uri, params?, endpointKey?, headers?)` | POST 请求,返回 `Promise<SimApiBaseResponse<T>>` |
|
| `query(uri, params?, endpointKey?, headers?)` | POST 请求,返回 `Promise<SimApiBaseResponse<T>>` |
|
||||||
| `login(request)` | 登录,自动存 Token |
|
| `login(request)` | 登录,自动存 Token |
|
||||||
| `logout(url?)` | 登出,清除 Token |
|
| `logout(url?)` | 登出,清除 Token |
|
||||||
| `checkLogin(url?)` | 主动检查登录状态 |
|
| `checkLogin(url?)` | 主动检查登录状态 |
|
||||||
|
| `getVersion(endpointName?)` | 获取版本信息,返回 `Promise<SimApiVersions>` |
|
||||||
| `getToken()` | 获取 Token |
|
| `getToken()` | 获取 Token |
|
||||||
| `setToken(token)` | 手动设置 Token |
|
| `setToken(token)` | 手动设置 Token |
|
||||||
| `removeToken()` | 清除 Token |
|
| `removeToken()` | 清除 Token |
|
||||||
| `isLoggedIn` | getter,是否已登录 |
|
| `isLoggedIn` | getter,是否已登录 |
|
||||||
| `token` | getter,获取当前 Token |
|
|
||||||
| `debug` | boolean,调试模式 |
|
| `debug` | boolean,调试模式 |
|
||||||
| `versions` | 版本信息对象 |
|
| `uiAppVersion` | UI 应用版本号(可选配置) |
|
||||||
|
| `logDebug(...args)` | 日志工具(仅在 debug 模式输出) |
|
||||||
|
|
||||||
## 构建
|
## 构建
|
||||||
|
|
||||||
@@ -329,6 +332,28 @@ npm run build
|
|||||||
npm link # 本地调试
|
npm link # 本地调试
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 版本号注入
|
||||||
|
|
||||||
|
库使用 `declare const` 声明版本常量,构建时通过 Vite 的 `define` 注入版本号:
|
||||||
|
|
||||||
|
**本地构建:**
|
||||||
|
```bash
|
||||||
|
# Windows PowerShell
|
||||||
|
$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build
|
||||||
|
|
||||||
|
# Linux/Mac
|
||||||
|
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
**CI/CD 构建版本号:**
|
||||||
|
```bash
|
||||||
|
env:
|
||||||
|
AppVersion: ${VERSION}
|
||||||
|
SimApiVersion: ${VERSION}
|
||||||
|
```
|
||||||
|
|
||||||
|
如果未指定环境变量,版本号默认为 `0.0.0-develop`。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 从旧版迁移
|
## 从旧版迁移
|
||||||
|
|||||||
+1
-5
@@ -6,20 +6,17 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git@github.com:simcu/simapi-ts.git"
|
"url": "git@github.com:simcu/simapi-vue.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"simapi",
|
"simapi",
|
||||||
"vue3",
|
"vue3",
|
||||||
"pinia",
|
"pinia",
|
||||||
"rxjs",
|
|
||||||
"http",
|
"http",
|
||||||
"fetch"
|
"fetch"
|
||||||
],
|
],
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"simapi.core.ts",
|
|
||||||
"simapi.pinia.ts",
|
|
||||||
"README.md",
|
"README.md",
|
||||||
"package.json"
|
"package.json"
|
||||||
],
|
],
|
||||||
@@ -49,7 +46,6 @@
|
|||||||
"tslib": "^2.3.0"
|
"tslib": "^2.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@vitejs/plugin-vue": "^5.0.0",
|
|
||||||
"pinia": "^2.2.0",
|
"pinia": "^2.2.0",
|
||||||
"typescript": "~5.9.3",
|
"typescript": "~5.9.3",
|
||||||
"vite": "^5.0.0",
|
"vite": "^5.0.0",
|
||||||
|
|||||||
+2
-9
@@ -11,8 +11,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AppVersion,
|
|
||||||
SimApiVersion,
|
|
||||||
type SimApiVersions,
|
type SimApiVersions,
|
||||||
type SimApiAuthConfig,
|
type SimApiAuthConfig,
|
||||||
type SimApiApiConfig,
|
type SimApiApiConfig,
|
||||||
@@ -20,7 +18,6 @@ import {
|
|||||||
type SimApiBaseResponse,
|
type SimApiBaseResponse,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
|
||||||
export { SimApiVersion, AppVersion } from './types'
|
|
||||||
export type {
|
export type {
|
||||||
SimApiVersions,
|
SimApiVersions,
|
||||||
SimApiAuthConfig,
|
SimApiAuthConfig,
|
||||||
@@ -29,6 +26,8 @@ export type {
|
|||||||
SimApiBaseResponse,
|
SimApiBaseResponse,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
|
||||||
|
declare const AppVersion: string;
|
||||||
|
declare const SimApiVersion: string;
|
||||||
// ── Helper: Fetch with Timeout ────────────────────────────────────────
|
// ── Helper: Fetch with Timeout ────────────────────────────────────────
|
||||||
|
|
||||||
function fetchWithTimeout(
|
function fetchWithTimeout(
|
||||||
@@ -120,9 +119,6 @@ export class SimApiCore {
|
|||||||
if (config.debug !== undefined) {
|
if (config.debug !== undefined) {
|
||||||
this.debug = config.debug
|
this.debug = config.debug
|
||||||
}
|
}
|
||||||
if (config.uiAppVersion !== undefined) {
|
|
||||||
this.uiAppVersion = config.uiAppVersion
|
|
||||||
}
|
|
||||||
if (config.endpoints) {
|
if (config.endpoints) {
|
||||||
this.api.endpoints = { ...this.api.endpoints, ...config.endpoints }
|
this.api.endpoints = { ...this.api.endpoints, ...config.endpoints }
|
||||||
}
|
}
|
||||||
@@ -135,9 +131,6 @@ export class SimApiCore {
|
|||||||
if (options.debug !== undefined) {
|
if (options.debug !== undefined) {
|
||||||
this.debug = options.debug
|
this.debug = options.debug
|
||||||
}
|
}
|
||||||
if (options.uiAppVersion !== undefined) {
|
|
||||||
this.uiAppVersion = options.uiAppVersion
|
|
||||||
}
|
|
||||||
if (options.auth) {
|
if (options.auth) {
|
||||||
this.auth = { ...this.auth, ...options.auth }
|
this.auth = { ...this.auth, ...options.auth }
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-5
@@ -2,10 +2,6 @@
|
|||||||
* SimApi 类型定义
|
* SimApi 类型定义
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 版本号占位符,构建时由 GitHub Action 替换
|
|
||||||
export const SimApiVersion = '0.0.0-version-placeholder'
|
|
||||||
export const AppVersion = '0.0.0-version-placeholder'
|
|
||||||
|
|
||||||
/** 版本信息 */
|
/** 版本信息 */
|
||||||
export interface SimApiVersions {
|
export interface SimApiVersions {
|
||||||
uiApp: string
|
uiApp: string
|
||||||
@@ -56,7 +52,7 @@ export interface SimApiApiConfig {
|
|||||||
/** SimApi 完整配置 */
|
/** SimApi 完整配置 */
|
||||||
export interface SimApiOptions {
|
export interface SimApiOptions {
|
||||||
debug?: boolean
|
debug?: boolean
|
||||||
/** UI 应用版本,如果不指定则使用库内置的占位符版本 */
|
/** UI 应用版本,如果不指定则使用库内置的版本号 */
|
||||||
uiAppVersion?: string
|
uiAppVersion?: string
|
||||||
auth?: Partial<SimApiAuthConfig>
|
auth?: Partial<SimApiAuthConfig>
|
||||||
api?: Partial<SimApiApiConfig>
|
api?: Partial<SimApiApiConfig>
|
||||||
|
|||||||
+10
-2
@@ -1,6 +1,6 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig(({ mode }) => ({
|
||||||
build: {
|
build: {
|
||||||
outDir: 'dist',
|
outDir: 'dist',
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
@@ -13,5 +13,13 @@ export default defineConfig({
|
|||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
// 不再需要 external,使用原生 fetch
|
// 不再需要 external,使用原生 fetch
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
define: {
|
||||||
|
// 版本号从环境变量读取,构建时传入:
|
||||||
|
// npm run build:core -- --AppVersion=1.2.3 --SimApiVersion=2.3.4
|
||||||
|
// 或:
|
||||||
|
// AppVersion=1.2.3 SimApiVersion=2.3.4 npm run build:core
|
||||||
|
'AppVersion': JSON.stringify(process.env.AppVersion || process.env.npm_config_AppVersion || '0.0.0-develop'),
|
||||||
|
'SimApiVersion': JSON.stringify(process.env.SimApiVersion || process.env.npm_config_SimApiVersion || '0.0.0-develop'),
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user