Compare commits

..
4 Commits
Author SHA1 Message Date
xrain 29d3705073 fix ci
Publish to npm / publish (push) Failing after 11s
2026-04-08 01:52:19 +08:00
xrain df0d7a4fe3 fix ci
Publish to npm / publish (push) Failing after 10s
2026-04-08 01:51:14 +08:00
xrain d7aa0f171d fix ci
Publish to npm / publish (push) Failing after 12s
2026-04-08 01:45:53 +08:00
xrain b6d5a5dbe1 fix ci
Publish to npm / publish (push) Failing after 12s
2026-04-08 01:33:23 +08:00
7 changed files with 74 additions and 28 deletions
+3 -1
View File
@@ -31,7 +31,9 @@ jobs:
run: npm ci run: npm ci
- name: Build - name: Build
run: npm run build -- --define=$VERSION env:
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
+33 -6
View File
@@ -54,7 +54,7 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co
- 改 Token 存储:替换 `localStorage``sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()` - 改 Token 存储:替换 `localStorage``sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()`
- 改登录/登出逻辑:修改 `login()`/`logout()` 方法 - 改登录/登出逻辑:修改 `login()`/`logout()` 方法
- 改超时处理:修改 `fetchWithTimeout()` 函数 - 改超时处理:修改 `fetchWithTimeout()` 函数
- 版本管理:版本号在库构建时通过 `scripts/replace-version.js` 注入,源码中使用占位符 `0.0.0-version-placeholder` - **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-dev`
**autoInit 设计约束** **autoInit 设计约束**
@@ -104,11 +104,38 @@ npm run build
→ vite build vite.core.config.ts 输出 dist/index.mjs / index.cjs → vite build vite.core.config.ts 输出 dist/index.mjs / index.cjs
→ vite build vite.pinia.config.ts 输出 dist/pinia.mjs → vite build vite.pinia.config.ts 输出 dist/pinia.mjs
→ tsc -p tsconfig.build.json 输出 *.d.ts 类型声明 → tsc -p tsconfig.build.json 输出 *.d.ts 类型声明
```
npm run build:version **版本号注入:**
→ node scripts/replace-version.js 替换版本占位符
→ npm run build 构建 版本号通过 `vite.core.config.ts``define` 配置注入,从 npm config 读取:
→ node scripts/restore-version.js 恢复占位符
```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
# 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 的编译产物全部在同一目录:
@@ -142,4 +169,4 @@ dist/
- **无 Cookie**:所有请求不发送 CookieToken 通过请求头传递 - **无 Cookie**:所有请求不发送 CookieToken 通过请求头传递
- **零依赖**:不需要安装 axios,使用原生 fetch - **零依赖**:不需要安装 axios,使用原生 fetch
- 删除了 Angular 支持,如需恢复参考 git 历史 - 删除了 Angular 支持,如需恢复参考 git 历史
- 版本号通过构建脚本注入,GitHub Actions 发布时会自动替换 `0.0.0-version-placeholder` 占位符 - **版本号管理**:使用 `declare const` + Vite `define` 注入,不再需要 sed 替换脚本
+26
View File
@@ -332,6 +332,32 @@ npm run build
npm link # 本地调试 npm link # 本地调试
``` ```
### 版本号注入
库使用 `declare const` 声明版本常量,构建时通过 Vite 的 `define` 注入版本号:
**本地构建:**
```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
# Linux/Mac
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
```
**CI/CD 构建版本号:**
```bash
env:
AppVersion: ${{ github.ref_name }}
SimApiVersion: ${{ github.ref_name }}
```
如果未指定环境变量,版本号默认为 `0.0.0-develop`
--- ---
## 从旧版迁移 ## 从旧版迁移
-1
View File
@@ -38,7 +38,6 @@
"build": "npm run build:core && npm run build:pinia && npm run types", "build": "npm run build:core && npm run build:pinia && npm run types",
"build:core": "vite build --config vite.core.config.ts", "build:core": "vite build --config vite.core.config.ts",
"build:pinia": "vite build --config vite.pinia.config.ts", "build:pinia": "vite build --config vite.pinia.config.ts",
"build:version": "node scripts/replace-version.js && npm run build && node scripts/restore-version.js",
"dev": "vite build --config vite.core.config.ts --watch", "dev": "vite build --config vite.core.config.ts --watch",
"types": "tsc --declaration --emitDeclarationOnly --project tsconfig.build.json", "types": "tsc --declaration --emitDeclarationOnly --project tsconfig.build.json",
"lint": "tsc --noEmit" "lint": "tsc --noEmit"
+6 -15
View File
@@ -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(
@@ -78,8 +77,6 @@ async function fetchPost<T = any>(
export class SimApiCore { export class SimApiCore {
debug: boolean = true debug: boolean = true
uiAppVersion?: string
auth: SimApiAuthConfig = { auth: SimApiAuthConfig = {
token_name: 'simapi-auth-token', token_name: 'simapi-auth-token',
check_url: '/auth/check', check_url: '/auth/check',
@@ -120,9 +117,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 +129,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 }
} }
@@ -217,8 +208,8 @@ export class SimApiCore {
if (resp?.data) { if (resp?.data) {
const d = resp.data const d = resp.data
const versions: SimApiVersions = { const versions: SimApiVersions = {
uiApp: this.uiAppVersion ?? AppVersion, uiApp: AppVersion ?? "0.0.0-develop",
uiSimApi: SimApiVersion, uiSimApi: SimApiVersion ?? "0.0.0-develop",
apiApp: d.App?.split('+')[0] ?? '0.0.0', apiApp: d.App?.split('+')[0] ?? '0.0.0',
apiSimApi: d.SimApi?.split('+')[0] ?? '0.0.0', apiSimApi: d.SimApi?.split('+')[0] ?? '0.0.0',
apiAppFull: d.App ?? '0.0.0', apiAppFull: d.App ?? '0.0.0',
@@ -233,8 +224,8 @@ export class SimApiCore {
// 版本获取失败返回默认值 // 版本获取失败返回默认值
} }
return { return {
uiApp: AppVersion, uiApp: "0.0.0-develop",
uiSimApi: SimApiVersion, uiSimApi: "0.0.0-develop",
apiApp: '0.0.0', apiApp: '0.0.0',
apiSimApi: '0.0.0', apiSimApi: '0.0.0',
apiAppFull: '0.0.0', apiAppFull: '0.0.0',
+1 -5
View File
@@ -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>
+5
View File
@@ -13,5 +13,10 @@ export default defineConfig({
rollupOptions: { rollupOptions: {
// 不再需要 external,使用原生 fetch // 不再需要 external,使用原生 fetch
} }
},
define: {
// SimApiVersion 由构建注入,AppVersion 留给调用方 APP 注入
// 使用方式: npm run build -- --SimApiVersion=1.0.0
'SimApiVersion': JSON.stringify(process.env.npm_config_SimApiVersion || '0.0.0-develop'),
} }
}) })